| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "extensions/browser/lazy_background_task_queue.h" | 5 #include "extensions/browser/lazy_background_task_queue.h" |
| 6 | 6 |
| 7 #include "base/callback.h" | 7 #include "base/callback.h" |
| 8 #include "content/public/browser/browser_context.h" | 8 #include "content/public/browser/browser_context.h" |
| 9 #include "content/public/browser/notification_service.h" | 9 #include "content/public/browser/notification_service.h" |
| 10 #include "content/public/browser/render_process_host.h" | 10 #include "content/public/browser/render_process_host.h" |
| 11 #include "content/public/browser/render_view_host.h" | 11 #include "content/public/browser/render_view_host.h" |
| 12 #include "content/public/browser/site_instance.h" | 12 #include "content/public/browser/site_instance.h" |
| 13 #include "content/public/browser/web_contents.h" | 13 #include "content/public/browser/web_contents.h" |
| 14 #include "extensions/browser/extension_host.h" | 14 #include "extensions/browser/extension_host.h" |
| 15 #include "extensions/browser/extension_registry.h" | 15 #include "extensions/browser/extension_registry.h" |
| 16 #include "extensions/browser/extensions_browser_client.h" | 16 #include "extensions/browser/extensions_browser_client.h" |
| 17 #include "extensions/browser/notification_types.h" | 17 #include "extensions/browser/notification_types.h" |
| 18 #include "extensions/browser/process_manager.h" | 18 #include "extensions/browser/process_manager.h" |
| 19 #include "extensions/browser/process_map.h" | 19 #include "extensions/browser/process_map.h" |
| 20 #include "extensions/common/extension.h" | 20 #include "extensions/common/extension.h" |
| 21 #include "extensions/common/manifest_handlers/background_info.h" | 21 #include "extensions/common/manifest_handlers/background_info.h" |
| 22 #include "extensions/common/view_type.h" | 22 #include "extensions/common/view_type.h" |
| 23 | 23 |
| 24 namespace extensions { | 24 namespace extensions { |
| 25 | 25 |
| 26 LazyBackgroundTaskQueue::LazyBackgroundTaskQueue( | 26 LazyBackgroundTaskQueue::LazyBackgroundTaskQueue( |
| 27 content::BrowserContext* browser_context) | 27 content::BrowserContext* browser_context) |
| 28 : browser_context_(browser_context), extension_registry_observer_(this) { | 28 : browser_context_(browser_context), extension_registry_observer_(this) { |
| 29 registrar_.Add(this, | 29 registrar_.Add(this, |
| 30 extensions::NOTIFICATION_EXTENSION_HOST_DID_STOP_LOADING, | 30 extensions::NOTIFICATION_EXTENSION_HOST_DID_STOP_FIRST_LOAD, |
| 31 content::NotificationService::AllBrowserContextsAndSources()); | 31 content::NotificationService::AllBrowserContextsAndSources()); |
| 32 registrar_.Add(this, | 32 registrar_.Add(this, |
| 33 extensions::NOTIFICATION_EXTENSION_HOST_DESTROYED, | 33 extensions::NOTIFICATION_EXTENSION_HOST_DESTROYED, |
| 34 content::NotificationService::AllBrowserContextsAndSources()); | 34 content::NotificationService::AllBrowserContextsAndSources()); |
| 35 | 35 |
| 36 extension_registry_observer_.Add(ExtensionRegistry::Get(browser_context)); | 36 extension_registry_observer_.Add(ExtensionRegistry::Get(browser_context)); |
| 37 } | 37 } |
| 38 | 38 |
| 39 LazyBackgroundTaskQueue::~LazyBackgroundTaskQueue() { | 39 LazyBackgroundTaskQueue::~LazyBackgroundTaskQueue() { |
| 40 } | 40 } |
| 41 | 41 |
| 42 bool LazyBackgroundTaskQueue::ShouldEnqueueTask( | 42 bool LazyBackgroundTaskQueue::ShouldEnqueueTask( |
| 43 content::BrowserContext* browser_context, | 43 content::BrowserContext* browser_context, |
| 44 const Extension* extension) { | 44 const Extension* extension) { |
| 45 // Note: browser_context may not be the same as browser_context_ for incognito | 45 // Note: browser_context may not be the same as browser_context_ for incognito |
| 46 // extension tasks. | 46 // extension tasks. |
| 47 DCHECK(extension); | 47 DCHECK(extension); |
| 48 if (BackgroundInfo::HasBackgroundPage(extension)) { | 48 if (BackgroundInfo::HasBackgroundPage(extension)) { |
| 49 ProcessManager* pm = ProcessManager::Get(browser_context); | 49 ProcessManager* pm = ProcessManager::Get(browser_context); |
| 50 ExtensionHost* background_host = | 50 ExtensionHost* background_host = |
| 51 pm->GetBackgroundHostForExtension(extension->id()); | 51 pm->GetBackgroundHostForExtension(extension->id()); |
| 52 if (!background_host || !background_host->did_stop_loading()) | 52 if (!background_host || !background_host->has_loaded_once()) |
| 53 return true; | 53 return true; |
| 54 if (pm->IsBackgroundHostClosing(extension->id())) | 54 if (pm->IsBackgroundHostClosing(extension->id())) |
| 55 pm->CancelSuspend(extension); | 55 pm->CancelSuspend(extension); |
| 56 } | 56 } |
| 57 | 57 |
| 58 return false; | 58 return false; |
| 59 } | 59 } |
| 60 | 60 |
| 61 void LazyBackgroundTaskQueue::AddPendingTask( | 61 void LazyBackgroundTaskQueue::AddPendingTask( |
| 62 content::BrowserContext* browser_context, | 62 content::BrowserContext* browser_context, |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 129 ProcessManager::Get(browser_context) | 129 ProcessManager::Get(browser_context) |
| 130 ->DecrementLazyKeepaliveCount(extension); | 130 ->DecrementLazyKeepaliveCount(extension); |
| 131 } | 131 } |
| 132 } | 132 } |
| 133 | 133 |
| 134 void LazyBackgroundTaskQueue::Observe( | 134 void LazyBackgroundTaskQueue::Observe( |
| 135 int type, | 135 int type, |
| 136 const content::NotificationSource& source, | 136 const content::NotificationSource& source, |
| 137 const content::NotificationDetails& details) { | 137 const content::NotificationDetails& details) { |
| 138 switch (type) { | 138 switch (type) { |
| 139 case extensions::NOTIFICATION_EXTENSION_HOST_DID_STOP_LOADING: { | 139 case extensions::NOTIFICATION_EXTENSION_HOST_DID_STOP_FIRST_LOAD: { |
| 140 // If an on-demand background page finished loading, dispatch queued up | 140 // If an on-demand background page finished loading, dispatch queued up |
| 141 // events for it. | 141 // events for it. |
| 142 ExtensionHost* host = | 142 ExtensionHost* host = |
| 143 content::Details<ExtensionHost>(details).ptr(); | 143 content::Details<ExtensionHost>(details).ptr(); |
| 144 if (host->extension_host_type() == VIEW_TYPE_EXTENSION_BACKGROUND_PAGE) { | 144 if (host->extension_host_type() == VIEW_TYPE_EXTENSION_BACKGROUND_PAGE) { |
| 145 CHECK(host->did_stop_loading()); | 145 CHECK(host->has_loaded_once()); |
| 146 ProcessPendingTasks(host, host->browser_context(), host->extension()); | 146 ProcessPendingTasks(host, host->browser_context(), host->extension()); |
| 147 } | 147 } |
| 148 break; | 148 break; |
| 149 } | 149 } |
| 150 case extensions::NOTIFICATION_EXTENSION_HOST_DESTROYED: { | 150 case extensions::NOTIFICATION_EXTENSION_HOST_DESTROYED: { |
| 151 // Notify consumers about the load failure when the background host dies. | 151 // Notify consumers about the load failure when the background host dies. |
| 152 // This can happen if the extension crashes. This is not strictly | 152 // This can happen if the extension crashes. This is not strictly |
| 153 // necessary, since we also unload the extension in that case (which | 153 // necessary, since we also unload the extension in that case (which |
| 154 // dispatches the tasks below), but is a good extra precaution. | 154 // dispatches the tasks below), but is a good extra precaution. |
| 155 content::BrowserContext* browser_context = | 155 content::BrowserContext* browser_context = |
| (...skipping 21 matching lines...) Expand all Loading... |
| 177 // task queue as well. | 177 // task queue as well. |
| 178 ExtensionsBrowserClient* browser_client = ExtensionsBrowserClient::Get(); | 178 ExtensionsBrowserClient* browser_client = ExtensionsBrowserClient::Get(); |
| 179 if (browser_client->HasOffTheRecordContext(browser_context)) { | 179 if (browser_client->HasOffTheRecordContext(browser_context)) { |
| 180 ProcessPendingTasks(NULL, | 180 ProcessPendingTasks(NULL, |
| 181 browser_client->GetOffTheRecordContext(browser_context), | 181 browser_client->GetOffTheRecordContext(browser_context), |
| 182 extension); | 182 extension); |
| 183 } | 183 } |
| 184 } | 184 } |
| 185 | 185 |
| 186 } // namespace extensions | 186 } // namespace extensions |
| OLD | NEW |