Cast: Stop logging kVideoFrameSentToEncoder and rename a couple events.
[chromium-blink-merge.git] / chrome / browser / devtools / device / devtools_android_bridge.h
blob8873816848f42eb24451f3c29413758f5483605b
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.
5 #ifndef CHROME_BROWSER_DEVTOOLS_DEVICE_DEVTOOLS_ANDROID_BRIDGE_H_
6 #define CHROME_BROWSER_DEVTOOLS_DEVICE_DEVTOOLS_ANDROID_BRIDGE_H_
8 #include <string>
9 #include <vector>
11 #include "base/callback.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/prefs/pref_change_registrar.h"
15 #include "chrome/browser/devtools/device/android_device_manager.h"
16 #include "components/keyed_service/content/browser_context_keyed_service_factory.h"
17 #include "components/keyed_service/core/keyed_service.h"
18 #include "ui/gfx/size.h"
20 template<typename T> struct DefaultSingletonTraits;
22 namespace base {
23 class MessageLoop;
24 class DictionaryValue;
25 class ListValue;
26 class Thread;
29 namespace content {
30 class BrowserContext;
33 namespace crypto {
34 class RSAPrivateKey;
37 class DevToolsTargetImpl;
38 class Profile;
40 // The format used for constructing DevTools server socket names.
41 extern const char kDevToolsChannelNameFormat[];
43 class DevToolsAndroidBridge
44 : public base::RefCountedThreadSafe<
45 DevToolsAndroidBridge,
46 content::BrowserThread::DeleteOnUIThread> {
47 public:
48 typedef base::Callback<void(int result,
49 const std::string& response)> Callback;
50 typedef base::Callback<void(DevToolsTargetImpl*)> TargetCallback;
52 class Wrapper : public KeyedService {
53 public:
54 explicit Wrapper(content::BrowserContext* context);
55 virtual ~Wrapper();
57 DevToolsAndroidBridge* Get();
58 private:
59 scoped_refptr<DevToolsAndroidBridge> bridge_;
62 class Factory : public BrowserContextKeyedServiceFactory {
63 public:
64 // Returns singleton instance of DevToolsAndroidBridge.
65 static Factory* GetInstance();
67 // Returns DevToolsAndroidBridge associated with |profile|.
68 static DevToolsAndroidBridge* GetForProfile(Profile* profile);
70 private:
71 friend struct DefaultSingletonTraits<Factory>;
73 Factory();
74 virtual ~Factory();
76 // BrowserContextKeyedServiceFactory overrides:
77 virtual KeyedService* BuildServiceInstanceFor(
78 content::BrowserContext* context) const OVERRIDE;
79 DISALLOW_COPY_AND_ASSIGN(Factory);
82 class AndroidWebSocket : public base::RefCountedThreadSafe<AndroidWebSocket> {
83 public:
84 class Delegate {
85 public:
86 virtual void OnSocketOpened() = 0;
87 virtual void OnFrameRead(const std::string& message) = 0;
88 virtual void OnSocketClosed(bool closed_by_device) = 0;
90 protected:
91 virtual ~Delegate() {}
94 AndroidWebSocket() {}
96 virtual void Disconnect() = 0;
98 virtual void SendFrame(const std::string& message) = 0;
100 protected:
101 virtual ~AndroidWebSocket() {}
103 private:
104 friend class base::RefCountedThreadSafe<AndroidWebSocket>;
106 DISALLOW_COPY_AND_ASSIGN(AndroidWebSocket);
109 class RemoteBrowser : public base::RefCounted<RemoteBrowser> {
110 public:
111 RemoteBrowser(
112 scoped_refptr<DevToolsAndroidBridge> android_bridge,
113 const std::string& serial,
114 const std::string& socket);
116 std::string serial() { return serial_; }
117 std::string socket() { return socket_; }
119 std::string display_name() { return display_name_; }
120 void set_display_name(const std::string& name) { display_name_ = name; }
122 std::string version() { return version_; }
123 void set_version(const std::string& version) { version_ = version; }
125 bool IsChrome() const;
127 typedef std::vector<int> ParsedVersion;
128 ParsedVersion GetParsedVersion() const;
130 std::vector<DevToolsTargetImpl*> CreatePageTargets();
131 void SetPageDescriptors(const base::ListValue&);
133 typedef base::Callback<void(int, const std::string&)> JsonRequestCallback;
134 void SendJsonRequest(const std::string& request,
135 const JsonRequestCallback& callback);
136 void SendProtocolCommand(const std::string& debug_url,
137 const std::string& method,
138 base::DictionaryValue* params,
139 const base::Closure callback);
141 void Open(const std::string& url,
142 const TargetCallback& callback);
144 scoped_refptr<AndroidWebSocket> CreateWebSocket(
145 const std::string& url,
146 DevToolsAndroidBridge::AndroidWebSocket::Delegate* delegate);
148 private:
149 friend class base::RefCounted<RemoteBrowser>;
150 virtual ~RemoteBrowser();
152 void InnerOpen(const std::string& url,
153 const JsonRequestCallback& callback);
155 void PageCreatedOnUIThread(
156 const JsonRequestCallback& callback,
157 const std::string& url, int result, const std::string& response);
159 void NavigatePageOnUIThread(const JsonRequestCallback& callback,
160 int result,
161 const std::string& response,
162 const std::string& url);
164 void RespondToOpenOnUIThread(
165 const DevToolsAndroidBridge::TargetCallback& callback,
166 int result,
167 const std::string& response);
169 scoped_refptr<DevToolsAndroidBridge> android_bridge_;
170 const std::string serial_;
171 const std::string socket_;
172 std::string display_name_;
173 std::string version_;
174 scoped_ptr<base::ListValue> page_descriptors_;
176 DISALLOW_COPY_AND_ASSIGN(RemoteBrowser);
179 typedef std::vector<scoped_refptr<RemoteBrowser> > RemoteBrowsers;
181 class RemoteDevice : public base::RefCounted<RemoteDevice> {
182 public:
183 RemoteDevice(scoped_refptr<DevToolsAndroidBridge> android_bridge,
184 const std::string& serial,
185 const std::string& model,
186 bool connected);
188 std::string serial() { return serial_; }
189 std::string model() { return model_; }
190 bool is_connected() { return connected_; }
191 void AddBrowser(scoped_refptr<RemoteBrowser> browser);
193 RemoteBrowsers& browsers() { return browsers_; }
194 gfx::Size screen_size() { return screen_size_; }
195 void set_screen_size(const gfx::Size& size) { screen_size_ = size; }
197 void OpenSocket(const std::string& socket_name,
198 const AndroidDeviceManager::SocketCallback& callback);
200 private:
201 friend class base::RefCounted<RemoteDevice>;
202 virtual ~RemoteDevice();
204 scoped_refptr<DevToolsAndroidBridge> android_bridge_;
205 std::string serial_;
206 std::string model_;
207 bool connected_;
208 RemoteBrowsers browsers_;
209 gfx::Size screen_size_;
211 DISALLOW_COPY_AND_ASSIGN(RemoteDevice);
214 typedef std::vector<scoped_refptr<RemoteDevice> > RemoteDevices;
216 class DeviceListListener {
217 public:
218 virtual void DeviceListChanged(const RemoteDevices& devices) = 0;
219 protected:
220 virtual ~DeviceListListener() {}
223 explicit DevToolsAndroidBridge(Profile* profile);
224 void AddDeviceListListener(DeviceListListener* listener);
225 void RemoveDeviceListListener(DeviceListListener* listener);
227 class DeviceCountListener {
228 public:
229 virtual void DeviceCountChanged(int count) = 0;
230 protected:
231 virtual ~DeviceCountListener() {}
234 void AddDeviceCountListener(DeviceCountListener* listener);
235 void RemoveDeviceCountListener(DeviceCountListener* listener);
237 void set_device_providers_for_test(
238 const AndroidDeviceManager::DeviceProviders& device_providers) {
239 device_providers_ = device_providers;
242 static bool HasDevToolsWindow(const std::string& agent_id);
244 private:
245 friend struct content::BrowserThread::DeleteOnThread<
246 content::BrowserThread::UI>;
247 friend class base::DeleteHelper<DevToolsAndroidBridge>;
249 class HandlerThread : public base::RefCountedThreadSafe<HandlerThread> {
250 public:
251 static scoped_refptr<HandlerThread> GetInstance();
252 base::MessageLoop* message_loop();
254 private:
255 friend class base::RefCountedThreadSafe<HandlerThread>;
256 static HandlerThread* instance_;
257 static void StopThread(base::Thread* thread);
259 HandlerThread();
260 virtual ~HandlerThread();
261 base::Thread* thread_;
264 virtual ~DevToolsAndroidBridge();
266 base::MessageLoop* device_message_loop() {
267 return handler_thread_->message_loop();
270 AndroidDeviceManager* device_manager() {
271 return device_manager_.get();
274 void CreatedDeviceManager(scoped_refptr<AndroidDeviceManager> device_manager);
275 void RequestDeviceList();
276 void ReceivedDeviceList(RemoteDevices* devices);
278 void RequestDeviceCount();
279 void ReceivedDeviceCount(int count);
281 void CreateDeviceProviders();
283 Profile* profile_;
284 scoped_refptr<HandlerThread> handler_thread_;
285 scoped_refptr<AndroidDeviceManager> device_manager_;
287 typedef std::vector<DeviceListListener*> DeviceListListeners;
288 DeviceListListeners device_list_listeners_;
290 typedef std::vector<DeviceCountListener*> DeviceCountListeners;
291 DeviceCountListeners device_count_listeners_;
293 AndroidDeviceManager::DeviceProviders device_providers_;
294 PrefChangeRegistrar pref_change_registrar_;
295 DISALLOW_COPY_AND_ASSIGN(DevToolsAndroidBridge);
298 #endif // CHROME_BROWSER_DEVTOOLS_DEVICE_DEVTOOLS_ANDROID_BRIDGE_H_