OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "chrome/browser/extensions/lazy_background_task_queue.h" | 5 #include "chrome/browser/extensions/lazy_background_task_queue.h" |
6 | 6 |
7 #include "base/callback.h" | 7 #include "base/callback.h" |
8 #include "chrome/browser/extensions/extension_host.h" | 8 #include "chrome/browser/extensions/extension_host.h" |
9 #include "chrome/browser/extensions/extension_process_manager.h" | 9 #include "chrome/browser/extensions/extension_process_manager.h" |
10 #include "chrome/browser/extensions/extension_service.h" | 10 #include "chrome/browser/extensions/extension_service.h" |
11 #include "chrome/browser/extensions/extension_system.h" | 11 #include "chrome/browser/extensions/extension_system.h" |
12 #include "chrome/browser/extensions/process_map.h" | 12 #include "chrome/browser/extensions/process_map.h" |
13 #include "chrome/browser/profiles/profile.h" | 13 #include "chrome/browser/profiles/profile.h" |
14 #include "chrome/common/chrome_notification_types.h" | 14 #include "chrome/common/chrome_notification_types.h" |
15 #include "chrome/common/extensions/background_info.h" | 15 #include "chrome/common/extensions/background_info.h" |
16 #include "chrome/common/extensions/extension.h" | 16 #include "chrome/common/extensions/extension.h" |
17 #include "chrome/common/extensions/extension_messages.h" | 17 #include "chrome/common/extensions/extension_messages.h" |
18 #include "chrome/common/view_type.h" | |
19 #include "content/public/browser/notification_service.h" | 18 #include "content/public/browser/notification_service.h" |
20 #include "content/public/browser/render_process_host.h" | 19 #include "content/public/browser/render_process_host.h" |
21 #include "content/public/browser/render_view_host.h" | 20 #include "content/public/browser/render_view_host.h" |
22 #include "content/public/browser/site_instance.h" | 21 #include "content/public/browser/site_instance.h" |
23 #include "content/public/browser/web_contents.h" | 22 #include "content/public/browser/web_contents.h" |
| 23 #include "extensions/common/view_type.h" |
24 | 24 |
25 namespace extensions { | 25 namespace extensions { |
26 | 26 |
27 LazyBackgroundTaskQueue::LazyBackgroundTaskQueue(Profile* profile) | 27 LazyBackgroundTaskQueue::LazyBackgroundTaskQueue(Profile* profile) |
28 : profile_(profile) { | 28 : profile_(profile) { |
29 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_HOST_DID_STOP_LOADING, | 29 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_HOST_DID_STOP_LOADING, |
30 content::NotificationService::AllBrowserContextsAndSources()); | 30 content::NotificationService::AllBrowserContextsAndSources()); |
31 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED, | 31 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED, |
32 content::NotificationService::AllBrowserContextsAndSources()); | 32 content::NotificationService::AllBrowserContextsAndSources()); |
33 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED, | 33 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED, |
34 content::Source<Profile>(profile)); | 34 content::Source<Profile>(profile)); |
35 } | 35 } |
36 | 36 |
37 LazyBackgroundTaskQueue::~LazyBackgroundTaskQueue() { | 37 LazyBackgroundTaskQueue::~LazyBackgroundTaskQueue() { |
38 } | 38 } |
39 | 39 |
40 bool LazyBackgroundTaskQueue::ShouldEnqueueTask( | 40 bool LazyBackgroundTaskQueue::ShouldEnqueueTask( |
41 Profile* profile, const Extension* extension) { | 41 Profile* profile, const Extension* extension) { |
42 DCHECK(extension); | 42 DCHECK(extension); |
43 if (BackgroundInfo::HasBackgroundPage(extension)) { | 43 if (BackgroundInfo::HasBackgroundPage(extension)) { |
44 ExtensionProcessManager* pm = extensions::ExtensionSystem::Get(profile)-> | 44 ExtensionProcessManager* pm = ExtensionSystem::Get(profile)-> |
45 process_manager(); | 45 process_manager(); |
46 ExtensionHost* background_host = | 46 ExtensionHost* background_host = |
47 pm->GetBackgroundHostForExtension(extension->id()); | 47 pm->GetBackgroundHostForExtension(extension->id()); |
48 if (!background_host || !background_host->did_stop_loading()) | 48 if (!background_host || !background_host->did_stop_loading()) |
49 return true; | 49 return true; |
50 if (pm->IsBackgroundHostClosing(extension->id())) | 50 if (pm->IsBackgroundHostClosing(extension->id())) |
51 pm->CancelSuspend(extension); | 51 pm->CancelSuspend(extension); |
52 } | 52 } |
53 | 53 |
54 return false; | 54 return false; |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
121 void LazyBackgroundTaskQueue::Observe( | 121 void LazyBackgroundTaskQueue::Observe( |
122 int type, | 122 int type, |
123 const content::NotificationSource& source, | 123 const content::NotificationSource& source, |
124 const content::NotificationDetails& details) { | 124 const content::NotificationDetails& details) { |
125 switch (type) { | 125 switch (type) { |
126 case chrome::NOTIFICATION_EXTENSION_HOST_DID_STOP_LOADING: { | 126 case chrome::NOTIFICATION_EXTENSION_HOST_DID_STOP_LOADING: { |
127 // If an on-demand background page finished loading, dispatch queued up | 127 // If an on-demand background page finished loading, dispatch queued up |
128 // events for it. | 128 // events for it. |
129 ExtensionHost* host = | 129 ExtensionHost* host = |
130 content::Details<ExtensionHost>(details).ptr(); | 130 content::Details<ExtensionHost>(details).ptr(); |
131 if (host->extension_host_type() == | 131 if (host->extension_host_type() == VIEW_TYPE_EXTENSION_BACKGROUND_PAGE) { |
132 chrome::VIEW_TYPE_EXTENSION_BACKGROUND_PAGE) { | |
133 CHECK(host->did_stop_loading()); | 132 CHECK(host->did_stop_loading()); |
134 ProcessPendingTasks(host, host->profile(), host->extension()); | 133 ProcessPendingTasks(host, host->profile(), host->extension()); |
135 } | 134 } |
136 break; | 135 break; |
137 } | 136 } |
138 case chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED: { | 137 case chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED: { |
139 // Notify consumers about the load failure when the background host dies. | 138 // Notify consumers about the load failure when the background host dies. |
140 // This can happen if the extension crashes. This is not strictly | 139 // This can happen if the extension crashes. This is not strictly |
141 // necessary, since we also unload the extension in that case (which | 140 // necessary, since we also unload the extension in that case (which |
142 // dispatches the tasks below), but is a good extra precaution. | 141 // dispatches the tasks below), but is a good extra precaution. |
143 Profile* profile = content::Source<Profile>(source).ptr(); | 142 Profile* profile = content::Source<Profile>(source).ptr(); |
144 ExtensionHost* host = | 143 ExtensionHost* host = |
145 content::Details<ExtensionHost>(details).ptr(); | 144 content::Details<ExtensionHost>(details).ptr(); |
146 if (host->extension_host_type() == | 145 if (host->extension_host_type() == VIEW_TYPE_EXTENSION_BACKGROUND_PAGE) { |
147 chrome::VIEW_TYPE_EXTENSION_BACKGROUND_PAGE) { | |
148 ProcessPendingTasks(NULL, profile, host->extension()); | 146 ProcessPendingTasks(NULL, profile, host->extension()); |
149 } | 147 } |
150 break; | 148 break; |
151 } | 149 } |
152 case chrome::NOTIFICATION_EXTENSION_UNLOADED: { | 150 case chrome::NOTIFICATION_EXTENSION_UNLOADED: { |
153 // Notify consumers that the page failed to load. | 151 // Notify consumers that the page failed to load. |
154 Profile* profile = content::Source<Profile>(source).ptr(); | 152 Profile* profile = content::Source<Profile>(source).ptr(); |
155 UnloadedExtensionInfo* unloaded = | 153 UnloadedExtensionInfo* unloaded = |
156 content::Details<UnloadedExtensionInfo>(details).ptr(); | 154 content::Details<UnloadedExtensionInfo>(details).ptr(); |
157 ProcessPendingTasks(NULL, profile, unloaded->extension); | 155 ProcessPendingTasks(NULL, profile, unloaded->extension); |
158 if (profile->HasOffTheRecordProfile()) { | 156 if (profile->HasOffTheRecordProfile()) { |
159 ProcessPendingTasks(NULL, profile->GetOffTheRecordProfile(), | 157 ProcessPendingTasks(NULL, profile->GetOffTheRecordProfile(), |
160 unloaded->extension); | 158 unloaded->extension); |
161 } | 159 } |
162 break; | 160 break; |
163 } | 161 } |
164 default: | 162 default: |
165 NOTREACHED(); | 163 NOTREACHED(); |
166 break; | 164 break; |
167 } | 165 } |
168 } | 166 } |
169 | 167 |
170 } // namespace extensions | 168 } // namespace extensions |
OLD | NEW |