| 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 "base/single_thread_task_runner.h" | 10 #include "base/single_thread_task_runner.h" |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 99 void ExtensionGCMAppHandler::OnExtensionLoaded( | 99 void ExtensionGCMAppHandler::OnExtensionLoaded( |
| 100 content::BrowserContext* browser_context, | 100 content::BrowserContext* browser_context, |
| 101 const Extension* extension) { | 101 const Extension* extension) { |
| 102 if (IsGCMPermissionEnabled(extension)) | 102 if (IsGCMPermissionEnabled(extension)) |
| 103 AddAppHandler(extension->id()); | 103 AddAppHandler(extension->id()); |
| 104 } | 104 } |
| 105 | 105 |
| 106 void ExtensionGCMAppHandler::OnExtensionUnloaded( | 106 void ExtensionGCMAppHandler::OnExtensionUnloaded( |
| 107 content::BrowserContext* browser_context, | 107 content::BrowserContext* browser_context, |
| 108 const Extension* extension, | 108 const Extension* extension, |
| 109 UnloadedExtensionInfo::Reason reason) { | 109 UnloadedExtensionReason reason) { |
| 110 if (!IsGCMPermissionEnabled(extension)) | 110 if (!IsGCMPermissionEnabled(extension)) |
| 111 return; | 111 return; |
| 112 | 112 |
| 113 if (reason == UnloadedExtensionInfo::REASON_UPDATE && | 113 if (reason == UnloadedExtensionReason::REASON_UPDATE && |
| 114 GetGCMDriver()->app_handlers().size() >= 1) { | 114 GetGCMDriver()->app_handlers().size() >= 1) { |
| 115 // When the extension is being updated, it will be first unloaded and then | 115 // When the extension is being updated, it will be first unloaded and then |
| 116 // loaded again by ExtensionService::AddExtension. If the app handler for | 116 // loaded again by ExtensionService::AddExtension. If the app handler for |
| 117 // this extension is the only handler, removing it and adding it again will | 117 // this extension is the only handler, removing it and adding it again will |
| 118 // cause the GCM service being stopped and restarted unnecessarily. To work | 118 // cause the GCM service being stopped and restarted unnecessarily. To work |
| 119 // around this, we add a dummy app handler to guard against it. This dummy | 119 // around this, we add a dummy app handler to guard against it. This dummy |
| 120 // app handler will be removed once the extension loading logic is done. | 120 // app handler will be removed once the extension loading logic is done. |
| 121 // | 121 // |
| 122 // Note that this dummy app handler is added when there is at least one | 122 // Note that this dummy app handler is added when there is at least one |
| 123 // handler. This is because there might be a built-in app handler, like | 123 // handler. This is because there might be a built-in app handler, like |
| 124 // GCMAccountMapper, which is automatically added and removed by | 124 // GCMAccountMapper, which is automatically added and removed by |
| 125 // GCMDriverDesktop. | 125 // GCMDriverDesktop. |
| 126 // | 126 // |
| 127 // Also note that the GCM message routing will not be interruptted during | 127 // Also note that the GCM message routing will not be interruptted during |
| 128 // the update process since unloading and reloading extension are done in | 128 // the update process since unloading and reloading extension are done in |
| 129 // the single function ExtensionService::AddExtension. | 129 // the single function ExtensionService::AddExtension. |
| 130 AddDummyAppHandler(); | 130 AddDummyAppHandler(); |
| 131 | 131 |
| 132 base::ThreadTaskRunnerHandle::Get()->PostTask( | 132 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 133 FROM_HERE, | 133 FROM_HERE, |
| 134 base::BindOnce(&ExtensionGCMAppHandler::RemoveDummyAppHandler, | 134 base::BindOnce(&ExtensionGCMAppHandler::RemoveDummyAppHandler, |
| 135 weak_factory_.GetWeakPtr())); | 135 weak_factory_.GetWeakPtr())); |
| 136 } | 136 } |
| 137 | 137 |
| 138 // When the extention is being uninstalled, it will be unloaded first. We | 138 // When the extention is being uninstalled, it will be unloaded first. We |
| 139 // should not remove the app handler in this case and it will be handled | 139 // should not remove the app handler in this case and it will be handled |
| 140 // in OnExtensionUninstalled. | 140 // in OnExtensionUninstalled. |
| 141 if (reason != UnloadedExtensionInfo::REASON_UNINSTALL) | 141 if (reason != UnloadedExtensionReason::REASON_UNINSTALL) |
| 142 RemoveAppHandler(extension->id()); | 142 RemoveAppHandler(extension->id()); |
| 143 } | 143 } |
| 144 | 144 |
| 145 void ExtensionGCMAppHandler::OnExtensionUninstalled( | 145 void ExtensionGCMAppHandler::OnExtensionUninstalled( |
| 146 content::BrowserContext* browser_context, | 146 content::BrowserContext* browser_context, |
| 147 const Extension* extension, | 147 const Extension* extension, |
| 148 extensions::UninstallReason reason) { | 148 extensions::UninstallReason reason) { |
| 149 if (IsGCMPermissionEnabled(extension)) { | 149 if (IsGCMPermissionEnabled(extension)) { |
| 150 // Let's first remove InstanceID data. GCM unregistration will be triggered | 150 // Let's first remove InstanceID data. GCM unregistration will be triggered |
| 151 // after the asynchronous call is returned in OnDeleteIDCompleted. | 151 // after the asynchronous call is returned in OnDeleteIDCompleted. |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 201 | 201 |
| 202 void ExtensionGCMAppHandler::AddAppHandler(const std::string& app_id) { | 202 void ExtensionGCMAppHandler::AddAppHandler(const std::string& app_id) { |
| 203 GetGCMDriver()->AddAppHandler(app_id, this); | 203 GetGCMDriver()->AddAppHandler(app_id, this); |
| 204 } | 204 } |
| 205 | 205 |
| 206 void ExtensionGCMAppHandler::RemoveAppHandler(const std::string& app_id) { | 206 void ExtensionGCMAppHandler::RemoveAppHandler(const std::string& app_id) { |
| 207 GetGCMDriver()->RemoveAppHandler(app_id); | 207 GetGCMDriver()->RemoveAppHandler(app_id); |
| 208 } | 208 } |
| 209 | 209 |
| 210 } // namespace extensions | 210 } // namespace extensions |
| OLD | NEW |