1 // Copyright 2014 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.
5 #include "chrome/browser/extensions/extension_gcm_app_handler.h"
8 #include "base/lazy_instance.h"
9 #include "base/location.h"
10 #include "base/single_thread_task_runner.h"
11 #include "base/thread_task_runner_handle.h"
12 #include "chrome/browser/chrome_notification_types.h"
13 #include "chrome/browser/extensions/api/gcm/gcm_api.h"
14 #include "chrome/browser/profiles/profile.h"
15 #include "chrome/browser/services/gcm/gcm_profile_service.h"
16 #include "chrome/browser/services/gcm/gcm_profile_service_factory.h"
17 #include "chrome/browser/services/gcm/instance_id/instance_id_profile_service.h"
18 #include "chrome/browser/services/gcm/instance_id/instance_id_profile_service_factory.h"
19 #include "components/gcm_driver/gcm_driver.h"
20 #include "components/gcm_driver/instance_id/instance_id_driver.h"
21 #include "extensions/browser/extension_registry.h"
22 #include "extensions/browser/extension_system.h"
23 #include "extensions/common/extension.h"
24 #include "extensions/common/permissions/permissions_data.h"
26 namespace extensions
{
30 const char kDummyAppId
[] = "extension.guard.dummy.id";
32 base::LazyInstance
<BrowserContextKeyedAPIFactory
<ExtensionGCMAppHandler
> >
33 g_factory
= LAZY_INSTANCE_INITIALIZER
;
35 bool IsGCMPermissionEnabled(const Extension
* extension
) {
36 return extension
->permissions_data()->HasAPIPermission(APIPermission::kGcm
);
43 BrowserContextKeyedAPIFactory
<ExtensionGCMAppHandler
>*
44 ExtensionGCMAppHandler::GetFactoryInstance() {
45 return g_factory
.Pointer();
48 ExtensionGCMAppHandler::ExtensionGCMAppHandler(content::BrowserContext
* context
)
49 : profile_(Profile::FromBrowserContext(context
)),
50 extension_registry_observer_(this),
52 extension_registry_observer_
.Add(ExtensionRegistry::Get(profile_
));
53 js_event_router_
.reset(new extensions::GcmJsEventRouter(profile_
));
56 ExtensionGCMAppHandler::~ExtensionGCMAppHandler() {
57 const ExtensionSet
& enabled_extensions
=
58 ExtensionRegistry::Get(profile_
)->enabled_extensions();
59 for (ExtensionSet::const_iterator extension
= enabled_extensions
.begin();
60 extension
!= enabled_extensions
.end();
62 if (IsGCMPermissionEnabled(extension
->get()))
63 GetGCMDriver()->RemoveAppHandler((*extension
)->id());
67 void ExtensionGCMAppHandler::ShutdownHandler() {
68 js_event_router_
.reset();
71 void ExtensionGCMAppHandler::OnMessage(
72 const std::string
& app_id
,
73 const gcm::GCMClient::IncomingMessage
& message
) {
74 js_event_router_
->OnMessage(app_id
, message
);
77 void ExtensionGCMAppHandler::OnMessagesDeleted(const std::string
& app_id
) {
78 js_event_router_
->OnMessagesDeleted(app_id
);
81 void ExtensionGCMAppHandler::OnSendError(
82 const std::string
& app_id
,
83 const gcm::GCMClient::SendErrorDetails
& send_error_details
) {
84 js_event_router_
->OnSendError(app_id
, send_error_details
);
87 void ExtensionGCMAppHandler::OnSendAcknowledged(
88 const std::string
& app_id
,
89 const std::string
& message_id
) {
90 // This event is not exposed to JS API. It terminates here.
93 void ExtensionGCMAppHandler::OnExtensionLoaded(
94 content::BrowserContext
* browser_context
,
95 const Extension
* extension
) {
96 if (IsGCMPermissionEnabled(extension
))
97 AddAppHandler(extension
->id());
100 void ExtensionGCMAppHandler::OnExtensionUnloaded(
101 content::BrowserContext
* browser_context
,
102 const Extension
* extension
,
103 UnloadedExtensionInfo::Reason reason
) {
104 if (!IsGCMPermissionEnabled(extension
))
107 if (reason
== UnloadedExtensionInfo::REASON_UPDATE
&&
108 GetGCMDriver()->app_handlers().size() >= 1) {
109 // When the extension is being updated, it will be first unloaded and then
110 // loaded again by ExtensionService::AddExtension. If the app handler for
111 // this extension is the only handler, removing it and adding it again will
112 // cause the GCM service being stopped and restarted unnecessarily. To work
113 // around this, we add a dummy app handler to guard against it. This dummy
114 // app handler will be removed once the extension loading logic is done.
116 // Note that this dummy app handler is added when there is at least one
117 // handler. This is because there might be a built-in app handler, like
118 // GCMAccountMapper, which is automatically added and removed by
121 // Also note that the GCM message routing will not be interruptted during
122 // the update process since unloading and reloading extension are done in
123 // the single function ExtensionService::AddExtension.
124 AddDummyAppHandler();
126 base::ThreadTaskRunnerHandle::Get()->PostTask(
127 FROM_HERE
, base::Bind(&ExtensionGCMAppHandler::RemoveDummyAppHandler
,
128 weak_factory_
.GetWeakPtr()));
131 // When the extention is being uninstalled, it will be unloaded first. We
132 // should not remove the app handler in this case and it will be handled
133 // in OnExtensionUninstalled.
134 if (reason
!= UnloadedExtensionInfo::REASON_UNINSTALL
)
135 RemoveAppHandler(extension
->id());
138 void ExtensionGCMAppHandler::OnExtensionUninstalled(
139 content::BrowserContext
* browser_context
,
140 const Extension
* extension
,
141 extensions::UninstallReason reason
) {
142 if (IsGCMPermissionEnabled(extension
)) {
143 // Let's first remove InstanceID data. GCM unregistration will be triggered
144 // after the asynchronous call is returned in OnDeleteIDCompleted.
145 GetInstanceIDDriver()->GetInstanceID(extension
->id())->DeleteID(
146 base::Bind(&ExtensionGCMAppHandler::OnDeleteIDCompleted
,
147 weak_factory_
.GetWeakPtr(),
152 void ExtensionGCMAppHandler::AddDummyAppHandler() {
153 AddAppHandler(kDummyAppId
);
156 void ExtensionGCMAppHandler::RemoveDummyAppHandler() {
157 RemoveAppHandler(kDummyAppId
);
160 gcm::GCMDriver
* ExtensionGCMAppHandler::GetGCMDriver() const {
161 return gcm::GCMProfileServiceFactory::GetForProfile(profile_
)->driver();
164 instance_id::InstanceIDDriver
* ExtensionGCMAppHandler::GetInstanceIDDriver()
166 return instance_id::InstanceIDProfileServiceFactory::GetForProfile(profile_
)->
170 void ExtensionGCMAppHandler::OnUnregisterCompleted(
171 const std::string
& app_id
, gcm::GCMClient::Result result
) {
172 RemoveAppHandler(app_id
);
175 void ExtensionGCMAppHandler::OnDeleteIDCompleted(
176 const std::string
& app_id
, instance_id::InstanceID::Result result
) {
177 GetGCMDriver()->Unregister(
179 base::Bind(&ExtensionGCMAppHandler::OnUnregisterCompleted
,
180 weak_factory_
.GetWeakPtr(),
183 // InstanceIDDriver::RemoveInstanceID will delete the InstanceID itself.
184 // Postpone to do it outside this calling context to avoid any risk to
186 base::ThreadTaskRunnerHandle::Get()->PostTask(
187 FROM_HERE
, base::Bind(&ExtensionGCMAppHandler::RemoveInstanceID
,
188 weak_factory_
.GetWeakPtr(), app_id
));
191 void ExtensionGCMAppHandler::RemoveInstanceID(const std::string
& app_id
) {
192 GetInstanceIDDriver()->RemoveInstanceID(app_id
);
195 void ExtensionGCMAppHandler::AddAppHandler(const std::string
& app_id
) {
196 GetGCMDriver()->AddAppHandler(app_id
, this);
199 void ExtensionGCMAppHandler::RemoveAppHandler(const std::string
& app_id
) {
200 GetGCMDriver()->RemoveAppHandler(app_id
);
203 } // namespace extensions