blob: 8fb507b4b48e4f0dc35d2da9eb1cf8c916dc9883 [file] [log] [blame]
Avi Drissman4a8573c2022-09-09 19:35:541// Copyright 2016 The Chromium Authors
hironod36c21d2016-06-21 01:25:222// 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
Peter Boström6b701822021-04-15 03:53:087#include <memory>
8
Evan Stadeecfc48c2019-06-17 21:35:239#include "ash/public/cpp/notifier_metadata.h"
hironod36c21d2016-06-21 01:25:2210#include "base/strings/utf_string_conversions.h"
khmel8c1f6622017-05-11 19:14:5011#include "chrome/browser/extensions/chrome_app_icon_loader.h"
hironod36c21d2016-06-21 01:25:2212#include "chrome/browser/notifications/notifier_state_tracker.h"
13#include "chrome/browser/notifications/notifier_state_tracker_factory.h"
14#include "chrome/browser/profiles/profile.h"
Evan Stadefeec2d112017-10-19 03:24:5315#include "chrome/common/extensions/api/notifications.h"
16#include "extensions/browser/event_router.h"
hironod36c21d2016-06-21 01:25:2217#include "extensions/common/constants.h"
18#include "extensions/common/extension_set.h"
19#include "extensions/common/permissions/api_permission.h"
20#include "extensions/common/permissions/permissions_data.h"
Evan Stade889ce4712018-01-28 15:26:2621#include "ui/message_center/public/cpp/notifier_id.h"
hironod36c21d2016-06-21 01:25:2222
Evan Staded6d837f2017-09-29 02:51:0223ExtensionNotifierController::ExtensionNotifierController(Observer* observer)
hironod36c21d2016-06-21 01:25:2224 : observer_(observer) {}
25
Sorin Jianufd5d3292024-11-28 05:56:0726ExtensionNotifierController::~ExtensionNotifierController() = default;
hironod36c21d2016-06-21 01:25:2227
Evan Stadeecfc48c2019-06-17 21:35:2328std::vector<ash::NotifierMetadata> ExtensionNotifierController::GetNotifierList(
29 Profile* profile) {
30 std::vector<ash::NotifierMetadata> notifiers;
hironod36c21d2016-06-21 01:25:2231 const extensions::ExtensionSet& extension_set =
32 extensions::ExtensionRegistry::Get(profile)->enabled_extensions();
33 // The extension icon size has to be 32x32 at least to load bigger icons if
34 // the icon doesn't exist for the specified size, and in that case it falls
35 // back to the default icon. The fetched icon will be resized in the
36 // settings dialog. See chrome/browser/extensions/extension_icon_image.cc
37 // and crbug.com/222931
Peter Boström6b701822021-04-15 03:53:0838 app_icon_loader_ = std::make_unique<extensions::ChromeAppIconLoader>(
39 profile, extension_misc::EXTENSION_ICON_SMALL, this);
hironod36c21d2016-06-21 01:25:2240 for (extensions::ExtensionSet::const_iterator iter = extension_set.begin();
41 iter != extension_set.end(); ++iter) {
42 const extensions::Extension* extension = iter->get();
43 if (!extension->permissions_data()->HasAPIPermission(
Gyuyoung Kim7b514152021-03-30 23:55:5044 extensions::mojom::APIPermissionID::kNotifications)) {
hironod36c21d2016-06-21 01:25:2245 continue;
46 }
47
48 // Hosted apps are no longer able to affect the notifications permission
49 // state for web notifications.
50 // TODO(dewittj): Deprecate the 'notifications' permission for hosted
51 // apps.
52 if (extension->is_hosted_app())
53 continue;
54
55 message_center::NotifierId notifier_id(
Daniel Chenga925cbb52018-11-06 21:52:3556 message_center::NotifierType::APPLICATION, extension->id());
hironod36c21d2016-06-21 01:25:2257 NotifierStateTracker* const notifier_state_tracker =
58 NotifierStateTrackerFactory::GetForProfile(profile);
Evan Stadeecfc48c2019-06-17 21:35:2359 notifiers.emplace_back(
hironod36c21d2016-06-21 01:25:2260 notifier_id, base::UTF8ToUTF16(extension->name()),
Evan Stade55575d82017-11-02 00:52:3661 notifier_state_tracker->IsNotifierEnabled(notifier_id),
Evan Stadeecfc48c2019-06-17 21:35:2362 false /* enforced */, gfx::ImageSkia());
hironod36c21d2016-06-21 01:25:2263 app_icon_loader_->FetchImage(extension->id());
64 }
65
Evan Stadeecfc48c2019-06-17 21:35:2366 return notifiers;
hironod36c21d2016-06-21 01:25:2267}
68
Evan Staded6d837f2017-09-29 02:51:0269void ExtensionNotifierController::SetNotifierEnabled(
hironod36c21d2016-06-21 01:25:2270 Profile* profile,
Evan Stade844ad7f2017-09-20 18:05:3171 const message_center::NotifierId& notifier_id,
hironod36c21d2016-06-21 01:25:2272 bool enabled) {
73 NotifierStateTrackerFactory::GetForProfile(profile)->SetNotifierEnabled(
Evan Stade844ad7f2017-09-20 18:05:3174 notifier_id, enabled);
75 observer_->OnNotifierEnabledChanged(notifier_id, enabled);
hironod36c21d2016-06-21 01:25:2276}
77
Evan Staded6d837f2017-09-29 02:51:0278void ExtensionNotifierController::OnAppImageUpdated(
79 const std::string& id,
Toni Barzicc3d4a4c2023-11-02 01:36:3980 const gfx::ImageSkia& image,
Ana Salazar955fb6d2023-11-07 00:14:1581 bool is_placeholder_icon,
Arthur Sonzognife132ee2024-01-15 11:01:0482 const std::optional<gfx::ImageSkia>& badge_image) {
hironod36c21d2016-06-21 01:25:2283 observer_->OnIconImageUpdated(
Daniel Chenga925cbb52018-11-06 21:52:3584 message_center::NotifierId(message_center::NotifierType::APPLICATION, id),
Evan Stade55575d82017-11-02 00:52:3685 image);
hironod36c21d2016-06-21 01:25:2286}