OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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/extension_gcm_app_handler.h" | 5 #include "chrome/browser/extensions/extension_gcm_app_handler.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/lazy_instance.h" | 8 #include "base/lazy_instance.h" |
9 #include "base/location.h" | 9 #include "base/location.h" |
10 #include "chrome/browser/chrome_notification_types.h" | 10 #include "chrome/browser/chrome_notification_types.h" |
11 #include "chrome/browser/profiles/profile.h" | 11 #include "chrome/browser/profiles/profile.h" |
12 #include "chrome/browser/services/gcm/gcm_profile_service.h" | 12 #include "chrome/browser/services/gcm/gcm_profile_service.h" |
13 #include "chrome/browser/services/gcm/gcm_profile_service_factory.h" | 13 #include "chrome/browser/services/gcm/gcm_profile_service_factory.h" |
14 #include "components/gcm_driver/gcm_driver.h" | 14 #include "components/gcm_driver/gcm_driver.h" |
15 #include "extensions/browser/extension_registry.h" | 15 #include "extensions/browser/extension_registry.h" |
16 #include "extensions/browser/extension_system.h" | 16 #include "extensions/browser/extension_system.h" |
17 #include "extensions/common/extension.h" | 17 #include "extensions/common/extension.h" |
18 #include "extensions/common/permissions/permissions_data.h" | 18 #include "extensions/common/permissions/permissions_data.h" |
19 | 19 |
20 #if !defined(OS_ANDROID) | 20 #if !defined(OS_ANDROID) |
21 #include "chrome/browser/extensions/api/gcm/gcm_api.h" | 21 #include "chrome/browser/extensions/api/gcm/gcm_api.h" |
22 #endif | 22 #endif |
23 | 23 |
24 namespace extensions { | 24 namespace extensions { |
25 | 25 |
26 namespace { | 26 namespace { |
27 | 27 |
| 28 const char kDummyAppId[] = "extension.guard.dummy.id"; |
| 29 |
28 base::LazyInstance<BrowserContextKeyedAPIFactory<ExtensionGCMAppHandler> > | 30 base::LazyInstance<BrowserContextKeyedAPIFactory<ExtensionGCMAppHandler> > |
29 g_factory = LAZY_INSTANCE_INITIALIZER; | 31 g_factory = LAZY_INSTANCE_INITIALIZER; |
30 | 32 |
31 bool IsGCMPermissionEnabled(const Extension* extension) { | 33 bool IsGCMPermissionEnabled(const Extension* extension) { |
32 return extension->permissions_data()->HasAPIPermission(APIPermission::kGcm); | 34 return extension->permissions_data()->HasAPIPermission(APIPermission::kGcm); |
33 } | 35 } |
34 | 36 |
35 } // namespace | 37 } // namespace |
36 | 38 |
37 | 39 |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
87 const gcm::GCMClient::SendErrorDetails& send_error_details) { | 89 const gcm::GCMClient::SendErrorDetails& send_error_details) { |
88 #if !defined(OS_ANDROID) | 90 #if !defined(OS_ANDROID) |
89 js_event_router_->OnSendError(app_id, send_error_details); | 91 js_event_router_->OnSendError(app_id, send_error_details); |
90 #endif | 92 #endif |
91 } | 93 } |
92 | 94 |
93 void ExtensionGCMAppHandler::OnExtensionLoaded( | 95 void ExtensionGCMAppHandler::OnExtensionLoaded( |
94 content::BrowserContext* browser_context, | 96 content::BrowserContext* browser_context, |
95 const Extension* extension) { | 97 const Extension* extension) { |
96 if (IsGCMPermissionEnabled(extension)) | 98 if (IsGCMPermissionEnabled(extension)) |
97 GetGCMDriver()->AddAppHandler(extension->id(), this); | 99 AddAppHandler(extension->id()); |
98 } | 100 } |
99 | 101 |
100 void ExtensionGCMAppHandler::OnExtensionUnloaded( | 102 void ExtensionGCMAppHandler::OnExtensionUnloaded( |
101 content::BrowserContext* browser_context, | 103 content::BrowserContext* browser_context, |
102 const Extension* extension, | 104 const Extension* extension, |
103 UnloadedExtensionInfo::Reason reason) { | 105 UnloadedExtensionInfo::Reason reason) { |
104 if (IsGCMPermissionEnabled(extension)) | 106 if (!IsGCMPermissionEnabled(extension)) |
105 GetGCMDriver()->RemoveAppHandler(extension->id()); | 107 return; |
| 108 |
| 109 if (reason == UnloadedExtensionInfo::REASON_UPDATE && |
| 110 GetGCMDriver()->app_handlers().size() == 1) { |
| 111 // When the extension is being updated, it will be first unloaded and then |
| 112 // loaded again by ExtensionService::AddExtension. If the app handler for |
| 113 // this extension is the only handler, removing it and adding it again will |
| 114 // cause the GCM service being stopped and restarted unnecessarily. To work |
| 115 // around this, we add a dummy app handler to guard against it. This dummy |
| 116 // app handler will be removed once the extension loading logic is done. |
| 117 // |
| 118 // Also note that the GCM message routing will not be interruptted during |
| 119 // the update process since unloading and reloading extension are done in |
| 120 // the single function ExtensionService::AddExtension. |
| 121 AddDummyAppHandler(); |
| 122 |
| 123 base::MessageLoop::current()->PostTask( |
| 124 FROM_HERE, |
| 125 base::Bind(&ExtensionGCMAppHandler::RemoveDummyAppHandler, |
| 126 weak_factory_.GetWeakPtr())); |
| 127 } |
| 128 |
| 129 RemoveAppHandler(extension->id()); |
106 } | 130 } |
107 | 131 |
108 void ExtensionGCMAppHandler::OnExtensionUninstalled( | 132 void ExtensionGCMAppHandler::OnExtensionUninstalled( |
109 content::BrowserContext* browser_context, | 133 content::BrowserContext* browser_context, |
110 const Extension* extension) { | 134 const Extension* extension) { |
111 if (IsGCMPermissionEnabled(extension)) { | 135 if (IsGCMPermissionEnabled(extension)) { |
112 GetGCMDriver()->Unregister( | 136 GetGCMDriver()->Unregister( |
113 extension->id(), | 137 extension->id(), |
114 base::Bind(&ExtensionGCMAppHandler::OnUnregisterCompleted, | 138 base::Bind(&ExtensionGCMAppHandler::OnUnregisterCompleted, |
115 weak_factory_.GetWeakPtr(), | 139 weak_factory_.GetWeakPtr(), |
116 extension->id())); | 140 extension->id())); |
117 GetGCMDriver()->RemoveAppHandler(extension->id()); | 141 RemoveAppHandler(extension->id()); |
118 } | 142 } |
119 } | 143 } |
120 | 144 |
| 145 void ExtensionGCMAppHandler::AddDummyAppHandler() { |
| 146 AddAppHandler(kDummyAppId); |
| 147 } |
| 148 |
| 149 void ExtensionGCMAppHandler::RemoveDummyAppHandler() { |
| 150 RemoveAppHandler(kDummyAppId); |
| 151 } |
| 152 |
121 gcm::GCMDriver* ExtensionGCMAppHandler::GetGCMDriver() const { | 153 gcm::GCMDriver* ExtensionGCMAppHandler::GetGCMDriver() const { |
122 return gcm::GCMProfileServiceFactory::GetForProfile(profile_)->driver(); | 154 return gcm::GCMProfileServiceFactory::GetForProfile(profile_)->driver(); |
123 } | 155 } |
124 | 156 |
125 void ExtensionGCMAppHandler::OnUnregisterCompleted( | 157 void ExtensionGCMAppHandler::OnUnregisterCompleted( |
126 const std::string& app_id, gcm::GCMClient::Result result) { | 158 const std::string& app_id, gcm::GCMClient::Result result) { |
127 // Nothing to do. | 159 // Nothing to do. |
128 } | 160 } |
129 | 161 |
| 162 void ExtensionGCMAppHandler::AddAppHandler(const std::string& app_id) { |
| 163 GetGCMDriver()->AddAppHandler(app_id, this); |
| 164 } |
| 165 |
| 166 void ExtensionGCMAppHandler::RemoveAppHandler(const std::string& app_id) { |
| 167 GetGCMDriver()->RemoveAppHandler(app_id); |
| 168 } |
| 169 |
130 } // namespace extensions | 170 } // namespace extensions |
OLD | NEW |