| 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 base::FilePath& storage_partition_path, |
| 57 const GURL& scope, |
| 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 storage_partition_path, |
| 66 scope, |
| 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 storage_partition_path, |
| 80 scope); |
| 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_); |
| 25 registry_->RemoveWorker(process_id_, embedded_worker_id_); | 93 registry_->RemoveWorker(process_id_, embedded_worker_id_); |
| 26 } | 94 } |
| 27 | 95 |
| 28 void EmbeddedWorkerInstance::Start(int64 service_worker_version_id, | 96 void EmbeddedWorkerInstance::Start(int64 service_worker_version_id, |
| 29 const GURL& scope, | |
| 30 const GURL& script_url, | 97 const GURL& script_url, |
| 31 const std::vector<int>& possible_process_ids, | 98 const std::vector<int>& possible_process_ids, |
| 32 const StatusCallback& callback) { | 99 const StatusCallback& callback) { |
| 100 if (!context_) { |
| 101 callback.Run(SERVICE_WORKER_ERROR_ABORT); |
| 102 return; |
| 103 } |
| 33 DCHECK(status_ == STOPPED); | 104 DCHECK(status_ == STOPPED); |
| 34 status_ = STARTING; | 105 status_ = STARTING; |
| 35 std::vector<int> ordered_process_ids = SortProcesses(possible_process_ids); | 106 scoped_ptr<EmbeddedWorkerMsg_StartWorker_Params> params( |
| 36 registry_->StartWorker(ordered_process_ids, | 107 new EmbeddedWorkerMsg_StartWorker_Params()); |
| 37 embedded_worker_id_, | 108 params->embedded_worker_id = embedded_worker_id_; |
| 38 service_worker_version_id, | 109 params->service_worker_version_id = service_worker_version_id; |
| 39 scope, | 110 params->scope = scope_; |
| 40 script_url, | 111 params->script_url = script_url; |
| 41 callback); | 112 params->worker_devtools_agent_route_id = MSG_ROUTING_NONE; |
| 113 params->pause_on_start = false; |
| 114 context_->process_manager()->AllocateWorkerProcess( |
| 115 SortProcesses(possible_process_ids), |
| 116 script_url, |
| 117 base::Bind(&EmbeddedWorkerInstance::RunProcessAllocated, |
| 118 AsWeakPtr(), |
| 119 context_, |
| 120 base::Passed(¶ms), |
| 121 callback)); |
| 42 } | 122 } |
| 43 | 123 |
| 44 ServiceWorkerStatusCode EmbeddedWorkerInstance::Stop() { | 124 ServiceWorkerStatusCode EmbeddedWorkerInstance::Stop() { |
| 45 DCHECK(status_ == STARTING || status_ == RUNNING); | 125 DCHECK(status_ == STARTING || status_ == RUNNING); |
| 46 ServiceWorkerStatusCode status = | 126 ServiceWorkerStatusCode status = |
| 47 registry_->StopWorker(process_id_, embedded_worker_id_); | 127 registry_->StopWorker(process_id_, embedded_worker_id_); |
| 48 if (status == SERVICE_WORKER_OK) | 128 if (status == SERVICE_WORKER_OK) |
| 49 status_ = STOPPING; | 129 status_ = STOPPING; |
| 50 return status; | 130 return status; |
| 51 } | 131 } |
| (...skipping 16 matching lines...) Expand all Loading... |
| 68 void EmbeddedWorkerInstance::ReleaseProcessReference(int process_id) { | 148 void EmbeddedWorkerInstance::ReleaseProcessReference(int process_id) { |
| 69 ProcessRefMap::iterator found = process_refs_.find(process_id); | 149 ProcessRefMap::iterator found = process_refs_.find(process_id); |
| 70 if (found == process_refs_.end()) { | 150 if (found == process_refs_.end()) { |
| 71 NOTREACHED() << "Releasing unknown process ref " << process_id; | 151 NOTREACHED() << "Releasing unknown process ref " << process_id; |
| 72 return; | 152 return; |
| 73 } | 153 } |
| 74 if (--found->second == 0) | 154 if (--found->second == 0) |
| 75 process_refs_.erase(found); | 155 process_refs_.erase(found); |
| 76 } | 156 } |
| 77 | 157 |
| 78 EmbeddedWorkerInstance::EmbeddedWorkerInstance(EmbeddedWorkerRegistry* registry, | 158 EmbeddedWorkerInstance::EmbeddedWorkerInstance( |
| 79 int embedded_worker_id) | 159 base::WeakPtr<ServiceWorkerContextCore> context, |
| 80 : registry_(registry), | 160 int embedded_worker_id, |
| 161 const GURL& scope) |
| 162 : context_(context), |
| 163 registry_(context->embedded_worker_registry()), |
| 81 embedded_worker_id_(embedded_worker_id), | 164 embedded_worker_id_(embedded_worker_id), |
| 165 scope_(scope), |
| 82 status_(STOPPED), | 166 status_(STOPPED), |
| 83 process_id_(-1), | 167 process_id_(-1), |
| 84 thread_id_(-1), | 168 thread_id_(-1), |
| 85 worker_devtools_agent_route_id_(MSG_ROUTING_NONE) { | 169 worker_devtools_agent_route_id_(MSG_ROUTING_NONE) { |
| 86 } | 170 } |
| 87 | 171 |
| 88 void EmbeddedWorkerInstance::RecordProcessId( | 172 // static |
| 173 void EmbeddedWorkerInstance::RunProcessAllocated( |
| 174 base::WeakPtr<EmbeddedWorkerInstance> instance, |
| 175 base::WeakPtr<ServiceWorkerContextCore> context, |
| 176 scoped_ptr<EmbeddedWorkerMsg_StartWorker_Params> params, |
| 177 const EmbeddedWorkerInstance::StatusCallback& callback, |
| 178 ServiceWorkerStatusCode status, |
| 179 int process_id) { |
| 180 if (!context) { |
| 181 callback.Run(SERVICE_WORKER_ERROR_ABORT); |
| 182 return; |
| 183 } |
| 184 if (!instance) { |
| 185 context->process_manager()->ReleaseWorkerProcess(process_id); |
| 186 callback.Run(SERVICE_WORKER_ERROR_ABORT); |
| 187 return; |
| 188 } |
| 189 instance->ProcessAllocated(params.Pass(), callback, process_id, status); |
| 190 } |
| 191 |
| 192 void EmbeddedWorkerInstance::ProcessAllocated( |
| 193 scoped_ptr<EmbeddedWorkerMsg_StartWorker_Params> params, |
| 194 const StatusCallback& callback, |
| 89 int process_id, | 195 int process_id, |
| 90 ServiceWorkerStatusCode status, | 196 ServiceWorkerStatusCode status) { |
| 91 int worker_devtools_agent_route_id) { | |
| 92 DCHECK_EQ(process_id_, -1); | 197 DCHECK_EQ(process_id_, -1); |
| 93 DCHECK_EQ(worker_devtools_agent_route_id_, MSG_ROUTING_NONE); | 198 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; | 199 status_ = STOPPED; |
| 200 callback.Run(status); |
| 201 return; |
| 99 } | 202 } |
| 203 process_id_ = process_id; |
| 204 RegisterToWorkerDevToolsManager( |
| 205 process_id, |
| 206 context_->storage_partition_path(), |
| 207 scope_, |
| 208 base::Bind(&EmbeddedWorkerInstance::RunSendStartWorker, |
| 209 AsWeakPtr(), |
| 210 base::Passed(¶ms), |
| 211 callback)); |
| 212 } |
| 213 |
| 214 // static |
| 215 void EmbeddedWorkerInstance::RunSendStartWorker( |
| 216 base::WeakPtr<EmbeddedWorkerInstance> instance, |
| 217 scoped_ptr<EmbeddedWorkerMsg_StartWorker_Params> params, |
| 218 const EmbeddedWorkerInstance::StatusCallback& callback, |
| 219 int worker_devtools_agent_route_id, |
| 220 bool pause_on_start) { |
| 221 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 222 if (!instance) { |
| 223 callback.Run(SERVICE_WORKER_ERROR_ABORT); |
| 224 return; |
| 225 } |
| 226 params->worker_devtools_agent_route_id = worker_devtools_agent_route_id; |
| 227 params->pause_on_start = pause_on_start; |
| 228 instance->SendStartWorker(params.Pass(), callback); |
| 229 } |
| 230 |
| 231 void EmbeddedWorkerInstance::SendStartWorker( |
| 232 scoped_ptr<EmbeddedWorkerMsg_StartWorker_Params> params, |
| 233 const StatusCallback& callback) { |
| 234 worker_devtools_agent_route_id_ = params->worker_devtools_agent_route_id; |
| 235 registry_->SendStartWorker(params.Pass(), callback, process_id_); |
| 100 } | 236 } |
| 101 | 237 |
| 102 void EmbeddedWorkerInstance::OnScriptLoaded() { | 238 void EmbeddedWorkerInstance::OnScriptLoaded() { |
| 103 // TODO(horo): Implement this. | 239 if (worker_devtools_agent_route_id_ != MSG_ROUTING_NONE) |
| 240 NotifyWorkerContextStarted(process_id_, worker_devtools_agent_route_id_); |
| 104 } | 241 } |
| 105 | 242 |
| 106 void EmbeddedWorkerInstance::OnScriptLoadFailed() { | 243 void EmbeddedWorkerInstance::OnScriptLoadFailed() { |
| 107 } | 244 } |
| 108 | 245 |
| 109 void EmbeddedWorkerInstance::OnStarted(int thread_id) { | 246 void EmbeddedWorkerInstance::OnStarted(int thread_id) { |
| 110 // Stop is requested before OnStarted is sent back from the worker. | 247 // Stop is requested before OnStarted is sent back from the worker. |
| 111 if (status_ == STOPPING) | 248 if (status_ == STOPPING) |
| 112 return; | 249 return; |
| 113 DCHECK(status_ == STARTING); | 250 DCHECK(status_ == STARTING); |
| 114 status_ = RUNNING; | 251 status_ = RUNNING; |
| 115 thread_id_ = thread_id; | 252 thread_id_ = thread_id; |
| 116 FOR_EACH_OBSERVER(Listener, listener_list_, OnStarted()); | 253 FOR_EACH_OBSERVER(Listener, listener_list_, OnStarted()); |
| 117 } | 254 } |
| 118 | 255 |
| 119 void EmbeddedWorkerInstance::OnStopped() { | 256 void EmbeddedWorkerInstance::OnStopped() { |
| 257 if (worker_devtools_agent_route_id_ != MSG_ROUTING_NONE) |
| 258 NotifyWorkerDestroyed(process_id_, worker_devtools_agent_route_id_); |
| 120 status_ = STOPPED; | 259 status_ = STOPPED; |
| 121 process_id_ = -1; | 260 process_id_ = -1; |
| 122 thread_id_ = -1; | 261 thread_id_ = -1; |
| 123 worker_devtools_agent_route_id_ = MSG_ROUTING_NONE; | 262 worker_devtools_agent_route_id_ = MSG_ROUTING_NONE; |
| 124 FOR_EACH_OBSERVER(Listener, listener_list_, OnStopped()); | 263 FOR_EACH_OBSERVER(Listener, listener_list_, OnStopped()); |
| 125 } | 264 } |
| 126 | 265 |
| 127 bool EmbeddedWorkerInstance::OnMessageReceived(const IPC::Message& message) { | 266 bool EmbeddedWorkerInstance::OnMessageReceived(const IPC::Message& message) { |
| 128 ListenerList::Iterator it(listener_list_); | 267 ListenerList::Iterator it(listener_list_); |
| 129 while (Listener* listener = it.GetNext()) { | 268 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. | 320 // Sort descending by the reference count. |
| 182 std::sort(counted.begin(), counted.end(), SecondGreater()); | 321 std::sort(counted.begin(), counted.end(), SecondGreater()); |
| 183 | 322 |
| 184 std::vector<int> result(counted.size()); | 323 std::vector<int> result(counted.size()); |
| 185 for (size_t i = 0; i < counted.size(); ++i) | 324 for (size_t i = 0; i < counted.size(); ++i) |
| 186 result[i] = counted[i].first; | 325 result[i] = counted[i].first; |
| 187 return result; | 326 return result; |
| 188 } | 327 } |
| 189 | 328 |
| 190 } // namespace content | 329 } // namespace content |
| OLD | NEW |