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