OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/devtools/protocol/service_worker_handler.h" | 5 #include "content/browser/devtools/protocol/service_worker_handler.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/containers/scoped_ptr_hash_map.h" | 8 #include "base/containers/scoped_ptr_hash_map.h" |
9 #include "base/strings/string_number_conversions.h" | 9 #include "base/strings/string_number_conversions.h" |
10 #include "content/browser/devtools/service_worker_devtools_agent_host.h" | 10 #include "content/browser/devtools/service_worker_devtools_agent_host.h" |
11 #include "content/browser/devtools/service_worker_devtools_manager.h" | 11 #include "content/browser/devtools/service_worker_devtools_manager.h" |
12 #include "content/browser/frame_host/frame_tree.h" | 12 #include "content/browser/frame_host/frame_tree.h" |
13 #include "content/browser/frame_host/frame_tree_node.h" | 13 #include "content/browser/frame_host/frame_tree_node.h" |
14 #include "content/browser/frame_host/render_frame_host_impl.h" | 14 #include "content/browser/frame_host/render_frame_host_impl.h" |
| 15 #include "content/browser/service_worker/service_worker_context_core.h" |
15 #include "content/browser/service_worker/service_worker_context_watcher.h" | 16 #include "content/browser/service_worker/service_worker_context_watcher.h" |
16 #include "content/browser/service_worker/service_worker_context_wrapper.h" | 17 #include "content/browser/service_worker/service_worker_context_wrapper.h" |
17 #include "content/browser/service_worker/service_worker_version.h" | 18 #include "content/browser/service_worker/service_worker_version.h" |
18 #include "content/public/browser/browser_context.h" | 19 #include "content/public/browser/browser_context.h" |
19 #include "content/public/browser/browser_thread.h" | 20 #include "content/public/browser/browser_thread.h" |
20 #include "content/public/browser/devtools_agent_host.h" | 21 #include "content/public/browser/devtools_agent_host.h" |
21 #include "content/public/browser/render_frame_host.h" | 22 #include "content/public/browser/render_frame_host.h" |
22 #include "content/public/browser/render_process_host.h" | 23 #include "content/public/browser/render_process_host.h" |
23 #include "content/public/browser/storage_partition.h" | 24 #include "content/public/browser/storage_partition.h" |
24 #include "url/gurl.h" | 25 #include "url/gurl.h" |
25 | 26 |
26 // Windows headers will redefine SendMessage. | 27 // Windows headers will redefine SendMessage. |
27 #ifdef SendMessage | 28 #ifdef SendMessage |
28 #undef SendMessage | 29 #undef SendMessage |
29 #endif | 30 #endif |
30 | 31 |
31 namespace content { | 32 namespace content { |
32 namespace devtools { | 33 namespace devtools { |
33 namespace service_worker { | 34 namespace service_worker { |
34 | 35 |
| 36 using Response = DevToolsProtocolClient::Response; |
| 37 |
35 namespace { | 38 namespace { |
36 | 39 |
37 const char kServiceWorkerVersionRunningStatusStopped[] = "stopped"; | 40 void ResultNoOp(bool success) { |
38 const char kServiceWorkerVersionRunningStatusStarting[] = "starting"; | 41 } |
39 const char kServiceWorkerVersionRunningStatusRunning[] = "running"; | 42 void StatusNoOp(ServiceWorkerStatusCode status) { |
40 const char kServiceWorkerVersionRunningStatusStopping[] = "stopping"; | 43 } |
41 | |
42 const char kServiceWorkerVersionStatusNew[] = "new"; | |
43 const char kServiceWorkerVersionStatusInstalling[] = "installing"; | |
44 const char kServiceWorkerVersionStatusInstalled[] = "installed"; | |
45 const char kServiceWorkerVersionStatusActivating[] = "activating"; | |
46 const char kServiceWorkerVersionStatusActivated[] = "activated"; | |
47 const char kServiceWorkerVersionStatusRedundant[] = "redundant"; | |
48 | 44 |
49 const std::string GetVersionRunningStatusString( | 45 const std::string GetVersionRunningStatusString( |
50 content::ServiceWorkerVersion::RunningStatus running_status) { | 46 content::ServiceWorkerVersion::RunningStatus running_status) { |
51 switch (running_status) { | 47 switch (running_status) { |
52 case content::ServiceWorkerVersion::STOPPED: | 48 case content::ServiceWorkerVersion::STOPPED: |
53 return kServiceWorkerVersionRunningStatusStopped; | 49 return kServiceWorkerVersionRunningStatusStopped; |
54 case content::ServiceWorkerVersion::STARTING: | 50 case content::ServiceWorkerVersion::STARTING: |
55 return kServiceWorkerVersionRunningStatusStarting; | 51 return kServiceWorkerVersionRunningStatusStarting; |
56 case content::ServiceWorkerVersion::RUNNING: | 52 case content::ServiceWorkerVersion::RUNNING: |
57 return kServiceWorkerVersionRunningStatusRunning; | 53 return kServiceWorkerVersionRunningStatusRunning; |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
138 result[host->GetId()] = host; | 134 result[host->GetId()] = host; |
139 } | 135 } |
140 return result; | 136 return result; |
141 } | 137 } |
142 | 138 |
143 bool CollectURLs(std::set<GURL>* urls, FrameTreeNode* tree_node) { | 139 bool CollectURLs(std::set<GURL>* urls, FrameTreeNode* tree_node) { |
144 urls->insert(tree_node->current_url()); | 140 urls->insert(tree_node->current_url()); |
145 return false; | 141 return false; |
146 } | 142 } |
147 | 143 |
| 144 void StopServiceWorkerOnIO(scoped_refptr<ServiceWorkerContextWrapper> context, |
| 145 int64 version_id) { |
| 146 ServiceWorkerContextCore* context_core = context->context(); |
| 147 if (!context_core) |
| 148 return; |
| 149 if (content::ServiceWorkerVersion* version = |
| 150 context_core->GetLiveVersion(version_id)) { |
| 151 version->StopWorker(base::Bind(&StatusNoOp)); |
| 152 } |
| 153 } |
| 154 |
| 155 void GetDevToolsRouteInfoOnIO( |
| 156 scoped_refptr<ServiceWorkerContextWrapper> context, |
| 157 int64 version_id, |
| 158 const base::Callback<void(int, int)>& callback) { |
| 159 ServiceWorkerContextCore* context_core = context->context(); |
| 160 if (!context_core) |
| 161 return; |
| 162 if (content::ServiceWorkerVersion* version = |
| 163 context_core->GetLiveVersion(version_id)) { |
| 164 BrowserThread::PostTask( |
| 165 BrowserThread::UI, FROM_HERE, |
| 166 base::Bind( |
| 167 callback, version->embedded_worker()->process_id(), |
| 168 version->embedded_worker()->worker_devtools_agent_route_id())); |
| 169 } |
| 170 } |
| 171 |
| 172 Response CreateContextErrorResoponse() { |
| 173 return Response::InternalError("Could not connect to the context"); |
| 174 } |
| 175 |
| 176 Response CreateInvalidVersionIdErrorResoponse() { |
| 177 return Response::InternalError("Invalid version ID"); |
| 178 } |
| 179 |
148 } // namespace | 180 } // namespace |
149 | 181 |
150 using Response = DevToolsProtocolClient::Response; | |
151 | |
152 ServiceWorkerHandler::ServiceWorkerHandler() | 182 ServiceWorkerHandler::ServiceWorkerHandler() |
153 : enabled_(false), weak_factory_(this) { | 183 : enabled_(false), weak_factory_(this) { |
154 } | 184 } |
155 | 185 |
156 ServiceWorkerHandler::~ServiceWorkerHandler() { | 186 ServiceWorkerHandler::~ServiceWorkerHandler() { |
157 Disable(); | 187 Disable(); |
158 } | 188 } |
159 | 189 |
160 void ServiceWorkerHandler::SetRenderFrameHost( | 190 void ServiceWorkerHandler::SetRenderFrameHost( |
161 RenderFrameHostImpl* render_frame_host) { | 191 RenderFrameHostImpl* render_frame_host) { |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
253 | 283 |
254 Response ServiceWorkerHandler::Stop( | 284 Response ServiceWorkerHandler::Stop( |
255 const std::string& worker_id) { | 285 const std::string& worker_id) { |
256 auto it = attached_hosts_.find(worker_id); | 286 auto it = attached_hosts_.find(worker_id); |
257 if (it == attached_hosts_.end()) | 287 if (it == attached_hosts_.end()) |
258 return Response::InternalError("Not connected to the worker"); | 288 return Response::InternalError("Not connected to the worker"); |
259 it->second->UnregisterWorker(); | 289 it->second->UnregisterWorker(); |
260 return Response::OK(); | 290 return Response::OK(); |
261 } | 291 } |
262 | 292 |
| 293 Response ServiceWorkerHandler::Unregister(const std::string& scope_url) { |
| 294 if (!enabled_) |
| 295 return Response::OK(); |
| 296 if (!context_) |
| 297 return CreateContextErrorResoponse(); |
| 298 context_->UnregisterServiceWorker(GURL(scope_url), base::Bind(&ResultNoOp)); |
| 299 return Response::OK(); |
| 300 } |
| 301 |
| 302 Response ServiceWorkerHandler::StartWorker(const std::string& scope_url) { |
| 303 if (!enabled_) |
| 304 return Response::OK(); |
| 305 if (!context_) |
| 306 return CreateContextErrorResoponse(); |
| 307 context_->StartServiceWorker(GURL(scope_url), base::Bind(&StatusNoOp)); |
| 308 return Response::OK(); |
| 309 } |
| 310 |
| 311 Response ServiceWorkerHandler::StopWorker(const std::string& version_id) { |
| 312 if (!enabled_) |
| 313 return Response::OK(); |
| 314 if (!context_) |
| 315 return CreateContextErrorResoponse(); |
| 316 int64 id = 0; |
| 317 if (!base::StringToInt64(version_id, &id)) |
| 318 return CreateInvalidVersionIdErrorResoponse(); |
| 319 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
| 320 base::Bind(&StopServiceWorkerOnIO, context_, id)); |
| 321 return Response::OK(); |
| 322 } |
| 323 |
| 324 Response ServiceWorkerHandler::InspectWorker(const std::string& version_id) { |
| 325 if (!enabled_) |
| 326 return Response::OK(); |
| 327 if (!context_) |
| 328 return CreateContextErrorResoponse(); |
| 329 |
| 330 int64 id = 0; |
| 331 if (!base::StringToInt64(version_id, &id)) |
| 332 return CreateInvalidVersionIdErrorResoponse(); |
| 333 BrowserThread::PostTask( |
| 334 BrowserThread::IO, FROM_HERE, |
| 335 base::Bind(&GetDevToolsRouteInfoOnIO, context_, id, |
| 336 base::Bind(&ServiceWorkerHandler::OpenNewDevToolsWindow, |
| 337 weak_factory_.GetWeakPtr()))); |
| 338 return Response::OK(); |
| 339 } |
| 340 |
| 341 void ServiceWorkerHandler::OpenNewDevToolsWindow(int process_id, |
| 342 int devtools_agent_route_id) { |
| 343 scoped_refptr<DevToolsAgentHostImpl> agent_host( |
| 344 ServiceWorkerDevToolsManager::GetInstance() |
| 345 ->GetDevToolsAgentHostForWorker(process_id, devtools_agent_route_id)); |
| 346 if (!agent_host.get()) |
| 347 return; |
| 348 agent_host->Inspect(render_frame_host_->GetProcess()->GetBrowserContext()); |
| 349 } |
| 350 |
263 void ServiceWorkerHandler::OnWorkerRegistrationUpdated( | 351 void ServiceWorkerHandler::OnWorkerRegistrationUpdated( |
264 const std::vector<ServiceWorkerRegistrationInfo>& registrations) { | 352 const std::vector<ServiceWorkerRegistrationInfo>& registrations) { |
265 std::vector<scoped_refptr<ServiceWorkerRegistration>> registration_values; | 353 std::vector<scoped_refptr<ServiceWorkerRegistration>> registration_values; |
266 for (const auto& registration : registrations) { | 354 for (const auto& registration : registrations) { |
267 registration_values.push_back( | 355 registration_values.push_back( |
268 CreateRegistrationDictionaryValue(registration)); | 356 CreateRegistrationDictionaryValue(registration)); |
269 } | 357 } |
270 client_->WorkerRegistrationUpdated( | 358 client_->WorkerRegistrationUpdated( |
271 WorkerRegistrationUpdatedParams::Create()->set_registrations( | 359 WorkerRegistrationUpdatedParams::Create()->set_registrations( |
272 registration_values)); | 360 registration_values)); |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
339 return; | 427 return; |
340 host->DetachClient(); | 428 host->DetachClient(); |
341 client_->WorkerTerminated(WorkerTerminatedParams::Create()-> | 429 client_->WorkerTerminated(WorkerTerminatedParams::Create()-> |
342 set_worker_id(host->GetId())); | 430 set_worker_id(host->GetId())); |
343 attached_hosts_.erase(it); | 431 attached_hosts_.erase(it); |
344 } | 432 } |
345 | 433 |
346 } // namespace service_worker | 434 } // namespace service_worker |
347 } // namespace devtools | 435 } // namespace devtools |
348 } // namespace content | 436 } // namespace content |
OLD | NEW |