| 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 "base/bind_helpers.h" |
| 8 #include "content/browser/devtools/embedded_worker_devtools_manager.h" |
| 7 #include "content/browser/service_worker/embedded_worker_registry.h" | 9 #include "content/browser/service_worker/embedded_worker_registry.h" |
| 10 #include "content/browser/service_worker/service_worker_context_core.h" |
| 8 #include "content/common/service_worker/embedded_worker_messages.h" | 11 #include "content/common/service_worker/embedded_worker_messages.h" |
| 12 #include "content/public/browser/browser_thread.h" |
| 13 #include "content/public/browser/render_process_host.h" |
| 9 #include "ipc/ipc_message.h" | 14 #include "ipc/ipc_message.h" |
| 10 #include "url/gurl.h" | 15 #include "url/gurl.h" |
| 11 | 16 |
| 12 namespace content { | 17 namespace content { |
| 13 | 18 |
| 14 namespace { | 19 namespace { |
| 20 |
| 15 // Functor to sort by the .second element of a struct. | 21 // Functor to sort by the .second element of a struct. |
| 16 struct SecondGreater { | 22 struct SecondGreater { |
| 17 template <typename Value> | 23 template <typename Value> |
| 18 bool operator()(const Value& lhs, const Value& rhs) { | 24 bool operator()(const Value& lhs, const Value& rhs) { |
| 19 return lhs.second > rhs.second; | 25 return lhs.second > rhs.second; |
| 20 } | 26 } |
| 21 }; | 27 }; |
| 28 |
| 29 void NotifyWorkerContextStarted(int worker_process_id, int worker_route_id) { |
| 30 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
| 31 BrowserThread::PostTask( |
| 32 BrowserThread::UI, |
| 33 FROM_HERE, |
| 34 base::Bind( |
| 35 NotifyWorkerContextStarted, worker_process_id, worker_route_id)); |
| 36 return; |
| 37 } |
| 38 EmbeddedWorkerDevToolsManager::GetInstance()->WorkerContextStarted( |
| 39 worker_process_id, worker_route_id); |
| 40 } |
| 41 |
| 42 void NotifyWorkerDestroyed(int worker_process_id, int worker_route_id) { |
| 43 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
| 44 BrowserThread::PostTask( |
| 45 BrowserThread::UI, |
| 46 FROM_HERE, |
| 47 base::Bind(NotifyWorkerDestroyed, worker_process_id, worker_route_id)); |
| 48 return; |
| 49 } |
| 50 EmbeddedWorkerDevToolsManager::GetInstance()->WorkerDestroyed( |
| 51 worker_process_id, worker_route_id); |
| 52 } |
| 53 |
| 54 void RegisterToWorkerDevToolsManager( |
| 55 int process_id, |
| 56 const ServiceWorkerContextCore* const service_worker_context, |
| 57 int64 service_worker_version_id, |
| 58 const base::Callback<void(int worker_devtools_agent_route_id, |
| 59 bool pause_on_start)>& callback) { |
| 60 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
| 61 BrowserThread::PostTask(BrowserThread::UI, |
| 62 FROM_HERE, |
| 63 base::Bind(RegisterToWorkerDevToolsManager, |
| 64 process_id, |
| 65 service_worker_context, |
| 66 service_worker_version_id, |
| 67 callback)); |
| 68 return; |
| 69 } |
| 70 int worker_devtools_agent_route_id = MSG_ROUTING_NONE; |
| 71 bool pause_on_start = false; |
| 72 if (RenderProcessHost* rph = RenderProcessHost::FromID(process_id)) { |
| 73 // |rph| may be NULL in unit tests. |
| 74 worker_devtools_agent_route_id = rph->GetNextRoutingID(); |
| 75 pause_on_start = |
| 76 EmbeddedWorkerDevToolsManager::GetInstance()->ServiceWorkerCreated( |
| 77 process_id, |
| 78 worker_devtools_agent_route_id, |
| 79 service_worker_context, |
| 80 service_worker_version_id); |
| 81 } |
| 82 BrowserThread::PostTask( |
| 83 BrowserThread::IO, |
| 84 FROM_HERE, |
| 85 base::Bind(callback, worker_devtools_agent_route_id, pause_on_start)); |
| 86 } |
| 87 |
| 22 } // namespace | 88 } // namespace |
| 23 | 89 |
| 24 EmbeddedWorkerInstance::~EmbeddedWorkerInstance() { | 90 EmbeddedWorkerInstance::~EmbeddedWorkerInstance() { |
| 91 if (worker_devtools_agent_route_id_ != MSG_ROUTING_NONE) |
| 92 NotifyWorkerDestroyed(process_id_, worker_devtools_agent_route_id_); |
| 93 if (context_ && process_id_ != -1) |
| 94 context_->process_manager()->ReleaseWorkerProcess(process_id_); |
| 25 registry_->RemoveWorker(process_id_, embedded_worker_id_); | 95 registry_->RemoveWorker(process_id_, embedded_worker_id_); |
| 26 } | 96 } |
| 27 | 97 |
| 28 void EmbeddedWorkerInstance::Start(int64 service_worker_version_id, | 98 void EmbeddedWorkerInstance::Start(int64 service_worker_version_id, |
| 29 const GURL& scope, | 99 const GURL& scope, |
| 30 const GURL& script_url, | 100 const GURL& script_url, |
| 31 const std::vector<int>& possible_process_ids, | 101 const std::vector<int>& possible_process_ids, |
| 32 const StatusCallback& callback) { | 102 const StatusCallback& callback) { |
| 103 if (!context_) { |
| 104 callback.Run(SERVICE_WORKER_ERROR_ABORT); |
| 105 return; |
| 106 } |
| 33 DCHECK(status_ == STOPPED); | 107 DCHECK(status_ == STOPPED); |
| 34 status_ = STARTING; | 108 status_ = STARTING; |
| 35 std::vector<int> ordered_process_ids = SortProcesses(possible_process_ids); | 109 scoped_ptr<EmbeddedWorkerMsg_StartWorker_Params> params( |
| 36 registry_->StartWorker(ordered_process_ids, | 110 new EmbeddedWorkerMsg_StartWorker_Params()); |
| 37 embedded_worker_id_, | 111 params->embedded_worker_id = embedded_worker_id_; |
| 38 service_worker_version_id, | 112 params->service_worker_version_id = service_worker_version_id; |
| 39 scope, | 113 params->scope = scope; |
| 40 script_url, | 114 params->script_url = script_url; |
| 41 callback); | 115 params->worker_devtools_agent_route_id = MSG_ROUTING_NONE; |
| 116 params->pause_on_start = false; |
| 117 context_->process_manager()->AllocateWorkerProcess( |
| 118 SortProcesses(possible_process_ids), |
| 119 script_url, |
| 120 base::Bind(&EmbeddedWorkerInstance::RunProcessAllocated, |
| 121 weak_factory_.GetWeakPtr(), |
| 122 context_, |
| 123 base::Passed(¶ms), |
| 124 callback)); |
| 42 } | 125 } |
| 43 | 126 |
| 44 ServiceWorkerStatusCode EmbeddedWorkerInstance::Stop() { | 127 ServiceWorkerStatusCode EmbeddedWorkerInstance::Stop() { |
| 45 DCHECK(status_ == STARTING || status_ == RUNNING); | 128 DCHECK(status_ == STARTING || status_ == RUNNING); |
| 46 ServiceWorkerStatusCode status = | 129 ServiceWorkerStatusCode status = |
| 47 registry_->StopWorker(process_id_, embedded_worker_id_); | 130 registry_->StopWorker(process_id_, embedded_worker_id_); |
| 48 if (status == SERVICE_WORKER_OK) | 131 if (status == SERVICE_WORKER_OK) |
| 49 status_ = STOPPING; | 132 status_ = STOPPING; |
| 50 return status; | 133 return status; |
| 51 } | 134 } |
| (...skipping 16 matching lines...) Expand all Loading... |
| 68 void EmbeddedWorkerInstance::ReleaseProcessReference(int process_id) { | 151 void EmbeddedWorkerInstance::ReleaseProcessReference(int process_id) { |
| 69 ProcessRefMap::iterator found = process_refs_.find(process_id); | 152 ProcessRefMap::iterator found = process_refs_.find(process_id); |
| 70 if (found == process_refs_.end()) { | 153 if (found == process_refs_.end()) { |
| 71 NOTREACHED() << "Releasing unknown process ref " << process_id; | 154 NOTREACHED() << "Releasing unknown process ref " << process_id; |
| 72 return; | 155 return; |
| 73 } | 156 } |
| 74 if (--found->second == 0) | 157 if (--found->second == 0) |
| 75 process_refs_.erase(found); | 158 process_refs_.erase(found); |
| 76 } | 159 } |
| 77 | 160 |
| 78 EmbeddedWorkerInstance::EmbeddedWorkerInstance(EmbeddedWorkerRegistry* registry, | 161 EmbeddedWorkerInstance::EmbeddedWorkerInstance( |
| 79 int embedded_worker_id) | 162 base::WeakPtr<ServiceWorkerContextCore> context, |
| 80 : registry_(registry), | 163 int embedded_worker_id) |
| 164 : context_(context), |
| 165 registry_(context->embedded_worker_registry()), |
| 81 embedded_worker_id_(embedded_worker_id), | 166 embedded_worker_id_(embedded_worker_id), |
| 82 status_(STOPPED), | 167 status_(STOPPED), |
| 83 process_id_(-1), | 168 process_id_(-1), |
| 84 thread_id_(-1), | 169 thread_id_(-1), |
| 85 worker_devtools_agent_route_id_(MSG_ROUTING_NONE) { | 170 worker_devtools_agent_route_id_(MSG_ROUTING_NONE), |
| 171 weak_factory_(this) { |
| 86 } | 172 } |
| 87 | 173 |
| 88 void EmbeddedWorkerInstance::RecordProcessId( | 174 // static |
| 175 void EmbeddedWorkerInstance::RunProcessAllocated( |
| 176 base::WeakPtr<EmbeddedWorkerInstance> instance, |
| 177 base::WeakPtr<ServiceWorkerContextCore> context, |
| 178 scoped_ptr<EmbeddedWorkerMsg_StartWorker_Params> params, |
| 179 const EmbeddedWorkerInstance::StatusCallback& callback, |
| 180 ServiceWorkerStatusCode status, |
| 181 int process_id) { |
| 182 if (!context) { |
| 183 callback.Run(SERVICE_WORKER_ERROR_ABORT); |
| 184 return; |
| 185 } |
| 186 if (!instance) { |
| 187 context->process_manager()->ReleaseWorkerProcess(process_id); |
| 188 callback.Run(SERVICE_WORKER_ERROR_ABORT); |
| 189 return; |
| 190 } |
| 191 instance->ProcessAllocated(params.Pass(), callback, process_id, status); |
| 192 } |
| 193 |
| 194 void EmbeddedWorkerInstance::ProcessAllocated( |
| 195 scoped_ptr<EmbeddedWorkerMsg_StartWorker_Params> params, |
| 196 const StatusCallback& callback, |
| 89 int process_id, | 197 int process_id, |
| 90 ServiceWorkerStatusCode status, | 198 ServiceWorkerStatusCode status) { |
| 91 int worker_devtools_agent_route_id) { | |
| 92 DCHECK_EQ(process_id_, -1); | 199 DCHECK_EQ(process_id_, -1); |
| 93 DCHECK_EQ(worker_devtools_agent_route_id_, MSG_ROUTING_NONE); | 200 if (status != SERVICE_WORKER_OK) { |
| 94 if (status == SERVICE_WORKER_OK) { | |
| 95 process_id_ = process_id; | |
| 96 worker_devtools_agent_route_id_ = worker_devtools_agent_route_id; | |
| 97 } else { | |
| 98 status_ = STOPPED; | 201 status_ = STOPPED; |
| 202 callback.Run(status); |
| 203 return; |
| 99 } | 204 } |
| 205 const int64 service_worker_version_id = params->service_worker_version_id; |
| 206 process_id_ = process_id; |
| 207 RegisterToWorkerDevToolsManager( |
| 208 process_id, |
| 209 context_.get(), |
| 210 service_worker_version_id, |
| 211 base::Bind(&EmbeddedWorkerInstance::SendStartWorker, |
| 212 weak_factory_.GetWeakPtr(), |
| 213 base::Passed(¶ms), |
| 214 callback)); |
| 215 } |
| 216 |
| 217 void EmbeddedWorkerInstance::SendStartWorker( |
| 218 scoped_ptr<EmbeddedWorkerMsg_StartWorker_Params> params, |
| 219 const StatusCallback& callback, |
| 220 int worker_devtools_agent_route_id, |
| 221 bool pause_on_start) { |
| 222 worker_devtools_agent_route_id_ = worker_devtools_agent_route_id; |
| 223 params->worker_devtools_agent_route_id = worker_devtools_agent_route_id; |
| 224 params->pause_on_start = pause_on_start; |
| 225 registry_->SendStartWorker(params.Pass(), callback, process_id_); |
| 100 } | 226 } |
| 101 | 227 |
| 102 void EmbeddedWorkerInstance::OnScriptLoaded() { | 228 void EmbeddedWorkerInstance::OnScriptLoaded() { |
| 103 // TODO(horo): Implement this. | 229 if (worker_devtools_agent_route_id_ != MSG_ROUTING_NONE) |
| 230 NotifyWorkerContextStarted(process_id_, worker_devtools_agent_route_id_); |
| 104 } | 231 } |
| 105 | 232 |
| 106 void EmbeddedWorkerInstance::OnScriptLoadFailed() { | 233 void EmbeddedWorkerInstance::OnScriptLoadFailed() { |
| 107 } | 234 } |
| 108 | 235 |
| 109 void EmbeddedWorkerInstance::OnStarted(int thread_id) { | 236 void EmbeddedWorkerInstance::OnStarted(int thread_id) { |
| 110 // Stop is requested before OnStarted is sent back from the worker. | 237 // Stop is requested before OnStarted is sent back from the worker. |
| 111 if (status_ == STOPPING) | 238 if (status_ == STOPPING) |
| 112 return; | 239 return; |
| 113 DCHECK(status_ == STARTING); | 240 DCHECK(status_ == STARTING); |
| 114 status_ = RUNNING; | 241 status_ = RUNNING; |
| 115 thread_id_ = thread_id; | 242 thread_id_ = thread_id; |
| 116 FOR_EACH_OBSERVER(Listener, listener_list_, OnStarted()); | 243 FOR_EACH_OBSERVER(Listener, listener_list_, OnStarted()); |
| 117 } | 244 } |
| 118 | 245 |
| 119 void EmbeddedWorkerInstance::OnStopped() { | 246 void EmbeddedWorkerInstance::OnStopped() { |
| 247 if (worker_devtools_agent_route_id_ != MSG_ROUTING_NONE) |
| 248 NotifyWorkerDestroyed(process_id_, worker_devtools_agent_route_id_); |
| 249 if (context_) |
| 250 context_->process_manager()->ReleaseWorkerProcess(process_id_); |
| 120 status_ = STOPPED; | 251 status_ = STOPPED; |
| 121 process_id_ = -1; | 252 process_id_ = -1; |
| 122 thread_id_ = -1; | 253 thread_id_ = -1; |
| 123 worker_devtools_agent_route_id_ = MSG_ROUTING_NONE; | 254 worker_devtools_agent_route_id_ = MSG_ROUTING_NONE; |
| 124 FOR_EACH_OBSERVER(Listener, listener_list_, OnStopped()); | 255 FOR_EACH_OBSERVER(Listener, listener_list_, OnStopped()); |
| 125 } | 256 } |
| 126 | 257 |
| 127 bool EmbeddedWorkerInstance::OnMessageReceived(const IPC::Message& message) { | 258 bool EmbeddedWorkerInstance::OnMessageReceived(const IPC::Message& message) { |
| 128 ListenerList::Iterator it(listener_list_); | 259 ListenerList::Iterator it(listener_list_); |
| 129 while (Listener* listener = it.GetNext()) { | 260 while (Listener* listener = it.GetNext()) { |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 181 // Sort descending by the reference count. | 312 // Sort descending by the reference count. |
| 182 std::sort(counted.begin(), counted.end(), SecondGreater()); | 313 std::sort(counted.begin(), counted.end(), SecondGreater()); |
| 183 | 314 |
| 184 std::vector<int> result(counted.size()); | 315 std::vector<int> result(counted.size()); |
| 185 for (size_t i = 0; i < counted.size(); ++i) | 316 for (size_t i = 0; i < counted.size(); ++i) |
| 186 result[i] = counted[i].first; | 317 result[i] = counted[i].first; |
| 187 return result; | 318 return result; |
| 188 } | 319 } |
| 189 | 320 |
| 190 } // namespace content | 321 } // namespace content |
| OLD | NEW |