Use native radiobutton for Default Search Engine picker dialog.
[chromium-blink-merge.git] / extensions / browser / lazy_background_task_queue.cc
blobb76b6d36743746c7650e670b99d0e2d9a46949e2
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/notification_types.h"
18 #include "extensions/browser/process_manager.h"
19 #include "extensions/browser/process_map.h"
20 #include "extensions/common/extension.h"
21 #include "extensions/common/manifest_handlers/background_info.h"
22 #include "extensions/common/view_type.h"
24 namespace extensions {
26 LazyBackgroundTaskQueue::LazyBackgroundTaskQueue(
27 content::BrowserContext* browser_context)
28 : browser_context_(browser_context), extension_registry_observer_(this) {
29 registrar_.Add(this,
30 extensions::NOTIFICATION_EXTENSION_HOST_DID_STOP_FIRST_LOAD,
31 content::NotificationService::AllBrowserContextsAndSources());
32 registrar_.Add(this,
33 extensions::NOTIFICATION_EXTENSION_HOST_DESTROYED,
34 content::NotificationService::AllBrowserContextsAndSources());
36 extension_registry_observer_.Add(ExtensionRegistry::Get(browser_context));
39 LazyBackgroundTaskQueue::~LazyBackgroundTaskQueue() {
42 bool LazyBackgroundTaskQueue::ShouldEnqueueTask(
43 content::BrowserContext* browser_context,
44 const Extension* extension) {
45 // Note: browser_context may not be the same as browser_context_ for incognito
46 // extension tasks.
47 DCHECK(extension);
48 if (BackgroundInfo::HasBackgroundPage(extension)) {
49 ProcessManager* pm = ProcessManager::Get(browser_context);
50 ExtensionHost* background_host =
51 pm->GetBackgroundHostForExtension(extension->id());
52 if (!background_host || !background_host->has_loaded_once())
53 return true;
54 if (pm->IsBackgroundHostClosing(extension->id()))
55 pm->CancelSuspend(extension);
58 return false;
61 void LazyBackgroundTaskQueue::AddPendingTask(
62 content::BrowserContext* browser_context,
63 const std::string& extension_id,
64 const PendingTask& task) {
65 if (ExtensionsBrowserClient::Get()->IsShuttingDown()) {
66 task.Run(NULL);
67 return;
69 PendingTasksList* tasks_list = NULL;
70 PendingTasksKey key(browser_context, extension_id);
71 PendingTasksMap::iterator it = pending_tasks_.find(key);
72 if (it == pending_tasks_.end()) {
73 tasks_list = new PendingTasksList();
74 pending_tasks_[key] = linked_ptr<PendingTasksList>(tasks_list);
76 const Extension* extension =
77 ExtensionRegistry::Get(browser_context)->enabled_extensions().GetByID(
78 extension_id);
79 if (extension && BackgroundInfo::HasLazyBackgroundPage(extension)) {
80 // If this is the first enqueued task, and we're not waiting for the
81 // background page to unload, ensure the background page is loaded.
82 ProcessManager* pm = ProcessManager::Get(browser_context);
83 pm->IncrementLazyKeepaliveCount(extension);
84 // Creating the background host may fail, e.g. if |profile| is incognito
85 // but the extension isn't enabled in incognito mode.
86 if (!pm->CreateBackgroundHost(
87 extension, BackgroundInfo::GetBackgroundURL(extension))) {
88 task.Run(NULL);
89 return;
92 } else {
93 tasks_list = it->second.get();
96 tasks_list->push_back(task);
99 void LazyBackgroundTaskQueue::ProcessPendingTasks(
100 ExtensionHost* host,
101 content::BrowserContext* browser_context,
102 const Extension* extension) {
103 if (!ExtensionsBrowserClient::Get()->IsSameContext(browser_context,
104 browser_context_))
105 return;
107 PendingTasksKey key(browser_context, extension->id());
108 PendingTasksMap::iterator map_it = pending_tasks_.find(key);
109 if (map_it == pending_tasks_.end()) {
110 if (BackgroundInfo::HasLazyBackgroundPage(extension))
111 CHECK(!host); // lazy page should not load without any pending tasks
112 return;
115 // Swap the pending tasks to a temporary, to avoid problems if the task
116 // list is modified during processing.
117 PendingTasksList tasks;
118 tasks.swap(*map_it->second);
119 for (PendingTasksList::const_iterator it = tasks.begin();
120 it != tasks.end(); ++it) {
121 it->Run(host);
124 pending_tasks_.erase(key);
126 // Balance the keepalive in AddPendingTask. Note we don't do this on a
127 // failure to load, because the keepalive count is reset in that case.
128 if (host && BackgroundInfo::HasLazyBackgroundPage(extension)) {
129 ProcessManager::Get(browser_context)
130 ->DecrementLazyKeepaliveCount(extension);
134 void LazyBackgroundTaskQueue::Observe(
135 int type,
136 const content::NotificationSource& source,
137 const content::NotificationDetails& details) {
138 switch (type) {
139 case extensions::NOTIFICATION_EXTENSION_HOST_DID_STOP_FIRST_LOAD: {
140 // If an on-demand background page finished loading, dispatch queued up
141 // events for it.
142 ExtensionHost* host =
143 content::Details<ExtensionHost>(details).ptr();
144 if (host->extension_host_type() == VIEW_TYPE_EXTENSION_BACKGROUND_PAGE) {
145 CHECK(host->has_loaded_once());
146 ProcessPendingTasks(host, host->browser_context(), host->extension());
148 break;
150 case extensions::NOTIFICATION_EXTENSION_HOST_DESTROYED: {
151 // Notify consumers about the load failure when the background host dies.
152 // This can happen if the extension crashes. This is not strictly
153 // necessary, since we also unload the extension in that case (which
154 // dispatches the tasks below), but is a good extra precaution.
155 content::BrowserContext* browser_context =
156 content::Source<content::BrowserContext>(source).ptr();
157 ExtensionHost* host =
158 content::Details<ExtensionHost>(details).ptr();
159 if (host->extension_host_type() == VIEW_TYPE_EXTENSION_BACKGROUND_PAGE) {
160 ProcessPendingTasks(NULL, browser_context, host->extension());
162 break;
164 default:
165 NOTREACHED();
166 break;
170 void LazyBackgroundTaskQueue::OnExtensionUnloaded(
171 content::BrowserContext* browser_context,
172 const Extension* extension,
173 UnloadedExtensionInfo::Reason reason) {
174 // Notify consumers that the page failed to load.
175 ProcessPendingTasks(NULL, browser_context, extension);
176 // If this extension is also running in an off-the-record context, notify that
177 // task queue as well.
178 ExtensionsBrowserClient* browser_client = ExtensionsBrowserClient::Get();
179 if (browser_client->HasOffTheRecordContext(browser_context)) {
180 ProcessPendingTasks(NULL,
181 browser_client->GetOffTheRecordContext(browser_context),
182 extension);
186 } // namespace extensions