base::Time multiplicative operator overloading
[chromium-blink-merge.git] / chrome / browser / extensions / extension_gcm_app_handler.cc
blob6a9f27538590fcbe1d3e37fd02c11e1cf78d389b
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 "chrome/browser/chrome_notification_types.h"
11 #include "chrome/browser/extensions/api/gcm/gcm_api.h"
12 #include "chrome/browser/profiles/profile.h"
13 #include "chrome/browser/services/gcm/gcm_profile_service.h"
14 #include "chrome/browser/services/gcm/gcm_profile_service_factory.h"
15 #include "components/gcm_driver/gcm_driver.h"
16 #include "extensions/browser/extension_registry.h"
17 #include "extensions/browser/extension_system.h"
18 #include "extensions/common/extension.h"
19 #include "extensions/common/permissions/permissions_data.h"
21 namespace extensions {
23 namespace {
25 const char kDummyAppId[] = "extension.guard.dummy.id";
27 base::LazyInstance<BrowserContextKeyedAPIFactory<ExtensionGCMAppHandler> >
28 g_factory = LAZY_INSTANCE_INITIALIZER;
30 bool IsGCMPermissionEnabled(const Extension* extension) {
31 return extension->permissions_data()->HasAPIPermission(APIPermission::kGcm);
34 } // namespace
37 // static
38 BrowserContextKeyedAPIFactory<ExtensionGCMAppHandler>*
39 ExtensionGCMAppHandler::GetFactoryInstance() {
40 return g_factory.Pointer();
43 ExtensionGCMAppHandler::ExtensionGCMAppHandler(content::BrowserContext* context)
44 : profile_(Profile::FromBrowserContext(context)),
45 extension_registry_observer_(this),
46 weak_factory_(this) {
47 extension_registry_observer_.Add(ExtensionRegistry::Get(profile_));
48 js_event_router_.reset(new extensions::GcmJsEventRouter(profile_));
51 ExtensionGCMAppHandler::~ExtensionGCMAppHandler() {
52 const ExtensionSet& enabled_extensions =
53 ExtensionRegistry::Get(profile_)->enabled_extensions();
54 for (ExtensionSet::const_iterator extension = enabled_extensions.begin();
55 extension != enabled_extensions.end();
56 ++extension) {
57 if (IsGCMPermissionEnabled(extension->get()))
58 GetGCMDriver()->RemoveAppHandler((*extension)->id());
62 void ExtensionGCMAppHandler::ShutdownHandler() {
63 js_event_router_.reset();
66 void ExtensionGCMAppHandler::OnMessage(
67 const std::string& app_id,
68 const gcm::GCMClient::IncomingMessage& message) {
69 js_event_router_->OnMessage(app_id, message);
72 void ExtensionGCMAppHandler::OnMessagesDeleted(const std::string& app_id) {
73 js_event_router_->OnMessagesDeleted(app_id);
76 void ExtensionGCMAppHandler::OnSendError(
77 const std::string& app_id,
78 const gcm::GCMClient::SendErrorDetails& send_error_details) {
79 js_event_router_->OnSendError(app_id, send_error_details);
82 void ExtensionGCMAppHandler::OnSendAcknowledged(
83 const std::string& app_id,
84 const std::string& message_id) {
85 // This event is not exposed to JS API. It terminates here.
88 void ExtensionGCMAppHandler::OnExtensionLoaded(
89 content::BrowserContext* browser_context,
90 const Extension* extension) {
91 if (IsGCMPermissionEnabled(extension))
92 AddAppHandler(extension->id());
95 void ExtensionGCMAppHandler::OnExtensionUnloaded(
96 content::BrowserContext* browser_context,
97 const Extension* extension,
98 UnloadedExtensionInfo::Reason reason) {
99 if (!IsGCMPermissionEnabled(extension))
100 return;
102 if (reason == UnloadedExtensionInfo::REASON_UPDATE &&
103 GetGCMDriver()->app_handlers().size() >= 1) {
104 // When the extension is being updated, it will be first unloaded and then
105 // loaded again by ExtensionService::AddExtension. If the app handler for
106 // this extension is the only handler, removing it and adding it again will
107 // cause the GCM service being stopped and restarted unnecessarily. To work
108 // around this, we add a dummy app handler to guard against it. This dummy
109 // app handler will be removed once the extension loading logic is done.
111 // Note that this dummy app handler is added when there is at least one
112 // handler. This is because there might be a built-in app handler, like
113 // GCMAccountMapper, which is automatically added and removed by
114 // GCMDriverDesktop.
116 // Also note that the GCM message routing will not be interruptted during
117 // the update process since unloading and reloading extension are done in
118 // the single function ExtensionService::AddExtension.
119 AddDummyAppHandler();
121 base::MessageLoop::current()->PostTask(
122 FROM_HERE,
123 base::Bind(&ExtensionGCMAppHandler::RemoveDummyAppHandler,
124 weak_factory_.GetWeakPtr()));
127 // When the extention is being uninstalled, it will be unloaded first. We
128 // should not remove the app handler in this case and it will be handled
129 // in OnExtensionUninstalled.
130 if (reason != UnloadedExtensionInfo::REASON_UNINSTALL)
131 RemoveAppHandler(extension->id());
134 void ExtensionGCMAppHandler::OnExtensionUninstalled(
135 content::BrowserContext* browser_context,
136 const Extension* extension,
137 extensions::UninstallReason reason) {
138 if (IsGCMPermissionEnabled(extension)) {
139 GetGCMDriver()->Unregister(
140 extension->id(),
141 base::Bind(&ExtensionGCMAppHandler::OnUnregisterCompleted,
142 weak_factory_.GetWeakPtr(),
143 extension->id()));
147 void ExtensionGCMAppHandler::AddDummyAppHandler() {
148 AddAppHandler(kDummyAppId);
151 void ExtensionGCMAppHandler::RemoveDummyAppHandler() {
152 RemoveAppHandler(kDummyAppId);
155 gcm::GCMDriver* ExtensionGCMAppHandler::GetGCMDriver() const {
156 return gcm::GCMProfileServiceFactory::GetForProfile(profile_)->driver();
159 void ExtensionGCMAppHandler::OnUnregisterCompleted(
160 const std::string& app_id, gcm::GCMClient::Result result) {
161 RemoveAppHandler(app_id);
164 void ExtensionGCMAppHandler::AddAppHandler(const std::string& app_id) {
165 GetGCMDriver()->AddAppHandler(app_id, this);
168 void ExtensionGCMAppHandler::RemoveAppHandler(const std::string& app_id) {
169 GetGCMDriver()->RemoveAppHandler(app_id);
172 } // namespace extensions