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" |
(...skipping 257 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
268 | 268 |
269 current_register_operation_ = | 269 current_register_operation_ = |
270 current_http_client_->CreateRegisterOperation(GetSyncAccount(), this); | 270 current_http_client_->CreateRegisterOperation(GetSyncAccount(), this); |
271 current_register_operation_->Start(); | 271 current_register_operation_->Start(); |
272 } | 272 } |
273 | 273 |
274 void LocalDiscoveryUIHandler::OnPrivetRegisterClaimToken( | 274 void LocalDiscoveryUIHandler::OnPrivetRegisterClaimToken( |
275 PrivetRegisterOperation* operation, | 275 PrivetRegisterOperation* operation, |
276 const std::string& token, | 276 const std::string& token, |
277 const GURL& url) { | 277 const GURL& url) { |
278 web_ui()->CallJavascriptFunction( | 278 web_ui()->CallJavascriptFunctionUnsafe( |
279 "local_discovery.onRegistrationConfirmedOnPrinter"); | 279 "local_discovery.onRegistrationConfirmedOnPrinter"); |
280 if (!ContainsKey(device_descriptions_, current_http_client_->GetName())) { | 280 if (!ContainsKey(device_descriptions_, current_http_client_->GetName())) { |
281 SendRegisterError(); | 281 SendRegisterError(); |
282 return; | 282 return; |
283 } | 283 } |
284 | 284 |
285 confirm_api_call_flow_ = CreateApiFlow(); | 285 confirm_api_call_flow_ = CreateApiFlow(); |
286 if (!confirm_api_call_flow_) { | 286 if (!confirm_api_call_flow_) { |
287 SendRegisterError(); | 287 SendRegisterError(); |
288 return; | 288 return; |
289 } | 289 } |
290 confirm_api_call_flow_->Start(base::WrapUnique<GCDApiFlow::Request>( | 290 confirm_api_call_flow_->Start(base::WrapUnique<GCDApiFlow::Request>( |
291 new cloud_print::PrivetConfirmApiCallFlow( | 291 new cloud_print::PrivetConfirmApiCallFlow( |
292 token, base::Bind(&LocalDiscoveryUIHandler::OnConfirmDone, | 292 token, base::Bind(&LocalDiscoveryUIHandler::OnConfirmDone, |
293 base::Unretained(this))))); | 293 base::Unretained(this))))); |
294 } | 294 } |
295 | 295 |
296 void LocalDiscoveryUIHandler::OnPrivetRegisterError( | 296 void LocalDiscoveryUIHandler::OnPrivetRegisterError( |
297 PrivetRegisterOperation* operation, | 297 PrivetRegisterOperation* operation, |
298 const std::string& action, | 298 const std::string& action, |
299 PrivetRegisterOperation::FailureReason reason, | 299 PrivetRegisterOperation::FailureReason reason, |
300 int printer_http_code, | 300 int printer_http_code, |
301 const base::DictionaryValue* json) { | 301 const base::DictionaryValue* json) { |
302 std::string error; | 302 std::string error; |
303 | 303 |
304 if (reason == PrivetRegisterOperation::FAILURE_JSON_ERROR && | 304 if (reason == PrivetRegisterOperation::FAILURE_JSON_ERROR && |
305 json->GetString(cloud_print::kPrivetKeyError, &error)) { | 305 json->GetString(cloud_print::kPrivetKeyError, &error)) { |
306 if (error == cloud_print::kPrivetErrorTimeout) { | 306 if (error == cloud_print::kPrivetErrorTimeout) { |
307 web_ui()->CallJavascriptFunction( | 307 web_ui()->CallJavascriptFunctionUnsafe( |
308 "local_discovery.onRegistrationTimeout"); | 308 "local_discovery.onRegistrationTimeout"); |
309 return; | 309 return; |
310 } | 310 } |
311 if (error == cloud_print::kPrivetErrorCancel) { | 311 if (error == cloud_print::kPrivetErrorCancel) { |
312 web_ui()->CallJavascriptFunction( | 312 web_ui()->CallJavascriptFunctionUnsafe( |
313 "local_discovery.onRegistrationCanceledPrinter"); | 313 "local_discovery.onRegistrationCanceledPrinter"); |
314 return; | 314 return; |
315 } | 315 } |
316 } | 316 } |
317 | 317 |
318 SendRegisterError(); | 318 SendRegisterError(); |
319 } | 319 } |
320 | 320 |
321 void LocalDiscoveryUIHandler::OnPrivetRegisterDone( | 321 void LocalDiscoveryUIHandler::OnPrivetRegisterDone( |
322 PrivetRegisterOperation* operation, | 322 PrivetRegisterOperation* operation, |
323 const std::string& device_id) { | 323 const std::string& device_id) { |
(...skipping 26 matching lines...) Expand all Loading... |
350 | 350 |
351 base::StringValue service_key(kKeyPrefixMDns + name); | 351 base::StringValue service_key(kKeyPrefixMDns + name); |
352 | 352 |
353 if (description.id.empty()) { | 353 if (description.id.empty()) { |
354 info.SetString(kDictionaryKeyServiceName, name); | 354 info.SetString(kDictionaryKeyServiceName, name); |
355 info.SetString(kDictionaryKeyDisplayName, description.name); | 355 info.SetString(kDictionaryKeyDisplayName, description.name); |
356 info.SetString(kDictionaryKeyDescription, description.description); | 356 info.SetString(kDictionaryKeyDescription, description.description); |
357 info.SetString(kDictionaryKeyType, description.type); | 357 info.SetString(kDictionaryKeyType, description.type); |
358 info.SetBoolean(kDictionaryKeyIsWifi, false); | 358 info.SetBoolean(kDictionaryKeyIsWifi, false); |
359 | 359 |
360 web_ui()->CallJavascriptFunction( | 360 web_ui()->CallJavascriptFunctionUnsafe( |
361 "local_discovery.onUnregisteredDeviceUpdate", service_key, info); | 361 "local_discovery.onUnregisteredDeviceUpdate", service_key, info); |
362 } else { | 362 } else { |
363 std::unique_ptr<base::Value> null_value = base::Value::CreateNullValue(); | 363 std::unique_ptr<base::Value> null_value = base::Value::CreateNullValue(); |
364 | 364 |
365 web_ui()->CallJavascriptFunction( | 365 web_ui()->CallJavascriptFunctionUnsafe( |
366 "local_discovery.onUnregisteredDeviceUpdate", service_key, *null_value); | 366 "local_discovery.onUnregisteredDeviceUpdate", service_key, *null_value); |
367 } | 367 } |
368 } | 368 } |
369 | 369 |
370 void LocalDiscoveryUIHandler::DeviceRemoved(const std::string& name) { | 370 void LocalDiscoveryUIHandler::DeviceRemoved(const std::string& name) { |
371 device_descriptions_.erase(name); | 371 device_descriptions_.erase(name); |
372 std::unique_ptr<base::Value> null_value = base::Value::CreateNullValue(); | 372 std::unique_ptr<base::Value> null_value = base::Value::CreateNullValue(); |
373 base::StringValue name_value(kKeyPrefixMDns + name); | 373 base::StringValue name_value(kKeyPrefixMDns + name); |
374 | 374 |
375 web_ui()->CallJavascriptFunction("local_discovery.onUnregisteredDeviceUpdate", | 375 web_ui()->CallJavascriptFunctionUnsafe( |
376 name_value, *null_value); | 376 "local_discovery.onUnregisteredDeviceUpdate", name_value, *null_value); |
377 } | 377 } |
378 | 378 |
379 void LocalDiscoveryUIHandler::DeviceCacheFlushed() { | 379 void LocalDiscoveryUIHandler::DeviceCacheFlushed() { |
380 web_ui()->CallJavascriptFunction("local_discovery.onDeviceCacheFlushed"); | 380 web_ui()->CallJavascriptFunctionUnsafe( |
| 381 "local_discovery.onDeviceCacheFlushed"); |
381 privet_lister_->DiscoverNewDevices(false); | 382 privet_lister_->DiscoverNewDevices(false); |
382 } | 383 } |
383 | 384 |
384 void LocalDiscoveryUIHandler::OnDeviceListReady( | 385 void LocalDiscoveryUIHandler::OnDeviceListReady( |
385 const CloudPrintPrinterList::DeviceList& devices) { | 386 const CloudPrintPrinterList::DeviceList& devices) { |
386 cloud_devices_.insert(cloud_devices_.end(), devices.begin(), devices.end()); | 387 cloud_devices_.insert(cloud_devices_.end(), devices.begin(), devices.end()); |
387 ++succeded_list_count_; | 388 ++succeded_list_count_; |
388 CheckListingDone(); | 389 CheckListingDone(); |
389 } | 390 } |
390 | 391 |
391 void LocalDiscoveryUIHandler::OnDeviceListUnavailable() { | 392 void LocalDiscoveryUIHandler::OnDeviceListUnavailable() { |
392 ++failed_list_count_; | 393 ++failed_list_count_; |
393 CheckListingDone(); | 394 CheckListingDone(); |
394 } | 395 } |
395 | 396 |
396 void LocalDiscoveryUIHandler::GoogleSigninSucceeded( | 397 void LocalDiscoveryUIHandler::GoogleSigninSucceeded( |
397 const std::string& account_id, | 398 const std::string& account_id, |
398 const std::string& username, | 399 const std::string& username, |
399 const std::string& password) { | 400 const std::string& password) { |
400 CheckUserLoggedIn(); | 401 CheckUserLoggedIn(); |
401 } | 402 } |
402 | 403 |
403 void LocalDiscoveryUIHandler::GoogleSignedOut(const std::string& account_id, | 404 void LocalDiscoveryUIHandler::GoogleSignedOut(const std::string& account_id, |
404 const std::string& username) { | 405 const std::string& username) { |
405 CheckUserLoggedIn(); | 406 CheckUserLoggedIn(); |
406 } | 407 } |
407 | 408 |
408 void LocalDiscoveryUIHandler::SendRegisterError() { | 409 void LocalDiscoveryUIHandler::SendRegisterError() { |
409 web_ui()->CallJavascriptFunction("local_discovery.onRegistrationFailed"); | 410 web_ui()->CallJavascriptFunctionUnsafe( |
| 411 "local_discovery.onRegistrationFailed"); |
410 } | 412 } |
411 | 413 |
412 void LocalDiscoveryUIHandler::SendRegisterDone( | 414 void LocalDiscoveryUIHandler::SendRegisterDone( |
413 const std::string& service_name) { | 415 const std::string& service_name) { |
414 // HACK(noamsml): Generate network traffic so the Windows firewall doesn't | 416 // HACK(noamsml): Generate network traffic so the Windows firewall doesn't |
415 // block the printer's announcement. | 417 // block the printer's announcement. |
416 privet_lister_->DiscoverNewDevices(false); | 418 privet_lister_->DiscoverNewDevices(false); |
417 | 419 |
418 DeviceDescriptionMap::iterator it = device_descriptions_.find(service_name); | 420 DeviceDescriptionMap::iterator it = device_descriptions_.find(service_name); |
419 | 421 |
420 if (it == device_descriptions_.end()) { | 422 if (it == device_descriptions_.end()) { |
421 // TODO(noamsml): Handle the case where a printer's record is not present at | 423 // TODO(noamsml): Handle the case where a printer's record is not present at |
422 // the end of registration. | 424 // the end of registration. |
423 SendRegisterError(); | 425 SendRegisterError(); |
424 return; | 426 return; |
425 } | 427 } |
426 | 428 |
427 const DeviceDescription& device = it->second; | 429 const DeviceDescription& device = it->second; |
428 base::DictionaryValue device_value; | 430 base::DictionaryValue device_value; |
429 | 431 |
430 device_value.SetString(kDictionaryKeyType, device.type); | 432 device_value.SetString(kDictionaryKeyType, device.type); |
431 device_value.SetString(kDictionaryKeyID, device.id); | 433 device_value.SetString(kDictionaryKeyID, device.id); |
432 device_value.SetString(kDictionaryKeyDisplayName, device.name); | 434 device_value.SetString(kDictionaryKeyDisplayName, device.name); |
433 device_value.SetString(kDictionaryKeyDescription, device.description); | 435 device_value.SetString(kDictionaryKeyDescription, device.description); |
434 device_value.SetString(kDictionaryKeyServiceName, service_name); | 436 device_value.SetString(kDictionaryKeyServiceName, service_name); |
435 | 437 |
436 web_ui()->CallJavascriptFunction("local_discovery.onRegistrationSuccess", | 438 web_ui()->CallJavascriptFunctionUnsafe( |
437 device_value); | 439 "local_discovery.onRegistrationSuccess", device_value); |
438 } | 440 } |
439 | 441 |
440 void LocalDiscoveryUIHandler::SetIsVisible(bool visible) { | 442 void LocalDiscoveryUIHandler::SetIsVisible(bool visible) { |
441 if (visible == is_visible_) | 443 if (visible == is_visible_) |
442 return; | 444 return; |
443 | 445 |
444 g_num_visible += visible ? 1 : -1; | 446 g_num_visible += visible ? 1 : -1; |
445 is_visible_ = visible; | 447 is_visible_ = visible; |
446 } | 448 } |
447 | 449 |
(...skipping 16 matching lines...) Expand all Loading... |
464 } | 466 } |
465 | 467 |
466 confirm_api_call_flow_.reset(); | 468 confirm_api_call_flow_.reset(); |
467 privet_resolution_.reset(); | 469 privet_resolution_.reset(); |
468 current_http_client_.reset(); | 470 current_http_client_.reset(); |
469 } | 471 } |
470 | 472 |
471 void LocalDiscoveryUIHandler::CheckUserLoggedIn() { | 473 void LocalDiscoveryUIHandler::CheckUserLoggedIn() { |
472 base::FundamentalValue logged_in_value(!GetSyncAccount().empty()); | 474 base::FundamentalValue logged_in_value(!GetSyncAccount().empty()); |
473 base::FundamentalValue is_supervised_value(IsUserSupervisedOrOffTheRecord()); | 475 base::FundamentalValue is_supervised_value(IsUserSupervisedOrOffTheRecord()); |
474 web_ui()->CallJavascriptFunction( | 476 web_ui()->CallJavascriptFunctionUnsafe("local_discovery.setUserLoggedIn", |
475 "local_discovery.setUserLoggedIn", logged_in_value, is_supervised_value); | 477 logged_in_value, is_supervised_value); |
476 } | 478 } |
477 | 479 |
478 void LocalDiscoveryUIHandler::CheckListingDone() { | 480 void LocalDiscoveryUIHandler::CheckListingDone() { |
479 int started = cloud_print_printer_list_ ? 1 : 0; | 481 int started = cloud_print_printer_list_ ? 1 : 0; |
480 if (started > failed_list_count_ + succeded_list_count_) | 482 if (started > failed_list_count_ + succeded_list_count_) |
481 return; | 483 return; |
482 | 484 |
483 if (succeded_list_count_ <= 0) { | 485 if (succeded_list_count_ <= 0) { |
484 web_ui()->CallJavascriptFunction( | 486 web_ui()->CallJavascriptFunctionUnsafe( |
485 "local_discovery.onCloudDeviceListUnavailable"); | 487 "local_discovery.onCloudDeviceListUnavailable"); |
486 return; | 488 return; |
487 } | 489 } |
488 | 490 |
489 base::ListValue devices_list; | 491 base::ListValue devices_list; |
490 std::set<std::string> local_ids; | 492 std::set<std::string> local_ids; |
491 | 493 |
492 for (const auto& it : device_descriptions_) | 494 for (const auto& it : device_descriptions_) |
493 local_ids.insert(it.second.id); | 495 local_ids.insert(it.second.id); |
494 | 496 |
495 ReadDevicesList(cloud_devices_, local_ids, &devices_list); | 497 ReadDevicesList(cloud_devices_, local_ids, &devices_list); |
496 | 498 |
497 web_ui()->CallJavascriptFunction( | 499 web_ui()->CallJavascriptFunctionUnsafe( |
498 "local_discovery.onCloudDeviceListAvailable", devices_list); | 500 "local_discovery.onCloudDeviceListAvailable", devices_list); |
499 cloud_print_printer_list_.reset(); | 501 cloud_print_printer_list_.reset(); |
500 } | 502 } |
501 | 503 |
502 std::unique_ptr<GCDApiFlow> LocalDiscoveryUIHandler::CreateApiFlow() { | 504 std::unique_ptr<GCDApiFlow> LocalDiscoveryUIHandler::CreateApiFlow() { |
503 Profile* profile = Profile::FromWebUI(web_ui()); | 505 Profile* profile = Profile::FromWebUI(web_ui()); |
504 if (!profile) | 506 if (!profile) |
505 return std::unique_ptr<GCDApiFlow>(); | 507 return std::unique_ptr<GCDApiFlow>(); |
506 | 508 |
507 ProfileOAuth2TokenService* token_service = | 509 ProfileOAuth2TokenService* token_service = |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
590 IDS_LOCAL_DISCOVERY_CLOUD_PRINT_CONNECTOR_DISABLED_LABEL, | 592 IDS_LOCAL_DISCOVERY_CLOUD_PRINT_CONNECTOR_DISABLED_LABEL, |
591 l10n_util::GetStringUTF16(IDS_GOOGLE_CLOUD_PRINT)); | 593 l10n_util::GetStringUTF16(IDS_GOOGLE_CLOUD_PRINT)); |
592 } else { | 594 } else { |
593 label_str = l10n_util::GetStringFUTF16( | 595 label_str = l10n_util::GetStringFUTF16( |
594 IDS_OPTIONS_CLOUD_PRINT_CONNECTOR_ENABLED_LABEL, | 596 IDS_OPTIONS_CLOUD_PRINT_CONNECTOR_ENABLED_LABEL, |
595 l10n_util::GetStringUTF16(IDS_GOOGLE_CLOUD_PRINT), | 597 l10n_util::GetStringUTF16(IDS_GOOGLE_CLOUD_PRINT), |
596 base::UTF8ToUTF16(email)); | 598 base::UTF8ToUTF16(email)); |
597 } | 599 } |
598 base::StringValue label(label_str); | 600 base::StringValue label(label_str); |
599 | 601 |
600 web_ui()->CallJavascriptFunction( | 602 web_ui()->CallJavascriptFunctionUnsafe( |
601 "local_discovery.setupCloudPrintConnectorSection", disabled, label, | 603 "local_discovery.setupCloudPrintConnectorSection", disabled, label, |
602 allowed); | 604 allowed); |
603 } | 605 } |
604 | 606 |
605 void LocalDiscoveryUIHandler::RefreshCloudPrintStatusFromService() { | 607 void LocalDiscoveryUIHandler::RefreshCloudPrintStatusFromService() { |
606 auto* service = GetCloudPrintProxyService(); | 608 auto* service = GetCloudPrintProxyService(); |
607 if (service) | 609 if (service) |
608 service->RefreshStatusFromService(); | 610 service->RefreshStatusFromService(); |
609 } | 611 } |
610 | 612 |
611 CloudPrintProxyService* LocalDiscoveryUIHandler::GetCloudPrintProxyService() { | 613 CloudPrintProxyService* LocalDiscoveryUIHandler::GetCloudPrintProxyService() { |
612 return CloudPrintProxyServiceFactory::GetForProfile( | 614 return CloudPrintProxyServiceFactory::GetForProfile( |
613 Profile::FromWebUI(web_ui())); | 615 Profile::FromWebUI(web_ui())); |
614 } | 616 } |
615 #endif // defined(CLOUD_PRINT_CONNECTOR_UI_AVAILABLE) | 617 #endif // defined(CLOUD_PRINT_CONNECTOR_UI_AVAILABLE) |
616 | 618 |
617 } // namespace local_discovery | 619 } // namespace local_discovery |
OLD | NEW |