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 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
119 NotifyWorkerDestroyed(process_id_, worker_devtools_agent_route_id_); | 119 NotifyWorkerDestroyed(process_id_, worker_devtools_agent_route_id_); |
120 if (context_ && process_id_ != -1) | 120 if (context_ && process_id_ != -1) |
121 context_->process_manager()->ReleaseWorkerProcess(embedded_worker_id_); | 121 context_->process_manager()->ReleaseWorkerProcess(embedded_worker_id_); |
122 registry_->RemoveWorker(process_id_, embedded_worker_id_); | 122 registry_->RemoveWorker(process_id_, embedded_worker_id_); |
123 } | 123 } |
124 | 124 |
125 void EmbeddedWorkerInstance::Start(int64 service_worker_version_id, | 125 void EmbeddedWorkerInstance::Start(int64 service_worker_version_id, |
126 const GURL& scope, | 126 const GURL& scope, |
127 const GURL& script_url, | 127 const GURL& script_url, |
128 bool pause_after_download, | 128 bool pause_after_download, |
129 const std::vector<int>& possible_process_ids, | |
130 const StatusCallback& callback) { | 129 const StatusCallback& callback) { |
131 if (!context_) { | 130 if (!context_) { |
132 callback.Run(SERVICE_WORKER_ERROR_ABORT); | 131 callback.Run(SERVICE_WORKER_ERROR_ABORT); |
133 return; | 132 return; |
134 } | 133 } |
135 DCHECK(status_ == STOPPED); | 134 DCHECK(status_ == STOPPED); |
136 status_ = STARTING; | 135 status_ = STARTING; |
137 scoped_ptr<EmbeddedWorkerMsg_StartWorker_Params> params( | 136 scoped_ptr<EmbeddedWorkerMsg_StartWorker_Params> params( |
138 new EmbeddedWorkerMsg_StartWorker_Params()); | 137 new EmbeddedWorkerMsg_StartWorker_Params()); |
139 params->embedded_worker_id = embedded_worker_id_; | 138 params->embedded_worker_id = embedded_worker_id_; |
140 params->service_worker_version_id = service_worker_version_id; | 139 params->service_worker_version_id = service_worker_version_id; |
141 params->scope = scope; | 140 params->scope = scope; |
142 params->script_url = script_url; | 141 params->script_url = script_url; |
143 params->worker_devtools_agent_route_id = MSG_ROUTING_NONE; | 142 params->worker_devtools_agent_route_id = MSG_ROUTING_NONE; |
144 params->pause_after_download = pause_after_download; | 143 params->pause_after_download = pause_after_download; |
145 params->wait_for_debugger = false; | 144 params->wait_for_debugger = false; |
146 context_->process_manager()->AllocateWorkerProcess( | 145 context_->process_manager()->AllocateWorkerProcess( |
147 embedded_worker_id_, | 146 embedded_worker_id_, |
148 SortProcesses(possible_process_ids), | 147 scope, |
149 script_url, | 148 script_url, |
150 base::Bind(&EmbeddedWorkerInstance::RunProcessAllocated, | 149 base::Bind(&EmbeddedWorkerInstance::RunProcessAllocated, |
151 weak_factory_.GetWeakPtr(), | 150 weak_factory_.GetWeakPtr(), |
152 context_, | 151 context_, |
153 base::Passed(¶ms), | 152 base::Passed(¶ms), |
154 callback)); | 153 callback)); |
155 } | 154 } |
156 | 155 |
157 ServiceWorkerStatusCode EmbeddedWorkerInstance::Stop() { | 156 ServiceWorkerStatusCode EmbeddedWorkerInstance::Stop() { |
158 DCHECK(status_ == STARTING || status_ == RUNNING); | 157 DCHECK(status_ == STARTING || status_ == RUNNING); |
(...skipping 14 matching lines...) Expand all Loading... |
173 ServiceWorkerStatusCode EmbeddedWorkerInstance::SendMessage( | 172 ServiceWorkerStatusCode EmbeddedWorkerInstance::SendMessage( |
174 const IPC::Message& message) { | 173 const IPC::Message& message) { |
175 DCHECK_NE(kInvalidEmbeddedWorkerThreadId, thread_id_); | 174 DCHECK_NE(kInvalidEmbeddedWorkerThreadId, thread_id_); |
176 if (status_ != RUNNING && status_ != STARTING) | 175 if (status_ != RUNNING && status_ != STARTING) |
177 return SERVICE_WORKER_ERROR_IPC_FAILED; | 176 return SERVICE_WORKER_ERROR_IPC_FAILED; |
178 return registry_->Send(process_id_, | 177 return registry_->Send(process_id_, |
179 new EmbeddedWorkerContextMsg_MessageToWorker( | 178 new EmbeddedWorkerContextMsg_MessageToWorker( |
180 thread_id_, embedded_worker_id_, message)); | 179 thread_id_, embedded_worker_id_, message)); |
181 } | 180 } |
182 | 181 |
183 void EmbeddedWorkerInstance::AddProcessReference(int process_id) { | |
184 ProcessRefMap::iterator found = process_refs_.find(process_id); | |
185 if (found == process_refs_.end()) | |
186 found = process_refs_.insert(std::make_pair(process_id, 0)).first; | |
187 ++found->second; | |
188 } | |
189 | |
190 void EmbeddedWorkerInstance::ReleaseProcessReference(int process_id) { | |
191 ProcessRefMap::iterator found = process_refs_.find(process_id); | |
192 if (found == process_refs_.end()) { | |
193 NOTREACHED() << "Releasing unknown process ref " << process_id; | |
194 return; | |
195 } | |
196 if (--found->second == 0) | |
197 process_refs_.erase(found); | |
198 } | |
199 | |
200 EmbeddedWorkerInstance::EmbeddedWorkerInstance( | 182 EmbeddedWorkerInstance::EmbeddedWorkerInstance( |
201 base::WeakPtr<ServiceWorkerContextCore> context, | 183 base::WeakPtr<ServiceWorkerContextCore> context, |
202 int embedded_worker_id) | 184 int embedded_worker_id) |
203 : context_(context), | 185 : context_(context), |
204 registry_(context->embedded_worker_registry()), | 186 registry_(context->embedded_worker_registry()), |
205 embedded_worker_id_(embedded_worker_id), | 187 embedded_worker_id_(embedded_worker_id), |
206 status_(STOPPED), | 188 status_(STOPPED), |
207 process_id_(-1), | 189 process_id_(-1), |
208 thread_id_(kInvalidEmbeddedWorkerThreadId), | 190 thread_id_(kInvalidEmbeddedWorkerThreadId), |
209 worker_devtools_agent_route_id_(MSG_ROUTING_NONE), | 191 worker_devtools_agent_route_id_(MSG_ROUTING_NONE), |
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
349 } | 331 } |
350 | 332 |
351 void EmbeddedWorkerInstance::AddListener(Listener* listener) { | 333 void EmbeddedWorkerInstance::AddListener(Listener* listener) { |
352 listener_list_.AddObserver(listener); | 334 listener_list_.AddObserver(listener); |
353 } | 335 } |
354 | 336 |
355 void EmbeddedWorkerInstance::RemoveListener(Listener* listener) { | 337 void EmbeddedWorkerInstance::RemoveListener(Listener* listener) { |
356 listener_list_.RemoveObserver(listener); | 338 listener_list_.RemoveObserver(listener); |
357 } | 339 } |
358 | 340 |
359 std::vector<int> EmbeddedWorkerInstance::SortProcesses( | |
360 const std::vector<int>& possible_process_ids) const { | |
361 // Add the |possible_process_ids| to the existing process_refs_ since each one | |
362 // is likely to take a reference once the SW starts up. | |
363 ProcessRefMap refs_with_new_ids = process_refs_; | |
364 for (std::vector<int>::const_iterator it = possible_process_ids.begin(); | |
365 it != possible_process_ids.end(); | |
366 ++it) { | |
367 refs_with_new_ids[*it]++; | |
368 } | |
369 | |
370 std::vector<std::pair<int, int> > counted(refs_with_new_ids.begin(), | |
371 refs_with_new_ids.end()); | |
372 // Sort descending by the reference count. | |
373 std::sort(counted.begin(), counted.end(), SecondGreater()); | |
374 | |
375 std::vector<int> result(counted.size()); | |
376 for (size_t i = 0; i < counted.size(); ++i) | |
377 result[i] = counted[i].first; | |
378 return result; | |
379 } | |
380 | |
381 } // namespace content | 341 } // namespace content |
OLD | NEW |