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