Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / extensions / browser / lazy_background_task_queue.cc
blob44a527f9bd3bec8d1a99b20edfafc4f724ee8d8f
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/extension_system.h"
17 #include "extensions/browser/extensions_browser_client.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/extension_messages.h"
23 #include "extensions/common/manifest_handlers/background_info.h"
24 #include "extensions/common/view_type.h"
26 namespace extensions {
28 LazyBackgroundTaskQueue::LazyBackgroundTaskQueue(
29 content::BrowserContext* browser_context)
30 : browser_context_(browser_context), extension_registry_observer_(this) {
31 registrar_.Add(this,
32 extensions::NOTIFICATION_EXTENSION_HOST_DID_STOP_LOADING,
33 content::NotificationService::AllBrowserContextsAndSources());
34 registrar_.Add(this,
35 extensions::NOTIFICATION_EXTENSION_HOST_DESTROYED,
36 content::NotificationService::AllBrowserContextsAndSources());
38 extension_registry_observer_.Add(ExtensionRegistry::Get(browser_context));
41 LazyBackgroundTaskQueue::~LazyBackgroundTaskQueue() {
44 bool LazyBackgroundTaskQueue::ShouldEnqueueTask(
45 content::BrowserContext* browser_context,
46 const Extension* extension) {
47 // Note: browser_context may not be the same as browser_context_ for incognito
48 // extension tasks.
49 DCHECK(extension);
50 if (BackgroundInfo::HasBackgroundPage(extension)) {
51 ProcessManager* pm = ExtensionSystem::Get(
52 browser_context)->process_manager();
53 DCHECK(pm);
54 ExtensionHost* background_host =
55 pm->GetBackgroundHostForExtension(extension->id());
56 if (!background_host || !background_host->did_stop_loading())
57 return true;
58 if (pm->IsBackgroundHostClosing(extension->id()))
59 pm->CancelSuspend(extension);
62 return false;
65 void LazyBackgroundTaskQueue::AddPendingTask(
66 content::BrowserContext* browser_context,
67 const std::string& extension_id,
68 const PendingTask& task) {
69 if (ExtensionsBrowserClient::Get()->IsShuttingDown()) {
70 task.Run(NULL);
71 return;
73 PendingTasksList* tasks_list = NULL;
74 PendingTasksKey key(browser_context, extension_id);
75 PendingTasksMap::iterator it = pending_tasks_.find(key);
76 if (it == pending_tasks_.end()) {
77 tasks_list = new PendingTasksList();
78 pending_tasks_[key] = linked_ptr<PendingTasksList>(tasks_list);
80 const Extension* extension =
81 ExtensionRegistry::Get(browser_context)->enabled_extensions().GetByID(
82 extension_id);
83 if (extension && BackgroundInfo::HasLazyBackgroundPage(extension)) {
84 // If this is the first enqueued task, and we're not waiting for the
85 // background page to unload, ensure the background page is loaded.
86 ProcessManager* pm = ExtensionSystem::Get(
87 browser_context)->process_manager();
88 pm->IncrementLazyKeepaliveCount(extension);
89 // Creating the background host may fail, e.g. if |profile| is incognito
90 // but the extension isn't enabled in incognito mode.
91 if (!pm->CreateBackgroundHost(
92 extension, BackgroundInfo::GetBackgroundURL(extension))) {
93 task.Run(NULL);
94 return;
97 } else {
98 tasks_list = it->second.get();
101 tasks_list->push_back(task);
104 void LazyBackgroundTaskQueue::ProcessPendingTasks(
105 ExtensionHost* host,
106 content::BrowserContext* browser_context,
107 const Extension* extension) {
108 if (!ExtensionsBrowserClient::Get()->IsSameContext(browser_context,
109 browser_context_))
110 return;
112 PendingTasksKey key(browser_context, extension->id());
113 PendingTasksMap::iterator map_it = pending_tasks_.find(key);
114 if (map_it == pending_tasks_.end()) {
115 if (BackgroundInfo::HasLazyBackgroundPage(extension))
116 CHECK(!host); // lazy page should not load without any pending tasks
117 return;
120 // Swap the pending tasks to a temporary, to avoid problems if the task
121 // list is modified during processing.
122 PendingTasksList tasks;
123 tasks.swap(*map_it->second);
124 for (PendingTasksList::const_iterator it = tasks.begin();
125 it != tasks.end(); ++it) {
126 it->Run(host);
129 pending_tasks_.erase(key);
131 // Balance the keepalive in AddPendingTask. Note we don't do this on a
132 // failure to load, because the keepalive count is reset in that case.
133 if (host && BackgroundInfo::HasLazyBackgroundPage(extension)) {
134 ExtensionSystem::Get(browser_context)->process_manager()->
135 DecrementLazyKeepaliveCount(extension);
139 void LazyBackgroundTaskQueue::Observe(
140 int type,
141 const content::NotificationSource& source,
142 const content::NotificationDetails& details) {
143 switch (type) {
144 case extensions::NOTIFICATION_EXTENSION_HOST_DID_STOP_LOADING: {
145 // If an on-demand background page finished loading, dispatch queued up
146 // events for it.
147 ExtensionHost* host =
148 content::Details<ExtensionHost>(details).ptr();
149 if (host->extension_host_type() == VIEW_TYPE_EXTENSION_BACKGROUND_PAGE) {
150 CHECK(host->did_stop_loading());
151 ProcessPendingTasks(host, host->browser_context(), host->extension());
153 break;
155 case extensions::NOTIFICATION_EXTENSION_HOST_DESTROYED: {
156 // Notify consumers about the load failure when the background host dies.
157 // This can happen if the extension crashes. This is not strictly
158 // necessary, since we also unload the extension in that case (which
159 // dispatches the tasks below), but is a good extra precaution.
160 content::BrowserContext* browser_context =
161 content::Source<content::BrowserContext>(source).ptr();
162 ExtensionHost* host =
163 content::Details<ExtensionHost>(details).ptr();
164 if (host->extension_host_type() == VIEW_TYPE_EXTENSION_BACKGROUND_PAGE) {
165 ProcessPendingTasks(NULL, browser_context, host->extension());
167 break;
169 default:
170 NOTREACHED();
171 break;
175 void LazyBackgroundTaskQueue::OnExtensionUnloaded(
176 content::BrowserContext* browser_context,
177 const Extension* extension,
178 UnloadedExtensionInfo::Reason reason) {
179 // Notify consumers that the page failed to load.
180 ProcessPendingTasks(NULL, browser_context, extension);
181 // If this extension is also running in an off-the-record context, notify that
182 // task queue as well.
183 ExtensionsBrowserClient* browser_client = ExtensionsBrowserClient::Get();
184 if (browser_client->HasOffTheRecordContext(browser_context)) {
185 ProcessPendingTasks(NULL,
186 browser_client->GetOffTheRecordContext(browser_context),
187 extension);
191 } // namespace extensions