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