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