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
3 // found in the LICENSE file.
5 #include "chrome/browser/ui/webui/print_preview/print_preview_ui.h"
9 #include "base/id_map.h"
10 #include "base/lazy_instance.h"
11 #include "base/memory/ref_counted_memory.h"
12 #include "base/metrics/histogram.h"
13 #include "base/strings/string_number_conversions.h"
14 #include "base/strings/string_split.h"
15 #include "base/strings/string_util.h"
16 #include "base/strings/utf_string_conversions.h"
17 #include "base/synchronization/lock.h"
18 #include "base/values.h"
19 #include "chrome/browser/browser_process.h"
20 #include "chrome/browser/printing/background_printing_manager.h"
21 #include "chrome/browser/printing/print_preview_data_service.h"
22 #include "chrome/browser/profiles/profile.h"
23 #include "chrome/browser/ui/webui/print_preview/print_preview_handler.h"
24 #include "chrome/browser/ui/webui/theme_source.h"
25 #include "chrome/common/print_messages.h"
26 #include "chrome/common/url_constants.h"
27 #include "content/public/browser/url_data_source.h"
28 #include "content/public/browser/web_contents.h"
29 #include "content/public/browser/web_ui_data_source.h"
30 #include "grit/browser_resources.h"
31 #include "grit/chromium_strings.h"
32 #include "grit/generated_resources.h"
33 #include "grit/google_chrome_strings.h"
34 #include "printing/page_size_margins.h"
35 #include "printing/print_job_constants.h"
36 #include "ui/base/l10n/l10n_util.h"
37 #include "ui/gfx/rect.h"
38 #include "ui/web_dialogs/web_dialog_delegate.h"
39 #include "ui/web_dialogs/web_dialog_ui.h"
41 using content::WebContents;
42 using printing::PageSizeMargins;
46 #if defined(OS_MACOSX)
47 // U+0028 U+21E7 U+2318 U+0050 U+0029 in UTF8
48 const char kAdvancedPrintShortcut[] = "\x28\xE2\x8c\xA5\xE2\x8C\x98\x50\x29";
49 #elif defined(OS_WIN) || defined(OS_CHROMEOS)
50 const char kAdvancedPrintShortcut[] = "(Ctrl+Shift+P)";
52 const char kAdvancedPrintShortcut[] = "(Shift+Ctrl+P)";
55 // Thread-safe wrapper around a std::map to keep track of mappings from
56 // PrintPreviewUI IDs to most recent print preview request IDs.
57 class PrintPreviewRequestIdMapWithLock {
59 PrintPreviewRequestIdMapWithLock() {}
60 ~PrintPreviewRequestIdMapWithLock() {}
62 // Gets the value for |preview_id|.
63 // Returns true and sets |out_value| on success.
64 bool Get(int32 preview_id, int* out_value) {
65 base::AutoLock lock(lock_);
66 PrintPreviewRequestIdMap::const_iterator it = map_.find(preview_id);
69 *out_value = it->second;
73 // Sets the |value| for |preview_id|.
74 void Set(int32 preview_id, int value) {
75 base::AutoLock lock(lock_);
76 map_[preview_id] = value;
79 // Erases the entry for |preview_id|.
80 void Erase(int32 preview_id) {
81 base::AutoLock lock(lock_);
82 map_.erase(preview_id);
86 // Mapping from PrintPreviewUI ID to print preview request ID.
87 typedef std::map<int, int> PrintPreviewRequestIdMap;
89 PrintPreviewRequestIdMap map_;
92 DISALLOW_COPY_AND_ASSIGN(PrintPreviewRequestIdMapWithLock);
95 // Written to on the UI thread, read from any thread.
96 base::LazyInstance<PrintPreviewRequestIdMapWithLock>
97 g_print_preview_request_id_map = LAZY_INSTANCE_INITIALIZER;
99 // PrintPreviewUI IDMap used to avoid exposing raw pointer addresses to WebUI.
100 // Only accessed on the UI thread.
101 base::LazyInstance<IDMap<PrintPreviewUI> >
102 g_print_preview_ui_id_map = LAZY_INSTANCE_INITIALIZER;
104 // PrintPreviewUI serves data for chrome://print requests.
106 // The format for requesting PDF data is as follows:
107 // chrome://print/<PrintPreviewUIID>/<PageIndex>/print.pdf
109 // Parameters (< > required):
110 // <PrintPreviewUIID> = PrintPreview UI ID
111 // <PageIndex> = Page index is zero-based or
112 // |printing::COMPLETE_PREVIEW_DOCUMENT_INDEX| to represent
113 // a print ready PDF.
116 // chrome://print/123/10/print.pdf
118 // Requests to chrome://print with paths not ending in /print.pdf are used
119 // to return the markup or other resources for the print preview page itself.
120 bool HandleRequestCallback(
121 const std::string& path,
122 const content::WebUIDataSource::GotDataCallback& callback) {
123 // ChromeWebUIDataSource handles most requests except for the print preview
125 if (!EndsWith(path, "/print.pdf", true))
128 // Print Preview data.
129 scoped_refptr<base::RefCountedBytes> data;
130 std::vector<std::string> url_substr;
131 base::SplitString(path, '/', &url_substr);
132 int preview_ui_id = -1;
134 if (url_substr.size() == 3 &&
135 base::StringToInt(url_substr[0], &preview_ui_id),
136 base::StringToInt(url_substr[1], &page_index) &&
137 preview_ui_id >= 0) {
138 PrintPreviewDataService::GetInstance()->GetDataEntry(
139 preview_ui_id, page_index, &data);
142 callback.Run(data.get());
146 scoped_refptr<base::RefCountedBytes> empty_bytes(new base::RefCountedBytes);
147 callback.Run(empty_bytes.get());
151 content::WebUIDataSource* CreatePrintPreviewUISource() {
152 content::WebUIDataSource* source =
153 content::WebUIDataSource::Create(chrome::kChromeUIPrintHost);
154 #if defined(OS_CHROMEOS)
155 source->AddLocalizedString("title",
156 IDS_PRINT_PREVIEW_GOOGLE_CLOUD_PRINT_TITLE);
158 source->AddLocalizedString("title", IDS_PRINT_PREVIEW_TITLE);
160 source->AddLocalizedString("loading", IDS_PRINT_PREVIEW_LOADING);
161 source->AddLocalizedString("noPlugin", IDS_PRINT_PREVIEW_NO_PLUGIN);
162 source->AddLocalizedString("launchNativeDialog",
163 IDS_PRINT_PREVIEW_NATIVE_DIALOG);
164 source->AddLocalizedString("previewFailed", IDS_PRINT_PREVIEW_FAILED);
165 source->AddLocalizedString("invalidPrinterSettings",
166 IDS_PRINT_PREVIEW_INVALID_PRINTER_SETTINGS);
167 source->AddLocalizedString("printButton", IDS_PRINT_PREVIEW_PRINT_BUTTON);
168 source->AddLocalizedString("saveButton", IDS_PRINT_PREVIEW_SAVE_BUTTON);
169 source->AddLocalizedString("cancelButton", IDS_PRINT_PREVIEW_CANCEL_BUTTON);
170 source->AddLocalizedString("printing", IDS_PRINT_PREVIEW_PRINTING);
171 source->AddLocalizedString("printingToPDFInProgress",
172 IDS_PRINT_PREVIEW_PRINTING_TO_PDF_IN_PROGRESS);
173 #if defined(OS_MACOSX)
174 source->AddLocalizedString("openingPDFInPreview",
175 IDS_PRINT_PREVIEW_OPENING_PDF_IN_PREVIEW);
177 source->AddLocalizedString("destinationLabel",
178 IDS_PRINT_PREVIEW_DESTINATION_LABEL);
179 source->AddLocalizedString("copiesLabel", IDS_PRINT_PREVIEW_COPIES_LABEL);
180 source->AddLocalizedString("examplePageRangeText",
181 IDS_PRINT_PREVIEW_EXAMPLE_PAGE_RANGE_TEXT);
182 source->AddLocalizedString("layoutLabel", IDS_PRINT_PREVIEW_LAYOUT_LABEL);
183 source->AddLocalizedString("optionAllPages",
184 IDS_PRINT_PREVIEW_OPTION_ALL_PAGES);
185 source->AddLocalizedString("optionBw", IDS_PRINT_PREVIEW_OPTION_BW);
186 source->AddLocalizedString("optionCollate", IDS_PRINT_PREVIEW_OPTION_COLLATE);
187 source->AddLocalizedString("optionColor", IDS_PRINT_PREVIEW_OPTION_COLOR);
188 source->AddLocalizedString("optionLandscape",
189 IDS_PRINT_PREVIEW_OPTION_LANDSCAPE);
190 source->AddLocalizedString("optionPortrait",
191 IDS_PRINT_PREVIEW_OPTION_PORTRAIT);
192 source->AddLocalizedString("optionTwoSided",
193 IDS_PRINT_PREVIEW_OPTION_TWO_SIDED);
194 source->AddLocalizedString("pagesLabel", IDS_PRINT_PREVIEW_PAGES_LABEL);
195 source->AddLocalizedString("pageRangeTextBox",
196 IDS_PRINT_PREVIEW_PAGE_RANGE_TEXT);
197 source->AddLocalizedString("pageRangeRadio",
198 IDS_PRINT_PREVIEW_PAGE_RANGE_RADIO);
199 source->AddLocalizedString("printToPDF", IDS_PRINT_PREVIEW_PRINT_TO_PDF);
200 source->AddLocalizedString("printPreviewSummaryFormatShort",
201 IDS_PRINT_PREVIEW_SUMMARY_FORMAT_SHORT);
202 source->AddLocalizedString("printPreviewSummaryFormatLong",
203 IDS_PRINT_PREVIEW_SUMMARY_FORMAT_LONG);
204 source->AddLocalizedString("printPreviewSheetsLabelSingular",
205 IDS_PRINT_PREVIEW_SHEETS_LABEL_SINGULAR);
206 source->AddLocalizedString("printPreviewSheetsLabelPlural",
207 IDS_PRINT_PREVIEW_SHEETS_LABEL_PLURAL);
208 source->AddLocalizedString("printPreviewPageLabelSingular",
209 IDS_PRINT_PREVIEW_PAGE_LABEL_SINGULAR);
210 source->AddLocalizedString("printPreviewPageLabelPlural",
211 IDS_PRINT_PREVIEW_PAGE_LABEL_PLURAL);
212 const string16 shortcut_text(UTF8ToUTF16(kAdvancedPrintShortcut));
213 #if defined(OS_CHROMEOS)
215 "systemDialogOption",
216 l10n_util::GetStringFUTF16(
217 IDS_PRINT_PREVIEW_CLOUD_DIALOG_OPTION,
218 l10n_util::GetStringUTF16(IDS_GOOGLE_CLOUD_PRINT),
222 "systemDialogOption",
223 l10n_util::GetStringFUTF16(
224 IDS_PRINT_PREVIEW_SYSTEM_DIALOG_OPTION,
228 "cloudPrintDialogOption",
229 l10n_util::GetStringFUTF16(
230 IDS_PRINT_PREVIEW_CLOUD_DIALOG_OPTION_NO_SHORTCUT,
231 l10n_util::GetStringUTF16(IDS_GOOGLE_CLOUD_PRINT)));
232 #if defined(OS_MACOSX)
233 source->AddLocalizedString("openPdfInPreviewOption",
234 IDS_PRINT_PREVIEW_OPEN_PDF_IN_PREVIEW_APP);
237 "printWithCloudPrintWait",
238 l10n_util::GetStringFUTF16(
239 IDS_PRINT_PREVIEW_PRINT_WITH_CLOUD_PRINT_WAIT,
240 l10n_util::GetStringUTF16(IDS_GOOGLE_CLOUD_PRINT)));
242 "noDestsPromoLearnMoreUrl",
243 chrome::kCloudPrintNoDestinationsLearnMoreURL);
244 source->AddLocalizedString("pageRangeInstruction",
245 IDS_PRINT_PREVIEW_PAGE_RANGE_INSTRUCTION);
246 source->AddLocalizedString("copiesInstruction",
247 IDS_PRINT_PREVIEW_COPIES_INSTRUCTION);
248 source->AddLocalizedString("incrementTitle",
249 IDS_PRINT_PREVIEW_INCREMENT_TITLE);
250 source->AddLocalizedString("decrementTitle",
251 IDS_PRINT_PREVIEW_DECREMENT_TITLE);
252 source->AddLocalizedString("printPagesLabel",
253 IDS_PRINT_PREVIEW_PRINT_PAGES_LABEL);
254 source->AddLocalizedString("optionsLabel", IDS_PRINT_PREVIEW_OPTIONS_LABEL);
255 source->AddLocalizedString("optionHeaderFooter",
256 IDS_PRINT_PREVIEW_OPTION_HEADER_FOOTER);
257 source->AddLocalizedString("optionFitToPage",
258 IDS_PRINT_PREVIEW_OPTION_FIT_TO_PAGE);
259 source->AddLocalizedString(
260 "optionBackgroundColorsAndImages",
261 IDS_PRINT_PREVIEW_OPTION_BACKGROUND_COLORS_AND_IMAGES);
262 source->AddLocalizedString("optionSelectionOnly",
263 IDS_PRINT_PREVIEW_OPTION_SELECTION_ONLY);
264 source->AddLocalizedString("marginsLabel", IDS_PRINT_PREVIEW_MARGINS_LABEL);
265 source->AddLocalizedString("defaultMargins",
266 IDS_PRINT_PREVIEW_DEFAULT_MARGINS);
267 source->AddLocalizedString("noMargins", IDS_PRINT_PREVIEW_NO_MARGINS);
268 source->AddLocalizedString("customMargins", IDS_PRINT_PREVIEW_CUSTOM_MARGINS);
269 source->AddLocalizedString("minimumMargins",
270 IDS_PRINT_PREVIEW_MINIMUM_MARGINS);
271 source->AddLocalizedString("top", IDS_PRINT_PREVIEW_TOP_MARGIN_LABEL);
272 source->AddLocalizedString("bottom", IDS_PRINT_PREVIEW_BOTTOM_MARGIN_LABEL);
273 source->AddLocalizedString("left", IDS_PRINT_PREVIEW_LEFT_MARGIN_LABEL);
274 source->AddLocalizedString("right", IDS_PRINT_PREVIEW_RIGHT_MARGIN_LABEL);
275 source->AddLocalizedString("destinationSearchTitle",
276 IDS_PRINT_PREVIEW_DESTINATION_SEARCH_TITLE);
277 source->AddLocalizedString("userInfo", IDS_PRINT_PREVIEW_USER_INFO);
278 source->AddLocalizedString("cloudPrintPromotion",
279 IDS_PRINT_PREVIEW_CLOUD_PRINT_PROMOTION);
280 source->AddLocalizedString("searchBoxPlaceholder",
281 IDS_PRINT_PREVIEW_SEARCH_BOX_PLACEHOLDER);
282 source->AddLocalizedString("noDestinationsMessage",
283 IDS_PRINT_PREVIEW_NO_DESTINATIONS_MESSAGE);
284 source->AddLocalizedString("showAllButtonText",
285 IDS_PRINT_PREVIEW_SHOW_ALL_BUTTON_TEXT);
286 source->AddLocalizedString("destinationCount",
287 IDS_PRINT_PREVIEW_DESTINATION_COUNT);
288 source->AddLocalizedString("recentDestinationsTitle",
289 IDS_PRINT_PREVIEW_RECENT_DESTINATIONS_TITLE);
290 source->AddLocalizedString("localDestinationsTitle",
291 IDS_PRINT_PREVIEW_LOCAL_DESTINATIONS_TITLE);
292 source->AddLocalizedString("cloudDestinationsTitle",
293 IDS_PRINT_PREVIEW_CLOUD_DESTINATIONS_TITLE);
294 source->AddLocalizedString("manage", IDS_PRINT_PREVIEW_MANAGE);
295 source->AddLocalizedString("setupCloudPrinters",
296 IDS_PRINT_PREVIEW_SETUP_CLOUD_PRINTERS);
297 source->AddLocalizedString("changeDestination",
298 IDS_PRINT_PREVIEW_CHANGE_DESTINATION);
299 source->AddLocalizedString("offlineForYear",
300 IDS_PRINT_PREVIEW_OFFLINE_FOR_YEAR);
301 source->AddLocalizedString("offlineForMonth",
302 IDS_PRINT_PREVIEW_OFFLINE_FOR_MONTH);
303 source->AddLocalizedString("offlineForWeek",
304 IDS_PRINT_PREVIEW_OFFLINE_FOR_WEEK);
305 source->AddLocalizedString("offline", IDS_PRINT_PREVIEW_OFFLINE);
306 source->AddLocalizedString("fedexTos", IDS_PRINT_PREVIEW_FEDEX_TOS);
307 source->AddLocalizedString("tosCheckboxLabel",
308 IDS_PRINT_PREVIEW_TOS_CHECKBOX_LABEL);
309 source->AddLocalizedString("noDestsPromoTitle",
310 IDS_PRINT_PREVIEW_NO_DESTS_PROMO_TITLE);
311 source->AddLocalizedString("noDestsPromoBody",
312 IDS_PRINT_PREVIEW_NO_DESTS_PROMO_BODY);
313 source->AddLocalizedString("noDestsPromoGcpDesc",
314 IDS_PRINT_PREVIEW_NO_DESTS_GCP_DESC);
315 source->AddLocalizedString("learnMore",
317 source->AddLocalizedString(
318 "noDestsPromoAddPrinterButtonLabel",
319 IDS_PRINT_PREVIEW_NO_DESTS_PROMO_ADD_PRINTER_BUTTON_LABEL);
320 source->AddLocalizedString(
321 "noDestsPromoNotNowButtonLabel",
322 IDS_PRINT_PREVIEW_NO_DESTS_PROMO_NOT_NOW_BUTTON_LABEL);
324 source->SetJsonPath("strings.js");
325 source->AddResourcePath("print_preview.js", IDR_PRINT_PREVIEW_JS);
326 source->AddResourcePath("images/printer.png",
327 IDR_PRINT_PREVIEW_IMAGES_PRINTER);
328 source->AddResourcePath("images/printer_shared.png",
329 IDR_PRINT_PREVIEW_IMAGES_PRINTER_SHARED);
330 source->AddResourcePath("images/third_party.png",
331 IDR_PRINT_PREVIEW_IMAGES_THIRD_PARTY);
332 source->AddResourcePath("images/third_party_fedex.png",
333 IDR_PRINT_PREVIEW_IMAGES_THIRD_PARTY_FEDEX);
334 source->AddResourcePath("images/google_doc.png",
335 IDR_PRINT_PREVIEW_IMAGES_GOOGLE_DOC);
336 source->AddResourcePath("images/pdf.png", IDR_PRINT_PREVIEW_IMAGES_PDF);
337 source->AddResourcePath("images/mobile.png", IDR_PRINT_PREVIEW_IMAGES_MOBILE);
338 source->AddResourcePath("images/mobile_shared.png",
339 IDR_PRINT_PREVIEW_IMAGES_MOBILE_SHARED);
340 source->SetDefaultResource(IDR_PRINT_PREVIEW_HTML);
341 source->SetRequestFilter(base::Bind(&HandleRequestCallback));
342 source->OverrideContentSecurityPolicyObjectSrc("object-src 'self';");
346 PrintPreviewUI::TestingDelegate* g_testing_delegate = NULL;
350 PrintPreviewUI::PrintPreviewUI(content::WebUI* web_ui)
351 : ConstrainedWebDialogUI(web_ui),
352 initial_preview_start_time_(base::TimeTicks::Now()),
353 id_(g_print_preview_ui_id_map.Get().Add(this)),
355 source_is_modifiable_(true),
356 source_has_selection_(false),
357 dialog_closed_(false) {
358 // Set up the chrome://print/ data source.
359 Profile* profile = Profile::FromWebUI(web_ui);
360 content::WebUIDataSource::Add(profile, CreatePrintPreviewUISource());
362 // Set up the chrome://theme/ source.
363 content::URLDataSource::Add(profile, new ThemeSource(profile));
365 // WebUI owns |handler_|.
366 handler_ = new PrintPreviewHandler();
367 web_ui->AddMessageHandler(handler_);
369 g_print_preview_request_id_map.Get().Set(id_, -1);
372 PrintPreviewUI::~PrintPreviewUI() {
373 print_preview_data_service()->RemoveEntry(id_);
374 g_print_preview_request_id_map.Get().Erase(id_);
375 g_print_preview_ui_id_map.Get().Remove(id_);
378 void PrintPreviewUI::GetPrintPreviewDataForIndex(
380 scoped_refptr<base::RefCountedBytes>* data) {
381 print_preview_data_service()->GetDataEntry(id_, index, data);
384 void PrintPreviewUI::SetPrintPreviewDataForIndex(
386 const base::RefCountedBytes* data) {
387 print_preview_data_service()->SetDataEntry(id_, index, data);
390 void PrintPreviewUI::ClearAllPreviewData() {
391 print_preview_data_service()->RemoveEntry(id_);
394 int PrintPreviewUI::GetAvailableDraftPageCount() {
395 return print_preview_data_service()->GetAvailableDraftPageCount(id_);
398 void PrintPreviewUI::SetInitiatorTitle(
399 const string16& job_title) {
400 initiator_title_ = job_title;
404 void PrintPreviewUI::SetInitialParams(
405 content::WebContents* print_preview_dialog,
406 const PrintHostMsg_RequestPrintPreview_Params& params) {
407 if (!print_preview_dialog || !print_preview_dialog->GetWebUI())
409 PrintPreviewUI* print_preview_ui = static_cast<PrintPreviewUI*>(
410 print_preview_dialog->GetWebUI()->GetController());
411 print_preview_ui->source_is_modifiable_ = params.is_modifiable;
412 print_preview_ui->source_has_selection_ = params.has_selection;
413 print_preview_ui->print_selection_only_ = params.selection_only;
417 void PrintPreviewUI::GetCurrentPrintPreviewStatus(int32 preview_ui_id,
421 if (!g_print_preview_request_id_map.Get().Get(preview_ui_id, ¤t_id)) {
425 *cancel = (request_id != current_id);
428 int32 PrintPreviewUI::GetIDForPrintPreviewUI() const {
432 void PrintPreviewUI::OnPrintPreviewDialogClosed() {
433 WebContents* preview_dialog = web_ui()->GetWebContents();
434 printing::BackgroundPrintingManager* background_printing_manager =
435 g_browser_process->background_printing_manager();
436 if (background_printing_manager->HasPrintPreviewDialog(preview_dialog))
438 OnClosePrintPreviewDialog();
441 void PrintPreviewUI::OnInitiatorClosed() {
442 WebContents* preview_dialog = web_ui()->GetWebContents();
443 printing::BackgroundPrintingManager* background_printing_manager =
444 g_browser_process->background_printing_manager();
445 if (background_printing_manager->HasPrintPreviewDialog(preview_dialog))
446 web_ui()->CallJavascriptFunction("cancelPendingPrintRequest");
448 OnClosePrintPreviewDialog();
451 void PrintPreviewUI::OnPrintPreviewRequest(int request_id) {
452 g_print_preview_request_id_map.Get().Set(id_, request_id);
455 void PrintPreviewUI::OnShowSystemDialog() {
456 web_ui()->CallJavascriptFunction("onSystemDialogLinkClicked");
459 void PrintPreviewUI::OnDidGetPreviewPageCount(
460 const PrintHostMsg_DidGetPreviewPageCount_Params& params) {
461 DCHECK_GT(params.page_count, 0);
462 if (g_testing_delegate)
463 g_testing_delegate->DidGetPreviewPageCount(params.page_count);
464 base::FundamentalValue count(params.page_count);
465 base::FundamentalValue request_id(params.preview_request_id);
466 web_ui()->CallJavascriptFunction("onDidGetPreviewPageCount",
471 void PrintPreviewUI::OnDidGetDefaultPageLayout(
472 const PageSizeMargins& page_layout, const gfx::Rect& printable_area,
473 bool has_custom_page_size_style) {
474 if (page_layout.margin_top < 0 || page_layout.margin_left < 0 ||
475 page_layout.margin_bottom < 0 || page_layout.margin_right < 0 ||
476 page_layout.content_width < 0 || page_layout.content_height < 0 ||
477 printable_area.width() <= 0 || printable_area.height() <= 0) {
482 base::DictionaryValue layout;
483 layout.SetDouble(printing::kSettingMarginTop, page_layout.margin_top);
484 layout.SetDouble(printing::kSettingMarginLeft, page_layout.margin_left);
485 layout.SetDouble(printing::kSettingMarginBottom, page_layout.margin_bottom);
486 layout.SetDouble(printing::kSettingMarginRight, page_layout.margin_right);
487 layout.SetDouble(printing::kSettingContentWidth, page_layout.content_width);
488 layout.SetDouble(printing::kSettingContentHeight, page_layout.content_height);
489 layout.SetInteger(printing::kSettingPrintableAreaX, printable_area.x());
490 layout.SetInteger(printing::kSettingPrintableAreaY, printable_area.y());
491 layout.SetInteger(printing::kSettingPrintableAreaWidth,
492 printable_area.width());
493 layout.SetInteger(printing::kSettingPrintableAreaHeight,
494 printable_area.height());
496 base::FundamentalValue has_page_size_style(has_custom_page_size_style);
497 web_ui()->CallJavascriptFunction("onDidGetDefaultPageLayout", layout,
498 has_page_size_style);
501 void PrintPreviewUI::OnDidPreviewPage(int page_number,
502 int preview_request_id) {
503 DCHECK_GE(page_number, 0);
504 base::FundamentalValue number(page_number);
505 base::FundamentalValue ui_identifier(id_);
506 base::FundamentalValue request_id(preview_request_id);
507 if (g_testing_delegate)
508 g_testing_delegate->DidRenderPreviewPage(*web_ui()->GetWebContents());
509 web_ui()->CallJavascriptFunction(
510 "onDidPreviewPage", number, ui_identifier, request_id);
511 if (g_testing_delegate && g_testing_delegate->IsAutoCancelEnabled())
512 web_ui()->CallJavascriptFunction("autoCancelForTesting");
515 void PrintPreviewUI::OnReusePreviewData(int preview_request_id) {
516 base::FundamentalValue ui_identifier(id_);
517 base::FundamentalValue ui_preview_request_id(preview_request_id);
518 web_ui()->CallJavascriptFunction("reloadPreviewPages", ui_identifier,
519 ui_preview_request_id);
522 void PrintPreviewUI::OnPreviewDataIsAvailable(int expected_pages_count,
523 int preview_request_id) {
524 VLOG(1) << "Print preview request finished with "
525 << expected_pages_count << " pages";
527 if (!initial_preview_start_time_.is_null()) {
528 UMA_HISTOGRAM_TIMES("PrintPreview.InitialDisplayTime",
529 base::TimeTicks::Now() - initial_preview_start_time_);
530 UMA_HISTOGRAM_COUNTS("PrintPreview.PageCount.Initial",
531 expected_pages_count);
532 initial_preview_start_time_ = base::TimeTicks();
534 base::FundamentalValue ui_identifier(id_);
535 base::FundamentalValue ui_preview_request_id(preview_request_id);
536 web_ui()->CallJavascriptFunction("updatePrintPreview", ui_identifier,
537 ui_preview_request_id);
540 void PrintPreviewUI::OnPrintPreviewDialogDestroyed() {
541 handler_->OnPrintPreviewDialogDestroyed();
544 void PrintPreviewUI::OnFileSelectionCancelled() {
545 web_ui()->CallJavascriptFunction("fileSelectionCancelled");
548 void PrintPreviewUI::OnCancelPendingPreviewRequest() {
549 g_print_preview_request_id_map.Get().Set(id_, -1);
552 void PrintPreviewUI::OnPrintPreviewFailed() {
553 handler_->OnPrintPreviewFailed();
554 web_ui()->CallJavascriptFunction("printPreviewFailed");
557 void PrintPreviewUI::OnInvalidPrinterSettings() {
558 web_ui()->CallJavascriptFunction("invalidPrinterSettings");
561 PrintPreviewDataService* PrintPreviewUI::print_preview_data_service() {
562 return PrintPreviewDataService::GetInstance();
565 void PrintPreviewUI::OnHidePreviewDialog() {
566 WebContents* preview_dialog = web_ui()->GetWebContents();
567 printing::BackgroundPrintingManager* background_printing_manager =
568 g_browser_process->background_printing_manager();
569 if (background_printing_manager->HasPrintPreviewDialog(preview_dialog))
572 ConstrainedWebDialogDelegate* delegate = GetConstrainedDelegate();
575 delegate->ReleaseWebContentsOnDialogClose();
576 background_printing_manager->OwnPrintPreviewDialog(preview_dialog);
577 OnClosePrintPreviewDialog();
580 void PrintPreviewUI::OnClosePrintPreviewDialog() {
583 dialog_closed_ = true;
584 ConstrainedWebDialogDelegate* delegate = GetConstrainedDelegate();
587 delegate->GetWebDialogDelegate()->OnDialogClosed(std::string());
588 delegate->OnDialogCloseFromWebUI();
591 void PrintPreviewUI::OnReloadPrintersList() {
592 web_ui()->CallJavascriptFunction("reloadPrintersList");
595 void PrintPreviewUI::OnPrintPreviewScalingDisabled() {
596 web_ui()->CallJavascriptFunction("printScalingDisabledForSourcePDF");
600 void PrintPreviewUI::SetDelegateForTesting(TestingDelegate* delegate) {
601 g_testing_delegate = delegate;