blob: 44a527f9bd3bec8d1a99b20edfafc4f724ee8d8f [file] [log] [blame]
[email protected]9fe42042013-10-29 21:13:331// Copyright 2013 The Chromium Authors. All rights reserved.
[email protected]b6536df2012-03-16 18:55:232// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]9fe42042013-10-29 21:13:335#include "extensions/browser/lazy_background_task_queue.h"
[email protected]b6536df2012-03-16 18:55:236
7#include "base/callback.h"
[email protected]9fe42042013-10-29 21:13:338#include "content/public/browser/browser_context.h"
[email protected]b6536df2012-03-16 18:55:239#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"
[email protected]22401dc2014-03-21 01:38:5714#include "extensions/browser/extension_host.h"
[email protected]5fdfa562013-12-27 17:43:5915#include "extensions/browser/extension_registry.h"
[email protected]59b0e602014-01-30 00:41:2416#include "extensions/browser/extension_system.h"
[email protected]9fe42042013-10-29 21:13:3317#include "extensions/browser/extensions_browser_client.h"
[email protected]adf5a102014-07-31 12:44:0618#include "extensions/browser/notification_types.h"
[email protected]98b6d942013-11-10 00:34:0719#include "extensions/browser/process_manager.h"
[email protected]50de9aa22013-11-14 06:30:3420#include "extensions/browser/process_map.h"
[email protected]e4452d32013-11-15 23:07:4121#include "extensions/common/extension.h"
[email protected]fb820c02014-03-13 15:07:0822#include "extensions/common/extension_messages.h"
[email protected]558878cc82013-11-09 01:25:5123#include "extensions/common/manifest_handlers/background_info.h"
[email protected]cb2edf22013-04-01 20:25:2324#include "extensions/common/view_type.h"
[email protected]b6536df2012-03-16 18:55:2325
[email protected]83055ea72012-04-05 18:56:3626namespace extensions {
27
[email protected]9fe42042013-10-29 21:13:3328LazyBackgroundTaskQueue::LazyBackgroundTaskQueue(
29 content::BrowserContext* browser_context)
[email protected]c3bb7182014-07-16 21:15:1830 : browser_context_(browser_context), extension_registry_observer_(this) {
[email protected]adf5a102014-07-31 12:44:0631 registrar_.Add(this,
32 extensions::NOTIFICATION_EXTENSION_HOST_DID_STOP_LOADING,
[email protected]b6536df2012-03-16 18:55:2333 content::NotificationService::AllBrowserContextsAndSources());
[email protected]adf5a102014-07-31 12:44:0634 registrar_.Add(this,
35 extensions::NOTIFICATION_EXTENSION_HOST_DESTROYED,
[email protected]76a383e2012-04-13 18:46:0136 content::NotificationService::AllBrowserContextsAndSources());
[email protected]c3bb7182014-07-16 21:15:1837
38 extension_registry_observer_.Add(ExtensionRegistry::Get(browser_context));
[email protected]b6536df2012-03-16 18:55:2339}
40
41LazyBackgroundTaskQueue::~LazyBackgroundTaskQueue() {
42}
43
[email protected]d79e3ab2012-04-03 18:28:3644bool LazyBackgroundTaskQueue::ShouldEnqueueTask(
[email protected]9fe42042013-10-29 21:13:3345 content::BrowserContext* browser_context,
46 const Extension* extension) {
[email protected]9fc5bdc82014-08-03 23:49:2647 // Note: browser_context may not be the same as browser_context_ for incognito
48 // extension tasks.
[email protected]d79e3ab2012-04-03 18:28:3649 DCHECK(extension);
[email protected]9367eabc2013-03-01 01:29:2950 if (BackgroundInfo::HasBackgroundPage(extension)) {
[email protected]59b0e602014-01-30 00:41:2451 ProcessManager* pm = ExtensionSystem::Get(
[email protected]9fe42042013-10-29 21:13:3352 browser_context)->process_manager();
[email protected]f7240212013-10-27 03:39:1253 DCHECK(pm);
[email protected]d79e3ab2012-04-03 18:28:3654 ExtensionHost* background_host =
55 pm->GetBackgroundHostForExtension(extension->id());
[email protected]0d475e072012-07-26 02:30:4256 if (!background_host || !background_host->did_stop_loading())
[email protected]d79e3ab2012-04-03 18:28:3657 return true;
[email protected]0d475e072012-07-26 02:30:4258 if (pm->IsBackgroundHostClosing(extension->id()))
59 pm->CancelSuspend(extension);
[email protected]d79e3ab2012-04-03 18:28:3660 }
61
62 return false;
63}
64
[email protected]b6536df2012-03-16 18:55:2365void LazyBackgroundTaskQueue::AddPendingTask(
[email protected]9fe42042013-10-29 21:13:3366 content::BrowserContext* browser_context,
[email protected]b6536df2012-03-16 18:55:2367 const std::string& extension_id,
68 const PendingTask& task) {
[email protected]9fe42042013-10-29 21:13:3369 if (ExtensionsBrowserClient::Get()->IsShuttingDown()) {
[email protected]09510772013-09-05 00:08:4770 task.Run(NULL);
71 return;
72 }
[email protected]b6536df2012-03-16 18:55:2373 PendingTasksList* tasks_list = NULL;
[email protected]9fe42042013-10-29 21:13:3374 PendingTasksKey key(browser_context, extension_id);
[email protected]b6536df2012-03-16 18:55:2375 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);
79
[email protected]ed78d3fd2012-08-09 03:59:5780 const Extension* extension =
[email protected]5fdfa562013-12-27 17:43:5981 ExtensionRegistry::Get(browser_context)->enabled_extensions().GetByID(
82 extension_id);
[email protected]9367eabc2013-03-01 01:29:2983 if (extension && BackgroundInfo::HasLazyBackgroundPage(extension)) {
[email protected]546fc9d2012-08-18 04:10:0684 // 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.
[email protected]59b0e602014-01-30 00:41:2486 ProcessManager* pm = ExtensionSystem::Get(
[email protected]9fe42042013-10-29 21:13:3387 browser_context)->process_manager();
[email protected]546fc9d2012-08-18 04:10:0688 pm->IncrementLazyKeepaliveCount(extension);
[email protected]61aa8c62013-10-01 00:43:0789 // 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;
95 }
[email protected]546fc9d2012-08-18 04:10:0696 }
[email protected]b6536df2012-03-16 18:55:2397 } else {
98 tasks_list = it->second.get();
99 }
100
101 tasks_list->push_back(task);
102}
103
[email protected]1ad12ef2012-04-16 19:26:22104void LazyBackgroundTaskQueue::ProcessPendingTasks(
105 ExtensionHost* host,
[email protected]9fe42042013-10-29 21:13:33106 content::BrowserContext* browser_context,
[email protected]1ad12ef2012-04-16 19:26:22107 const Extension* extension) {
[email protected]9fe42042013-10-29 21:13:33108 if (!ExtensionsBrowserClient::Get()->IsSameContext(browser_context,
109 browser_context_))
[email protected]1ad12ef2012-04-16 19:26:22110 return;
111
[email protected]9fe42042013-10-29 21:13:33112 PendingTasksKey key(browser_context, extension->id());
[email protected]b6536df2012-03-16 18:55:23113 PendingTasksMap::iterator map_it = pending_tasks_.find(key);
114 if (map_it == pending_tasks_.end()) {
[email protected]9367eabc2013-03-01 01:29:29115 if (BackgroundInfo::HasLazyBackgroundPage(extension))
[email protected]546fc9d2012-08-18 04:10:06116 CHECK(!host); // lazy page should not load without any pending tasks
[email protected]b6536df2012-03-16 18:55:23117 return;
118 }
119
[email protected]b49fd0bdb2012-07-25 20:30:56120 // 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) {
[email protected]b6536df2012-03-16 18:55:23126 it->Run(host);
127 }
128
[email protected]b49fd0bdb2012-07-25 20:30:56129 pending_tasks_.erase(key);
[email protected]b6536df2012-03-16 18:55:23130
[email protected]1ad12ef2012-04-16 19:26:22131 // 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.
[email protected]9367eabc2013-03-01 01:29:29133 if (host && BackgroundInfo::HasLazyBackgroundPage(extension)) {
[email protected]59b0e602014-01-30 00:41:24134 ExtensionSystem::Get(browser_context)->process_manager()->
[email protected]1ad12ef2012-04-16 19:26:22135 DecrementLazyKeepaliveCount(extension);
136 }
[email protected]b6536df2012-03-16 18:55:23137}
138
139void LazyBackgroundTaskQueue::Observe(
140 int type,
141 const content::NotificationSource& source,
142 const content::NotificationDetails& details) {
143 switch (type) {
[email protected]adf5a102014-07-31 12:44:06144 case extensions::NOTIFICATION_EXTENSION_HOST_DID_STOP_LOADING: {
[email protected]b6536df2012-03-16 18:55:23145 // If an on-demand background page finished loading, dispatch queued up
146 // events for it.
[email protected]3a1dc572012-07-31 22:25:13147 ExtensionHost* host =
148 content::Details<ExtensionHost>(details).ptr();
[email protected]cb2edf22013-04-01 20:25:23149 if (host->extension_host_type() == VIEW_TYPE_EXTENSION_BACKGROUND_PAGE) {
[email protected]b6536df2012-03-16 18:55:23150 CHECK(host->did_stop_loading());
[email protected]9fe42042013-10-29 21:13:33151 ProcessPendingTasks(host, host->browser_context(), host->extension());
[email protected]b6536df2012-03-16 18:55:23152 }
153 break;
154 }
[email protected]adf5a102014-07-31 12:44:06155 case extensions::NOTIFICATION_EXTENSION_HOST_DESTROYED: {
[email protected]ed78d3fd2012-08-09 03:59:57156 // 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.
[email protected]9fe42042013-10-29 21:13:33160 content::BrowserContext* browser_context =
161 content::Source<content::BrowserContext>(source).ptr();
[email protected]3a1dc572012-07-31 22:25:13162 ExtensionHost* host =
163 content::Details<ExtensionHost>(details).ptr();
[email protected]cb2edf22013-04-01 20:25:23164 if (host->extension_host_type() == VIEW_TYPE_EXTENSION_BACKGROUND_PAGE) {
[email protected]9fe42042013-10-29 21:13:33165 ProcessPendingTasks(NULL, browser_context, host->extension());
[email protected]76a383e2012-04-13 18:46:01166 }
167 break;
168 }
[email protected]b6536df2012-03-16 18:55:23169 default:
170 NOTREACHED();
171 break;
172 }
173}
[email protected]83055ea72012-04-05 18:56:36174
[email protected]c3bb7182014-07-16 21:15:18175void 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);
188 }
189}
190
[email protected]83055ea72012-04-05 18:56:36191} // namespace extensions