Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2113)

Unified Diff: chrome/browser/notifications/message_center_settings_controller.cc

Issue 2064363002: Refactor MessageCenterSettingsController. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix misspell. Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/notifications/message_center_settings_controller.cc
diff --git a/chrome/browser/notifications/message_center_settings_controller.cc b/chrome/browser/notifications/message_center_settings_controller.cc
index 8de630ec10ea64c011b22ab73d8c35181e39ab92..03c8a32220a6cba237c2a81b9b19016b75a74c69 100644
--- a/chrome/browser/notifications/message_center_settings_controller.cc
+++ b/chrome/browser/notifications/message_center_settings_controller.cc
@@ -11,46 +11,26 @@
#include "base/command_line.h"
#include "base/i18n/string_compare.h"
#include "base/strings/utf_string_conversions.h"
-#include "base/task/cancelable_task_tracker.h"
#include "base/threading/thread_task_runner_handle.h"
#include "build/build_config.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/chrome_notification_types.h"
-#include "chrome/browser/content_settings/host_content_settings_map_factory.h"
-#include "chrome/browser/extensions/extension_app_icon_loader.h"
-#include "chrome/browser/favicon/favicon_service_factory.h"
-#include "chrome/browser/notifications/desktop_notification_profile_util.h"
-#include "chrome/browser/notifications/notifier_state_tracker.h"
-#include "chrome/browser/notifications/notifier_state_tracker_factory.h"
+#include "chrome/browser/notifications/application_notifier_source.h"
+#include "chrome/browser/notifications/web_page_notifier_source.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_attributes_entry.h"
#include "chrome/browser/profiles/profile_attributes_storage.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/common/extensions/api/notifications.h"
-#include "chrome/common/extensions/extension_constants.h"
-#include "components/content_settings/core/browser/host_content_settings_map.h"
-#include "components/favicon/core/favicon_service.h"
-#include "components/favicon_base/favicon_types.h"
-#include "components/history/core/browser/history_types.h"
#include "content/public/browser/notification_service.h"
-#include "content/public/browser/notification_source.h"
#include "extensions/browser/event_router.h"
-#include "extensions/browser/extension_registry.h"
-#include "extensions/common/constants.h"
-#include "extensions/common/extension.h"
-#include "extensions/common/permissions/permissions_data.h"
-#include "grit/theme_resources.h"
-#include "ui/base/l10n/l10n_util.h"
-#include "ui/base/resource/resource_bundle.h"
#include "ui/gfx/image/image.h"
#include "ui/message_center/message_center_style.h"
-#include "ui/strings/grit/ui_strings.h"
#if defined(OS_CHROMEOS)
-#include "ash/common/system/system_notifier.h"
#include "chrome/browser/chromeos/profiles/profile_helper.h"
-#include "chrome/browser/notifications/arc_notifier_manager.h"
-#include "components/arc/arc_bridge_service.h"
+#include "chrome/browser/notifications/arc_application_notifier_source_chromeos.h"
+#include "chrome/browser/notifications/system_component_notifier_source_chromeos.h"
#endif
using message_center::Notifier;
@@ -98,6 +78,7 @@ ProfileNotifierGroup::ProfileNotifierGroup(const gfx::Image& icon,
} // namespace message_center
namespace {
+
class NotifierComparator {
public:
explicit NotifierComparator(icu::Collator* collator) : collator_(collator) {}
@@ -139,13 +120,26 @@ MessageCenterSettingsController::MessageCenterSettingsController(
profile_attributes_storage_.AddObserver(this);
RebuildNotifierGroups(false);
+ sources_.insert(std::make_pair(
+ NotifierId::APPLICATION,
+ std::unique_ptr<NotifierSource>(new ApplicationNotifierSource(this))));
+ sources_.insert(std::make_pair(
+ NotifierId::WEB_PAGE,
+ std::unique_ptr<NotifierSource>(new WebPageNotifiereSource(this))));
+
#if defined(OS_CHROMEOS)
// UserManager may not exist in some tests.
if (user_manager::UserManager::IsInitialized())
user_manager::UserManager::Get()->AddSessionStateObserver(this);
- // Check if ARC is enabled.
- if (arc::ArcBridgeService::Get())
- arc_notifier_manager_.reset(new arc::ArcNotifierManager());
+ // For system components.
+ sources_.insert(
+ std::make_pair(NotifierId::SYSTEM_COMPONENT,
+ std::unique_ptr<NotifierSource>(
+ new SystemComponentNotifierSourceChromeOS(this))));
+ sources_.insert(
+ std::make_pair(NotifierId::ARC_APPLICATION,
+ std::unique_ptr<NotifierSource>(
+ new arc::ArcApplicationNotifierSourceChromeOS(this))));
#endif
}
@@ -195,9 +189,7 @@ void MessageCenterSettingsController::SwitchToNotifierGroup(size_t index) {
return;
current_notifier_group_ = index;
- FOR_EACH_OBSERVER(message_center::NotifierSettingsObserver,
- observers_,
- NotifierGroupChanged());
+ DispatchNotifierGroupChanged();
}
void MessageCenterSettingsController::GetNotifierList(
@@ -209,101 +201,13 @@ void MessageCenterSettingsController::GetNotifierList(
// the default profile is not loaded.
Profile* profile = notifier_groups_[current_notifier_group_]->profile();
- NotifierStateTracker* notifier_state_tracker =
- NotifierStateTrackerFactory::GetForProfile(profile);
-
- const extensions::ExtensionSet& extension_set =
- extensions::ExtensionRegistry::Get(profile)->enabled_extensions();
- // The extension icon size has to be 32x32 at least to load bigger icons if
- // the icon doesn't exist for the specified size, and in that case it falls
- // back to the default icon. The fetched icon will be resized in the settings
- // dialog. See chrome/browser/extensions/extension_icon_image.cc and
- // crbug.com/222931
- app_icon_loader_.reset(new extensions::ExtensionAppIconLoader(
- profile, extension_misc::EXTENSION_ICON_SMALL, this));
- for (extensions::ExtensionSet::const_iterator iter = extension_set.begin();
- iter != extension_set.end();
- ++iter) {
- const extensions::Extension* extension = iter->get();
- if (!extension->permissions_data()->HasAPIPermission(
- extensions::APIPermission::kNotifications)) {
- continue;
- }
-
- // Hosted apps are no longer able to affect the notifications permission
- // state for web notifications.
- // TODO(dewittj): Deprecate the 'notifications' permission for hosted apps.
- if (extension->is_hosted_app())
- continue;
-
- NotifierId notifier_id(NotifierId::APPLICATION, extension->id());
- notifiers->push_back(new Notifier(
- notifier_id,
- base::UTF8ToUTF16(extension->name()),
- notifier_state_tracker->IsNotifierEnabled(notifier_id)));
- app_icon_loader_->FetchImage(extension->id());
- }
-
- ContentSettingsForOneType settings;
- DesktopNotificationProfileUtil::GetNotificationsSettings(profile, &settings);
-
- favicon::FaviconService* favicon_service =
- FaviconServiceFactory::GetForProfile(profile,
- ServiceAccessType::EXPLICIT_ACCESS);
- favicon_tracker_.reset(new base::CancelableTaskTracker());
- patterns_.clear();
- for (ContentSettingsForOneType::const_iterator iter = settings.begin();
- iter != settings.end(); ++iter) {
- if (iter->primary_pattern == ContentSettingsPattern::Wildcard() &&
- iter->secondary_pattern == ContentSettingsPattern::Wildcard() &&
- iter->source != "preference") {
- continue;
- }
-
- std::string url_pattern = iter->primary_pattern.ToString();
- base::string16 name = base::UTF8ToUTF16(url_pattern);
- GURL url(url_pattern);
- NotifierId notifier_id(url);
- notifiers->push_back(new Notifier(
- notifier_id,
- name,
- notifier_state_tracker->IsNotifierEnabled(notifier_id)));
- patterns_[name] = iter->primary_pattern;
- // Note that favicon service obtains the favicon from history. This means
- // that it will fail to obtain the image if there are no history data for
- // that URL.
- favicon_service->GetFaviconImageForPageURL(
- url,
- base::Bind(&MessageCenterSettingsController::OnFaviconLoaded,
- base::Unretained(this),
- url),
- favicon_tracker_.get());
- }
-
-#if defined(OS_CHROMEOS)
- // Add ARC++ apps.
- if (arc_notifier_manager_) {
- auto list = arc_notifier_manager_->GetNotifiers(profile);
- for (auto& notifier : list) {
+ for (auto& source : sources_) {
+ auto source_notifiers = source.second->GetNotifierList(profile);
+ for (auto& notifier : source_notifiers) {
notifiers->push_back(notifier.release());
}
}
- // Screenshot notification feature is only for ChromeOS. See crbug.com/238358
- const base::string16& screenshot_name =
- l10n_util::GetStringUTF16(IDS_MESSAGE_CENTER_NOTIFIER_SCREENSHOT_NAME);
- NotifierId screenshot_notifier_id(
- NotifierId::SYSTEM_COMPONENT, ash::system_notifier::kNotifierScreenshot);
- Notifier* const screenshot_notifier = new Notifier(
- screenshot_notifier_id,
- screenshot_name,
- notifier_state_tracker->IsNotifierEnabled(screenshot_notifier_id));
- screenshot_notifier->icon =
- ui::ResourceBundle::GetSharedInstance().GetImageNamed(
- IDR_SCREENSHOT_NOTIFICATION_ICON);
- notifiers->push_back(screenshot_notifier);
-#endif
-
UErrorCode error = U_ZERO_ERROR;
std::unique_ptr<icu::Collator> collator(icu::Collator::createInstance(error));
std::unique_ptr<NotifierComparator> comparator(
@@ -318,96 +222,19 @@ void MessageCenterSettingsController::SetNotifierEnabled(
DCHECK_LT(current_notifier_group_, notifier_groups_.size());
Profile* profile = notifier_groups_[current_notifier_group_]->profile();
- switch (notifier.notifier_id.type) {
- case NotifierId::WEB_PAGE: {
- // WEB_PAGE notifier cannot handle in DesktopNotificationService
- // since it has the exact URL pattern.
- // TODO(mukai): fix this.
- ContentSetting default_setting =
- HostContentSettingsMapFactory::GetForProfile(profile)
- ->GetDefaultContentSetting(CONTENT_SETTINGS_TYPE_NOTIFICATIONS,
- NULL);
-
- DCHECK(default_setting == CONTENT_SETTING_ALLOW ||
- default_setting == CONTENT_SETTING_BLOCK ||
- default_setting == CONTENT_SETTING_ASK);
-
- // The content setting for notifications needs to clear when it changes to
- // the default value or get explicitly set when it differs from the
- // default.
- bool differs_from_default_value =
- (default_setting != CONTENT_SETTING_ALLOW && enabled) ||
- (default_setting == CONTENT_SETTING_ALLOW && !enabled);
-
- if (differs_from_default_value) {
- if (notifier.notifier_id.url.is_valid()) {
- if (enabled) {
- DesktopNotificationProfileUtil::GrantPermission(
- profile, notifier.notifier_id.url);
- } else {
- DesktopNotificationProfileUtil::DenyPermission(
- profile, notifier.notifier_id.url);
- }
- } else {
- LOG(ERROR) << "Invalid url pattern: "
- << notifier.notifier_id.url.spec();
- }
- } else {
- ContentSettingsPattern pattern;
-
- const auto& iter = patterns_.find(notifier.name);
- if (iter != patterns_.end()) {
- pattern = iter->second;
- } else if (notifier.notifier_id.url.is_valid()) {
- pattern = ContentSettingsPattern::FromURLNoWildcard(
- notifier.notifier_id.url);
- } else {
- LOG(ERROR) << "Invalid url pattern: "
- << notifier.notifier_id.url.spec();
- }
-
- if (pattern.IsValid()) {
- // Note that we don't use
- // DesktopNotificationProfileUtil::ClearSetting()
- // here because pattern might be from user manual input and not match
- // the default one used by ClearSetting().
- HostContentSettingsMapFactory::GetForProfile(profile)
- ->SetContentSettingCustomScope(
- pattern, ContentSettingsPattern::Wildcard(),
- CONTENT_SETTINGS_TYPE_NOTIFICATIONS,
- content_settings::ResourceIdentifier(),
- CONTENT_SETTING_DEFAULT);
- }
- }
- break;
- }
- case NotifierId::APPLICATION:
- case NotifierId::SYSTEM_COMPONENT: {
- NotifierStateTrackerFactory::GetForProfile(profile)->SetNotifierEnabled(
- notifier.notifier_id, enabled);
- break;
- }
- case NotifierId::ARC_APPLICATION: {
-#if defined(OS_CHROMEOS)
- if (arc_notifier_manager_) {
- arc_notifier_manager_->SetNotifierEnabled(notifier.notifier_id.id,
- enabled);
- }
-#else
- NOTREACHED();
-#endif
- break;
- }
+ if (!sources_.count(notifier.notifier_id.type)) {
+ NOTREACHED();
+ return;
}
- FOR_EACH_OBSERVER(message_center::NotifierSettingsObserver,
- observers_,
- NotifierEnabledChanged(notifier.notifier_id, enabled));
+
+ sources_[notifier.notifier_id.type]->SetNotifierEnabled(profile, notifier,
+ enabled);
}
void MessageCenterSettingsController::OnNotifierSettingsClosing() {
- DCHECK(favicon_tracker_.get());
- favicon_tracker_->TryCancelAll();
- patterns_.clear();
+ for (auto& source : sources_) {
+ source.second->OnNotifierSettingsClosing();
+ }
}
bool MessageCenterSettingsController::NotifierHasAdvancedSettings(
@@ -453,15 +280,6 @@ void MessageCenterSettingsController::OnNotifierAdvancedSettingsRequested(
event_router->DispatchEventToExtension(extension_id, std::move(event));
}
-void MessageCenterSettingsController::OnFaviconLoaded(
- const GURL& url,
- const favicon_base::FaviconImageResult& favicon_result) {
- FOR_EACH_OBSERVER(message_center::NotifierSettingsObserver,
- observers_,
- UpdateIconImage(NotifierId(url), favicon_result.image));
-}
-
-
#if defined(OS_CHROMEOS)
void MessageCenterSettingsController::ActiveUserChanged(
const user_manager::User* active_user) {
@@ -469,14 +287,6 @@ void MessageCenterSettingsController::ActiveUserChanged(
}
#endif
-void MessageCenterSettingsController::OnAppImageUpdated(
- const std::string& id, const gfx::ImageSkia& image) {
- FOR_EACH_OBSERVER(message_center::NotifierSettingsObserver,
- observers_,
- UpdateIconImage(NotifierId(NotifierId::APPLICATION, id),
- gfx::Image(image)));
-}
-
void MessageCenterSettingsController::Observe(
int type,
const content::NotificationSource& source,
@@ -510,6 +320,25 @@ void MessageCenterSettingsController::OnProfileAuthInfoChanged(
RebuildNotifierGroups(true);
}
+void MessageCenterSettingsController::OnIconImageUpdated(
+ const message_center::NotifierId& id,
+ const gfx::Image& image) {
+ FOR_EACH_OBSERVER(message_center::NotifierSettingsObserver, observers_,
+ UpdateIconImage(id, image));
+}
+
+void MessageCenterSettingsController::OnNotifierEnabledChanged(
+ const message_center::NotifierId& id,
+ bool enabled) {
+ FOR_EACH_OBSERVER(message_center::NotifierSettingsObserver, observers_,
+ NotifierEnabledChanged(id, enabled));
+}
+
+void MessageCenterSettingsController::DispatchNotifierGroupChanged() {
+ FOR_EACH_OBSERVER(message_center::NotifierSettingsObserver, observers_,
+ NotifierGroupChanged());
+}
+
#if defined(OS_CHROMEOS)
void MessageCenterSettingsController::CreateNotifierGroupForGuestLogin() {
// Already created.
@@ -532,10 +361,7 @@ void MessageCenterSettingsController::CreateNotifierGroupForGuestLogin() {
user->GetDisplayName(), profile));
notifier_groups_.push_back(std::move(group));
-
- FOR_EACH_OBSERVER(message_center::NotifierSettingsObserver,
- observers_,
- NotifierGroupChanged());
+ DispatchNotifierGroupChanged();
}
#endif
@@ -595,9 +421,6 @@ void MessageCenterSettingsController::RebuildNotifierGroups(bool notify) {
}
#endif
- if (notify) {
- FOR_EACH_OBSERVER(message_center::NotifierSettingsObserver,
- observers_,
- NotifierGroupChanged());
- }
+ if (notify)
+ DispatchNotifierGroupChanged();
}
« no previous file with comments | « chrome/browser/notifications/message_center_settings_controller.h ('k') | chrome/browser/notifications/notifier_source.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698