Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / devtools / device / devtools_android_bridge.h
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_DEVTOOLS_ANDROID_BRIDGE_H_
6 #define CHROME_BROWSER_DEVTOOLS_DEVICE_DEVTOOLS_ANDROID_BRIDGE_H_
7
8 #include <string>
9 #include <vector>
10
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"
21
22 template<typename T> struct DefaultSingletonTraits;
23
24 namespace base {
25 class MessageLoop;
26 class DictionaryValue;
27 class ListValue;
28 class Thread;
29 }
30
31 namespace content {
32 class BrowserContext;
33 }
34
35 class DevToolsTargetImpl;
36 class PortForwardingController;
37 class Profile;
38
39 class DevToolsAndroidBridge
40     : public base::RefCountedThreadSafe<
41           DevToolsAndroidBridge,
42           content::BrowserThread::DeleteOnUIThread> {
43  public:
44   class Wrapper : public KeyedService {
45    public:
46     explicit Wrapper(content::BrowserContext* context);
47     ~Wrapper() override;
48
49     DevToolsAndroidBridge* Get();
50    private:
51     scoped_refptr<DevToolsAndroidBridge> bridge_;
52   };
53
54   class Factory : public BrowserContextKeyedServiceFactory {
55    public:
56     // Returns singleton instance of DevToolsAndroidBridge.
57     static Factory* GetInstance();
58
59     // Returns DevToolsAndroidBridge associated with |profile|.
60     static DevToolsAndroidBridge* GetForProfile(Profile* profile);
61
62    private:
63     friend struct DefaultSingletonTraits<Factory>;
64
65     Factory();
66     ~Factory() override;
67
68     // BrowserContextKeyedServiceFactory overrides:
69     KeyedService* BuildServiceInstanceFor(
70         content::BrowserContext* context) const override;
71     DISALLOW_COPY_AND_ASSIGN(Factory);
72   };
73
74   typedef std::pair<std::string, std::string> BrowserId;
75
76   class RemotePage : public base::RefCounted<RemotePage> {
77    public:
78     const std::string& serial() { return browser_id_.first; }
79     const std::string& socket() { return browser_id_.second; }
80     const std::string& frontend_url() { return frontend_url_; }
81     bool is_web_view() { return is_web_view_; }
82
83    private:
84     friend class base::RefCounted<RemotePage>;
85     friend class DevToolsAndroidBridge;
86
87     RemotePage(const BrowserId& browser_id,
88                const base::DictionaryValue& dict,
89                bool is_web_view);
90
91     virtual ~RemotePage();
92
93     BrowserId browser_id_;
94     std::string frontend_url_;
95     bool is_web_view_;
96     scoped_ptr<base::DictionaryValue> dict_;
97
98     DISALLOW_COPY_AND_ASSIGN(RemotePage);
99   };
100
101   typedef std::vector<scoped_refptr<RemotePage> > RemotePages;
102   typedef base::Callback<void(int, const std::string&)> JsonRequestCallback;
103
104   class RemoteBrowser : public base::RefCounted<RemoteBrowser> {
105    public:
106     const std::string& serial() { return browser_id_.first; }
107     const std::string& socket() { return browser_id_.second; }
108     const std::string& display_name() { return display_name_; }
109     const std::string& version() { return version_; }
110     const RemotePages& pages() { return pages_; }
111
112     bool IsChrome();
113     bool IsWebView();
114
115     typedef std::vector<int> ParsedVersion;
116     ParsedVersion GetParsedVersion();
117
118    private:
119     friend class base::RefCounted<RemoteBrowser>;
120     friend class DevToolsAndroidBridge;
121
122     RemoteBrowser(const std::string& serial,
123                   const AndroidDeviceManager::BrowserInfo& browser_info);
124
125     virtual ~RemoteBrowser();
126
127     BrowserId browser_id_;
128     std::string display_name_;
129     AndroidDeviceManager::BrowserInfo::Type type_;
130     std::string version_;
131     RemotePages pages_;
132
133     DISALLOW_COPY_AND_ASSIGN(RemoteBrowser);
134   };
135
136   typedef std::vector<scoped_refptr<RemoteBrowser> > RemoteBrowsers;
137
138   class RemoteDevice : public base::RefCounted<RemoteDevice> {
139    public:
140     std::string serial() { return serial_; }
141     std::string model() { return model_; }
142     bool is_connected() { return connected_; }
143     RemoteBrowsers& browsers() { return browsers_; }
144     gfx::Size screen_size() { return screen_size_; }
145
146    private:
147     friend class base::RefCounted<RemoteDevice>;
148     friend class DevToolsAndroidBridge;
149
150     RemoteDevice(const std::string& serial,
151                  const AndroidDeviceManager::DeviceInfo& device_info);
152
153     virtual ~RemoteDevice();
154
155     std::string serial_;
156     std::string model_;
157     bool connected_;
158     RemoteBrowsers browsers_;
159     gfx::Size screen_size_;
160
161     DISALLOW_COPY_AND_ASSIGN(RemoteDevice);
162   };
163
164   typedef std::vector<scoped_refptr<RemoteDevice> > RemoteDevices;
165
166   class DeviceListListener {
167    public:
168     virtual void DeviceListChanged(const RemoteDevices& devices) = 0;
169    protected:
170     virtual ~DeviceListListener() {}
171   };
172
173   explicit DevToolsAndroidBridge(Profile* profile);
174   void AddDeviceListListener(DeviceListListener* listener);
175   void RemoveDeviceListListener(DeviceListListener* listener);
176
177   class DeviceCountListener {
178    public:
179     virtual void DeviceCountChanged(int count) = 0;
180    protected:
181     virtual ~DeviceCountListener() {}
182   };
183
184   void AddDeviceCountListener(DeviceCountListener* listener);
185   void RemoveDeviceCountListener(DeviceCountListener* listener);
186
187   typedef int PortStatus;
188   typedef std::map<int, PortStatus> PortStatusMap;
189   typedef std::pair<scoped_refptr<RemoteBrowser>, PortStatusMap>
190       BrowserStatus;
191   typedef std::vector<BrowserStatus> ForwardingStatus;
192
193   class PortForwardingListener {
194    public:
195     typedef DevToolsAndroidBridge::PortStatusMap PortStatusMap;
196     typedef DevToolsAndroidBridge::BrowserStatus BrowserStatus;
197     typedef DevToolsAndroidBridge::ForwardingStatus ForwardingStatus;
198
199     virtual void PortStatusChanged(const ForwardingStatus&) = 0;
200    protected:
201     virtual ~PortForwardingListener() {}
202   };
203
204   void AddPortForwardingListener(PortForwardingListener* listener);
205   void RemovePortForwardingListener(PortForwardingListener* listener);
206
207   void set_device_providers_for_test(
208       const AndroidDeviceManager::DeviceProviders& device_providers) {
209     device_manager_->SetDeviceProviders(device_providers);
210   }
211
212   void set_task_scheduler_for_test(
213       base::Callback<void(const base::Closure&)> scheduler) {
214     task_scheduler_ = scheduler;
215   }
216
217   bool HasDevToolsWindow(const std::string& agent_id);
218
219   // Creates new target instance owned by caller.
220   DevToolsTargetImpl* CreatePageTarget(scoped_refptr<RemotePage> browser);
221
222   typedef base::Callback<void(scoped_refptr<RemotePage>)> RemotePageCallback;
223   void OpenRemotePage(scoped_refptr<RemoteBrowser> browser,
224                       const std::string& url,
225                       const RemotePageCallback& callback);
226
227   scoped_refptr<content::DevToolsAgentHost> GetBrowserAgentHost(
228       scoped_refptr<RemoteBrowser> browser);
229  private:
230   friend struct content::BrowserThread::DeleteOnThread<
231       content::BrowserThread::UI>;
232   friend class base::DeleteHelper<DevToolsAndroidBridge>;
233
234   friend class PortForwardingController;
235
236   class AgentHostDelegate;
237   class DiscoveryRequest;
238   class RemotePageTarget;
239
240   virtual ~DevToolsAndroidBridge();
241
242   void StartDeviceListPolling();
243   void StopDeviceListPolling();
244   bool NeedsDeviceListPolling();
245
246   typedef std::pair<scoped_refptr<AndroidDeviceManager::Device>,
247                     scoped_refptr<RemoteDevice>> CompleteDevice;
248   typedef std::vector<CompleteDevice> CompleteDevices;
249   typedef base::Callback<void(const CompleteDevices&)> DeviceListCallback;
250
251   void RequestDeviceList(const DeviceListCallback& callback);
252   void ReceivedDeviceList(const CompleteDevices& complete_devices);
253
254   void StartDeviceCountPolling();
255   void StopDeviceCountPolling();
256   void RequestDeviceCount(const base::Callback<void(int)>& callback);
257   void ReceivedDeviceCount(int count);
258
259   static void ScheduleTaskDefault(const base::Closure& task);
260
261   void CreateDeviceProviders();
262
263   void SendJsonRequest(const BrowserId& browser_id,
264                        const std::string& url,
265                        const JsonRequestCallback& callback);
266
267   void SendProtocolCommand(const BrowserId& browser_id,
268                            const std::string& debug_url,
269                            const std::string& method,
270                            base::DictionaryValue* params,
271                            const base::Closure callback);
272
273   scoped_refptr<AndroidDeviceManager::Device> FindDevice(
274       const std::string& serial);
275
276   void PageCreatedOnUIThread(scoped_refptr<RemoteBrowser> browser,
277                              const RemotePageCallback& callback,
278                              const std::string& url,
279                              int result,
280                              const std::string& response);
281
282   void NavigatePageOnUIThread(scoped_refptr<RemoteBrowser> browser,
283                               const RemotePageCallback& callback,
284                               int result,
285                               const std::string& response,
286                               const std::string& url);
287
288   void RespondToOpenOnUIThread(scoped_refptr<RemoteBrowser> browser,
289                                const RemotePageCallback& callback,
290                                int result,
291                                const std::string& response);
292
293   Profile* profile_;
294   scoped_refptr<AndroidDeviceManager> device_manager_;
295
296   typedef std::map<std::string, scoped_refptr<AndroidDeviceManager::Device>>
297       DeviceMap;
298   DeviceMap device_map_;
299
300   typedef std::map<std::string, AgentHostDelegate*> AgentHostDelegates;
301   AgentHostDelegates host_delegates_;
302
303   typedef std::vector<DeviceListListener*> DeviceListListeners;
304   DeviceListListeners device_list_listeners_;
305   base::CancelableCallback<void(const CompleteDevices&)> device_list_callback_;
306
307   typedef std::vector<DeviceCountListener*> DeviceCountListeners;
308   DeviceCountListeners device_count_listeners_;
309   base::CancelableCallback<void(int)> device_count_callback_;
310   base::Callback<void(const base::Closure&)> task_scheduler_;
311
312   typedef std::vector<PortForwardingListener*> PortForwardingListeners;
313   PortForwardingListeners port_forwarding_listeners_;
314   scoped_ptr<PortForwardingController> port_forwarding_controller_;
315
316   PrefChangeRegistrar pref_change_registrar_;
317   DISALLOW_COPY_AND_ASSIGN(DevToolsAndroidBridge);
318 };
319
320 #endif  // CHROME_BROWSER_DEVTOOLS_DEVICE_DEVTOOLS_ANDROID_BRIDGE_H_