| 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 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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 ""; |
| 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 int render_process_id = client.second.process_id; |
| 88 int render_frame_id = client.second.frame_id; |
| 89 int shared_worker_route_id = client.second.shared_worker_route_id; |
| 90 if (render_frame_id != MSG_ROUTING_NONE) { |
| 91 RenderFrameHostImpl* render_frame_host = |
| 92 RenderFrameHostImpl::FromID(render_process_id, render_frame_id); |
| 93 WebContents* web_contents = |
| 94 WebContents::FromRenderFrameHost(render_frame_host); |
| 95 scoped_refptr<DevToolsAgentHost> agent_host( |
| 96 DevToolsAgentHost::GetOrCreateFor(web_contents)); |
| 97 if (agent_host) |
| 98 clients.push_back(agent_host->GetId()); |
| 99 } else if (shared_worker_route_id != MSG_ROUTING_NONE) { |
| 100 scoped_refptr<DevToolsAgentHost> agent_host( |
| 101 DevToolsAgentHost::GetForWorker(render_process_id, |
| 102 shared_worker_route_id)); |
| 103 if (agent_host) |
| 104 clients.push_back(agent_host->GetId()); |
| 105 } |
| 106 } |
| 84 scoped_refptr<ServiceWorkerVersion> version( | 107 scoped_refptr<ServiceWorkerVersion> version( |
| 85 ServiceWorkerVersion::Create() | 108 ServiceWorkerVersion::Create() |
| 86 ->set_version_id(base::Int64ToString(version_info.version_id)) | 109 ->set_version_id(base::Int64ToString(version_info.version_id)) |
| 87 ->set_registration_id( | 110 ->set_registration_id( |
| 88 base::Int64ToString(version_info.registration_id)) | 111 base::Int64ToString(version_info.registration_id)) |
| 89 ->set_script_url(version_info.script_url.spec()) | 112 ->set_script_url(version_info.script_url.spec()) |
| 90 ->set_running_status( | 113 ->set_running_status( |
| 91 GetVersionRunningStatusString(version_info.running_status)) | 114 GetVersionRunningStatusString(version_info.running_status)) |
| 92 ->set_status(GetVersionStatusString(version_info.status)) | 115 ->set_status(GetVersionStatusString(version_info.status)) |
| 93 ->set_script_last_modified( | 116 ->set_script_last_modified( |
| 94 version_info.script_last_modified.ToDoubleT()) | 117 version_info.script_last_modified.ToDoubleT()) |
| 95 ->set_script_response_time( | 118 ->set_script_response_time( |
| 96 version_info.script_response_time.ToDoubleT())); | 119 version_info.script_response_time.ToDoubleT()) |
| 120 ->set_controlled_clients(clients)); |
| 97 return version; | 121 return version; |
| 98 } | 122 } |
| 99 | 123 |
| 100 scoped_refptr<ServiceWorkerRegistration> CreateRegistrationDictionaryValue( | 124 scoped_refptr<ServiceWorkerRegistration> CreateRegistrationDictionaryValue( |
| 101 const ServiceWorkerRegistrationInfo& registration_info) { | 125 const ServiceWorkerRegistrationInfo& registration_info) { |
| 102 scoped_refptr<ServiceWorkerRegistration> registration( | 126 scoped_refptr<ServiceWorkerRegistration> registration( |
| 103 ServiceWorkerRegistration::Create() | 127 ServiceWorkerRegistration::Create() |
| 104 ->set_registration_id( | 128 ->set_registration_id( |
| 105 base::Int64ToString(registration_info.registration_id)) | 129 base::Int64ToString(registration_info.registration_id)) |
| 106 ->set_scope_url(registration_info.pattern.spec()) | 130 ->set_scope_url(registration_info.pattern.spec()) |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 174 } | 198 } |
| 175 | 199 |
| 176 Response CreateContextErrorResponse() { | 200 Response CreateContextErrorResponse() { |
| 177 return Response::InternalError("Could not connect to the context"); | 201 return Response::InternalError("Could not connect to the context"); |
| 178 } | 202 } |
| 179 | 203 |
| 180 Response CreateInvalidVersionIdErrorResponse() { | 204 Response CreateInvalidVersionIdErrorResponse() { |
| 181 return Response::InternalError("Invalid version ID"); | 205 return Response::InternalError("Invalid version ID"); |
| 182 } | 206 } |
| 183 | 207 |
| 208 const std::string GetDevToolsAgentHostTypeString( |
| 209 content::DevToolsAgentHost::Type type) { |
| 210 switch (type) { |
| 211 case DevToolsAgentHost::TYPE_WEB_CONTENTS: |
| 212 return "web_contents"; |
| 213 case DevToolsAgentHost::TYPE_FRAME: |
| 214 return "frame"; |
| 215 case DevToolsAgentHost::TYPE_SHARED_WORKER: |
| 216 return "shared_worker"; |
| 217 case DevToolsAgentHost::TYPE_SERVICE_WORKER: |
| 218 return "service_worker"; |
| 219 case DevToolsAgentHost::TYPE_EXTERNAL: |
| 220 return "external"; |
| 221 case DevToolsAgentHost::TYPE_BROWSER: |
| 222 return "browser"; |
| 223 default: |
| 224 NOTREACHED() << type; |
| 225 } |
| 226 return ""; |
| 227 } |
| 228 |
| 184 } // namespace | 229 } // namespace |
| 185 | 230 |
| 186 ServiceWorkerHandler::ServiceWorkerHandler() | 231 ServiceWorkerHandler::ServiceWorkerHandler() |
| 187 : enabled_(false), weak_factory_(this) { | 232 : enabled_(false), weak_factory_(this) { |
| 188 } | 233 } |
| 189 | 234 |
| 190 ServiceWorkerHandler::~ServiceWorkerHandler() { | 235 ServiceWorkerHandler::~ServiceWorkerHandler() { |
| 191 Disable(); | 236 Disable(); |
| 192 } | 237 } |
| 193 | 238 |
| (...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 390 return CreateContextErrorResponse(); | 435 return CreateContextErrorResponse(); |
| 391 int64 id = 0; | 436 int64 id = 0; |
| 392 if (!base::StringToInt64(registration_id, &id)) | 437 if (!base::StringToInt64(registration_id, &id)) |
| 393 return CreateInvalidVersionIdErrorResponse(); | 438 return CreateInvalidVersionIdErrorResponse(); |
| 394 BrowserContext::DeliverPushMessage( | 439 BrowserContext::DeliverPushMessage( |
| 395 render_frame_host_->GetProcess()->GetBrowserContext(), GURL(origin), id, | 440 render_frame_host_->GetProcess()->GetBrowserContext(), GURL(origin), id, |
| 396 data, base::Bind(&PushDeliveryNoOp)); | 441 data, base::Bind(&PushDeliveryNoOp)); |
| 397 return Response::OK(); | 442 return Response::OK(); |
| 398 } | 443 } |
| 399 | 444 |
| 445 // TODO(horo): I will remove it in crrev.com/1143363009. |
| 400 Response ServiceWorkerHandler::GetTargetInfo(DevToolsCommandId command_id, | 446 Response ServiceWorkerHandler::GetTargetInfo(DevToolsCommandId command_id, |
| 401 const std::string& target_id) { | 447 const std::string& target_id) { |
| 402 return Response::InternalError("Not implemented yet"); | 448 return Response::InternalError("Not implemented yet"); |
| 403 } | 449 } |
| 404 | 450 |
| 451 Response ServiceWorkerHandler::GetTargetInfo( |
| 452 const std::string& target_id, |
| 453 scoped_refptr<TargetInfo>* target_info) { |
| 454 scoped_refptr<DevToolsAgentHost> agent_host( |
| 455 DevToolsAgentHost::GetForId(target_id)); |
| 456 if (!agent_host) |
| 457 return Response::InvalidParams("targetId"); |
| 458 *target_info = |
| 459 TargetInfo::Create() |
| 460 ->set_id(agent_host->GetId()) |
| 461 ->set_type(GetDevToolsAgentHostTypeString(agent_host->GetType())) |
| 462 ->set_title(agent_host->GetTitle()) |
| 463 ->set_url(agent_host->GetURL().spec()); |
| 464 return Response::OK(); |
| 465 } |
| 466 |
| 405 Response ServiceWorkerHandler::ActivateTarget(const std::string& target_id) { | 467 Response ServiceWorkerHandler::ActivateTarget(const std::string& target_id) { |
| 406 scoped_refptr<DevToolsAgentHost> agent_host( | 468 scoped_refptr<DevToolsAgentHost> agent_host( |
| 407 DevToolsAgentHost::GetForId(target_id)); | 469 DevToolsAgentHost::GetForId(target_id)); |
| 408 if (!agent_host) | 470 if (!agent_host) |
| 409 return Response::InvalidParams("targetId"); | 471 return Response::InvalidParams("targetId"); |
| 410 agent_host->Activate(); | 472 agent_host->Activate(); |
| 411 return Response::OK(); | 473 return Response::OK(); |
| 412 } | 474 } |
| 413 | 475 |
| 414 void ServiceWorkerHandler::OpenNewDevToolsWindow(int process_id, | 476 void ServiceWorkerHandler::OpenNewDevToolsWindow(int process_id, |
| (...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 531 return; | 593 return; |
| 532 host->DetachClient(); | 594 host->DetachClient(); |
| 533 client_->WorkerTerminated(WorkerTerminatedParams::Create()-> | 595 client_->WorkerTerminated(WorkerTerminatedParams::Create()-> |
| 534 set_worker_id(host->GetId())); | 596 set_worker_id(host->GetId())); |
| 535 attached_hosts_.erase(it); | 597 attached_hosts_.erase(it); |
| 536 } | 598 } |
| 537 | 599 |
| 538 } // namespace service_worker | 600 } // namespace service_worker |
| 539 } // namespace devtools | 601 } // namespace devtools |
| 540 } // namespace content | 602 } // namespace content |
| OLD | NEW |