| 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 27 matching lines...) Expand all Loading... |
| 351 | 351 |
| 352 base::StringValue service_key(kKeyPrefixMDns + name); | 352 base::StringValue service_key(kKeyPrefixMDns + name); |
| 353 | 353 |
| 354 if (description.id.empty()) { | 354 if (description.id.empty()) { |
| 355 info.SetString(kDictionaryKeyServiceName, name); | 355 info.SetString(kDictionaryKeyServiceName, name); |
| 356 info.SetString(kDictionaryKeyDisplayName, description.name); | 356 info.SetString(kDictionaryKeyDisplayName, description.name); |
| 357 info.SetString(kDictionaryKeyDescription, description.description); | 357 info.SetString(kDictionaryKeyDescription, description.description); |
| 358 info.SetString(kDictionaryKeyType, description.type); | 358 info.SetString(kDictionaryKeyType, description.type); |
| 359 info.SetBoolean(kDictionaryKeyIsWifi, false); | 359 info.SetBoolean(kDictionaryKeyIsWifi, false); |
| 360 | 360 |
| 361 web_ui()->CallJavascriptFunction( | 361 web_ui()->CallJavascriptFunctionUnsafe( |
| 362 "local_discovery.onUnregisteredDeviceUpdate", service_key, info); | 362 "local_discovery.onUnregisteredDeviceUpdate", service_key, info); |
| 363 } else { | 363 } else { |
| 364 std::unique_ptr<base::Value> null_value = base::Value::CreateNullValue(); | 364 std::unique_ptr<base::Value> null_value = base::Value::CreateNullValue(); |
| 365 | 365 |
| 366 web_ui()->CallJavascriptFunction( | 366 web_ui()->CallJavascriptFunctionUnsafe( |
| 367 "local_discovery.onUnregisteredDeviceUpdate", service_key, *null_value); | 367 "local_discovery.onUnregisteredDeviceUpdate", service_key, *null_value); |
| 368 } | 368 } |
| 369 } | 369 } |
| 370 | 370 |
| 371 void LocalDiscoveryUIHandler::DeviceRemoved(const std::string& name) { | 371 void LocalDiscoveryUIHandler::DeviceRemoved(const std::string& name) { |
| 372 device_descriptions_.erase(name); | 372 device_descriptions_.erase(name); |
| 373 std::unique_ptr<base::Value> null_value = base::Value::CreateNullValue(); | 373 std::unique_ptr<base::Value> null_value = base::Value::CreateNullValue(); |
| 374 base::StringValue name_value(kKeyPrefixMDns + name); | 374 base::StringValue name_value(kKeyPrefixMDns + name); |
| 375 | 375 |
| 376 web_ui()->CallJavascriptFunction("local_discovery.onUnregisteredDeviceUpdate", | 376 web_ui()->CallJavascriptFunctionUnsafe( |
| 377 name_value, *null_value); | 377 "local_discovery.onUnregisteredDeviceUpdate", name_value, *null_value); |
| 378 } | 378 } |
| 379 | 379 |
| 380 void LocalDiscoveryUIHandler::DeviceCacheFlushed() { | 380 void LocalDiscoveryUIHandler::DeviceCacheFlushed() { |
| 381 web_ui()->CallJavascriptFunction("local_discovery.onDeviceCacheFlushed"); | 381 web_ui()->CallJavascriptFunctionUnsafe( |
| 382 "local_discovery.onDeviceCacheFlushed"); |
| 382 privet_lister_->DiscoverNewDevices(false); | 383 privet_lister_->DiscoverNewDevices(false); |
| 383 } | 384 } |
| 384 | 385 |
| 385 void LocalDiscoveryUIHandler::OnDeviceListReady( | 386 void LocalDiscoveryUIHandler::OnDeviceListReady( |
| 386 const CloudPrintPrinterList::DeviceList& devices) { | 387 const CloudPrintPrinterList::DeviceList& devices) { |
| 387 cloud_devices_.insert(cloud_devices_.end(), devices.begin(), devices.end()); | 388 cloud_devices_.insert(cloud_devices_.end(), devices.begin(), devices.end()); |
| 388 ++succeded_list_count_; | 389 ++succeded_list_count_; |
| 389 CheckListingDone(); | 390 CheckListingDone(); |
| 390 } | 391 } |
| 391 | 392 |
| 392 void LocalDiscoveryUIHandler::OnDeviceListUnavailable() { | 393 void LocalDiscoveryUIHandler::OnDeviceListUnavailable() { |
| 393 ++failed_list_count_; | 394 ++failed_list_count_; |
| 394 CheckListingDone(); | 395 CheckListingDone(); |
| 395 } | 396 } |
| 396 | 397 |
| 397 void LocalDiscoveryUIHandler::GoogleSigninSucceeded( | 398 void LocalDiscoveryUIHandler::GoogleSigninSucceeded( |
| 398 const std::string& account_id, | 399 const std::string& account_id, |
| 399 const std::string& username, | 400 const std::string& username, |
| 400 const std::string& password) { | 401 const std::string& password) { |
| 401 CheckUserLoggedIn(); | 402 CheckUserLoggedIn(); |
| 402 } | 403 } |
| 403 | 404 |
| 404 void LocalDiscoveryUIHandler::GoogleSignedOut(const std::string& account_id, | 405 void LocalDiscoveryUIHandler::GoogleSignedOut(const std::string& account_id, |
| 405 const std::string& username) { | 406 const std::string& username) { |
| 406 CheckUserLoggedIn(); | 407 CheckUserLoggedIn(); |
| 407 } | 408 } |
| 408 | 409 |
| 409 void LocalDiscoveryUIHandler::SendRegisterError() { | 410 void LocalDiscoveryUIHandler::SendRegisterError() { |
| 410 web_ui()->CallJavascriptFunction("local_discovery.onRegistrationFailed"); | 411 web_ui()->CallJavascriptFunctionUnsafe( |
| 412 "local_discovery.onRegistrationFailed"); |
| 411 } | 413 } |
| 412 | 414 |
| 413 void LocalDiscoveryUIHandler::SendRegisterDone( | 415 void LocalDiscoveryUIHandler::SendRegisterDone( |
| 414 const std::string& service_name) { | 416 const std::string& service_name) { |
| 415 // HACK(noamsml): Generate network traffic so the Windows firewall doesn't | 417 // HACK(noamsml): Generate network traffic so the Windows firewall doesn't |
| 416 // block the printer's announcement. | 418 // block the printer's announcement. |
| 417 privet_lister_->DiscoverNewDevices(false); | 419 privet_lister_->DiscoverNewDevices(false); |
| 418 | 420 |
| 419 DeviceDescriptionMap::iterator it = device_descriptions_.find(service_name); | 421 DeviceDescriptionMap::iterator it = device_descriptions_.find(service_name); |
| 420 | 422 |
| 421 if (it == device_descriptions_.end()) { | 423 if (it == device_descriptions_.end()) { |
| 422 // TODO(noamsml): Handle the case where a printer's record is not present at | 424 // TODO(noamsml): Handle the case where a printer's record is not present at |
| 423 // the end of registration. | 425 // the end of registration. |
| 424 SendRegisterError(); | 426 SendRegisterError(); |
| 425 return; | 427 return; |
| 426 } | 428 } |
| 427 | 429 |
| 428 const DeviceDescription& device = it->second; | 430 const DeviceDescription& device = it->second; |
| 429 base::DictionaryValue device_value; | 431 base::DictionaryValue device_value; |
| 430 | 432 |
| 431 device_value.SetString(kDictionaryKeyType, device.type); | 433 device_value.SetString(kDictionaryKeyType, device.type); |
| 432 device_value.SetString(kDictionaryKeyID, device.id); | 434 device_value.SetString(kDictionaryKeyID, device.id); |
| 433 device_value.SetString(kDictionaryKeyDisplayName, device.name); | 435 device_value.SetString(kDictionaryKeyDisplayName, device.name); |
| 434 device_value.SetString(kDictionaryKeyDescription, device.description); | 436 device_value.SetString(kDictionaryKeyDescription, device.description); |
| 435 device_value.SetString(kDictionaryKeyServiceName, service_name); | 437 device_value.SetString(kDictionaryKeyServiceName, service_name); |
| 436 | 438 |
| 437 web_ui()->CallJavascriptFunction("local_discovery.onRegistrationSuccess", | 439 web_ui()->CallJavascriptFunctionUnsafe( |
| 438 device_value); | 440 "local_discovery.onRegistrationSuccess", device_value); |
| 439 } | 441 } |
| 440 | 442 |
| 441 void LocalDiscoveryUIHandler::SetIsVisible(bool visible) { | 443 void LocalDiscoveryUIHandler::SetIsVisible(bool visible) { |
| 442 if (visible == is_visible_) | 444 if (visible == is_visible_) |
| 443 return; | 445 return; |
| 444 | 446 |
| 445 g_num_visible += visible ? 1 : -1; | 447 g_num_visible += visible ? 1 : -1; |
| 446 is_visible_ = visible; | 448 is_visible_ = visible; |
| 447 } | 449 } |
| 448 | 450 |
| (...skipping 16 matching lines...) Expand all Loading... |
| 465 } | 467 } |
| 466 | 468 |
| 467 confirm_api_call_flow_.reset(); | 469 confirm_api_call_flow_.reset(); |
| 468 privet_resolution_.reset(); | 470 privet_resolution_.reset(); |
| 469 current_http_client_.reset(); | 471 current_http_client_.reset(); |
| 470 } | 472 } |
| 471 | 473 |
| 472 void LocalDiscoveryUIHandler::CheckUserLoggedIn() { | 474 void LocalDiscoveryUIHandler::CheckUserLoggedIn() { |
| 473 base::FundamentalValue logged_in_value(!GetSyncAccount().empty()); | 475 base::FundamentalValue logged_in_value(!GetSyncAccount().empty()); |
| 474 base::FundamentalValue is_supervised_value(IsUserSupervisedOrOffTheRecord()); | 476 base::FundamentalValue is_supervised_value(IsUserSupervisedOrOffTheRecord()); |
| 475 web_ui()->CallJavascriptFunction( | 477 web_ui()->CallJavascriptFunctionUnsafe("local_discovery.setUserLoggedIn", |
| 476 "local_discovery.setUserLoggedIn", logged_in_value, is_supervised_value); | 478 logged_in_value, is_supervised_value); |
| 477 } | 479 } |
| 478 | 480 |
| 479 void LocalDiscoveryUIHandler::CheckListingDone() { | 481 void LocalDiscoveryUIHandler::CheckListingDone() { |
| 480 int started = cloud_print_printer_list_ ? 1 : 0; | 482 int started = cloud_print_printer_list_ ? 1 : 0; |
| 481 if (started > failed_list_count_ + succeded_list_count_) | 483 if (started > failed_list_count_ + succeded_list_count_) |
| 482 return; | 484 return; |
| 483 | 485 |
| 484 if (succeded_list_count_ <= 0) { | 486 if (succeded_list_count_ <= 0) { |
| 485 web_ui()->CallJavascriptFunction( | 487 web_ui()->CallJavascriptFunctionUnsafe( |
| 486 "local_discovery.onCloudDeviceListUnavailable"); | 488 "local_discovery.onCloudDeviceListUnavailable"); |
| 487 return; | 489 return; |
| 488 } | 490 } |
| 489 | 491 |
| 490 base::ListValue devices_list; | 492 base::ListValue devices_list; |
| 491 std::set<std::string> local_ids; | 493 std::set<std::string> local_ids; |
| 492 | 494 |
| 493 for (const auto& it : device_descriptions_) | 495 for (const auto& it : device_descriptions_) |
| 494 local_ids.insert(it.second.id); | 496 local_ids.insert(it.second.id); |
| 495 | 497 |
| 496 ReadDevicesList(cloud_devices_, local_ids, &devices_list); | 498 ReadDevicesList(cloud_devices_, local_ids, &devices_list); |
| 497 | 499 |
| 498 web_ui()->CallJavascriptFunction( | 500 web_ui()->CallJavascriptFunctionUnsafe( |
| 499 "local_discovery.onCloudDeviceListAvailable", devices_list); | 501 "local_discovery.onCloudDeviceListAvailable", devices_list); |
| 500 cloud_print_printer_list_.reset(); | 502 cloud_print_printer_list_.reset(); |
| 501 } | 503 } |
| 502 | 504 |
| 503 std::unique_ptr<GCDApiFlow> LocalDiscoveryUIHandler::CreateApiFlow() { | 505 std::unique_ptr<GCDApiFlow> LocalDiscoveryUIHandler::CreateApiFlow() { |
| 504 Profile* profile = Profile::FromWebUI(web_ui()); | 506 Profile* profile = Profile::FromWebUI(web_ui()); |
| 505 if (!profile) | 507 if (!profile) |
| 506 return std::unique_ptr<GCDApiFlow>(); | 508 return std::unique_ptr<GCDApiFlow>(); |
| 507 | 509 |
| 508 ProfileOAuth2TokenService* token_service = | 510 ProfileOAuth2TokenService* token_service = |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 591 IDS_LOCAL_DISCOVERY_CLOUD_PRINT_CONNECTOR_DISABLED_LABEL, | 593 IDS_LOCAL_DISCOVERY_CLOUD_PRINT_CONNECTOR_DISABLED_LABEL, |
| 592 l10n_util::GetStringUTF16(IDS_GOOGLE_CLOUD_PRINT)); | 594 l10n_util::GetStringUTF16(IDS_GOOGLE_CLOUD_PRINT)); |
| 593 } else { | 595 } else { |
| 594 label_str = l10n_util::GetStringFUTF16( | 596 label_str = l10n_util::GetStringFUTF16( |
| 595 IDS_OPTIONS_CLOUD_PRINT_CONNECTOR_ENABLED_LABEL, | 597 IDS_OPTIONS_CLOUD_PRINT_CONNECTOR_ENABLED_LABEL, |
| 596 l10n_util::GetStringUTF16(IDS_GOOGLE_CLOUD_PRINT), | 598 l10n_util::GetStringUTF16(IDS_GOOGLE_CLOUD_PRINT), |
| 597 base::UTF8ToUTF16(email)); | 599 base::UTF8ToUTF16(email)); |
| 598 } | 600 } |
| 599 base::StringValue label(label_str); | 601 base::StringValue label(label_str); |
| 600 | 602 |
| 601 web_ui()->CallJavascriptFunction( | 603 web_ui()->CallJavascriptFunctionUnsafe( |
| 602 "local_discovery.setupCloudPrintConnectorSection", disabled, label, | 604 "local_discovery.setupCloudPrintConnectorSection", disabled, label, |
| 603 allowed); | 605 allowed); |
| 604 } | 606 } |
| 605 | 607 |
| 606 void LocalDiscoveryUIHandler::RefreshCloudPrintStatusFromService() { | 608 void LocalDiscoveryUIHandler::RefreshCloudPrintStatusFromService() { |
| 607 auto* service = GetCloudPrintProxyService(); | 609 auto* service = GetCloudPrintProxyService(); |
| 608 if (service) | 610 if (service) |
| 609 service->RefreshStatusFromService(); | 611 service->RefreshStatusFromService(); |
| 610 } | 612 } |
| 611 | 613 |
| 612 CloudPrintProxyService* LocalDiscoveryUIHandler::GetCloudPrintProxyService() { | 614 CloudPrintProxyService* LocalDiscoveryUIHandler::GetCloudPrintProxyService() { |
| 613 return CloudPrintProxyServiceFactory::GetForProfile( | 615 return CloudPrintProxyServiceFactory::GetForProfile( |
| 614 Profile::FromWebUI(web_ui())); | 616 Profile::FromWebUI(web_ui())); |
| 615 } | 617 } |
| 616 #endif // defined(CLOUD_PRINT_CONNECTOR_UI_AVAILABLE) | 618 #endif // defined(CLOUD_PRINT_CONNECTOR_UI_AVAILABLE) |
| 617 | 619 |
| 618 } // namespace local_discovery | 620 } // namespace local_discovery |
| OLD | NEW |