Switch global error menu icon to vectorized MD asset
[chromium-blink-merge.git] / chrome / browser / extensions / extension_gcm_app_handler.cc
blob4d9e2accb4834a879bd08b2a0a4121514d135fdd
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"
7 #include "base/bind.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 {
28 namespace {
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);
39 } // namespace
42 // static
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),
51 weak_factory_(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();
61 ++extension) {
62 if (IsGCMPermissionEnabled(extension->get()))
63 GetGCMDriver()->RemoveAppHandler((*extension)->id());
67 void ExtensionGCMAppHandler::ShutdownHandler() {
68 js_event_router_.reset();
71 void ExtensionGCMAppHandler::OnMessage(const std::string& app_id,
72 const gcm::IncomingMessage& message) {
73 js_event_router_->OnMessage(app_id, message);
76 void ExtensionGCMAppHandler::OnMessagesDeleted(const std::string& app_id) {
77 js_event_router_->OnMessagesDeleted(app_id);
80 void ExtensionGCMAppHandler::OnSendError(
81 const std::string& app_id,
82 const gcm::GCMClient::SendErrorDetails& send_error_details) {
83 js_event_router_->OnSendError(app_id, send_error_details);
86 void ExtensionGCMAppHandler::OnSendAcknowledged(
87 const std::string& app_id,
88 const std::string& message_id) {
89 // This event is not exposed to JS API. It terminates here.
92 void ExtensionGCMAppHandler::OnExtensionLoaded(
93 content::BrowserContext* browser_context,
94 const Extension* extension) {
95 if (IsGCMPermissionEnabled(extension))
96 AddAppHandler(extension->id());
99 void ExtensionGCMAppHandler::OnExtensionUnloaded(
100 content::BrowserContext* browser_context,
101 const Extension* extension,
102 UnloadedExtensionInfo::Reason reason) {
103 if (!IsGCMPermissionEnabled(extension))
104 return;
106 if (reason == UnloadedExtensionInfo::REASON_UPDATE &&
107 GetGCMDriver()->app_handlers().size() >= 1) {
108 // When the extension is being updated, it will be first unloaded and then
109 // loaded again by ExtensionService::AddExtension. If the app handler for
110 // this extension is the only handler, removing it and adding it again will
111 // cause the GCM service being stopped and restarted unnecessarily. To work
112 // around this, we add a dummy app handler to guard against it. This dummy
113 // app handler will be removed once the extension loading logic is done.
115 // Note that this dummy app handler is added when there is at least one
116 // handler. This is because there might be a built-in app handler, like
117 // GCMAccountMapper, which is automatically added and removed by
118 // GCMDriverDesktop.
120 // Also note that the GCM message routing will not be interruptted during
121 // the update process since unloading and reloading extension are done in
122 // the single function ExtensionService::AddExtension.
123 AddDummyAppHandler();
125 base::ThreadTaskRunnerHandle::Get()->PostTask(
126 FROM_HERE, base::Bind(&ExtensionGCMAppHandler::RemoveDummyAppHandler,
127 weak_factory_.GetWeakPtr()));
130 // When the extention is being uninstalled, it will be unloaded first. We
131 // should not remove the app handler in this case and it will be handled
132 // in OnExtensionUninstalled.
133 if (reason != UnloadedExtensionInfo::REASON_UNINSTALL)
134 RemoveAppHandler(extension->id());
137 void ExtensionGCMAppHandler::OnExtensionUninstalled(
138 content::BrowserContext* browser_context,
139 const Extension* extension,
140 extensions::UninstallReason reason) {
141 if (IsGCMPermissionEnabled(extension)) {
142 // Let's first remove InstanceID data. GCM unregistration will be triggered
143 // after the asynchronous call is returned in OnDeleteIDCompleted.
144 GetInstanceIDDriver()->GetInstanceID(extension->id())->DeleteID(
145 base::Bind(&ExtensionGCMAppHandler::OnDeleteIDCompleted,
146 weak_factory_.GetWeakPtr(),
147 extension->id()));
151 void ExtensionGCMAppHandler::AddDummyAppHandler() {
152 AddAppHandler(kDummyAppId);
155 void ExtensionGCMAppHandler::RemoveDummyAppHandler() {
156 RemoveAppHandler(kDummyAppId);
159 gcm::GCMDriver* ExtensionGCMAppHandler::GetGCMDriver() const {
160 return gcm::GCMProfileServiceFactory::GetForProfile(profile_)->driver();
163 instance_id::InstanceIDDriver* ExtensionGCMAppHandler::GetInstanceIDDriver()
164 const {
165 return instance_id::InstanceIDProfileServiceFactory::GetForProfile(profile_)->
166 driver();
169 void ExtensionGCMAppHandler::OnUnregisterCompleted(
170 const std::string& app_id, gcm::GCMClient::Result result) {
171 RemoveAppHandler(app_id);
174 void ExtensionGCMAppHandler::OnDeleteIDCompleted(
175 const std::string& app_id, instance_id::InstanceID::Result result) {
176 GetGCMDriver()->Unregister(
177 app_id,
178 base::Bind(&ExtensionGCMAppHandler::OnUnregisterCompleted,
179 weak_factory_.GetWeakPtr(),
180 app_id));
182 // InstanceIDDriver::RemoveInstanceID will delete the InstanceID itself.
183 // Postpone to do it outside this calling context to avoid any risk to
184 // the caller.
185 base::ThreadTaskRunnerHandle::Get()->PostTask(
186 FROM_HERE, base::Bind(&ExtensionGCMAppHandler::RemoveInstanceID,
187 weak_factory_.GetWeakPtr(), app_id));
190 void ExtensionGCMAppHandler::RemoveInstanceID(const std::string& app_id) {
191 GetInstanceIDDriver()->RemoveInstanceID(app_id);
194 void ExtensionGCMAppHandler::AddAppHandler(const std::string& app_id) {
195 GetGCMDriver()->AddAppHandler(app_id, this);
198 void ExtensionGCMAppHandler::RemoveAppHandler(const std::string& app_id) {
199 GetGCMDriver()->RemoveAppHandler(app_id);
202 } // namespace extensions