Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / devtools / device / devtools_android_bridge.h
blob3b989a3a0a1d9b0827f5b94dc0efce0dbe45db9f
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/cancelable_callback.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/memory/weak_ptr.h"
16 #include "base/prefs/pref_change_registrar.h"
17 #include "chrome/browser/devtools/device/android_device_manager.h"
18 #include "components/keyed_service/content/browser_context_keyed_service_factory.h"
19 #include "components/keyed_service/core/keyed_service.h"
20 #include "content/public/browser/devtools_agent_host.h"
21 #include "ui/gfx/geometry/size.h"
23 namespace base {
24 template<typename T> struct DefaultSingletonTraits;
26 class MessageLoop;
27 class DictionaryValue;
28 class ListValue;
29 class Thread;
30 } // namespace base
32 namespace content {
33 class BrowserContext;
36 class DevToolsTargetImpl;
37 class PortForwardingController;
38 class Profile;
39 class WebRTCDeviceProvider;
40 class SigninManagerBase;
41 class ProfileOAuth2TokenService;
43 class DevToolsAndroidBridge : public KeyedService {
44 public:
45 class Factory : public BrowserContextKeyedServiceFactory {
46 public:
47 // Returns singleton instance of DevToolsAndroidBridge.
48 static Factory* GetInstance();
50 // Returns DevToolsAndroidBridge associated with |profile|.
51 static DevToolsAndroidBridge* GetForProfile(Profile* profile);
53 private:
54 friend struct base::DefaultSingletonTraits<Factory>;
56 Factory();
57 ~Factory() override;
59 // BrowserContextKeyedServiceFactory overrides:
60 KeyedService* BuildServiceInstanceFor(
61 content::BrowserContext* context) const override;
62 DISALLOW_COPY_AND_ASSIGN(Factory);
65 typedef std::pair<std::string, std::string> BrowserId;
67 class RemotePage : public base::RefCounted<RemotePage> {
68 public:
69 const std::string& serial() { return browser_id_.first; }
70 const std::string& socket() { return browser_id_.second; }
71 const std::string& frontend_url() { return frontend_url_; }
73 private:
74 friend class base::RefCounted<RemotePage>;
75 friend class DevToolsAndroidBridge;
77 RemotePage(const BrowserId& browser_id, const base::DictionaryValue& dict);
79 virtual ~RemotePage();
81 BrowserId browser_id_;
82 std::string frontend_url_;
83 scoped_ptr<base::DictionaryValue> dict_;
85 DISALLOW_COPY_AND_ASSIGN(RemotePage);
88 typedef std::vector<scoped_refptr<RemotePage> > RemotePages;
89 typedef base::Callback<void(int, const std::string&)> JsonRequestCallback;
91 class RemoteBrowser : public base::RefCounted<RemoteBrowser> {
92 public:
93 const std::string& serial() { return browser_id_.first; }
94 const std::string& socket() { return browser_id_.second; }
95 const std::string& display_name() { return display_name_; }
96 const std::string& user() { return user_; }
97 const std::string& version() { return version_; }
98 const RemotePages& pages() { return pages_; }
100 bool IsChrome();
101 std::string GetId();
103 typedef std::vector<int> ParsedVersion;
104 ParsedVersion GetParsedVersion();
106 private:
107 friend class base::RefCounted<RemoteBrowser>;
108 friend class DevToolsAndroidBridge;
110 RemoteBrowser(const std::string& serial,
111 const AndroidDeviceManager::BrowserInfo& browser_info);
113 virtual ~RemoteBrowser();
115 BrowserId browser_id_;
116 std::string display_name_;
117 std::string user_;
118 AndroidDeviceManager::BrowserInfo::Type type_;
119 std::string version_;
120 RemotePages pages_;
122 DISALLOW_COPY_AND_ASSIGN(RemoteBrowser);
125 typedef std::vector<scoped_refptr<RemoteBrowser> > RemoteBrowsers;
127 class RemoteDevice : public base::RefCounted<RemoteDevice> {
128 public:
129 std::string serial() { return serial_; }
130 std::string model() { return model_; }
131 bool is_connected() { return connected_; }
132 RemoteBrowsers& browsers() { return browsers_; }
133 gfx::Size screen_size() { return screen_size_; }
135 private:
136 friend class base::RefCounted<RemoteDevice>;
137 friend class DevToolsAndroidBridge;
139 RemoteDevice(const std::string& serial,
140 const AndroidDeviceManager::DeviceInfo& device_info);
142 virtual ~RemoteDevice();
144 std::string serial_;
145 std::string model_;
146 bool connected_;
147 RemoteBrowsers browsers_;
148 gfx::Size screen_size_;
150 DISALLOW_COPY_AND_ASSIGN(RemoteDevice);
153 typedef std::vector<scoped_refptr<RemoteDevice> > RemoteDevices;
155 class DeviceListListener {
156 public:
157 virtual void DeviceListChanged(const RemoteDevices& devices) = 0;
158 protected:
159 virtual ~DeviceListListener() {}
162 DevToolsAndroidBridge(Profile* profile,
163 SigninManagerBase* signin_manager,
164 ProfileOAuth2TokenService* token_service);
165 void AddDeviceListListener(DeviceListListener* listener);
166 void RemoveDeviceListListener(DeviceListListener* listener);
168 class DeviceCountListener {
169 public:
170 virtual void DeviceCountChanged(int count) = 0;
171 protected:
172 virtual ~DeviceCountListener() {}
175 void AddDeviceCountListener(DeviceCountListener* listener);
176 void RemoveDeviceCountListener(DeviceCountListener* listener);
178 typedef int PortStatus;
179 typedef std::map<int, PortStatus> PortStatusMap;
180 typedef std::pair<scoped_refptr<RemoteBrowser>, PortStatusMap>
181 BrowserStatus;
182 typedef std::vector<BrowserStatus> ForwardingStatus;
184 class PortForwardingListener {
185 public:
186 typedef DevToolsAndroidBridge::PortStatusMap PortStatusMap;
187 typedef DevToolsAndroidBridge::BrowserStatus BrowserStatus;
188 typedef DevToolsAndroidBridge::ForwardingStatus ForwardingStatus;
190 virtual void PortStatusChanged(const ForwardingStatus&) = 0;
191 protected:
192 virtual ~PortForwardingListener() {}
195 void AddPortForwardingListener(PortForwardingListener* listener);
196 void RemovePortForwardingListener(PortForwardingListener* listener);
198 void set_device_providers_for_test(
199 const AndroidDeviceManager::DeviceProviders& device_providers) {
200 device_manager_->SetDeviceProviders(device_providers);
203 void set_task_scheduler_for_test(
204 base::Callback<void(const base::Closure&)> scheduler) {
205 task_scheduler_ = scheduler;
208 bool HasDevToolsWindow(const std::string& agent_id);
210 // Creates new target instance owned by caller.
211 DevToolsTargetImpl* CreatePageTarget(scoped_refptr<RemotePage> browser);
213 typedef base::Callback<void(scoped_refptr<RemotePage>)> RemotePageCallback;
214 void OpenRemotePage(scoped_refptr<RemoteBrowser> browser,
215 const std::string& url);
217 scoped_refptr<content::DevToolsAgentHost> GetBrowserAgentHost(
218 scoped_refptr<RemoteBrowser> browser);
220 void SendJsonRequest(const std::string& browser_id_str,
221 const std::string& url,
222 const JsonRequestCallback& callback);
224 private:
225 friend struct content::BrowserThread::DeleteOnThread<
226 content::BrowserThread::UI>;
227 friend class base::DeleteHelper<DevToolsAndroidBridge>;
229 friend class PortForwardingController;
231 class AgentHostDelegate;
232 class DiscoveryRequest;
233 class RemotePageTarget;
235 ~DevToolsAndroidBridge() override;
237 void StartDeviceListPolling();
238 void StopDeviceListPolling();
239 bool NeedsDeviceListPolling();
241 typedef std::pair<scoped_refptr<AndroidDeviceManager::Device>,
242 scoped_refptr<RemoteDevice>> CompleteDevice;
243 typedef std::vector<CompleteDevice> CompleteDevices;
244 typedef base::Callback<void(const CompleteDevices&)> DeviceListCallback;
246 void RequestDeviceList(const DeviceListCallback& callback);
247 void ReceivedDeviceList(const CompleteDevices& complete_devices);
249 void StartDeviceCountPolling();
250 void StopDeviceCountPolling();
251 void RequestDeviceCount(const base::Callback<void(int)>& callback);
252 void ReceivedDeviceCount(int count);
254 static void ScheduleTaskDefault(const base::Closure& task);
256 void CreateDeviceProviders();
258 void SendJsonRequest(const BrowserId& browser_id,
259 const std::string& url,
260 const JsonRequestCallback& callback);
262 void SendProtocolCommand(const BrowserId& browser_id,
263 const std::string& target_path,
264 const std::string& method,
265 scoped_ptr<base::DictionaryValue> params,
266 const base::Closure callback);
268 scoped_refptr<AndroidDeviceManager::Device> FindDevice(
269 const std::string& serial);
271 base::WeakPtr<DevToolsAndroidBridge> AsWeakPtr() {
272 return weak_factory_.GetWeakPtr();
275 Profile* const profile_;
276 SigninManagerBase* const signin_manager_;
277 ProfileOAuth2TokenService* const token_service_;
278 const scoped_ptr<AndroidDeviceManager> device_manager_;
280 typedef std::map<std::string, scoped_refptr<AndroidDeviceManager::Device>>
281 DeviceMap;
282 DeviceMap device_map_;
284 typedef std::map<std::string, AgentHostDelegate*> AgentHostDelegates;
285 AgentHostDelegates host_delegates_;
287 typedef std::vector<DeviceListListener*> DeviceListListeners;
288 DeviceListListeners device_list_listeners_;
289 base::CancelableCallback<void(const CompleteDevices&)> device_list_callback_;
291 typedef std::vector<DeviceCountListener*> DeviceCountListeners;
292 DeviceCountListeners device_count_listeners_;
293 base::CancelableCallback<void(int)> device_count_callback_;
294 base::Callback<void(const base::Closure&)> task_scheduler_;
296 typedef std::vector<PortForwardingListener*> PortForwardingListeners;
297 PortForwardingListeners port_forwarding_listeners_;
298 scoped_ptr<PortForwardingController> port_forwarding_controller_;
300 PrefChangeRegistrar pref_change_registrar_;
302 base::WeakPtrFactory<DevToolsAndroidBridge> weak_factory_;
304 DISALLOW_COPY_AND_ASSIGN(DevToolsAndroidBridge);
307 #endif // CHROME_BROWSER_DEVTOOLS_DEVICE_DEVTOOLS_ANDROID_BRIDGE_H_