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