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

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

Issue 14093016: Native api to get OAuth2 access tokens in Print Preview Web UI. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 8 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 | Annotate | Revision Log
« no previous file with comments | « chrome/browser/ui/webui/print_preview/print_preview_handler.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 8
9 #include <string> 9 #include <string>
10 10
(...skipping 17 matching lines...) Expand all
28 #include "chrome/browser/platform_util.h" 28 #include "chrome/browser/platform_util.h"
29 #include "chrome/browser/printing/cloud_print/cloud_print_url.h" 29 #include "chrome/browser/printing/cloud_print/cloud_print_url.h"
30 #include "chrome/browser/printing/print_dialog_cloud.h" 30 #include "chrome/browser/printing/print_dialog_cloud.h"
31 #include "chrome/browser/printing/print_error_dialog.h" 31 #include "chrome/browser/printing/print_error_dialog.h"
32 #include "chrome/browser/printing/print_job_manager.h" 32 #include "chrome/browser/printing/print_job_manager.h"
33 #include "chrome/browser/printing/print_preview_dialog_controller.h" 33 #include "chrome/browser/printing/print_preview_dialog_controller.h"
34 #include "chrome/browser/printing/print_system_task_proxy.h" 34 #include "chrome/browser/printing/print_system_task_proxy.h"
35 #include "chrome/browser/printing/print_view_manager.h" 35 #include "chrome/browser/printing/print_view_manager.h"
36 #include "chrome/browser/printing/printer_manager_dialog.h" 36 #include "chrome/browser/printing/printer_manager_dialog.h"
37 #include "chrome/browser/profiles/profile.h" 37 #include "chrome/browser/profiles/profile.h"
38 #include "chrome/browser/signin/oauth2_token_service.h"
39 #include "chrome/browser/signin/oauth2_token_service_factory.h"
38 #include "chrome/browser/ui/browser_finder.h" 40 #include "chrome/browser/ui/browser_finder.h"
39 #include "chrome/browser/ui/browser_tabstrip.h" 41 #include "chrome/browser/ui/browser_tabstrip.h"
40 #include "chrome/browser/ui/chrome_select_file_policy.h" 42 #include "chrome/browser/ui/chrome_select_file_policy.h"
41 #include "chrome/browser/ui/webui/print_preview/print_preview_ui.h" 43 #include "chrome/browser/ui/webui/print_preview/print_preview_ui.h"
42 #include "chrome/browser/ui/webui/print_preview/sticky_settings.h" 44 #include "chrome/browser/ui/webui/print_preview/sticky_settings.h"
43 #include "chrome/common/chrome_paths.h" 45 #include "chrome/common/chrome_paths.h"
44 #include "chrome/common/chrome_switches.h" 46 #include "chrome/common/chrome_switches.h"
45 #include "chrome/common/pref_names.h" 47 #include "chrome/common/pref_names.h"
46 #include "chrome/common/print_messages.h" 48 #include "chrome/common/print_messages.h"
47 #include "content/public/browser/browser_context.h" 49 #include "content/public/browser/browser_context.h"
(...skipping 21 matching lines...) Expand all
69 using content::BrowserThread; 71 using content::BrowserThread;
70 using content::NavigationEntry; 72 using content::NavigationEntry;
71 using content::OpenURLParams; 73 using content::OpenURLParams;
72 using content::RenderViewHost; 74 using content::RenderViewHost;
73 using content::Referrer; 75 using content::Referrer;
74 using content::WebContents; 76 using content::WebContents;
75 using printing::Metafile; 77 using printing::Metafile;
76 78
77 namespace { 79 namespace {
78 80
81 // The cloud print OAuth2 scope.
82 const char kCloudPrintAuth[] = "https://www.googleapis.com/auth/cloudprint";
83
79 enum UserActionBuckets { 84 enum UserActionBuckets {
80 PRINT_TO_PRINTER, 85 PRINT_TO_PRINTER,
81 PRINT_TO_PDF, 86 PRINT_TO_PDF,
82 CANCEL, 87 CANCEL,
83 FALLBACK_TO_ADVANCED_SETTINGS_DIALOG, 88 FALLBACK_TO_ADVANCED_SETTINGS_DIALOG,
84 PREVIEW_FAILED, 89 PREVIEW_FAILED,
85 PREVIEW_STARTED, 90 PREVIEW_STARTED,
86 INITIATOR_TAB_CRASHED, // UNUSED 91 INITIATOR_TAB_CRASHED, // UNUSED
87 INITIATOR_TAB_CLOSED, 92 INITIATOR_TAB_CLOSED,
88 PRINT_WITH_CLOUD_PRINT, 93 PRINT_WITH_CLOUD_PRINT,
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after
229 BrowserThread::UI, FROM_HERE, 234 BrowserThread::UI, FROM_HERE,
230 base::Bind(&base::DeletePointer<Metafile>, metafile)); 235 base::Bind(&base::DeletePointer<Metafile>, metafile));
231 } 236 }
232 #endif 237 #endif
233 238
234 static base::LazyInstance<printing::StickySettings> sticky_settings = 239 static base::LazyInstance<printing::StickySettings> sticky_settings =
235 LAZY_INSTANCE_INITIALIZER; 240 LAZY_INSTANCE_INITIALIZER;
236 241
237 } // namespace 242 } // namespace
238 243
244 class PrintPreviewHandler::AccessTokenService
245 : public OAuth2TokenService::Consumer {
246 public:
247 typedef const base::Callback<void(const std::string&,
248 const std::string&)> CompleteonCallback;
249
250 explicit AccessTokenService(const CompleteonCallback& callback)
251 : callback_(callback) {
252 }
253
254 void AddOAuth2TokenService(const std::string& type,
255 OAuth2TokenService* service) {
256 services_.push_back(make_linked_ptr(new Service()));
257 services_.back()->type = type;
258 services_.back()->service = service;
259 }
260
261 void RequestToken(const std::string& type) {
262 for (Services::iterator i = services_.begin();
263 i != services_.end(); ++i) {
264 Service& service = **i;
265 if (service.type == type) {
266 if (service.request) // Already in progress.
267 return;
268 OAuth2TokenService::ScopeSet oauth_scopes;
269 oauth_scopes.insert(kCloudPrintAuth);
270 service.request = service.service->StartRequest(oauth_scopes, this);
271 return;
272 }
273 }
274 callback_.Run(type, std::string()); // Unknown type.
275 }
276
277 void OnGetTokenSuccess(const OAuth2TokenService::Request* request,
278 const std::string& access_token,
279 const base::Time& expiration_time) OVERRIDE {
280 OnServiceResponce(request, access_token);
281 }
282
283 void OnGetTokenFailure(const OAuth2TokenService::Request* request,
284 const GoogleServiceAuthError& error) OVERRIDE {
285 OnServiceResponce(request, std::string());
286 }
287
288 private:
289 void OnServiceResponce(const OAuth2TokenService::Request* request,
290 const std::string& access_token) {
291 for (Services::iterator i = services_.begin();
292 i != services_.end(); ++i) {
293 Service& service = **i;
294 if (service.request == request) {
295 service.request.reset();
296 callback_.Run(service.type, access_token);
297 return;
298 }
299 }
300 NOTREACHED();
301 }
302
303 struct Service {
304 std::string type;
305 OAuth2TokenService* service;
306 scoped_ptr<OAuth2TokenService::Request> request;
307 };
308 typedef std::vector<linked_ptr<Service> > Services;
309 Services services_;
310 CompleteonCallback callback_;
311
312 DISALLOW_COPY_AND_ASSIGN(AccessTokenService);
313 };
314
239 // static 315 // static
240 printing::StickySettings* PrintPreviewHandler::GetStickySettings() { 316 printing::StickySettings* PrintPreviewHandler::GetStickySettings() {
241 return sticky_settings.Pointer(); 317 return sticky_settings.Pointer();
242 } 318 }
243 319
244 PrintPreviewHandler::PrintPreviewHandler() 320 PrintPreviewHandler::PrintPreviewHandler()
245 : print_backend_(printing::PrintBackend::CreateInstance(NULL)), 321 : print_backend_(printing::PrintBackend::CreateInstance(NULL)),
246 regenerate_preview_request_count_(0), 322 regenerate_preview_request_count_(0),
247 manage_printers_dialog_request_count_(0), 323 manage_printers_dialog_request_count_(0),
248 manage_cloud_printers_dialog_request_count_(0), 324 manage_cloud_printers_dialog_request_count_(0),
(...skipping 19 matching lines...) Expand all
268 base::Unretained(this))); 344 base::Unretained(this)));
269 web_ui()->RegisterMessageCallback("getPrinterCapabilities", 345 web_ui()->RegisterMessageCallback("getPrinterCapabilities",
270 base::Bind(&PrintPreviewHandler::HandleGetPrinterCapabilities, 346 base::Bind(&PrintPreviewHandler::HandleGetPrinterCapabilities,
271 base::Unretained(this))); 347 base::Unretained(this)));
272 web_ui()->RegisterMessageCallback("showSystemDialog", 348 web_ui()->RegisterMessageCallback("showSystemDialog",
273 base::Bind(&PrintPreviewHandler::HandleShowSystemDialog, 349 base::Bind(&PrintPreviewHandler::HandleShowSystemDialog,
274 base::Unretained(this))); 350 base::Unretained(this)));
275 web_ui()->RegisterMessageCallback("signIn", 351 web_ui()->RegisterMessageCallback("signIn",
276 base::Bind(&PrintPreviewHandler::HandleSignin, 352 base::Bind(&PrintPreviewHandler::HandleSignin,
277 base::Unretained(this))); 353 base::Unretained(this)));
354 web_ui()->RegisterMessageCallback("getAccessToken",
355 base::Bind(&PrintPreviewHandler::HandleGetAccessToken,
356 base::Unretained(this)));
278 web_ui()->RegisterMessageCallback("manageCloudPrinters", 357 web_ui()->RegisterMessageCallback("manageCloudPrinters",
279 base::Bind(&PrintPreviewHandler::HandleManageCloudPrint, 358 base::Bind(&PrintPreviewHandler::HandleManageCloudPrint,
280 base::Unretained(this))); 359 base::Unretained(this)));
281 web_ui()->RegisterMessageCallback("manageLocalPrinters", 360 web_ui()->RegisterMessageCallback("manageLocalPrinters",
282 base::Bind(&PrintPreviewHandler::HandleManagePrinters, 361 base::Bind(&PrintPreviewHandler::HandleManagePrinters,
283 base::Unretained(this))); 362 base::Unretained(this)));
284 web_ui()->RegisterMessageCallback("closePrintPreviewDialog", 363 web_ui()->RegisterMessageCallback("closePrintPreviewDialog",
285 base::Bind(&PrintPreviewHandler::HandleClosePreviewDialog, 364 base::Bind(&PrintPreviewHandler::HandleClosePreviewDialog,
286 base::Unretained(this))); 365 base::Unretained(this)));
287 web_ui()->RegisterMessageCallback("hidePreview", 366 web_ui()->RegisterMessageCallback("hidePreview",
(...skipping 290 matching lines...) Expand 10 before | Expand all | Expand 10 after
578 657
579 void PrintPreviewHandler::HandleSignin(const ListValue* /*args*/) { 658 void PrintPreviewHandler::HandleSignin(const ListValue* /*args*/) {
580 gfx::NativeWindow modal_parent = platform_util::GetTopLevel( 659 gfx::NativeWindow modal_parent = platform_util::GetTopLevel(
581 preview_web_contents()->GetView()->GetNativeView()); 660 preview_web_contents()->GetView()->GetNativeView());
582 print_dialog_cloud::CreateCloudPrintSigninDialog( 661 print_dialog_cloud::CreateCloudPrintSigninDialog(
583 preview_web_contents()->GetBrowserContext(), 662 preview_web_contents()->GetBrowserContext(),
584 modal_parent, 663 modal_parent,
585 base::Bind(&PrintPreviewHandler::OnSigninComplete, AsWeakPtr())); 664 base::Bind(&PrintPreviewHandler::OnSigninComplete, AsWeakPtr()));
586 } 665 }
587 666
667 void PrintPreviewHandler::HandleGetAccessToken(const base::ListValue* args) {
668 std::string type;
669 if (!args->GetString(0, &type))
670 return;
671 if (!token_service_)
672 InitTokenService();
673 token_service_->RequestToken(type);
674 }
675
676 void PrintPreviewHandler::InitTokenService() {
677 token_service_.reset(
678 new AccessTokenService(base::Bind(&PrintPreviewHandler::SendAccessToken,
679 base::Unretained(this))));
680 Profile* profile = Profile::FromWebUI(web_ui());
681 OAuth2TokenService* profile_service =
682 OAuth2TokenServiceFactory::GetForProfile(profile);
683 if (profile_service) {
684 token_service_->AddOAuth2TokenService("profile", profile_service);
685 // TODO(vitalybuka): Replace with source of device tokens.
686 token_service_->AddOAuth2TokenService("device", profile_service);
687 }
688 }
689
588 void PrintPreviewHandler::PrintWithCloudPrintDialog( 690 void PrintPreviewHandler::PrintWithCloudPrintDialog(
589 const base::RefCountedBytes* data, 691 const base::RefCountedBytes* data,
590 const string16& title) { 692 const string16& title) {
591 // Record the number of times the user asks to print via cloud print 693 // Record the number of times the user asks to print via cloud print
592 // instead of the print preview dialog. 694 // instead of the print preview dialog.
593 ReportStats(); 695 ReportStats();
594 696
595 gfx::NativeWindow modal_parent = platform_util::GetTopLevel( 697 gfx::NativeWindow modal_parent = platform_util::GetTopLevel(
596 preview_web_contents()->GetView()->GetNativeView()); 698 preview_web_contents()->GetView()->GetNativeView());
597 print_dialog_cloud::CreatePrintDialogForBytes( 699 print_dialog_cloud::CreatePrintDialogForBytes(
(...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after
776 GetNumberFormatAndMeasurementSystem(&initial_settings); 878 GetNumberFormatAndMeasurementSystem(&initial_settings);
777 web_ui()->CallJavascriptFunction("setInitialSettings", initial_settings); 879 web_ui()->CallJavascriptFunction("setInitialSettings", initial_settings);
778 } 880 }
779 881
780 void PrintPreviewHandler::ClosePreviewDialog() { 882 void PrintPreviewHandler::ClosePreviewDialog() {
781 PrintPreviewUI* print_preview_ui = 883 PrintPreviewUI* print_preview_ui =
782 static_cast<PrintPreviewUI*>(web_ui()->GetController()); 884 static_cast<PrintPreviewUI*>(web_ui()->GetController());
783 print_preview_ui->OnClosePrintPreviewDialog(); 885 print_preview_ui->OnClosePrintPreviewDialog();
784 } 886 }
785 887
888 void PrintPreviewHandler::SendAccessToken(const std::string& type,
889 const std::string& access_token) {
890 VLOG(1) << "Get getAccessToken finished";
891 web_ui()->CallJavascriptFunction("onDidGetAccessToken", StringValue(type),
892 StringValue(access_token));
893 }
894
786 void PrintPreviewHandler::SendPrinterCapabilities( 895 void PrintPreviewHandler::SendPrinterCapabilities(
787 const DictionaryValue& settings_info) { 896 const DictionaryValue& settings_info) {
788 VLOG(1) << "Get printer capabilities finished"; 897 VLOG(1) << "Get printer capabilities finished";
789 web_ui()->CallJavascriptFunction("updateWithPrinterCapabilities", 898 web_ui()->CallJavascriptFunction("updateWithPrinterCapabilities",
790 settings_info); 899 settings_info);
791 } 900 }
792 901
793 void PrintPreviewHandler::SendFailedToGetPrinterCapabilities( 902 void PrintPreviewHandler::SendFailedToGetPrinterCapabilities(
794 const std::string& printer_name) { 903 const std::string& printer_name) {
795 VLOG(1) << "Get printer capabilities failed"; 904 VLOG(1) << "Get printer capabilities failed";
(...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after
964 // Nothing to print, no preview available. 1073 // Nothing to print, no preview available.
965 return false; 1074 return false;
966 } 1075 }
967 DCHECK(tmp_data->size() && tmp_data->front()); 1076 DCHECK(tmp_data->size() && tmp_data->front());
968 1077
969 *data = tmp_data; 1078 *data = tmp_data;
970 *title = print_preview_ui->initiator_tab_title(); 1079 *title = print_preview_ui->initiator_tab_title();
971 return true; 1080 return true;
972 } 1081 }
973 1082
OLDNEW
« no previous file with comments | « chrome/browser/ui/webui/print_preview/print_preview_handler.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698