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(int process_id) { |
| 16 RenderProcessHost* rph = RenderProcessHost::FromID(process_id); |
| 17 if (!rph || rph->FastShutdownStarted()) |
| 18 return false; |
| 19 |
| 20 static_cast<RenderProcessHostImpl*>(rph)->IncrementWorkerRefCount(); |
| 21 return true; |
| 22 } |
| 23 |
| 24 ServiceWorkerProcessManager::ProcessInfo::ProcessInfo( |
| 25 const scoped_refptr<SiteInstance>& site_instance) |
| 26 : site_instance(site_instance), |
| 27 process_id(site_instance->GetProcess()->GetID()) { |
| 28 } |
| 29 |
| 30 ServiceWorkerProcessManager::ProcessInfo::ProcessInfo(int process_id) |
| 31 : process_id(process_id) { |
| 32 } |
| 33 |
| 34 ServiceWorkerProcessManager::ProcessInfo::~ProcessInfo() { |
| 35 } |
| 36 |
15 ServiceWorkerProcessManager::ServiceWorkerProcessManager( | 37 ServiceWorkerProcessManager::ServiceWorkerProcessManager( |
16 ServiceWorkerContextWrapper* context_wrapper) | 38 ServiceWorkerContextWrapper* context_wrapper) |
17 : context_wrapper_(context_wrapper), | 39 : context_wrapper_(context_wrapper), |
| 40 process_id_for_test_(-1), |
18 weak_this_factory_(this), | 41 weak_this_factory_(this), |
19 weak_this_(weak_this_factory_.GetWeakPtr()) { | 42 weak_this_(weak_this_factory_.GetWeakPtr()) { |
20 } | 43 } |
21 | 44 |
22 ServiceWorkerProcessManager::~ServiceWorkerProcessManager() { | 45 ServiceWorkerProcessManager::~ServiceWorkerProcessManager() { |
23 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 46 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
24 } | 47 } |
25 | 48 |
26 void ServiceWorkerProcessManager::AllocateWorkerProcess( | 49 void ServiceWorkerProcessManager::AllocateWorkerProcess( |
| 50 int embedded_worker_id, |
27 const std::vector<int>& process_ids, | 51 const std::vector<int>& process_ids, |
28 const GURL& script_url, | 52 const GURL& script_url, |
29 const base::Callback<void(ServiceWorkerStatusCode, int process_id)>& | 53 const base::Callback<void(ServiceWorkerStatusCode, int process_id)>& |
30 callback) const { | 54 callback) { |
31 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { | 55 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
32 BrowserThread::PostTask( | 56 BrowserThread::PostTask( |
33 BrowserThread::UI, | 57 BrowserThread::UI, |
34 FROM_HERE, | 58 FROM_HERE, |
35 base::Bind(&ServiceWorkerProcessManager::AllocateWorkerProcess, | 59 base::Bind(&ServiceWorkerProcessManager::AllocateWorkerProcess, |
36 weak_this_, | 60 weak_this_, |
| 61 embedded_worker_id, |
37 process_ids, | 62 process_ids, |
38 script_url, | 63 script_url, |
39 callback)); | 64 callback)); |
40 return; | 65 return; |
41 } | 66 } |
42 | 67 |
| 68 if (process_id_for_test_ != -1) { |
| 69 // Let tests specify the returned process ID. Note: We may need to be able |
| 70 // to specify the error code too. |
| 71 BrowserThread::PostTask( |
| 72 BrowserThread::IO, |
| 73 FROM_HERE, |
| 74 base::Bind(callback, SERVICE_WORKER_OK, process_id_for_test_)); |
| 75 return; |
| 76 } |
| 77 |
| 78 DCHECK(!ContainsKey(instance_info_, embedded_worker_id)) |
| 79 << embedded_worker_id << " already has a process allocated"; |
| 80 |
43 for (std::vector<int>::const_iterator it = process_ids.begin(); | 81 for (std::vector<int>::const_iterator it = process_ids.begin(); |
44 it != process_ids.end(); | 82 it != process_ids.end(); |
45 ++it) { | 83 ++it) { |
46 if (IncrementWorkerRefcountByPid(*it)) { | 84 if (IncrementWorkerRefCountByPid(*it)) { |
| 85 instance_info_.insert( |
| 86 std::make_pair(embedded_worker_id, ProcessInfo(*it))); |
47 BrowserThread::PostTask(BrowserThread::IO, | 87 BrowserThread::PostTask(BrowserThread::IO, |
48 FROM_HERE, | 88 FROM_HERE, |
49 base::Bind(callback, SERVICE_WORKER_OK, *it)); | 89 base::Bind(callback, SERVICE_WORKER_OK, *it)); |
50 return; | 90 return; |
51 } | 91 } |
52 } | 92 } |
53 | 93 |
54 if (!context_wrapper_->browser_context_) { | 94 if (!context_wrapper_->browser_context_) { |
55 // Shutdown has started. | 95 // Shutdown has started. |
56 BrowserThread::PostTask( | 96 BrowserThread::PostTask( |
(...skipping 11 matching lines...) Expand all Loading... |
68 // EmbeddedWorkerRegistry::process_sender_map_. | 108 // EmbeddedWorkerRegistry::process_sender_map_. |
69 if (!rph->Init()) { | 109 if (!rph->Init()) { |
70 LOG(ERROR) << "Couldn't start a new process!"; | 110 LOG(ERROR) << "Couldn't start a new process!"; |
71 BrowserThread::PostTask( | 111 BrowserThread::PostTask( |
72 BrowserThread::IO, | 112 BrowserThread::IO, |
73 FROM_HERE, | 113 FROM_HERE, |
74 base::Bind(callback, SERVICE_WORKER_ERROR_START_WORKER_FAILED, -1)); | 114 base::Bind(callback, SERVICE_WORKER_ERROR_START_WORKER_FAILED, -1)); |
75 return; | 115 return; |
76 } | 116 } |
77 | 117 |
| 118 instance_info_.insert( |
| 119 std::make_pair(embedded_worker_id, ProcessInfo(site_instance))); |
| 120 |
78 static_cast<RenderProcessHostImpl*>(rph)->IncrementWorkerRefCount(); | 121 static_cast<RenderProcessHostImpl*>(rph)->IncrementWorkerRefCount(); |
79 BrowserThread::PostTask( | 122 BrowserThread::PostTask( |
80 BrowserThread::IO, | 123 BrowserThread::IO, |
81 FROM_HERE, | 124 FROM_HERE, |
82 base::Bind(callback, SERVICE_WORKER_OK, rph->GetID())); | 125 base::Bind(callback, SERVICE_WORKER_OK, rph->GetID())); |
83 } | 126 } |
84 | 127 |
85 void ServiceWorkerProcessManager::ReleaseWorkerProcess(int process_id) { | 128 void ServiceWorkerProcessManager::ReleaseWorkerProcess(int embedded_worker_id) { |
86 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { | 129 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
87 BrowserThread::PostTask( | 130 BrowserThread::PostTask( |
88 BrowserThread::UI, | 131 BrowserThread::UI, |
89 FROM_HERE, | 132 FROM_HERE, |
90 base::Bind(&ServiceWorkerProcessManager::ReleaseWorkerProcess, | 133 base::Bind(&ServiceWorkerProcessManager::ReleaseWorkerProcess, |
91 weak_this_, | 134 weak_this_, |
92 process_id)); | 135 embedded_worker_id)); |
93 return; | 136 return; |
94 } | 137 } |
95 if (!DecrementWorkerRefcountByPid(process_id)) { | 138 if (process_id_for_test_ != -1) { |
96 DCHECK(false) << "DecrementWorkerRef(" << process_id | 139 // Unittests don't increment or decrement the worker refcount of a |
97 << ") doesn't match a previous IncrementWorkerRef"; | 140 // RenderProcessHost. |
| 141 return; |
98 } | 142 } |
99 } | 143 std::map<int, ProcessInfo>::iterator info = |
100 | 144 instance_info_.find(embedded_worker_id); |
101 void ServiceWorkerProcessManager::SetProcessRefcountOpsForTest( | 145 DCHECK(info != instance_info_.end()); |
102 const base::Callback<bool(int)>& increment_for_test, | 146 RenderProcessHost* rph = NULL; |
103 const base::Callback<bool(int)>& decrement_for_test) { | 147 if (info->second.site_instance) { |
104 increment_for_test_ = increment_for_test; | 148 rph = info->second.site_instance->GetProcess(); |
105 decrement_for_test_ = decrement_for_test; | 149 DCHECK_EQ(info->second.process_id, rph->GetID()) |
106 } | 150 << "A SiteInstance's process shouldn't get destroyed while we're " |
107 | 151 "holding a reference to it. Was the reference actually held?"; |
108 bool ServiceWorkerProcessManager::IncrementWorkerRefcountByPid( | 152 } else { |
109 int process_id) const { | 153 rph = RenderProcessHost::FromID(info->second.process_id); |
110 if (!increment_for_test_.is_null()) | 154 DCHECK(rph) |
111 return increment_for_test_.Run(process_id); | 155 << "Process " << info->second.process_id |
112 | 156 << " was destroyed unexpectedly. Did we actually hold a reference?"; |
113 RenderProcessHost* rph = RenderProcessHost::FromID(process_id); | |
114 if (rph && !rph->FastShutdownStarted()) { | |
115 static_cast<RenderProcessHostImpl*>(rph)->IncrementWorkerRefCount(); | |
116 return true; | |
117 } | 157 } |
118 | 158 static_cast<RenderProcessHostImpl*>(rph)->DecrementWorkerRefCount(); |
119 return false; | 159 instance_info_.erase(info); |
120 } | |
121 | |
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 |