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
3 // found in the LICENSE file.
5 #include "extensions/browser/lazy_background_task_queue.h"
7 #include "base/callback.h"
8 #include "content/public/browser/browser_context.h"
9 #include "content/public/browser/notification_service.h"
10 #include "content/public/browser/render_process_host.h"
11 #include "content/public/browser/render_view_host.h"
12 #include "content/public/browser/site_instance.h"
13 #include "content/public/browser/web_contents.h"
14 #include "extensions/browser/extension_host.h"
15 #include "extensions/browser/extension_registry.h"
16 #include "extensions/browser/extensions_browser_client.h"
17 #include "extensions/browser/lazy_background_task_queue_factory.h"
18 #include "extensions/browser/notification_types.h"
19 #include "extensions/browser/process_manager.h"
20 #include "extensions/browser/process_map.h"
21 #include "extensions/common/extension.h"
22 #include "extensions/common/manifest_handlers/background_info.h"
23 #include "extensions/common/view_type.h"
25 namespace extensions
{
27 LazyBackgroundTaskQueue::LazyBackgroundTaskQueue(
28 content::BrowserContext
* browser_context
)
29 : browser_context_(browser_context
), extension_registry_observer_(this) {
31 extensions::NOTIFICATION_EXTENSION_HOST_DID_STOP_FIRST_LOAD
,
32 content::NotificationService::AllBrowserContextsAndSources());
34 extensions::NOTIFICATION_EXTENSION_HOST_DESTROYED
,
35 content::NotificationService::AllBrowserContextsAndSources());
37 extension_registry_observer_
.Add(ExtensionRegistry::Get(browser_context
));
40 LazyBackgroundTaskQueue::~LazyBackgroundTaskQueue() {
44 LazyBackgroundTaskQueue
* LazyBackgroundTaskQueue::Get(
45 content::BrowserContext
* browser_context
) {
46 return LazyBackgroundTaskQueueFactory::GetForBrowserContext(browser_context
);
49 bool LazyBackgroundTaskQueue::ShouldEnqueueTask(
50 content::BrowserContext
* browser_context
,
51 const Extension
* extension
) {
52 // Note: browser_context may not be the same as browser_context_ for incognito
55 if (BackgroundInfo::HasBackgroundPage(extension
)) {
56 ProcessManager
* pm
= ProcessManager::Get(browser_context
);
57 ExtensionHost
* background_host
=
58 pm
->GetBackgroundHostForExtension(extension
->id());
59 if (!background_host
|| !background_host
->has_loaded_once())
61 if (pm
->IsBackgroundHostClosing(extension
->id()))
62 pm
->CancelSuspend(extension
);
68 void LazyBackgroundTaskQueue::AddPendingTask(
69 content::BrowserContext
* browser_context
,
70 const std::string
& extension_id
,
71 const PendingTask
& task
) {
72 if (ExtensionsBrowserClient::Get()->IsShuttingDown()) {
76 PendingTasksList
* tasks_list
= NULL
;
77 PendingTasksKey
key(browser_context
, extension_id
);
78 PendingTasksMap::iterator it
= pending_tasks_
.find(key
);
79 if (it
== pending_tasks_
.end()) {
80 tasks_list
= new PendingTasksList();
81 pending_tasks_
[key
] = linked_ptr
<PendingTasksList
>(tasks_list
);
83 const Extension
* extension
=
84 ExtensionRegistry::Get(browser_context
)->enabled_extensions().GetByID(
86 if (extension
&& BackgroundInfo::HasLazyBackgroundPage(extension
)) {
87 // If this is the first enqueued task, and we're not waiting for the
88 // background page to unload, ensure the background page is loaded.
89 ProcessManager
* pm
= ProcessManager::Get(browser_context
);
90 pm
->IncrementLazyKeepaliveCount(extension
);
91 // Creating the background host may fail, e.g. if |profile| is incognito
92 // but the extension isn't enabled in incognito mode.
93 if (!pm
->CreateBackgroundHost(
94 extension
, BackgroundInfo::GetBackgroundURL(extension
))) {
100 tasks_list
= it
->second
.get();
103 tasks_list
->push_back(task
);
106 void LazyBackgroundTaskQueue::ProcessPendingTasks(
108 content::BrowserContext
* browser_context
,
109 const Extension
* extension
) {
110 if (!ExtensionsBrowserClient::Get()->IsSameContext(browser_context
,
114 PendingTasksKey
key(browser_context
, extension
->id());
115 PendingTasksMap::iterator map_it
= pending_tasks_
.find(key
);
116 if (map_it
== pending_tasks_
.end()) {
117 if (BackgroundInfo::HasLazyBackgroundPage(extension
))
118 CHECK(!host
); // lazy page should not load without any pending tasks
122 // Swap the pending tasks to a temporary, to avoid problems if the task
123 // list is modified during processing.
124 PendingTasksList tasks
;
125 tasks
.swap(*map_it
->second
);
126 for (PendingTasksList::const_iterator it
= tasks
.begin();
127 it
!= tasks
.end(); ++it
) {
131 pending_tasks_
.erase(key
);
133 // Balance the keepalive in AddPendingTask. Note we don't do this on a
134 // failure to load, because the keepalive count is reset in that case.
135 if (host
&& BackgroundInfo::HasLazyBackgroundPage(extension
)) {
136 ProcessManager::Get(browser_context
)
137 ->DecrementLazyKeepaliveCount(extension
);
141 void LazyBackgroundTaskQueue::Observe(
143 const content::NotificationSource
& source
,
144 const content::NotificationDetails
& details
) {
146 case extensions::NOTIFICATION_EXTENSION_HOST_DID_STOP_FIRST_LOAD
: {
147 // If an on-demand background page finished loading, dispatch queued up
149 ExtensionHost
* host
=
150 content::Details
<ExtensionHost
>(details
).ptr();
151 if (host
->extension_host_type() == VIEW_TYPE_EXTENSION_BACKGROUND_PAGE
) {
152 CHECK(host
->has_loaded_once());
153 ProcessPendingTasks(host
, host
->browser_context(), host
->extension());
157 case extensions::NOTIFICATION_EXTENSION_HOST_DESTROYED
: {
158 // Notify consumers about the load failure when the background host dies.
159 // This can happen if the extension crashes. This is not strictly
160 // necessary, since we also unload the extension in that case (which
161 // dispatches the tasks below), but is a good extra precaution.
162 content::BrowserContext
* browser_context
=
163 content::Source
<content::BrowserContext
>(source
).ptr();
164 ExtensionHost
* host
=
165 content::Details
<ExtensionHost
>(details
).ptr();
166 if (host
->extension_host_type() == VIEW_TYPE_EXTENSION_BACKGROUND_PAGE
) {
167 ProcessPendingTasks(NULL
, browser_context
, host
->extension());
177 void LazyBackgroundTaskQueue::OnExtensionUnloaded(
178 content::BrowserContext
* browser_context
,
179 const Extension
* extension
,
180 UnloadedExtensionInfo::Reason reason
) {
181 // Notify consumers that the page failed to load.
182 ProcessPendingTasks(NULL
, browser_context
, extension
);
183 // If this extension is also running in an off-the-record context, notify that
184 // task queue as well.
185 ExtensionsBrowserClient
* browser_client
= ExtensionsBrowserClient::Get();
186 if (browser_client
->HasOffTheRecordContext(browser_context
)) {
187 ProcessPendingTasks(NULL
,
188 browser_client
->GetOffTheRecordContext(browser_context
),
193 } // namespace extensions