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_WEBRTC_DEVTOOLS_BRIDGE_CLIENT_H_
6 #define CHROME_BROWSER_DEVTOOLS_DEVICE_WEBRTC_DEVTOOLS_BRIDGE_CLIENT_H_
8 #include "chrome/browser/devtools/device/android_device_manager.h"
9 #include "chrome/browser/devtools/device/webrtc/devtools_bridge_instances_request.h"
10 #include "chrome/browser/devtools/device/webrtc/send_command_request.h"
11 #include "chrome/browser/local_discovery/cloud_device_list_delegate.h"
12 #include "components/signin/core/browser/profile_identity_provider.h"
13 #include "content/public/browser/notification_observer.h"
14 #include "content/public/browser/notification_registrar.h"
15 #include "content/public/browser/web_contents_observer.h"
16 #include "google_apis/gaia/identity_provider.h"
19 class SigninManagerBase
;
20 class ProfileOAuth2TokenService
;
24 } // namespace content
26 namespace local_discovery
{
30 // Lives on the UI thread.
31 class DevToolsBridgeClient
: protected content::WebContentsObserver
,
32 private content::NotificationObserver
,
33 private IdentityProvider::Observer
,
34 private SendCommandRequest::Delegate
,
35 private DevToolsBridgeInstancesRequest::Delegate
{
37 using BrowserInfo
= AndroidDeviceManager::BrowserInfo
;
38 using DeviceInfo
= AndroidDeviceManager::DeviceInfo
;
39 using SerialList
= std::vector
<std::string
>;
40 using BrowserInfoList
= std::vector
<BrowserInfo
>;
42 static base::WeakPtr
<DevToolsBridgeClient
> Create(
44 SigninManagerBase
* signin_manager
,
45 ProfileOAuth2TokenService
* token_service
);
49 static SerialList
GetDevices(base::WeakPtr
<DevToolsBridgeClient
> weak_ptr
);
50 static DeviceInfo
GetDeviceInfo(base::WeakPtr
<DevToolsBridgeClient
> weak_ptr
,
51 const std::string
& serial
);
52 void StartSessionIfNeeded(const std::string
& socket_name
);
54 static DevToolsBridgeClient
* FromWebContents(
55 content::WebContents
* web_contents
);
56 void RegisterMessageHandlers(content::WebUI
* web_ui
);
59 DevToolsBridgeClient(Profile
* profile
,
60 SigninManagerBase
* signin_manager
,
61 ProfileOAuth2TokenService
* token_service
);
63 // Implementation of content::WebContentsObserver.
64 void DocumentOnLoadCompletedInMainFrame() override
;
66 ~DevToolsBridgeClient() override
;
68 bool IsAuthenticated();
70 // Overridden in tests.
71 virtual scoped_ptr
<local_discovery::GCDApiFlow
> CreateGCDApiFlow();
72 virtual void OnBrowserListUpdatedForTests() {}
74 const BrowserInfoList
& browsers() const { return browsers_
; }
75 ProfileIdentityProvider
& identity_provider() { return identity_provider_
; }
78 void CreateBackgroundWorker();
79 void UpdateBrowserList();
81 void HandleSendCommand(const base::ListValue
* args
);
83 // Implementation of IdentityProvider::Observer.
84 void OnActiveAccountLogin() override
;
85 void OnActiveAccountLogout() override
;
87 // Implementation of NotificationObserver.
88 void Observe(int type
,
89 const content::NotificationSource
& source
,
90 const content::NotificationDetails
& details
) override
;
92 // Implementation of SendCommandRequest::Delegate.
93 void OnCommandSucceeded(const base::DictionaryValue
& response
) override
;
94 void OnCommandFailed() override
;
96 // Implementation of DevToolsBridgeInstancesRequest::Delegate.
97 void OnDevToolsBridgeInstancesRequestSucceeded(
98 const DevToolsBridgeInstancesRequest::InstanceList
& instances
) override
;
99 void OnDevToolsBridgeInstancesRequestFailed() override
;
101 Profile
* const profile_
;
102 ProfileIdentityProvider identity_provider_
;
103 content::NotificationRegistrar registrar_
;
104 scoped_ptr
<content::WebContents
> background_worker_
;
105 scoped_ptr
<local_discovery::GCDApiFlow
> browser_list_request_
;
106 scoped_ptr
<local_discovery::GCDApiFlow
> send_command_request_
;
107 BrowserInfoList browsers_
;
108 bool worker_is_loaded_
;
109 base::WeakPtrFactory
<DevToolsBridgeClient
> weak_factory_
;
111 DISALLOW_COPY_AND_ASSIGN(DevToolsBridgeClient
);
114 #endif // CHROME_BROWSER_DEVTOOLS_DEVICE_WEBRTC_DEVTOOLS_BRIDGE_CLIENT_H_