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 "chrome/browser/chrome_notification_types.h" | 8 #include "chrome/browser/chrome_notification_types.h" |
9 #include "chrome/browser/extensions/extension_host.h" | 9 #include "chrome/browser/extensions/extension_host.h" |
10 #include "chrome/browser/extensions/extension_system.h" | |
11 #include "chrome/common/extensions/extension_messages.h" | 10 #include "chrome/common/extensions/extension_messages.h" |
12 #include "content/public/browser/browser_context.h" | 11 #include "content/public/browser/browser_context.h" |
13 #include "content/public/browser/notification_service.h" | 12 #include "content/public/browser/notification_service.h" |
14 #include "content/public/browser/render_process_host.h" | 13 #include "content/public/browser/render_process_host.h" |
15 #include "content/public/browser/render_view_host.h" | 14 #include "content/public/browser/render_view_host.h" |
16 #include "content/public/browser/site_instance.h" | 15 #include "content/public/browser/site_instance.h" |
17 #include "content/public/browser/web_contents.h" | 16 #include "content/public/browser/web_contents.h" |
18 #include "extensions/browser/extension_registry.h" | 17 #include "extensions/browser/extension_registry.h" |
| 18 #include "extensions/browser/extension_system.h" |
19 #include "extensions/browser/extensions_browser_client.h" | 19 #include "extensions/browser/extensions_browser_client.h" |
20 #include "extensions/browser/process_manager.h" | 20 #include "extensions/browser/process_manager.h" |
21 #include "extensions/browser/process_map.h" | 21 #include "extensions/browser/process_map.h" |
22 #include "extensions/common/extension.h" | 22 #include "extensions/common/extension.h" |
23 #include "extensions/common/manifest_handlers/background_info.h" | 23 #include "extensions/common/manifest_handlers/background_info.h" |
24 #include "extensions/common/view_type.h" | 24 #include "extensions/common/view_type.h" |
25 | 25 |
26 namespace extensions { | 26 namespace extensions { |
27 | 27 |
28 LazyBackgroundTaskQueue::LazyBackgroundTaskQueue( | 28 LazyBackgroundTaskQueue::LazyBackgroundTaskQueue( |
29 content::BrowserContext* browser_context) | 29 content::BrowserContext* browser_context) |
30 : browser_context_(browser_context) { | 30 : browser_context_(browser_context) { |
31 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_HOST_DID_STOP_LOADING, | 31 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_HOST_DID_STOP_LOADING, |
32 content::NotificationService::AllBrowserContextsAndSources()); | 32 content::NotificationService::AllBrowserContextsAndSources()); |
33 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED, | 33 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED, |
34 content::NotificationService::AllBrowserContextsAndSources()); | 34 content::NotificationService::AllBrowserContextsAndSources()); |
35 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED, | 35 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED, |
36 content::Source<content::BrowserContext>(browser_context)); | 36 content::Source<content::BrowserContext>(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 DCHECK(extension); | 45 DCHECK(extension); |
46 if (BackgroundInfo::HasBackgroundPage(extension)) { | 46 if (BackgroundInfo::HasBackgroundPage(extension)) { |
47 ProcessManager* pm = ExtensionSystem::GetForBrowserContext( | 47 ProcessManager* pm = ExtensionSystem::Get( |
48 browser_context)->process_manager(); | 48 browser_context)->process_manager(); |
49 DCHECK(pm); | 49 DCHECK(pm); |
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->did_stop_loading()) |
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 |
(...skipping 14 matching lines...) Expand all Loading... |
72 if (it == pending_tasks_.end()) { | 72 if (it == pending_tasks_.end()) { |
73 tasks_list = new PendingTasksList(); | 73 tasks_list = new PendingTasksList(); |
74 pending_tasks_[key] = linked_ptr<PendingTasksList>(tasks_list); | 74 pending_tasks_[key] = linked_ptr<PendingTasksList>(tasks_list); |
75 | 75 |
76 const Extension* extension = | 76 const Extension* extension = |
77 ExtensionRegistry::Get(browser_context)->enabled_extensions().GetByID( | 77 ExtensionRegistry::Get(browser_context)->enabled_extensions().GetByID( |
78 extension_id); | 78 extension_id); |
79 if (extension && BackgroundInfo::HasLazyBackgroundPage(extension)) { | 79 if (extension && BackgroundInfo::HasLazyBackgroundPage(extension)) { |
80 // If this is the first enqueued task, and we're not waiting for the | 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. | 81 // background page to unload, ensure the background page is loaded. |
82 ProcessManager* pm = ExtensionSystem::GetForBrowserContext( | 82 ProcessManager* pm = ExtensionSystem::Get( |
83 browser_context)->process_manager(); | 83 browser_context)->process_manager(); |
84 pm->IncrementLazyKeepaliveCount(extension); | 84 pm->IncrementLazyKeepaliveCount(extension); |
85 // Creating the background host may fail, e.g. if |profile| is incognito | 85 // Creating the background host may fail, e.g. if |profile| is incognito |
86 // but the extension isn't enabled in incognito mode. | 86 // but the extension isn't enabled in incognito mode. |
87 if (!pm->CreateBackgroundHost( | 87 if (!pm->CreateBackgroundHost( |
88 extension, BackgroundInfo::GetBackgroundURL(extension))) { | 88 extension, BackgroundInfo::GetBackgroundURL(extension))) { |
89 task.Run(NULL); | 89 task.Run(NULL); |
90 return; | 90 return; |
91 } | 91 } |
92 } | 92 } |
(...skipping 27 matching lines...) Expand all Loading... |
120 for (PendingTasksList::const_iterator it = tasks.begin(); | 120 for (PendingTasksList::const_iterator it = tasks.begin(); |
121 it != tasks.end(); ++it) { | 121 it != tasks.end(); ++it) { |
122 it->Run(host); | 122 it->Run(host); |
123 } | 123 } |
124 | 124 |
125 pending_tasks_.erase(key); | 125 pending_tasks_.erase(key); |
126 | 126 |
127 // Balance the keepalive in AddPendingTask. Note we don't do this on a | 127 // Balance the keepalive in AddPendingTask. Note we don't do this on a |
128 // failure to load, because the keepalive count is reset in that case. | 128 // failure to load, because the keepalive count is reset in that case. |
129 if (host && BackgroundInfo::HasLazyBackgroundPage(extension)) { | 129 if (host && BackgroundInfo::HasLazyBackgroundPage(extension)) { |
130 ExtensionSystem::GetForBrowserContext(browser_context)->process_manager()-> | 130 ExtensionSystem::Get(browser_context)->process_manager()-> |
131 DecrementLazyKeepaliveCount(extension); | 131 DecrementLazyKeepaliveCount(extension); |
132 } | 132 } |
133 } | 133 } |
134 | 134 |
135 void LazyBackgroundTaskQueue::Observe( | 135 void LazyBackgroundTaskQueue::Observe( |
136 int type, | 136 int type, |
137 const content::NotificationSource& source, | 137 const content::NotificationSource& source, |
138 const content::NotificationDetails& details) { | 138 const content::NotificationDetails& details) { |
139 switch (type) { | 139 switch (type) { |
140 case chrome::NOTIFICATION_EXTENSION_HOST_DID_STOP_LOADING: { | 140 case chrome::NOTIFICATION_EXTENSION_HOST_DID_STOP_LOADING: { |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
180 } | 180 } |
181 break; | 181 break; |
182 } | 182 } |
183 default: | 183 default: |
184 NOTREACHED(); | 184 NOTREACHED(); |
185 break; | 185 break; |
186 } | 186 } |
187 } | 187 } |
188 | 188 |
189 } // namespace extensions | 189 } // namespace extensions |
OLD | NEW |