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_
11 #include "base/callback.h"
12 #include "base/cancelable_callback.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/prefs/pref_change_registrar.h"
16 #include "chrome/browser/devtools/device/android_device_manager.h"
17 #include "components/keyed_service/content/browser_context_keyed_service_factory.h"
18 #include "components/keyed_service/core/keyed_service.h"
19 #include "content/public/browser/devtools_agent_host.h"
20 #include "ui/gfx/size.h"
22 template<typename T> struct DefaultSingletonTraits;
26 class DictionaryValue;
35 class DevToolsTargetImpl;
38 class DevToolsAndroidBridge
39 : public base::RefCountedThreadSafe<
40 DevToolsAndroidBridge,
41 content::BrowserThread::DeleteOnUIThread> {
43 typedef base::Callback<void(int result,
44 const std::string& response)> Callback;
46 class Wrapper : public KeyedService {
48 explicit Wrapper(content::BrowserContext* context);
51 DevToolsAndroidBridge* Get();
53 scoped_refptr<DevToolsAndroidBridge> bridge_;
56 class Factory : public BrowserContextKeyedServiceFactory {
58 // Returns singleton instance of DevToolsAndroidBridge.
59 static Factory* GetInstance();
61 // Returns DevToolsAndroidBridge associated with |profile|.
62 static DevToolsAndroidBridge* GetForProfile(Profile* profile);
65 friend struct DefaultSingletonTraits<Factory>;
70 // BrowserContextKeyedServiceFactory overrides:
71 virtual KeyedService* BuildServiceInstanceFor(
72 content::BrowserContext* context) const OVERRIDE;
73 DISALLOW_COPY_AND_ASSIGN(Factory);
78 virtual ~RemotePage() {}
79 virtual DevToolsTargetImpl* GetTarget() = 0;
80 virtual std::string GetFrontendURL() = 0;
83 typedef base::Callback<void(RemotePage*)> RemotePageCallback;
84 typedef base::Callback<void(int, const std::string&)> JsonRequestCallback;
85 typedef AndroidDeviceManager::Device Device;
86 typedef AndroidDeviceManager::AndroidWebSocket AndroidWebSocket;
88 class RemoteBrowser : public base::RefCounted<RemoteBrowser> {
90 RemoteBrowser(scoped_refptr<Device> device,
91 const AndroidDeviceManager::BrowserInfo& browser_info);
93 std::string serial() { return device_->serial(); }
94 std::string socket() { return socket_; }
96 std::string display_name() { return display_name_; }
97 void set_display_name(const std::string& name) { display_name_ = name; }
99 std::string version() { return version_; }
100 void set_version(const std::string& version) { version_ = version; }
102 bool IsChrome() const;
103 bool IsWebView() const;
105 typedef std::vector<int> ParsedVersion;
106 ParsedVersion GetParsedVersion() const;
108 std::vector<RemotePage*> CreatePages();
109 void SetPageDescriptors(const base::ListValue&);
111 void SendJsonRequest(const std::string& request,
112 const JsonRequestCallback& callback);
114 void SendProtocolCommand(const std::string& debug_url,
115 const std::string& method,
116 base::DictionaryValue* params,
117 const base::Closure callback);
119 void Open(const std::string& url,
120 const RemotePageCallback& callback);
122 scoped_refptr<content::DevToolsAgentHost> GetAgentHost();
124 AndroidWebSocket* CreateWebSocket(
125 const std::string& url,
126 DevToolsAndroidBridge::AndroidWebSocket::Delegate* delegate);
129 friend class base::RefCounted<RemoteBrowser>;
130 virtual ~RemoteBrowser();
132 void InnerOpen(const std::string& url,
133 const JsonRequestCallback& callback);
135 void PageCreatedOnUIThread(
136 const JsonRequestCallback& callback,
137 const std::string& url, int result, const std::string& response);
139 void NavigatePageOnUIThread(const JsonRequestCallback& callback,
141 const std::string& response,
142 const std::string& url);
144 void RespondToOpenOnUIThread(
145 const DevToolsAndroidBridge::RemotePageCallback& callback,
147 const std::string& response);
149 scoped_refptr<Device> device_;
150 const std::string socket_;
151 std::string display_name_;
152 const AndroidDeviceManager::BrowserInfo::Type type_;
153 std::string version_;
154 scoped_ptr<base::ListValue> page_descriptors_;
156 DISALLOW_COPY_AND_ASSIGN(RemoteBrowser);
159 typedef std::vector<scoped_refptr<RemoteBrowser> > RemoteBrowsers;
161 class RemoteDevice : public base::RefCounted<RemoteDevice> {
163 RemoteDevice(scoped_refptr<Device> device,
164 const AndroidDeviceManager::DeviceInfo& device_info);
166 std::string serial() { return device_->serial(); }
167 std::string model() { return model_; }
168 bool is_connected() { return connected_; }
169 RemoteBrowsers& browsers() { return browsers_; }
170 gfx::Size screen_size() { return screen_size_; }
172 void OpenSocket(const std::string& socket_name,
173 const AndroidDeviceManager::SocketCallback& callback);
176 friend class base::RefCounted<RemoteDevice>;
177 virtual ~RemoteDevice();
179 scoped_refptr<Device> device_;
182 RemoteBrowsers browsers_;
183 gfx::Size screen_size_;
185 DISALLOW_COPY_AND_ASSIGN(RemoteDevice);
188 typedef std::vector<scoped_refptr<RemoteDevice> > RemoteDevices;
190 class DeviceListListener {
192 virtual void DeviceListChanged(const RemoteDevices& devices) = 0;
194 virtual ~DeviceListListener() {}
197 explicit DevToolsAndroidBridge(Profile* profile);
198 void AddDeviceListListener(DeviceListListener* listener);
199 void RemoveDeviceListListener(DeviceListListener* listener);
201 class DeviceCountListener {
203 virtual void DeviceCountChanged(int count) = 0;
205 virtual ~DeviceCountListener() {}
208 void AddDeviceCountListener(DeviceCountListener* listener);
209 void RemoveDeviceCountListener(DeviceCountListener* listener);
211 void set_device_providers_for_test(
212 const AndroidDeviceManager::DeviceProviders& device_providers) {
213 device_manager_->SetDeviceProviders(device_providers);
216 void set_task_scheduler_for_test(
217 base::Callback<void(const base::Closure&)> scheduler) {
218 task_scheduler_ = scheduler;
221 static bool HasDevToolsWindow(const std::string& agent_id);
224 friend struct content::BrowserThread::DeleteOnThread<
225 content::BrowserThread::UI>;
226 friend class base::DeleteHelper<DevToolsAndroidBridge>;
228 virtual ~DevToolsAndroidBridge();
230 void StartDeviceListPolling();
231 void StopDeviceListPolling();
232 void RequestDeviceList(
233 const base::Callback<void(const RemoteDevices&)>& callback);
234 void ReceivedDeviceList(const RemoteDevices& devices);
235 void StartDeviceCountPolling();
236 void StopDeviceCountPolling();
237 void RequestDeviceCount(const base::Callback<void(int)>& callback);
238 void ReceivedDeviceCount(int count);
240 static void ScheduleTaskDefault(const base::Closure& task);
242 void CreateDeviceProviders();
245 scoped_refptr<AndroidDeviceManager> device_manager_;
246 RemoteDevices devices_;
248 typedef std::vector<DeviceListListener*> DeviceListListeners;
249 DeviceListListeners device_list_listeners_;
250 base::CancelableCallback<void(const RemoteDevices&)> device_list_callback_;
252 typedef std::vector<DeviceCountListener*> DeviceCountListeners;
253 DeviceCountListeners device_count_listeners_;
254 base::CancelableCallback<void(int)> device_count_callback_;
255 base::Callback<void(const base::Closure&)> task_scheduler_;
257 PrefChangeRegistrar pref_change_registrar_;
258 DISALLOW_COPY_AND_ASSIGN(DevToolsAndroidBridge);
261 #endif // CHROME_BROWSER_DEVTOOLS_DEVICE_DEVTOOLS_ANDROID_BRIDGE_H_