Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "content/browser/service_worker/service_worker_process_manager.h" | 5 #include "content/browser/service_worker/service_worker_process_manager.h" |
| 6 | 6 |
| 7 #include "content/browser/renderer_host/render_process_host_impl.h" | 7 #include "content/browser/renderer_host/render_process_host_impl.h" |
| 8 #include "content/browser/service_worker/service_worker_context_wrapper.h" | 8 #include "content/browser/service_worker/service_worker_context_wrapper.h" |
| 9 #include "content/public/browser/browser_thread.h" | 9 #include "content/public/browser/browser_thread.h" |
| 10 #include "content/public/browser/site_instance.h" | 10 #include "content/public/browser/site_instance.h" |
| 11 #include "url/gurl.h" | 11 #include "url/gurl.h" |
| 12 | 12 |
| 13 namespace content { | 13 namespace content { |
| 14 | 14 |
| 15 static bool IncrementWorkerRefcountByPid( | |
|
dominicc (has gone to gerrit)
2014/05/20 02:01:18
The casing of Refcount is inconsistent with Increm
Jeffrey Yasskin
2014/05/20 05:20:10
IncrementWorkerRefCount is pre-existing, so I've f
| |
| 16 int process_id) { | |
| 17 RenderProcessHost* rph = RenderProcessHost::FromID(process_id); | |
| 18 if (rph && !rph->FastShutdownStarted()) { | |
|
dominicc (has gone to gerrit)
2014/05/20 02:01:18
Would it be more readable to make this an early re
Jeffrey Yasskin
2014/05/20 05:20:10
It looks pretty similar.
| |
| 19 static_cast<RenderProcessHostImpl*>(rph)->IncrementWorkerRefCount(); | |
| 20 return true; | |
| 21 } | |
| 22 | |
| 23 return false; | |
| 24 } | |
| 25 | |
| 15 ServiceWorkerProcessManager::ServiceWorkerProcessManager( | 26 ServiceWorkerProcessManager::ServiceWorkerProcessManager( |
| 16 ServiceWorkerContextWrapper* context_wrapper) | 27 ServiceWorkerContextWrapper* context_wrapper) |
| 17 : context_wrapper_(context_wrapper), | 28 : context_wrapper_(context_wrapper), |
| 29 process_id_for_test_(-1), | |
| 18 weak_this_factory_(this), | 30 weak_this_factory_(this), |
| 19 weak_this_(weak_this_factory_.GetWeakPtr()) { | 31 weak_this_(weak_this_factory_.GetWeakPtr()) { |
| 20 } | 32 } |
| 21 | 33 |
| 22 ServiceWorkerProcessManager::~ServiceWorkerProcessManager() { | 34 ServiceWorkerProcessManager::~ServiceWorkerProcessManager() { |
| 23 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 35 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 24 } | 36 } |
| 25 | 37 |
| 26 void ServiceWorkerProcessManager::AllocateWorkerProcess( | 38 void ServiceWorkerProcessManager::AllocateWorkerProcess( |
| 39 int embedded_worker_id, | |
| 27 const std::vector<int>& process_ids, | 40 const std::vector<int>& process_ids, |
| 28 const GURL& script_url, | 41 const GURL& script_url, |
| 29 const base::Callback<void(ServiceWorkerStatusCode, int process_id)>& | 42 const base::Callback<void(ServiceWorkerStatusCode, int process_id)>& |
| 30 callback) const { | 43 callback) { |
| 31 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { | 44 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
| 32 BrowserThread::PostTask( | 45 BrowserThread::PostTask( |
| 33 BrowserThread::UI, | 46 BrowserThread::UI, |
| 34 FROM_HERE, | 47 FROM_HERE, |
| 35 base::Bind(&ServiceWorkerProcessManager::AllocateWorkerProcess, | 48 base::Bind(&ServiceWorkerProcessManager::AllocateWorkerProcess, |
| 36 weak_this_, | 49 weak_this_, |
| 50 embedded_worker_id, | |
| 37 process_ids, | 51 process_ids, |
| 38 script_url, | 52 script_url, |
| 39 callback)); | 53 callback)); |
| 40 return; | 54 return; |
| 41 } | 55 } |
| 42 | 56 |
| 57 if (process_id_for_test_ != -1) { | |
| 58 // Let tests specify the returned process ID. Note: We may need to be able | |
|
dominicc (has gone to gerrit)
2014/05/20 02:01:18
One space after .?
Jeffrey Yasskin
2014/05/20 05:20:10
Done.
| |
| 59 // to specify the error code too. | |
| 60 BrowserThread::PostTask( | |
| 61 BrowserThread::IO, | |
| 62 FROM_HERE, | |
| 63 base::Bind(callback, SERVICE_WORKER_OK, process_id_for_test_)); | |
| 64 return; | |
| 65 } | |
| 66 | |
| 67 DCHECK(!ContainsKey(instance_info_, embedded_worker_id)) | |
| 68 << embedded_worker_id << " already has a process allocated"; | |
| 69 | |
| 43 for (std::vector<int>::const_iterator it = process_ids.begin(); | 70 for (std::vector<int>::const_iterator it = process_ids.begin(); |
| 44 it != process_ids.end(); | 71 it != process_ids.end(); |
| 45 ++it) { | 72 ++it) { |
| 46 if (IncrementWorkerRefcountByPid(*it)) { | 73 if (IncrementWorkerRefcountByPid(*it)) { |
| 74 instance_info_.insert( | |
| 75 std::make_pair(embedded_worker_id, ProcessInfo(*it))); | |
| 47 BrowserThread::PostTask(BrowserThread::IO, | 76 BrowserThread::PostTask(BrowserThread::IO, |
| 48 FROM_HERE, | 77 FROM_HERE, |
| 49 base::Bind(callback, SERVICE_WORKER_OK, *it)); | 78 base::Bind(callback, SERVICE_WORKER_OK, *it)); |
| 50 return; | 79 return; |
| 51 } | 80 } |
| 52 } | 81 } |
| 53 | 82 |
| 54 if (!context_wrapper_->browser_context_) { | 83 if (!context_wrapper_->browser_context_) { |
| 55 // Shutdown has started. | 84 // Shutdown has started. |
| 56 BrowserThread::PostTask( | 85 BrowserThread::PostTask( |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 68 // EmbeddedWorkerRegistry::process_sender_map_. | 97 // EmbeddedWorkerRegistry::process_sender_map_. |
| 69 if (!rph->Init()) { | 98 if (!rph->Init()) { |
| 70 LOG(ERROR) << "Couldn't start a new process!"; | 99 LOG(ERROR) << "Couldn't start a new process!"; |
| 71 BrowserThread::PostTask( | 100 BrowserThread::PostTask( |
| 72 BrowserThread::IO, | 101 BrowserThread::IO, |
| 73 FROM_HERE, | 102 FROM_HERE, |
| 74 base::Bind(callback, SERVICE_WORKER_ERROR_START_WORKER_FAILED, -1)); | 103 base::Bind(callback, SERVICE_WORKER_ERROR_START_WORKER_FAILED, -1)); |
| 75 return; | 104 return; |
| 76 } | 105 } |
| 77 | 106 |
| 107 instance_info_.insert( | |
| 108 std::make_pair(embedded_worker_id, ProcessInfo(site_instance))); | |
| 109 | |
| 78 static_cast<RenderProcessHostImpl*>(rph)->IncrementWorkerRefCount(); | 110 static_cast<RenderProcessHostImpl*>(rph)->IncrementWorkerRefCount(); |
| 79 BrowserThread::PostTask( | 111 BrowserThread::PostTask( |
| 80 BrowserThread::IO, | 112 BrowserThread::IO, |
| 81 FROM_HERE, | 113 FROM_HERE, |
| 82 base::Bind(callback, SERVICE_WORKER_OK, rph->GetID())); | 114 base::Bind(callback, SERVICE_WORKER_OK, rph->GetID())); |
| 83 } | 115 } |
| 84 | 116 |
| 85 void ServiceWorkerProcessManager::ReleaseWorkerProcess(int process_id) { | 117 void ServiceWorkerProcessManager::ReleaseWorkerProcess(int embedded_worker_id) { |
| 86 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { | 118 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
| 87 BrowserThread::PostTask( | 119 BrowserThread::PostTask( |
| 88 BrowserThread::UI, | 120 BrowserThread::UI, |
| 89 FROM_HERE, | 121 FROM_HERE, |
| 90 base::Bind(&ServiceWorkerProcessManager::ReleaseWorkerProcess, | 122 base::Bind(&ServiceWorkerProcessManager::ReleaseWorkerProcess, |
| 91 weak_this_, | 123 weak_this_, |
| 92 process_id)); | 124 embedded_worker_id)); |
| 93 return; | 125 return; |
| 94 } | 126 } |
| 95 if (!DecrementWorkerRefcountByPid(process_id)) { | 127 if (process_id_for_test_ != -1) { |
| 96 DCHECK(false) << "DecrementWorkerRef(" << process_id | 128 // Unittests don't increment or decrement the worker refcount of a |
| 97 << ") doesn't match a previous IncrementWorkerRef"; | 129 // RenderProcessHost. |
| 130 return; | |
| 98 } | 131 } |
| 132 std::map<int, ProcessInfo>::iterator info = | |
| 133 instance_info_.find(embedded_worker_id); | |
| 134 DCHECK(info != instance_info_.end()); | |
| 135 RenderProcessHost* rph = NULL; | |
| 136 if (info->second.site_instance) { | |
| 137 rph = info->second.site_instance->GetProcess(); | |
| 138 DCHECK_EQ(info->second.process_id, rph->GetID()) | |
| 139 << "A SiteInstance's process shouldn't get destroyed while we're " | |
| 140 "holding a reference to it. Was the reference actually held?"; | |
| 141 } else { | |
| 142 rph = RenderProcessHost::FromID(info->second.process_id); | |
| 143 DCHECK(rph) | |
| 144 << "Process " << info->second.process_id | |
| 145 << " was destroyed unexpectedly. Did we actually hold a reference?"; | |
| 146 } | |
| 147 static_cast<RenderProcessHostImpl*>(rph)->DecrementWorkerRefCount(); | |
| 148 instance_info_.erase(info); | |
| 99 } | 149 } |
| 100 | 150 |
| 101 void ServiceWorkerProcessManager::SetProcessRefcountOpsForTest( | 151 ServiceWorkerProcessManager::ProcessInfo::ProcessInfo( |
| 102 const base::Callback<bool(int)>& increment_for_test, | 152 const scoped_refptr<SiteInstance>& site_instance) |
| 103 const base::Callback<bool(int)>& decrement_for_test) { | 153 : site_instance(site_instance), |
| 104 increment_for_test_ = increment_for_test; | 154 process_id(site_instance->GetProcess()->GetID()) { |
| 105 decrement_for_test_ = decrement_for_test; | |
| 106 } | 155 } |
| 107 | 156 ServiceWorkerProcessManager::ProcessInfo::ProcessInfo(int process_id) |
| 108 bool ServiceWorkerProcessManager::IncrementWorkerRefcountByPid( | 157 : process_id(process_id) { |
| 109 int process_id) const { | |
| 110 if (!increment_for_test_.is_null()) | |
| 111 return increment_for_test_.Run(process_id); | |
| 112 | |
| 113 RenderProcessHost* rph = RenderProcessHost::FromID(process_id); | |
| 114 if (rph && !rph->FastShutdownStarted()) { | |
| 115 static_cast<RenderProcessHostImpl*>(rph)->IncrementWorkerRefCount(); | |
| 116 return true; | |
| 117 } | |
| 118 | |
| 119 return false; | |
| 120 } | 158 } |
| 121 | 159 ServiceWorkerProcessManager::ProcessInfo::~ProcessInfo() { |
| 122 bool ServiceWorkerProcessManager::DecrementWorkerRefcountByPid( | |
| 123 int process_id) const { | |
| 124 if (!decrement_for_test_.is_null()) | |
| 125 return decrement_for_test_.Run(process_id); | |
| 126 | |
| 127 RenderProcessHost* rph = RenderProcessHost::FromID(process_id); | |
| 128 if (rph) | |
| 129 static_cast<RenderProcessHostImpl*>(rph)->DecrementWorkerRefCount(); | |
| 130 | |
| 131 return rph != NULL; | |
| 132 } | 160 } |
| 133 | 161 |
| 134 } // namespace content | 162 } // namespace content |
| 135 | 163 |
| 136 namespace base { | 164 namespace base { |
| 137 // Destroying ServiceWorkerProcessManagers only on the UI thread allows the | 165 // Destroying ServiceWorkerProcessManagers only on the UI thread allows the |
| 138 // member WeakPtr to safely guard the object's lifetime when used on that | 166 // member WeakPtr to safely guard the object's lifetime when used on that |
| 139 // thread. | 167 // thread. |
| 140 void DefaultDeleter<content::ServiceWorkerProcessManager>::operator()( | 168 void DefaultDeleter<content::ServiceWorkerProcessManager>::operator()( |
| 141 content::ServiceWorkerProcessManager* ptr) const { | 169 content::ServiceWorkerProcessManager* ptr) const { |
| 142 content::BrowserThread::DeleteSoon( | 170 content::BrowserThread::DeleteSoon( |
| 143 content::BrowserThread::UI, FROM_HERE, ptr); | 171 content::BrowserThread::UI, FROM_HERE, ptr); |
| 144 } | 172 } |
| 145 } // namespace base | 173 } // namespace base |
| OLD | NEW |