1 // Copyright (c) 2013 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 #ifndef CHROME_BROWSER_DEVTOOLS_ADB_WEB_SOCKET_H_ 6 #define CHROME_BROWSER_DEVTOOLS_ADB_WEB_SOCKET_H_ 7 8 #include "chrome/browser/devtools/android_device.h" 9 10 class AdbWebSocket : public base::RefCountedThreadSafe<AdbWebSocket> { 11 public: 12 class Delegate { 13 public: 14 virtual void OnSocketOpened() = 0; 15 virtual void OnFrameRead(const std::string& message) = 0; 16 virtual void OnSocketClosed(bool closed_by_device) = 0; 17 virtual bool ProcessIncomingMessage(const std::string& message) = 0; 18 19 protected: ~Delegate()20 virtual ~Delegate() {} 21 }; 22 23 AdbWebSocket(scoped_refptr<AndroidDevice> device, 24 const std::string& socket_name, 25 const std::string& url, 26 base::MessageLoop* adb_message_loop, 27 Delegate* delegate); 28 29 void Disconnect(); 30 31 void SendFrame(const std::string& message); 32 33 void SendFrameOnHandlerThread(const std::string& message); 34 35 private: 36 friend class base::RefCountedThreadSafe<AdbWebSocket>; 37 38 virtual ~AdbWebSocket(); 39 40 void ConnectOnHandlerThread(); 41 void ConnectedOnHandlerThread(int result, net::StreamSocket* socket); 42 void StartListeningOnHandlerThread(); 43 void OnBytesRead(scoped_refptr<net::IOBuffer> response_buffer, int result); 44 void SendPendingRequests(int result); 45 void DisconnectOnHandlerThread(bool closed_by_device); 46 47 void OnSocketOpened(); 48 void OnFrameRead(const std::string& message); 49 void OnSocketClosed(bool closed_by_device); 50 51 scoped_refptr<AndroidDevice> device_; 52 std::string socket_name_; 53 std::string url_; 54 base::MessageLoop* adb_message_loop_; 55 scoped_ptr<net::StreamSocket> socket_; 56 Delegate* delegate_; 57 std::string response_buffer_; 58 std::string request_buffer_; 59 DISALLOW_COPY_AND_ASSIGN(AdbWebSocket); 60 }; 61 62 #endif // CHROME_BROWSER_DEVTOOLS_ADB_WEB_SOCKET_H_ 63