| 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 | 8 |
| 9 #include <string> | 9 #include <string> |
| 10 | 10 |
| (...skipping 17 matching lines...) Expand all Loading... |
| 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" | |
| 40 #include "chrome/browser/ui/browser_finder.h" | 38 #include "chrome/browser/ui/browser_finder.h" |
| 41 #include "chrome/browser/ui/browser_tabstrip.h" | 39 #include "chrome/browser/ui/browser_tabstrip.h" |
| 42 #include "chrome/browser/ui/chrome_select_file_policy.h" | 40 #include "chrome/browser/ui/chrome_select_file_policy.h" |
| 43 #include "chrome/browser/ui/webui/print_preview/print_preview_ui.h" | 41 #include "chrome/browser/ui/webui/print_preview/print_preview_ui.h" |
| 44 #include "chrome/browser/ui/webui/print_preview/sticky_settings.h" | 42 #include "chrome/browser/ui/webui/print_preview/sticky_settings.h" |
| 45 #include "chrome/common/chrome_paths.h" | 43 #include "chrome/common/chrome_paths.h" |
| 46 #include "chrome/common/chrome_switches.h" | 44 #include "chrome/common/chrome_switches.h" |
| 47 #include "chrome/common/pref_names.h" | 45 #include "chrome/common/pref_names.h" |
| 48 #include "chrome/common/print_messages.h" | 46 #include "chrome/common/print_messages.h" |
| 49 #include "content/public/browser/browser_context.h" | 47 #include "content/public/browser/browser_context.h" |
| (...skipping 21 matching lines...) Expand all Loading... |
| 71 using content::BrowserThread; | 69 using content::BrowserThread; |
| 72 using content::NavigationEntry; | 70 using content::NavigationEntry; |
| 73 using content::OpenURLParams; | 71 using content::OpenURLParams; |
| 74 using content::RenderViewHost; | 72 using content::RenderViewHost; |
| 75 using content::Referrer; | 73 using content::Referrer; |
| 76 using content::WebContents; | 74 using content::WebContents; |
| 77 using printing::Metafile; | 75 using printing::Metafile; |
| 78 | 76 |
| 79 namespace { | 77 namespace { |
| 80 | 78 |
| 81 // The cloud print OAuth2 scope. | |
| 82 const char kCloudPrintAuth[] = "https://www.googleapis.com/auth/cloudprint"; | |
| 83 | |
| 84 enum UserActionBuckets { | 79 enum UserActionBuckets { |
| 85 PRINT_TO_PRINTER, | 80 PRINT_TO_PRINTER, |
| 86 PRINT_TO_PDF, | 81 PRINT_TO_PDF, |
| 87 CANCEL, | 82 CANCEL, |
| 88 FALLBACK_TO_ADVANCED_SETTINGS_DIALOG, | 83 FALLBACK_TO_ADVANCED_SETTINGS_DIALOG, |
| 89 PREVIEW_FAILED, | 84 PREVIEW_FAILED, |
| 90 PREVIEW_STARTED, | 85 PREVIEW_STARTED, |
| 91 INITIATOR_TAB_CRASHED, // UNUSED | 86 INITIATOR_TAB_CRASHED, // UNUSED |
| 92 INITIATOR_TAB_CLOSED, | 87 INITIATOR_TAB_CLOSED, |
| 93 PRINT_WITH_CLOUD_PRINT, | 88 PRINT_WITH_CLOUD_PRINT, |
| (...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 234 BrowserThread::UI, FROM_HERE, | 229 BrowserThread::UI, FROM_HERE, |
| 235 base::Bind(&base::DeletePointer<Metafile>, metafile)); | 230 base::Bind(&base::DeletePointer<Metafile>, metafile)); |
| 236 } | 231 } |
| 237 #endif | 232 #endif |
| 238 | 233 |
| 239 static base::LazyInstance<printing::StickySettings> sticky_settings = | 234 static base::LazyInstance<printing::StickySettings> sticky_settings = |
| 240 LAZY_INSTANCE_INITIALIZER; | 235 LAZY_INSTANCE_INITIALIZER; |
| 241 | 236 |
| 242 } // namespace | 237 } // namespace |
| 243 | 238 |
| 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 | |
| 315 // static | 239 // static |
| 316 printing::StickySettings* PrintPreviewHandler::GetStickySettings() { | 240 printing::StickySettings* PrintPreviewHandler::GetStickySettings() { |
| 317 return sticky_settings.Pointer(); | 241 return sticky_settings.Pointer(); |
| 318 } | 242 } |
| 319 | 243 |
| 320 PrintPreviewHandler::PrintPreviewHandler() | 244 PrintPreviewHandler::PrintPreviewHandler() |
| 321 : print_backend_(printing::PrintBackend::CreateInstance(NULL)), | 245 : print_backend_(printing::PrintBackend::CreateInstance(NULL)), |
| 322 regenerate_preview_request_count_(0), | 246 regenerate_preview_request_count_(0), |
| 323 manage_printers_dialog_request_count_(0), | 247 manage_printers_dialog_request_count_(0), |
| 324 manage_cloud_printers_dialog_request_count_(0), | 248 manage_cloud_printers_dialog_request_count_(0), |
| (...skipping 19 matching lines...) Expand all Loading... |
| 344 base::Unretained(this))); | 268 base::Unretained(this))); |
| 345 web_ui()->RegisterMessageCallback("getPrinterCapabilities", | 269 web_ui()->RegisterMessageCallback("getPrinterCapabilities", |
| 346 base::Bind(&PrintPreviewHandler::HandleGetPrinterCapabilities, | 270 base::Bind(&PrintPreviewHandler::HandleGetPrinterCapabilities, |
| 347 base::Unretained(this))); | 271 base::Unretained(this))); |
| 348 web_ui()->RegisterMessageCallback("showSystemDialog", | 272 web_ui()->RegisterMessageCallback("showSystemDialog", |
| 349 base::Bind(&PrintPreviewHandler::HandleShowSystemDialog, | 273 base::Bind(&PrintPreviewHandler::HandleShowSystemDialog, |
| 350 base::Unretained(this))); | 274 base::Unretained(this))); |
| 351 web_ui()->RegisterMessageCallback("signIn", | 275 web_ui()->RegisterMessageCallback("signIn", |
| 352 base::Bind(&PrintPreviewHandler::HandleSignin, | 276 base::Bind(&PrintPreviewHandler::HandleSignin, |
| 353 base::Unretained(this))); | 277 base::Unretained(this))); |
| 354 web_ui()->RegisterMessageCallback("getAccessToken", | |
| 355 base::Bind(&PrintPreviewHandler::HandleGetAccessToken, | |
| 356 base::Unretained(this))); | |
| 357 web_ui()->RegisterMessageCallback("manageCloudPrinters", | 278 web_ui()->RegisterMessageCallback("manageCloudPrinters", |
| 358 base::Bind(&PrintPreviewHandler::HandleManageCloudPrint, | 279 base::Bind(&PrintPreviewHandler::HandleManageCloudPrint, |
| 359 base::Unretained(this))); | 280 base::Unretained(this))); |
| 360 web_ui()->RegisterMessageCallback("manageLocalPrinters", | 281 web_ui()->RegisterMessageCallback("manageLocalPrinters", |
| 361 base::Bind(&PrintPreviewHandler::HandleManagePrinters, | 282 base::Bind(&PrintPreviewHandler::HandleManagePrinters, |
| 362 base::Unretained(this))); | 283 base::Unretained(this))); |
| 363 web_ui()->RegisterMessageCallback("closePrintPreviewDialog", | 284 web_ui()->RegisterMessageCallback("closePrintPreviewDialog", |
| 364 base::Bind(&PrintPreviewHandler::HandleClosePreviewDialog, | 285 base::Bind(&PrintPreviewHandler::HandleClosePreviewDialog, |
| 365 base::Unretained(this))); | 286 base::Unretained(this))); |
| 366 web_ui()->RegisterMessageCallback("hidePreview", | 287 web_ui()->RegisterMessageCallback("hidePreview", |
| (...skipping 290 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 657 | 578 |
| 658 void PrintPreviewHandler::HandleSignin(const ListValue* /*args*/) { | 579 void PrintPreviewHandler::HandleSignin(const ListValue* /*args*/) { |
| 659 gfx::NativeWindow modal_parent = platform_util::GetTopLevel( | 580 gfx::NativeWindow modal_parent = platform_util::GetTopLevel( |
| 660 preview_web_contents()->GetView()->GetNativeView()); | 581 preview_web_contents()->GetView()->GetNativeView()); |
| 661 print_dialog_cloud::CreateCloudPrintSigninDialog( | 582 print_dialog_cloud::CreateCloudPrintSigninDialog( |
| 662 preview_web_contents()->GetBrowserContext(), | 583 preview_web_contents()->GetBrowserContext(), |
| 663 modal_parent, | 584 modal_parent, |
| 664 base::Bind(&PrintPreviewHandler::OnSigninComplete, AsWeakPtr())); | 585 base::Bind(&PrintPreviewHandler::OnSigninComplete, AsWeakPtr())); |
| 665 } | 586 } |
| 666 | 587 |
| 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 | |
| 690 void PrintPreviewHandler::PrintWithCloudPrintDialog( | 588 void PrintPreviewHandler::PrintWithCloudPrintDialog( |
| 691 const base::RefCountedBytes* data, | 589 const base::RefCountedBytes* data, |
| 692 const string16& title) { | 590 const string16& title) { |
| 693 // Record the number of times the user asks to print via cloud print | 591 // Record the number of times the user asks to print via cloud print |
| 694 // instead of the print preview dialog. | 592 // instead of the print preview dialog. |
| 695 ReportStats(); | 593 ReportStats(); |
| 696 | 594 |
| 697 gfx::NativeWindow modal_parent = platform_util::GetTopLevel( | 595 gfx::NativeWindow modal_parent = platform_util::GetTopLevel( |
| 698 preview_web_contents()->GetView()->GetNativeView()); | 596 preview_web_contents()->GetView()->GetNativeView()); |
| 699 print_dialog_cloud::CreatePrintDialogForBytes( | 597 print_dialog_cloud::CreatePrintDialogForBytes( |
| (...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 878 GetNumberFormatAndMeasurementSystem(&initial_settings); | 776 GetNumberFormatAndMeasurementSystem(&initial_settings); |
| 879 web_ui()->CallJavascriptFunction("setInitialSettings", initial_settings); | 777 web_ui()->CallJavascriptFunction("setInitialSettings", initial_settings); |
| 880 } | 778 } |
| 881 | 779 |
| 882 void PrintPreviewHandler::ClosePreviewDialog() { | 780 void PrintPreviewHandler::ClosePreviewDialog() { |
| 883 PrintPreviewUI* print_preview_ui = | 781 PrintPreviewUI* print_preview_ui = |
| 884 static_cast<PrintPreviewUI*>(web_ui()->GetController()); | 782 static_cast<PrintPreviewUI*>(web_ui()->GetController()); |
| 885 print_preview_ui->OnClosePrintPreviewDialog(); | 783 print_preview_ui->OnClosePrintPreviewDialog(); |
| 886 } | 784 } |
| 887 | 785 |
| 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 | |
| 895 void PrintPreviewHandler::SendPrinterCapabilities( | 786 void PrintPreviewHandler::SendPrinterCapabilities( |
| 896 const DictionaryValue& settings_info) { | 787 const DictionaryValue& settings_info) { |
| 897 VLOG(1) << "Get printer capabilities finished"; | 788 VLOG(1) << "Get printer capabilities finished"; |
| 898 web_ui()->CallJavascriptFunction("updateWithPrinterCapabilities", | 789 web_ui()->CallJavascriptFunction("updateWithPrinterCapabilities", |
| 899 settings_info); | 790 settings_info); |
| 900 } | 791 } |
| 901 | 792 |
| 902 void PrintPreviewHandler::SendFailedToGetPrinterCapabilities( | 793 void PrintPreviewHandler::SendFailedToGetPrinterCapabilities( |
| 903 const std::string& printer_name) { | 794 const std::string& printer_name) { |
| 904 VLOG(1) << "Get printer capabilities failed"; | 795 VLOG(1) << "Get printer capabilities failed"; |
| (...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1073 // Nothing to print, no preview available. | 964 // Nothing to print, no preview available. |
| 1074 return false; | 965 return false; |
| 1075 } | 966 } |
| 1076 DCHECK(tmp_data->size() && tmp_data->front()); | 967 DCHECK(tmp_data->size() && tmp_data->front()); |
| 1077 | 968 |
| 1078 *data = tmp_data; | 969 *data = tmp_data; |
| 1079 *title = print_preview_ui->initiator_tab_title(); | 970 *title = print_preview_ui->initiator_tab_title(); |
| 1080 return true; | 971 return true; |
| 1081 } | 972 } |
| 1082 | 973 |
| OLD | NEW |