| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/worker_devtools_agent_host.h" | 5 #include "content/browser/devtools/worker_devtools_agent_host.h" |
| 6 | 6 |
| 7 #include "base/guid.h" | 7 #include "base/guid.h" |
| 8 #include "base/json/json_reader.h" | |
| 9 #include "content/browser/devtools/devtools_protocol_handler.h" | |
| 10 #include "content/browser/devtools/devtools_session.h" | 8 #include "content/browser/devtools/devtools_session.h" |
| 9 #include "content/browser/devtools/protocol/inspector_handler.h" |
| 10 #include "content/browser/devtools/protocol/network_handler.h" |
| 11 #include "content/browser/devtools/protocol/protocol.h" |
| 11 #include "content/browser/devtools/protocol/schema_handler.h" | 12 #include "content/browser/devtools/protocol/schema_handler.h" |
| 12 #include "content/public/browser/browser_thread.h" | 13 #include "content/public/browser/browser_thread.h" |
| 13 #include "content/public/browser/render_process_host.h" | 14 #include "content/public/browser/render_process_host.h" |
| 14 | 15 |
| 15 namespace content { | 16 namespace content { |
| 16 | 17 |
| 17 BrowserContext* WorkerDevToolsAgentHost::GetBrowserContext() { | 18 BrowserContext* WorkerDevToolsAgentHost::GetBrowserContext() { |
| 18 RenderProcessHost* rph = RenderProcessHost::FromID(worker_id_.first); | 19 RenderProcessHost* rph = RenderProcessHost::FromID(worker_id_.first); |
| 19 return rph ? rph->GetBrowserContext() : nullptr; | 20 return rph ? rph->GetBrowserContext() : nullptr; |
| 20 } | 21 } |
| 21 | 22 |
| 22 void WorkerDevToolsAgentHost::Attach() { | 23 void WorkerDevToolsAgentHost::Attach() { |
| 23 if (state_ != WORKER_INSPECTED) { | 24 if (state_ != WORKER_INSPECTED) { |
| 24 state_ = WORKER_INSPECTED; | 25 state_ = WORKER_INSPECTED; |
| 25 AttachToWorker(); | 26 AttachToWorker(); |
| 26 } | 27 } |
| 27 if (RenderProcessHost* host = RenderProcessHost::FromID(worker_id_.first)) { | 28 if (RenderProcessHost* host = RenderProcessHost::FromID(worker_id_.first)) { |
| 28 host->Send(new DevToolsAgentMsg_Attach( | 29 host->Send(new DevToolsAgentMsg_Attach( |
| 29 worker_id_.second, GetId(), session()->session_id())); | 30 worker_id_.second, GetId(), session()->session_id())); |
| 30 } | 31 } |
| 32 inspector_handler_.reset(new protocol::InspectorHandler()); |
| 33 inspector_handler_->Wire(session()->dispatcher()); |
| 34 network_handler_.reset(new protocol::NetworkHandler()); |
| 35 network_handler_->Wire(session()->dispatcher()); |
| 36 schema_handler_.reset(new protocol::SchemaHandler()); |
| 37 schema_handler_->Wire(session()->dispatcher()); |
| 38 session()->dispatcher()->setFallThroughForNotFound(true); |
| 31 OnAttachedStateChanged(true); | 39 OnAttachedStateChanged(true); |
| 32 } | 40 } |
| 33 | 41 |
| 34 void WorkerDevToolsAgentHost::Detach() { | 42 void WorkerDevToolsAgentHost::Detach() { |
| 35 if (RenderProcessHost* host = RenderProcessHost::FromID(worker_id_.first)) | 43 if (RenderProcessHost* host = RenderProcessHost::FromID(worker_id_.first)) |
| 36 host->Send(new DevToolsAgentMsg_Detach(worker_id_.second)); | 44 host->Send(new DevToolsAgentMsg_Detach(worker_id_.second)); |
| 45 inspector_handler_->Disable(); |
| 46 inspector_handler_.reset(); |
| 47 network_handler_->Disable(); |
| 48 network_handler_.reset(); |
| 49 schema_handler_->Disable(); |
| 50 schema_handler_.reset(); |
| 37 OnAttachedStateChanged(false); | 51 OnAttachedStateChanged(false); |
| 38 if (state_ == WORKER_INSPECTED) { | 52 if (state_ == WORKER_INSPECTED) { |
| 39 state_ = WORKER_UNINSPECTED; | 53 state_ = WORKER_UNINSPECTED; |
| 40 DetachFromWorker(); | 54 DetachFromWorker(); |
| 41 } else if (state_ == WORKER_PAUSED_FOR_REATTACH) { | 55 } else if (state_ == WORKER_PAUSED_FOR_REATTACH) { |
| 42 state_ = WORKER_UNINSPECTED; | 56 state_ = WORKER_UNINSPECTED; |
| 43 } | 57 } |
| 44 } | 58 } |
| 45 | 59 |
| 46 bool WorkerDevToolsAgentHost::DispatchProtocolMessage( | 60 bool WorkerDevToolsAgentHost::DispatchProtocolMessage( |
| 47 const std::string& message) { | 61 const std::string& message) { |
| 48 if (state_ != WORKER_INSPECTED) | 62 if (state_ != WORKER_INSPECTED) |
| 49 return true; | 63 return true; |
| 50 | 64 |
| 51 std::unique_ptr<base::Value> value = base::JSONReader::Read(message); | 65 int call_id = 0; |
| 52 int call_id; | |
| 53 std::string method; | 66 std::string method; |
| 54 if (protocol_handler_->HandleOptionalMessage( | 67 if (session()->Dispatch(message, &call_id, &method) != |
| 55 session()->session_id(), std::move(value), &call_id, &method)) { | 68 protocol::Response::kFallThrough) { |
| 56 return true; | 69 return true; |
| 57 } | 70 } |
| 58 | 71 |
| 59 if (RenderProcessHost* host = RenderProcessHost::FromID(worker_id_.first)) { | 72 if (RenderProcessHost* host = RenderProcessHost::FromID(worker_id_.first)) { |
| 60 host->Send(new DevToolsAgentMsg_DispatchOnInspectorBackend( | 73 host->Send(new DevToolsAgentMsg_DispatchOnInspectorBackend( |
| 61 worker_id_.second, session()->session_id(), call_id, method, message)); | 74 worker_id_.second, session()->session_id(), call_id, method, message)); |
| 62 } | 75 } |
| 63 return true; | 76 return true; |
| 64 } | 77 } |
| 65 | 78 |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 102 DCHECK_EQ(WORKER_TERMINATED, state_); | 115 DCHECK_EQ(WORKER_TERMINATED, state_); |
| 103 state_ = IsAttached() ? WORKER_PAUSED_FOR_REATTACH : WORKER_UNINSPECTED; | 116 state_ = IsAttached() ? WORKER_PAUSED_FOR_REATTACH : WORKER_UNINSPECTED; |
| 104 worker_id_ = worker_id; | 117 worker_id_ = worker_id; |
| 105 WorkerCreated(); | 118 WorkerCreated(); |
| 106 } | 119 } |
| 107 | 120 |
| 108 void WorkerDevToolsAgentHost::WorkerDestroyed() { | 121 void WorkerDevToolsAgentHost::WorkerDestroyed() { |
| 109 DCHECK_NE(WORKER_TERMINATED, state_); | 122 DCHECK_NE(WORKER_TERMINATED, state_); |
| 110 if (state_ == WORKER_INSPECTED) { | 123 if (state_ == WORKER_INSPECTED) { |
| 111 DCHECK(IsAttached()); | 124 DCHECK(IsAttached()); |
| 112 // Client host is debugging this worker agent host. | 125 inspector_handler_->TargetCrashed(); |
| 113 devtools::inspector::Client inspector(this); | |
| 114 inspector.TargetCrashed( | |
| 115 devtools::inspector::TargetCrashedParams::Create()); | |
| 116 DetachFromWorker(); | 126 DetachFromWorker(); |
| 117 } | 127 } |
| 118 state_ = WORKER_TERMINATED; | 128 state_ = WORKER_TERMINATED; |
| 119 Release(); // Balanced in WorkerCreated(). | 129 Release(); // Balanced in WorkerCreated(). |
| 120 } | 130 } |
| 121 | 131 |
| 122 bool WorkerDevToolsAgentHost::IsTerminated() { | 132 bool WorkerDevToolsAgentHost::IsTerminated() { |
| 123 return state_ == WORKER_TERMINATED; | 133 return state_ == WORKER_TERMINATED; |
| 124 } | 134 } |
| 125 | 135 |
| 126 WorkerDevToolsAgentHost::WorkerDevToolsAgentHost(WorkerId worker_id) | 136 WorkerDevToolsAgentHost::WorkerDevToolsAgentHost(WorkerId worker_id) |
| 127 : DevToolsAgentHostImpl(base::GenerateGUID()), | 137 : DevToolsAgentHostImpl(base::GenerateGUID()), |
| 128 schema_handler_(new devtools::schema::SchemaHandler()), | |
| 129 protocol_handler_(new DevToolsProtocolHandler(this)), | |
| 130 chunk_processor_(base::Bind(&WorkerDevToolsAgentHost::SendMessageToClient, | 138 chunk_processor_(base::Bind(&WorkerDevToolsAgentHost::SendMessageToClient, |
| 131 base::Unretained(this))), | 139 base::Unretained(this))), |
| 132 state_(WORKER_UNINSPECTED), | 140 state_(WORKER_UNINSPECTED), |
| 133 worker_id_(worker_id) { | 141 worker_id_(worker_id) { |
| 134 protocol_handler_->dispatcher()->SetSchemaHandler(schema_handler_.get()); | |
| 135 WorkerCreated(); | 142 WorkerCreated(); |
| 136 } | 143 } |
| 137 | 144 |
| 138 WorkerDevToolsAgentHost::~WorkerDevToolsAgentHost() { | 145 WorkerDevToolsAgentHost::~WorkerDevToolsAgentHost() { |
| 139 DCHECK_EQ(WORKER_TERMINATED, state_); | 146 DCHECK_EQ(WORKER_TERMINATED, state_); |
| 140 } | 147 } |
| 141 | 148 |
| 142 void WorkerDevToolsAgentHost::OnAttachedStateChanged(bool attached) { | 149 void WorkerDevToolsAgentHost::OnAttachedStateChanged(bool attached) { |
| 143 } | 150 } |
| 144 | 151 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 158 | 165 |
| 159 void WorkerDevToolsAgentHost::OnDispatchOnInspectorFrontend( | 166 void WorkerDevToolsAgentHost::OnDispatchOnInspectorFrontend( |
| 160 const DevToolsMessageChunk& message) { | 167 const DevToolsMessageChunk& message) { |
| 161 if (!IsAttached()) | 168 if (!IsAttached()) |
| 162 return; | 169 return; |
| 163 | 170 |
| 164 chunk_processor_.ProcessChunkedMessageFromAgent(message); | 171 chunk_processor_.ProcessChunkedMessageFromAgent(message); |
| 165 } | 172 } |
| 166 | 173 |
| 167 } // namespace content | 174 } // namespace content |
| OLD | NEW |