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 "content/browser/service_worker/embedded_worker_instance.h" | 5 #include "content/browser/service_worker/embedded_worker_instance.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
120 NotifyWorkerDestroyed(process_id_, worker_devtools_agent_route_id_); | 120 NotifyWorkerDestroyed(process_id_, worker_devtools_agent_route_id_); |
121 if (context_ && process_id_ != -1) | 121 if (context_ && process_id_ != -1) |
122 context_->process_manager()->ReleaseWorkerProcess(embedded_worker_id_); | 122 context_->process_manager()->ReleaseWorkerProcess(embedded_worker_id_); |
123 registry_->RemoveWorker(process_id_, embedded_worker_id_); | 123 registry_->RemoveWorker(process_id_, embedded_worker_id_); |
124 } | 124 } |
125 | 125 |
126 void EmbeddedWorkerInstance::Start(int64 service_worker_version_id, | 126 void EmbeddedWorkerInstance::Start(int64 service_worker_version_id, |
127 const GURL& scope, | 127 const GURL& scope, |
128 const GURL& script_url, | 128 const GURL& script_url, |
129 bool pause_after_download, | 129 bool pause_after_download, |
130 const std::vector<int>& possible_process_ids, | |
131 const StatusCallback& callback) { | 130 const StatusCallback& callback) { |
132 if (!context_) { | 131 if (!context_) { |
133 callback.Run(SERVICE_WORKER_ERROR_ABORT); | 132 callback.Run(SERVICE_WORKER_ERROR_ABORT); |
134 return; | 133 return; |
135 } | 134 } |
136 DCHECK(status_ == STOPPED); | 135 DCHECK(status_ == STOPPED); |
137 status_ = STARTING; | 136 status_ = STARTING; |
138 scoped_ptr<EmbeddedWorkerMsg_StartWorker_Params> params( | 137 scoped_ptr<EmbeddedWorkerMsg_StartWorker_Params> params( |
139 new EmbeddedWorkerMsg_StartWorker_Params()); | 138 new EmbeddedWorkerMsg_StartWorker_Params()); |
140 TRACE_EVENT_ASYNC_BEGIN2("ServiceWorker", | 139 TRACE_EVENT_ASYNC_BEGIN2("ServiceWorker", |
141 "EmbeddedWorkerInstance::ProcessAllocate", | 140 "EmbeddedWorkerInstance::ProcessAllocate", |
142 params.get(), | 141 params.get(), |
143 "Scope", scope.spec(), | 142 "Scope", scope.spec(), |
144 "Script URL", script_url.spec()); | 143 "Script URL", script_url.spec()); |
145 params->embedded_worker_id = embedded_worker_id_; | 144 params->embedded_worker_id = embedded_worker_id_; |
146 params->service_worker_version_id = service_worker_version_id; | 145 params->service_worker_version_id = service_worker_version_id; |
147 params->scope = scope; | 146 params->scope = scope; |
148 params->script_url = script_url; | 147 params->script_url = script_url; |
149 params->worker_devtools_agent_route_id = MSG_ROUTING_NONE; | 148 params->worker_devtools_agent_route_id = MSG_ROUTING_NONE; |
150 params->pause_after_download = pause_after_download; | 149 params->pause_after_download = pause_after_download; |
151 params->wait_for_debugger = false; | 150 params->wait_for_debugger = false; |
152 context_->process_manager()->AllocateWorkerProcess( | 151 context_->process_manager()->AllocateWorkerProcess( |
153 embedded_worker_id_, | 152 embedded_worker_id_, |
154 SortProcesses(possible_process_ids), | 153 scope, |
155 script_url, | 154 script_url, |
156 base::Bind(&EmbeddedWorkerInstance::RunProcessAllocated, | 155 base::Bind(&EmbeddedWorkerInstance::RunProcessAllocated, |
157 weak_factory_.GetWeakPtr(), | 156 weak_factory_.GetWeakPtr(), |
158 context_, | 157 context_, |
159 base::Passed(¶ms), | 158 base::Passed(¶ms), |
160 callback)); | 159 callback)); |
161 } | 160 } |
162 | 161 |
163 ServiceWorkerStatusCode EmbeddedWorkerInstance::Stop() { | 162 ServiceWorkerStatusCode EmbeddedWorkerInstance::Stop() { |
164 DCHECK(status_ == STARTING || status_ == RUNNING); | 163 DCHECK(status_ == STARTING || status_ == RUNNING); |
(...skipping 14 matching lines...) Expand all Loading... |
179 ServiceWorkerStatusCode EmbeddedWorkerInstance::SendMessage( | 178 ServiceWorkerStatusCode EmbeddedWorkerInstance::SendMessage( |
180 const IPC::Message& message) { | 179 const IPC::Message& message) { |
181 DCHECK_NE(kInvalidEmbeddedWorkerThreadId, thread_id_); | 180 DCHECK_NE(kInvalidEmbeddedWorkerThreadId, thread_id_); |
182 if (status_ != RUNNING && status_ != STARTING) | 181 if (status_ != RUNNING && status_ != STARTING) |
183 return SERVICE_WORKER_ERROR_IPC_FAILED; | 182 return SERVICE_WORKER_ERROR_IPC_FAILED; |
184 return registry_->Send(process_id_, | 183 return registry_->Send(process_id_, |
185 new EmbeddedWorkerContextMsg_MessageToWorker( | 184 new EmbeddedWorkerContextMsg_MessageToWorker( |
186 thread_id_, embedded_worker_id_, message)); | 185 thread_id_, embedded_worker_id_, message)); |
187 } | 186 } |
188 | 187 |
189 void EmbeddedWorkerInstance::AddProcessReference(int process_id) { | |
190 ProcessRefMap::iterator found = process_refs_.find(process_id); | |
191 if (found == process_refs_.end()) | |
192 found = process_refs_.insert(std::make_pair(process_id, 0)).first; | |
193 ++found->second; | |
194 } | |
195 | |
196 void EmbeddedWorkerInstance::ReleaseProcessReference(int process_id) { | |
197 ProcessRefMap::iterator found = process_refs_.find(process_id); | |
198 if (found == process_refs_.end()) { | |
199 NOTREACHED() << "Releasing unknown process ref " << process_id; | |
200 return; | |
201 } | |
202 if (--found->second == 0) | |
203 process_refs_.erase(found); | |
204 } | |
205 | |
206 EmbeddedWorkerInstance::EmbeddedWorkerInstance( | 188 EmbeddedWorkerInstance::EmbeddedWorkerInstance( |
207 base::WeakPtr<ServiceWorkerContextCore> context, | 189 base::WeakPtr<ServiceWorkerContextCore> context, |
208 int embedded_worker_id) | 190 int embedded_worker_id) |
209 : context_(context), | 191 : context_(context), |
210 registry_(context->embedded_worker_registry()), | 192 registry_(context->embedded_worker_registry()), |
211 embedded_worker_id_(embedded_worker_id), | 193 embedded_worker_id_(embedded_worker_id), |
212 status_(STOPPED), | 194 status_(STOPPED), |
213 process_id_(-1), | 195 process_id_(-1), |
214 thread_id_(kInvalidEmbeddedWorkerThreadId), | 196 thread_id_(kInvalidEmbeddedWorkerThreadId), |
215 worker_devtools_agent_route_id_(MSG_ROUTING_NONE), | 197 worker_devtools_agent_route_id_(MSG_ROUTING_NONE), |
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
359 } | 341 } |
360 | 342 |
361 void EmbeddedWorkerInstance::AddListener(Listener* listener) { | 343 void EmbeddedWorkerInstance::AddListener(Listener* listener) { |
362 listener_list_.AddObserver(listener); | 344 listener_list_.AddObserver(listener); |
363 } | 345 } |
364 | 346 |
365 void EmbeddedWorkerInstance::RemoveListener(Listener* listener) { | 347 void EmbeddedWorkerInstance::RemoveListener(Listener* listener) { |
366 listener_list_.RemoveObserver(listener); | 348 listener_list_.RemoveObserver(listener); |
367 } | 349 } |
368 | 350 |
369 std::vector<int> EmbeddedWorkerInstance::SortProcesses( | |
370 const std::vector<int>& possible_process_ids) const { | |
371 // Add the |possible_process_ids| to the existing process_refs_ since each one | |
372 // is likely to take a reference once the SW starts up. | |
373 ProcessRefMap refs_with_new_ids = process_refs_; | |
374 for (std::vector<int>::const_iterator it = possible_process_ids.begin(); | |
375 it != possible_process_ids.end(); | |
376 ++it) { | |
377 refs_with_new_ids[*it]++; | |
378 } | |
379 | |
380 std::vector<std::pair<int, int> > counted(refs_with_new_ids.begin(), | |
381 refs_with_new_ids.end()); | |
382 // Sort descending by the reference count. | |
383 std::sort(counted.begin(), counted.end(), SecondGreater()); | |
384 | |
385 std::vector<int> result(counted.size()); | |
386 for (size_t i = 0; i < counted.size(); ++i) | |
387 result[i] = counted[i].first; | |
388 return result; | |
389 } | |
390 | |
391 } // namespace content | 351 } // namespace content |
OLD | NEW |