| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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_handler.h" | 5 #include "chrome/browser/ui/webui/print_preview_handler.h" |
| 6 | 6 |
| 7 #include <ctype.h> | 7 #include <ctype.h> |
| 8 | 8 |
| 9 #include <string> | 9 #include <string> |
| 10 #include <vector> | 10 #include <vector> |
| (...skipping 23 matching lines...) Expand all Loading... |
| 34 #include "chrome/browser/printing/printer_manager_dialog.h" | 34 #include "chrome/browser/printing/printer_manager_dialog.h" |
| 35 #include "chrome/browser/profiles/profile.h" | 35 #include "chrome/browser/profiles/profile.h" |
| 36 #include "chrome/browser/ui/browser_list.h" | 36 #include "chrome/browser/ui/browser_list.h" |
| 37 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" | 37 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" |
| 38 #include "chrome/browser/ui/webui/cloud_print_signin_dialog.h" | 38 #include "chrome/browser/ui/webui/cloud_print_signin_dialog.h" |
| 39 #include "chrome/browser/ui/webui/print_preview_ui.h" | 39 #include "chrome/browser/ui/webui/print_preview_ui.h" |
| 40 #include "chrome/common/chrome_paths.h" | 40 #include "chrome/common/chrome_paths.h" |
| 41 #include "chrome/common/pref_names.h" | 41 #include "chrome/common/pref_names.h" |
| 42 #include "chrome/common/print_messages.h" | 42 #include "chrome/common/print_messages.h" |
| 43 #include "content/browser/renderer_host/render_view_host.h" | 43 #include "content/browser/renderer_host/render_view_host.h" |
| 44 #include "content/browser/tab_contents/tab_contents.h" | 44 #include "content/browser/renderer_host/render_view_host_delegate.h" |
| 45 #include "content/browser/tab_contents/navigation_controller.h" |
| 45 #include "content/public/browser/browser_thread.h" | 46 #include "content/public/browser/browser_thread.h" |
| 46 #include "content/public/browser/navigation_entry.h" | 47 #include "content/public/browser/navigation_entry.h" |
| 48 #include "content/public/browser/web_contents.h" |
| 47 #include "printing/backend/print_backend.h" | 49 #include "printing/backend/print_backend.h" |
| 48 #include "printing/metafile.h" | 50 #include "printing/metafile.h" |
| 49 #include "printing/metafile_impl.h" | 51 #include "printing/metafile_impl.h" |
| 50 #include "printing/page_range.h" | 52 #include "printing/page_range.h" |
| 51 #include "printing/page_size_margins.h" | 53 #include "printing/page_size_margins.h" |
| 52 #include "printing/print_settings.h" | 54 #include "printing/print_settings.h" |
| 53 #include "unicode/ulocdata.h" | 55 #include "unicode/ulocdata.h" |
| 54 | 56 |
| 55 #if !defined(OS_MACOSX) | 57 #if !defined(OS_MACOSX) |
| 56 #include "base/command_line.h" | 58 #include "base/command_line.h" |
| 57 #include "chrome/common/chrome_switches.h" | 59 #include "chrome/common/chrome_switches.h" |
| 58 #endif | 60 #endif |
| 59 | 61 |
| 60 using content::BrowserThread; | 62 using content::BrowserThread; |
| 61 using content::NavigationEntry; | 63 using content::NavigationEntry; |
| 62 using content::OpenURLParams; | 64 using content::OpenURLParams; |
| 63 using content::Referrer; | 65 using content::Referrer; |
| 66 using content::WebContents; |
| 64 using printing::Metafile; | 67 using printing::Metafile; |
| 65 | 68 |
| 66 namespace { | 69 namespace { |
| 67 | 70 |
| 68 enum UserActionBuckets { | 71 enum UserActionBuckets { |
| 69 PRINT_TO_PRINTER, | 72 PRINT_TO_PRINTER, |
| 70 PRINT_TO_PDF, | 73 PRINT_TO_PDF, |
| 71 CANCEL, | 74 CANCEL, |
| 72 FALLBACK_TO_ADVANCED_SETTINGS_DIALOG, | 75 FALLBACK_TO_ADVANCED_SETTINGS_DIALOG, |
| 73 PREVIEW_FAILED, | 76 PREVIEW_FAILED, |
| (...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 254 base::Bind(&PrintPreviewHandler::HandleSaveLastPrinter, | 257 base::Bind(&PrintPreviewHandler::HandleSaveLastPrinter, |
| 255 base::Unretained(this))); | 258 base::Unretained(this))); |
| 256 web_ui()->RegisterMessageCallback("getInitialSettings", | 259 web_ui()->RegisterMessageCallback("getInitialSettings", |
| 257 base::Bind(&PrintPreviewHandler::HandleGetInitialSettings, | 260 base::Bind(&PrintPreviewHandler::HandleGetInitialSettings, |
| 258 base::Unretained(this))); | 261 base::Unretained(this))); |
| 259 } | 262 } |
| 260 | 263 |
| 261 TabContentsWrapper* PrintPreviewHandler::preview_tab_wrapper() const { | 264 TabContentsWrapper* PrintPreviewHandler::preview_tab_wrapper() const { |
| 262 return TabContentsWrapper::GetCurrentWrapperForContents(preview_tab()); | 265 return TabContentsWrapper::GetCurrentWrapperForContents(preview_tab()); |
| 263 } | 266 } |
| 264 TabContents* PrintPreviewHandler::preview_tab() const { | 267 |
| 265 return web_ui()->tab_contents(); | 268 WebContents* PrintPreviewHandler::preview_tab() const { |
| 269 return web_ui()->web_contents(); |
| 266 } | 270 } |
| 267 | 271 |
| 268 void PrintPreviewHandler::HandleGetPrinters(const ListValue* /*args*/) { | 272 void PrintPreviewHandler::HandleGetPrinters(const ListValue* /*args*/) { |
| 269 scoped_refptr<PrintSystemTaskProxy> task = | 273 scoped_refptr<PrintSystemTaskProxy> task = |
| 270 new PrintSystemTaskProxy(AsWeakPtr(), | 274 new PrintSystemTaskProxy(AsWeakPtr(), |
| 271 print_backend_.get(), | 275 print_backend_.get(), |
| 272 has_logged_printers_count_); | 276 has_logged_printers_count_); |
| 273 has_logged_printers_count_ = true; | 277 has_logged_printers_count_ = true; |
| 274 | 278 |
| 275 BrowserThread::PostTask( | 279 BrowserThread::PostTask( |
| (...skipping 30 matching lines...) Expand all Loading... |
| 306 | 310 |
| 307 // Retrieve the page title and url and send it to the renderer process if | 311 // Retrieve the page title and url and send it to the renderer process if |
| 308 // headers and footers are to be displayed. | 312 // headers and footers are to be displayed. |
| 309 bool display_header_footer = false; | 313 bool display_header_footer = false; |
| 310 if (!settings->GetBoolean(printing::kSettingHeaderFooterEnabled, | 314 if (!settings->GetBoolean(printing::kSettingHeaderFooterEnabled, |
| 311 &display_header_footer)) { | 315 &display_header_footer)) { |
| 312 NOTREACHED(); | 316 NOTREACHED(); |
| 313 } | 317 } |
| 314 if (display_header_footer) { | 318 if (display_header_footer) { |
| 315 settings->SetString(printing::kSettingHeaderFooterTitle, | 319 settings->SetString(printing::kSettingHeaderFooterTitle, |
| 316 initiator_tab->tab_contents()->GetTitle()); | 320 initiator_tab->web_contents()->GetTitle()); |
| 317 std::string url; | 321 std::string url; |
| 318 NavigationEntry* entry = | 322 NavigationEntry* entry = |
| 319 initiator_tab->tab_contents()->GetController().GetActiveEntry(); | 323 initiator_tab->web_contents()->GetController().GetActiveEntry(); |
| 320 if (entry) | 324 if (entry) |
| 321 url = entry->GetVirtualURL().spec(); | 325 url = entry->GetVirtualURL().spec(); |
| 322 settings->SetString(printing::kSettingHeaderFooterURL, url); | 326 settings->SetString(printing::kSettingHeaderFooterURL, url); |
| 323 } | 327 } |
| 324 | 328 |
| 325 bool generate_draft_data = false; | 329 bool generate_draft_data = false; |
| 326 bool success = settings->GetBoolean(printing::kSettingGenerateDraftData, | 330 bool success = settings->GetBoolean(printing::kSettingGenerateDraftData, |
| 327 &generate_draft_data); | 331 &generate_draft_data); |
| 328 DCHECK(success); | 332 DCHECK(success); |
| 329 | 333 |
| 330 if (!generate_draft_data) { | 334 if (!generate_draft_data) { |
| 331 double draft_page_count_double = -1; | 335 double draft_page_count_double = -1; |
| 332 success = args->GetDouble(1, &draft_page_count_double); | 336 success = args->GetDouble(1, &draft_page_count_double); |
| 333 DCHECK(success); | 337 DCHECK(success); |
| 334 int draft_page_count = static_cast<int>(draft_page_count_double); | 338 int draft_page_count = static_cast<int>(draft_page_count_double); |
| 335 | 339 |
| 336 bool preview_modifiable = false; | 340 bool preview_modifiable = false; |
| 337 success = args->GetBoolean(2, &preview_modifiable); | 341 success = args->GetBoolean(2, &preview_modifiable); |
| 338 DCHECK(success); | 342 DCHECK(success); |
| 339 | 343 |
| 340 if (draft_page_count != -1 && preview_modifiable && | 344 if (draft_page_count != -1 && preview_modifiable && |
| 341 print_preview_ui->GetAvailableDraftPageCount() != draft_page_count) { | 345 print_preview_ui->GetAvailableDraftPageCount() != draft_page_count) { |
| 342 settings->SetBoolean(printing::kSettingGenerateDraftData, true); | 346 settings->SetBoolean(printing::kSettingGenerateDraftData, true); |
| 343 } | 347 } |
| 344 } | 348 } |
| 345 | 349 |
| 346 VLOG(1) << "Print preview request start"; | 350 VLOG(1) << "Print preview request start"; |
| 347 RenderViewHost* rvh = initiator_tab->tab_contents()->GetRenderViewHost(); | 351 RenderViewHost* rvh = initiator_tab->web_contents()->GetRenderViewHost(); |
| 348 rvh->Send(new PrintMsg_PrintPreview(rvh->routing_id(), *settings)); | 352 rvh->Send(new PrintMsg_PrintPreview(rvh->routing_id(), *settings)); |
| 349 } | 353 } |
| 350 | 354 |
| 351 void PrintPreviewHandler::HandlePrint(const ListValue* args) { | 355 void PrintPreviewHandler::HandlePrint(const ListValue* args) { |
| 352 ReportStats(); | 356 ReportStats(); |
| 353 | 357 |
| 354 // Record the number of times the user requests to regenerate preview data | 358 // Record the number of times the user requests to regenerate preview data |
| 355 // before printing. | 359 // before printing. |
| 356 UMA_HISTOGRAM_COUNTS("PrintPreview.RegeneratePreviewRequest.BeforePrint", | 360 UMA_HISTOGRAM_COUNTS("PrintPreview.RegeneratePreviewRequest.BeforePrint", |
| 357 regenerate_preview_request_count_); | 361 regenerate_preview_request_count_); |
| 358 | 362 |
| 359 TabContentsWrapper* initiator_tab = GetInitiatorTab(); | 363 TabContentsWrapper* initiator_tab = GetInitiatorTab(); |
| 360 CHECK(initiator_tab); | 364 CHECK(initiator_tab); |
| 361 | 365 |
| 362 RenderViewHost* init_rvh = initiator_tab->tab_contents()->GetRenderViewHost(); | 366 RenderViewHost* init_rvh = initiator_tab->web_contents()->GetRenderViewHost(); |
| 363 init_rvh->Send(new PrintMsg_ResetScriptedPrintCount(init_rvh->routing_id())); | 367 init_rvh->Send(new PrintMsg_ResetScriptedPrintCount(init_rvh->routing_id())); |
| 364 | 368 |
| 365 scoped_ptr<DictionaryValue> settings(GetSettingsDictionary(args)); | 369 scoped_ptr<DictionaryValue> settings(GetSettingsDictionary(args)); |
| 366 if (!settings.get()) | 370 if (!settings.get()) |
| 367 return; | 371 return; |
| 368 | 372 |
| 369 // Storing last used color model. | 373 // Storing last used color model. |
| 370 int color_model; | 374 int color_model; |
| 371 if (!settings->GetInteger(printing::kSettingColor, &color_model)) | 375 if (!settings->GetInteger(printing::kSettingColor, &color_model)) |
| 372 color_model = printing::GRAY; | 376 color_model = printing::GRAY; |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 419 // association with the initiator tab yet. | 423 // association with the initiator tab yet. |
| 420 PrintPreviewUI* print_preview_ui = static_cast<PrintPreviewUI*>(web_ui()); | 424 PrintPreviewUI* print_preview_ui = static_cast<PrintPreviewUI*>(web_ui()); |
| 421 print_preview_ui->OnHidePreviewTab(); | 425 print_preview_ui->OnHidePreviewTab(); |
| 422 | 426 |
| 423 // Do this so the initiator tab can open a new print preview tab. | 427 // Do this so the initiator tab can open a new print preview tab. |
| 424 ClearInitiatorTabDetails(); | 428 ClearInitiatorTabDetails(); |
| 425 | 429 |
| 426 // The PDF being printed contains only the pages that the user selected, | 430 // The PDF being printed contains only the pages that the user selected, |
| 427 // so ignore the page range and print all pages. | 431 // so ignore the page range and print all pages. |
| 428 settings->Remove(printing::kSettingPageRange, NULL); | 432 settings->Remove(printing::kSettingPageRange, NULL); |
| 429 RenderViewHost* rvh = web_ui()->tab_contents()->GetRenderViewHost(); | 433 RenderViewHost* rvh = web_ui()->web_contents()->GetRenderViewHost(); |
| 430 rvh->Send(new PrintMsg_PrintForPrintPreview(rvh->routing_id(), *settings)); | 434 rvh->Send(new PrintMsg_PrintForPrintPreview(rvh->routing_id(), *settings)); |
| 431 } | 435 } |
| 432 initiator_tab->print_view_manager()->PrintPreviewDone(); | 436 initiator_tab->print_view_manager()->PrintPreviewDone(); |
| 433 } | 437 } |
| 434 | 438 |
| 435 void PrintPreviewHandler::HandlePrintToPdf( | 439 void PrintPreviewHandler::HandlePrintToPdf( |
| 436 const base::DictionaryValue& settings) { | 440 const base::DictionaryValue& settings) { |
| 437 if (print_to_pdf_path_.get()) { | 441 if (print_to_pdf_path_.get()) { |
| 438 // User has already selected a path, no need to show the dialog again. | 442 // User has already selected a path, no need to show the dialog again. |
| 439 PostPrintToPdfTask(); | 443 PostPrintToPdfTask(); |
| (...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 658 | 662 |
| 659 if (print_preview_ui->source_is_modifiable()) { | 663 if (print_preview_ui->source_is_modifiable()) { |
| 660 GetLastUsedMarginSettings(&initial_settings); | 664 GetLastUsedMarginSettings(&initial_settings); |
| 661 GetNumberFormatAndMeasurementSystem(&initial_settings); | 665 GetNumberFormatAndMeasurementSystem(&initial_settings); |
| 662 } | 666 } |
| 663 web_ui()->CallJavascriptFunction("setInitialSettings", initial_settings); | 667 web_ui()->CallJavascriptFunction("setInitialSettings", initial_settings); |
| 664 } | 668 } |
| 665 | 669 |
| 666 void PrintPreviewHandler::ActivateInitiatorTabAndClosePreviewTab() { | 670 void PrintPreviewHandler::ActivateInitiatorTabAndClosePreviewTab() { |
| 667 TabContentsWrapper* initiator_tab = GetInitiatorTab(); | 671 TabContentsWrapper* initiator_tab = GetInitiatorTab(); |
| 668 if (initiator_tab) { | 672 if (initiator_tab) |
| 669 static_cast<RenderViewHostDelegate*>( | 673 initiator_tab->web_contents()->GetRenderViewHost()->delegate()->Activate(); |
| 670 initiator_tab->tab_contents())->Activate(); | |
| 671 } | |
| 672 PrintPreviewUI* print_preview_ui = static_cast<PrintPreviewUI*>(web_ui()); | 674 PrintPreviewUI* print_preview_ui = static_cast<PrintPreviewUI*>(web_ui()); |
| 673 print_preview_ui->OnClosePrintPreviewTab(); | 675 print_preview_ui->OnClosePrintPreviewTab(); |
| 674 } | 676 } |
| 675 | 677 |
| 676 void PrintPreviewHandler::SendPrinterCapabilities( | 678 void PrintPreviewHandler::SendPrinterCapabilities( |
| 677 const DictionaryValue& settings_info) { | 679 const DictionaryValue& settings_info) { |
| 678 VLOG(1) << "Get printer capabilities finished"; | 680 VLOG(1) << "Get printer capabilities finished"; |
| 679 web_ui()->CallJavascriptFunction("updateWithPrinterCapabilities", | 681 web_ui()->CallJavascriptFunction("updateWithPrinterCapabilities", |
| 680 settings_info); | 682 settings_info); |
| 681 } | 683 } |
| (...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 855 return; | 857 return; |
| 856 | 858 |
| 857 // We no longer require the initiator tab details. Remove those details | 859 // We no longer require the initiator tab details. Remove those details |
| 858 // associated with the preview tab to allow the initiator tab to create | 860 // associated with the preview tab to allow the initiator tab to create |
| 859 // another preview tab. | 861 // another preview tab. |
| 860 printing::PrintPreviewTabController* tab_controller = | 862 printing::PrintPreviewTabController* tab_controller = |
| 861 printing::PrintPreviewTabController::GetInstance(); | 863 printing::PrintPreviewTabController::GetInstance(); |
| 862 if (tab_controller) | 864 if (tab_controller) |
| 863 tab_controller->EraseInitiatorTabInfo(preview_tab_wrapper()); | 865 tab_controller->EraseInitiatorTabInfo(preview_tab_wrapper()); |
| 864 } | 866 } |
| OLD | NEW |