| 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/extension_tab_util.h" | 12 #include "chrome/browser/extensions/extension_tab_util.h" |
| 13 #include "chrome/browser/extensions/process_map.h" | 13 #include "chrome/browser/extensions/process_map.h" |
| 14 #include "chrome/browser/profiles/profile.h" | 14 #include "chrome/browser/profiles/profile.h" |
| 15 #include "chrome/browser/tab_contents/tab_util.h" | 15 #include "chrome/browser/tab_contents/tab_util.h" |
| 16 #include "chrome/common/chrome_notification_types.h" | 16 #include "chrome/common/chrome_notification_types.h" |
| 17 #include "chrome/common/extensions/background_info.h" |
| 17 #include "chrome/common/extensions/extension.h" | 18 #include "chrome/common/extensions/extension.h" |
| 18 #include "chrome/common/extensions/extension_messages.h" | 19 #include "chrome/common/extensions/extension_messages.h" |
| 19 #include "chrome/common/view_type.h" | 20 #include "chrome/common/view_type.h" |
| 20 #include "content/public/browser/notification_service.h" | 21 #include "content/public/browser/notification_service.h" |
| 21 #include "content/public/browser/render_process_host.h" | 22 #include "content/public/browser/render_process_host.h" |
| 22 #include "content/public/browser/render_view_host.h" | 23 #include "content/public/browser/render_view_host.h" |
| 23 #include "content/public/browser/site_instance.h" | 24 #include "content/public/browser/site_instance.h" |
| 24 #include "content/public/browser/web_contents.h" | 25 #include "content/public/browser/web_contents.h" |
| 25 | 26 |
| 26 namespace extensions { | 27 namespace extensions { |
| 27 | 28 |
| 28 LazyBackgroundTaskQueue::LazyBackgroundTaskQueue(Profile* profile) | 29 LazyBackgroundTaskQueue::LazyBackgroundTaskQueue(Profile* profile) |
| 29 : profile_(profile) { | 30 : profile_(profile) { |
| 30 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_HOST_DID_STOP_LOADING, | 31 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_HOST_DID_STOP_LOADING, |
| 31 content::NotificationService::AllBrowserContextsAndSources()); | 32 content::NotificationService::AllBrowserContextsAndSources()); |
| 32 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED, | 33 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED, |
| 33 content::NotificationService::AllBrowserContextsAndSources()); | 34 content::NotificationService::AllBrowserContextsAndSources()); |
| 34 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED, | 35 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED, |
| 35 content::Source<Profile>(profile)); | 36 content::Source<Profile>(profile)); |
| 36 } | 37 } |
| 37 | 38 |
| 38 LazyBackgroundTaskQueue::~LazyBackgroundTaskQueue() { | 39 LazyBackgroundTaskQueue::~LazyBackgroundTaskQueue() { |
| 39 } | 40 } |
| 40 | 41 |
| 41 bool LazyBackgroundTaskQueue::ShouldEnqueueTask( | 42 bool LazyBackgroundTaskQueue::ShouldEnqueueTask( |
| 42 Profile* profile, const Extension* extension) { | 43 Profile* profile, const Extension* extension) { |
| 43 DCHECK(extension); | 44 DCHECK(extension); |
| 44 if (extension->has_background_page()) { | 45 if (BackgroundInfo::HasBackgroundPage(extension)) { |
| 45 ExtensionProcessManager* pm = extensions::ExtensionSystem::Get(profile)-> | 46 ExtensionProcessManager* pm = extensions::ExtensionSystem::Get(profile)-> |
| 46 process_manager(); | 47 process_manager(); |
| 47 ExtensionHost* background_host = | 48 ExtensionHost* background_host = |
| 48 pm->GetBackgroundHostForExtension(extension->id()); | 49 pm->GetBackgroundHostForExtension(extension->id()); |
| 49 if (!background_host || !background_host->did_stop_loading()) | 50 if (!background_host || !background_host->did_stop_loading()) |
| 50 return true; | 51 return true; |
| 51 if (pm->IsBackgroundHostClosing(extension->id())) | 52 if (pm->IsBackgroundHostClosing(extension->id())) |
| 52 pm->CancelSuspend(extension); | 53 pm->CancelSuspend(extension); |
| 53 } | 54 } |
| 54 | 55 |
| 55 return false; | 56 return false; |
| 56 } | 57 } |
| 57 | 58 |
| 58 void LazyBackgroundTaskQueue::AddPendingTask( | 59 void LazyBackgroundTaskQueue::AddPendingTask( |
| 59 Profile* profile, | 60 Profile* profile, |
| 60 const std::string& extension_id, | 61 const std::string& extension_id, |
| 61 const PendingTask& task) { | 62 const PendingTask& task) { |
| 62 PendingTasksList* tasks_list = NULL; | 63 PendingTasksList* tasks_list = NULL; |
| 63 PendingTasksKey key(profile, extension_id); | 64 PendingTasksKey key(profile, extension_id); |
| 64 PendingTasksMap::iterator it = pending_tasks_.find(key); | 65 PendingTasksMap::iterator it = pending_tasks_.find(key); |
| 65 if (it == pending_tasks_.end()) { | 66 if (it == pending_tasks_.end()) { |
| 66 tasks_list = new PendingTasksList(); | 67 tasks_list = new PendingTasksList(); |
| 67 pending_tasks_[key] = linked_ptr<PendingTasksList>(tasks_list); | 68 pending_tasks_[key] = linked_ptr<PendingTasksList>(tasks_list); |
| 68 | 69 |
| 69 const Extension* extension = | 70 const Extension* extension = |
| 70 ExtensionSystem::Get(profile)->extension_service()-> | 71 ExtensionSystem::Get(profile)->extension_service()-> |
| 71 extensions()->GetByID(extension_id); | 72 extensions()->GetByID(extension_id); |
| 72 if (extension && extension->has_lazy_background_page()) { | 73 if (extension && BackgroundInfo::HasLazyBackgroundPage(extension)) { |
| 73 // If this is the first enqueued task, and we're not waiting for the | 74 // If this is the first enqueued task, and we're not waiting for the |
| 74 // background page to unload, ensure the background page is loaded. | 75 // background page to unload, ensure the background page is loaded. |
| 75 ExtensionProcessManager* pm = | 76 ExtensionProcessManager* pm = |
| 76 ExtensionSystem::Get(profile)->process_manager(); | 77 ExtensionSystem::Get(profile)->process_manager(); |
| 77 pm->IncrementLazyKeepaliveCount(extension); | 78 pm->IncrementLazyKeepaliveCount(extension); |
| 78 pm->CreateBackgroundHost(extension, extension->GetBackgroundURL()); | 79 pm->CreateBackgroundHost(extension, |
| 80 BackgroundInfo::GetBackgroundURL(extension)); |
| 79 } | 81 } |
| 80 } else { | 82 } else { |
| 81 tasks_list = it->second.get(); | 83 tasks_list = it->second.get(); |
| 82 } | 84 } |
| 83 | 85 |
| 84 tasks_list->push_back(task); | 86 tasks_list->push_back(task); |
| 85 } | 87 } |
| 86 | 88 |
| 87 void LazyBackgroundTaskQueue::ProcessPendingTasks( | 89 void LazyBackgroundTaskQueue::ProcessPendingTasks( |
| 88 ExtensionHost* host, | 90 ExtensionHost* host, |
| 89 Profile* profile, | 91 Profile* profile, |
| 90 const Extension* extension) { | 92 const Extension* extension) { |
| 91 if (!profile->IsSameProfile(profile_)) | 93 if (!profile->IsSameProfile(profile_)) |
| 92 return; | 94 return; |
| 93 | 95 |
| 94 PendingTasksKey key(profile, extension->id()); | 96 PendingTasksKey key(profile, extension->id()); |
| 95 PendingTasksMap::iterator map_it = pending_tasks_.find(key); | 97 PendingTasksMap::iterator map_it = pending_tasks_.find(key); |
| 96 if (map_it == pending_tasks_.end()) { | 98 if (map_it == pending_tasks_.end()) { |
| 97 if (extension->has_lazy_background_page()) | 99 if (BackgroundInfo::HasLazyBackgroundPage(extension)) |
| 98 CHECK(!host); // lazy page should not load without any pending tasks | 100 CHECK(!host); // lazy page should not load without any pending tasks |
| 99 return; | 101 return; |
| 100 } | 102 } |
| 101 | 103 |
| 102 // Swap the pending tasks to a temporary, to avoid problems if the task | 104 // Swap the pending tasks to a temporary, to avoid problems if the task |
| 103 // list is modified during processing. | 105 // list is modified during processing. |
| 104 PendingTasksList tasks; | 106 PendingTasksList tasks; |
| 105 tasks.swap(*map_it->second); | 107 tasks.swap(*map_it->second); |
| 106 for (PendingTasksList::const_iterator it = tasks.begin(); | 108 for (PendingTasksList::const_iterator it = tasks.begin(); |
| 107 it != tasks.end(); ++it) { | 109 it != tasks.end(); ++it) { |
| 108 it->Run(host); | 110 it->Run(host); |
| 109 } | 111 } |
| 110 | 112 |
| 111 pending_tasks_.erase(key); | 113 pending_tasks_.erase(key); |
| 112 | 114 |
| 113 // Balance the keepalive in AddPendingTask. Note we don't do this on a | 115 // Balance the keepalive in AddPendingTask. Note we don't do this on a |
| 114 // failure to load, because the keepalive count is reset in that case. | 116 // failure to load, because the keepalive count is reset in that case. |
| 115 if (host && extension->has_lazy_background_page()) { | 117 if (host && BackgroundInfo::HasLazyBackgroundPage(extension)) { |
| 116 ExtensionSystem::Get(profile)->process_manager()-> | 118 ExtensionSystem::Get(profile)->process_manager()-> |
| 117 DecrementLazyKeepaliveCount(extension); | 119 DecrementLazyKeepaliveCount(extension); |
| 118 } | 120 } |
| 119 } | 121 } |
| 120 | 122 |
| 121 void LazyBackgroundTaskQueue::Observe( | 123 void LazyBackgroundTaskQueue::Observe( |
| 122 int type, | 124 int type, |
| 123 const content::NotificationSource& source, | 125 const content::NotificationSource& source, |
| 124 const content::NotificationDetails& details) { | 126 const content::NotificationDetails& details) { |
| 125 switch (type) { | 127 switch (type) { |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 161 } | 163 } |
| 162 break; | 164 break; |
| 163 } | 165 } |
| 164 default: | 166 default: |
| 165 NOTREACHED(); | 167 NOTREACHED(); |
| 166 break; | 168 break; |
| 167 } | 169 } |
| 168 } | 170 } |
| 169 | 171 |
| 170 } // namespace extensions | 172 } // namespace extensions |
| OLD | NEW |