| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/print_preview/print_preview_handler.h" | 5 #include "chrome/browser/ui/webui/print_preview/print_preview_handler.h" |
| 6 | 6 |
| 7 #include <ctype.h> | 7 #include <ctype.h> |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 | 9 |
| 10 #include <map> | 10 #include <map> |
| (...skipping 458 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 469 } // namespace | 469 } // namespace |
| 470 | 470 |
| 471 class PrintPreviewHandler::AccessTokenService | 471 class PrintPreviewHandler::AccessTokenService |
| 472 : public OAuth2TokenService::Consumer { | 472 : public OAuth2TokenService::Consumer { |
| 473 public: | 473 public: |
| 474 explicit AccessTokenService(PrintPreviewHandler* handler) | 474 explicit AccessTokenService(PrintPreviewHandler* handler) |
| 475 : OAuth2TokenService::Consumer("print_preview"), | 475 : OAuth2TokenService::Consumer("print_preview"), |
| 476 handler_(handler) { | 476 handler_(handler) { |
| 477 } | 477 } |
| 478 | 478 |
| 479 void RequestToken(const std::string& type) { | 479 void RequestToken(const std::string& type, const std::string& callback_id) { |
| 480 if (requests_.find(type) != requests_.end()) | 480 if (requests_.find(type) != requests_.end()) |
| 481 return; // Already in progress. | 481 return; // Should never happen, see cloud_print_interface.js |
| 482 | 482 |
| 483 OAuth2TokenService* service = NULL; | 483 OAuth2TokenService* service = NULL; |
| 484 std::string account_id; | 484 std::string account_id; |
| 485 if (type == "profile") { | 485 if (type == "profile") { |
| 486 Profile* profile = Profile::FromWebUI(handler_->web_ui()); | 486 Profile* profile = Profile::FromWebUI(handler_->web_ui()); |
| 487 if (profile) { | 487 if (profile) { |
| 488 ProfileOAuth2TokenService* token_service = | 488 ProfileOAuth2TokenService* token_service = |
| 489 ProfileOAuth2TokenServiceFactory::GetForProfile(profile); | 489 ProfileOAuth2TokenServiceFactory::GetForProfile(profile); |
| 490 SigninManagerBase* signin_manager = | 490 SigninManagerBase* signin_manager = |
| 491 SigninManagerFactory::GetInstance()->GetForProfile(profile); | 491 SigninManagerFactory::GetInstance()->GetForProfile(profile); |
| 492 account_id = signin_manager->GetAuthenticatedAccountId(); | 492 account_id = signin_manager->GetAuthenticatedAccountId(); |
| 493 service = token_service; | 493 service = token_service; |
| 494 } | 494 } |
| 495 } else if (type == "device") { | 495 } else if (type == "device") { |
| 496 #if defined(OS_CHROMEOS) | 496 #if defined(OS_CHROMEOS) |
| 497 chromeos::DeviceOAuth2TokenService* token_service = | 497 chromeos::DeviceOAuth2TokenService* token_service = |
| 498 chromeos::DeviceOAuth2TokenServiceFactory::Get(); | 498 chromeos::DeviceOAuth2TokenServiceFactory::Get(); |
| 499 account_id = token_service->GetRobotAccountId(); | 499 account_id = token_service->GetRobotAccountId(); |
| 500 service = token_service; | 500 service = token_service; |
| 501 #endif | 501 #endif |
| 502 } | 502 } |
| 503 | 503 |
| 504 if (service) { | 504 if (service) { |
| 505 OAuth2TokenService::ScopeSet oauth_scopes; | 505 OAuth2TokenService::ScopeSet oauth_scopes; |
| 506 oauth_scopes.insert(cloud_devices::kCloudPrintAuthScope); | 506 oauth_scopes.insert(cloud_devices::kCloudPrintAuthScope); |
| 507 std::unique_ptr<OAuth2TokenService::Request> request( | 507 std::unique_ptr<OAuth2TokenService::Request> request( |
| 508 service->StartRequest(account_id, oauth_scopes, this)); | 508 service->StartRequest(account_id, oauth_scopes, this)); |
| 509 requests_[type] = std::move(request); | 509 requests_[type] = std::move(request); |
| 510 callbacks_[type] = callback_id; |
| 510 } else { | 511 } else { |
| 511 handler_->SendAccessToken(type, std::string()); // Unknown type. | 512 // Unknown type. |
| 513 handler_->SendAccessToken(callback_id, std::string()); |
| 512 } | 514 } |
| 513 } | 515 } |
| 514 | 516 |
| 515 void OnGetTokenSuccess(const OAuth2TokenService::Request* request, | 517 void OnGetTokenSuccess(const OAuth2TokenService::Request* request, |
| 516 const std::string& access_token, | 518 const std::string& access_token, |
| 517 const base::Time& expiration_time) override { | 519 const base::Time& expiration_time) override { |
| 518 OnServiceResponce(request, access_token); | 520 OnServiceResponse(request, access_token); |
| 519 } | 521 } |
| 520 | 522 |
| 521 void OnGetTokenFailure(const OAuth2TokenService::Request* request, | 523 void OnGetTokenFailure(const OAuth2TokenService::Request* request, |
| 522 const GoogleServiceAuthError& error) override { | 524 const GoogleServiceAuthError& error) override { |
| 523 OnServiceResponce(request, std::string()); | 525 OnServiceResponse(request, std::string()); |
| 524 } | 526 } |
| 525 | 527 |
| 526 private: | 528 private: |
| 527 void OnServiceResponce(const OAuth2TokenService::Request* request, | 529 void OnServiceResponse(const OAuth2TokenService::Request* request, |
| 528 const std::string& access_token) { | 530 const std::string& access_token) { |
| 529 for (Requests::iterator i = requests_.begin(); i != requests_.end(); ++i) { | 531 for (Requests::iterator i = requests_.begin(); i != requests_.end(); ++i) { |
| 530 if (i->second.get() == request) { | 532 if (i->second.get() == request) { |
| 531 handler_->SendAccessToken(i->first, access_token); | 533 handler_->SendAccessToken(callbacks_[i->first], access_token); |
| 532 requests_.erase(i); | 534 requests_.erase(i); |
| 535 callbacks_.erase(i->first); |
| 533 return; | 536 return; |
| 534 } | 537 } |
| 535 } | 538 } |
| 536 NOTREACHED(); | 539 NOTREACHED(); |
| 537 } | 540 } |
| 538 | 541 |
| 539 using Requests = | 542 using Requests = |
| 540 std::map<std::string, std::unique_ptr<OAuth2TokenService::Request>>; | 543 std::map<std::string, std::unique_ptr<OAuth2TokenService::Request>>; |
| 541 Requests requests_; | 544 Requests requests_; |
| 545 using Callbacks = std::map<std::string, std::string>; |
| 546 Callbacks callbacks_; |
| 542 PrintPreviewHandler* handler_; | 547 PrintPreviewHandler* handler_; |
| 543 | 548 |
| 544 DISALLOW_COPY_AND_ASSIGN(AccessTokenService); | 549 DISALLOW_COPY_AND_ASSIGN(AccessTokenService); |
| 545 }; | 550 }; |
| 546 | 551 |
| 547 PrintPreviewHandler::PrintPreviewHandler() | 552 PrintPreviewHandler::PrintPreviewHandler() |
| 548 : regenerate_preview_request_count_(0), | 553 : regenerate_preview_request_count_(0), |
| 549 manage_printers_dialog_request_count_(0), | 554 manage_printers_dialog_request_count_(0), |
| 550 manage_cloud_printers_dialog_request_count_(0), | 555 manage_cloud_printers_dialog_request_count_(0), |
| 551 reported_failed_preview_(false), | 556 reported_failed_preview_(false), |
| (...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 738 // Make sure all in progress requests are canceled before new printer search | 743 // Make sure all in progress requests are canceled before new printer search |
| 739 // starts. | 744 // starts. |
| 740 extension_printer_handler_->Reset(); | 745 extension_printer_handler_->Reset(); |
| 741 extension_printer_handler_->StartGetPrinters( | 746 extension_printer_handler_->StartGetPrinters( |
| 742 base::Bind(&PrintPreviewHandler::OnGotPrintersForExtension, | 747 base::Bind(&PrintPreviewHandler::OnGotPrintersForExtension, |
| 743 weak_factory_.GetWeakPtr(), callback_id)); | 748 weak_factory_.GetWeakPtr(), callback_id)); |
| 744 } | 749 } |
| 745 | 750 |
| 746 void PrintPreviewHandler::HandleGrantExtensionPrinterAccess( | 751 void PrintPreviewHandler::HandleGrantExtensionPrinterAccess( |
| 747 const base::ListValue* args) { | 752 const base::ListValue* args) { |
| 753 std::string callback_id; |
| 748 std::string printer_id; | 754 std::string printer_id; |
| 749 bool ok = args->GetString(0, &printer_id); | 755 bool ok = args->GetString(0, &callback_id) && |
| 756 args->GetString(1, &printer_id) && !callback_id.empty(); |
| 750 DCHECK(ok); | 757 DCHECK(ok); |
| 751 | 758 |
| 759 AllowJavascript(); |
| 752 EnsureExtensionPrinterHandlerSet(); | 760 EnsureExtensionPrinterHandlerSet(); |
| 753 extension_printer_handler_->StartGrantPrinterAccess( | 761 extension_printer_handler_->StartGrantPrinterAccess( |
| 754 printer_id, base::Bind(&PrintPreviewHandler::OnGotExtensionPrinterInfo, | 762 printer_id, base::Bind(&PrintPreviewHandler::OnGotExtensionPrinterInfo, |
| 755 weak_factory_.GetWeakPtr(), printer_id)); | 763 weak_factory_.GetWeakPtr(), callback_id)); |
| 756 } | 764 } |
| 757 | 765 |
| 758 void PrintPreviewHandler::HandleGetExtensionPrinterCapabilities( | 766 void PrintPreviewHandler::HandleGetExtensionPrinterCapabilities( |
| 759 const base::ListValue* args) { | 767 const base::ListValue* args) { |
| 760 AllowJavascript(); | 768 AllowJavascript(); |
| 761 | 769 |
| 762 std::string callback_id; | 770 std::string callback_id; |
| 763 std::string printer_name; | 771 std::string printer_name; |
| 764 if (!args->GetString(0, &callback_id) || !args->GetString(1, &printer_name) || | 772 if (!args->GetString(0, &callback_id) || !args->GetString(1, &printer_name) || |
| 765 callback_id.empty() || printer_name.empty()) { | 773 callback_id.empty() || printer_name.empty()) { |
| (...skipping 381 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1147 preview_web_contents()->GetBrowserContext()); | 1155 preview_web_contents()->GetBrowserContext()); |
| 1148 chrome::ScopedTabbedBrowserDisplayer displayer(profile); | 1156 chrome::ScopedTabbedBrowserDisplayer displayer(profile); |
| 1149 print_dialog_cloud::CreateCloudPrintSigninTab( | 1157 print_dialog_cloud::CreateCloudPrintSigninTab( |
| 1150 displayer.browser(), | 1158 displayer.browser(), |
| 1151 add_account, | 1159 add_account, |
| 1152 base::Bind(&PrintPreviewHandler::OnSigninComplete, | 1160 base::Bind(&PrintPreviewHandler::OnSigninComplete, |
| 1153 weak_factory_.GetWeakPtr())); | 1161 weak_factory_.GetWeakPtr())); |
| 1154 } | 1162 } |
| 1155 | 1163 |
| 1156 void PrintPreviewHandler::HandleGetAccessToken(const base::ListValue* args) { | 1164 void PrintPreviewHandler::HandleGetAccessToken(const base::ListValue* args) { |
| 1165 std::string callback_id; |
| 1157 std::string type; | 1166 std::string type; |
| 1158 if (!args->GetString(0, &type)) | 1167 |
| 1159 return; | 1168 bool ok = args->GetString(0, &callback_id) && args->GetString(1, &type) && |
| 1169 !callback_id.empty(); |
| 1170 DCHECK(ok); |
| 1171 |
| 1172 AllowJavascript(); |
| 1160 if (!token_service_) | 1173 if (!token_service_) |
| 1161 token_service_ = base::MakeUnique<AccessTokenService>(this); | 1174 token_service_ = base::MakeUnique<AccessTokenService>(this); |
| 1162 token_service_->RequestToken(type); | 1175 token_service_->RequestToken(type, callback_id); |
| 1163 } | 1176 } |
| 1164 | 1177 |
| 1165 void PrintPreviewHandler::HandleManageCloudPrint( | 1178 void PrintPreviewHandler::HandleManageCloudPrint( |
| 1166 const base::ListValue* args) { | 1179 const base::ListValue* args) { |
| 1167 ++manage_cloud_printers_dialog_request_count_; | 1180 ++manage_cloud_printers_dialog_request_count_; |
| 1168 GURL manage_url(cloud_devices::GetCloudPrintRelativeURL("manage.html")); | 1181 GURL manage_url(cloud_devices::GetCloudPrintRelativeURL("manage.html")); |
| 1169 std::string user; | 1182 std::string user; |
| 1170 if (!args->GetString(0, &user)) | 1183 if (!args->GetString(0, &user)) |
| 1171 return; | 1184 return; |
| 1172 if (!user.empty()) | 1185 if (!user.empty()) |
| (...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1311 | 1324 |
| 1312 if (print_preview_ui()->source_is_modifiable()) | 1325 if (print_preview_ui()->source_is_modifiable()) |
| 1313 GetNumberFormatAndMeasurementSystem(&initial_settings); | 1326 GetNumberFormatAndMeasurementSystem(&initial_settings); |
| 1314 ResolveJavascriptCallback(base::Value(callback_id), initial_settings); | 1327 ResolveJavascriptCallback(base::Value(callback_id), initial_settings); |
| 1315 } | 1328 } |
| 1316 | 1329 |
| 1317 void PrintPreviewHandler::ClosePreviewDialog() { | 1330 void PrintPreviewHandler::ClosePreviewDialog() { |
| 1318 print_preview_ui()->OnClosePrintPreviewDialog(); | 1331 print_preview_ui()->OnClosePrintPreviewDialog(); |
| 1319 } | 1332 } |
| 1320 | 1333 |
| 1321 void PrintPreviewHandler::SendAccessToken(const std::string& type, | 1334 void PrintPreviewHandler::SendAccessToken(const std::string& callback_id, |
| 1322 const std::string& access_token) { | 1335 const std::string& access_token) { |
| 1323 VLOG(1) << "Get getAccessToken finished"; | 1336 VLOG(1) << "Get getAccessToken finished"; |
| 1324 web_ui()->CallJavascriptFunctionUnsafe( | 1337 ResolveJavascriptCallback(base::Value(callback_id), |
| 1325 "onDidGetAccessToken", base::Value(type), base::Value(access_token)); | 1338 base::Value(access_token)); |
| 1326 } | 1339 } |
| 1327 | 1340 |
| 1328 void PrintPreviewHandler::SendPrinterCapabilities( | 1341 void PrintPreviewHandler::SendPrinterCapabilities( |
| 1329 const std::string& callback_id, | 1342 const std::string& callback_id, |
| 1330 const std::string& printer_name, | 1343 const std::string& printer_name, |
| 1331 std::unique_ptr<base::DictionaryValue> settings_info) { | 1344 std::unique_ptr<base::DictionaryValue> settings_info) { |
| 1332 // Check that |settings_info| is valid. | 1345 // Check that |settings_info| is valid. |
| 1333 if (settings_info && settings_info->Get("capabilities", nullptr)) { | 1346 if (settings_info && settings_info->Get("capabilities", nullptr)) { |
| 1334 VLOG(1) << "Get printer capabilities finished"; | 1347 VLOG(1) << "Get printer capabilities finished"; |
| 1335 ResolveJavascriptCallback(base::Value(callback_id), *settings_info); | 1348 ResolveJavascriptCallback(base::Value(callback_id), *settings_info); |
| (...skipping 440 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1776 bool done) { | 1789 bool done) { |
| 1777 AllowJavascript(); | 1790 AllowJavascript(); |
| 1778 | 1791 |
| 1779 FireWebUIListener("extension-printers-added", printers); | 1792 FireWebUIListener("extension-printers-added", printers); |
| 1780 if (done) { | 1793 if (done) { |
| 1781 ResolveJavascriptCallback(base::Value(callback_id), base::Value()); | 1794 ResolveJavascriptCallback(base::Value(callback_id), base::Value()); |
| 1782 } | 1795 } |
| 1783 } | 1796 } |
| 1784 | 1797 |
| 1785 void PrintPreviewHandler::OnGotExtensionPrinterInfo( | 1798 void PrintPreviewHandler::OnGotExtensionPrinterInfo( |
| 1786 const std::string& printer_id, | 1799 const std::string& callback_id, |
| 1787 const base::DictionaryValue& printer_info) { | 1800 const base::DictionaryValue& printer_info) { |
| 1788 if (printer_info.empty()) { | 1801 if (printer_info.empty()) { |
| 1789 web_ui()->CallJavascriptFunctionUnsafe("failedToResolveProvisionalPrinter", | 1802 RejectJavascriptCallback(base::Value(callback_id), base::Value()); |
| 1790 base::Value(printer_id)); | |
| 1791 return; | 1803 return; |
| 1792 } | 1804 } |
| 1793 | 1805 ResolveJavascriptCallback(base::Value(callback_id), printer_info); |
| 1794 web_ui()->CallJavascriptFunctionUnsafe("onProvisionalPrinterResolved", | |
| 1795 base::Value(printer_id), printer_info); | |
| 1796 } | 1806 } |
| 1797 | 1807 |
| 1798 void PrintPreviewHandler::OnGotExtensionPrinterCapabilities( | 1808 void PrintPreviewHandler::OnGotExtensionPrinterCapabilities( |
| 1799 const std::string& callback_id, | 1809 const std::string& callback_id, |
| 1800 const base::DictionaryValue& capabilities) { | 1810 const base::DictionaryValue& capabilities) { |
| 1801 if (capabilities.empty()) { | 1811 if (capabilities.empty()) { |
| 1802 RejectJavascriptCallback(base::Value(callback_id), base::Value()); | 1812 RejectJavascriptCallback(base::Value(callback_id), base::Value()); |
| 1803 return; | 1813 return; |
| 1804 } | 1814 } |
| 1805 ResolveJavascriptCallback(base::Value(callback_id), capabilities); | 1815 ResolveJavascriptCallback(base::Value(callback_id), capabilities); |
| (...skipping 22 matching lines...) Expand all Loading... |
| 1828 | 1838 |
| 1829 void PrintPreviewHandler::UnregisterForGaiaCookieChanges() { | 1839 void PrintPreviewHandler::UnregisterForGaiaCookieChanges() { |
| 1830 if (gaia_cookie_manager_service_) | 1840 if (gaia_cookie_manager_service_) |
| 1831 gaia_cookie_manager_service_->RemoveObserver(this); | 1841 gaia_cookie_manager_service_->RemoveObserver(this); |
| 1832 } | 1842 } |
| 1833 | 1843 |
| 1834 void PrintPreviewHandler::SetPdfSavedClosureForTesting( | 1844 void PrintPreviewHandler::SetPdfSavedClosureForTesting( |
| 1835 const base::Closure& closure) { | 1845 const base::Closure& closure) { |
| 1836 pdf_file_saved_closure_ = closure; | 1846 pdf_file_saved_closure_ = closure; |
| 1837 } | 1847 } |
| OLD | NEW |