Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/extensions/api/management/management_api.h" | 5 #include "chrome/browser/extensions/api/management/management_api.h" |
| 6 | 6 |
| 7 #include <map> | |
| 8 #include <string> | 7 #include <string> |
| 9 #include <vector> | 8 #include <vector> |
| 10 | 9 |
| 11 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| 12 #include "base/bind.h" | 11 #include "base/bind.h" |
| 13 #include "base/json/json_writer.h" | 12 #include "base/json/json_writer.h" |
| 14 #include "base/lazy_instance.h" | 13 #include "base/lazy_instance.h" |
| 15 #include "base/logging.h" | 14 #include "base/logging.h" |
| 16 #include "base/memory/linked_ptr.h" | 15 #include "base/memory/linked_ptr.h" |
| 17 #include "base/memory/scoped_ptr.h" | 16 #include "base/memory/scoped_ptr.h" |
| 18 #include "base/metrics/histogram.h" | 17 #include "base/metrics/histogram.h" |
| 19 #include "base/strings/string_number_conversions.h" | 18 #include "base/strings/string_number_conversions.h" |
| 20 #include "base/strings/string_util.h" | 19 #include "base/strings/string_util.h" |
| 21 #include "base/strings/utf_string_conversions.h" | 20 #include "base/strings/utf_string_conversions.h" |
| 22 #include "chrome/browser/chrome_notification_types.h" | |
| 23 #include "chrome/browser/extensions/api/management/management_api_constants.h" | 21 #include "chrome/browser/extensions/api/management/management_api_constants.h" |
| 24 #include "chrome/browser/extensions/extension_service.h" | 22 #include "chrome/browser/extensions/extension_service.h" |
| 25 #include "chrome/browser/extensions/extension_ui_util.h" | 23 #include "chrome/browser/extensions/extension_ui_util.h" |
| 26 #include "chrome/browser/extensions/extension_uninstall_dialog.h" | 24 #include "chrome/browser/extensions/extension_uninstall_dialog.h" |
| 27 #include "chrome/browser/extensions/launch_util.h" | 25 #include "chrome/browser/extensions/launch_util.h" |
| 28 #include "chrome/browser/favicon/favicon_service_factory.h" | 26 #include "chrome/browser/favicon/favicon_service_factory.h" |
| 29 #include "chrome/browser/profiles/profile.h" | 27 #include "chrome/browser/profiles/profile.h" |
| 30 #include "chrome/browser/ui/browser_dialogs.h" | 28 #include "chrome/browser/ui/browser_dialogs.h" |
| 31 #include "chrome/browser/ui/browser_finder.h" | 29 #include "chrome/browser/ui/browser_finder.h" |
| 32 #include "chrome/browser/ui/browser_window.h" | 30 #include "chrome/browser/ui/browser_window.h" |
| 33 #include "chrome/browser/ui/extensions/application_launch.h" | 31 #include "chrome/browser/ui/extensions/application_launch.h" |
| 34 #include "chrome/browser/ui/webui/extensions/extension_icon_source.h" | 32 #include "chrome/browser/ui/webui/extensions/extension_icon_source.h" |
| 35 #include "chrome/common/chrome_switches.h" | 33 #include "chrome/common/chrome_switches.h" |
| 36 #include "chrome/common/chrome_utility_messages.h" | 34 #include "chrome/common/chrome_utility_messages.h" |
| 37 #include "chrome/common/extensions/api/management.h" | 35 #include "chrome/common/extensions/api/management.h" |
| 38 #include "chrome/common/extensions/extension_constants.h" | 36 #include "chrome/common/extensions/extension_constants.h" |
| 39 #include "chrome/common/extensions/manifest_handlers/app_launch_info.h" | 37 #include "chrome/common/extensions/manifest_handlers/app_launch_info.h" |
| 40 #include "chrome/common/extensions/manifest_url_handler.h" | 38 #include "chrome/common/extensions/manifest_url_handler.h" |
| 41 #include "content/public/browser/notification_details.h" | |
| 42 #include "content/public/browser/notification_source.h" | |
| 43 #include "content/public/browser/utility_process_host.h" | 39 #include "content/public/browser/utility_process_host.h" |
| 44 #include "content/public/browser/utility_process_host_client.h" | 40 #include "content/public/browser/utility_process_host_client.h" |
| 45 #include "extensions/browser/event_router.h" | 41 #include "extensions/browser/event_router.h" |
| 46 #include "extensions/browser/extension_prefs.h" | 42 #include "extensions/browser/extension_prefs.h" |
| 47 #include "extensions/browser/extension_registry.h" | 43 #include "extensions/browser/extension_registry.h" |
| 48 #include "extensions/browser/extension_system.h" | 44 #include "extensions/browser/extension_system.h" |
| 49 #include "extensions/browser/management_policy.h" | 45 #include "extensions/browser/management_policy.h" |
| 50 #include "extensions/common/constants.h" | 46 #include "extensions/common/constants.h" |
| 51 #include "extensions/common/error_utils.h" | 47 #include "extensions/common/error_utils.h" |
| 52 #include "extensions/common/extension.h" | 48 #include "extensions/common/extension.h" |
| (...skipping 799 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 852 return true; | 848 return true; |
| 853 } | 849 } |
| 854 | 850 |
| 855 ManagementGenerateAppForLinkFunction::ManagementGenerateAppForLinkFunction() { | 851 ManagementGenerateAppForLinkFunction::ManagementGenerateAppForLinkFunction() { |
| 856 } | 852 } |
| 857 | 853 |
| 858 ManagementGenerateAppForLinkFunction::~ManagementGenerateAppForLinkFunction() { | 854 ManagementGenerateAppForLinkFunction::~ManagementGenerateAppForLinkFunction() { |
| 859 } | 855 } |
| 860 | 856 |
| 861 void ManagementGenerateAppForLinkFunction::FinishCreateBookmarkApp( | 857 void ManagementGenerateAppForLinkFunction::FinishCreateBookmarkApp( |
| 862 const extensions::Extension* extension, | 858 const Extension* extension, |
| 863 const WebApplicationInfo& web_app_info) { | 859 const WebApplicationInfo& web_app_info) { |
| 864 if (extension) { | 860 if (extension) { |
| 865 scoped_ptr<management::ExtensionInfo> info = | 861 scoped_ptr<management::ExtensionInfo> info = |
| 866 CreateExtensionInfo(*extension, ExtensionSystem::Get(GetProfile())); | 862 CreateExtensionInfo(*extension, ExtensionSystem::Get(GetProfile())); |
| 867 results_ = management::GenerateAppForLink::Results::Create(*info); | 863 results_ = management::GenerateAppForLink::Results::Create(*info); |
| 868 | 864 |
| 869 SendResponse(true); | 865 SendResponse(true); |
| 870 Release(); | 866 Release(); |
| 871 } else { | 867 } else { |
| 872 error_ = keys::kGenerateAppForLinkInstallError; | 868 error_ = keys::kGenerateAppForLinkInstallError; |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 930 base::Bind(&ManagementGenerateAppForLinkFunction::OnFaviconForApp, this), | 926 base::Bind(&ManagementGenerateAppForLinkFunction::OnFaviconForApp, this), |
| 931 &cancelable_task_tracker_); | 927 &cancelable_task_tracker_); |
| 932 | 928 |
| 933 // Matched with a Release() in OnExtensionLoaded(). | 929 // Matched with a Release() in OnExtensionLoaded(). |
| 934 AddRef(); | 930 AddRef(); |
| 935 | 931 |
| 936 // Response is sent async in OnExtensionLoaded(). | 932 // Response is sent async in OnExtensionLoaded(). |
| 937 return true; | 933 return true; |
| 938 } | 934 } |
| 939 | 935 |
| 940 ManagementEventRouter::ManagementEventRouter(Profile* profile) | 936 ManagementEventRouter::ManagementEventRouter(content::BrowserContext* context) |
| 941 : profile_(profile) { | 937 : browser_context_(context), extension_registry_observer_(this) { |
| 942 int types[] = {chrome::NOTIFICATION_EXTENSION_INSTALLED_DEPRECATED, | 938 extension_registry_observer_.Add(ExtensionRegistry::Get(browser_context_)); |
| 943 chrome::NOTIFICATION_EXTENSION_UNINSTALLED_DEPRECATED, | |
| 944 chrome::NOTIFICATION_EXTENSION_LOADED_DEPRECATED, | |
| 945 chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED}; | |
| 946 | |
| 947 CHECK(registrar_.IsEmpty()); | |
| 948 for (size_t i = 0; i < arraysize(types); i++) { | |
| 949 registrar_.Add(this, | |
| 950 types[i], | |
| 951 content::Source<Profile>(profile_)); | |
| 952 } | |
| 953 } | 939 } |
| 954 | 940 |
| 955 ManagementEventRouter::~ManagementEventRouter() {} | 941 ManagementEventRouter::~ManagementEventRouter() {} |
| 956 | 942 |
| 957 void ManagementEventRouter::Observe( | 943 void ManagementEventRouter::OnExtensionLoaded( |
| 958 int type, | 944 content::BrowserContext* browser_context, |
| 959 const content::NotificationSource& source, | 945 const Extension* extension) { |
| 960 const content::NotificationDetails& details) { | 946 BroadcastEvent(extension, browser_context, management::OnEnabled::kEventName); |
| 961 const char* event_name = NULL; | 947 } |
| 962 const Extension* extension = NULL; | |
| 963 Profile* profile = content::Source<Profile>(source).ptr(); | |
| 964 CHECK(profile); | |
| 965 CHECK(profile_->IsSameProfile(profile)); | |
| 966 | 948 |
| 967 switch (type) { | 949 void ManagementEventRouter::OnExtensionUnloaded( |
| 968 case chrome::NOTIFICATION_EXTENSION_INSTALLED_DEPRECATED: | 950 content::BrowserContext* browser_context, |
| 969 event_name = management::OnInstalled::kEventName; | 951 const Extension* extension, |
| 970 extension = | 952 UnloadedExtensionInfo::Reason reason) { |
| 971 content::Details<const InstalledExtensionInfo>(details)->extension; | 953 BroadcastEvent( |
| 972 break; | 954 extension, browser_context, management::OnDisabled::kEventName); |
| 973 case chrome::NOTIFICATION_EXTENSION_UNINSTALLED_DEPRECATED: | 955 } |
| 974 event_name = management::OnUninstalled::kEventName; | |
| 975 extension = content::Details<const Extension>(details).ptr(); | |
| 976 break; | |
| 977 case chrome::NOTIFICATION_EXTENSION_LOADED_DEPRECATED: | |
| 978 event_name = management::OnEnabled::kEventName; | |
| 979 extension = content::Details<const Extension>(details).ptr(); | |
| 980 break; | |
| 981 case chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED: | |
| 982 event_name = management::OnDisabled::kEventName; | |
| 983 extension = | |
| 984 content::Details<const UnloadedExtensionInfo>(details)->extension; | |
| 985 break; | |
| 986 default: | |
| 987 NOTREACHED(); | |
| 988 return; | |
| 989 } | |
| 990 DCHECK(event_name); | |
| 991 DCHECK(extension); | |
| 992 | 956 |
| 993 if (ui_util::ShouldNotBeVisible(extension, profile_)) | 957 void ManagementEventRouter::OnExtensionInstalled( |
| 994 return; // Don't dispatch events for built-in extensions. | 958 content::BrowserContext* browser_context, |
| 959 const Extension* extension) { | |
| 960 BroadcastEvent( | |
| 961 extension, browser_context, management::OnInstalled::kEventName); | |
| 962 } | |
| 995 | 963 |
| 964 void ManagementEventRouter::OnExtensionUninstalled( | |
| 965 content::BrowserContext* browser_context, | |
| 966 const Extension* extension) { | |
| 967 if (ui_util::ShouldNotBeVisible(extension, browser_context_)) | |
| 968 return; | |
| 996 scoped_ptr<base::ListValue> args(new base::ListValue()); | 969 scoped_ptr<base::ListValue> args(new base::ListValue()); |
| 997 if (event_name == management::OnUninstalled::kEventName) { | 970 args->Append(new base::StringValue(extension->id())); |
| 998 args->Append(new base::StringValue(extension->id())); | 971 |
| 999 } else { | 972 scoped_ptr<Event> event( |
| 1000 scoped_ptr<management::ExtensionInfo> info = CreateExtensionInfo( | 973 new Event(management::OnUninstalled::kEventName, args.Pass())); |
| 1001 *extension, ExtensionSystem::Get(profile)); | 974 EventRouter::Get(browser_context)->BroadcastEvent(event.Pass()); |
| 1002 args->Append(info->ToValue().release()); | 975 } |
| 1003 } | 976 |
| 977 void ManagementEventRouter::BroadcastEvent( | |
| 978 const Extension* extension, | |
| 979 content::BrowserContext* browser_context, | |
| 980 const char* event_name) { | |
| 981 if (ui_util::ShouldNotBeVisible(extension, browser_context_)) | |
| 982 return; // Don't dispatch events for built-in extensions. | |
| 983 scoped_ptr<base::ListValue> args(new base::ListValue()); | |
| 984 scoped_ptr<management::ExtensionInfo> info = | |
|
Devlin
2014/06/18 16:04:34
A few small things:
- You actually don't need to p
limasdf
2014/06/19 01:48:42
Done.
| |
| 985 CreateExtensionInfo(*extension, ExtensionSystem::Get(browser_context)); | |
| 986 args->Append(info->ToValue().release()); | |
| 1004 | 987 |
| 1005 scoped_ptr<Event> event(new Event(event_name, args.Pass())); | 988 scoped_ptr<Event> event(new Event(event_name, args.Pass())); |
| 1006 EventRouter::Get(profile)->BroadcastEvent(event.Pass()); | 989 EventRouter::Get(browser_context)->BroadcastEvent(event.Pass()); |
| 1007 } | 990 } |
| 1008 | 991 |
| 1009 ManagementAPI::ManagementAPI(content::BrowserContext* context) | 992 ManagementAPI::ManagementAPI(content::BrowserContext* context) |
| 1010 : browser_context_(context) { | 993 : browser_context_(context) { |
| 1011 EventRouter* event_router = EventRouter::Get(browser_context_); | 994 EventRouter* event_router = EventRouter::Get(browser_context_); |
| 1012 event_router->RegisterObserver(this, management::OnInstalled::kEventName); | 995 event_router->RegisterObserver(this, management::OnInstalled::kEventName); |
| 1013 event_router->RegisterObserver(this, management::OnUninstalled::kEventName); | 996 event_router->RegisterObserver(this, management::OnUninstalled::kEventName); |
| 1014 event_router->RegisterObserver(this, management::OnEnabled::kEventName); | 997 event_router->RegisterObserver(this, management::OnEnabled::kEventName); |
| 1015 event_router->RegisterObserver(this, management::OnDisabled::kEventName); | 998 event_router->RegisterObserver(this, management::OnDisabled::kEventName); |
| 1016 } | 999 } |
| 1017 | 1000 |
| 1018 ManagementAPI::~ManagementAPI() { | 1001 ManagementAPI::~ManagementAPI() { |
| 1019 } | 1002 } |
| 1020 | 1003 |
| 1021 void ManagementAPI::Shutdown() { | 1004 void ManagementAPI::Shutdown() { |
| 1022 EventRouter::Get(browser_context_)->UnregisterObserver(this); | 1005 EventRouter::Get(browser_context_)->UnregisterObserver(this); |
| 1023 } | 1006 } |
| 1024 | 1007 |
| 1025 static base::LazyInstance<BrowserContextKeyedAPIFactory<ManagementAPI> > | 1008 static base::LazyInstance<BrowserContextKeyedAPIFactory<ManagementAPI> > |
| 1026 g_factory = LAZY_INSTANCE_INITIALIZER; | 1009 g_factory = LAZY_INSTANCE_INITIALIZER; |
| 1027 | 1010 |
| 1028 // static | 1011 // static |
| 1029 BrowserContextKeyedAPIFactory<ManagementAPI>* | 1012 BrowserContextKeyedAPIFactory<ManagementAPI>* |
| 1030 ManagementAPI::GetFactoryInstance() { | 1013 ManagementAPI::GetFactoryInstance() { |
| 1031 return g_factory.Pointer(); | 1014 return g_factory.Pointer(); |
| 1032 } | 1015 } |
| 1033 | 1016 |
| 1034 void ManagementAPI::OnListenerAdded(const EventListenerInfo& details) { | 1017 void ManagementAPI::OnListenerAdded(const EventListenerInfo& details) { |
| 1035 management_event_router_.reset( | 1018 management_event_router_.reset(new ManagementEventRouter(browser_context_)); |
| 1036 new ManagementEventRouter(Profile::FromBrowserContext(browser_context_))); | |
| 1037 EventRouter::Get(browser_context_)->UnregisterObserver(this); | 1019 EventRouter::Get(browser_context_)->UnregisterObserver(this); |
| 1038 } | 1020 } |
| 1039 | 1021 |
| 1040 } // namespace extensions | 1022 } // namespace extensions |
| OLD | NEW |