b9cbb76a23b891bee0502bc7210bc109680eb6c1
[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 Profile;
37
38 class DevToolsAndroidBridge
39     : public base::RefCountedThreadSafe<
40           DevToolsAndroidBridge,
41           content::BrowserThread::DeleteOnUIThread> {
42  public:
43   typedef base::Callback<void(int result,
44                               const std::string& response)> Callback;
45
46   class Wrapper : public KeyedService {
47    public:
48     explicit Wrapper(content::BrowserContext* context);
49     virtual ~Wrapper();
50
51     DevToolsAndroidBridge* Get();
52    private:
53     scoped_refptr<DevToolsAndroidBridge> bridge_;
54   };
55
56   class Factory : public BrowserContextKeyedServiceFactory {
57    public:
58     // Returns singleton instance of DevToolsAndroidBridge.
59     static Factory* GetInstance();
60
61     // Returns DevToolsAndroidBridge associated with |profile|.
62     static DevToolsAndroidBridge* GetForProfile(Profile* profile);
63
64    private:
65     friend struct DefaultSingletonTraits<Factory>;
66
67     Factory();
68     virtual ~Factory();
69
70     // BrowserContextKeyedServiceFactory overrides:
71     virtual KeyedService* BuildServiceInstanceFor(
72         content::BrowserContext* context) const OVERRIDE;
73     DISALLOW_COPY_AND_ASSIGN(Factory);
74   };
75
76   class RemotePage {
77    public:
78     virtual ~RemotePage() {}
79     virtual DevToolsTargetImpl* GetTarget() = 0;
80     virtual std::string GetFrontendURL() = 0;
81   };
82
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;
87
88   class RemoteBrowser : public base::RefCounted<RemoteBrowser> {
89    public:
90     RemoteBrowser(scoped_refptr<Device> device,
91                   const AndroidDeviceManager::BrowserInfo& browser_info);
92
93     std::string serial() { return device_->serial(); }
94     std::string socket() { return socket_; }
95
96     std::string display_name() { return display_name_; }
97     void set_display_name(const std::string& name) { display_name_ = name; }
98
99     std::string version() { return version_; }
100     void set_version(const std::string& version) { version_ = version; }
101
102     bool IsChrome() const;
103     bool IsWebView() const;
104
105     typedef std::vector<int> ParsedVersion;
106     ParsedVersion GetParsedVersion() const;
107
108     std::vector<RemotePage*> CreatePages();
109     void SetPageDescriptors(const base::ListValue&);
110
111     void SendJsonRequest(const std::string& request,
112                          const JsonRequestCallback& callback);
113
114     void SendProtocolCommand(const std::string& debug_url,
115                              const std::string& method,
116                              base::DictionaryValue* params,
117                              const base::Closure callback);
118
119     void Open(const std::string& url,
120               const RemotePageCallback& callback);
121
122     scoped_refptr<content::DevToolsAgentHost> GetAgentHost();
123
124     AndroidWebSocket* CreateWebSocket(
125         const std::string& url,
126         DevToolsAndroidBridge::AndroidWebSocket::Delegate* delegate);
127
128    private:
129     friend class base::RefCounted<RemoteBrowser>;
130     virtual ~RemoteBrowser();
131
132     void InnerOpen(const std::string& url,
133                    const JsonRequestCallback& callback);
134
135     void PageCreatedOnUIThread(
136         const JsonRequestCallback& callback,
137         const std::string& url, int result, const std::string& response);
138
139     void NavigatePageOnUIThread(const JsonRequestCallback& callback,
140         int result,
141         const std::string& response,
142         const std::string& url);
143
144     void RespondToOpenOnUIThread(
145         const DevToolsAndroidBridge::RemotePageCallback& callback,
146         int result,
147         const std::string& response);
148
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_;
155
156     DISALLOW_COPY_AND_ASSIGN(RemoteBrowser);
157   };
158
159   typedef std::vector<scoped_refptr<RemoteBrowser> > RemoteBrowsers;
160
161   class RemoteDevice : public base::RefCounted<RemoteDevice> {
162    public:
163     RemoteDevice(scoped_refptr<Device> device,
164                  const AndroidDeviceManager::DeviceInfo& device_info);
165
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_; }
171
172     void OpenSocket(const std::string& socket_name,
173                     const AndroidDeviceManager::SocketCallback& callback);
174
175    private:
176     friend class base::RefCounted<RemoteDevice>;
177     virtual ~RemoteDevice();
178
179     scoped_refptr<Device> device_;
180     std::string model_;
181     bool connected_;
182     RemoteBrowsers browsers_;
183     gfx::Size screen_size_;
184
185     DISALLOW_COPY_AND_ASSIGN(RemoteDevice);
186   };
187
188   typedef std::vector<scoped_refptr<RemoteDevice> > RemoteDevices;
189
190   class DeviceListListener {
191    public:
192     virtual void DeviceListChanged(const RemoteDevices& devices) = 0;
193    protected:
194     virtual ~DeviceListListener() {}
195   };
196
197   explicit DevToolsAndroidBridge(Profile* profile);
198   void AddDeviceListListener(DeviceListListener* listener);
199   void RemoveDeviceListListener(DeviceListListener* listener);
200
201   class DeviceCountListener {
202    public:
203     virtual void DeviceCountChanged(int count) = 0;
204    protected:
205     virtual ~DeviceCountListener() {}
206   };
207
208   void AddDeviceCountListener(DeviceCountListener* listener);
209   void RemoveDeviceCountListener(DeviceCountListener* listener);
210
211   void set_device_providers_for_test(
212       const AndroidDeviceManager::DeviceProviders& device_providers) {
213     device_manager_->SetDeviceProviders(device_providers);
214   }
215
216   void set_task_scheduler_for_test(
217       base::Callback<void(const base::Closure&)> scheduler) {
218     task_scheduler_ = scheduler;
219   }
220
221   static bool HasDevToolsWindow(const std::string& agent_id);
222
223  private:
224   friend struct content::BrowserThread::DeleteOnThread<
225       content::BrowserThread::UI>;
226   friend class base::DeleteHelper<DevToolsAndroidBridge>;
227
228   virtual ~DevToolsAndroidBridge();
229
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);
239
240   static void ScheduleTaskDefault(const base::Closure& task);
241
242   void CreateDeviceProviders();
243
244   Profile* profile_;
245   scoped_refptr<AndroidDeviceManager> device_manager_;
246   RemoteDevices devices_;
247
248   typedef std::vector<DeviceListListener*> DeviceListListeners;
249   DeviceListListeners device_list_listeners_;
250   base::CancelableCallback<void(const RemoteDevices&)> device_list_callback_;
251
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_;
256
257   PrefChangeRegistrar pref_change_registrar_;
258   DISALLOW_COPY_AND_ASSIGN(DevToolsAndroidBridge);
259 };
260
261 #endif  // CHROME_BROWSER_DEVTOOLS_DEVICE_DEVTOOLS_ANDROID_BRIDGE_H_