// Copyright 2014 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #ifndef CHROME_BROWSER_DEVTOOLS_DEVICE_ANDROID_DEVICE_MANAGER_H_ #define CHROME_BROWSER_DEVTOOLS_DEVICE_ANDROID_DEVICE_MANAGER_H_ #include #include "base/memory/ref_counted.h" #include "base/memory/weak_ptr.h" #include "base/single_thread_task_runner.h" #include "base/threading/non_thread_safe.h" #include "chrome/browser/profiles/profile.h" #include "content/public/browser/browser_thread.h" #include "ui/gfx/geometry/size.h" namespace net { class StreamSocket; } class AndroidDeviceManager : public base::NonThreadSafe { public: using CommandCallback = base::Callback; using SocketCallback = base::Callback)>; // |body_head| should contain the body (WebSocket frame data) part that has // been read during processing the header (WebSocket handshake). using HttpUpgradeCallback = base::Callback)>; using SerialsCallback = base::Callback&)>; struct BrowserInfo { BrowserInfo(); enum Type { kTypeChrome, kTypeWebView, kTypeOther }; std::string socket_name; std::string display_name; std::string user; Type type; }; struct DeviceInfo { DeviceInfo(); ~DeviceInfo(); std::string model; bool connected; gfx::Size screen_size; std::vector browser_info; }; typedef base::Callback DeviceInfoCallback; class Device; class AndroidWebSocket { public: class Delegate { public: virtual void OnSocketOpened() = 0; virtual void OnFrameRead(const std::string& message) = 0; virtual void OnSocketClosed() = 0; protected: virtual ~Delegate() {} }; ~AndroidWebSocket(); void SendFrame(const std::string& message); private: friend class Device; class WebSocketImpl; AndroidWebSocket( scoped_refptr device, const std::string& socket_name, const std::string& url, AndroidWebSocket::Delegate* delegate); void Connected(int result, const std::string& extensions, const std::string& body_head, scoped_ptr socket); void OnFrameRead(const std::string& message); void OnSocketClosed(); void Terminate(); Device* device_; WebSocketImpl* socket_impl_; Delegate* delegate_; base::WeakPtrFactory weak_factory_; DISALLOW_COPY_AND_ASSIGN(AndroidWebSocket); }; class DeviceProvider; class Device : public base::RefCountedThreadSafe, public base::NonThreadSafe { public: void QueryDeviceInfo(const DeviceInfoCallback& callback); void OpenSocket(const std::string& socket_name, const SocketCallback& callback); void SendJsonRequest(const std::string& socket_name, const std::string& request, const CommandCallback& callback); void HttpUpgrade(const std::string& socket_name, const std::string& url, const std::string& extensions, const HttpUpgradeCallback& callback); AndroidWebSocket* CreateWebSocket( const std::string& socket_name, const std::string& url, AndroidWebSocket::Delegate* delegate); std::string serial() { return serial_; } private: friend class base::RefCountedThreadSafe; friend class AndroidDeviceManager; friend class AndroidWebSocket; Device(scoped_refptr device_task_runner, scoped_refptr provider, const std::string& serial); virtual ~Device(); scoped_refptr task_runner_; scoped_refptr provider_; std::string serial_; std::set sockets_; base::WeakPtrFactory weak_factory_; DISALLOW_COPY_AND_ASSIGN(Device); }; typedef std::vector > Devices; typedef base::Callback DevicesCallback; class DeviceProvider : public base::RefCountedThreadSafe { public: typedef AndroidDeviceManager::SerialsCallback SerialsCallback; typedef AndroidDeviceManager::DeviceInfoCallback DeviceInfoCallback; typedef AndroidDeviceManager::SocketCallback SocketCallback; typedef AndroidDeviceManager::CommandCallback CommandCallback; virtual void QueryDevices(const SerialsCallback& callback) = 0; virtual void QueryDeviceInfo(const std::string& serial, const DeviceInfoCallback& callback) = 0; virtual void OpenSocket(const std::string& serial, const std::string& socket_name, const SocketCallback& callback) = 0; virtual void SendJsonRequest(const std::string& serial, const std::string& socket_name, const std::string& request, const CommandCallback& callback); virtual void HttpUpgrade(const std::string& serial, const std::string& socket_name, const std::string& url, const std::string& extensions, const HttpUpgradeCallback& callback); virtual void ReleaseDevice(const std::string& serial); protected: friend class base::RefCountedThreadSafe; DeviceProvider(); virtual ~DeviceProvider(); }; typedef std::vector > DeviceProviders; virtual ~AndroidDeviceManager(); static scoped_ptr Create(); void SetDeviceProviders(const DeviceProviders& providers); void QueryDevices(const DevicesCallback& callback); static std::string GetBrowserName(const std::string& socket, const std::string& package); using RunCommandCallback = base::Callback; static void QueryDeviceInfo(const RunCommandCallback& command_callback, const DeviceInfoCallback& callback); struct DeviceDescriptor { DeviceDescriptor(); ~DeviceDescriptor(); scoped_refptr provider; std::string serial; }; typedef std::vector DeviceDescriptors; private: class HandlerThread : public base::RefCountedThreadSafe { public: static scoped_refptr GetInstance(); scoped_refptr message_loop(); private: friend class base::RefCountedThreadSafe; static HandlerThread* instance_; static void StopThread(base::Thread* thread); HandlerThread(); virtual ~HandlerThread(); base::Thread* thread_; }; AndroidDeviceManager(); void UpdateDevices(const DevicesCallback& callback, scoped_ptr descriptors); typedef std::map > DeviceWeakMap; scoped_refptr handler_thread_; DeviceProviders providers_; DeviceWeakMap devices_; base::WeakPtrFactory weak_factory_; }; #endif // CHROME_BROWSER_DEVTOOLS_DEVICE_ANDROID_DEVICE_MANAGER_H_