Avi Drissman | 4a8573c | 2022-09-09 19:35:54 | [diff] [blame] | 1 | // Copyright 2016 The Chromium Authors |
hirono | d36c21d | 2016-06-21 01:25:22 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Evan Stade | d6d837f | 2017-09-29 02:51:02 | [diff] [blame] | 5 | #include "chrome/browser/notifications/extension_notifier_controller.h" |
hirono | d36c21d | 2016-06-21 01:25:22 | [diff] [blame] | 6 | |
Peter Boström | 6b70182 | 2021-04-15 03:53:08 | [diff] [blame] | 7 | #include <memory> |
| 8 | |
Evan Stade | ecfc48c | 2019-06-17 21:35:23 | [diff] [blame] | 9 | #include "ash/public/cpp/notifier_metadata.h" |
hirono | d36c21d | 2016-06-21 01:25:22 | [diff] [blame] | 10 | #include "base/strings/utf_string_conversions.h" |
khmel | 8c1f662 | 2017-05-11 19:14:50 | [diff] [blame] | 11 | #include "chrome/browser/extensions/chrome_app_icon_loader.h" |
hirono | d36c21d | 2016-06-21 01:25:22 | [diff] [blame] | 12 | #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 Stade | feec2d11 | 2017-10-19 03:24:53 | [diff] [blame] | 15 | #include "chrome/common/extensions/api/notifications.h" |
| 16 | #include "extensions/browser/event_router.h" |
hirono | d36c21d | 2016-06-21 01:25:22 | [diff] [blame] | 17 | #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 Stade | 889ce471 | 2018-01-28 15:26:26 | [diff] [blame] | 21 | #include "ui/message_center/public/cpp/notifier_id.h" |
hirono | d36c21d | 2016-06-21 01:25:22 | [diff] [blame] | 22 | |
Evan Stade | d6d837f | 2017-09-29 02:51:02 | [diff] [blame] | 23 | ExtensionNotifierController::ExtensionNotifierController(Observer* observer) |
hirono | d36c21d | 2016-06-21 01:25:22 | [diff] [blame] | 24 | : observer_(observer) {} |
| 25 | |
Sorin Jianu | fd5d329 | 2024-11-28 05:56:07 | [diff] [blame] | 26 | ExtensionNotifierController::~ExtensionNotifierController() = default; |
hirono | d36c21d | 2016-06-21 01:25:22 | [diff] [blame] | 27 | |
Evan Stade | ecfc48c | 2019-06-17 21:35:23 | [diff] [blame] | 28 | std::vector<ash::NotifierMetadata> ExtensionNotifierController::GetNotifierList( |
| 29 | Profile* profile) { |
| 30 | std::vector<ash::NotifierMetadata> notifiers; |
hirono | d36c21d | 2016-06-21 01:25:22 | [diff] [blame] | 31 | 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öm | 6b70182 | 2021-04-15 03:53:08 | [diff] [blame] | 38 | app_icon_loader_ = std::make_unique<extensions::ChromeAppIconLoader>( |
| 39 | profile, extension_misc::EXTENSION_ICON_SMALL, this); |
hirono | d36c21d | 2016-06-21 01:25:22 | [diff] [blame] | 40 | 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 Kim | 7b51415 | 2021-03-30 23:55:50 | [diff] [blame] | 44 | extensions::mojom::APIPermissionID::kNotifications)) { |
hirono | d36c21d | 2016-06-21 01:25:22 | [diff] [blame] | 45 | 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 Cheng | a925cbb5 | 2018-11-06 21:52:35 | [diff] [blame] | 56 | message_center::NotifierType::APPLICATION, extension->id()); |
hirono | d36c21d | 2016-06-21 01:25:22 | [diff] [blame] | 57 | NotifierStateTracker* const notifier_state_tracker = |
| 58 | NotifierStateTrackerFactory::GetForProfile(profile); |
Evan Stade | ecfc48c | 2019-06-17 21:35:23 | [diff] [blame] | 59 | notifiers.emplace_back( |
hirono | d36c21d | 2016-06-21 01:25:22 | [diff] [blame] | 60 | notifier_id, base::UTF8ToUTF16(extension->name()), |
Evan Stade | 55575d8 | 2017-11-02 00:52:36 | [diff] [blame] | 61 | notifier_state_tracker->IsNotifierEnabled(notifier_id), |
Evan Stade | ecfc48c | 2019-06-17 21:35:23 | [diff] [blame] | 62 | false /* enforced */, gfx::ImageSkia()); |
hirono | d36c21d | 2016-06-21 01:25:22 | [diff] [blame] | 63 | app_icon_loader_->FetchImage(extension->id()); |
| 64 | } |
| 65 | |
Evan Stade | ecfc48c | 2019-06-17 21:35:23 | [diff] [blame] | 66 | return notifiers; |
hirono | d36c21d | 2016-06-21 01:25:22 | [diff] [blame] | 67 | } |
| 68 | |
Evan Stade | d6d837f | 2017-09-29 02:51:02 | [diff] [blame] | 69 | void ExtensionNotifierController::SetNotifierEnabled( |
hirono | d36c21d | 2016-06-21 01:25:22 | [diff] [blame] | 70 | Profile* profile, |
Evan Stade | 844ad7f | 2017-09-20 18:05:31 | [diff] [blame] | 71 | const message_center::NotifierId& notifier_id, |
hirono | d36c21d | 2016-06-21 01:25:22 | [diff] [blame] | 72 | bool enabled) { |
| 73 | NotifierStateTrackerFactory::GetForProfile(profile)->SetNotifierEnabled( |
Evan Stade | 844ad7f | 2017-09-20 18:05:31 | [diff] [blame] | 74 | notifier_id, enabled); |
| 75 | observer_->OnNotifierEnabledChanged(notifier_id, enabled); |
hirono | d36c21d | 2016-06-21 01:25:22 | [diff] [blame] | 76 | } |
| 77 | |
Evan Stade | d6d837f | 2017-09-29 02:51:02 | [diff] [blame] | 78 | void ExtensionNotifierController::OnAppImageUpdated( |
| 79 | const std::string& id, |
Toni Barzic | c3d4a4c | 2023-11-02 01:36:39 | [diff] [blame] | 80 | const gfx::ImageSkia& image, |
Ana Salazar | 955fb6d | 2023-11-07 00:14:15 | [diff] [blame] | 81 | bool is_placeholder_icon, |
Arthur Sonzogni | fe132ee | 2024-01-15 11:01:04 | [diff] [blame] | 82 | const std::optional<gfx::ImageSkia>& badge_image) { |
hirono | d36c21d | 2016-06-21 01:25:22 | [diff] [blame] | 83 | observer_->OnIconImageUpdated( |
Daniel Cheng | a925cbb5 | 2018-11-06 21:52:35 | [diff] [blame] | 84 | message_center::NotifierId(message_center::NotifierType::APPLICATION, id), |
Evan Stade | 55575d8 | 2017-11-02 00:52:36 | [diff] [blame] | 85 | image); |
hirono | d36c21d | 2016-06-21 01:25:22 | [diff] [blame] | 86 | } |