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 "chrome/browser/devtools/device/devtools_android_bridge.h" | 5 #include "chrome/browser/devtools/device/devtools_android_bridge.h" |
6 | 6 |
7 #include <map> | 7 #include <map> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/base64.h" | 10 #include "base/base64.h" |
(...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
180 public: | 180 public: |
181 ProtocolCommand( | 181 ProtocolCommand( |
182 scoped_refptr<DevToolsAndroidBridge::RemoteBrowser> browser, | 182 scoped_refptr<DevToolsAndroidBridge::RemoteBrowser> browser, |
183 const std::string& debug_url, | 183 const std::string& debug_url, |
184 const std::string& command, | 184 const std::string& command, |
185 const base::Closure callback); | 185 const base::Closure callback); |
186 | 186 |
187 private: | 187 private: |
188 virtual void OnSocketOpened() OVERRIDE; | 188 virtual void OnSocketOpened() OVERRIDE; |
189 virtual void OnFrameRead(const std::string& message) OVERRIDE; | 189 virtual void OnFrameRead(const std::string& message) OVERRIDE; |
190 virtual void OnSocketClosed(bool closed_by_device) OVERRIDE; | 190 virtual void OnSocketClosed() OVERRIDE; |
| 191 virtual ~ProtocolCommand(); |
191 | 192 |
192 const std::string command_; | 193 const std::string command_; |
193 const base::Closure callback_; | 194 const base::Closure callback_; |
194 scoped_refptr<DevToolsAndroidBridge::AndroidWebSocket> web_socket_; | 195 scoped_ptr<DevToolsAndroidBridge::AndroidWebSocket> web_socket_; |
195 | 196 |
196 DISALLOW_COPY_AND_ASSIGN(ProtocolCommand); | 197 DISALLOW_COPY_AND_ASSIGN(ProtocolCommand); |
197 }; | 198 }; |
198 | 199 |
199 ProtocolCommand::ProtocolCommand( | 200 ProtocolCommand::ProtocolCommand( |
200 scoped_refptr<DevToolsAndroidBridge::RemoteBrowser> browser, | 201 scoped_refptr<DevToolsAndroidBridge::RemoteBrowser> browser, |
201 const std::string& debug_url, | 202 const std::string& debug_url, |
202 const std::string& command, | 203 const std::string& command, |
203 const base::Closure callback) | 204 const base::Closure callback) |
204 : command_(command), | 205 : command_(command), |
205 callback_(callback){ | 206 callback_(callback), |
206 web_socket_ = browser->CreateWebSocket(debug_url, this); | 207 web_socket_(browser->CreateWebSocket(debug_url, this)) { |
207 web_socket_->Connect(); | |
208 } | 208 } |
209 | 209 |
210 void ProtocolCommand::OnSocketOpened() { | 210 void ProtocolCommand::OnSocketOpened() { |
211 web_socket_->SendFrame(command_); | 211 web_socket_->SendFrame(command_); |
212 } | 212 } |
213 | 213 |
214 void ProtocolCommand::OnFrameRead(const std::string& message) { | 214 void ProtocolCommand::OnFrameRead(const std::string& message) { |
215 web_socket_->Disconnect(); | 215 delete this; |
216 } | 216 } |
217 | 217 |
218 void ProtocolCommand::OnSocketClosed(bool closed_by_device) { | 218 void ProtocolCommand::OnSocketClosed() { |
219 if (!callback_.is_null()) { | 219 delete this; |
| 220 } |
| 221 |
| 222 ProtocolCommand::~ProtocolCommand() { |
| 223 if (!callback_.is_null()) |
220 callback_.Run(); | 224 callback_.Run(); |
221 } | |
222 delete this; | |
223 } | 225 } |
224 | 226 |
225 } // namespace | 227 } // namespace |
226 | 228 |
227 class AgentHostDelegate; | 229 class AgentHostDelegate; |
228 | 230 |
229 typedef std::map<std::string, AgentHostDelegate*> AgentHostDelegates; | 231 typedef std::map<std::string, AgentHostDelegate*> AgentHostDelegates; |
230 | 232 |
231 base::LazyInstance<AgentHostDelegates>::Leaky g_host_delegates = | 233 base::LazyInstance<AgentHostDelegates>::Leaky g_host_delegates = |
232 LAZY_INSTANCE_INITIALIZER; | 234 LAZY_INSTANCE_INITIALIZER; |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
285 const std::string& id, | 287 const std::string& id, |
286 scoped_refptr<DevToolsAndroidBridge::RemoteBrowser> browser, | 288 scoped_refptr<DevToolsAndroidBridge::RemoteBrowser> browser, |
287 const std::string& debug_url); | 289 const std::string& debug_url); |
288 virtual ~AgentHostDelegate(); | 290 virtual ~AgentHostDelegate(); |
289 virtual void Attach(content::DevToolsExternalAgentProxy* proxy) OVERRIDE; | 291 virtual void Attach(content::DevToolsExternalAgentProxy* proxy) OVERRIDE; |
290 virtual void Detach() OVERRIDE; | 292 virtual void Detach() OVERRIDE; |
291 virtual void SendMessageToBackend( | 293 virtual void SendMessageToBackend( |
292 const std::string& message) OVERRIDE; | 294 const std::string& message) OVERRIDE; |
293 virtual void OnSocketOpened() OVERRIDE; | 295 virtual void OnSocketOpened() OVERRIDE; |
294 virtual void OnFrameRead(const std::string& message) OVERRIDE; | 296 virtual void OnFrameRead(const std::string& message) OVERRIDE; |
295 virtual void OnSocketClosed(bool closed_by_device) OVERRIDE; | 297 virtual void OnSocketClosed() OVERRIDE; |
296 | 298 |
297 const std::string id_; | 299 const std::string id_; |
| 300 scoped_refptr<DevToolsAndroidBridge::RemoteBrowser> browser_; |
| 301 const std::string debug_url_; |
298 bool socket_opened_; | 302 bool socket_opened_; |
299 bool detached_; | |
300 bool is_web_view_; | 303 bool is_web_view_; |
301 std::vector<std::string> pending_messages_; | 304 std::vector<std::string> pending_messages_; |
302 scoped_refptr<DevToolsAndroidBridge::AndroidWebSocket> web_socket_; | 305 scoped_ptr<DevToolsAndroidBridge::AndroidWebSocket> web_socket_; |
303 content::DevToolsAgentHost* agent_host_; | 306 content::DevToolsAgentHost* agent_host_; |
304 content::DevToolsExternalAgentProxy* proxy_; | 307 content::DevToolsExternalAgentProxy* proxy_; |
305 DISALLOW_COPY_AND_ASSIGN(AgentHostDelegate); | 308 DISALLOW_COPY_AND_ASSIGN(AgentHostDelegate); |
306 }; | 309 }; |
307 | 310 |
308 // static | 311 // static |
309 scoped_refptr<content::DevToolsAgentHost> | 312 scoped_refptr<content::DevToolsAgentHost> |
310 AgentHostDelegate::GetOrCreateAgentHost( | 313 AgentHostDelegate::GetOrCreateAgentHost( |
311 const std::string& id, | 314 const std::string& id, |
312 scoped_refptr<DevToolsAndroidBridge::RemoteBrowser> browser, | 315 scoped_refptr<DevToolsAndroidBridge::RemoteBrowser> browser, |
313 const std::string& debug_url) { | 316 const std::string& debug_url) { |
314 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 317 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
315 AgentHostDelegates::iterator it = g_host_delegates.Get().find(id); | 318 AgentHostDelegates::iterator it = g_host_delegates.Get().find(id); |
316 if (it != g_host_delegates.Get().end()) | 319 if (it != g_host_delegates.Get().end()) |
317 return it->second->agent_host_; | 320 return it->second->agent_host_; |
318 | 321 |
319 AgentHostDelegate* delegate = new AgentHostDelegate(id, browser, debug_url); | 322 AgentHostDelegate* delegate = new AgentHostDelegate(id, browser, debug_url); |
320 scoped_refptr<content::DevToolsAgentHost> result = | 323 scoped_refptr<content::DevToolsAgentHost> result = |
321 content::DevToolsAgentHost::Create(delegate); | 324 content::DevToolsAgentHost::Create(delegate); |
322 delegate->agent_host_ = result.get(); | 325 delegate->agent_host_ = result.get(); |
323 return result; | 326 return result; |
324 } | 327 } |
325 | 328 |
326 AgentHostDelegate::AgentHostDelegate( | 329 AgentHostDelegate::AgentHostDelegate( |
327 const std::string& id, | 330 const std::string& id, |
328 scoped_refptr<DevToolsAndroidBridge::RemoteBrowser> browser, | 331 scoped_refptr<DevToolsAndroidBridge::RemoteBrowser> browser, |
329 const std::string& debug_url) | 332 const std::string& debug_url) |
330 : id_(id), | 333 : id_(id), |
| 334 browser_(browser), |
| 335 debug_url_(debug_url), |
331 socket_opened_(false), | 336 socket_opened_(false), |
332 detached_(false), | |
333 is_web_view_(browser->IsWebView()), | 337 is_web_view_(browser->IsWebView()), |
334 web_socket_(browser->CreateWebSocket(debug_url, this)), | |
335 agent_host_(NULL), | 338 agent_host_(NULL), |
336 proxy_(NULL) { | 339 proxy_(NULL) { |
337 g_host_delegates.Get()[id] = this; | 340 g_host_delegates.Get()[id] = this; |
338 } | 341 } |
339 | 342 |
340 AgentHostDelegate::~AgentHostDelegate() { | 343 AgentHostDelegate::~AgentHostDelegate() { |
341 g_host_delegates.Get().erase(id_); | 344 g_host_delegates.Get().erase(id_); |
342 web_socket_->ClearDelegate(); | |
343 } | 345 } |
344 | 346 |
345 void AgentHostDelegate::Attach(content::DevToolsExternalAgentProxy* proxy) { | 347 void AgentHostDelegate::Attach(content::DevToolsExternalAgentProxy* proxy) { |
346 proxy_ = proxy; | 348 proxy_ = proxy; |
347 content::RecordAction(base::UserMetricsAction(is_web_view_ ? | 349 content::RecordAction(base::UserMetricsAction(is_web_view_ ? |
348 "DevTools_InspectAndroidWebView" : "DevTools_InspectAndroidPage")); | 350 "DevTools_InspectAndroidWebView" : "DevTools_InspectAndroidPage")); |
349 web_socket_->Connect(); | 351 web_socket_.reset(browser_->CreateWebSocket(debug_url_, this)); |
350 } | 352 } |
351 | 353 |
352 void AgentHostDelegate::Detach() { | 354 void AgentHostDelegate::Detach() { |
353 detached_ = true; | 355 web_socket_.reset(); |
354 if (socket_opened_) | |
355 web_socket_->Disconnect(); | |
356 } | 356 } |
357 | 357 |
358 void AgentHostDelegate::SendMessageToBackend(const std::string& message) { | 358 void AgentHostDelegate::SendMessageToBackend(const std::string& message) { |
359 if (socket_opened_) | 359 if (socket_opened_) |
360 web_socket_->SendFrame(message); | 360 web_socket_->SendFrame(message); |
361 else | 361 else |
362 pending_messages_.push_back(message); | 362 pending_messages_.push_back(message); |
363 } | 363 } |
364 | 364 |
365 void AgentHostDelegate::OnSocketOpened() { | 365 void AgentHostDelegate::OnSocketOpened() { |
366 if (detached_) { | |
367 web_socket_->Disconnect(); | |
368 return; | |
369 } | |
370 | |
371 socket_opened_ = true; | 366 socket_opened_ = true; |
372 for (std::vector<std::string>::iterator it = pending_messages_.begin(); | 367 for (std::vector<std::string>::iterator it = pending_messages_.begin(); |
373 it != pending_messages_.end(); ++it) { | 368 it != pending_messages_.end(); ++it) { |
374 SendMessageToBackend(*it); | 369 SendMessageToBackend(*it); |
375 } | 370 } |
376 pending_messages_.clear(); | 371 pending_messages_.clear(); |
377 } | 372 } |
378 | 373 |
379 void AgentHostDelegate::OnFrameRead(const std::string& message) { | 374 void AgentHostDelegate::OnFrameRead(const std::string& message) { |
380 if (proxy_) | 375 if (proxy_) |
381 proxy_->DispatchOnClientHost(message); | 376 proxy_->DispatchOnClientHost(message); |
382 } | 377 } |
383 | 378 |
384 void AgentHostDelegate::OnSocketClosed(bool closed_by_device) { | 379 void AgentHostDelegate::OnSocketClosed() { |
385 if (proxy_ && closed_by_device) | 380 if (proxy_) |
386 proxy_->ConnectionClosed(); | 381 proxy_->ConnectionClosed(); |
387 } | 382 } |
388 | 383 |
389 //// RemotePageTarget ---------------------------------------------- | 384 //// RemotePageTarget ---------------------------------------------- |
390 | 385 |
391 class RemotePageTarget : public DevToolsTargetImpl, | 386 class RemotePageTarget : public DevToolsTargetImpl, |
392 public DevToolsAndroidBridge::RemotePage { | 387 public DevToolsAndroidBridge::RemotePage { |
393 public: | 388 public: |
394 RemotePageTarget(scoped_refptr<DevToolsAndroidBridge::RemoteBrowser> browser, | 389 RemotePageTarget(scoped_refptr<DevToolsAndroidBridge::RemoteBrowser> browser, |
395 const base::DictionaryValue& value); | 390 const base::DictionaryValue& value); |
(...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
612 InnerOpen(url, base::Bind(&RemoteBrowser::RespondToOpenOnUIThread, | 607 InnerOpen(url, base::Bind(&RemoteBrowser::RespondToOpenOnUIThread, |
613 this, callback)); | 608 this, callback)); |
614 } | 609 } |
615 | 610 |
616 scoped_refptr<content::DevToolsAgentHost> | 611 scoped_refptr<content::DevToolsAgentHost> |
617 DevToolsAndroidBridge::RemoteBrowser::GetAgentHost() { | 612 DevToolsAndroidBridge::RemoteBrowser::GetAgentHost() { |
618 return AgentHostDelegate::GetOrCreateAgentHost( | 613 return AgentHostDelegate::GetOrCreateAgentHost( |
619 "adb:" + device_->serial() + ":" + socket_, this, kBrowserTargetSocket); | 614 "adb:" + device_->serial() + ":" + socket_, this, kBrowserTargetSocket); |
620 } | 615 } |
621 | 616 |
622 scoped_refptr<DevToolsAndroidBridge::AndroidWebSocket> | 617 DevToolsAndroidBridge::AndroidWebSocket* |
623 DevToolsAndroidBridge::RemoteBrowser::CreateWebSocket( | 618 DevToolsAndroidBridge::RemoteBrowser::CreateWebSocket( |
624 const std::string& url, | 619 const std::string& url, |
625 DevToolsAndroidBridge::AndroidWebSocket::Delegate* delegate) { | 620 DevToolsAndroidBridge::AndroidWebSocket::Delegate* delegate) { |
626 return device_->CreateWebSocket(socket_, url, delegate); | 621 return device_->CreateWebSocket(socket_, url, delegate); |
627 } | 622 } |
628 | 623 |
629 void DevToolsAndroidBridge::RemoteBrowser::RespondToOpenOnUIThread( | 624 void DevToolsAndroidBridge::RemoteBrowser::RespondToOpenOnUIThread( |
630 const DevToolsAndroidBridge::RemotePageCallback& callback, | 625 const DevToolsAndroidBridge::RemotePageCallback& callback, |
631 int result, | 626 int result, |
632 const std::string& response) { | 627 const std::string& response) { |
(...skipping 297 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
930 bool enabled; | 925 bool enabled; |
931 if (pref_value->GetAsBoolean(&enabled) && enabled) { | 926 if (pref_value->GetAsBoolean(&enabled) && enabled) { |
932 device_providers.push_back(new UsbDeviceProvider(profile_)); | 927 device_providers.push_back(new UsbDeviceProvider(profile_)); |
933 } | 928 } |
934 device_manager_->SetDeviceProviders(device_providers); | 929 device_manager_->SetDeviceProviders(device_providers); |
935 if (NeedsDeviceListPolling()) { | 930 if (NeedsDeviceListPolling()) { |
936 StopDeviceListPolling(); | 931 StopDeviceListPolling(); |
937 StartDeviceListPolling(); | 932 StartDeviceListPolling(); |
938 } | 933 } |
939 } | 934 } |
OLD | NEW |