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/embedded_worker_devtools_agent_host.h" | 5 #include "content/browser/devtools/worker_devtools_agent_host.h" |
6 | 6 |
7 #include "content/browser/devtools/protocol/devtools_protocol_handler.h" | 7 #include "content/browser/devtools/protocol/devtools_protocol_handler.h" |
8 #include "content/common/devtools_messages.h" | 8 #include "content/common/devtools_messages.h" |
9 #include "content/public/browser/browser_thread.h" | 9 #include "content/public/browser/browser_thread.h" |
10 #include "content/public/browser/render_process_host.h" | 10 #include "content/public/browser/render_process_host.h" |
11 | 11 |
12 namespace content { | 12 namespace content { |
13 | 13 |
14 bool EmbeddedWorkerDevToolsAgentHost::IsWorker() const { | 14 bool WorkerDevToolsAgentHost::IsWorker() const { |
15 return true; | 15 return true; |
16 } | 16 } |
17 | 17 |
18 BrowserContext* EmbeddedWorkerDevToolsAgentHost::GetBrowserContext() { | 18 BrowserContext* WorkerDevToolsAgentHost::GetBrowserContext() { |
19 RenderProcessHost* rph = RenderProcessHost::FromID(worker_id_.first); | 19 RenderProcessHost* rph = RenderProcessHost::FromID(worker_id_.first); |
20 return rph ? rph->GetBrowserContext() : nullptr; | 20 return rph ? rph->GetBrowserContext() : nullptr; |
21 } | 21 } |
22 | 22 |
23 void EmbeddedWorkerDevToolsAgentHost::SendMessageToAgent( | 23 void WorkerDevToolsAgentHost::SendMessageToAgent( |
24 IPC::Message* message_raw) { | 24 IPC::Message* message_raw) { |
25 scoped_ptr<IPC::Message> message(message_raw); | 25 scoped_ptr<IPC::Message> message(message_raw); |
26 if (state_ != WORKER_INSPECTED) | 26 if (state_ != WORKER_INSPECTED) |
27 return; | 27 return; |
28 if (RenderProcessHost* host = RenderProcessHost::FromID(worker_id_.first)) { | 28 if (RenderProcessHost* host = RenderProcessHost::FromID(worker_id_.first)) { |
29 message->set_routing_id(worker_id_.second); | 29 message->set_routing_id(worker_id_.second); |
30 host->Send(message.release()); | 30 host->Send(message.release()); |
31 } | 31 } |
32 } | 32 } |
33 | 33 |
34 void EmbeddedWorkerDevToolsAgentHost::Attach() { | 34 void WorkerDevToolsAgentHost::Attach() { |
35 if (state_ != WORKER_INSPECTED) { | 35 if (state_ != WORKER_INSPECTED) { |
36 state_ = WORKER_INSPECTED; | 36 state_ = WORKER_INSPECTED; |
37 AttachToWorker(); | 37 AttachToWorker(); |
38 } | 38 } |
39 IPCDevToolsAgentHost::Attach(); | 39 IPCDevToolsAgentHost::Attach(); |
40 } | 40 } |
41 | 41 |
42 void EmbeddedWorkerDevToolsAgentHost::OnClientAttached() { | 42 void WorkerDevToolsAgentHost::OnClientAttached() { |
43 DevToolsAgentHostImpl::NotifyCallbacks(this, true); | 43 DevToolsAgentHostImpl::NotifyCallbacks(this, true); |
44 } | 44 } |
45 | 45 |
46 void EmbeddedWorkerDevToolsAgentHost::OnClientDetached() { | 46 void WorkerDevToolsAgentHost::OnClientDetached() { |
47 if (state_ == WORKER_INSPECTED) { | 47 if (state_ == WORKER_INSPECTED) { |
48 state_ = WORKER_UNINSPECTED; | 48 state_ = WORKER_UNINSPECTED; |
49 DetachFromWorker(); | 49 DetachFromWorker(); |
50 } else if (state_ == WORKER_PAUSED_FOR_REATTACH) { | 50 } else if (state_ == WORKER_PAUSED_FOR_REATTACH) { |
51 state_ = WORKER_UNINSPECTED; | 51 state_ = WORKER_UNINSPECTED; |
52 } | 52 } |
53 DevToolsAgentHostImpl::NotifyCallbacks(this, false); | 53 DevToolsAgentHostImpl::NotifyCallbacks(this, false); |
54 } | 54 } |
55 | 55 |
56 bool EmbeddedWorkerDevToolsAgentHost::OnMessageReceived( | 56 bool WorkerDevToolsAgentHost::OnMessageReceived( |
57 const IPC::Message& msg) { | 57 const IPC::Message& msg) { |
58 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 58 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
59 bool handled = true; | 59 bool handled = true; |
60 IPC_BEGIN_MESSAGE_MAP(EmbeddedWorkerDevToolsAgentHost, msg) | 60 IPC_BEGIN_MESSAGE_MAP(WorkerDevToolsAgentHost, msg) |
61 IPC_MESSAGE_HANDLER(DevToolsClientMsg_DispatchOnInspectorFrontend, | 61 IPC_MESSAGE_HANDLER(DevToolsClientMsg_DispatchOnInspectorFrontend, |
62 OnDispatchOnInspectorFrontend) | 62 OnDispatchOnInspectorFrontend) |
63 IPC_MESSAGE_HANDLER(DevToolsHostMsg_SaveAgentRuntimeState, | 63 IPC_MESSAGE_HANDLER(DevToolsHostMsg_SaveAgentRuntimeState, |
64 OnSaveAgentRuntimeState) | 64 OnSaveAgentRuntimeState) |
65 IPC_MESSAGE_UNHANDLED(handled = false) | 65 IPC_MESSAGE_UNHANDLED(handled = false) |
66 IPC_END_MESSAGE_MAP() | 66 IPC_END_MESSAGE_MAP() |
67 return handled; | 67 return handled; |
68 } | 68 } |
69 | 69 |
70 void EmbeddedWorkerDevToolsAgentHost::WorkerReadyForInspection() { | 70 void WorkerDevToolsAgentHost::WorkerReadyForInspection() { |
71 if (state_ == WORKER_PAUSED_FOR_DEBUG_ON_START) { | 71 if (state_ == WORKER_PAUSED_FOR_DEBUG_ON_START) { |
72 RenderProcessHost* rph = RenderProcessHost::FromID(worker_id_.first); | 72 RenderProcessHost* rph = RenderProcessHost::FromID(worker_id_.first); |
73 Inspect(rph->GetBrowserContext()); | 73 Inspect(rph->GetBrowserContext()); |
74 } else if (state_ == WORKER_PAUSED_FOR_REATTACH) { | 74 } else if (state_ == WORKER_PAUSED_FOR_REATTACH) { |
75 DCHECK(IsAttached()); | 75 DCHECK(IsAttached()); |
76 state_ = WORKER_INSPECTED; | 76 state_ = WORKER_INSPECTED; |
77 AttachToWorker(); | 77 AttachToWorker(); |
78 Reattach(saved_agent_state_); | 78 Reattach(saved_agent_state_); |
79 } | 79 } |
80 } | 80 } |
81 | 81 |
82 void EmbeddedWorkerDevToolsAgentHost::WorkerRestarted(WorkerId worker_id) { | 82 void WorkerDevToolsAgentHost::WorkerRestarted(WorkerId worker_id) { |
83 DCHECK_EQ(WORKER_TERMINATED, state_); | 83 DCHECK_EQ(WORKER_TERMINATED, state_); |
84 state_ = IsAttached() ? WORKER_PAUSED_FOR_REATTACH : WORKER_UNINSPECTED; | 84 state_ = IsAttached() ? WORKER_PAUSED_FOR_REATTACH : WORKER_UNINSPECTED; |
85 worker_id_ = worker_id; | 85 worker_id_ = worker_id; |
86 WorkerCreated(); | 86 WorkerCreated(); |
87 } | 87 } |
88 | 88 |
89 void EmbeddedWorkerDevToolsAgentHost::WorkerDestroyed() { | 89 void WorkerDevToolsAgentHost::WorkerDestroyed() { |
90 DCHECK_NE(WORKER_TERMINATED, state_); | 90 DCHECK_NE(WORKER_TERMINATED, state_); |
91 if (state_ == WORKER_INSPECTED) { | 91 if (state_ == WORKER_INSPECTED) { |
92 DCHECK(IsAttached()); | 92 DCHECK(IsAttached()); |
93 // Client host is debugging this worker agent host. | 93 // Client host is debugging this worker agent host. |
94 base::Callback<void(const std::string&)> raw_message_callback( | 94 base::Callback<void(const std::string&)> raw_message_callback( |
95 base::Bind(&EmbeddedWorkerDevToolsAgentHost::SendMessageToClient, | 95 base::Bind(&WorkerDevToolsAgentHost::SendMessageToClient, |
96 base::Unretained(this))); | 96 base::Unretained(this))); |
97 devtools::worker::Client worker(raw_message_callback); | 97 devtools::worker::Client worker(raw_message_callback); |
98 worker.DisconnectedFromWorker( | 98 worker.DisconnectedFromWorker( |
99 devtools::worker::DisconnectedFromWorkerParams::Create()); | 99 devtools::worker::DisconnectedFromWorkerParams::Create()); |
100 DetachFromWorker(); | 100 DetachFromWorker(); |
101 } | 101 } |
102 state_ = WORKER_TERMINATED; | 102 state_ = WORKER_TERMINATED; |
103 Release(); // Balanced in WorkerCreated(). | 103 Release(); // Balanced in WorkerCreated(). |
104 } | 104 } |
105 | 105 |
106 bool EmbeddedWorkerDevToolsAgentHost::IsTerminated() { | 106 bool WorkerDevToolsAgentHost::IsTerminated() { |
107 return state_ == WORKER_TERMINATED; | 107 return state_ == WORKER_TERMINATED; |
108 } | 108 } |
109 | 109 |
110 bool EmbeddedWorkerDevToolsAgentHost::Matches( | 110 WorkerDevToolsAgentHost::WorkerDevToolsAgentHost( |
111 const SharedWorkerInstance& other) { | |
112 return false; | |
113 } | |
114 | |
115 bool EmbeddedWorkerDevToolsAgentHost::Matches( | |
116 const ServiceWorkerIdentifier& other) { | |
117 return false; | |
118 } | |
119 | |
120 EmbeddedWorkerDevToolsAgentHost::EmbeddedWorkerDevToolsAgentHost( | |
121 WorkerId worker_id) | 111 WorkerId worker_id) |
122 : state_(WORKER_UNINSPECTED), | 112 : state_(WORKER_UNINSPECTED), |
123 worker_id_(worker_id) { | 113 worker_id_(worker_id) { |
124 WorkerCreated(); | 114 WorkerCreated(); |
125 } | 115 } |
126 | 116 |
127 EmbeddedWorkerDevToolsAgentHost::~EmbeddedWorkerDevToolsAgentHost() { | 117 WorkerDevToolsAgentHost::~WorkerDevToolsAgentHost() { |
128 DCHECK_EQ(WORKER_TERMINATED, state_); | 118 DCHECK_EQ(WORKER_TERMINATED, state_); |
129 EmbeddedWorkerDevToolsManager::GetInstance()->RemoveInspectedWorkerData( | |
130 worker_id_); | |
131 } | 119 } |
132 | 120 |
133 void EmbeddedWorkerDevToolsAgentHost::AttachToWorker() { | 121 void WorkerDevToolsAgentHost::AttachToWorker() { |
134 if (RenderProcessHost* host = RenderProcessHost::FromID(worker_id_.first)) | 122 if (RenderProcessHost* host = RenderProcessHost::FromID(worker_id_.first)) |
135 host->AddRoute(worker_id_.second, this); | 123 host->AddRoute(worker_id_.second, this); |
136 } | 124 } |
137 | 125 |
138 void EmbeddedWorkerDevToolsAgentHost::DetachFromWorker() { | 126 void WorkerDevToolsAgentHost::DetachFromWorker() { |
139 if (RenderProcessHost* host = RenderProcessHost::FromID(worker_id_.first)) | 127 if (RenderProcessHost* host = RenderProcessHost::FromID(worker_id_.first)) |
140 host->RemoveRoute(worker_id_.second); | 128 host->RemoveRoute(worker_id_.second); |
141 } | 129 } |
142 | 130 |
143 void EmbeddedWorkerDevToolsAgentHost::WorkerCreated() { | 131 void WorkerDevToolsAgentHost::WorkerCreated() { |
144 AddRef(); // Balanced in WorkerDestroyed() | 132 AddRef(); // Balanced in WorkerDestroyed() |
145 } | 133 } |
146 | 134 |
147 void EmbeddedWorkerDevToolsAgentHost::OnDispatchOnInspectorFrontend( | 135 void WorkerDevToolsAgentHost::OnDispatchOnInspectorFrontend( |
148 const std::string& message, | 136 const std::string& message, |
149 uint32 total_size) { | 137 uint32 total_size) { |
150 if (!IsAttached()) | 138 if (!IsAttached()) |
151 return; | 139 return; |
152 | 140 |
153 ProcessChunkedMessageFromAgent(message, total_size); | 141 ProcessChunkedMessageFromAgent(message, total_size); |
154 } | 142 } |
155 | 143 |
156 void EmbeddedWorkerDevToolsAgentHost::OnSaveAgentRuntimeState( | 144 void WorkerDevToolsAgentHost::OnSaveAgentRuntimeState( |
157 const std::string& state) { | 145 const std::string& state) { |
158 saved_agent_state_ = state; | 146 saved_agent_state_ = state; |
159 } | 147 } |
160 | 148 |
161 } // namespace content | 149 } // namespace content |
OLD | NEW |