Chromium Code Reviews| 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 "google_apis/gaia/identity_provider.h" | 20 #include "google_apis/gaia/identity_provider.h" |
| 18 #include "grit/webrtc_device_provider_resources_map.h" | 21 #include "grit/webrtc_device_provider_resources_map.h" |
| 19 #include "net/base/net_errors.h" | 22 #include "net/base/net_errors.h" |
| 20 #include "net/socket/stream_socket.h" | 23 #include "net/socket/stream_socket.h" |
| 21 #include "ui/base/page_transition_types.h" | 24 #include "ui/base/page_transition_types.h" |
| 22 | 25 |
| 26 using BrowserInfo = AndroidDeviceManager::BrowserInfo; | |
| 27 using BrowserInfoList = std::vector<BrowserInfo>; | |
| 28 using DeviceInfo = AndroidDeviceManager::DeviceInfo; | |
| 29 using Serials = std::vector<std::string>; | |
|
dgozman
2014/12/08 14:27:21
Let's be consistent: SerialsList.
SeRya
2014/12/08 15:01:22
Done.
| |
| 23 using content::BrowserThread; | 30 using content::BrowserThread; |
| 24 using content::NotificationDetails; | 31 using content::NotificationDetails; |
| 25 using content::NotificationObserver; | 32 using content::NotificationObserver; |
| 26 using content::NotificationRegistrar; | 33 using content::NotificationRegistrar; |
| 27 using content::NotificationSource; | 34 using content::NotificationSource; |
| 28 using content::Source; | 35 using content::Source; |
| 29 using content::WebContents; | 36 using content::WebContents; |
| 30 using content::WebUIDataSource; | 37 using content::WebUIDataSource; |
| 31 using content::WebUIMessageHandler; | 38 using content::WebUIMessageHandler; |
| 39 using local_discovery::CloudDeviceList; | |
| 40 using local_discovery::CloudDeviceListDelegate; | |
| 41 using local_discovery::GCDApiFlow; | |
| 32 | 42 |
| 33 namespace { | 43 namespace { |
| 34 | 44 |
| 35 const char kBackgroundWorkerURL[] = | 45 const char kBackgroundWorkerURL[] = |
| 36 "chrome://webrtc-device-provider/background_worker.html"; | 46 "chrome://webrtc-device-provider/background_worker.html"; |
| 37 | 47 const char kSerial[] = "clouddevices"; |
|
dgozman
2014/12/08 14:27:21
nit: add empty line after this please.
SeRya
2014/12/08 15:01:22
Done.
| |
| 38 } // namespace | 48 } // namespace |
| 39 | 49 |
| 40 // Lives on the UI thread. | 50 // Lives on the UI thread. |
| 41 class WebRTCDeviceProvider::DevToolsBridgeClient : | 51 class WebRTCDeviceProvider::DevToolsBridgeClient : |
| 42 private NotificationObserver, | 52 private NotificationObserver, |
| 43 private IdentityProvider::Observer { | 53 private IdentityProvider::Observer, |
| 54 private CloudDeviceListDelegate { | |
| 44 public: | 55 public: |
| 45 static base::WeakPtr<DevToolsBridgeClient> Create( | 56 static base::WeakPtr<DevToolsBridgeClient> Create( |
| 46 Profile* profile, ProfileIdentityProvider* identity_provider); | 57 Profile* profile, ProfileIdentityProvider* identity_provider); |
| 47 | 58 |
| 48 void DeleteSelf(); | 59 void DeleteSelf(); |
| 49 | 60 |
| 61 void UpdateBrowserList(); | |
|
dgozman
2014/12/08 14:27:22
this one is private
SeRya
2014/12/08 15:01:22
Done.
| |
| 62 BrowserInfoList const& GetRemoteBrowsers() const { return browsers_; } | |
|
dgozman
2014/12/08 14:27:22
nit: this is not used.
SeRya
2014/12/08 15:01:22
Done.
| |
| 63 | |
| 64 static Serials GetDevices(base::WeakPtr<DevToolsBridgeClient> weak_ptr); | |
|
dgozman
2014/12/06 13:16:33
Why not use an instance method with callback inste
SeRya
2014/12/07 13:40:46
Non-void method can't be bound to a weak reference
| |
| 65 static DeviceInfo GetDeviceInfo(base::WeakPtr<DevToolsBridgeClient> weak_ptr, | |
| 66 const std::string& serial); | |
| 67 | |
| 50 private: | 68 private: |
| 51 DevToolsBridgeClient(Profile* profile, | 69 DevToolsBridgeClient(Profile* profile, |
| 52 ProfileIdentityProvider* identity_provider); | 70 ProfileIdentityProvider* identity_provider); |
| 53 | 71 |
| 54 ~DevToolsBridgeClient(); | 72 ~DevToolsBridgeClient(); |
| 55 | 73 |
| 56 void CreateBackgroundWorker(); | 74 void CreateBackgroundWorker(); |
| 57 | 75 |
| 58 // implementation of IdentityProvider::Observer. | 76 // implementation of IdentityProvider::Observer. |
| 59 void OnActiveAccountLogin() override; | 77 void OnActiveAccountLogin() override; |
| 60 void OnActiveAccountLogout() override; | 78 void OnActiveAccountLogout() override; |
| 61 | 79 |
| 62 // implementation of NotificationObserver. | 80 // implementation of NotificationObserver. |
| 63 void Observe(int type, | 81 void Observe(int type, |
| 64 const NotificationSource& source, | 82 const NotificationSource& source, |
| 65 const NotificationDetails& details) override; | 83 const NotificationDetails& details) override; |
| 66 | 84 |
| 85 // CloudDeviceListDelegate implementation. | |
| 86 void OnDeviceListReady( | |
| 87 const CloudDeviceListDelegate::DeviceList& devices) override; | |
| 88 void OnDeviceListUnavailable() override; | |
| 89 | |
| 67 Profile* const profile_; | 90 Profile* const profile_; |
| 68 ProfileIdentityProvider* const identity_provider_; | 91 ProfileIdentityProvider* const identity_provider_; |
| 69 NotificationRegistrar registrar_; | 92 NotificationRegistrar registrar_; |
| 70 scoped_ptr<WebContents> background_worker_; | 93 scoped_ptr<WebContents> background_worker_; |
| 94 scoped_ptr<GCDApiFlow> browser_list_request_; | |
| 95 BrowserInfoList browsers_; | |
| 96 bool browser_list_updating_; | |
| 71 base::WeakPtrFactory<DevToolsBridgeClient> weak_factory_; | 97 base::WeakPtrFactory<DevToolsBridgeClient> weak_factory_; |
| 72 }; | 98 }; |
| 73 | 99 |
| 74 class WebRTCDeviceProvider::MessageHandler : public WebUIMessageHandler { | 100 class WebRTCDeviceProvider::MessageHandler : public WebUIMessageHandler { |
| 75 public: | 101 public: |
| 76 explicit MessageHandler(DevToolsBridgeClient* owner); | 102 explicit MessageHandler(DevToolsBridgeClient* owner); |
| 77 | 103 |
| 78 void RegisterMessages() override; | 104 void RegisterMessages() override; |
| 79 | 105 |
| 80 private: | 106 private: |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 115 WebRTCDeviceProvider::DevToolsBridgeClient::Create( | 141 WebRTCDeviceProvider::DevToolsBridgeClient::Create( |
| 116 Profile* profile, ProfileIdentityProvider* identity_provider) { | 142 Profile* profile, ProfileIdentityProvider* identity_provider) { |
| 117 auto instance = new DevToolsBridgeClient(profile, identity_provider); | 143 auto instance = new DevToolsBridgeClient(profile, identity_provider); |
| 118 return instance->weak_factory_.GetWeakPtr(); | 144 return instance->weak_factory_.GetWeakPtr(); |
| 119 } | 145 } |
| 120 | 146 |
| 121 WebRTCDeviceProvider::DevToolsBridgeClient::DevToolsBridgeClient( | 147 WebRTCDeviceProvider::DevToolsBridgeClient::DevToolsBridgeClient( |
| 122 Profile* profile, ProfileIdentityProvider* identity_provider) | 148 Profile* profile, ProfileIdentityProvider* identity_provider) |
| 123 : profile_(profile), | 149 : profile_(profile), |
| 124 identity_provider_(identity_provider), | 150 identity_provider_(identity_provider), |
| 151 browser_list_updating_(false), | |
| 125 weak_factory_(this) { | 152 weak_factory_(this) { |
| 126 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 153 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 127 | 154 |
| 128 identity_provider_->AddObserver(this); | 155 identity_provider_->AddObserver(this); |
| 129 registrar_.Add(this, chrome::NOTIFICATION_PROFILE_DESTROYED, | 156 registrar_.Add(this, chrome::NOTIFICATION_PROFILE_DESTROYED, |
| 130 Source<Profile>(profile_)); | 157 Source<Profile>(profile_)); |
| 131 | 158 |
| 132 if (!identity_provider_->GetActiveAccountId().empty()) | 159 if (!identity_provider_->GetActiveAccountId().empty()) |
| 133 CreateBackgroundWorker(); | 160 CreateBackgroundWorker(); |
| 134 } | 161 } |
| 135 | 162 |
| 136 WebRTCDeviceProvider::DevToolsBridgeClient::~DevToolsBridgeClient() { | 163 WebRTCDeviceProvider::DevToolsBridgeClient::~DevToolsBridgeClient() { |
| 137 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 164 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 138 | 165 |
| 139 identity_provider_->RemoveObserver(this); | 166 identity_provider_->RemoveObserver(this); |
| 140 } | 167 } |
| 141 | 168 |
| 142 void WebRTCDeviceProvider::DevToolsBridgeClient::DeleteSelf() { | 169 void WebRTCDeviceProvider::DevToolsBridgeClient::DeleteSelf() { |
| 143 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 170 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 144 delete this; | 171 delete this; |
| 145 } | 172 } |
| 146 | 173 |
| 174 void WebRTCDeviceProvider::DevToolsBridgeClient::UpdateBrowserList() { | |
| 175 if (browser_list_updating_ || !browser_list_request_.get()) | |
| 176 return; | |
|
dgozman
2014/12/06 13:16:33
nit: indentation
SeRya
2014/12/08 15:01:22
Done.
| |
| 177 browser_list_updating_ = true; | |
| 178 | |
| 179 browser_list_request_->Start(make_scoped_ptr(new CloudDeviceList(this))); | |
| 180 } | |
| 181 | |
| 182 // static | |
| 183 Serials WebRTCDeviceProvider::DevToolsBridgeClient::GetDevices( | |
| 184 base::WeakPtr<DevToolsBridgeClient> weak_ptr) { | |
| 185 Serials result; | |
| 186 if (auto* ptr = weak_ptr.get()) { | |
| 187 if (ptr->background_worker_.get()) | |
| 188 result.push_back(kSerial); | |
| 189 | |
| 190 ptr->UpdateBrowserList(); | |
| 191 } | |
| 192 return result; | |
| 193 } | |
| 194 | |
| 195 // static | |
| 196 DeviceInfo WebRTCDeviceProvider::DevToolsBridgeClient::GetDeviceInfo( | |
| 197 base::WeakPtr<DevToolsBridgeClient> weak_ptr, | |
| 198 const std::string& serial) { | |
| 199 DeviceInfo result; | |
| 200 if (auto* ptr = weak_ptr.get()) { | |
| 201 result.connected = ptr->background_worker_.get(); | |
| 202 result.model = "Remote browsers"; | |
|
dgozman
2014/12/08 14:27:21
nit: make a named constant
SeRya
2014/12/08 15:01:22
Done.
| |
| 203 result.browser_info = ptr->browsers_; | |
| 204 } | |
| 205 return result; | |
| 206 } | |
| 207 | |
| 147 void WebRTCDeviceProvider::DevToolsBridgeClient::CreateBackgroundWorker() { | 208 void WebRTCDeviceProvider::DevToolsBridgeClient::CreateBackgroundWorker() { |
| 148 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 209 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 149 | 210 |
| 150 background_worker_.reset( | 211 background_worker_.reset( |
| 151 WebContents::Create(WebContents::CreateParams(profile_))); | 212 WebContents::Create(WebContents::CreateParams(profile_))); |
| 152 | 213 |
| 153 GURL url(kBackgroundWorkerURL); | 214 GURL url(kBackgroundWorkerURL); |
| 154 DCHECK_EQ(chrome::kChromeUIWebRTCDeviceProviderHost, url.host()); | 215 DCHECK_EQ(chrome::kChromeUIWebRTCDeviceProviderHost, url.host()); |
| 155 | 216 |
| 156 background_worker_->GetController().LoadURL( | 217 background_worker_->GetController().LoadURL( |
| 157 url, | 218 url, |
| 158 content::Referrer(), | 219 content::Referrer(), |
| 159 ui::PAGE_TRANSITION_AUTO_TOPLEVEL, | 220 ui::PAGE_TRANSITION_AUTO_TOPLEVEL, |
| 160 std::string()); | 221 std::string()); |
| 161 | 222 |
| 162 background_worker_->GetWebUI()->AddMessageHandler( | 223 background_worker_->GetWebUI()->AddMessageHandler( |
| 163 new MessageHandler(this)); | 224 new MessageHandler(this)); |
| 225 | |
| 226 browser_list_request_ = | |
| 227 GCDApiFlow::Create(profile_->GetRequestContext(), | |
| 228 identity_provider_->GetTokenService(), | |
| 229 identity_provider_->GetActiveAccountId()); | |
| 164 } | 230 } |
| 165 | 231 |
| 166 void WebRTCDeviceProvider::DevToolsBridgeClient::Observe( | 232 void WebRTCDeviceProvider::DevToolsBridgeClient::Observe( |
| 167 int type, | 233 int type, |
| 168 const NotificationSource& source, | 234 const NotificationSource& source, |
| 169 const NotificationDetails& details) { | 235 const NotificationDetails& details) { |
| 170 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 236 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 171 DCHECK_EQ(chrome::NOTIFICATION_PROFILE_DESTROYED, type); | 237 DCHECK_EQ(chrome::NOTIFICATION_PROFILE_DESTROYED, type); |
| 172 | 238 |
| 173 delete this; | 239 delete this; |
| 174 } | 240 } |
| 175 | 241 |
| 176 void WebRTCDeviceProvider::DevToolsBridgeClient::OnActiveAccountLogin() { | 242 void WebRTCDeviceProvider::DevToolsBridgeClient::OnActiveAccountLogin() { |
| 177 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 243 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 178 CreateBackgroundWorker(); | 244 CreateBackgroundWorker(); |
| 179 } | 245 } |
| 180 | 246 |
| 181 void WebRTCDeviceProvider::DevToolsBridgeClient::OnActiveAccountLogout() { | 247 void WebRTCDeviceProvider::DevToolsBridgeClient::OnActiveAccountLogout() { |
| 182 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 248 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 183 background_worker_.reset(); | 249 background_worker_.reset(); |
| 250 browser_list_request_.reset(); | |
| 251 BrowserInfoList().swap(browsers_); | |
|
dgozman
2014/12/06 13:16:33
This looks awkward to me...
SeRya
2014/12/07 13:40:46
This is well-known pattern. Standard clear method
| |
| 252 } | |
| 253 | |
| 254 void WebRTCDeviceProvider::DevToolsBridgeClient::OnDeviceListReady( | |
| 255 const CloudDeviceListDelegate::DeviceList& devices) { | |
| 256 browser_list_updating_ = false; | |
| 257 | |
| 258 // TODO(serya): Not all devices are browsers. Filter them out. | |
|
dgozman
2014/12/06 13:16:33
Let's implement this right away. We should also gr
SeRya
2014/12/07 13:40:46
There is not enough information provided by CloudD
dgozman
2014/12/08 14:27:22
We should check for the command we use to be prese
| |
| 259 BrowserInfoList browsers; | |
| 260 browsers.reserve(devices.size()); | |
| 261 for (const auto& device : devices) { | |
| 262 BrowserInfo browser; | |
| 263 browser.type = BrowserInfo::kTypeChrome; | |
| 264 browser.display_name = device.display_name; | |
| 265 browsers.push_back(browser); | |
| 266 } | |
| 267 | |
| 268 browsers_.swap(browsers); | |
| 269 } | |
| 270 | |
| 271 void WebRTCDeviceProvider::DevToolsBridgeClient::OnDeviceListUnavailable() { | |
| 272 browser_list_updating_ = false; | |
| 273 | |
| 274 BrowserInfoList().swap(browsers_); | |
| 184 } | 275 } |
| 185 | 276 |
| 186 // WebRTCDeviceProvider::MessageHandler ---------------------------------------- | 277 // WebRTCDeviceProvider::MessageHandler ---------------------------------------- |
| 187 | 278 |
| 188 WebRTCDeviceProvider::MessageHandler::MessageHandler( | 279 WebRTCDeviceProvider::MessageHandler::MessageHandler( |
| 189 DevToolsBridgeClient* owner) : owner_(owner) { | 280 DevToolsBridgeClient* owner) : owner_(owner) { |
| 190 } | 281 } |
| 191 | 282 |
| 192 void WebRTCDeviceProvider::MessageHandler::RegisterMessages() { | 283 void WebRTCDeviceProvider::MessageHandler::RegisterMessages() { |
| 193 web_ui()->RegisterMessageCallback( | 284 web_ui()->RegisterMessageCallback( |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 210 } | 301 } |
| 211 | 302 |
| 212 WebRTCDeviceProvider::~WebRTCDeviceProvider() { | 303 WebRTCDeviceProvider::~WebRTCDeviceProvider() { |
| 213 BrowserThread::PostTask( | 304 BrowserThread::PostTask( |
| 214 BrowserThread::UI, | 305 BrowserThread::UI, |
| 215 FROM_HERE, | 306 FROM_HERE, |
| 216 base::Bind(&DevToolsBridgeClient::DeleteSelf, client_)); | 307 base::Bind(&DevToolsBridgeClient::DeleteSelf, client_)); |
| 217 } | 308 } |
| 218 | 309 |
| 219 void WebRTCDeviceProvider::QueryDevices(const SerialsCallback& callback) { | 310 void WebRTCDeviceProvider::QueryDevices(const SerialsCallback& callback) { |
| 220 // TODO(serya): Implement | 311 BrowserThread::PostTaskAndReplyWithResult( |
| 221 base::MessageLoop::current()->PostTask( | 312 BrowserThread::UI, |
| 222 FROM_HERE, base::Bind(callback, std::vector<std::string>())); | 313 FROM_HERE, |
| 314 base::Bind(&DevToolsBridgeClient::GetDevices, client_), | |
| 315 callback); | |
| 223 } | 316 } |
| 224 | 317 |
| 225 void WebRTCDeviceProvider::QueryDeviceInfo(const std::string& serial, | 318 void WebRTCDeviceProvider::QueryDeviceInfo(const std::string& serial, |
| 226 const DeviceInfoCallback& callback) { | 319 const DeviceInfoCallback& callback) { |
|
dgozman
2014/12/06 13:16:33
nit: indentation
SeRya
2014/12/08 15:01:22
Done.
| |
| 227 // TODO(serya): Implement | 320 BrowserThread::PostTaskAndReplyWithResult( |
| 228 base::MessageLoop::current()->PostTask( | 321 BrowserThread::UI, |
| 229 FROM_HERE, base::Bind(callback, AndroidDeviceManager::DeviceInfo())); | 322 FROM_HERE, |
| 323 base::Bind(&DevToolsBridgeClient::GetDeviceInfo, client_, serial), | |
| 324 callback); | |
| 230 } | 325 } |
| 231 | 326 |
| 232 void WebRTCDeviceProvider::OpenSocket(const std::string& serial, | 327 void WebRTCDeviceProvider::OpenSocket(const std::string& serial, |
| 233 const std::string& socket_name, | 328 const std::string& socket_name, |
| 234 const SocketCallback& callback) { | 329 const SocketCallback& callback) { |
| 235 // TODO(serya): Implement | 330 // TODO(serya): Implement |
| 236 scoped_ptr<net::StreamSocket> socket; | 331 scoped_ptr<net::StreamSocket> socket; |
| 237 base::MessageLoop::current()->PostTask( | 332 base::MessageLoop::current()->PostTask( |
| 238 FROM_HERE, base::Bind(callback, net::ERR_FAILED, base::Passed(&socket))); | 333 FROM_HERE, base::Bind(callback, net::ERR_FAILED, base::Passed(&socket))); |
| 239 } | 334 } |
| OLD | NEW |