blob: 9c0e5759ad1ebc5c443f81327765ad71b7e16913 [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"
Evan Stadefeec2d112017-10-19 03:24:5312#include "chrome/common/extensions/api/notifications.h"
13#include "extensions/browser/event_router.h"
hironod36c21d2016-06-21 01:25:2214#include "extensions/common/constants.h"
15#include "extensions/common/extension_set.h"
16#include "extensions/common/permissions/api_permission.h"
17#include "extensions/common/permissions/permissions_data.h"
Evan Stade889ce4712018-01-28 15:26:2618#include "ui/message_center/public/cpp/notifier_id.h"
hironod36c21d2016-06-21 01:25:2219
Evan Staded6d837f2017-09-29 02:51:0220ExtensionNotifierController::ExtensionNotifierController(Observer* observer)
hironod36c21d2016-06-21 01:25:2221 : observer_(observer) {}
22
Evan Staded6d837f2017-09-29 02:51:0223ExtensionNotifierController::~ExtensionNotifierController() {}
hironod36c21d2016-06-21 01:25:2224
Ian Barkley-Yeungb2893472019-06-15 01:44:0625std::vector<ash::mojom::NotifierUiDataPtr>
26ExtensionNotifierController::GetNotifierList(Profile* profile) {
27 std::vector<ash::mojom::NotifierUiDataPtr> ui_data;
hironod36c21d2016-06-21 01:25:2228 const extensions::ExtensionSet& extension_set =
29 extensions::ExtensionRegistry::Get(profile)->enabled_extensions();
30 // The extension icon size has to be 32x32 at least to load bigger icons if
31 // the icon doesn't exist for the specified size, and in that case it falls
32 // back to the default icon. The fetched icon will be resized in the
33 // settings dialog. See chrome/browser/extensions/extension_icon_image.cc
34 // and crbug.com/222931
khmel8c1f6622017-05-11 19:14:5035 app_icon_loader_.reset(new extensions::ChromeAppIconLoader(
hironod36c21d2016-06-21 01:25:2236 profile, extension_misc::EXTENSION_ICON_SMALL, this));
37 for (extensions::ExtensionSet::const_iterator iter = extension_set.begin();
38 iter != extension_set.end(); ++iter) {
39 const extensions::Extension* extension = iter->get();
40 if (!extension->permissions_data()->HasAPIPermission(
41 extensions::APIPermission::kNotifications)) {
42 continue;
43 }
44
45 // Hosted apps are no longer able to affect the notifications permission
46 // state for web notifications.
47 // TODO(dewittj): Deprecate the 'notifications' permission for hosted
48 // apps.
49 if (extension->is_hosted_app())
50 continue;
51
52 message_center::NotifierId notifier_id(
Daniel Chenga925cbb52018-11-06 21:52:3553 message_center::NotifierType::APPLICATION, extension->id());
hironod36c21d2016-06-21 01:25:2254 NotifierStateTracker* const notifier_state_tracker =
55 NotifierStateTrackerFactory::GetForProfile(profile);
Ian Barkley-Yeungb2893472019-06-15 01:44:0656 ui_data.push_back(ash::mojom::NotifierUiData::New(
hironod36c21d2016-06-21 01:25:2257 notifier_id, base::UTF8ToUTF16(extension->name()),
Evan Stade55575d82017-11-02 00:52:3658 notifier_state_tracker->IsNotifierEnabled(notifier_id),
Ian Barkley-Yeungb2893472019-06-15 01:44:0659 false /* enforced */, gfx::ImageSkia()));
hironod36c21d2016-06-21 01:25:2260 app_icon_loader_->FetchImage(extension->id());
61 }
62
Ian Barkley-Yeungb2893472019-06-15 01:44:0663 return ui_data;
hironod36c21d2016-06-21 01:25:2264}
65
Evan Staded6d837f2017-09-29 02:51:0266void ExtensionNotifierController::SetNotifierEnabled(
hironod36c21d2016-06-21 01:25:2267 Profile* profile,
Evan Stade844ad7f2017-09-20 18:05:3168 const message_center::NotifierId& notifier_id,
hironod36c21d2016-06-21 01:25:2269 bool enabled) {
70 NotifierStateTrackerFactory::GetForProfile(profile)->SetNotifierEnabled(
Evan Stade844ad7f2017-09-20 18:05:3171 notifier_id, enabled);
72 observer_->OnNotifierEnabledChanged(notifier_id, enabled);
hironod36c21d2016-06-21 01:25:2273}
74
Evan Staded6d837f2017-09-29 02:51:0275void ExtensionNotifierController::OnAppImageUpdated(
76 const std::string& id,
77 const gfx::ImageSkia& image) {
hironod36c21d2016-06-21 01:25:2278 observer_->OnIconImageUpdated(
Daniel Chenga925cbb52018-11-06 21:52:3579 message_center::NotifierId(message_center::NotifierType::APPLICATION, id),
Evan Stade55575d82017-11-02 00:52:3680 image);
hironod36c21d2016-06-21 01:25:2281}