Home | History | Annotate | Download | only in device
      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
      3 // found in the LICENSE file.
      4 
      5 #ifndef CHROME_BROWSER_DEVTOOLS_DEVICE_ANDROID_DEVICE_MANAGER_H_
      6 #define CHROME_BROWSER_DEVTOOLS_DEVICE_ANDROID_DEVICE_MANAGER_H_
      7 
      8 #include <vector>
      9 
     10 #include "base/memory/ref_counted.h"
     11 #include "base/threading/non_thread_safe.h"
     12 #include "chrome/browser/profiles/profile.h"
     13 #include "content/public/browser/browser_thread.h"
     14 #include "ui/gfx/size.h"
     15 
     16 namespace net {
     17 class StreamSocket;
     18 }
     19 
     20 class AndroidDeviceManager
     21     : public base::RefCountedThreadSafe<
     22           AndroidDeviceManager,
     23           content::BrowserThread::DeleteOnUIThread>,
     24       public base::NonThreadSafe {
     25  public:
     26   typedef base::Callback<void(int, const std::string&)> CommandCallback;
     27   typedef base::Callback<void(int result, scoped_ptr<net::StreamSocket>)>
     28       SocketCallback;
     29   typedef base::Callback<void(const std::vector<std::string>&)> SerialsCallback;
     30 
     31   struct BrowserInfo {
     32     BrowserInfo();
     33 
     34     enum Type {
     35       kTypeChrome,
     36       kTypeWebView,
     37       kTypeOther
     38     };
     39 
     40     std::string socket_name;
     41     std::string display_name;
     42     Type type;
     43   };
     44 
     45   struct DeviceInfo {
     46     DeviceInfo();
     47     ~DeviceInfo();
     48 
     49     std::string model;
     50     bool connected;
     51     gfx::Size screen_size;
     52     std::vector<BrowserInfo> browser_info;
     53   };
     54 
     55   typedef base::Callback<void(const DeviceInfo&)> DeviceInfoCallback;
     56 
     57   class AndroidWebSocket {
     58    public:
     59     class Delegate {
     60      public:
     61       virtual void OnSocketOpened() = 0;
     62       virtual void OnFrameRead(const std::string& message) = 0;
     63       virtual void OnSocketClosed() = 0;
     64 
     65      protected:
     66       virtual ~Delegate() {}
     67     };
     68 
     69     virtual ~AndroidWebSocket() {}
     70 
     71     virtual void SendFrame(const std::string& message) = 0;
     72   };
     73 
     74   class DeviceProvider;
     75 
     76   class Device : public base::RefCountedThreadSafe<Device>,
     77                  public base::NonThreadSafe {
     78    public:
     79     typedef AndroidDeviceManager::DeviceInfoCallback DeviceInfoCallback;
     80     typedef AndroidDeviceManager::CommandCallback CommandCallback;
     81     typedef AndroidDeviceManager::SocketCallback SocketCallback;
     82 
     83     void QueryDeviceInfo(const DeviceInfoCallback& callback);
     84 
     85     void OpenSocket(const std::string& socket_name,
     86                     const SocketCallback& callback);
     87 
     88     void SendJsonRequest(const std::string& socket_name,
     89                          const std::string& request,
     90                          const CommandCallback& callback);
     91 
     92     void HttpUpgrade(const std::string& socket_name,
     93                      const std::string& url,
     94                      const SocketCallback& callback);
     95 
     96     AndroidWebSocket* CreateWebSocket(
     97         const std::string& socket_name,
     98         const std::string& url,
     99         AndroidWebSocket::Delegate* delegate);
    100 
    101     std::string serial() { return serial_; }
    102 
    103    private:
    104     friend class AndroidDeviceManager;
    105     Device(scoped_refptr<base::MessageLoopProxy> device_message_loop,
    106            scoped_refptr<DeviceProvider> provider,
    107            const std::string& serial);
    108 
    109     friend class base::RefCountedThreadSafe<Device>;
    110     virtual ~Device();
    111 
    112     scoped_refptr<base::MessageLoopProxy> device_message_loop_;
    113     scoped_refptr<DeviceProvider> provider_;
    114     std::string serial_;
    115     base::WeakPtrFactory<Device> weak_factory_;
    116 
    117     DISALLOW_COPY_AND_ASSIGN(Device);
    118   };
    119 
    120   typedef std::vector<scoped_refptr<Device> > Devices;
    121   typedef base::Callback<void(const Devices&)> DevicesCallback;
    122 
    123   class DeviceProvider : public base::RefCountedThreadSafe<DeviceProvider> {
    124    public:
    125     typedef AndroidDeviceManager::SerialsCallback SerialsCallback;
    126     typedef AndroidDeviceManager::DeviceInfoCallback DeviceInfoCallback;
    127     typedef AndroidDeviceManager::SocketCallback SocketCallback;
    128     typedef AndroidDeviceManager::CommandCallback CommandCallback;
    129 
    130     virtual void QueryDevices(const SerialsCallback& callback) = 0;
    131 
    132     virtual void QueryDeviceInfo(const std::string& serial,
    133                                  const DeviceInfoCallback& callback) = 0;
    134 
    135     virtual void OpenSocket(const std::string& serial,
    136                             const std::string& socket_name,
    137                             const SocketCallback& callback) = 0;
    138 
    139     virtual void SendJsonRequest(const std::string& serial,
    140                                  const std::string& socket_name,
    141                                  const std::string& request,
    142                                  const CommandCallback& callback);
    143 
    144     virtual void HttpUpgrade(const std::string& serial,
    145                              const std::string& socket_name,
    146                              const std::string& url,
    147                              const SocketCallback& callback);
    148 
    149     virtual void ReleaseDevice(const std::string& serial);
    150 
    151    protected:
    152     friend class base::RefCountedThreadSafe<DeviceProvider>;
    153     DeviceProvider();
    154     virtual ~DeviceProvider();
    155   };
    156 
    157   typedef std::vector<scoped_refptr<DeviceProvider> > DeviceProviders;
    158 
    159   static scoped_refptr<AndroidDeviceManager> Create();
    160 
    161   void SetDeviceProviders(const DeviceProviders& providers);
    162 
    163   void QueryDevices(const DevicesCallback& callback);
    164 
    165   struct DeviceDescriptor {
    166     DeviceDescriptor();
    167     ~DeviceDescriptor();
    168 
    169     scoped_refptr<DeviceProvider> provider;
    170     std::string serial;
    171   };
    172 
    173   typedef std::vector<DeviceDescriptor> DeviceDescriptors;
    174 
    175  private:
    176   class HandlerThread : public base::RefCountedThreadSafe<HandlerThread> {
    177    public:
    178     static scoped_refptr<HandlerThread> GetInstance();
    179     scoped_refptr<base::MessageLoopProxy> message_loop();
    180 
    181    private:
    182     friend class base::RefCountedThreadSafe<HandlerThread>;
    183     static HandlerThread* instance_;
    184     static void StopThread(base::Thread* thread);
    185 
    186     HandlerThread();
    187     virtual ~HandlerThread();
    188     base::Thread* thread_;
    189   };
    190 
    191   friend struct content::BrowserThread::DeleteOnThread<
    192       content::BrowserThread::UI>;
    193   friend class base::DeleteHelper<AndroidDeviceManager>;
    194   AndroidDeviceManager();
    195   virtual ~AndroidDeviceManager();
    196 
    197   void UpdateDevices(const DevicesCallback& callback,
    198                      DeviceDescriptors* descriptors);
    199 
    200   typedef std::map<std::string, base::WeakPtr<Device> > DeviceWeakMap;
    201 
    202   scoped_refptr<HandlerThread> handler_thread_;
    203   DeviceProviders providers_;
    204   DeviceWeakMap devices_;
    205 };
    206 
    207 #endif  // CHROME_BROWSER_DEVTOOLS_DEVICE_ANDROID_DEVICE_MANAGER_H_
    208