Add more checks to investigate SupervisedUserPrefStore crash at startup.
[chromium-blink-merge.git] / chrome / browser / devtools / device / devtools_android_bridge.h
blobfde14f55f5f8acd9c3dfc80d143bffc430e0e160
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 template<typename T> struct DefaultSingletonTraits;
25 namespace base {
26 class MessageLoop;
27 class DictionaryValue;
28 class ListValue;
29 class Thread;
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 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_; }
72 bool is_web_view() { return is_web_view_; }
74 private:
75 friend class base::RefCounted<RemotePage>;
76 friend class DevToolsAndroidBridge;
78 RemotePage(const BrowserId& browser_id,
79 const base::DictionaryValue& dict,
80 bool is_web_view);
82 virtual ~RemotePage();
84 BrowserId browser_id_;
85 std::string frontend_url_;
86 bool is_web_view_;
87 scoped_ptr<base::DictionaryValue> dict_;
89 DISALLOW_COPY_AND_ASSIGN(RemotePage);
92 typedef std::vector<scoped_refptr<RemotePage> > RemotePages;
93 typedef base::Callback<void(int, const std::string&)> JsonRequestCallback;
95 class RemoteBrowser : public base::RefCounted<RemoteBrowser> {
96 public:
97 const std::string& serial() { return browser_id_.first; }
98 const std::string& socket() { return browser_id_.second; }
99 const std::string& display_name() { return display_name_; }
100 const std::string& user() { return user_; }
101 const std::string& version() { return version_; }
102 const RemotePages& pages() { return pages_; }
104 bool IsChrome();
105 bool IsWebView();
107 typedef std::vector<int> ParsedVersion;
108 ParsedVersion GetParsedVersion();
110 private:
111 friend class base::RefCounted<RemoteBrowser>;
112 friend class DevToolsAndroidBridge;
114 RemoteBrowser(const std::string& serial,
115 const AndroidDeviceManager::BrowserInfo& browser_info);
117 virtual ~RemoteBrowser();
119 BrowserId browser_id_;
120 std::string display_name_;
121 std::string user_;
122 AndroidDeviceManager::BrowserInfo::Type type_;
123 std::string version_;
124 RemotePages pages_;
126 DISALLOW_COPY_AND_ASSIGN(RemoteBrowser);
129 typedef std::vector<scoped_refptr<RemoteBrowser> > RemoteBrowsers;
131 class RemoteDevice : public base::RefCounted<RemoteDevice> {
132 public:
133 std::string serial() { return serial_; }
134 std::string model() { return model_; }
135 bool is_connected() { return connected_; }
136 RemoteBrowsers& browsers() { return browsers_; }
137 gfx::Size screen_size() { return screen_size_; }
139 private:
140 friend class base::RefCounted<RemoteDevice>;
141 friend class DevToolsAndroidBridge;
143 RemoteDevice(const std::string& serial,
144 const AndroidDeviceManager::DeviceInfo& device_info);
146 virtual ~RemoteDevice();
148 std::string serial_;
149 std::string model_;
150 bool connected_;
151 RemoteBrowsers browsers_;
152 gfx::Size screen_size_;
154 DISALLOW_COPY_AND_ASSIGN(RemoteDevice);
157 typedef std::vector<scoped_refptr<RemoteDevice> > RemoteDevices;
159 class DeviceListListener {
160 public:
161 virtual void DeviceListChanged(const RemoteDevices& devices) = 0;
162 protected:
163 virtual ~DeviceListListener() {}
166 DevToolsAndroidBridge(Profile* profile,
167 SigninManagerBase* signin_manager,
168 ProfileOAuth2TokenService* token_service);
169 void AddDeviceListListener(DeviceListListener* listener);
170 void RemoveDeviceListListener(DeviceListListener* listener);
172 class DeviceCountListener {
173 public:
174 virtual void DeviceCountChanged(int count) = 0;
175 protected:
176 virtual ~DeviceCountListener() {}
179 void AddDeviceCountListener(DeviceCountListener* listener);
180 void RemoveDeviceCountListener(DeviceCountListener* listener);
182 typedef int PortStatus;
183 typedef std::map<int, PortStatus> PortStatusMap;
184 typedef std::pair<scoped_refptr<RemoteBrowser>, PortStatusMap>
185 BrowserStatus;
186 typedef std::vector<BrowserStatus> ForwardingStatus;
188 class PortForwardingListener {
189 public:
190 typedef DevToolsAndroidBridge::PortStatusMap PortStatusMap;
191 typedef DevToolsAndroidBridge::BrowserStatus BrowserStatus;
192 typedef DevToolsAndroidBridge::ForwardingStatus ForwardingStatus;
194 virtual void PortStatusChanged(const ForwardingStatus&) = 0;
195 protected:
196 virtual ~PortForwardingListener() {}
199 void AddPortForwardingListener(PortForwardingListener* listener);
200 void RemovePortForwardingListener(PortForwardingListener* listener);
202 void set_device_providers_for_test(
203 const AndroidDeviceManager::DeviceProviders& device_providers) {
204 device_manager_->SetDeviceProviders(device_providers);
207 void set_task_scheduler_for_test(
208 base::Callback<void(const base::Closure&)> scheduler) {
209 task_scheduler_ = scheduler;
212 bool HasDevToolsWindow(const std::string& agent_id);
214 // Creates new target instance owned by caller.
215 DevToolsTargetImpl* CreatePageTarget(scoped_refptr<RemotePage> browser);
217 typedef base::Callback<void(scoped_refptr<RemotePage>)> RemotePageCallback;
218 void OpenRemotePage(scoped_refptr<RemoteBrowser> browser,
219 const std::string& url,
220 const RemotePageCallback& callback);
222 scoped_refptr<content::DevToolsAgentHost> GetBrowserAgentHost(
223 scoped_refptr<RemoteBrowser> browser);
225 private:
226 friend struct content::BrowserThread::DeleteOnThread<
227 content::BrowserThread::UI>;
228 friend class base::DeleteHelper<DevToolsAndroidBridge>;
230 friend class PortForwardingController;
232 class AgentHostDelegate;
233 class DiscoveryRequest;
234 class RemotePageTarget;
236 ~DevToolsAndroidBridge() override;
238 void StartDeviceListPolling();
239 void StopDeviceListPolling();
240 bool NeedsDeviceListPolling();
242 typedef std::pair<scoped_refptr<AndroidDeviceManager::Device>,
243 scoped_refptr<RemoteDevice>> CompleteDevice;
244 typedef std::vector<CompleteDevice> CompleteDevices;
245 typedef base::Callback<void(const CompleteDevices&)> DeviceListCallback;
247 void RequestDeviceList(const DeviceListCallback& callback);
248 void ReceivedDeviceList(const CompleteDevices& complete_devices);
250 void StartDeviceCountPolling();
251 void StopDeviceCountPolling();
252 void RequestDeviceCount(const base::Callback<void(int)>& callback);
253 void ReceivedDeviceCount(int count);
255 static void ScheduleTaskDefault(const base::Closure& task);
257 void CreateDeviceProviders();
259 void SendJsonRequest(const BrowserId& browser_id,
260 const std::string& url,
261 const JsonRequestCallback& callback);
263 void SendProtocolCommand(const BrowserId& browser_id,
264 const std::string& debug_url,
265 const std::string& method,
266 scoped_ptr<base::DictionaryValue> params,
267 const base::Closure callback);
269 scoped_refptr<AndroidDeviceManager::Device> FindDevice(
270 const std::string& serial);
272 void PageCreatedOnUIThread(scoped_refptr<RemoteBrowser> browser,
273 const RemotePageCallback& callback,
274 const std::string& url,
275 int result,
276 const std::string& response);
278 void NavigatePageOnUIThread(scoped_refptr<RemoteBrowser> browser,
279 const RemotePageCallback& callback,
280 int result,
281 const std::string& response,
282 const std::string& url);
284 void RespondToOpenOnUIThread(scoped_refptr<RemoteBrowser> browser,
285 const RemotePageCallback& callback,
286 int result,
287 const std::string& response);
289 base::WeakPtr<DevToolsAndroidBridge> AsWeakPtr() {
290 return weak_factory_.GetWeakPtr();
293 Profile* const profile_;
294 SigninManagerBase* const signin_manager_;
295 ProfileOAuth2TokenService* const token_service_;
296 const scoped_ptr<AndroidDeviceManager> device_manager_;
298 typedef std::map<std::string, scoped_refptr<AndroidDeviceManager::Device>>
299 DeviceMap;
300 DeviceMap device_map_;
302 typedef std::map<std::string, AgentHostDelegate*> AgentHostDelegates;
303 AgentHostDelegates host_delegates_;
305 typedef std::vector<DeviceListListener*> DeviceListListeners;
306 DeviceListListeners device_list_listeners_;
307 base::CancelableCallback<void(const CompleteDevices&)> device_list_callback_;
309 typedef std::vector<DeviceCountListener*> DeviceCountListeners;
310 DeviceCountListeners device_count_listeners_;
311 base::CancelableCallback<void(int)> device_count_callback_;
312 base::Callback<void(const base::Closure&)> task_scheduler_;
314 typedef std::vector<PortForwardingListener*> PortForwardingListeners;
315 PortForwardingListeners port_forwarding_listeners_;
316 scoped_ptr<PortForwardingController> port_forwarding_controller_;
318 PrefChangeRegistrar pref_change_registrar_;
320 base::WeakPtrFactory<DevToolsAndroidBridge> weak_factory_;
322 DISALLOW_COPY_AND_ASSIGN(DevToolsAndroidBridge);
325 #endif // CHROME_BROWSER_DEVTOOLS_DEVICE_DEVTOOLS_ANDROID_BRIDGE_H_