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