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 | |
| 5 | #include "chrome/browser/notifications/application_notifier_source.h" |
| 6 | |
| 7 | #include "base/strings/utf_string_conversions.h" |
khmel | 8c1f662 | 2017-05-11 19:14:50 | [diff] [blame] | 8 | #include "chrome/browser/extensions/chrome_app_icon_loader.h" |
hirono | d36c21d | 2016-06-21 01:25:22 | [diff] [blame] | 9 | #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" |
| 12 | #include "extensions/common/constants.h" |
| 13 | #include "extensions/common/extension_set.h" |
| 14 | #include "extensions/common/permissions/api_permission.h" |
| 15 | #include "extensions/common/permissions/permissions_data.h" |
| 16 | #include "ui/message_center/notifier_settings.h" |
| 17 | |
| 18 | ApplicationNotifierSource::ApplicationNotifierSource(Observer* observer) |
| 19 | : observer_(observer) {} |
| 20 | |
| 21 | ApplicationNotifierSource::~ApplicationNotifierSource() {} |
| 22 | |
| 23 | std::vector<std::unique_ptr<message_center::Notifier>> |
| 24 | ApplicationNotifierSource::GetNotifierList(Profile* profile) { |
| 25 | std::vector<std::unique_ptr<message_center::Notifier>> notifiers; |
| 26 | const extensions::ExtensionSet& extension_set = |
| 27 | extensions::ExtensionRegistry::Get(profile)->enabled_extensions(); |
| 28 | // The extension icon size has to be 32x32 at least to load bigger icons if |
| 29 | // the icon doesn't exist for the specified size, and in that case it falls |
| 30 | // back to the default icon. The fetched icon will be resized in the |
| 31 | // settings dialog. See chrome/browser/extensions/extension_icon_image.cc |
| 32 | // and crbug.com/222931 |
khmel | 8c1f662 | 2017-05-11 19:14:50 | [diff] [blame] | 33 | app_icon_loader_.reset(new extensions::ChromeAppIconLoader( |
hirono | d36c21d | 2016-06-21 01:25:22 | [diff] [blame] | 34 | profile, extension_misc::EXTENSION_ICON_SMALL, this)); |
| 35 | for (extensions::ExtensionSet::const_iterator iter = extension_set.begin(); |
| 36 | iter != extension_set.end(); ++iter) { |
| 37 | const extensions::Extension* extension = iter->get(); |
| 38 | if (!extension->permissions_data()->HasAPIPermission( |
| 39 | extensions::APIPermission::kNotifications)) { |
| 40 | continue; |
| 41 | } |
| 42 | |
| 43 | // Hosted apps are no longer able to affect the notifications permission |
| 44 | // state for web notifications. |
| 45 | // TODO(dewittj): Deprecate the 'notifications' permission for hosted |
| 46 | // apps. |
| 47 | if (extension->is_hosted_app()) |
| 48 | continue; |
| 49 | |
| 50 | message_center::NotifierId notifier_id( |
| 51 | message_center::NotifierId::APPLICATION, extension->id()); |
| 52 | NotifierStateTracker* const notifier_state_tracker = |
| 53 | NotifierStateTrackerFactory::GetForProfile(profile); |
| 54 | notifiers.emplace_back(new message_center::Notifier( |
| 55 | notifier_id, base::UTF8ToUTF16(extension->name()), |
| 56 | notifier_state_tracker->IsNotifierEnabled(notifier_id))); |
| 57 | app_icon_loader_->FetchImage(extension->id()); |
| 58 | } |
| 59 | |
| 60 | return notifiers; |
| 61 | } |
| 62 | |
| 63 | void ApplicationNotifierSource::SetNotifierEnabled( |
| 64 | Profile* profile, |
| 65 | const message_center::Notifier& notifier, |
| 66 | bool enabled) { |
| 67 | NotifierStateTrackerFactory::GetForProfile(profile)->SetNotifierEnabled( |
| 68 | notifier.notifier_id, enabled); |
| 69 | observer_->OnNotifierEnabledChanged(notifier.notifier_id, enabled); |
| 70 | } |
| 71 | |
| 72 | message_center::NotifierId::NotifierType |
| 73 | ApplicationNotifierSource::GetNotifierType() { |
| 74 | return message_center::NotifierId::APPLICATION; |
| 75 | } |
| 76 | |
| 77 | void ApplicationNotifierSource::OnAppImageUpdated(const std::string& id, |
| 78 | const gfx::ImageSkia& image) { |
| 79 | observer_->OnIconImageUpdated( |
| 80 | message_center::NotifierId(message_center::NotifierId::APPLICATION, id), |
| 81 | gfx::Image(image)); |
| 82 | } |