Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(821)

Side by Side Diff: chrome/browser/ui/webui/print_preview/print_preview_handler.cc

Issue 2938073003: Change getAccessToken and getExtensionPrinterAccess to sendWithPromise (Closed)
Patch Set: Cleanup Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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 handler_->SendRequestInProgress(callback_id);
481 return; // Already in progress. 482 return; // Already in progress.
483 }
482 484
483 OAuth2TokenService* service = NULL; 485 OAuth2TokenService* service = NULL;
484 std::string account_id; 486 std::string account_id;
485 if (type == "profile") { 487 if (type == "profile") {
486 Profile* profile = Profile::FromWebUI(handler_->web_ui()); 488 Profile* profile = Profile::FromWebUI(handler_->web_ui());
487 if (profile) { 489 if (profile) {
488 ProfileOAuth2TokenService* token_service = 490 ProfileOAuth2TokenService* token_service =
489 ProfileOAuth2TokenServiceFactory::GetForProfile(profile); 491 ProfileOAuth2TokenServiceFactory::GetForProfile(profile);
490 SigninManagerBase* signin_manager = 492 SigninManagerBase* signin_manager =
491 SigninManagerFactory::GetInstance()->GetForProfile(profile); 493 SigninManagerFactory::GetInstance()->GetForProfile(profile);
492 account_id = signin_manager->GetAuthenticatedAccountId(); 494 account_id = signin_manager->GetAuthenticatedAccountId();
493 service = token_service; 495 service = token_service;
494 } 496 }
495 } else if (type == "device") { 497 } else if (type == "device") {
496 #if defined(OS_CHROMEOS) 498 #if defined(OS_CHROMEOS)
497 chromeos::DeviceOAuth2TokenService* token_service = 499 chromeos::DeviceOAuth2TokenService* token_service =
498 chromeos::DeviceOAuth2TokenServiceFactory::Get(); 500 chromeos::DeviceOAuth2TokenServiceFactory::Get();
499 account_id = token_service->GetRobotAccountId(); 501 account_id = token_service->GetRobotAccountId();
500 service = token_service; 502 service = token_service;
501 #endif 503 #endif
502 } 504 }
503 505
504 if (service) { 506 if (service) {
505 OAuth2TokenService::ScopeSet oauth_scopes; 507 OAuth2TokenService::ScopeSet oauth_scopes;
506 oauth_scopes.insert(cloud_devices::kCloudPrintAuthScope); 508 oauth_scopes.insert(cloud_devices::kCloudPrintAuthScope);
507 std::unique_ptr<OAuth2TokenService::Request> request( 509 std::unique_ptr<OAuth2TokenService::Request> request(
508 service->StartRequest(account_id, oauth_scopes, this)); 510 service->StartRequest(account_id, oauth_scopes, this));
509 requests_[type] = std::move(request); 511 requests_[type] = std::move(request);
512 callbacks_[type] = callback_id;
510 } else { 513 } else {
511 handler_->SendAccessToken(type, std::string()); // Unknown type. 514 // Unknown type.
515 handler_->SendAccessToken(callback_id, std::string());
512 } 516 }
513 } 517 }
514 518
515 void OnGetTokenSuccess(const OAuth2TokenService::Request* request, 519 void OnGetTokenSuccess(const OAuth2TokenService::Request* request,
516 const std::string& access_token, 520 const std::string& access_token,
517 const base::Time& expiration_time) override { 521 const base::Time& expiration_time) override {
518 OnServiceResponce(request, access_token); 522 OnServiceResponse(request, access_token);
519 } 523 }
520 524
521 void OnGetTokenFailure(const OAuth2TokenService::Request* request, 525 void OnGetTokenFailure(const OAuth2TokenService::Request* request,
522 const GoogleServiceAuthError& error) override { 526 const GoogleServiceAuthError& error) override {
523 OnServiceResponce(request, std::string()); 527 OnServiceResponse(request, std::string());
524 } 528 }
525 529
526 private: 530 private:
527 void OnServiceResponce(const OAuth2TokenService::Request* request, 531 void OnServiceResponse(const OAuth2TokenService::Request* request,
528 const std::string& access_token) { 532 const std::string& access_token) {
529 for (Requests::iterator i = requests_.begin(); i != requests_.end(); ++i) { 533 for (Requests::iterator i = requests_.begin(); i != requests_.end(); ++i) {
530 if (i->second.get() == request) { 534 if (i->second.get() == request) {
531 handler_->SendAccessToken(i->first, access_token); 535 handler_->SendAccessToken(callbacks_[i->first], access_token);
532 requests_.erase(i); 536 requests_.erase(i);
537 callbacks_.erase(i->first);
533 return; 538 return;
534 } 539 }
535 } 540 }
536 NOTREACHED(); 541 NOTREACHED();
537 } 542 }
538 543
539 using Requests = 544 using Requests =
540 std::map<std::string, std::unique_ptr<OAuth2TokenService::Request>>; 545 std::map<std::string, std::unique_ptr<OAuth2TokenService::Request>>;
541 Requests requests_; 546 Requests requests_;
547 using Callbacks = std::map<std::string, std::string>;
548 Callbacks callbacks_;
542 PrintPreviewHandler* handler_; 549 PrintPreviewHandler* handler_;
543 550
544 DISALLOW_COPY_AND_ASSIGN(AccessTokenService); 551 DISALLOW_COPY_AND_ASSIGN(AccessTokenService);
545 }; 552 };
546 553
547 PrintPreviewHandler::PrintPreviewHandler() 554 PrintPreviewHandler::PrintPreviewHandler()
548 : regenerate_preview_request_count_(0), 555 : regenerate_preview_request_count_(0),
549 manage_printers_dialog_request_count_(0), 556 manage_printers_dialog_request_count_(0),
550 manage_cloud_printers_dialog_request_count_(0), 557 manage_cloud_printers_dialog_request_count_(0),
551 reported_failed_preview_(false), 558 reported_failed_preview_(false),
(...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after
738 // Make sure all in progress requests are canceled before new printer search 745 // Make sure all in progress requests are canceled before new printer search
739 // starts. 746 // starts.
740 extension_printer_handler_->Reset(); 747 extension_printer_handler_->Reset();
741 extension_printer_handler_->StartGetPrinters( 748 extension_printer_handler_->StartGetPrinters(
742 base::Bind(&PrintPreviewHandler::OnGotPrintersForExtension, 749 base::Bind(&PrintPreviewHandler::OnGotPrintersForExtension,
743 weak_factory_.GetWeakPtr(), callback_id)); 750 weak_factory_.GetWeakPtr(), callback_id));
744 } 751 }
745 752
746 void PrintPreviewHandler::HandleGrantExtensionPrinterAccess( 753 void PrintPreviewHandler::HandleGrantExtensionPrinterAccess(
747 const base::ListValue* args) { 754 const base::ListValue* args) {
755 std::string callback_id;
748 std::string printer_id; 756 std::string printer_id;
749 bool ok = args->GetString(0, &printer_id); 757 bool ok = args->GetString(0, &callback_id) && args->GetString(1, &printer_id);
750 DCHECK(ok); 758 DCHECK(ok);
751 759
760 AllowJavascript();
761 if (callback_id.empty() || printer_id.empty())
762 RejectJavascriptCallback(base::Value(callback_id), base::Value());
763
752 EnsureExtensionPrinterHandlerSet(); 764 EnsureExtensionPrinterHandlerSet();
753 extension_printer_handler_->StartGrantPrinterAccess( 765 extension_printer_handler_->StartGrantPrinterAccess(
754 printer_id, base::Bind(&PrintPreviewHandler::OnGotExtensionPrinterInfo, 766 printer_id, base::Bind(&PrintPreviewHandler::OnGotExtensionPrinterInfo,
755 weak_factory_.GetWeakPtr(), printer_id)); 767 weak_factory_.GetWeakPtr(), callback_id));
756 } 768 }
757 769
758 void PrintPreviewHandler::HandleGetExtensionPrinterCapabilities( 770 void PrintPreviewHandler::HandleGetExtensionPrinterCapabilities(
759 const base::ListValue* args) { 771 const base::ListValue* args) {
760 AllowJavascript(); 772 AllowJavascript();
761 773
762 std::string callback_id; 774 std::string callback_id;
763 std::string printer_name; 775 std::string printer_name;
764 if (!args->GetString(0, &callback_id) || !args->GetString(1, &printer_name) || 776 if (!args->GetString(0, &callback_id) || !args->GetString(1, &printer_name) ||
765 callback_id.empty() || printer_name.empty()) { 777 callback_id.empty() || printer_name.empty()) {
(...skipping 381 matching lines...) Expand 10 before | Expand all | Expand 10 after
1147 preview_web_contents()->GetBrowserContext()); 1159 preview_web_contents()->GetBrowserContext());
1148 chrome::ScopedTabbedBrowserDisplayer displayer(profile); 1160 chrome::ScopedTabbedBrowserDisplayer displayer(profile);
1149 print_dialog_cloud::CreateCloudPrintSigninTab( 1161 print_dialog_cloud::CreateCloudPrintSigninTab(
1150 displayer.browser(), 1162 displayer.browser(),
1151 add_account, 1163 add_account,
1152 base::Bind(&PrintPreviewHandler::OnSigninComplete, 1164 base::Bind(&PrintPreviewHandler::OnSigninComplete,
1153 weak_factory_.GetWeakPtr())); 1165 weak_factory_.GetWeakPtr()));
1154 } 1166 }
1155 1167
1156 void PrintPreviewHandler::HandleGetAccessToken(const base::ListValue* args) { 1168 void PrintPreviewHandler::HandleGetAccessToken(const base::ListValue* args) {
1169 std::string callback_id;
1157 std::string type; 1170 std::string type;
1158 if (!args->GetString(0, &type)) 1171
1172 AllowJavascript();
1173 if (!args->GetString(0, &callback_id) || !args->GetString(1, &type)) {
dpapad 2017/06/15 22:20:13 Can this happen other than programmer's error? Sho
rbpotter 2017/06/16 02:19:36 Done.
1174 RejectJavascriptCallback(base::Value(callback_id), base::Value());
1159 return; 1175 return;
1176 }
1177
1160 if (!token_service_) 1178 if (!token_service_)
1161 token_service_ = base::MakeUnique<AccessTokenService>(this); 1179 token_service_ = base::MakeUnique<AccessTokenService>(this);
1162 token_service_->RequestToken(type); 1180 token_service_->RequestToken(type, callback_id);
1163 } 1181 }
1164 1182
1165 void PrintPreviewHandler::HandleManageCloudPrint( 1183 void PrintPreviewHandler::HandleManageCloudPrint(
1166 const base::ListValue* args) { 1184 const base::ListValue* args) {
1167 ++manage_cloud_printers_dialog_request_count_; 1185 ++manage_cloud_printers_dialog_request_count_;
1168 GURL manage_url(cloud_devices::GetCloudPrintRelativeURL("manage.html")); 1186 GURL manage_url(cloud_devices::GetCloudPrintRelativeURL("manage.html"));
1169 std::string user; 1187 std::string user;
1170 if (!args->GetString(0, &user)) 1188 if (!args->GetString(0, &user))
1171 return; 1189 return;
1172 if (!user.empty()) 1190 if (!user.empty())
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after
1311 1329
1312 if (print_preview_ui()->source_is_modifiable()) 1330 if (print_preview_ui()->source_is_modifiable())
1313 GetNumberFormatAndMeasurementSystem(&initial_settings); 1331 GetNumberFormatAndMeasurementSystem(&initial_settings);
1314 ResolveJavascriptCallback(base::Value(callback_id), initial_settings); 1332 ResolveJavascriptCallback(base::Value(callback_id), initial_settings);
1315 } 1333 }
1316 1334
1317 void PrintPreviewHandler::ClosePreviewDialog() { 1335 void PrintPreviewHandler::ClosePreviewDialog() {
1318 print_preview_ui()->OnClosePrintPreviewDialog(); 1336 print_preview_ui()->OnClosePrintPreviewDialog();
1319 } 1337 }
1320 1338
1321 void PrintPreviewHandler::SendAccessToken(const std::string& type, 1339 void PrintPreviewHandler::SendAccessToken(const std::string& callback_id,
1322 const std::string& access_token) { 1340 const std::string& access_token) {
1323 VLOG(1) << "Get getAccessToken finished"; 1341 VLOG(1) << "Get getAccessToken finished";
1324 web_ui()->CallJavascriptFunctionUnsafe( 1342 base::DictionaryValue response;
1325 "onDidGetAccessToken", base::Value(type), base::Value(access_token)); 1343 response.SetBoolean("completed", true);
1344 response.SetString("accessToken", access_token);
1345 ResolveJavascriptCallback(base::Value(callback_id), response);
1346 }
1347
1348 void PrintPreviewHandler::SendRequestInProgress(
1349 const std::string& callback_id) {
1350 base::DictionaryValue response;
1351 response.SetBoolean("completed", false);
1352 response.SetString("accessToken", "");
1353 ResolveJavascriptCallback(base::Value(callback_id), response);
1326 } 1354 }
1327 1355
1328 void PrintPreviewHandler::SendPrinterCapabilities( 1356 void PrintPreviewHandler::SendPrinterCapabilities(
1329 const std::string& callback_id, 1357 const std::string& callback_id,
1330 const std::string& printer_name, 1358 const std::string& printer_name,
1331 std::unique_ptr<base::DictionaryValue> settings_info) { 1359 std::unique_ptr<base::DictionaryValue> settings_info) {
1332 // Check that |settings_info| is valid. 1360 // Check that |settings_info| is valid.
1333 if (settings_info && settings_info->Get("capabilities", nullptr)) { 1361 if (settings_info && settings_info->Get("capabilities", nullptr)) {
1334 VLOG(1) << "Get printer capabilities finished"; 1362 VLOG(1) << "Get printer capabilities finished";
1335 ResolveJavascriptCallback(base::Value(callback_id), *settings_info); 1363 ResolveJavascriptCallback(base::Value(callback_id), *settings_info);
(...skipping 440 matching lines...) Expand 10 before | Expand all | Expand 10 after
1776 bool done) { 1804 bool done) {
1777 AllowJavascript(); 1805 AllowJavascript();
1778 1806
1779 FireWebUIListener("extension-printers-added", printers); 1807 FireWebUIListener("extension-printers-added", printers);
1780 if (done) { 1808 if (done) {
1781 ResolveJavascriptCallback(base::Value(callback_id), base::Value()); 1809 ResolveJavascriptCallback(base::Value(callback_id), base::Value());
1782 } 1810 }
1783 } 1811 }
1784 1812
1785 void PrintPreviewHandler::OnGotExtensionPrinterInfo( 1813 void PrintPreviewHandler::OnGotExtensionPrinterInfo(
1786 const std::string& printer_id, 1814 const std::string& callback_id,
1787 const base::DictionaryValue& printer_info) { 1815 const base::DictionaryValue& printer_info) {
1788 if (printer_info.empty()) { 1816 if (printer_info.empty()) {
1789 web_ui()->CallJavascriptFunctionUnsafe("failedToResolveProvisionalPrinter", 1817 RejectJavascriptCallback(base::Value(callback_id), base::Value());
1790 base::Value(printer_id));
1791 return; 1818 return;
1792 } 1819 }
1793 1820 ResolveJavascriptCallback(base::Value(callback_id), printer_info);
1794 web_ui()->CallJavascriptFunctionUnsafe("onProvisionalPrinterResolved",
1795 base::Value(printer_id), printer_info);
1796 } 1821 }
1797 1822
1798 void PrintPreviewHandler::OnGotExtensionPrinterCapabilities( 1823 void PrintPreviewHandler::OnGotExtensionPrinterCapabilities(
1799 const std::string& callback_id, 1824 const std::string& callback_id,
1800 const base::DictionaryValue& capabilities) { 1825 const base::DictionaryValue& capabilities) {
1801 if (capabilities.empty()) { 1826 if (capabilities.empty()) {
1802 RejectJavascriptCallback(base::Value(callback_id), base::Value()); 1827 RejectJavascriptCallback(base::Value(callback_id), base::Value());
1803 return; 1828 return;
1804 } 1829 }
1805 ResolveJavascriptCallback(base::Value(callback_id), capabilities); 1830 ResolveJavascriptCallback(base::Value(callback_id), capabilities);
(...skipping 22 matching lines...) Expand all
1828 1853
1829 void PrintPreviewHandler::UnregisterForGaiaCookieChanges() { 1854 void PrintPreviewHandler::UnregisterForGaiaCookieChanges() {
1830 if (gaia_cookie_manager_service_) 1855 if (gaia_cookie_manager_service_)
1831 gaia_cookie_manager_service_->RemoveObserver(this); 1856 gaia_cookie_manager_service_->RemoveObserver(this);
1832 } 1857 }
1833 1858
1834 void PrintPreviewHandler::SetPdfSavedClosureForTesting( 1859 void PrintPreviewHandler::SetPdfSavedClosureForTesting(
1835 const base::Closure& closure) { 1860 const base::Closure& closure) {
1836 pdf_file_saved_closure_ = closure; 1861 pdf_file_saved_closure_ = closure;
1837 } 1862 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698