OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/ui/webui/local_discovery/local_discovery_ui_handler.h" | 5 #include "chrome/browser/ui/webui/local_discovery/local_discovery_ui_handler.h" |
6 | 6 |
7 #include <set> | 7 #include <set> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/command_line.h" | 11 #include "base/command_line.h" |
12 #include "base/message_loop/message_loop.h" | 12 #include "base/message_loop/message_loop.h" |
13 #include "base/prefs/pref_service.h" | 13 #include "base/prefs/pref_service.h" |
14 #include "base/strings/utf_string_conversions.h" | 14 #include "base/strings/utf_string_conversions.h" |
15 #include "base/values.h" | 15 #include "base/values.h" |
16 #include "build/build_config.h" | 16 #include "build/build_config.h" |
17 #include "chrome/browser/local_discovery/privet_confirm_api_flow.h" | |
18 #include "chrome/browser/local_discovery/privet_constants.h" | |
19 #include "chrome/browser/local_discovery/privet_device_lister_impl.h" | |
20 #include "chrome/browser/local_discovery/privet_http_asynchronous_factory.h" | |
21 #include "chrome/browser/local_discovery/service_discovery_shared_client.h" | 17 #include "chrome/browser/local_discovery/service_discovery_shared_client.h" |
22 #include "chrome/browser/printing/cloud_print/cloud_print_proxy_service.h" | 18 #include "chrome/browser/printing/cloud_print/cloud_print_proxy_service.h" |
23 #include "chrome/browser/printing/cloud_print/cloud_print_proxy_service_factory.
h" | 19 #include "chrome/browser/printing/cloud_print/cloud_print_proxy_service_factory.
h" |
| 20 #include "chrome/browser/printing/cloud_print/privet_confirm_api_flow.h" |
| 21 #include "chrome/browser/printing/cloud_print/privet_constants.h" |
| 22 #include "chrome/browser/printing/cloud_print/privet_device_lister_impl.h" |
| 23 #include "chrome/browser/printing/cloud_print/privet_http_asynchronous_factory.h
" |
24 #include "chrome/browser/profiles/profile.h" | 24 #include "chrome/browser/profiles/profile.h" |
25 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" | 25 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" |
26 #include "chrome/browser/signin/signin_manager_factory.h" | 26 #include "chrome/browser/signin/signin_manager_factory.h" |
27 #include "chrome/browser/ui/browser_finder.h" | 27 #include "chrome/browser/ui/browser_finder.h" |
28 #include "chrome/browser/ui/browser_tabstrip.h" | 28 #include "chrome/browser/ui/browser_tabstrip.h" |
29 #include "chrome/browser/ui/chrome_pages.h" | 29 #include "chrome/browser/ui/chrome_pages.h" |
30 #include "chrome/common/chrome_switches.h" | 30 #include "chrome/common/chrome_switches.h" |
31 #include "chrome/common/pref_names.h" | 31 #include "chrome/common/pref_names.h" |
32 #include "chrome/grit/generated_resources.h" | 32 #include "chrome/grit/generated_resources.h" |
33 #include "components/cloud_devices/common/cloud_devices_urls.h" | 33 #include "components/cloud_devices/common/cloud_devices_urls.h" |
34 #include "components/signin/core/browser/profile_oauth2_token_service.h" | 34 #include "components/signin/core/browser/profile_oauth2_token_service.h" |
35 #include "content/public/browser/user_metrics.h" | 35 #include "content/public/browser/user_metrics.h" |
36 #include "content/public/browser/web_ui.h" | 36 #include "content/public/browser/web_ui.h" |
37 #include "ui/base/l10n/l10n_util.h" | 37 #include "ui/base/l10n/l10n_util.h" |
38 | 38 |
39 #if defined(ENABLE_PRINT_PREVIEW) && !defined(OS_CHROMEOS) | 39 #if defined(ENABLE_PRINT_PREVIEW) && !defined(OS_CHROMEOS) |
40 #define CLOUD_PRINT_CONNECTOR_UI_AVAILABLE | 40 #define CLOUD_PRINT_CONNECTOR_UI_AVAILABLE |
41 #endif | 41 #endif |
42 | 42 |
| 43 using cloud_print::CloudPrintPrinterList; |
| 44 using cloud_print::DeviceDescription; |
| 45 using cloud_print::GCDApiFlow; |
| 46 using cloud_print::PrivetRegisterOperation; |
| 47 |
43 namespace local_discovery { | 48 namespace local_discovery { |
44 | 49 |
45 namespace { | 50 namespace { |
46 | 51 |
47 const char kDictionaryKeyServiceName[] = "service_name"; | 52 const char kDictionaryKeyServiceName[] = "service_name"; |
48 const char kDictionaryKeyDisplayName[] = "display_name"; | 53 const char kDictionaryKeyDisplayName[] = "display_name"; |
49 const char kDictionaryKeyDescription[] = "description"; | 54 const char kDictionaryKeyDescription[] = "description"; |
50 const char kDictionaryKeyType[] = "type"; | 55 const char kDictionaryKeyType[] = "type"; |
51 const char kDictionaryKeyIsWifi[] = "is_wifi"; | 56 const char kDictionaryKeyIsWifi[] = "is_wifi"; |
52 const char kDictionaryKeyID[] = "id"; | 57 const char kDictionaryKeyID[] = "id"; |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
162 } | 167 } |
163 | 168 |
164 void LocalDiscoveryUIHandler::HandleStart(const base::ListValue* args) { | 169 void LocalDiscoveryUIHandler::HandleStart(const base::ListValue* args) { |
165 Profile* profile = Profile::FromWebUI(web_ui()); | 170 Profile* profile = Profile::FromWebUI(web_ui()); |
166 | 171 |
167 // If privet_lister_ is already set, it is a mock used for tests or the result | 172 // If privet_lister_ is already set, it is a mock used for tests or the result |
168 // of a reload. | 173 // of a reload. |
169 if (!privet_lister_) { | 174 if (!privet_lister_) { |
170 service_discovery_client_ = ServiceDiscoverySharedClient::GetInstance(); | 175 service_discovery_client_ = ServiceDiscoverySharedClient::GetInstance(); |
171 privet_lister_.reset( | 176 privet_lister_.reset( |
172 new PrivetDeviceListerImpl(service_discovery_client_.get(), this)); | 177 new cloud_print::PrivetDeviceListerImpl(service_discovery_client_.get(), |
173 privet_http_factory_ = PrivetHTTPAsynchronousFactory::CreateInstance( | 178 this)); |
174 profile->GetRequestContext()); | 179 privet_http_factory_ = |
| 180 cloud_print::PrivetHTTPAsynchronousFactory::CreateInstance( |
| 181 profile->GetRequestContext()); |
175 | 182 |
176 SigninManagerBase* signin_manager = | 183 SigninManagerBase* signin_manager = |
177 SigninManagerFactory::GetInstance()->GetForProfile(profile); | 184 SigninManagerFactory::GetInstance()->GetForProfile(profile); |
178 if (signin_manager) | 185 if (signin_manager) |
179 signin_manager->AddObserver(this); | 186 signin_manager->AddObserver(this); |
180 } | 187 } |
181 | 188 |
182 privet_lister_->Start(); | 189 privet_lister_->Start(); |
183 privet_lister_->DiscoverNewDevices(false); | 190 privet_lister_->DiscoverNewDevices(false); |
184 | 191 |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
259 void LocalDiscoveryUIHandler::HandleShowSyncUI( | 266 void LocalDiscoveryUIHandler::HandleShowSyncUI( |
260 const base::ListValue* args) { | 267 const base::ListValue* args) { |
261 Browser* browser = chrome::FindBrowserWithWebContents( | 268 Browser* browser = chrome::FindBrowserWithWebContents( |
262 web_ui()->GetWebContents()); | 269 web_ui()->GetWebContents()); |
263 DCHECK(browser); | 270 DCHECK(browser); |
264 chrome::ShowBrowserSignin( | 271 chrome::ShowBrowserSignin( |
265 browser, signin_metrics::AccessPoint::ACCESS_POINT_DEVICES_PAGE); | 272 browser, signin_metrics::AccessPoint::ACCESS_POINT_DEVICES_PAGE); |
266 } | 273 } |
267 | 274 |
268 void LocalDiscoveryUIHandler::StartRegisterHTTP( | 275 void LocalDiscoveryUIHandler::StartRegisterHTTP( |
269 scoped_ptr<PrivetHTTPClient> http_client) { | 276 scoped_ptr<cloud_print::PrivetHTTPClient> http_client) { |
270 current_http_client_ = | 277 current_http_client_ = |
271 PrivetV1HTTPClient::CreateDefault(std::move(http_client)); | 278 cloud_print::PrivetV1HTTPClient::CreateDefault(std::move(http_client)); |
272 | 279 |
273 std::string user = GetSyncAccount(); | 280 std::string user = GetSyncAccount(); |
274 | 281 |
275 if (!current_http_client_) { | 282 if (!current_http_client_) { |
276 SendRegisterError(); | 283 SendRegisterError(); |
277 return; | 284 return; |
278 } | 285 } |
279 | 286 |
280 current_register_operation_ = | 287 current_register_operation_ = |
281 current_http_client_->CreateRegisterOperation(user, this); | 288 current_http_client_->CreateRegisterOperation(user, this); |
282 current_register_operation_->Start(); | 289 current_register_operation_->Start(); |
283 } | 290 } |
284 | 291 |
285 void LocalDiscoveryUIHandler::OnPrivetRegisterClaimToken( | 292 void LocalDiscoveryUIHandler::OnPrivetRegisterClaimToken( |
286 PrivetRegisterOperation* operation, | 293 cloud_print::PrivetRegisterOperation* operation, |
287 const std::string& token, | 294 const std::string& token, |
288 const GURL& url) { | 295 const GURL& url) { |
289 web_ui()->CallJavascriptFunction( | 296 web_ui()->CallJavascriptFunction( |
290 "local_discovery.onRegistrationConfirmedOnPrinter"); | 297 "local_discovery.onRegistrationConfirmedOnPrinter"); |
291 if (device_descriptions_.count(current_http_client_->GetName()) == 0) { | 298 if (device_descriptions_.count(current_http_client_->GetName()) == 0) { |
292 SendRegisterError(); | 299 SendRegisterError(); |
293 return; | 300 return; |
294 } | 301 } |
295 | 302 |
296 confirm_api_call_flow_ = CreateApiFlow(); | 303 confirm_api_call_flow_ = CreateApiFlow(); |
297 if (!confirm_api_call_flow_) { | 304 if (!confirm_api_call_flow_) { |
298 SendRegisterError(); | 305 SendRegisterError(); |
299 return; | 306 return; |
300 } | 307 } |
301 confirm_api_call_flow_->Start( | 308 confirm_api_call_flow_->Start( |
302 make_scoped_ptr<GCDApiFlow::Request>(new PrivetConfirmApiCallFlow( | 309 make_scoped_ptr<GCDApiFlow::Request>( |
303 token, | 310 new cloud_print::PrivetConfirmApiCallFlow( |
304 base::Bind(&LocalDiscoveryUIHandler::OnConfirmDone, | 311 token, |
305 base::Unretained(this))))); | 312 base::Bind(&LocalDiscoveryUIHandler::OnConfirmDone, |
| 313 base::Unretained(this))))); |
306 } | 314 } |
307 | 315 |
308 void LocalDiscoveryUIHandler::OnPrivetRegisterError( | 316 void LocalDiscoveryUIHandler::OnPrivetRegisterError( |
309 PrivetRegisterOperation* operation, | 317 PrivetRegisterOperation* operation, |
310 const std::string& action, | 318 const std::string& action, |
311 PrivetRegisterOperation::FailureReason reason, | 319 PrivetRegisterOperation::FailureReason reason, |
312 int printer_http_code, | 320 int printer_http_code, |
313 const base::DictionaryValue* json) { | 321 const base::DictionaryValue* json) { |
314 std::string error; | 322 std::string error; |
315 | 323 |
316 if (reason == PrivetRegisterOperation::FAILURE_JSON_ERROR && | 324 if (reason == PrivetRegisterOperation::FAILURE_JSON_ERROR && |
317 json->GetString(kPrivetKeyError, &error)) { | 325 json->GetString(cloud_print::kPrivetKeyError, &error)) { |
318 if (error == kPrivetErrorTimeout) { | 326 if (error == cloud_print::kPrivetErrorTimeout) { |
319 web_ui()->CallJavascriptFunction( | 327 web_ui()->CallJavascriptFunction( |
320 "local_discovery.onRegistrationTimeout"); | 328 "local_discovery.onRegistrationTimeout"); |
321 return; | 329 return; |
322 } else if (error == kPrivetErrorCancel) { | 330 } else if (error == cloud_print::kPrivetErrorCancel) { |
323 web_ui()->CallJavascriptFunction( | 331 web_ui()->CallJavascriptFunction( |
324 "local_discovery.onRegistrationCanceledPrinter"); | 332 "local_discovery.onRegistrationCanceledPrinter"); |
325 return; | 333 return; |
326 } | 334 } |
327 } | 335 } |
328 | 336 |
329 SendRegisterError(); | 337 SendRegisterError(); |
330 } | 338 } |
331 | 339 |
332 void LocalDiscoveryUIHandler::OnPrivetRegisterDone( | 340 void LocalDiscoveryUIHandler::OnPrivetRegisterDone( |
(...skipping 309 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
642 | 650 |
643 void LocalDiscoveryUIHandler::RefreshCloudPrintStatusFromService() { | 651 void LocalDiscoveryUIHandler::RefreshCloudPrintStatusFromService() { |
644 if (cloud_print_connector_ui_enabled_) | 652 if (cloud_print_connector_ui_enabled_) |
645 CloudPrintProxyServiceFactory::GetForProfile(Profile::FromWebUI(web_ui()))-> | 653 CloudPrintProxyServiceFactory::GetForProfile(Profile::FromWebUI(web_ui()))-> |
646 RefreshStatusFromService(); | 654 RefreshStatusFromService(); |
647 } | 655 } |
648 | 656 |
649 #endif // cloud print connector option stuff | 657 #endif // cloud print connector option stuff |
650 | 658 |
651 } // namespace local_discovery | 659 } // namespace local_discovery |
OLD | NEW |