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" | |
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; | |
29 using content::WebContents; | |
30 using content::WebUIDataSource; | |
31 using content::WebUIMessageHandler; | |
dgozman
2014/12/06 13:06:33
Using so much content classes may be confusing.
SeRya
2014/12/08 07:44:32
So what you suggest? "using namespace content;"?
| |
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, | |
dgozman
2014/12/06 13:06:33
Why use private inheritance?
SeRya
2014/12/08 07:44:32
Like with private members: if there is no need to
| |
43 private IdentityProvider::Observer { | |
21 public: | 44 public: |
22 explicit MessageHandler(WebRTCDeviceProvider* owner); | 45 static base::WeakPtr<DevToolsBridgeClient> Create( |
46 Profile* profile, ProfileIdentityProvider* identity_provider); | |
47 | |
48 void DeleteSelf(); | |
49 | |
50 private: | |
51 DevToolsBridgeClient(Profile* profile, | |
52 ProfileIdentityProvider* identity_provider); | |
53 | |
54 ~DevToolsBridgeClient(); | |
55 | |
56 void CreateBackgroundWorker(); | |
57 | |
58 // implementation of IdentityProvider::Observer. | |
dgozman
2014/12/06 13:06:33
nit (here and below): Implementation
SeRya
2014/12/08 07:44:32
Done.
| |
59 void OnActiveAccountLogin() override; | |
60 void OnActiveAccountLogout() override; | |
61 | |
62 // implementation of NotificationObserver. | |
63 void Observe(int type, | |
64 const NotificationSource& source, | |
65 const NotificationDetails& details) override; | |
66 | |
67 Profile* const profile_; | |
68 ProfileIdentityProvider* const identity_provider_; | |
69 NotificationRegistrar registrar_; | |
70 scoped_ptr<WebContents> background_worker_; | |
71 base::WeakPtrFactory<DevToolsBridgeClient> weak_factory_; | |
72 }; | |
73 | |
74 class WebRTCDeviceProvider::MessageHandler : public WebUIMessageHandler { | |
75 public: | |
76 explicit MessageHandler(DevToolsBridgeClient* owner); | |
23 | 77 |
24 void RegisterMessages() override; | 78 void RegisterMessages() override; |
25 | 79 |
26 private: | 80 private: |
27 void HandleLoaded(const base::ListValue* args); | 81 void HandleLoaded(const base::ListValue* args); |
28 | 82 |
29 WebRTCDeviceProvider* const owner_; | 83 DevToolsBridgeClient* const owner_; |
30 }; | 84 }; |
31 | 85 |
32 // MessageHandler ------------------------------------------------------------- | 86 // 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 | 87 |
55 WebRTCDeviceProvider::WebUI::WebUI(content::WebUI* web_ui) | 88 WebRTCDeviceProvider::WebUI::WebUI(content::WebUI* web_ui) |
56 : content::WebUIController(web_ui) { | 89 : content::WebUIController(web_ui) { |
57 Profile* profile = Profile::FromWebUI(web_ui); | 90 Profile* profile = Profile::FromWebUI(web_ui); |
58 | 91 |
59 content::WebUIDataSource* source = content::WebUIDataSource::Create( | 92 WebUIDataSource* source = WebUIDataSource::Create( |
60 chrome::kChromeUIWebRTCDeviceProviderHost); | 93 chrome::kChromeUIWebRTCDeviceProviderHost); |
61 | 94 |
62 for (size_t i = 0; i < kWebrtcDeviceProviderResourcesSize; i++) { | 95 for (size_t i = 0; i < kWebrtcDeviceProviderResourcesSize; i++) { |
63 source->AddResourcePath(kWebrtcDeviceProviderResources[i].name, | 96 source->AddResourcePath(kWebrtcDeviceProviderResources[i].name, |
64 kWebrtcDeviceProviderResources[i].value); | 97 kWebrtcDeviceProviderResources[i].value); |
65 } | 98 } |
66 | 99 |
67 // Sets a stub message handler. If web contents was created by | 100 // Sets a stub message handler. If web contents was created by |
68 // WebRTCDeviceProvider message callbacks will be overridden by | 101 // WebRTCDeviceProvider message callbacks will be overridden by |
69 // a real implementation. | 102 // a real implementation. |
70 web_ui->AddMessageHandler(new MessageHandler(nullptr)); | 103 web_ui->AddMessageHandler(new MessageHandler(nullptr)); |
71 | 104 |
72 content::WebUIDataSource::Add(profile, source); | 105 WebUIDataSource::Add(profile, source); |
73 } | 106 } |
74 | 107 |
75 WebRTCDeviceProvider::WebUI::~WebUI() { | 108 WebRTCDeviceProvider::WebUI::~WebUI() { |
76 } | 109 } |
77 | 110 |
78 // WebRTCDeviceProvider ------------------------------------------------------- | 111 // WebRTCDeviceProvider::DevToolsBridgeClient ---------------------------------- |
79 | 112 |
80 WebRTCDeviceProvider::WebRTCDeviceProvider(content::BrowserContext* context) { | 113 // static |
81 background_worker_.reset(content::WebContents::Create( | 114 base::WeakPtr<WebRTCDeviceProvider::DevToolsBridgeClient> |
82 content::WebContents::CreateParams(context))); | 115 WebRTCDeviceProvider::DevToolsBridgeClient::Create( |
dgozman
2014/12/06 13:06:33
This method seems to be called from handler thread
SeRya
2014/12/08 07:44:32
All providers are created on the UI thread from De
| |
116 Profile* profile, ProfileIdentityProvider* identity_provider) { | |
117 auto instance = new DevToolsBridgeClient(profile, identity_provider); | |
118 return instance->weak_factory_.GetWeakPtr(); | |
119 } | |
83 | 120 |
84 // TODO(serya): Make sure background_worker_ destructed before profile. | 121 WebRTCDeviceProvider::DevToolsBridgeClient::DevToolsBridgeClient( |
122 Profile* profile, ProfileIdentityProvider* identity_provider) | |
123 : profile_(profile), | |
dgozman
2014/12/06 13:06:33
nit: indentation
SeRya
2014/12/08 07:44:32
Done.
| |
124 identity_provider_(identity_provider), | |
125 weak_factory_(this) { | |
126 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
127 | |
128 identity_provider_->AddObserver(this); | |
129 registrar_.Add(this, chrome::NOTIFICATION_PROFILE_DESTROYED, | |
130 Source<Profile>(profile_)); | |
131 | |
132 if (!identity_provider_->GetActiveAccountId().empty()) | |
133 CreateBackgroundWorker(); | |
134 } | |
135 | |
136 WebRTCDeviceProvider::DevToolsBridgeClient::~DevToolsBridgeClient() { | |
137 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
138 | |
139 identity_provider_->RemoveObserver(this); | |
dgozman
2014/12/06 13:06:33
Is this provider still alive when NOTIFICATION_PRO
SeRya
2014/12/08 07:44:32
It is. And it's why NOTIFICATION_PROFILE_DESTROYED
| |
140 } | |
141 | |
142 void WebRTCDeviceProvider::DevToolsBridgeClient::DeleteSelf() { | |
dgozman
2014/12/06 13:06:33
Can we have 2 tests, which examine "DeleteSelf, th
SeRya
2014/12/08 07:44:32
Done.
| |
143 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
144 delete this; | |
145 } | |
146 | |
147 void WebRTCDeviceProvider::DevToolsBridgeClient::CreateBackgroundWorker() { | |
148 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
149 | |
150 background_worker_.reset( | |
151 WebContents::Create(WebContents::CreateParams(profile_))); | |
152 | |
85 GURL url(kBackgroundWorkerURL); | 153 GURL url(kBackgroundWorkerURL); |
86 | |
87 DCHECK_EQ(chrome::kChromeUIWebRTCDeviceProviderHost, url.host()); | 154 DCHECK_EQ(chrome::kChromeUIWebRTCDeviceProviderHost, url.host()); |
88 | 155 |
89 background_worker_->GetController().LoadURL( | 156 background_worker_->GetController().LoadURL( |
90 url, | 157 url, |
91 content::Referrer(), | 158 content::Referrer(), |
92 ui::PAGE_TRANSITION_AUTO_TOPLEVEL, | 159 ui::PAGE_TRANSITION_AUTO_TOPLEVEL, |
93 std::string()); | 160 std::string()); |
94 | 161 |
95 background_worker_->GetWebUI()->AddMessageHandler( | 162 background_worker_->GetWebUI()->AddMessageHandler( |
96 new MessageHandler(this)); | 163 new MessageHandler(this)); |
97 } | 164 } |
98 | 165 |
166 void WebRTCDeviceProvider::DevToolsBridgeClient::Observe( | |
167 int type, | |
168 const NotificationSource& source, | |
169 const NotificationDetails& details) { | |
170 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
171 DCHECK_EQ(chrome::NOTIFICATION_PROFILE_DESTROYED, type); | |
172 | |
173 delete this; | |
174 } | |
175 | |
176 void WebRTCDeviceProvider::DevToolsBridgeClient::OnActiveAccountLogin() { | |
177 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
178 CreateBackgroundWorker(); | |
179 } | |
180 | |
181 void WebRTCDeviceProvider::DevToolsBridgeClient::OnActiveAccountLogout() { | |
182 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
183 background_worker_.reset(); | |
184 } | |
185 | |
186 // WebRTCDeviceProvider::MessageHandler ---------------------------------------- | |
187 | |
188 WebRTCDeviceProvider::MessageHandler::MessageHandler( | |
dgozman
2014/12/06 13:06:33
Why has this changed to an inner class?
SeRya
2014/12/08 07:44:32
To have access to the private member DevToolsBridg
| |
189 DevToolsBridgeClient* owner) : owner_(owner) { | |
190 } | |
191 | |
192 void WebRTCDeviceProvider::MessageHandler::RegisterMessages() { | |
193 web_ui()->RegisterMessageCallback( | |
194 "loaded", | |
195 base::Bind(&MessageHandler::HandleLoaded, base::Unretained(this))); | |
196 } | |
197 | |
198 void WebRTCDeviceProvider::MessageHandler::HandleLoaded( | |
199 const base::ListValue* args) { | |
200 if (!owner_) | |
201 return; | |
202 // TODO(serya): implement | |
203 } | |
204 | |
205 // WebRTCDeviceProvider -------------------------------------------------------- | |
206 | |
207 WebRTCDeviceProvider::WebRTCDeviceProvider( | |
208 Profile* profile, ProfileIdentityProvider* identity_provider) | |
209 : client_(DevToolsBridgeClient::Create(profile, identity_provider)) { | |
210 } | |
211 | |
99 WebRTCDeviceProvider::~WebRTCDeviceProvider() { | 212 WebRTCDeviceProvider::~WebRTCDeviceProvider() { |
213 BrowserThread::PostTask( | |
214 BrowserThread::UI, | |
215 FROM_HERE, | |
216 base::Bind(&DevToolsBridgeClient::DeleteSelf, client_)); | |
100 } | 217 } |
101 | 218 |
102 void WebRTCDeviceProvider::QueryDevices(const SerialsCallback& callback) { | 219 void WebRTCDeviceProvider::QueryDevices(const SerialsCallback& callback) { |
103 // TODO(serya): Implement | 220 // TODO(serya): Implement |
221 base::MessageLoop::current()->PostTask( | |
222 FROM_HERE, base::Bind(callback, std::vector<std::string>())); | |
104 } | 223 } |
105 | 224 |
106 void WebRTCDeviceProvider::QueryDeviceInfo(const std::string& serial, | 225 void WebRTCDeviceProvider::QueryDeviceInfo(const std::string& serial, |
107 const DeviceInfoCallback& callback) { | 226 const DeviceInfoCallback& callback) { |
108 // TODO(serya): Implement | 227 // TODO(serya): Implement |
228 base::MessageLoop::current()->PostTask( | |
229 FROM_HERE, base::Bind(callback, AndroidDeviceManager::DeviceInfo())); | |
109 } | 230 } |
110 | 231 |
111 void WebRTCDeviceProvider::OpenSocket(const std::string& serial, | 232 void WebRTCDeviceProvider::OpenSocket(const std::string& serial, |
112 const std::string& socket_name, | 233 const std::string& socket_name, |
113 const SocketCallback& callback) { | 234 const SocketCallback& callback) { |
114 // TODO(serya): Implement | 235 // TODO(serya): Implement |
236 scoped_ptr<net::StreamSocket> socket; | |
237 base::MessageLoop::current()->PostTask( | |
238 FROM_HERE, base::Bind(callback, net::ERR_FAILED, base::Passed(&socket))); | |
115 } | 239 } |
OLD | NEW |