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" | |
| 8 #include "chrome/browser/signin/profile_identity_provider.h" | |
| 7 #include "chrome/browser/ui/browser.h" | 9 #include "chrome/browser/ui/browser.h" |
| 8 #include "chrome/common/url_constants.h" | 10 #include "chrome/common/url_constants.h" |
| 11 #include "content/public/browser/notification_observer.h" | |
| 12 #include "content/public/browser/notification_registrar.h" | |
| 13 #include "content/public/browser/notification_source.h" | |
| 9 #include "content/public/browser/web_contents.h" | 14 #include "content/public/browser/web_contents.h" |
| 10 #include "content/public/browser/web_ui_data_source.h" | 15 #include "content/public/browser/web_ui_data_source.h" |
| 11 #include "content/public/browser/web_ui_message_handler.h" | 16 #include "content/public/browser/web_ui_message_handler.h" |
| 17 #include "google_apis/gaia/identity_provider.h" | |
|
dgozman
2014/12/08 12:22:43
nit: let's remove this.
SeRya
2014/12/08 13:02:25
Done.
| |
| 12 #include "grit/webrtc_device_provider_resources_map.h" | 18 #include "grit/webrtc_device_provider_resources_map.h" |
| 19 #include "net/base/net_errors.h" | |
| 20 #include "net/socket/stream_socket.h" | |
| 13 #include "ui/base/page_transition_types.h" | 21 #include "ui/base/page_transition_types.h" |
| 14 | 22 |
| 23 using content::BrowserThread; | |
| 24 using content::NotificationDetails; | |
| 25 using content::NotificationObserver; | |
| 26 using content::NotificationRegistrar; | |
| 27 using content::NotificationSource; | |
| 28 using content::Source; | |
|
dgozman
2014/12/08 12:22:43
Let's drop this one.
SeRya
2014/12/08 13:02:25
Done.
| |
| 29 using content::WebContents; | |
| 30 using content::WebUIDataSource; | |
| 31 using content::WebUIMessageHandler; | |
| 32 | |
| 33 namespace { | |
| 34 | |
| 15 const char kBackgroundWorkerURL[] = | 35 const char kBackgroundWorkerURL[] = |
| 16 "chrome://webrtc-device-provider/background_worker.html"; | 36 "chrome://webrtc-device-provider/background_worker.html"; |
| 17 | 37 |
| 18 namespace { | 38 } // namespace |
| 19 | 39 |
| 20 class MessageHandler : public content::WebUIMessageHandler { | 40 // Lives on the UI thread. |
| 41 class WebRTCDeviceProvider::DevToolsBridgeClient : | |
| 42 private NotificationObserver, | |
| 43 private IdentityProvider::Observer { | |
| 21 public: | 44 public: |
| 22 explicit MessageHandler(WebRTCDeviceProvider* owner); | 45 static base::WeakPtr<DevToolsBridgeClient> Create( |
| 46 Profile* profile, | |
| 47 SigninManagerBase* signin_manager, | |
| 48 ProfileOAuth2TokenService* token_service); | |
| 49 | |
| 50 void DeleteSelf(); | |
| 51 | |
| 52 private: | |
| 53 DevToolsBridgeClient(Profile* profile, | |
| 54 SigninManagerBase* signin_manager, | |
| 55 ProfileOAuth2TokenService* token_service); | |
| 56 | |
| 57 ~DevToolsBridgeClient(); | |
| 58 | |
| 59 void CreateBackgroundWorker(); | |
| 60 | |
| 61 // Implementation of IdentityProvider::Observer. | |
| 62 void OnActiveAccountLogin() override; | |
| 63 void OnActiveAccountLogout() override; | |
| 64 | |
| 65 // Implementation of NotificationObserver. | |
| 66 void Observe(int type, | |
| 67 const NotificationSource& source, | |
| 68 const NotificationDetails& details) override; | |
| 69 | |
| 70 Profile* const profile_; | |
| 71 ProfileIdentityProvider identity_provider_; | |
| 72 NotificationRegistrar registrar_; | |
| 73 scoped_ptr<WebContents> background_worker_; | |
| 74 base::WeakPtrFactory<DevToolsBridgeClient> weak_factory_; | |
| 75 }; | |
| 76 | |
| 77 class WebRTCDeviceProvider::MessageHandler : public WebUIMessageHandler { | |
| 78 public: | |
| 79 explicit MessageHandler(DevToolsBridgeClient* owner); | |
| 23 | 80 |
| 24 void RegisterMessages() override; | 81 void RegisterMessages() override; |
| 25 | 82 |
| 26 private: | 83 private: |
| 27 void HandleLoaded(const base::ListValue* args); | 84 void HandleLoaded(const base::ListValue* args); |
| 28 | 85 |
| 29 WebRTCDeviceProvider* const owner_; | 86 DevToolsBridgeClient* const owner_; |
| 30 }; | 87 }; |
| 31 | 88 |
| 32 // MessageHandler ------------------------------------------------------------- | 89 // WebRTCDeviceProvider::WebUI ------------------------------------------------- |
| 33 | |
| 34 MessageHandler::MessageHandler( | |
| 35 WebRTCDeviceProvider* owner) : owner_(owner) { | |
| 36 } | |
| 37 | |
| 38 void MessageHandler::RegisterMessages() { | |
| 39 web_ui()->RegisterMessageCallback( | |
| 40 "loaded", | |
| 41 base::Bind(&MessageHandler::HandleLoaded, base::Unretained(this))); | |
| 42 } | |
| 43 | |
| 44 void MessageHandler::HandleLoaded( | |
| 45 const base::ListValue* args) { | |
| 46 if (!owner_) | |
| 47 return; | |
| 48 // TODO(serya): implement | |
| 49 } | |
| 50 | |
| 51 } // namespace | |
| 52 | |
| 53 // WebRTCDeviceProvider::WebUI ------------------------------------------------ | |
| 54 | 90 |
| 55 WebRTCDeviceProvider::WebUI::WebUI(content::WebUI* web_ui) | 91 WebRTCDeviceProvider::WebUI::WebUI(content::WebUI* web_ui) |
| 56 : content::WebUIController(web_ui) { | 92 : content::WebUIController(web_ui) { |
| 57 Profile* profile = Profile::FromWebUI(web_ui); | 93 Profile* profile = Profile::FromWebUI(web_ui); |
| 58 | 94 |
| 59 content::WebUIDataSource* source = content::WebUIDataSource::Create( | 95 WebUIDataSource* source = WebUIDataSource::Create( |
| 60 chrome::kChromeUIWebRTCDeviceProviderHost); | 96 chrome::kChromeUIWebRTCDeviceProviderHost); |
| 61 | 97 |
| 62 for (size_t i = 0; i < kWebrtcDeviceProviderResourcesSize; i++) { | 98 for (size_t i = 0; i < kWebrtcDeviceProviderResourcesSize; i++) { |
| 63 source->AddResourcePath(kWebrtcDeviceProviderResources[i].name, | 99 source->AddResourcePath(kWebrtcDeviceProviderResources[i].name, |
| 64 kWebrtcDeviceProviderResources[i].value); | 100 kWebrtcDeviceProviderResources[i].value); |
| 65 } | 101 } |
| 66 | 102 |
| 67 // Sets a stub message handler. If web contents was created by | 103 // Sets a stub message handler. If web contents was created by |
| 68 // WebRTCDeviceProvider message callbacks will be overridden by | 104 // WebRTCDeviceProvider message callbacks will be overridden by |
| 69 // a real implementation. | 105 // a real implementation. |
| 70 web_ui->AddMessageHandler(new MessageHandler(nullptr)); | 106 web_ui->AddMessageHandler(new MessageHandler(nullptr)); |
| 71 | 107 |
| 72 content::WebUIDataSource::Add(profile, source); | 108 WebUIDataSource::Add(profile, source); |
| 73 } | 109 } |
| 74 | 110 |
| 75 WebRTCDeviceProvider::WebUI::~WebUI() { | 111 WebRTCDeviceProvider::WebUI::~WebUI() { |
| 76 } | 112 } |
| 77 | 113 |
| 78 // WebRTCDeviceProvider ------------------------------------------------------- | 114 // WebRTCDeviceProvider::DevToolsBridgeClient ---------------------------------- |
| 79 | 115 |
| 80 WebRTCDeviceProvider::WebRTCDeviceProvider(content::BrowserContext* context) { | 116 // static |
| 81 background_worker_.reset(content::WebContents::Create( | 117 base::WeakPtr<WebRTCDeviceProvider::DevToolsBridgeClient> |
| 82 content::WebContents::CreateParams(context))); | 118 WebRTCDeviceProvider::DevToolsBridgeClient::Create( |
| 119 Profile* profile, | |
| 120 SigninManagerBase* signin_manager, | |
| 121 ProfileOAuth2TokenService* token_service) { | |
| 122 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 123 auto instance = new DevToolsBridgeClient( | |
| 124 profile, signin_manager, token_service); | |
| 125 return instance->weak_factory_.GetWeakPtr(); | |
| 126 } | |
| 83 | 127 |
| 84 // TODO(serya): Make sure background_worker_ destructed before profile. | 128 WebRTCDeviceProvider::DevToolsBridgeClient::DevToolsBridgeClient( |
| 129 Profile* profile, | |
| 130 SigninManagerBase* signin_manager, | |
| 131 ProfileOAuth2TokenService* token_service) | |
| 132 : profile_(profile), | |
| 133 identity_provider_(signin_manager, token_service, NULL), | |
|
dgozman
2014/12/08 12:22:43
nit: nullptr
SeRya
2014/12/08 13:02:25
Done.
| |
| 134 weak_factory_(this) { | |
| 135 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 136 | |
| 137 identity_provider_.AddObserver(this); | |
| 138 registrar_.Add(this, chrome::NOTIFICATION_PROFILE_DESTROYED, | |
| 139 Source<Profile>(profile_)); | |
| 140 | |
| 141 if (!identity_provider_.GetActiveAccountId().empty()) | |
| 142 CreateBackgroundWorker(); | |
| 143 } | |
| 144 | |
| 145 WebRTCDeviceProvider::DevToolsBridgeClient::~DevToolsBridgeClient() { | |
| 146 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 147 | |
| 148 identity_provider_.RemoveObserver(this); | |
| 149 } | |
| 150 | |
| 151 void WebRTCDeviceProvider::DevToolsBridgeClient::DeleteSelf() { | |
| 152 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 153 delete this; | |
| 154 } | |
| 155 | |
| 156 void WebRTCDeviceProvider::DevToolsBridgeClient::CreateBackgroundWorker() { | |
| 157 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 158 | |
| 159 background_worker_.reset( | |
| 160 WebContents::Create(WebContents::CreateParams(profile_))); | |
| 161 | |
| 85 GURL url(kBackgroundWorkerURL); | 162 GURL url(kBackgroundWorkerURL); |
| 86 | |
| 87 DCHECK_EQ(chrome::kChromeUIWebRTCDeviceProviderHost, url.host()); | 163 DCHECK_EQ(chrome::kChromeUIWebRTCDeviceProviderHost, url.host()); |
| 88 | 164 |
| 89 background_worker_->GetController().LoadURL( | 165 background_worker_->GetController().LoadURL( |
| 90 url, | 166 url, |
| 91 content::Referrer(), | 167 content::Referrer(), |
| 92 ui::PAGE_TRANSITION_AUTO_TOPLEVEL, | 168 ui::PAGE_TRANSITION_AUTO_TOPLEVEL, |
| 93 std::string()); | 169 std::string()); |
| 94 | 170 |
| 95 background_worker_->GetWebUI()->AddMessageHandler( | 171 background_worker_->GetWebUI()->AddMessageHandler( |
| 96 new MessageHandler(this)); | 172 new MessageHandler(this)); |
| 97 } | 173 } |
| 98 | 174 |
| 175 void WebRTCDeviceProvider::DevToolsBridgeClient::Observe( | |
| 176 int type, | |
| 177 const NotificationSource& source, | |
| 178 const NotificationDetails& details) { | |
| 179 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 180 DCHECK_EQ(chrome::NOTIFICATION_PROFILE_DESTROYED, type); | |
| 181 | |
| 182 delete this; | |
| 183 } | |
| 184 | |
| 185 void WebRTCDeviceProvider::DevToolsBridgeClient::OnActiveAccountLogin() { | |
| 186 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 187 CreateBackgroundWorker(); | |
| 188 } | |
| 189 | |
| 190 void WebRTCDeviceProvider::DevToolsBridgeClient::OnActiveAccountLogout() { | |
| 191 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 192 background_worker_.reset(); | |
| 193 } | |
| 194 | |
| 195 // WebRTCDeviceProvider::MessageHandler ---------------------------------------- | |
| 196 | |
| 197 WebRTCDeviceProvider::MessageHandler::MessageHandler( | |
| 198 DevToolsBridgeClient* owner) : owner_(owner) { | |
| 199 } | |
| 200 | |
| 201 void WebRTCDeviceProvider::MessageHandler::RegisterMessages() { | |
| 202 web_ui()->RegisterMessageCallback( | |
| 203 "loaded", | |
| 204 base::Bind(&MessageHandler::HandleLoaded, base::Unretained(this))); | |
| 205 } | |
| 206 | |
| 207 void WebRTCDeviceProvider::MessageHandler::HandleLoaded( | |
| 208 const base::ListValue* args) { | |
| 209 if (!owner_) | |
| 210 return; | |
| 211 // TODO(serya): implement | |
| 212 } | |
| 213 | |
| 214 // WebRTCDeviceProvider -------------------------------------------------------- | |
| 215 | |
| 216 WebRTCDeviceProvider::WebRTCDeviceProvider( | |
| 217 Profile* profile, | |
| 218 SigninManagerBase* signin_manager, | |
| 219 ProfileOAuth2TokenService* token_service) | |
| 220 : client_(DevToolsBridgeClient::Create( | |
| 221 profile, signin_manager, token_service)) { | |
| 222 } | |
| 223 | |
| 99 WebRTCDeviceProvider::~WebRTCDeviceProvider() { | 224 WebRTCDeviceProvider::~WebRTCDeviceProvider() { |
| 225 BrowserThread::PostTask( | |
| 226 BrowserThread::UI, | |
| 227 FROM_HERE, | |
| 228 base::Bind(&DevToolsBridgeClient::DeleteSelf, client_)); | |
| 100 } | 229 } |
| 101 | 230 |
| 102 void WebRTCDeviceProvider::QueryDevices(const SerialsCallback& callback) { | 231 void WebRTCDeviceProvider::QueryDevices(const SerialsCallback& callback) { |
| 103 // TODO(serya): Implement | 232 // TODO(serya): Implement |
| 233 base::MessageLoop::current()->PostTask( | |
| 234 FROM_HERE, base::Bind(callback, std::vector<std::string>())); | |
| 104 } | 235 } |
| 105 | 236 |
| 106 void WebRTCDeviceProvider::QueryDeviceInfo(const std::string& serial, | 237 void WebRTCDeviceProvider::QueryDeviceInfo(const std::string& serial, |
| 107 const DeviceInfoCallback& callback) { | 238 const DeviceInfoCallback& callback) { |
| 108 // TODO(serya): Implement | 239 // TODO(serya): Implement |
| 240 base::MessageLoop::current()->PostTask( | |
| 241 FROM_HERE, base::Bind(callback, AndroidDeviceManager::DeviceInfo())); | |
| 109 } | 242 } |
| 110 | 243 |
| 111 void WebRTCDeviceProvider::OpenSocket(const std::string& serial, | 244 void WebRTCDeviceProvider::OpenSocket(const std::string& serial, |
| 112 const std::string& socket_name, | 245 const std::string& socket_name, |
| 113 const SocketCallback& callback) { | 246 const SocketCallback& callback) { |
| 114 // TODO(serya): Implement | 247 // TODO(serya): Implement |
| 248 scoped_ptr<net::StreamSocket> socket; | |
| 249 base::MessageLoop::current()->PostTask( | |
| 250 FROM_HERE, base::Bind(callback, net::ERR_FAILED, base::Passed(&socket))); | |
| 115 } | 251 } |
| OLD | NEW |