OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright (C) 2011 Google Inc. All rights reserved. | 2 * Copyright (C) 2011 Google Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
6 * met: | 6 * met: |
7 * | 7 * |
8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
(...skipping 25 matching lines...) Expand all Loading... | |
36 #include "platform/wtf/RefPtr.h" | 36 #include "platform/wtf/RefPtr.h" |
37 #include "platform/wtf/text/WTFString.h" | 37 #include "platform/wtf/text/WTFString.h" |
38 | 38 |
39 namespace blink { | 39 namespace blink { |
40 | 40 |
41 using protocol::Response; | 41 using protocol::Response; |
42 | 42 |
43 namespace WorkerAgentState { | 43 namespace WorkerAgentState { |
44 static const char kAutoAttach[] = "autoAttach"; | 44 static const char kAutoAttach[] = "autoAttach"; |
45 static const char kWaitForDebuggerOnStart[] = "waitForDebuggerOnStart"; | 45 static const char kWaitForDebuggerOnStart[] = "waitForDebuggerOnStart"; |
46 static const char kAttachedWorkerIds[] = "attachedWorkerIds"; | 46 static const char kAttachedProtocolIds[] = "attachedProtocolIds"; |
47 }; | 47 }; |
48 | 48 |
49 namespace { | 49 int InspectorWorkerAgent::last_session_id_ = 0; |
50 // TODO(dgozman): support multiple sessions in protocol. | |
51 static const int kSessionId = 1; | |
52 } // namespace | |
53 | 50 |
54 InspectorWorkerAgent::InspectorWorkerAgent(InspectedFrames* inspected_frames) | 51 InspectorWorkerAgent::InspectorWorkerAgent(InspectedFrames* inspected_frames) |
55 : inspected_frames_(inspected_frames) {} | 52 : inspected_frames_(inspected_frames) {} |
56 | 53 |
57 InspectorWorkerAgent::~InspectorWorkerAgent() {} | 54 InspectorWorkerAgent::~InspectorWorkerAgent() {} |
58 | 55 |
59 void InspectorWorkerAgent::Restore() { | 56 void InspectorWorkerAgent::Restore() { |
60 if (!AutoAttachEnabled()) | 57 if (!AutoAttachEnabled()) |
61 return; | 58 return; |
62 instrumenting_agents_->addInspectorWorkerAgent(this); | 59 instrumenting_agents_->addInspectorWorkerAgent(this); |
63 protocol::DictionaryValue* attached = AttachedWorkerIds(); | 60 protocol::DictionaryValue* attached = AttachedProtocolIds(); |
64 for (size_t i = 0; i < attached->size(); ++i) | 61 for (size_t i = 0; i < attached->size(); ++i) |
65 GetFrontend()->detachedFromTarget(attached->at(i).first); | 62 GetFrontend()->detachedFromTarget(attached->at(i).first); |
66 state_->remove(WorkerAgentState::kAttachedWorkerIds); | 63 state_->remove(WorkerAgentState::kAttachedProtocolIds); |
67 ConnectToAllProxies(); | 64 ConnectToAllProxies(); |
68 } | 65 } |
69 | 66 |
70 Response InspectorWorkerAgent::disable() { | 67 Response InspectorWorkerAgent::disable() { |
71 if (AutoAttachEnabled()) { | 68 if (AutoAttachEnabled()) { |
72 DisconnectFromAllProxies(false); | 69 DisconnectFromAllProxies(false); |
73 instrumenting_agents_->removeInspectorWorkerAgent(this); | 70 instrumenting_agents_->removeInspectorWorkerAgent(this); |
74 } | 71 } |
75 state_->setBoolean(WorkerAgentState::kAutoAttach, false); | 72 state_->setBoolean(WorkerAgentState::kAutoAttach, false); |
76 state_->setBoolean(WorkerAgentState::kWaitForDebuggerOnStart, false); | 73 state_->setBoolean(WorkerAgentState::kWaitForDebuggerOnStart, false); |
77 state_->remove(WorkerAgentState::kAttachedWorkerIds); | 74 state_->remove(WorkerAgentState::kAttachedProtocolIds); |
78 return Response::OK(); | 75 return Response::OK(); |
79 } | 76 } |
80 | 77 |
81 Response InspectorWorkerAgent::setAutoAttach(bool auto_attach, | 78 Response InspectorWorkerAgent::setAutoAttach(bool auto_attach, |
82 bool wait_for_debugger_on_start) { | 79 bool wait_for_debugger_on_start) { |
83 state_->setBoolean(WorkerAgentState::kWaitForDebuggerOnStart, | 80 state_->setBoolean(WorkerAgentState::kWaitForDebuggerOnStart, |
84 wait_for_debugger_on_start); | 81 wait_for_debugger_on_start); |
85 | 82 |
86 if (auto_attach == AutoAttachEnabled()) | 83 if (auto_attach == AutoAttachEnabled()) |
87 return Response::OK(); | 84 return Response::OK(); |
88 state_->setBoolean(WorkerAgentState::kAutoAttach, auto_attach); | 85 state_->setBoolean(WorkerAgentState::kAutoAttach, auto_attach); |
89 if (auto_attach) { | 86 if (auto_attach) { |
90 instrumenting_agents_->addInspectorWorkerAgent(this); | 87 instrumenting_agents_->addInspectorWorkerAgent(this); |
91 ConnectToAllProxies(); | 88 ConnectToAllProxies(); |
92 } else { | 89 } else { |
93 DisconnectFromAllProxies(true); | 90 DisconnectFromAllProxies(true); |
94 instrumenting_agents_->removeInspectorWorkerAgent(this); | 91 instrumenting_agents_->removeInspectorWorkerAgent(this); |
95 } | 92 } |
96 return Response::OK(); | 93 return Response::OK(); |
97 } | 94 } |
98 | 95 |
99 Response InspectorWorkerAgent::setAttachToFrames(bool attach) { | 96 Response InspectorWorkerAgent::setAttachToFrames(bool attach) { |
100 return Response::OK(); | 97 return Response::OK(); |
101 } | 98 } |
102 | 99 |
103 bool InspectorWorkerAgent::AutoAttachEnabled() { | 100 bool InspectorWorkerAgent::AutoAttachEnabled() { |
104 return state_->booleanProperty(WorkerAgentState::kAutoAttach, false); | 101 return state_->booleanProperty(WorkerAgentState::kAutoAttach, false); |
105 } | 102 } |
106 | 103 |
107 Response InspectorWorkerAgent::sendMessageToTarget(const String& target_id, | 104 Response InspectorWorkerAgent::sendMessageToTarget(const String& session_id, |
108 const String& message) { | 105 const String& message) { |
109 WorkerInspectorProxy* proxy = connected_proxies_.at(target_id); | 106 auto it = protocol_to_session_id_.find(session_id); |
caseq
2017/06/27 18:29:30
This now looks confusing -- should we call the par
| |
110 if (!proxy) | 107 if (it == protocol_to_session_id_.end()) |
111 return Response::Error("Not attached to a target with given id"); | 108 return Response::Error("No session with given id"); |
112 proxy->SendMessageToInspector(kSessionId, message); | 109 WorkerInspectorProxy* proxy = connected_proxies_.at(it->value); |
110 DCHECK(proxy); | |
caseq
2017/06/27 18:29:30
No need for a DCHECK() in a case like this -- you'
| |
111 proxy->SendMessageToInspector(it->value, message); | |
113 return Response::OK(); | 112 return Response::OK(); |
114 } | 113 } |
115 | 114 |
116 void InspectorWorkerAgent::SetTracingSessionId( | 115 void InspectorWorkerAgent::SetTracingSessionId( |
117 const String& tracing_session_id) { | 116 const String& tracing_session_id) { |
118 tracing_session_id_ = tracing_session_id; | 117 tracing_session_id_ = tracing_session_id; |
119 if (tracing_session_id.IsEmpty()) | 118 if (tracing_session_id.IsEmpty()) |
120 return; | 119 return; |
121 for (auto& id_proxy : connected_proxies_) | 120 for (auto& id_proxy : connected_proxies_) |
122 id_proxy.value->WriteTimelineStartedEvent(tracing_session_id); | 121 id_proxy.value->WriteTimelineStartedEvent(tracing_session_id); |
123 } | 122 } |
124 | 123 |
125 void InspectorWorkerAgent::ShouldWaitForDebuggerOnWorkerStart(bool* result) { | 124 void InspectorWorkerAgent::ShouldWaitForDebuggerOnWorkerStart(bool* result) { |
126 if (AutoAttachEnabled() && | 125 if (AutoAttachEnabled() && |
127 state_->booleanProperty(WorkerAgentState::kWaitForDebuggerOnStart, false)) | 126 state_->booleanProperty(WorkerAgentState::kWaitForDebuggerOnStart, false)) |
128 *result = true; | 127 *result = true; |
129 } | 128 } |
130 | 129 |
131 void InspectorWorkerAgent::DidStartWorker(WorkerInspectorProxy* proxy, | 130 void InspectorWorkerAgent::DidStartWorker(WorkerInspectorProxy* proxy, |
132 bool waiting_for_debugger) { | 131 bool waiting_for_debugger) { |
133 DCHECK(GetFrontend() && AutoAttachEnabled()); | 132 DCHECK(GetFrontend() && AutoAttachEnabled()); |
134 ConnectToProxy(proxy, waiting_for_debugger); | 133 ConnectToProxy(proxy, waiting_for_debugger); |
135 if (!tracing_session_id_.IsEmpty()) | 134 if (!tracing_session_id_.IsEmpty()) |
136 proxy->WriteTimelineStartedEvent(tracing_session_id_); | 135 proxy->WriteTimelineStartedEvent(tracing_session_id_); |
137 } | 136 } |
138 | 137 |
139 void InspectorWorkerAgent::WorkerTerminated(WorkerInspectorProxy* proxy) { | 138 void InspectorWorkerAgent::WorkerTerminated(WorkerInspectorProxy* proxy) { |
140 DCHECK(GetFrontend() && AutoAttachEnabled()); | 139 DCHECK(GetFrontend() && AutoAttachEnabled()); |
141 if (connected_proxies_.find(proxy->InspectorId()) == connected_proxies_.end()) | 140 Vector<String> protocol_ids; |
142 return; | 141 for (auto& it : protocol_to_session_id_) { |
143 AttachedWorkerIds()->remove(proxy->InspectorId()); | 142 if (connected_proxies_.at(it.value) == proxy) |
144 GetFrontend()->detachedFromTarget(proxy->InspectorId()); | 143 protocol_ids.push_back(it.key); |
145 proxy->DisconnectFromInspector(kSessionId, this); | 144 } |
146 connected_proxies_.erase(proxy->InspectorId()); | 145 for (const String& protocol_id : protocol_ids) { |
146 AttachedProtocolIds()->remove(protocol_id); | |
147 GetFrontend()->detachedFromTarget(protocol_id); | |
148 int session_id = protocol_to_session_id_.at(protocol_id); | |
149 proxy->DisconnectFromInspector(session_id, this); | |
150 connected_proxies_.erase(session_id); | |
151 protocol_to_session_id_.erase(protocol_id); | |
152 session_to_protocol_id_.erase(session_id); | |
153 } | |
147 } | 154 } |
148 | 155 |
149 void InspectorWorkerAgent::ConnectToAllProxies() { | 156 void InspectorWorkerAgent::ConnectToAllProxies() { |
150 for (WorkerInspectorProxy* proxy : WorkerInspectorProxy::AllProxies()) { | 157 for (WorkerInspectorProxy* proxy : WorkerInspectorProxy::AllProxies()) { |
151 // For now we assume this is document. TODO(kinuko): Fix this. | 158 // For now we assume this is document. TODO(kinuko): Fix this. |
152 DCHECK(proxy->GetExecutionContext()->IsDocument()); | 159 DCHECK(proxy->GetExecutionContext()->IsDocument()); |
153 Document* document = ToDocument(proxy->GetExecutionContext()); | 160 Document* document = ToDocument(proxy->GetExecutionContext()); |
154 if (document->GetFrame() && | 161 if (document->GetFrame() && |
155 inspected_frames_->Contains(document->GetFrame())) | 162 inspected_frames_->Contains(document->GetFrame())) { |
156 ConnectToProxy(proxy, false); | 163 ConnectToProxy(proxy, false); |
164 } | |
157 } | 165 } |
158 } | 166 } |
159 | 167 |
160 void InspectorWorkerAgent::DisconnectFromAllProxies(bool report_to_frontend) { | 168 void InspectorWorkerAgent::DisconnectFromAllProxies(bool report_to_frontend) { |
161 for (auto& id_proxy : connected_proxies_) { | 169 for (auto& it : protocol_to_session_id_) { |
162 if (report_to_frontend) { | 170 if (report_to_frontend) { |
163 AttachedWorkerIds()->remove(id_proxy.key); | 171 AttachedProtocolIds()->remove(it.key); |
164 GetFrontend()->detachedFromTarget(id_proxy.key); | 172 GetFrontend()->detachedFromTarget(it.key); |
165 } | 173 } |
166 id_proxy.value->DisconnectFromInspector(kSessionId, this); | 174 connected_proxies_.at(it.value)->DisconnectFromInspector(it.value, this); |
167 } | 175 } |
176 protocol_to_session_id_.clear(); | |
177 session_to_protocol_id_.clear(); | |
168 connected_proxies_.clear(); | 178 connected_proxies_.clear(); |
169 } | 179 } |
170 | 180 |
171 void InspectorWorkerAgent::DidCommitLoadForLocalFrame(LocalFrame* frame) { | 181 void InspectorWorkerAgent::DidCommitLoadForLocalFrame(LocalFrame* frame) { |
172 if (!AutoAttachEnabled() || frame != inspected_frames_->Root()) | 182 if (!AutoAttachEnabled() || frame != inspected_frames_->Root()) |
173 return; | 183 return; |
174 | 184 |
175 // During navigation workers from old page may die after a while. | 185 // During navigation workers from old page may die after a while. |
176 // Usually, it's fine to report them terminated later, but some tests | 186 // Usually, it's fine to report them terminated later, but some tests |
177 // expect strict set of workers, and we reuse renderer between tests. | 187 // expect strict set of workers, and we reuse renderer between tests. |
178 for (auto& id_proxy : connected_proxies_) { | 188 DisconnectFromAllProxies(true); |
179 AttachedWorkerIds()->remove(id_proxy.key); | |
180 GetFrontend()->detachedFromTarget(id_proxy.key); | |
181 id_proxy.value->DisconnectFromInspector(kSessionId, this); | |
182 } | |
183 connected_proxies_.clear(); | |
184 } | 189 } |
185 | 190 |
186 protocol::DictionaryValue* InspectorWorkerAgent::AttachedWorkerIds() { | 191 protocol::DictionaryValue* InspectorWorkerAgent::AttachedProtocolIds() { |
187 protocol::DictionaryValue* ids = | 192 protocol::DictionaryValue* ids = |
188 state_->getObject(WorkerAgentState::kAttachedWorkerIds); | 193 state_->getObject(WorkerAgentState::kAttachedProtocolIds); |
189 if (!ids) { | 194 if (!ids) { |
190 std::unique_ptr<protocol::DictionaryValue> new_ids = | 195 std::unique_ptr<protocol::DictionaryValue> new_ids = |
191 protocol::DictionaryValue::create(); | 196 protocol::DictionaryValue::create(); |
192 ids = new_ids.get(); | 197 ids = new_ids.get(); |
193 state_->setObject(WorkerAgentState::kAttachedWorkerIds, std::move(new_ids)); | 198 state_->setObject(WorkerAgentState::kAttachedProtocolIds, |
199 std::move(new_ids)); | |
194 } | 200 } |
195 return ids; | 201 return ids; |
196 } | 202 } |
197 | 203 |
198 void InspectorWorkerAgent::ConnectToProxy(WorkerInspectorProxy* proxy, | 204 void InspectorWorkerAgent::ConnectToProxy(WorkerInspectorProxy* proxy, |
199 bool waiting_for_debugger) { | 205 bool waiting_for_debugger) { |
200 connected_proxies_.Set(proxy->InspectorId(), proxy); | 206 int session_id = ++last_session_id_; |
caseq
2017/06/27 18:29:30
This looks more line proxy_id to me -- the word se
| |
201 proxy->ConnectToInspector(kSessionId, this); | 207 connected_proxies_.Set(session_id, proxy); |
208 | |
209 String protocol_id = proxy->InspectorId() + "-" + String::Number(session_id); | |
210 protocol_to_session_id_.Set(protocol_id, session_id); | |
211 session_to_protocol_id_.Set(session_id, protocol_id); | |
212 | |
213 proxy->ConnectToInspector(session_id, this); | |
202 DCHECK(GetFrontend()); | 214 DCHECK(GetFrontend()); |
203 AttachedWorkerIds()->setBoolean(proxy->InspectorId(), true); | 215 AttachedProtocolIds()->setBoolean(protocol_id, true); |
204 GetFrontend()->attachedToTarget(protocol::Target::TargetInfo::create() | 216 GetFrontend()->attachedToTarget(protocol_id, |
217 protocol::Target::TargetInfo::create() | |
205 .setTargetId(proxy->InspectorId()) | 218 .setTargetId(proxy->InspectorId()) |
206 .setType("worker") | 219 .setType("worker") |
207 .setTitle(proxy->Url()) | 220 .setTitle(proxy->Url()) |
208 .setUrl(proxy->Url()) | 221 .setUrl(proxy->Url()) |
209 .build(), | 222 .build(), |
210 waiting_for_debugger); | 223 waiting_for_debugger); |
211 } | 224 } |
212 | 225 |
213 void InspectorWorkerAgent::DispatchMessageFromWorker( | 226 void InspectorWorkerAgent::DispatchMessageFromWorker( |
214 WorkerInspectorProxy* proxy, | 227 WorkerInspectorProxy* proxy, |
215 int session_id, | 228 int session_id, |
216 const String& message) { | 229 const String& message) { |
217 DCHECK(session_id == kSessionId); | 230 auto it = session_to_protocol_id_.find(session_id); |
218 GetFrontend()->receivedMessageFromTarget(proxy->InspectorId(), message); | 231 if (it == session_to_protocol_id_.end()) |
232 return; | |
233 GetFrontend()->receivedMessageFromTarget(it->value, message); | |
219 } | 234 } |
220 | 235 |
221 DEFINE_TRACE(InspectorWorkerAgent) { | 236 DEFINE_TRACE(InspectorWorkerAgent) { |
222 visitor->Trace(connected_proxies_); | 237 visitor->Trace(connected_proxies_); |
223 visitor->Trace(inspected_frames_); | 238 visitor->Trace(inspected_frames_); |
224 InspectorBaseAgent::Trace(visitor); | 239 InspectorBaseAgent::Trace(visitor); |
225 } | 240 } |
226 | 241 |
227 } // namespace blink | 242 } // namespace blink |
OLD | NEW |