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"
10 #include "base/id_map.h"
11 #include "base/lazy_instance.h"
12 #include "base/memory/ref_counted_memory.h"
13 #include "base/metrics/histogram.h"
14 #include "base/strings/string_number_conversions.h"
15 #include "base/strings/string_split.h"
16 #include "base/strings/string_util.h"
17 #include "base/strings/utf_string_conversions.h"
18 #include "base/synchronization/lock.h"
19 #include "base/values.h"
20 #include "chrome/browser/browser_process.h"
21 #include "chrome/browser/printing/background_printing_manager.h"
22 #include "chrome/browser/printing/print_preview_data_service.h"
23 #include "chrome/browser/profiles/profile.h"
24 #include "chrome/browser/ui/webui/metrics_handler.h"
25 #include "chrome/browser/ui/webui/print_preview/print_preview_handler.h"
26 #include "chrome/browser/ui/webui/theme_source.h"
27 #include "chrome/common/print_messages.h"
28 #include "chrome/common/url_constants.h"
29 #include "chrome/grit/chromium_strings.h"
30 #include "chrome/grit/generated_resources.h"
31 #include "chrome/grit/google_chrome_strings.h"
32 #include "content/public/browser/url_data_source.h"
33 #include "content/public/browser/web_contents.h"
34 #include "content/public/browser/web_ui_data_source.h"
35 #include "grit/browser_resources.h"
36 #include "grit/components_strings.h"
37 #include "printing/page_size_margins.h"
38 #include "printing/print_job_constants.h"
39 #include "ui/base/l10n/l10n_util.h"
40 #include "ui/gfx/rect.h"
41 #include "ui/web_dialogs/web_dialog_delegate.h"
42 #include "ui/web_dialogs/web_dialog_ui.h"
44 using content::WebContents;
45 using printing::PageSizeMargins;
49 #if defined(OS_MACOSX)
50 // U+0028 U+21E7 U+2318 U+0050 U+0029 in UTF8
51 const char kBasicPrintShortcut[] = "\x28\xE2\x8c\xA5\xE2\x8C\x98\x50\x29";
52 #elif defined(OS_WIN) || defined(OS_CHROMEOS)
53 const char kBasicPrintShortcut[] = "(Ctrl+Shift+P)";
55 const char kBasicPrintShortcut[] = "(Shift+Ctrl+P)";
58 // Thread-safe wrapper around a std::map to keep track of mappings from
59 // PrintPreviewUI IDs to most recent print preview request IDs.
60 class PrintPreviewRequestIdMapWithLock {
62 PrintPreviewRequestIdMapWithLock() {}
63 ~PrintPreviewRequestIdMapWithLock() {}
65 // Gets the value for |preview_id|.
66 // Returns true and sets |out_value| on success.
67 bool Get(int32 preview_id, int* out_value) {
68 base::AutoLock lock(lock_);
69 PrintPreviewRequestIdMap::const_iterator it = map_.find(preview_id);
72 *out_value = it->second;
76 // Sets the |value| for |preview_id|.
77 void Set(int32 preview_id, int value) {
78 base::AutoLock lock(lock_);
79 map_[preview_id] = value;
82 // Erases the entry for |preview_id|.
83 void Erase(int32 preview_id) {
84 base::AutoLock lock(lock_);
85 map_.erase(preview_id);
89 // Mapping from PrintPreviewUI ID to print preview request ID.
90 typedef std::map<int, int> PrintPreviewRequestIdMap;
92 PrintPreviewRequestIdMap map_;
95 DISALLOW_COPY_AND_ASSIGN(PrintPreviewRequestIdMapWithLock);
98 // Written to on the UI thread, read from any thread.
99 base::LazyInstance<PrintPreviewRequestIdMapWithLock>
100 g_print_preview_request_id_map = LAZY_INSTANCE_INITIALIZER;
102 // PrintPreviewUI IDMap used to avoid exposing raw pointer addresses to WebUI.
103 // Only accessed on the UI thread.
104 base::LazyInstance<IDMap<PrintPreviewUI> >
105 g_print_preview_ui_id_map = LAZY_INSTANCE_INITIALIZER;
107 // PrintPreviewUI serves data for chrome://print requests.
109 // The format for requesting PDF data is as follows:
110 // chrome://print/<PrintPreviewUIID>/<PageIndex>/print.pdf
112 // Parameters (< > required):
113 // <PrintPreviewUIID> = PrintPreview UI ID
114 // <PageIndex> = Page index is zero-based or
115 // |printing::COMPLETE_PREVIEW_DOCUMENT_INDEX| to represent
116 // a print ready PDF.
119 // chrome://print/123/10/print.pdf
121 // Requests to chrome://print with paths not ending in /print.pdf are used
122 // to return the markup or other resources for the print preview page itself.
123 bool HandleRequestCallback(
124 const std::string& path,
125 const content::WebUIDataSource::GotDataCallback& callback) {
126 // ChromeWebUIDataSource handles most requests except for the print preview
128 if (!EndsWith(path, "/print.pdf", true))
131 // Print Preview data.
132 scoped_refptr<base::RefCountedBytes> data;
133 std::vector<std::string> url_substr;
134 base::SplitString(path, '/', &url_substr);
135 int preview_ui_id = -1;
137 if (url_substr.size() == 3 &&
138 base::StringToInt(url_substr[0], &preview_ui_id),
139 base::StringToInt(url_substr[1], &page_index) &&
140 preview_ui_id >= 0) {
141 PrintPreviewDataService::GetInstance()->GetDataEntry(
142 preview_ui_id, page_index, &data);
145 callback.Run(data.get());
149 scoped_refptr<base::RefCountedBytes> empty_bytes(new base::RefCountedBytes);
150 callback.Run(empty_bytes.get());
154 content::WebUIDataSource* CreatePrintPreviewUISource() {
155 content::WebUIDataSource* source =
156 content::WebUIDataSource::Create(chrome::kChromeUIPrintHost);
157 #if defined(OS_CHROMEOS)
158 source->AddLocalizedString("title",
159 IDS_PRINT_PREVIEW_GOOGLE_CLOUD_PRINT_TITLE);
161 source->AddLocalizedString("title", IDS_PRINT_PREVIEW_TITLE);
163 source->AddLocalizedString("loading", IDS_PRINT_PREVIEW_LOADING);
164 source->AddLocalizedString("noPlugin", IDS_PRINT_PREVIEW_NO_PLUGIN);
165 source->AddLocalizedString("launchNativeDialog",
166 IDS_PRINT_PREVIEW_NATIVE_DIALOG);
167 source->AddLocalizedString("previewFailed", IDS_PRINT_PREVIEW_FAILED);
168 source->AddLocalizedString("invalidPrinterSettings",
169 IDS_PRINT_INVALID_PRINTER_SETTINGS);
170 source->AddLocalizedString("printButton", IDS_PRINT_PREVIEW_PRINT_BUTTON);
171 source->AddLocalizedString("saveButton", IDS_PRINT_PREVIEW_SAVE_BUTTON);
172 source->AddLocalizedString("printing", IDS_PRINT_PREVIEW_PRINTING);
173 source->AddLocalizedString("printingToPDFInProgress",
174 IDS_PRINT_PREVIEW_PRINTING_TO_PDF_IN_PROGRESS);
175 #if defined(OS_MACOSX)
176 source->AddLocalizedString("openingPDFInPreview",
177 IDS_PRINT_PREVIEW_OPENING_PDF_IN_PREVIEW);
179 source->AddLocalizedString("destinationLabel",
180 IDS_PRINT_PREVIEW_DESTINATION_LABEL);
181 source->AddLocalizedString("copiesLabel", IDS_PRINT_PREVIEW_COPIES_LABEL);
182 source->AddLocalizedString("examplePageRangeText",
183 IDS_PRINT_PREVIEW_EXAMPLE_PAGE_RANGE_TEXT);
184 source->AddLocalizedString("layoutLabel", IDS_PRINT_PREVIEW_LAYOUT_LABEL);
185 source->AddLocalizedString("optionAllPages",
186 IDS_PRINT_PREVIEW_OPTION_ALL_PAGES);
187 source->AddLocalizedString("optionBw", IDS_PRINT_PREVIEW_OPTION_BW);
188 source->AddLocalizedString("optionCollate", IDS_PRINT_PREVIEW_OPTION_COLLATE);
189 source->AddLocalizedString("optionColor", IDS_PRINT_PREVIEW_OPTION_COLOR);
190 source->AddLocalizedString("optionLandscape",
191 IDS_PRINT_PREVIEW_OPTION_LANDSCAPE);
192 source->AddLocalizedString("optionPortrait",
193 IDS_PRINT_PREVIEW_OPTION_PORTRAIT);
194 source->AddLocalizedString("optionTwoSided",
195 IDS_PRINT_PREVIEW_OPTION_TWO_SIDED);
196 source->AddLocalizedString("pagesLabel", IDS_PRINT_PREVIEW_PAGES_LABEL);
197 source->AddLocalizedString("pageRangeTextBox",
198 IDS_PRINT_PREVIEW_PAGE_RANGE_TEXT);
199 source->AddLocalizedString("pageRangeRadio",
200 IDS_PRINT_PREVIEW_PAGE_RANGE_RADIO);
201 source->AddLocalizedString("printToPDF", IDS_PRINT_PREVIEW_PRINT_TO_PDF);
202 source->AddLocalizedString("printPreviewSummaryFormatShort",
203 IDS_PRINT_PREVIEW_SUMMARY_FORMAT_SHORT);
204 source->AddLocalizedString("printPreviewSummaryFormatLong",
205 IDS_PRINT_PREVIEW_SUMMARY_FORMAT_LONG);
206 source->AddLocalizedString("printPreviewSheetsLabelSingular",
207 IDS_PRINT_PREVIEW_SHEETS_LABEL_SINGULAR);
208 source->AddLocalizedString("printPreviewSheetsLabelPlural",
209 IDS_PRINT_PREVIEW_SHEETS_LABEL_PLURAL);
210 source->AddLocalizedString("printPreviewPageLabelSingular",
211 IDS_PRINT_PREVIEW_PAGE_LABEL_SINGULAR);
212 source->AddLocalizedString("printPreviewPageLabelPlural",
213 IDS_PRINT_PREVIEW_PAGE_LABEL_PLURAL);
214 const base::string16 shortcut_text(base::UTF8ToUTF16(kBasicPrintShortcut));
215 #if !defined(OS_CHROMEOS)
217 "systemDialogOption",
218 l10n_util::GetStringFUTF16(
219 IDS_PRINT_PREVIEW_SYSTEM_DIALOG_OPTION,
222 #if defined(OS_MACOSX)
223 source->AddLocalizedString("openPdfInPreviewOption",
224 IDS_PRINT_PREVIEW_OPEN_PDF_IN_PREVIEW_APP);
227 "printWithCloudPrintWait",
228 l10n_util::GetStringFUTF16(
229 IDS_PRINT_PREVIEW_PRINT_WITH_CLOUD_PRINT_WAIT,
230 l10n_util::GetStringUTF16(IDS_GOOGLE_CLOUD_PRINT)));
232 "noDestsPromoLearnMoreUrl",
233 chrome::kCloudPrintNoDestinationsLearnMoreURL);
234 source->AddLocalizedString("pageRangeInstruction",
235 IDS_PRINT_PREVIEW_PAGE_RANGE_INSTRUCTION);
236 source->AddLocalizedString("copiesInstruction",
237 IDS_PRINT_PREVIEW_COPIES_INSTRUCTION);
238 source->AddLocalizedString("incrementTitle",
239 IDS_PRINT_PREVIEW_INCREMENT_TITLE);
240 source->AddLocalizedString("decrementTitle",
241 IDS_PRINT_PREVIEW_DECREMENT_TITLE);
242 source->AddLocalizedString("printPagesLabel",
243 IDS_PRINT_PREVIEW_PRINT_PAGES_LABEL);
244 source->AddLocalizedString("optionsLabel", IDS_PRINT_PREVIEW_OPTIONS_LABEL);
245 source->AddLocalizedString("optionHeaderFooter",
246 IDS_PRINT_PREVIEW_OPTION_HEADER_FOOTER);
247 source->AddLocalizedString("optionFitToPage",
248 IDS_PRINT_PREVIEW_OPTION_FIT_TO_PAGE);
249 source->AddLocalizedString(
250 "optionBackgroundColorsAndImages",
251 IDS_PRINT_PREVIEW_OPTION_BACKGROUND_COLORS_AND_IMAGES);
252 source->AddLocalizedString("optionSelectionOnly",
253 IDS_PRINT_PREVIEW_OPTION_SELECTION_ONLY);
254 source->AddLocalizedString("marginsLabel", IDS_PRINT_PREVIEW_MARGINS_LABEL);
255 source->AddLocalizedString("defaultMargins",
256 IDS_PRINT_PREVIEW_DEFAULT_MARGINS);
257 source->AddLocalizedString("noMargins", IDS_PRINT_PREVIEW_NO_MARGINS);
258 source->AddLocalizedString("customMargins", IDS_PRINT_PREVIEW_CUSTOM_MARGINS);
259 source->AddLocalizedString("minimumMargins",
260 IDS_PRINT_PREVIEW_MINIMUM_MARGINS);
261 source->AddLocalizedString("top", IDS_PRINT_PREVIEW_TOP_MARGIN_LABEL);
262 source->AddLocalizedString("bottom", IDS_PRINT_PREVIEW_BOTTOM_MARGIN_LABEL);
263 source->AddLocalizedString("left", IDS_PRINT_PREVIEW_LEFT_MARGIN_LABEL);
264 source->AddLocalizedString("right", IDS_PRINT_PREVIEW_RIGHT_MARGIN_LABEL);
265 source->AddLocalizedString("mediaSizeLabel",
266 IDS_PRINT_PREVIEW_MEDIA_SIZE_LABEL);
267 source->AddLocalizedString("dpiLabel", IDS_PRINT_PREVIEW_DPI_LABEL);
268 source->AddLocalizedString("dpiItemLabel", IDS_PRINT_PREVIEW_DPI_ITEM_LABEL);
269 source->AddLocalizedString("nonIsotropicDpiItemLabel",
270 IDS_PRINT_PREVIEW_NON_ISOTROPIC_DPI_ITEM_LABEL);
271 source->AddLocalizedString("destinationSearchTitle",
272 IDS_PRINT_PREVIEW_DESTINATION_SEARCH_TITLE);
273 source->AddLocalizedString("accountSelectTitle",
274 IDS_PRINT_PREVIEW_ACCOUNT_SELECT_TITLE);
275 source->AddLocalizedString("addAccountTitle",
276 IDS_PRINT_PREVIEW_ADD_ACCOUNT_TITLE);
277 source->AddLocalizedString("cloudPrintPromotion",
278 IDS_PRINT_PREVIEW_CLOUD_PRINT_PROMOTION);
279 source->AddLocalizedString("searchBoxPlaceholder",
280 IDS_PRINT_PREVIEW_SEARCH_BOX_PLACEHOLDER);
281 source->AddLocalizedString("noDestinationsMessage",
282 IDS_PRINT_PREVIEW_NO_DESTINATIONS_MESSAGE);
283 source->AddLocalizedString("showAllButtonText",
284 IDS_PRINT_PREVIEW_SHOW_ALL_BUTTON_TEXT);
285 source->AddLocalizedString("destinationCount",
286 IDS_PRINT_PREVIEW_DESTINATION_COUNT);
287 source->AddLocalizedString("recentDestinationsTitle",
288 IDS_PRINT_PREVIEW_RECENT_DESTINATIONS_TITLE);
289 source->AddLocalizedString("localDestinationsTitle",
290 IDS_PRINT_PREVIEW_LOCAL_DESTINATIONS_TITLE);
291 source->AddLocalizedString("cloudDestinationsTitle",
292 IDS_PRINT_PREVIEW_CLOUD_DESTINATIONS_TITLE);
293 source->AddLocalizedString("manage", IDS_PRINT_PREVIEW_MANAGE);
294 source->AddLocalizedString("setupCloudPrinters",
295 IDS_PRINT_PREVIEW_SETUP_CLOUD_PRINTERS);
296 source->AddLocalizedString("changeDestination",
297 IDS_PRINT_PREVIEW_CHANGE_DESTINATION);
298 source->AddLocalizedString("offlineForYear",
299 IDS_PRINT_PREVIEW_OFFLINE_FOR_YEAR);
300 source->AddLocalizedString("offlineForMonth",
301 IDS_PRINT_PREVIEW_OFFLINE_FOR_MONTH);
302 source->AddLocalizedString("offlineForWeek",
303 IDS_PRINT_PREVIEW_OFFLINE_FOR_WEEK);
304 source->AddLocalizedString("offline", IDS_PRINT_PREVIEW_OFFLINE);
305 source->AddLocalizedString("fedexTos", IDS_PRINT_PREVIEW_FEDEX_TOS);
306 source->AddLocalizedString("tosCheckboxLabel",
307 IDS_PRINT_PREVIEW_TOS_CHECKBOX_LABEL);
308 source->AddLocalizedString("noDestsPromoTitle",
309 IDS_PRINT_PREVIEW_NO_DESTS_PROMO_TITLE);
310 source->AddLocalizedString("noDestsPromoBody",
311 IDS_PRINT_PREVIEW_NO_DESTS_PROMO_BODY);
312 source->AddLocalizedString("noDestsPromoGcpDesc",
313 IDS_PRINT_PREVIEW_NO_DESTS_GCP_DESC);
314 source->AddLocalizedString("learnMore",
316 source->AddLocalizedString(
317 "noDestsPromoAddPrinterButtonLabel",
318 IDS_PRINT_PREVIEW_NO_DESTS_PROMO_ADD_PRINTER_BUTTON_LABEL);
319 source->AddLocalizedString(
320 "noDestsPromoNotNowButtonLabel",
321 IDS_PRINT_PREVIEW_NO_DESTS_PROMO_NOT_NOW_BUTTON_LABEL);
322 source->AddLocalizedString("couldNotPrint",
323 IDS_PRINT_PREVIEW_COULD_NOT_PRINT);
324 source->AddLocalizedString("registerPromoButtonText",
325 IDS_PRINT_PREVIEW_REGISTER_PROMO_BUTTON_TEXT);
326 source->AddLocalizedString(
327 "advancedSettingsSearchBoxPlaceholder",
328 IDS_PRINT_PREVIEW_ADVANCED_SETTINGS_SEARCH_BOX_PLACEHOLDER);
329 source->AddLocalizedString("advancedSettingsDialogTitle",
330 IDS_PRINT_PREVIEW_ADVANCED_SETTINGS_DIALOG_TITLE);
331 source->AddLocalizedString(
332 "noAdvancedSettingsMatchSearchHint",
333 IDS_PRINT_PREVIEW_NO_ADVANCED_SETTINGS_MATCH_SEARCH_HINT);
334 source->AddLocalizedString(
335 "advancedSettingsDialogConfirm",
336 IDS_PRINT_PREVIEW_ADVANCED_SETTINGS_DIALOG_CONFIRM);
337 source->AddLocalizedString("cancel", IDS_CANCEL);
338 source->AddLocalizedString("advancedOptionsLabel",
339 IDS_PRINT_PREVIEW_ADVANCED_OPTIONS_LABEL);
340 source->AddLocalizedString("showAdvancedOptions",
341 IDS_PRINT_PREVIEW_SHOW_ADVANCED_OPTIONS);
343 source->AddLocalizedString("accept", IDS_PRINT_PREVIEW_ACCEPT_INVITE);
344 source->AddLocalizedString(
345 "acceptForGroup", IDS_PRINT_PREVIEW_ACCEPT_GROUP_INVITE);
346 source->AddLocalizedString("reject", IDS_PRINT_PREVIEW_REJECT_INVITE);
347 source->AddLocalizedString(
348 "groupPrinterSharingInviteText", IDS_PRINT_PREVIEW_GROUP_INVITE_TEXT);
349 source->AddLocalizedString(
350 "printerSharingInviteText", IDS_PRINT_PREVIEW_INVITE_TEXT);
352 source->SetJsonPath("strings.js");
353 source->AddResourcePath("print_preview.js", IDR_PRINT_PREVIEW_JS);
354 source->AddResourcePath("images/printer.png",
355 IDR_PRINT_PREVIEW_IMAGES_PRINTER);
356 source->AddResourcePath("images/printer_shared.png",
357 IDR_PRINT_PREVIEW_IMAGES_PRINTER_SHARED);
358 source->AddResourcePath("images/third_party.png",
359 IDR_PRINT_PREVIEW_IMAGES_THIRD_PARTY);
360 source->AddResourcePath("images/third_party_fedex.png",
361 IDR_PRINT_PREVIEW_IMAGES_THIRD_PARTY_FEDEX);
362 source->AddResourcePath("images/google_doc.png",
363 IDR_PRINT_PREVIEW_IMAGES_GOOGLE_DOC);
364 source->AddResourcePath("images/pdf.png", IDR_PRINT_PREVIEW_IMAGES_PDF);
365 source->AddResourcePath("images/mobile.png", IDR_PRINT_PREVIEW_IMAGES_MOBILE);
366 source->AddResourcePath("images/mobile_shared.png",
367 IDR_PRINT_PREVIEW_IMAGES_MOBILE_SHARED);
368 source->SetDefaultResource(IDR_PRINT_PREVIEW_HTML);
369 source->SetRequestFilter(base::Bind(&HandleRequestCallback));
370 source->OverrideContentSecurityPolicyObjectSrc("object-src 'self';");
371 source->AddLocalizedString("moreOptionsLabel", IDS_MORE_OPTIONS_LABEL);
372 source->AddLocalizedString("lessOptionsLabel", IDS_LESS_OPTIONS_LABEL);
376 PrintPreviewUI::TestingDelegate* g_testing_delegate = NULL;
380 PrintPreviewUI::PrintPreviewUI(content::WebUI* web_ui)
381 : ConstrainedWebDialogUI(web_ui),
382 initial_preview_start_time_(base::TimeTicks::Now()),
383 id_(g_print_preview_ui_id_map.Get().Add(this)),
385 source_is_modifiable_(true),
386 source_has_selection_(false),
387 dialog_closed_(false) {
388 // Set up the chrome://print/ data source.
389 Profile* profile = Profile::FromWebUI(web_ui);
390 content::WebUIDataSource::Add(profile, CreatePrintPreviewUISource());
392 // Set up the chrome://theme/ source.
393 content::URLDataSource::Add(profile, new ThemeSource(profile));
395 // WebUI owns |handler_|.
396 handler_ = new PrintPreviewHandler();
397 web_ui->AddMessageHandler(handler_);
399 web_ui->AddMessageHandler(new MetricsHandler());
401 g_print_preview_request_id_map.Get().Set(id_, -1);
404 PrintPreviewUI::~PrintPreviewUI() {
405 print_preview_data_service()->RemoveEntry(id_);
406 g_print_preview_request_id_map.Get().Erase(id_);
407 g_print_preview_ui_id_map.Get().Remove(id_);
410 void PrintPreviewUI::GetPrintPreviewDataForIndex(
412 scoped_refptr<base::RefCountedBytes>* data) {
413 print_preview_data_service()->GetDataEntry(id_, index, data);
416 void PrintPreviewUI::SetPrintPreviewDataForIndex(
418 const base::RefCountedBytes* data) {
419 print_preview_data_service()->SetDataEntry(id_, index, data);
422 void PrintPreviewUI::ClearAllPreviewData() {
423 print_preview_data_service()->RemoveEntry(id_);
426 int PrintPreviewUI::GetAvailableDraftPageCount() {
427 return print_preview_data_service()->GetAvailableDraftPageCount(id_);
430 void PrintPreviewUI::SetInitiatorTitle(
431 const base::string16& job_title) {
432 initiator_title_ = job_title;
436 void PrintPreviewUI::SetInitialParams(
437 content::WebContents* print_preview_dialog,
438 const PrintHostMsg_RequestPrintPreview_Params& params) {
439 if (!print_preview_dialog || !print_preview_dialog->GetWebUI())
441 PrintPreviewUI* print_preview_ui = static_cast<PrintPreviewUI*>(
442 print_preview_dialog->GetWebUI()->GetController());
443 print_preview_ui->source_is_modifiable_ = params.is_modifiable;
444 print_preview_ui->source_has_selection_ = params.has_selection;
445 print_preview_ui->print_selection_only_ = params.selection_only;
449 void PrintPreviewUI::GetCurrentPrintPreviewStatus(int32 preview_ui_id,
453 if (!g_print_preview_request_id_map.Get().Get(preview_ui_id, ¤t_id)) {
457 *cancel = (request_id != current_id);
460 int32 PrintPreviewUI::GetIDForPrintPreviewUI() const {
464 void PrintPreviewUI::OnPrintPreviewDialogClosed() {
465 WebContents* preview_dialog = web_ui()->GetWebContents();
466 printing::BackgroundPrintingManager* background_printing_manager =
467 g_browser_process->background_printing_manager();
468 if (background_printing_manager->HasPrintPreviewDialog(preview_dialog))
470 OnClosePrintPreviewDialog();
473 void PrintPreviewUI::OnInitiatorClosed() {
474 WebContents* preview_dialog = web_ui()->GetWebContents();
475 printing::BackgroundPrintingManager* background_printing_manager =
476 g_browser_process->background_printing_manager();
477 if (background_printing_manager->HasPrintPreviewDialog(preview_dialog))
478 web_ui()->CallJavascriptFunction("cancelPendingPrintRequest");
480 OnClosePrintPreviewDialog();
483 void PrintPreviewUI::OnPrintPreviewRequest(int request_id) {
484 if (!initial_preview_start_time_.is_null()) {
485 UMA_HISTOGRAM_TIMES("PrintPreview.InitializationTime",
486 base::TimeTicks::Now() - initial_preview_start_time_);
488 g_print_preview_request_id_map.Get().Set(id_, request_id);
491 #if defined(ENABLE_BASIC_PRINTING)
492 void PrintPreviewUI::OnShowSystemDialog() {
493 web_ui()->CallJavascriptFunction("onSystemDialogLinkClicked");
495 #endif // ENABLE_BASIC_PRINTING
497 void PrintPreviewUI::OnDidGetPreviewPageCount(
498 const PrintHostMsg_DidGetPreviewPageCount_Params& params) {
499 DCHECK_GT(params.page_count, 0);
500 if (g_testing_delegate)
501 g_testing_delegate->DidGetPreviewPageCount(params.page_count);
502 base::FundamentalValue count(params.page_count);
503 base::FundamentalValue request_id(params.preview_request_id);
504 web_ui()->CallJavascriptFunction("onDidGetPreviewPageCount",
509 void PrintPreviewUI::OnDidGetDefaultPageLayout(
510 const PageSizeMargins& page_layout, const gfx::Rect& printable_area,
511 bool has_custom_page_size_style) {
512 if (page_layout.margin_top < 0 || page_layout.margin_left < 0 ||
513 page_layout.margin_bottom < 0 || page_layout.margin_right < 0 ||
514 page_layout.content_width < 0 || page_layout.content_height < 0 ||
515 printable_area.width() <= 0 || printable_area.height() <= 0) {
520 base::DictionaryValue layout;
521 layout.SetDouble(printing::kSettingMarginTop, page_layout.margin_top);
522 layout.SetDouble(printing::kSettingMarginLeft, page_layout.margin_left);
523 layout.SetDouble(printing::kSettingMarginBottom, page_layout.margin_bottom);
524 layout.SetDouble(printing::kSettingMarginRight, page_layout.margin_right);
525 layout.SetDouble(printing::kSettingContentWidth, page_layout.content_width);
526 layout.SetDouble(printing::kSettingContentHeight, page_layout.content_height);
527 layout.SetInteger(printing::kSettingPrintableAreaX, printable_area.x());
528 layout.SetInteger(printing::kSettingPrintableAreaY, printable_area.y());
529 layout.SetInteger(printing::kSettingPrintableAreaWidth,
530 printable_area.width());
531 layout.SetInteger(printing::kSettingPrintableAreaHeight,
532 printable_area.height());
534 base::FundamentalValue has_page_size_style(has_custom_page_size_style);
535 web_ui()->CallJavascriptFunction("onDidGetDefaultPageLayout", layout,
536 has_page_size_style);
539 void PrintPreviewUI::OnDidPreviewPage(int page_number,
540 int preview_request_id) {
541 DCHECK_GE(page_number, 0);
542 base::FundamentalValue number(page_number);
543 base::FundamentalValue ui_identifier(id_);
544 base::FundamentalValue request_id(preview_request_id);
545 if (g_testing_delegate)
546 g_testing_delegate->DidRenderPreviewPage(web_ui()->GetWebContents());
547 web_ui()->CallJavascriptFunction(
548 "onDidPreviewPage", number, ui_identifier, request_id);
549 if (g_testing_delegate && g_testing_delegate->IsAutoCancelEnabled())
550 web_ui()->CallJavascriptFunction("autoCancelForTesting");
553 void PrintPreviewUI::OnPreviewDataIsAvailable(int expected_pages_count,
554 int preview_request_id) {
555 VLOG(1) << "Print preview request finished with "
556 << expected_pages_count << " pages";
558 if (!initial_preview_start_time_.is_null()) {
559 UMA_HISTOGRAM_TIMES("PrintPreview.InitialDisplayTime",
560 base::TimeTicks::Now() - initial_preview_start_time_);
561 UMA_HISTOGRAM_COUNTS("PrintPreview.PageCount.Initial",
562 expected_pages_count);
563 UMA_HISTOGRAM_COUNTS(
564 "PrintPreview.RegeneratePreviewRequest.BeforeFirstData",
565 handler_->regenerate_preview_request_count());
566 initial_preview_start_time_ = base::TimeTicks();
568 base::FundamentalValue ui_identifier(id_);
569 base::FundamentalValue ui_preview_request_id(preview_request_id);
570 web_ui()->CallJavascriptFunction("updatePrintPreview", ui_identifier,
571 ui_preview_request_id);
574 void PrintPreviewUI::OnPrintPreviewDialogDestroyed() {
575 handler_->OnPrintPreviewDialogDestroyed();
578 void PrintPreviewUI::OnFileSelectionCancelled() {
579 web_ui()->CallJavascriptFunction("fileSelectionCancelled");
582 void PrintPreviewUI::OnCancelPendingPreviewRequest() {
583 g_print_preview_request_id_map.Get().Set(id_, -1);
586 void PrintPreviewUI::OnPrintPreviewFailed() {
587 handler_->OnPrintPreviewFailed();
588 web_ui()->CallJavascriptFunction("printPreviewFailed");
591 void PrintPreviewUI::OnInvalidPrinterSettings() {
592 web_ui()->CallJavascriptFunction("invalidPrinterSettings");
595 PrintPreviewDataService* PrintPreviewUI::print_preview_data_service() {
596 return PrintPreviewDataService::GetInstance();
599 void PrintPreviewUI::OnHidePreviewDialog() {
600 WebContents* preview_dialog = web_ui()->GetWebContents();
601 printing::BackgroundPrintingManager* background_printing_manager =
602 g_browser_process->background_printing_manager();
603 if (background_printing_manager->HasPrintPreviewDialog(preview_dialog))
606 ConstrainedWebDialogDelegate* delegate = GetConstrainedDelegate();
609 delegate->ReleaseWebContentsOnDialogClose();
610 background_printing_manager->OwnPrintPreviewDialog(preview_dialog);
611 OnClosePrintPreviewDialog();
614 void PrintPreviewUI::OnClosePrintPreviewDialog() {
617 dialog_closed_ = true;
618 ConstrainedWebDialogDelegate* delegate = GetConstrainedDelegate();
621 delegate->GetWebDialogDelegate()->OnDialogClosed(std::string());
622 delegate->OnDialogCloseFromWebUI();
625 void PrintPreviewUI::OnReloadPrintersList() {
626 web_ui()->CallJavascriptFunction("reloadPrintersList");
629 void PrintPreviewUI::OnSetOptionsFromDocument(
630 const PrintHostMsg_SetOptionsFromDocument_Params& params) {
631 // Notify WebUI that print scaling is disabled
632 if (params.is_scaling_disabled)
633 web_ui()->CallJavascriptFunction("printScalingDisabledForSourcePDF");
637 void PrintPreviewUI::SetDelegateForTesting(TestingDelegate* delegate) {
638 g_testing_delegate = delegate;
641 void PrintPreviewUI::SetSelectedFileForTesting(const base::FilePath& path) {
642 handler_->FileSelected(path, 0, NULL);
645 void PrintPreviewUI::SetPdfSavedClosureForTesting(
646 const base::Closure& closure) {
647 handler_->SetPdfSavedClosureForTesting(closure);