blob: 6b1b9a748c6bca7bb89ceb456e45a5fddaf1ce2d [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
Evan Stadeecfc48c2019-06-17 21:35:237#include "ash/public/cpp/notifier_metadata.h"
hironod36c21d2016-06-21 01:25:228#include "base/strings/utf_string_conversions.h"
khmel8c1f6622017-05-11 19:14:509#include "chrome/browser/extensions/chrome_app_icon_loader.h"
hironod36c21d2016-06-21 01:25:2210#include "chrome/browser/notifications/notifier_state_tracker.h"
11#include "chrome/browser/notifications/notifier_state_tracker_factory.h"
12#include "chrome/browser/profiles/profile.h"
Evan Stadefeec2d112017-10-19 03:24:5313#include "chrome/common/extensions/api/notifications.h"
14#include "extensions/browser/event_router.h"
hironod36c21d2016-06-21 01:25:2215#include "extensions/common/constants.h"
16#include "extensions/common/extension_set.h"
17#include "extensions/common/permissions/api_permission.h"
18#include "extensions/common/permissions/permissions_data.h"
Evan Stade889ce4712018-01-28 15:26:2619#include "ui/message_center/public/cpp/notifier_id.h"
hironod36c21d2016-06-21 01:25:2220
Evan Staded6d837f2017-09-29 02:51:0221ExtensionNotifierController::ExtensionNotifierController(Observer* observer)
hironod36c21d2016-06-21 01:25:2222 : observer_(observer) {}
23
Evan Staded6d837f2017-09-29 02:51:0224ExtensionNotifierController::~ExtensionNotifierController() {}
hironod36c21d2016-06-21 01:25:2225
Evan Stadeecfc48c2019-06-17 21:35:2326std::vector<ash::NotifierMetadata> ExtensionNotifierController::GetNotifierList(
27 Profile* profile) {
28 std::vector<ash::NotifierMetadata> notifiers;
hironod36c21d2016-06-21 01:25:2229 const extensions::ExtensionSet& extension_set =
30 extensions::ExtensionRegistry::Get(profile)->enabled_extensions();
31 // The extension icon size has to be 32x32 at least to load bigger icons if
32 // the icon doesn't exist for the specified size, and in that case it falls
33 // back to the default icon. The fetched icon will be resized in the
34 // settings dialog. See chrome/browser/extensions/extension_icon_image.cc
35 // and crbug.com/222931
khmel8c1f6622017-05-11 19:14:5036 app_icon_loader_.reset(new extensions::ChromeAppIconLoader(
hironod36c21d2016-06-21 01:25:2237 profile, extension_misc::EXTENSION_ICON_SMALL, this));
38 for (extensions::ExtensionSet::const_iterator iter = extension_set.begin();
39 iter != extension_set.end(); ++iter) {
40 const extensions::Extension* extension = iter->get();
41 if (!extension->permissions_data()->HasAPIPermission(
42 extensions::APIPermission::kNotifications)) {
43 continue;
44 }
45
46 // Hosted apps are no longer able to affect the notifications permission
47 // state for web notifications.
48 // TODO(dewittj): Deprecate the 'notifications' permission for hosted
49 // apps.
50 if (extension->is_hosted_app())
51 continue;
52
53 message_center::NotifierId notifier_id(
Daniel Chenga925cbb52018-11-06 21:52:3554 message_center::NotifierType::APPLICATION, extension->id());
hironod36c21d2016-06-21 01:25:2255 NotifierStateTracker* const notifier_state_tracker =
56 NotifierStateTrackerFactory::GetForProfile(profile);
Evan Stadeecfc48c2019-06-17 21:35:2357 notifiers.emplace_back(
hironod36c21d2016-06-21 01:25:2258 notifier_id, base::UTF8ToUTF16(extension->name()),
Evan Stade55575d82017-11-02 00:52:3659 notifier_state_tracker->IsNotifierEnabled(notifier_id),
Evan Stadeecfc48c2019-06-17 21:35:2360 false /* enforced */, gfx::ImageSkia());
hironod36c21d2016-06-21 01:25:2261 app_icon_loader_->FetchImage(extension->id());
62 }
63
Evan Stadeecfc48c2019-06-17 21:35:2364 return notifiers;
hironod36c21d2016-06-21 01:25:2265}
66
Evan Staded6d837f2017-09-29 02:51:0267void ExtensionNotifierController::SetNotifierEnabled(
hironod36c21d2016-06-21 01:25:2268 Profile* profile,
Evan Stade844ad7f2017-09-20 18:05:3169 const message_center::NotifierId& notifier_id,
hironod36c21d2016-06-21 01:25:2270 bool enabled) {
71 NotifierStateTrackerFactory::GetForProfile(profile)->SetNotifierEnabled(
Evan Stade844ad7f2017-09-20 18:05:3172 notifier_id, enabled);
73 observer_->OnNotifierEnabledChanged(notifier_id, enabled);
hironod36c21d2016-06-21 01:25:2274}
75
Evan Staded6d837f2017-09-29 02:51:0276void ExtensionNotifierController::OnAppImageUpdated(
77 const std::string& id,
78 const gfx::ImageSkia& image) {
hironod36c21d2016-06-21 01:25:2279 observer_->OnIconImageUpdated(
Daniel Chenga925cbb52018-11-06 21:52:3580 message_center::NotifierId(message_center::NotifierType::APPLICATION, id),
Evan Stade55575d82017-11-02 00:52:3681 image);
hironod36c21d2016-06-21 01:25:2282}