blob: 1ac06bfe134088c846521288c73cfdb623088bbb [file] [log] [blame]
hironod36c21d2016-06-21 01:25:221// Copyright (c) 2016 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Evan Staded6d837f2017-09-29 02:51:025#include "chrome/browser/notifications/extension_notifier_controller.h"
hironod36c21d2016-06-21 01:25:226
7#include "base/strings/utf_string_conversions.h"
khmel8c1f6622017-05-11 19:14:508#include "chrome/browser/extensions/chrome_app_icon_loader.h"
hironod36c21d2016-06-21 01:25:229#include "chrome/browser/notifications/notifier_state_tracker.h"
10#include "chrome/browser/notifications/notifier_state_tracker_factory.h"
11#include "chrome/browser/profiles/profile.h"
12#include "extensions/common/constants.h"
13#include "extensions/common/extension_set.h"
14#include "extensions/common/permissions/api_permission.h"
15#include "extensions/common/permissions/permissions_data.h"
16#include "ui/message_center/notifier_settings.h"
17
Evan Staded6d837f2017-09-29 02:51:0218ExtensionNotifierController::ExtensionNotifierController(Observer* observer)
hironod36c21d2016-06-21 01:25:2219 : observer_(observer) {}
20
Evan Staded6d837f2017-09-29 02:51:0221ExtensionNotifierController::~ExtensionNotifierController() {}
hironod36c21d2016-06-21 01:25:2222
23std::vector<std::unique_ptr<message_center::Notifier>>
Evan Staded6d837f2017-09-29 02:51:0224ExtensionNotifierController::GetNotifierList(Profile* profile) {
hironod36c21d2016-06-21 01:25:2225 std::vector<std::unique_ptr<message_center::Notifier>> notifiers;
26 const extensions::ExtensionSet& extension_set =
27 extensions::ExtensionRegistry::Get(profile)->enabled_extensions();
28 // The extension icon size has to be 32x32 at least to load bigger icons if
29 // the icon doesn't exist for the specified size, and in that case it falls
30 // back to the default icon. The fetched icon will be resized in the
31 // settings dialog. See chrome/browser/extensions/extension_icon_image.cc
32 // and crbug.com/222931
khmel8c1f6622017-05-11 19:14:5033 app_icon_loader_.reset(new extensions::ChromeAppIconLoader(
hironod36c21d2016-06-21 01:25:2234 profile, extension_misc::EXTENSION_ICON_SMALL, this));
35 for (extensions::ExtensionSet::const_iterator iter = extension_set.begin();
36 iter != extension_set.end(); ++iter) {
37 const extensions::Extension* extension = iter->get();
38 if (!extension->permissions_data()->HasAPIPermission(
39 extensions::APIPermission::kNotifications)) {
40 continue;
41 }
42
43 // Hosted apps are no longer able to affect the notifications permission
44 // state for web notifications.
45 // TODO(dewittj): Deprecate the 'notifications' permission for hosted
46 // apps.
47 if (extension->is_hosted_app())
48 continue;
49
50 message_center::NotifierId notifier_id(
51 message_center::NotifierId::APPLICATION, extension->id());
52 NotifierStateTracker* const notifier_state_tracker =
53 NotifierStateTrackerFactory::GetForProfile(profile);
54 notifiers.emplace_back(new message_center::Notifier(
55 notifier_id, base::UTF8ToUTF16(extension->name()),
56 notifier_state_tracker->IsNotifierEnabled(notifier_id)));
57 app_icon_loader_->FetchImage(extension->id());
58 }
59
60 return notifiers;
61}
62
Evan Staded6d837f2017-09-29 02:51:0263void ExtensionNotifierController::SetNotifierEnabled(
hironod36c21d2016-06-21 01:25:2264 Profile* profile,
Evan Stade844ad7f2017-09-20 18:05:3165 const message_center::NotifierId& notifier_id,
hironod36c21d2016-06-21 01:25:2266 bool enabled) {
67 NotifierStateTrackerFactory::GetForProfile(profile)->SetNotifierEnabled(
Evan Stade844ad7f2017-09-20 18:05:3168 notifier_id, enabled);
69 observer_->OnNotifierEnabledChanged(notifier_id, enabled);
hironod36c21d2016-06-21 01:25:2270}
71
Evan Staded6d837f2017-09-29 02:51:0272void ExtensionNotifierController::OnAppImageUpdated(
73 const std::string& id,
74 const gfx::ImageSkia& image) {
hironod36c21d2016-06-21 01:25:2275 observer_->OnIconImageUpdated(
76 message_center::NotifierId(message_center::NotifierId::APPLICATION, id),
77 gfx::Image(image));
78}