OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/devtools/device/webrtc/webrtc_device_provider.h" | 5 #include "chrome/browser/devtools/device/webrtc/webrtc_device_provider.h" |
6 | 6 |
7 #include "chrome/browser/chrome_notification_types.h" | 7 #include "chrome/browser/chrome_notification_types.h" |
8 #include "chrome/browser/local_discovery/cloud_device_list.h" | |
9 #include "chrome/browser/local_discovery/cloud_device_list_delegate.h" | |
10 #include "chrome/browser/local_discovery/gcd_api_flow.h" | |
8 #include "chrome/browser/signin/profile_identity_provider.h" | 11 #include "chrome/browser/signin/profile_identity_provider.h" |
9 #include "chrome/browser/ui/browser.h" | 12 #include "chrome/browser/ui/browser.h" |
10 #include "chrome/common/url_constants.h" | 13 #include "chrome/common/url_constants.h" |
11 #include "content/public/browser/notification_observer.h" | 14 #include "content/public/browser/notification_observer.h" |
12 #include "content/public/browser/notification_registrar.h" | 15 #include "content/public/browser/notification_registrar.h" |
13 #include "content/public/browser/notification_source.h" | 16 #include "content/public/browser/notification_source.h" |
14 #include "content/public/browser/web_contents.h" | 17 #include "content/public/browser/web_contents.h" |
15 #include "content/public/browser/web_ui_data_source.h" | 18 #include "content/public/browser/web_ui_data_source.h" |
16 #include "content/public/browser/web_ui_message_handler.h" | 19 #include "content/public/browser/web_ui_message_handler.h" |
17 #include "grit/webrtc_device_provider_resources_map.h" | 20 #include "grit/webrtc_device_provider_resources_map.h" |
18 #include "net/base/net_errors.h" | 21 #include "net/base/net_errors.h" |
19 #include "net/socket/stream_socket.h" | 22 #include "net/socket/stream_socket.h" |
20 #include "ui/base/page_transition_types.h" | 23 #include "ui/base/page_transition_types.h" |
21 | 24 |
25 using BrowserInfo = AndroidDeviceManager::BrowserInfo; | |
26 using BrowserInfoList = std::vector<BrowserInfo>; | |
27 using DeviceInfo = AndroidDeviceManager::DeviceInfo; | |
28 using SerialList = std::vector<std::string>; | |
22 using content::BrowserThread; | 29 using content::BrowserThread; |
23 using content::NotificationDetails; | 30 using content::NotificationDetails; |
24 using content::NotificationObserver; | 31 using content::NotificationObserver; |
25 using content::NotificationRegistrar; | 32 using content::NotificationRegistrar; |
26 using content::NotificationSource; | 33 using content::NotificationSource; |
27 using content::WebContents; | 34 using content::WebContents; |
28 using content::WebUIDataSource; | 35 using content::WebUIDataSource; |
29 using content::WebUIMessageHandler; | 36 using content::WebUIMessageHandler; |
37 using local_discovery::CloudDeviceList; | |
38 using local_discovery::CloudDeviceListDelegate; | |
39 using local_discovery::GCDApiFlow; | |
30 | 40 |
31 namespace { | 41 namespace { |
32 | 42 |
33 const char kBackgroundWorkerURL[] = | 43 const char kBackgroundWorkerURL[] = |
34 "chrome://webrtc-device-provider/background_worker.html"; | 44 "chrome://webrtc-device-provider/background_worker.html"; |
45 const char kSerial[] = "clouddevices"; | |
46 const char kPseudoDeviceName[] = "Remote browsers"; | |
35 | 47 |
36 } // namespace | 48 } // namespace |
37 | 49 |
38 // Lives on the UI thread. | 50 // Lives on the UI thread. |
39 class WebRTCDeviceProvider::DevToolsBridgeClient : | 51 class WebRTCDeviceProvider::DevToolsBridgeClient : |
40 private NotificationObserver, | 52 private NotificationObserver, |
41 private IdentityProvider::Observer { | 53 private IdentityProvider::Observer, |
54 private CloudDeviceListDelegate { | |
42 public: | 55 public: |
43 static base::WeakPtr<DevToolsBridgeClient> Create( | 56 static base::WeakPtr<DevToolsBridgeClient> Create( |
44 Profile* profile, | 57 Profile* profile, |
45 SigninManagerBase* signin_manager, | 58 SigninManagerBase* signin_manager, |
46 ProfileOAuth2TokenService* token_service); | 59 ProfileOAuth2TokenService* token_service); |
47 | 60 |
48 void DeleteSelf(); | 61 void DeleteSelf(); |
49 | 62 |
63 static SerialList GetDevices(base::WeakPtr<DevToolsBridgeClient> weak_ptr); | |
64 static DeviceInfo GetDeviceInfo(base::WeakPtr<DevToolsBridgeClient> weak_ptr, | |
65 const std::string& serial); | |
66 | |
50 private: | 67 private: |
51 DevToolsBridgeClient(Profile* profile, | 68 DevToolsBridgeClient(Profile* profile, |
52 SigninManagerBase* signin_manager, | 69 SigninManagerBase* signin_manager, |
53 ProfileOAuth2TokenService* token_service); | 70 ProfileOAuth2TokenService* token_service); |
54 | 71 |
55 ~DevToolsBridgeClient(); | 72 ~DevToolsBridgeClient(); |
56 | 73 |
57 void CreateBackgroundWorker(); | 74 void CreateBackgroundWorker(); |
75 void UpdateBrowserList(); | |
58 | 76 |
59 // Implementation of IdentityProvider::Observer. | 77 // Implementation of IdentityProvider::Observer. |
60 void OnActiveAccountLogin() override; | 78 void OnActiveAccountLogin() override; |
61 void OnActiveAccountLogout() override; | 79 void OnActiveAccountLogout() override; |
62 | 80 |
63 // Implementation of NotificationObserver. | 81 // Implementation of NotificationObserver. |
64 void Observe(int type, | 82 void Observe(int type, |
65 const NotificationSource& source, | 83 const NotificationSource& source, |
66 const NotificationDetails& details) override; | 84 const NotificationDetails& details) override; |
67 | 85 |
86 // CloudDeviceListDelegate implementation. | |
87 void OnDeviceListReady( | |
88 const CloudDeviceListDelegate::DeviceList& devices) override; | |
89 void OnDeviceListUnavailable() override; | |
90 | |
68 Profile* const profile_; | 91 Profile* const profile_; |
69 ProfileIdentityProvider identity_provider_; | 92 ProfileIdentityProvider identity_provider_; |
70 NotificationRegistrar registrar_; | 93 NotificationRegistrar registrar_; |
71 scoped_ptr<WebContents> background_worker_; | 94 scoped_ptr<WebContents> background_worker_; |
95 scoped_ptr<GCDApiFlow> browser_list_request_; | |
96 BrowserInfoList browsers_; | |
97 bool browser_list_updating_; | |
72 base::WeakPtrFactory<DevToolsBridgeClient> weak_factory_; | 98 base::WeakPtrFactory<DevToolsBridgeClient> weak_factory_; |
73 }; | 99 }; |
74 | 100 |
75 class WebRTCDeviceProvider::MessageHandler : public WebUIMessageHandler { | 101 class WebRTCDeviceProvider::MessageHandler : public WebUIMessageHandler { |
76 public: | 102 public: |
77 explicit MessageHandler(DevToolsBridgeClient* owner); | 103 explicit MessageHandler(DevToolsBridgeClient* owner); |
78 | 104 |
79 void RegisterMessages() override; | 105 void RegisterMessages() override; |
80 | 106 |
81 private: | 107 private: |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
122 profile, signin_manager, token_service); | 148 profile, signin_manager, token_service); |
123 return instance->weak_factory_.GetWeakPtr(); | 149 return instance->weak_factory_.GetWeakPtr(); |
124 } | 150 } |
125 | 151 |
126 WebRTCDeviceProvider::DevToolsBridgeClient::DevToolsBridgeClient( | 152 WebRTCDeviceProvider::DevToolsBridgeClient::DevToolsBridgeClient( |
127 Profile* profile, | 153 Profile* profile, |
128 SigninManagerBase* signin_manager, | 154 SigninManagerBase* signin_manager, |
129 ProfileOAuth2TokenService* token_service) | 155 ProfileOAuth2TokenService* token_service) |
130 : profile_(profile), | 156 : profile_(profile), |
131 identity_provider_(signin_manager, token_service, nullptr), | 157 identity_provider_(signin_manager, token_service, nullptr), |
158 browser_list_updating_(false), | |
132 weak_factory_(this) { | 159 weak_factory_(this) { |
133 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 160 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
134 | 161 |
135 identity_provider_.AddObserver(this); | 162 identity_provider_.AddObserver(this); |
136 registrar_.Add(this, chrome::NOTIFICATION_PROFILE_DESTROYED, | 163 registrar_.Add(this, chrome::NOTIFICATION_PROFILE_DESTROYED, |
137 content::Source<Profile>(profile_)); | 164 content::Source<Profile>(profile_)); |
138 | 165 |
139 if (!identity_provider_.GetActiveAccountId().empty()) | 166 if (!identity_provider_.GetActiveAccountId().empty()) |
140 CreateBackgroundWorker(); | 167 CreateBackgroundWorker(); |
141 } | 168 } |
142 | 169 |
143 WebRTCDeviceProvider::DevToolsBridgeClient::~DevToolsBridgeClient() { | 170 WebRTCDeviceProvider::DevToolsBridgeClient::~DevToolsBridgeClient() { |
144 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 171 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
145 | 172 |
146 identity_provider_.RemoveObserver(this); | 173 identity_provider_.RemoveObserver(this); |
147 } | 174 } |
148 | 175 |
149 void WebRTCDeviceProvider::DevToolsBridgeClient::DeleteSelf() { | 176 void WebRTCDeviceProvider::DevToolsBridgeClient::DeleteSelf() { |
150 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 177 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
151 delete this; | 178 delete this; |
152 } | 179 } |
153 | 180 |
181 void WebRTCDeviceProvider::DevToolsBridgeClient::UpdateBrowserList() { | |
182 if (browser_list_updating_ || !browser_list_request_.get()) | |
183 return; | |
184 browser_list_updating_ = true; | |
185 | |
186 browser_list_request_->Start(make_scoped_ptr(new CloudDeviceList(this))); | |
187 } | |
188 | |
189 // static | |
190 SerialList WebRTCDeviceProvider::DevToolsBridgeClient::GetDevices( | |
191 base::WeakPtr<DevToolsBridgeClient> weak_ptr) { | |
192 SerialList result; | |
193 if (auto* ptr = weak_ptr.get()) { | |
194 if (ptr->background_worker_.get()) | |
195 result.push_back(kSerial); | |
196 | |
197 ptr->UpdateBrowserList(); | |
198 } | |
199 return result; | |
200 } | |
201 | |
202 // static | |
203 DeviceInfo WebRTCDeviceProvider::DevToolsBridgeClient::GetDeviceInfo( | |
204 base::WeakPtr<DevToolsBridgeClient> weak_ptr, | |
205 const std::string& serial) { | |
206 DeviceInfo result; | |
207 if (auto* ptr = weak_ptr.get()) { | |
dgozman
2014/12/08 16:08:16
What about renaming to |self| and |weak_self|? |pt
SeRya
2014/12/08 17:33:39
Done.
| |
208 result.connected = ptr->background_worker_.get(); | |
209 result.model = kPseudoDeviceName; | |
210 result.browser_info = ptr->browsers_; | |
211 } | |
212 return result; | |
213 } | |
214 | |
154 void WebRTCDeviceProvider::DevToolsBridgeClient::CreateBackgroundWorker() { | 215 void WebRTCDeviceProvider::DevToolsBridgeClient::CreateBackgroundWorker() { |
155 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 216 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
156 | 217 |
157 background_worker_.reset( | 218 background_worker_.reset( |
158 WebContents::Create(WebContents::CreateParams(profile_))); | 219 WebContents::Create(WebContents::CreateParams(profile_))); |
159 | 220 |
160 GURL url(kBackgroundWorkerURL); | 221 GURL url(kBackgroundWorkerURL); |
161 DCHECK_EQ(chrome::kChromeUIWebRTCDeviceProviderHost, url.host()); | 222 DCHECK_EQ(chrome::kChromeUIWebRTCDeviceProviderHost, url.host()); |
162 | 223 |
163 background_worker_->GetController().LoadURL( | 224 background_worker_->GetController().LoadURL( |
164 url, | 225 url, |
165 content::Referrer(), | 226 content::Referrer(), |
166 ui::PAGE_TRANSITION_AUTO_TOPLEVEL, | 227 ui::PAGE_TRANSITION_AUTO_TOPLEVEL, |
167 std::string()); | 228 std::string()); |
168 | 229 |
169 background_worker_->GetWebUI()->AddMessageHandler( | 230 background_worker_->GetWebUI()->AddMessageHandler( |
170 new MessageHandler(this)); | 231 new MessageHandler(this)); |
232 | |
233 browser_list_request_ = | |
234 GCDApiFlow::Create(profile_->GetRequestContext(), | |
235 identity_provider_.GetTokenService(), | |
236 identity_provider_.GetActiveAccountId()); | |
171 } | 237 } |
172 | 238 |
173 void WebRTCDeviceProvider::DevToolsBridgeClient::Observe( | 239 void WebRTCDeviceProvider::DevToolsBridgeClient::Observe( |
174 int type, | 240 int type, |
175 const NotificationSource& source, | 241 const NotificationSource& source, |
176 const NotificationDetails& details) { | 242 const NotificationDetails& details) { |
177 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 243 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
178 DCHECK_EQ(chrome::NOTIFICATION_PROFILE_DESTROYED, type); | 244 DCHECK_EQ(chrome::NOTIFICATION_PROFILE_DESTROYED, type); |
179 | 245 |
180 delete this; | 246 delete this; |
181 } | 247 } |
182 | 248 |
183 void WebRTCDeviceProvider::DevToolsBridgeClient::OnActiveAccountLogin() { | 249 void WebRTCDeviceProvider::DevToolsBridgeClient::OnActiveAccountLogin() { |
184 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 250 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
185 CreateBackgroundWorker(); | 251 CreateBackgroundWorker(); |
186 } | 252 } |
187 | 253 |
188 void WebRTCDeviceProvider::DevToolsBridgeClient::OnActiveAccountLogout() { | 254 void WebRTCDeviceProvider::DevToolsBridgeClient::OnActiveAccountLogout() { |
189 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 255 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
190 background_worker_.reset(); | 256 background_worker_.reset(); |
257 browser_list_request_.reset(); | |
258 BrowserInfoList().swap(browsers_); | |
259 } | |
260 | |
261 void WebRTCDeviceProvider::DevToolsBridgeClient::OnDeviceListReady( | |
262 const CloudDeviceListDelegate::DeviceList& devices) { | |
263 browser_list_updating_ = false; | |
264 | |
265 // TODO(serya): Not all devices are browsers. Filter them out. | |
266 BrowserInfoList browsers; | |
267 browsers.reserve(devices.size()); | |
268 for (const auto& device : devices) { | |
269 BrowserInfo browser; | |
270 browser.type = BrowserInfo::kTypeChrome; | |
271 browser.display_name = device.display_name; | |
272 browsers.push_back(browser); | |
273 } | |
274 | |
275 browsers_.swap(browsers); | |
276 } | |
277 | |
278 void WebRTCDeviceProvider::DevToolsBridgeClient::OnDeviceListUnavailable() { | |
279 browser_list_updating_ = false; | |
280 | |
281 BrowserInfoList().swap(browsers_); | |
191 } | 282 } |
192 | 283 |
193 // WebRTCDeviceProvider::MessageHandler ---------------------------------------- | 284 // WebRTCDeviceProvider::MessageHandler ---------------------------------------- |
194 | 285 |
195 WebRTCDeviceProvider::MessageHandler::MessageHandler( | 286 WebRTCDeviceProvider::MessageHandler::MessageHandler( |
196 DevToolsBridgeClient* owner) : owner_(owner) { | 287 DevToolsBridgeClient* owner) : owner_(owner) { |
197 } | 288 } |
198 | 289 |
199 void WebRTCDeviceProvider::MessageHandler::RegisterMessages() { | 290 void WebRTCDeviceProvider::MessageHandler::RegisterMessages() { |
200 web_ui()->RegisterMessageCallback( | 291 web_ui()->RegisterMessageCallback( |
(...skipping 19 matching lines...) Expand all Loading... | |
220 } | 311 } |
221 | 312 |
222 WebRTCDeviceProvider::~WebRTCDeviceProvider() { | 313 WebRTCDeviceProvider::~WebRTCDeviceProvider() { |
223 BrowserThread::PostTask( | 314 BrowserThread::PostTask( |
224 BrowserThread::UI, | 315 BrowserThread::UI, |
225 FROM_HERE, | 316 FROM_HERE, |
226 base::Bind(&DevToolsBridgeClient::DeleteSelf, client_)); | 317 base::Bind(&DevToolsBridgeClient::DeleteSelf, client_)); |
227 } | 318 } |
228 | 319 |
229 void WebRTCDeviceProvider::QueryDevices(const SerialsCallback& callback) { | 320 void WebRTCDeviceProvider::QueryDevices(const SerialsCallback& callback) { |
230 // TODO(serya): Implement | 321 BrowserThread::PostTaskAndReplyWithResult( |
231 base::MessageLoop::current()->PostTask( | 322 BrowserThread::UI, |
232 FROM_HERE, base::Bind(callback, std::vector<std::string>())); | 323 FROM_HERE, |
324 base::Bind(&DevToolsBridgeClient::GetDevices, client_), | |
325 callback); | |
233 } | 326 } |
234 | 327 |
235 void WebRTCDeviceProvider::QueryDeviceInfo(const std::string& serial, | 328 void WebRTCDeviceProvider::QueryDeviceInfo(const std::string& serial, |
236 const DeviceInfoCallback& callback) { | 329 const DeviceInfoCallback& callback) { |
237 // TODO(serya): Implement | 330 BrowserThread::PostTaskAndReplyWithResult( |
238 base::MessageLoop::current()->PostTask( | 331 BrowserThread::UI, |
239 FROM_HERE, base::Bind(callback, AndroidDeviceManager::DeviceInfo())); | 332 FROM_HERE, |
333 base::Bind(&DevToolsBridgeClient::GetDeviceInfo, client_, serial), | |
334 callback); | |
240 } | 335 } |
241 | 336 |
242 void WebRTCDeviceProvider::OpenSocket(const std::string& serial, | 337 void WebRTCDeviceProvider::OpenSocket(const std::string& serial, |
243 const std::string& socket_name, | 338 const std::string& socket_name, |
244 const SocketCallback& callback) { | 339 const SocketCallback& callback) { |
245 // TODO(serya): Implement | 340 // TODO(serya): Implement |
246 scoped_ptr<net::StreamSocket> socket; | 341 scoped_ptr<net::StreamSocket> socket; |
247 base::MessageLoop::current()->PostTask( | 342 base::MessageLoop::current()->PostTask( |
248 FROM_HERE, base::Bind(callback, net::ERR_FAILED, base::Passed(&socket))); | 343 FROM_HERE, base::Bind(callback, net::ERR_FAILED, base::Passed(&socket))); |
249 } | 344 } |
OLD | NEW |