hirono | d36c21d | 2016-06-21 01:25:22 | [diff] [blame] | 1 | // 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 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 | |
Evan Stade | ecfc48c | 2019-06-17 21:35:23 | [diff] [blame^] | 7 | #include "ash/public/cpp/notifier_metadata.h" |
hirono | d36c21d | 2016-06-21 01:25:22 | [diff] [blame] | 8 | #include "base/strings/utf_string_conversions.h" |
khmel | 8c1f662 | 2017-05-11 19:14:50 | [diff] [blame] | 9 | #include "chrome/browser/extensions/chrome_app_icon_loader.h" |
hirono | d36c21d | 2016-06-21 01:25:22 | [diff] [blame] | 10 | #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 Stade | feec2d11 | 2017-10-19 03:24:53 | [diff] [blame] | 13 | #include "chrome/common/extensions/api/notifications.h" |
| 14 | #include "extensions/browser/event_router.h" |
hirono | d36c21d | 2016-06-21 01:25:22 | [diff] [blame] | 15 | #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 Stade | 889ce471 | 2018-01-28 15:26:26 | [diff] [blame] | 19 | #include "ui/message_center/public/cpp/notifier_id.h" |
hirono | d36c21d | 2016-06-21 01:25:22 | [diff] [blame] | 20 | |
Evan Stade | d6d837f | 2017-09-29 02:51:02 | [diff] [blame] | 21 | ExtensionNotifierController::ExtensionNotifierController(Observer* observer) |
hirono | d36c21d | 2016-06-21 01:25:22 | [diff] [blame] | 22 | : observer_(observer) {} |
| 23 | |
Evan Stade | d6d837f | 2017-09-29 02:51:02 | [diff] [blame] | 24 | ExtensionNotifierController::~ExtensionNotifierController() {} |
hirono | d36c21d | 2016-06-21 01:25:22 | [diff] [blame] | 25 | |
Evan Stade | ecfc48c | 2019-06-17 21:35:23 | [diff] [blame^] | 26 | std::vector<ash::NotifierMetadata> ExtensionNotifierController::GetNotifierList( |
| 27 | Profile* profile) { |
| 28 | std::vector<ash::NotifierMetadata> notifiers; |
hirono | d36c21d | 2016-06-21 01:25:22 | [diff] [blame] | 29 | 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 |
khmel | 8c1f662 | 2017-05-11 19:14:50 | [diff] [blame] | 36 | app_icon_loader_.reset(new extensions::ChromeAppIconLoader( |
hirono | d36c21d | 2016-06-21 01:25:22 | [diff] [blame] | 37 | 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 Cheng | a925cbb5 | 2018-11-06 21:52:35 | [diff] [blame] | 54 | message_center::NotifierType::APPLICATION, extension->id()); |
hirono | d36c21d | 2016-06-21 01:25:22 | [diff] [blame] | 55 | NotifierStateTracker* const notifier_state_tracker = |
| 56 | NotifierStateTrackerFactory::GetForProfile(profile); |
Evan Stade | ecfc48c | 2019-06-17 21:35:23 | [diff] [blame^] | 57 | notifiers.emplace_back( |
hirono | d36c21d | 2016-06-21 01:25:22 | [diff] [blame] | 58 | notifier_id, base::UTF8ToUTF16(extension->name()), |
Evan Stade | 55575d8 | 2017-11-02 00:52:36 | [diff] [blame] | 59 | notifier_state_tracker->IsNotifierEnabled(notifier_id), |
Evan Stade | ecfc48c | 2019-06-17 21:35:23 | [diff] [blame^] | 60 | false /* enforced */, gfx::ImageSkia()); |
hirono | d36c21d | 2016-06-21 01:25:22 | [diff] [blame] | 61 | app_icon_loader_->FetchImage(extension->id()); |
| 62 | } |
| 63 | |
Evan Stade | ecfc48c | 2019-06-17 21:35:23 | [diff] [blame^] | 64 | return notifiers; |
hirono | d36c21d | 2016-06-21 01:25:22 | [diff] [blame] | 65 | } |
| 66 | |
Evan Stade | d6d837f | 2017-09-29 02:51:02 | [diff] [blame] | 67 | void ExtensionNotifierController::SetNotifierEnabled( |
hirono | d36c21d | 2016-06-21 01:25:22 | [diff] [blame] | 68 | Profile* profile, |
Evan Stade | 844ad7f | 2017-09-20 18:05:31 | [diff] [blame] | 69 | const message_center::NotifierId& notifier_id, |
hirono | d36c21d | 2016-06-21 01:25:22 | [diff] [blame] | 70 | bool enabled) { |
| 71 | NotifierStateTrackerFactory::GetForProfile(profile)->SetNotifierEnabled( |
Evan Stade | 844ad7f | 2017-09-20 18:05:31 | [diff] [blame] | 72 | notifier_id, enabled); |
| 73 | observer_->OnNotifierEnabledChanged(notifier_id, enabled); |
hirono | d36c21d | 2016-06-21 01:25:22 | [diff] [blame] | 74 | } |
| 75 | |
Evan Stade | d6d837f | 2017-09-29 02:51:02 | [diff] [blame] | 76 | void ExtensionNotifierController::OnAppImageUpdated( |
| 77 | const std::string& id, |
| 78 | const gfx::ImageSkia& image) { |
hirono | d36c21d | 2016-06-21 01:25:22 | [diff] [blame] | 79 | observer_->OnIconImageUpdated( |
Daniel Cheng | a925cbb5 | 2018-11-06 21:52:35 | [diff] [blame] | 80 | message_center::NotifierId(message_center::NotifierType::APPLICATION, id), |
Evan Stade | 55575d8 | 2017-11-02 00:52:36 | [diff] [blame] | 81 | image); |
hirono | d36c21d | 2016-06-21 01:25:22 | [diff] [blame] | 82 | } |