From d9fb69b27cd48a1893ac367c96b5731f27072861 Mon Sep 17 00:00:00 2001 From: "limasdf@gmail.com" Date: Mon, 26 May 2014 19:25:01 +0000 Subject: [PATCH] Use ExtensionRegistryObserver instead of deprecated extension notification. and Use ExtensionRegistry functions instead of deprecated ExtensionService functions. R=xiyuan@chromium.org,rdevlin.cronin@chromium.org BUG=354046, 354458 Review URL: https://codereview.chromium.org/275503003 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@272861 0039d316-1c4b-4281-b951-d872f2087c98 --- .../ui/webui/app_list/start_page_handler.cc | 80 ++++++++++------------ .../browser/ui/webui/app_list/start_page_handler.h | 27 +++++--- .../browser/ui/webui/extensions/command_handler.cc | 54 ++++++++------- .../browser/ui/webui/extensions/command_handler.h | 32 +++++---- 4 files changed, 101 insertions(+), 92 deletions(-) diff --git a/chrome/browser/ui/webui/app_list/start_page_handler.cc b/chrome/browser/ui/webui/app_list/start_page_handler.cc index 604797357880..09df3bebc4c2 100644 --- a/chrome/browser/ui/webui/app_list/start_page_handler.cc +++ b/chrome/browser/ui/webui/app_list/start_page_handler.cc @@ -9,8 +9,7 @@ #include "base/bind.h" #include "base/memory/scoped_ptr.h" #include "base/values.h" -#include "chrome/browser/chrome_notification_types.h" -#include "chrome/browser/extensions/extension_service.h" +#include "base/version.h" #include "chrome/browser/omaha_query_params/omaha_query_params.h" #include "chrome/browser/profiles/profile.h" #include "chrome/browser/search/hotword_service.h" @@ -21,10 +20,10 @@ #include "chrome/browser/ui/host_desktop.h" #include "chrome/browser/ui/webui/extensions/extension_icon_source.h" #include "chrome/common/pref_names.h" -#include "content/public/browser/notification_details.h" -#include "content/public/browser/notification_source.h" #include "content/public/browser/web_ui.h" +#include "extensions/browser/extension_registry.h" #include "extensions/browser/extension_system.h" +#include "extensions/common/constants.h" #include "extensions/common/extension.h" #include "extensions/common/extension_icon_set.h" #include "ui/app_list/app_list_switches.h" @@ -61,7 +60,10 @@ scoped_ptr CreateAppInfo( } // namespace -StartPageHandler::StartPageHandler() : recommended_apps_(NULL) {} +StartPageHandler::StartPageHandler() + : recommended_apps_(NULL), + extension_registry_observer_(this) { +} StartPageHandler::~StartPageHandler() { if (recommended_apps_) @@ -89,31 +91,26 @@ void StartPageHandler::RegisterMessages() { base::Unretained(this))); } -void StartPageHandler::Observe(int type, - const content::NotificationSource& source, - const content::NotificationDetails& details) { +void StartPageHandler::OnExtensionLoaded( + content::BrowserContext* browser_context, + const extensions::Extension* extension) { #if defined(OS_CHROMEOS) DCHECK_EQ(Profile::FromWebUI(web_ui()), - content::Source(source).ptr()); - switch (type) { - case chrome::NOTIFICATION_EXTENSION_LOADED_DEPRECATED: { - extensions::Extension* extension = - content::Details(details).ptr(); - if (extension->id() == extension_misc::kHotwordExtensionId) - OnHotwordEnabledChanged(); - break; - } - case chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED: { - extensions::UnloadedExtensionInfo* info = - content::Details(details).ptr(); - if (info->extension->id() == extension_misc::kHotwordExtensionId) - OnHotwordEnabledChanged(); - break; - } - default: - NOTREACHED(); - break; - } + Profile::FromBrowserContext(browser_context)); + if (extension->id() == extension_misc::kHotwordExtensionId) + OnHotwordEnabledChanged(); +#endif +} + +void StartPageHandler::OnExtensionUnloaded( + content::BrowserContext* browser_context, + const extensions::Extension* extension, + extensions::UnloadedExtensionInfo::Reason reason) { +#if defined(OS_CHROMEOS) + DCHECK_EQ(Profile::FromWebUI(web_ui()), + Profile::FromBrowserContext(browser_context)); + if (extension->id() == extension_misc::kHotwordExtensionId) + OnHotwordEnabledChanged(); #endif } @@ -139,15 +136,14 @@ void StartPageHandler::OnHotwordEnabledChanged() { // hotwordPrivate API to provide the feature. // TODO(mukai): remove this after everything gets stable. Profile* profile = Profile::FromWebUI(web_ui()); - ExtensionService* extension_service = - extensions::ExtensionSystem::Get(profile)->extension_service(); - if (!extension_service) - return; + extensions::ExtensionRegistry* registry = + extensions::ExtensionRegistry::Get(profile); const extensions::Extension* hotword_extension = - extension_service->GetExtensionById( - extension_misc::kHotwordExtensionId, false /* include_disabled */); - if (hotword_extension && hotword_extension->version()->CompareTo( + registry->GetExtensionById(extension_misc::kHotwordExtensionId, + extensions::ExtensionRegistry::ENABLED); + if (hotword_extension && + hotword_extension->version()->CompareTo( base::Version(kOldHotwordExtensionVersionString)) <= 0) { StartPageService* service = StartPageService::Get(profile); web_ui()->CallJavascriptFunction( @@ -177,11 +173,9 @@ void StartPageHandler::HandleInitialize(const base::ListValue* args) { prefs::kHotwordSearchEnabled, base::Bind(&StartPageHandler::OnHotwordEnabledChanged, base::Unretained(this))); - registrar_.Add(this, - chrome::NOTIFICATION_EXTENSION_LOADED_DEPRECATED, - content::Source(profile)); - registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED, - content::Source(profile)); + + extension_registry_observer_.Add( + extensions::ExtensionRegistry::Get(profile)); } #endif @@ -201,9 +195,9 @@ void StartPageHandler::HandleLaunchApp(const base::ListValue* args) { CHECK(args->GetString(0, &app_id)); Profile* profile = Profile::FromWebUI(web_ui()); - ExtensionService* service = - extensions::ExtensionSystem::Get(profile)->extension_service(); - const extensions::Extension* app = service->GetInstalledExtension(app_id); + const extensions::Extension* app = + extensions::ExtensionRegistry::Get(profile) + ->GetExtensionById(app_id, extensions::ExtensionRegistry::EVERYTHING); if (!app) { NOTREACHED(); return; diff --git a/chrome/browser/ui/webui/app_list/start_page_handler.h b/chrome/browser/ui/webui/app_list/start_page_handler.h index 1ebff187b629..b6cdf3a4756c 100644 --- a/chrome/browser/ui/webui/app_list/start_page_handler.h +++ b/chrome/browser/ui/webui/app_list/start_page_handler.h @@ -8,22 +8,26 @@ #include "base/basictypes.h" #include "base/compiler_specific.h" #include "base/prefs/pref_change_registrar.h" +#include "base/scoped_observer.h" #include "chrome/browser/ui/app_list/recommended_apps_observer.h" -#include "content/public/browser/notification_observer.h" -#include "content/public/browser/notification_registrar.h" #include "content/public/browser/web_ui_message_handler.h" +#include "extensions/browser/extension_registry_observer.h" namespace base { class ListValue; } +namespace extensions { +class ExtensionRegistry; +} + namespace app_list { class RecommendedApps; // Handler for the app launcher start page. class StartPageHandler : public content::WebUIMessageHandler, - public content::NotificationObserver, + public extensions::ExtensionRegistryObserver, public RecommendedAppsObserver { public: StartPageHandler(); @@ -33,10 +37,14 @@ class StartPageHandler : public content::WebUIMessageHandler, // content::WebUIMessageHandler overrides: virtual void RegisterMessages() OVERRIDE; - // Overridden from content::NotificationObserver: - virtual void Observe(int type, - const content::NotificationSource& source, - const content::NotificationDetails& details) OVERRIDE; + // extensions::ExtensionRegistryObserver implementation. + virtual void OnExtensionLoaded( + content::BrowserContext* browser_context, + const extensions::Extension* extension) OVERRIDE; + virtual void OnExtensionUnloaded( + content::BrowserContext* browser_context, + const extensions::Extension* extension, + extensions::UnloadedExtensionInfo::Reason reason) OVERRIDE; // RecommendedAppsObserver overrdies: virtual void OnRecommendedAppsChanged() OVERRIDE; @@ -58,7 +66,10 @@ class StartPageHandler : public content::WebUIMessageHandler, RecommendedApps* recommended_apps_; // Not owned. PrefChangeRegistrar pref_change_registrar_; - content::NotificationRegistrar registrar_; + + ScopedObserver + extension_registry_observer_; DISALLOW_COPY_AND_ASSIGN(StartPageHandler); }; diff --git a/chrome/browser/ui/webui/extensions/command_handler.cc b/chrome/browser/ui/webui/extensions/command_handler.cc index e444592004a0..40ddfa2cfc0d 100644 --- a/chrome/browser/ui/webui/extensions/command_handler.cc +++ b/chrome/browser/ui/webui/extensions/command_handler.cc @@ -6,14 +6,13 @@ #include "base/bind.h" #include "base/values.h" -#include "chrome/browser/chrome_notification_types.h" #include "chrome/browser/extensions/api/commands/command_service.h" #include "chrome/browser/extensions/extension_commands_global_registry.h" #include "chrome/browser/extensions/extension_keybinding_registry.h" -#include "chrome/browser/extensions/extension_service.h" #include "chrome/browser/profiles/profile.h" #include "content/public/browser/web_ui.h" #include "content/public/browser/web_ui_data_source.h" +#include "extensions/browser/extension_registry.h" #include "extensions/browser/extension_system.h" #include "extensions/common/extension_set.h" #include "grit/generated_resources.h" @@ -21,7 +20,9 @@ namespace extensions { -CommandHandler::CommandHandler(Profile* profile) : profile_(profile) { +CommandHandler::CommandHandler(Profile* profile) + : profile_(profile), + extension_registry_observer_(this) { } CommandHandler::~CommandHandler() { @@ -46,11 +47,7 @@ void CommandHandler::GetLocalizedValues(content::WebUIDataSource* source) { } void CommandHandler::RegisterMessages() { - registrar_.Add(this, - chrome::NOTIFICATION_EXTENSION_LOADED_DEPRECATED, - content::Source(profile_)); - registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED, - content::Source(profile_)); + extension_registry_observer_.Add(ExtensionRegistry::Get(profile_)); web_ui()->RegisterMessageCallback("extensionCommandsRequestExtensionsData", base::Bind(&CommandHandler::HandleRequestExtensionsData, @@ -66,12 +63,15 @@ void CommandHandler::RegisterMessages() { base::Unretained(this))); } -void CommandHandler::Observe( - int type, - const content::NotificationSource& source, - const content::NotificationDetails& details) { - DCHECK(type == chrome::NOTIFICATION_EXTENSION_LOADED_DEPRECATED || - type == chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED); +void CommandHandler::OnExtensionLoaded(content::BrowserContext* browser_context, + const Extension* extension) { + UpdateCommandDataOnPage(); +} + +void CommandHandler::OnExtensionUnloaded( + content::BrowserContext* browser_context, + const Extension* extension, + UnloadedExtensionInfo::Reason reason) { UpdateCommandDataOnPage(); } @@ -141,10 +141,11 @@ void CommandHandler::GetAllCommands(base::DictionaryValue* commands) { Profile* profile = Profile::FromWebUI(web_ui()); CommandService* command_service = CommandService::Get(profile); - const ExtensionSet* extensions = extensions::ExtensionSystem::Get(profile)-> - extension_service()->extensions(); - for (ExtensionSet::const_iterator extension = extensions->begin(); - extension != extensions->end(); ++extension) { + const ExtensionSet& extensions = + ExtensionRegistry::Get(profile)->enabled_extensions(); + for (ExtensionSet::const_iterator extension = extensions.begin(); + extension != extensions.end(); + ++extension) { scoped_ptr extension_dict(new base::DictionaryValue); extension_dict->SetString("name", (*extension)->name()); extension_dict->SetString("id", (*extension)->id()); @@ -154,7 +155,7 @@ void CommandHandler::GetAllCommands(base::DictionaryValue* commands) { bool active = false; - extensions::Command browser_action; + Command browser_action; if (command_service->GetBrowserActionCommand((*extension)->id(), CommandService::ALL, &browser_action, @@ -163,7 +164,7 @@ void CommandHandler::GetAllCommands(base::DictionaryValue* commands) { browser_action.ToValue((extension->get()), active)); } - extensions::Command page_action; + Command page_action; if (command_service->GetPageActionCommand((*extension)->id(), CommandService::ALL, &page_action, @@ -171,15 +172,16 @@ void CommandHandler::GetAllCommands(base::DictionaryValue* commands) { extensions_list->Append(page_action.ToValue((extension->get()), active)); } - extensions::CommandMap named_commands; + CommandMap named_commands; if (command_service->GetNamedCommands((*extension)->id(), CommandService::ALL, - extensions::CommandService::ANY_SCOPE, + CommandService::ANY_SCOPE, &named_commands)) { - for (extensions::CommandMap::const_iterator iter = named_commands.begin(); - iter != named_commands.end(); ++iter) { - extensions::Command command = command_service->FindCommandByName( - (*extension)->id(), iter->second.command_name()); + for (CommandMap::const_iterator iter = named_commands.begin(); + iter != named_commands.end(); + ++iter) { + Command command = command_service->FindCommandByName( + (*extension)->id(), iter->second.command_name()); ui::Accelerator shortcut_assigned = command.accelerator(); active = (shortcut_assigned.key_code() != ui::VKEY_UNKNOWN); diff --git a/chrome/browser/ui/webui/extensions/command_handler.h b/chrome/browser/ui/webui/extensions/command_handler.h index c2f557385703..8f6f14bdd388 100644 --- a/chrome/browser/ui/webui/extensions/command_handler.h +++ b/chrome/browser/ui/webui/extensions/command_handler.h @@ -6,9 +6,11 @@ #define CHROME_BROWSER_UI_WEBUI_EXTENSIONS_COMMAND_HANDLER_H_ #include "base/compiler_specific.h" -#include "content/public/browser/notification_observer.h" -#include "content/public/browser/notification_registrar.h" +#include "base/scoped_observer.h" #include "content/public/browser/web_ui_message_handler.h" +#include "extensions/browser/extension_registry_observer.h" + +class Profile; namespace base { class DictionaryValue; @@ -22,16 +24,12 @@ class WebUIDataSource; namespace extensions { class Command; class CommandService; -} - class Extension; -class Profile; - -namespace extensions { +class ExtensionRegistry; // The handler page for the Extension Commands UI overlay. class CommandHandler : public content::WebUIMessageHandler, - public content::NotificationObserver { + public ExtensionRegistryObserver { public: explicit CommandHandler(Profile* profile); virtual ~CommandHandler(); @@ -42,12 +40,15 @@ class CommandHandler : public content::WebUIMessageHandler, // WebUIMessageHandler implementation. virtual void RegisterMessages() OVERRIDE; - // NotificationObserver implementation. - virtual void Observe(int type, - const content::NotificationSource& source, - const content::NotificationDetails& details) OVERRIDE; - private: + // ExtensionRegistryObserver implementation. + virtual void OnExtensionLoaded(content::BrowserContext* browser_context, + const Extension* extension) OVERRIDE; + virtual void OnExtensionUnloaded( + content::BrowserContext* browser_context, + const Extension* extension, + UnloadedExtensionInfo::Reason reason) OVERRIDE; + // Update the list of extension commands in the config UI. void UpdateCommandDataOnPage(); @@ -72,10 +73,11 @@ class CommandHandler : public content::WebUIMessageHandler, // |commands|. void GetAllCommands(base::DictionaryValue* commands); - content::NotificationRegistrar registrar_; - Profile* profile_; + ScopedObserver + extension_registry_observer_; + DISALLOW_COPY_AND_ASSIGN(CommandHandler); }; -- 2.11.4.GIT