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 "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
11 #include "content/browser/devtools/service_worker_devtools_agent_host.h" | 11 #include "content/browser/devtools/service_worker_devtools_agent_host.h" |
12 #include "content/browser/devtools/service_worker_devtools_manager.h" | 12 #include "content/browser/devtools/service_worker_devtools_manager.h" |
13 #include "content/browser/frame_host/frame_tree.h" | 13 #include "content/browser/frame_host/frame_tree.h" |
14 #include "content/browser/frame_host/frame_tree_node.h" | 14 #include "content/browser/frame_host/frame_tree_node.h" |
15 #include "content/browser/frame_host/render_frame_host_impl.h" | 15 #include "content/browser/frame_host/render_frame_host_impl.h" |
16 #include "content/browser/service_worker/service_worker_context_watcher.h" | 16 #include "content/browser/service_worker/service_worker_context_watcher.h" |
17 #include "content/browser/service_worker/service_worker_context_wrapper.h" | 17 #include "content/browser/service_worker/service_worker_context_wrapper.h" |
18 #include "content/browser/service_worker/service_worker_version.h" | 18 #include "content/browser/service_worker/service_worker_version.h" |
19 #include "content/public/browser/browser_context.h" | 19 #include "content/public/browser/browser_context.h" |
20 #include "content/public/browser/browser_thread.h" | 20 #include "content/public/browser/browser_thread.h" |
21 #include "content/public/browser/devtools_agent_host.h" | 21 #include "content/public/browser/devtools_agent_host.h" |
22 #include "content/public/browser/render_frame_host.h" | 22 #include "content/public/browser/render_frame_host.h" |
23 #include "content/public/browser/render_process_host.h" | 23 #include "content/public/browser/render_process_host.h" |
24 #include "content/public/browser/storage_partition.h" | 24 #include "content/public/browser/storage_partition.h" |
| 25 #include "content/public/browser/web_contents.h" |
25 #include "content/public/common/push_messaging_status.h" | 26 #include "content/public/common/push_messaging_status.h" |
26 #include "url/gurl.h" | 27 #include "url/gurl.h" |
27 | 28 |
28 // Windows headers will redefine SendMessage. | 29 // Windows headers will redefine SendMessage. |
29 #ifdef SendMessage | 30 #ifdef SendMessage |
30 #undef SendMessage | 31 #undef SendMessage |
31 #endif | 32 #endif |
32 | 33 |
33 namespace content { | 34 namespace content { |
34 namespace devtools { | 35 namespace devtools { |
(...skipping 15 matching lines...) Expand all Loading... |
50 switch (running_status) { | 51 switch (running_status) { |
51 case content::ServiceWorkerVersion::STOPPED: | 52 case content::ServiceWorkerVersion::STOPPED: |
52 return kServiceWorkerVersionRunningStatusStopped; | 53 return kServiceWorkerVersionRunningStatusStopped; |
53 case content::ServiceWorkerVersion::STARTING: | 54 case content::ServiceWorkerVersion::STARTING: |
54 return kServiceWorkerVersionRunningStatusStarting; | 55 return kServiceWorkerVersionRunningStatusStarting; |
55 case content::ServiceWorkerVersion::RUNNING: | 56 case content::ServiceWorkerVersion::RUNNING: |
56 return kServiceWorkerVersionRunningStatusRunning; | 57 return kServiceWorkerVersionRunningStatusRunning; |
57 case content::ServiceWorkerVersion::STOPPING: | 58 case content::ServiceWorkerVersion::STOPPING: |
58 return kServiceWorkerVersionRunningStatusStopping; | 59 return kServiceWorkerVersionRunningStatusStopping; |
59 } | 60 } |
60 return ""; | 61 return std::string(); |
61 } | 62 } |
62 | 63 |
63 const std::string GetVersionStatusString( | 64 const std::string GetVersionStatusString( |
64 content::ServiceWorkerVersion::Status status) { | 65 content::ServiceWorkerVersion::Status status) { |
65 switch (status) { | 66 switch (status) { |
66 case content::ServiceWorkerVersion::NEW: | 67 case content::ServiceWorkerVersion::NEW: |
67 return kServiceWorkerVersionStatusNew; | 68 return kServiceWorkerVersionStatusNew; |
68 case content::ServiceWorkerVersion::INSTALLING: | 69 case content::ServiceWorkerVersion::INSTALLING: |
69 return kServiceWorkerVersionStatusInstalling; | 70 return kServiceWorkerVersionStatusInstalling; |
70 case content::ServiceWorkerVersion::INSTALLED: | 71 case content::ServiceWorkerVersion::INSTALLED: |
71 return kServiceWorkerVersionStatusInstalled; | 72 return kServiceWorkerVersionStatusInstalled; |
72 case content::ServiceWorkerVersion::ACTIVATING: | 73 case content::ServiceWorkerVersion::ACTIVATING: |
73 return kServiceWorkerVersionStatusActivating; | 74 return kServiceWorkerVersionStatusActivating; |
74 case content::ServiceWorkerVersion::ACTIVATED: | 75 case content::ServiceWorkerVersion::ACTIVATED: |
75 return kServiceWorkerVersionStatusActivated; | 76 return kServiceWorkerVersionStatusActivated; |
76 case content::ServiceWorkerVersion::REDUNDANT: | 77 case content::ServiceWorkerVersion::REDUNDANT: |
77 return kServiceWorkerVersionStatusRedundant; | 78 return kServiceWorkerVersionStatusRedundant; |
78 } | 79 } |
79 return ""; | 80 return std::string(); |
80 } | 81 } |
81 | 82 |
82 scoped_refptr<ServiceWorkerVersion> CreateVersionDictionaryValue( | 83 scoped_refptr<ServiceWorkerVersion> CreateVersionDictionaryValue( |
83 const ServiceWorkerVersionInfo& version_info) { | 84 const ServiceWorkerVersionInfo& version_info) { |
| 85 std::vector<std::string> clients; |
| 86 for (const auto& client : version_info.clients) { |
| 87 if (client.second.type == SERVICE_WORKER_PROVIDER_FOR_WINDOW) { |
| 88 RenderFrameHostImpl* render_frame_host = RenderFrameHostImpl::FromID( |
| 89 client.second.process_id, client.second.route_id); |
| 90 WebContents* web_contents = |
| 91 WebContents::FromRenderFrameHost(render_frame_host); |
| 92 scoped_refptr<DevToolsAgentHost> agent_host( |
| 93 DevToolsAgentHost::GetOrCreateFor(web_contents)); |
| 94 if (agent_host) |
| 95 clients.push_back(agent_host->GetId()); |
| 96 } else if (client.second.type == |
| 97 SERVICE_WORKER_PROVIDER_FOR_SHARED_WORKER) { |
| 98 scoped_refptr<DevToolsAgentHost> agent_host( |
| 99 DevToolsAgentHost::GetForWorker(client.second.process_id, |
| 100 client.second.route_id)); |
| 101 if (agent_host) |
| 102 clients.push_back(agent_host->GetId()); |
| 103 } |
| 104 } |
84 scoped_refptr<ServiceWorkerVersion> version( | 105 scoped_refptr<ServiceWorkerVersion> version( |
85 ServiceWorkerVersion::Create() | 106 ServiceWorkerVersion::Create() |
86 ->set_version_id(base::Int64ToString(version_info.version_id)) | 107 ->set_version_id(base::Int64ToString(version_info.version_id)) |
87 ->set_registration_id( | 108 ->set_registration_id( |
88 base::Int64ToString(version_info.registration_id)) | 109 base::Int64ToString(version_info.registration_id)) |
89 ->set_script_url(version_info.script_url.spec()) | 110 ->set_script_url(version_info.script_url.spec()) |
90 ->set_running_status( | 111 ->set_running_status( |
91 GetVersionRunningStatusString(version_info.running_status)) | 112 GetVersionRunningStatusString(version_info.running_status)) |
92 ->set_status(GetVersionStatusString(version_info.status)) | 113 ->set_status(GetVersionStatusString(version_info.status)) |
93 ->set_script_last_modified( | 114 ->set_script_last_modified( |
94 version_info.script_last_modified.ToDoubleT()) | 115 version_info.script_last_modified.ToDoubleT()) |
95 ->set_script_response_time( | 116 ->set_script_response_time( |
96 version_info.script_response_time.ToDoubleT())); | 117 version_info.script_response_time.ToDoubleT()) |
| 118 ->set_controlled_clients(clients)); |
97 return version; | 119 return version; |
98 } | 120 } |
99 | 121 |
100 scoped_refptr<ServiceWorkerRegistration> CreateRegistrationDictionaryValue( | 122 scoped_refptr<ServiceWorkerRegistration> CreateRegistrationDictionaryValue( |
101 const ServiceWorkerRegistrationInfo& registration_info) { | 123 const ServiceWorkerRegistrationInfo& registration_info) { |
102 scoped_refptr<ServiceWorkerRegistration> registration( | 124 scoped_refptr<ServiceWorkerRegistration> registration( |
103 ServiceWorkerRegistration::Create() | 125 ServiceWorkerRegistration::Create() |
104 ->set_registration_id( | 126 ->set_registration_id( |
105 base::Int64ToString(registration_info.registration_id)) | 127 base::Int64ToString(registration_info.registration_id)) |
106 ->set_scope_url(registration_info.pattern.spec()) | 128 ->set_scope_url(registration_info.pattern.spec()) |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
174 } | 196 } |
175 | 197 |
176 Response CreateContextErrorResponse() { | 198 Response CreateContextErrorResponse() { |
177 return Response::InternalError("Could not connect to the context"); | 199 return Response::InternalError("Could not connect to the context"); |
178 } | 200 } |
179 | 201 |
180 Response CreateInvalidVersionIdErrorResponse() { | 202 Response CreateInvalidVersionIdErrorResponse() { |
181 return Response::InternalError("Invalid version ID"); | 203 return Response::InternalError("Invalid version ID"); |
182 } | 204 } |
183 | 205 |
| 206 const std::string GetDevToolsAgentHostTypeString( |
| 207 content::DevToolsAgentHost::Type type) { |
| 208 switch (type) { |
| 209 case DevToolsAgentHost::TYPE_WEB_CONTENTS: |
| 210 return "web_contents"; |
| 211 case DevToolsAgentHost::TYPE_FRAME: |
| 212 return "frame"; |
| 213 case DevToolsAgentHost::TYPE_SHARED_WORKER: |
| 214 return "shared_worker"; |
| 215 case DevToolsAgentHost::TYPE_SERVICE_WORKER: |
| 216 return "service_worker"; |
| 217 case DevToolsAgentHost::TYPE_EXTERNAL: |
| 218 return "external"; |
| 219 case DevToolsAgentHost::TYPE_BROWSER: |
| 220 return "browser"; |
| 221 } |
| 222 NOTREACHED() << type; |
| 223 return std::string(); |
| 224 } |
| 225 |
184 } // namespace | 226 } // namespace |
185 | 227 |
186 ServiceWorkerHandler::ServiceWorkerHandler() | 228 ServiceWorkerHandler::ServiceWorkerHandler() |
187 : enabled_(false), weak_factory_(this) { | 229 : enabled_(false), weak_factory_(this) { |
188 } | 230 } |
189 | 231 |
190 ServiceWorkerHandler::~ServiceWorkerHandler() { | 232 ServiceWorkerHandler::~ServiceWorkerHandler() { |
191 Disable(); | 233 Disable(); |
192 } | 234 } |
193 | 235 |
(...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
390 return CreateContextErrorResponse(); | 432 return CreateContextErrorResponse(); |
391 int64 id = 0; | 433 int64 id = 0; |
392 if (!base::StringToInt64(registration_id, &id)) | 434 if (!base::StringToInt64(registration_id, &id)) |
393 return CreateInvalidVersionIdErrorResponse(); | 435 return CreateInvalidVersionIdErrorResponse(); |
394 BrowserContext::DeliverPushMessage( | 436 BrowserContext::DeliverPushMessage( |
395 render_frame_host_->GetProcess()->GetBrowserContext(), GURL(origin), id, | 437 render_frame_host_->GetProcess()->GetBrowserContext(), GURL(origin), id, |
396 data, base::Bind(&PushDeliveryNoOp)); | 438 data, base::Bind(&PushDeliveryNoOp)); |
397 return Response::OK(); | 439 return Response::OK(); |
398 } | 440 } |
399 | 441 |
| 442 // TODO(horo): I will remove it in crrev.com/1143363009. |
400 Response ServiceWorkerHandler::GetTargetInfo(DevToolsCommandId command_id, | 443 Response ServiceWorkerHandler::GetTargetInfo(DevToolsCommandId command_id, |
401 const std::string& target_id) { | 444 const std::string& target_id) { |
402 return Response::InternalError("Not implemented yet"); | 445 return Response::InternalError("Not implemented yet"); |
403 } | 446 } |
404 | 447 |
| 448 Response ServiceWorkerHandler::GetTargetInfo( |
| 449 const std::string& target_id, |
| 450 scoped_refptr<TargetInfo>* target_info) { |
| 451 scoped_refptr<DevToolsAgentHost> agent_host( |
| 452 DevToolsAgentHost::GetForId(target_id)); |
| 453 if (!agent_host) |
| 454 return Response::InvalidParams("targetId"); |
| 455 *target_info = |
| 456 TargetInfo::Create() |
| 457 ->set_id(agent_host->GetId()) |
| 458 ->set_type(GetDevToolsAgentHostTypeString(agent_host->GetType())) |
| 459 ->set_title(agent_host->GetTitle()) |
| 460 ->set_url(agent_host->GetURL().spec()); |
| 461 return Response::OK(); |
| 462 } |
| 463 |
405 Response ServiceWorkerHandler::ActivateTarget(const std::string& target_id) { | 464 Response ServiceWorkerHandler::ActivateTarget(const std::string& target_id) { |
406 scoped_refptr<DevToolsAgentHost> agent_host( | 465 scoped_refptr<DevToolsAgentHost> agent_host( |
407 DevToolsAgentHost::GetForId(target_id)); | 466 DevToolsAgentHost::GetForId(target_id)); |
408 if (!agent_host) | 467 if (!agent_host) |
409 return Response::InvalidParams("targetId"); | 468 return Response::InvalidParams("targetId"); |
410 agent_host->Activate(); | 469 agent_host->Activate(); |
411 return Response::OK(); | 470 return Response::OK(); |
412 } | 471 } |
413 | 472 |
414 void ServiceWorkerHandler::OpenNewDevToolsWindow(int process_id, | 473 void ServiceWorkerHandler::OpenNewDevToolsWindow(int process_id, |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
531 return; | 590 return; |
532 host->DetachClient(); | 591 host->DetachClient(); |
533 client_->WorkerTerminated(WorkerTerminatedParams::Create()-> | 592 client_->WorkerTerminated(WorkerTerminatedParams::Create()-> |
534 set_worker_id(host->GetId())); | 593 set_worker_id(host->GetId())); |
535 attached_hosts_.erase(it); | 594 attached_hosts_.erase(it); |
536 } | 595 } |
537 | 596 |
538 } // namespace service_worker | 597 } // namespace service_worker |
539 } // namespace devtools | 598 } // namespace devtools |
540 } // namespace content | 599 } // namespace content |
OLD | NEW |