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 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 storage_partition_path, |
| 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 storage_partition_path, |
| 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, | |
30 const GURL& script_url, | 99 const GURL& script_url, |
31 const std::vector<int>& possible_process_ids, | 100 const std::vector<int>& possible_process_ids, |
32 const StatusCallback& callback) { | 101 const StatusCallback& callback) { |
| 102 if (!context_) { |
| 103 callback.Run(SERVICE_WORKER_ERROR_ABORT); |
| 104 return; |
| 105 } |
33 DCHECK(status_ == STOPPED); | 106 DCHECK(status_ == STOPPED); |
34 status_ = STARTING; | 107 status_ = STARTING; |
35 std::vector<int> ordered_process_ids = SortProcesses(possible_process_ids); | 108 scoped_ptr<EmbeddedWorkerMsg_StartWorker_Params> params( |
36 registry_->StartWorker(ordered_process_ids, | 109 new EmbeddedWorkerMsg_StartWorker_Params()); |
37 embedded_worker_id_, | 110 params->embedded_worker_id = embedded_worker_id_; |
38 service_worker_version_id, | 111 params->service_worker_version_id = service_worker_version_id; |
39 scope, | 112 params->scope = scope_; |
40 script_url, | 113 params->script_url = script_url; |
41 callback); | 114 params->worker_devtools_agent_route_id = MSG_ROUTING_NONE; |
| 115 params->pause_on_start = false; |
| 116 context_->process_manager()->AllocateWorkerProcess( |
| 117 SortProcesses(possible_process_ids), |
| 118 script_url, |
| 119 base::Bind(&EmbeddedWorkerInstance::RunProcessAllocated, |
| 120 weak_factory_.GetWeakPtr(), |
| 121 context_, |
| 122 base::Passed(¶ms), |
| 123 callback)); |
42 } | 124 } |
43 | 125 |
44 ServiceWorkerStatusCode EmbeddedWorkerInstance::Stop() { | 126 ServiceWorkerStatusCode EmbeddedWorkerInstance::Stop() { |
45 DCHECK(status_ == STARTING || status_ == RUNNING); | 127 DCHECK(status_ == STARTING || status_ == RUNNING); |
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 } |
(...skipping 16 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 const int64 service_worker_version_id = params->service_worker_version_id; |
| 207 process_id_ = process_id; |
| 208 RegisterToWorkerDevToolsManager( |
| 209 process_id, |
| 210 context_->storage_partition_path(), |
| 211 service_worker_version_id, |
| 212 base::Bind(&EmbeddedWorkerInstance::SendStartWorker, |
| 213 weak_factory_.GetWeakPtr(), |
| 214 base::Passed(¶ms), |
| 215 callback)); |
| 216 } |
| 217 |
| 218 void EmbeddedWorkerInstance::SendStartWorker( |
| 219 scoped_ptr<EmbeddedWorkerMsg_StartWorker_Params> params, |
| 220 const StatusCallback& callback, |
| 221 int worker_devtools_agent_route_id, |
| 222 bool pause_on_start) { |
| 223 worker_devtools_agent_route_id_ = worker_devtools_agent_route_id; |
| 224 params->worker_devtools_agent_route_id = worker_devtools_agent_route_id; |
| 225 params->pause_on_start = pause_on_start; |
| 226 registry_->SendStartWorker(params.Pass(), callback, process_id_); |
100 } | 227 } |
101 | 228 |
102 void EmbeddedWorkerInstance::OnScriptLoaded() { | 229 void EmbeddedWorkerInstance::OnScriptLoaded() { |
103 // TODO(horo): Implement this. | 230 if (worker_devtools_agent_route_id_ != MSG_ROUTING_NONE) |
| 231 NotifyWorkerContextStarted(process_id_, worker_devtools_agent_route_id_); |
104 } | 232 } |
105 | 233 |
106 void EmbeddedWorkerInstance::OnScriptLoadFailed() { | 234 void EmbeddedWorkerInstance::OnScriptLoadFailed() { |
107 } | 235 } |
108 | 236 |
109 void EmbeddedWorkerInstance::OnStarted(int thread_id) { | 237 void EmbeddedWorkerInstance::OnStarted(int thread_id) { |
110 // Stop is requested before OnStarted is sent back from the worker. | 238 // Stop is requested before OnStarted is sent back from the worker. |
111 if (status_ == STOPPING) | 239 if (status_ == STOPPING) |
112 return; | 240 return; |
113 DCHECK(status_ == STARTING); | 241 DCHECK(status_ == STARTING); |
114 status_ = RUNNING; | 242 status_ = RUNNING; |
115 thread_id_ = thread_id; | 243 thread_id_ = thread_id; |
116 FOR_EACH_OBSERVER(Listener, listener_list_, OnStarted()); | 244 FOR_EACH_OBSERVER(Listener, listener_list_, OnStarted()); |
117 } | 245 } |
118 | 246 |
119 void EmbeddedWorkerInstance::OnStopped() { | 247 void EmbeddedWorkerInstance::OnStopped() { |
| 248 if (worker_devtools_agent_route_id_ != MSG_ROUTING_NONE) |
| 249 NotifyWorkerDestroyed(process_id_, worker_devtools_agent_route_id_); |
| 250 if (context_) |
| 251 context_->process_manager()->ReleaseWorkerProcess(process_id_); |
120 status_ = STOPPED; | 252 status_ = STOPPED; |
121 process_id_ = -1; | 253 process_id_ = -1; |
122 thread_id_ = -1; | 254 thread_id_ = -1; |
123 worker_devtools_agent_route_id_ = MSG_ROUTING_NONE; | 255 worker_devtools_agent_route_id_ = MSG_ROUTING_NONE; |
124 FOR_EACH_OBSERVER(Listener, listener_list_, OnStopped()); | 256 FOR_EACH_OBSERVER(Listener, listener_list_, OnStopped()); |
125 } | 257 } |
126 | 258 |
127 bool EmbeddedWorkerInstance::OnMessageReceived(const IPC::Message& message) { | 259 bool EmbeddedWorkerInstance::OnMessageReceived(const IPC::Message& message) { |
128 ListenerList::Iterator it(listener_list_); | 260 ListenerList::Iterator it(listener_list_); |
129 while (Listener* listener = it.GetNext()) { | 261 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. | 313 // Sort descending by the reference count. |
182 std::sort(counted.begin(), counted.end(), SecondGreater()); | 314 std::sort(counted.begin(), counted.end(), SecondGreater()); |
183 | 315 |
184 std::vector<int> result(counted.size()); | 316 std::vector<int> result(counted.size()); |
185 for (size_t i = 0; i < counted.size(); ++i) | 317 for (size_t i = 0; i < counted.size(); ++i) |
186 result[i] = counted[i].first; | 318 result[i] = counted[i].first; |
187 return result; | 319 return result; |
188 } | 320 } |
189 | 321 |
190 } // namespace content | 322 } // namespace content |
OLD | NEW |