Add new certificateProvider extension API.
[chromium-blink-merge.git] / chrome / browser / ui / webui / print_preview / print_preview_ui.cc
blobf075900d928d4d352be1f609f46b4c271b9ec08c
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"
7 #include <map>
8 #include <vector>
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/url_constants.h"
28 #include "chrome/grit/chromium_strings.h"
29 #include "chrome/grit/generated_resources.h"
30 #include "components/printing/common/print_messages.h"
31 #include "content/public/browser/url_data_source.h"
32 #include "content/public/browser/web_contents.h"
33 #include "content/public/browser/web_ui_data_source.h"
34 #include "grit/browser_resources.h"
35 #include "grit/components_strings.h"
36 #include "printing/page_size_margins.h"
37 #include "printing/print_job_constants.h"
38 #include "ui/base/l10n/l10n_util.h"
39 #include "ui/gfx/geometry/rect.h"
40 #include "ui/web_dialogs/web_dialog_delegate.h"
41 #include "ui/web_dialogs/web_dialog_ui.h"
43 using content::WebContents;
44 using printing::PageSizeMargins;
46 namespace {
48 #if defined(OS_MACOSX)
49 // U+0028 U+21E7 U+2318 U+0050 U+0029 in UTF8
50 const char kBasicPrintShortcut[] = "\x28\xE2\x8c\xA5\xE2\x8C\x98\x50\x29";
51 #elif defined(OS_WIN) || defined(OS_CHROMEOS)
52 const char kBasicPrintShortcut[] = "(Ctrl+Shift+P)";
53 #else
54 const char kBasicPrintShortcut[] = "(Shift+Ctrl+P)";
55 #endif
57 // Thread-safe wrapper around a std::map to keep track of mappings from
58 // PrintPreviewUI IDs to most recent print preview request IDs.
59 class PrintPreviewRequestIdMapWithLock {
60 public:
61 PrintPreviewRequestIdMapWithLock() {}
62 ~PrintPreviewRequestIdMapWithLock() {}
64 // Gets the value for |preview_id|.
65 // Returns true and sets |out_value| on success.
66 bool Get(int32 preview_id, int* out_value) {
67 base::AutoLock lock(lock_);
68 PrintPreviewRequestIdMap::const_iterator it = map_.find(preview_id);
69 if (it == map_.end())
70 return false;
71 *out_value = it->second;
72 return true;
75 // Sets the |value| for |preview_id|.
76 void Set(int32 preview_id, int value) {
77 base::AutoLock lock(lock_);
78 map_[preview_id] = value;
81 // Erases the entry for |preview_id|.
82 void Erase(int32 preview_id) {
83 base::AutoLock lock(lock_);
84 map_.erase(preview_id);
87 private:
88 // Mapping from PrintPreviewUI ID to print preview request ID.
89 typedef std::map<int, int> PrintPreviewRequestIdMap;
91 PrintPreviewRequestIdMap map_;
92 base::Lock lock_;
94 DISALLOW_COPY_AND_ASSIGN(PrintPreviewRequestIdMapWithLock);
97 // Written to on the UI thread, read from any thread.
98 base::LazyInstance<PrintPreviewRequestIdMapWithLock>
99 g_print_preview_request_id_map = LAZY_INSTANCE_INITIALIZER;
101 // PrintPreviewUI IDMap used to avoid exposing raw pointer addresses to WebUI.
102 // Only accessed on the UI thread.
103 base::LazyInstance<IDMap<PrintPreviewUI> >
104 g_print_preview_ui_id_map = LAZY_INSTANCE_INITIALIZER;
106 // PrintPreviewUI serves data for chrome://print requests.
108 // The format for requesting PDF data is as follows:
109 // chrome://print/<PrintPreviewUIID>/<PageIndex>/print.pdf
111 // Parameters (< > required):
112 // <PrintPreviewUIID> = PrintPreview UI ID
113 // <PageIndex> = Page index is zero-based or
114 // |printing::COMPLETE_PREVIEW_DOCUMENT_INDEX| to represent
115 // a print ready PDF.
117 // Example:
118 // chrome://print/123/10/print.pdf
120 // Requests to chrome://print with paths not ending in /print.pdf are used
121 // to return the markup or other resources for the print preview page itself.
122 bool HandleRequestCallback(
123 const std::string& path,
124 const content::WebUIDataSource::GotDataCallback& callback) {
125 // ChromeWebUIDataSource handles most requests except for the print preview
126 // data.
127 if (!base::EndsWith(path, "/print.pdf", base::CompareCase::SENSITIVE))
128 return false;
130 // Print Preview data.
131 scoped_refptr<base::RefCountedBytes> data;
132 std::vector<std::string> url_substr = base::SplitString(
133 path, "/", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
134 int preview_ui_id = -1;
135 int page_index = 0;
136 if (url_substr.size() == 3 &&
137 base::StringToInt(url_substr[0], &preview_ui_id),
138 base::StringToInt(url_substr[1], &page_index) &&
139 preview_ui_id >= 0) {
140 PrintPreviewDataService::GetInstance()->GetDataEntry(
141 preview_ui_id, page_index, &data);
143 if (data.get()) {
144 callback.Run(data.get());
145 return true;
147 // Invalid request.
148 scoped_refptr<base::RefCountedBytes> empty_bytes(new base::RefCountedBytes);
149 callback.Run(empty_bytes.get());
150 return true;
153 content::WebUIDataSource* CreatePrintPreviewUISource() {
154 content::WebUIDataSource* source =
155 content::WebUIDataSource::Create(chrome::kChromeUIPrintHost);
156 #if defined(OS_CHROMEOS)
157 source->AddLocalizedString("title",
158 IDS_PRINT_PREVIEW_GOOGLE_CLOUD_PRINT_TITLE);
159 #else
160 source->AddLocalizedString("title", IDS_PRINT_PREVIEW_TITLE);
161 #endif
162 source->AddLocalizedString("loading", IDS_PRINT_PREVIEW_LOADING);
163 source->AddLocalizedString("noPlugin", IDS_PRINT_PREVIEW_NO_PLUGIN);
164 source->AddLocalizedString("launchNativeDialog",
165 IDS_PRINT_PREVIEW_NATIVE_DIALOG);
166 source->AddLocalizedString("previewFailed", IDS_PRINT_PREVIEW_FAILED);
167 source->AddLocalizedString("invalidPrinterSettings",
168 IDS_PRINT_INVALID_PRINTER_SETTINGS);
169 source->AddLocalizedString("printButton", IDS_PRINT_PREVIEW_PRINT_BUTTON);
170 source->AddLocalizedString("saveButton", IDS_PRINT_PREVIEW_SAVE_BUTTON);
171 source->AddLocalizedString("printing", IDS_PRINT_PREVIEW_PRINTING);
172 source->AddLocalizedString("printingToPDFInProgress",
173 IDS_PRINT_PREVIEW_PRINTING_TO_PDF_IN_PROGRESS);
174 #if defined(OS_MACOSX)
175 source->AddLocalizedString("openingPDFInPreview",
176 IDS_PRINT_PREVIEW_OPENING_PDF_IN_PREVIEW);
177 #endif
178 source->AddLocalizedString("destinationLabel",
179 IDS_PRINT_PREVIEW_DESTINATION_LABEL);
180 source->AddLocalizedString("copiesLabel", IDS_PRINT_PREVIEW_COPIES_LABEL);
181 source->AddLocalizedString("examplePageRangeText",
182 IDS_PRINT_PREVIEW_EXAMPLE_PAGE_RANGE_TEXT);
183 source->AddLocalizedString("layoutLabel", IDS_PRINT_PREVIEW_LAYOUT_LABEL);
184 source->AddLocalizedString("optionAllPages",
185 IDS_PRINT_PREVIEW_OPTION_ALL_PAGES);
186 source->AddLocalizedString("optionBw", IDS_PRINT_PREVIEW_OPTION_BW);
187 source->AddLocalizedString("optionCollate", IDS_PRINT_PREVIEW_OPTION_COLLATE);
188 source->AddLocalizedString("optionColor", IDS_PRINT_PREVIEW_OPTION_COLOR);
189 source->AddLocalizedString("optionLandscape",
190 IDS_PRINT_PREVIEW_OPTION_LANDSCAPE);
191 source->AddLocalizedString("optionPortrait",
192 IDS_PRINT_PREVIEW_OPTION_PORTRAIT);
193 source->AddLocalizedString("optionTwoSided",
194 IDS_PRINT_PREVIEW_OPTION_TWO_SIDED);
195 source->AddLocalizedString("pagesLabel", IDS_PRINT_PREVIEW_PAGES_LABEL);
196 source->AddLocalizedString("pageRangeTextBox",
197 IDS_PRINT_PREVIEW_PAGE_RANGE_TEXT);
198 source->AddLocalizedString("pageRangeRadio",
199 IDS_PRINT_PREVIEW_PAGE_RANGE_RADIO);
200 source->AddLocalizedString("printToPDF", IDS_PRINT_PREVIEW_PRINT_TO_PDF);
201 source->AddLocalizedString("printPreviewSummaryFormatShort",
202 IDS_PRINT_PREVIEW_SUMMARY_FORMAT_SHORT);
203 source->AddLocalizedString("printPreviewSummaryFormatLong",
204 IDS_PRINT_PREVIEW_SUMMARY_FORMAT_LONG);
205 source->AddLocalizedString("printPreviewSheetsLabelSingular",
206 IDS_PRINT_PREVIEW_SHEETS_LABEL_SINGULAR);
207 source->AddLocalizedString("printPreviewSheetsLabelPlural",
208 IDS_PRINT_PREVIEW_SHEETS_LABEL_PLURAL);
209 source->AddLocalizedString("printPreviewPageLabelSingular",
210 IDS_PRINT_PREVIEW_PAGE_LABEL_SINGULAR);
211 source->AddLocalizedString("printPreviewPageLabelPlural",
212 IDS_PRINT_PREVIEW_PAGE_LABEL_PLURAL);
213 source->AddLocalizedString("selectButton",
214 IDS_PRINT_PREVIEW_BUTTON_SELECT);
215 source->AddLocalizedString("goBackButton",
216 IDS_PRINT_PREVIEW_BUTTON_GO_BACK);
217 source->AddLocalizedString(
218 "resolveExtensionUSBPermissionMessage",
219 IDS_PRINT_PREVIEW_RESOLVE_EXTENSION_USB_PERMISSION_MESSAGE);
220 source->AddLocalizedString(
221 "resolveExtensionUSBErrorMessage",
222 IDS_PRINT_PREVIEW_RESOLVE_EXTENSION_USB_ERROR_MESSAGE);
223 const base::string16 shortcut_text(base::UTF8ToUTF16(kBasicPrintShortcut));
224 #if !defined(OS_CHROMEOS)
225 source->AddString(
226 "systemDialogOption",
227 l10n_util::GetStringFUTF16(
228 IDS_PRINT_PREVIEW_SYSTEM_DIALOG_OPTION,
229 shortcut_text));
230 #endif
231 #if defined(OS_MACOSX)
232 source->AddLocalizedString("openPdfInPreviewOption",
233 IDS_PRINT_PREVIEW_OPEN_PDF_IN_PREVIEW_APP);
234 #endif
235 source->AddString(
236 "printWithCloudPrintWait",
237 l10n_util::GetStringFUTF16(
238 IDS_PRINT_PREVIEW_PRINT_WITH_CLOUD_PRINT_WAIT,
239 l10n_util::GetStringUTF16(IDS_GOOGLE_CLOUD_PRINT)));
240 source->AddString(
241 "noDestsPromoLearnMoreUrl",
242 chrome::kCloudPrintNoDestinationsLearnMoreURL);
243 source->AddLocalizedString("pageRangeInstruction",
244 IDS_PRINT_PREVIEW_PAGE_RANGE_INSTRUCTION);
245 source->AddLocalizedString("copiesInstruction",
246 IDS_PRINT_PREVIEW_COPIES_INSTRUCTION);
247 source->AddLocalizedString("incrementTitle",
248 IDS_PRINT_PREVIEW_INCREMENT_TITLE);
249 source->AddLocalizedString("decrementTitle",
250 IDS_PRINT_PREVIEW_DECREMENT_TITLE);
251 source->AddLocalizedString("printPagesLabel",
252 IDS_PRINT_PREVIEW_PRINT_PAGES_LABEL);
253 source->AddLocalizedString("optionsLabel", IDS_PRINT_PREVIEW_OPTIONS_LABEL);
254 source->AddLocalizedString("optionDistillPage",
255 IDS_PRINT_PREVIEW_OPTION_DISTILL_PAGE);
256 source->AddLocalizedString("optionHeaderFooter",
257 IDS_PRINT_PREVIEW_OPTION_HEADER_FOOTER);
258 source->AddLocalizedString("optionFitToPage",
259 IDS_PRINT_PREVIEW_OPTION_FIT_TO_PAGE);
260 source->AddLocalizedString(
261 "optionBackgroundColorsAndImages",
262 IDS_PRINT_PREVIEW_OPTION_BACKGROUND_COLORS_AND_IMAGES);
263 source->AddLocalizedString("optionSelectionOnly",
264 IDS_PRINT_PREVIEW_OPTION_SELECTION_ONLY);
265 source->AddLocalizedString("marginsLabel", IDS_PRINT_PREVIEW_MARGINS_LABEL);
266 source->AddLocalizedString("defaultMargins",
267 IDS_PRINT_PREVIEW_DEFAULT_MARGINS);
268 source->AddLocalizedString("noMargins", IDS_PRINT_PREVIEW_NO_MARGINS);
269 source->AddLocalizedString("customMargins", IDS_PRINT_PREVIEW_CUSTOM_MARGINS);
270 source->AddLocalizedString("minimumMargins",
271 IDS_PRINT_PREVIEW_MINIMUM_MARGINS);
272 source->AddLocalizedString("top", IDS_PRINT_PREVIEW_TOP_MARGIN_LABEL);
273 source->AddLocalizedString("bottom", IDS_PRINT_PREVIEW_BOTTOM_MARGIN_LABEL);
274 source->AddLocalizedString("left", IDS_PRINT_PREVIEW_LEFT_MARGIN_LABEL);
275 source->AddLocalizedString("right", IDS_PRINT_PREVIEW_RIGHT_MARGIN_LABEL);
276 source->AddLocalizedString("mediaSizeLabel",
277 IDS_PRINT_PREVIEW_MEDIA_SIZE_LABEL);
278 source->AddLocalizedString("dpiLabel", IDS_PRINT_PREVIEW_DPI_LABEL);
279 source->AddLocalizedString("dpiItemLabel", IDS_PRINT_PREVIEW_DPI_ITEM_LABEL);
280 source->AddLocalizedString("nonIsotropicDpiItemLabel",
281 IDS_PRINT_PREVIEW_NON_ISOTROPIC_DPI_ITEM_LABEL);
282 source->AddLocalizedString("destinationSearchTitle",
283 IDS_PRINT_PREVIEW_DESTINATION_SEARCH_TITLE);
284 source->AddLocalizedString("accountSelectTitle",
285 IDS_PRINT_PREVIEW_ACCOUNT_SELECT_TITLE);
286 source->AddLocalizedString("addAccountTitle",
287 IDS_PRINT_PREVIEW_ADD_ACCOUNT_TITLE);
288 source->AddLocalizedString("cloudPrintPromotion",
289 IDS_PRINT_PREVIEW_CLOUD_PRINT_PROMOTION);
290 source->AddLocalizedString("searchBoxPlaceholder",
291 IDS_PRINT_PREVIEW_SEARCH_BOX_PLACEHOLDER);
292 source->AddLocalizedString("noDestinationsMessage",
293 IDS_PRINT_PREVIEW_NO_DESTINATIONS_MESSAGE);
294 source->AddLocalizedString("showAllButtonText",
295 IDS_PRINT_PREVIEW_SHOW_ALL_BUTTON_TEXT);
296 source->AddLocalizedString("destinationCount",
297 IDS_PRINT_PREVIEW_DESTINATION_COUNT);
298 source->AddLocalizedString("recentDestinationsTitle",
299 IDS_PRINT_PREVIEW_RECENT_DESTINATIONS_TITLE);
300 source->AddLocalizedString("localDestinationsTitle",
301 IDS_PRINT_PREVIEW_LOCAL_DESTINATIONS_TITLE);
302 source->AddLocalizedString("cloudDestinationsTitle",
303 IDS_PRINT_PREVIEW_CLOUD_DESTINATIONS_TITLE);
304 source->AddLocalizedString("manage", IDS_PRINT_PREVIEW_MANAGE);
305 source->AddLocalizedString("setupCloudPrinters",
306 IDS_PRINT_PREVIEW_SETUP_CLOUD_PRINTERS);
307 source->AddLocalizedString("changeDestination",
308 IDS_PRINT_PREVIEW_CHANGE_DESTINATION);
309 source->AddLocalizedString("offlineForYear",
310 IDS_PRINT_PREVIEW_OFFLINE_FOR_YEAR);
311 source->AddLocalizedString("offlineForMonth",
312 IDS_PRINT_PREVIEW_OFFLINE_FOR_MONTH);
313 source->AddLocalizedString("offlineForWeek",
314 IDS_PRINT_PREVIEW_OFFLINE_FOR_WEEK);
315 source->AddLocalizedString("offline", IDS_PRINT_PREVIEW_OFFLINE);
316 source->AddLocalizedString("fedexTos", IDS_PRINT_PREVIEW_FEDEX_TOS);
317 source->AddLocalizedString("tosCheckboxLabel",
318 IDS_PRINT_PREVIEW_TOS_CHECKBOX_LABEL);
319 source->AddLocalizedString("noDestsPromoTitle",
320 IDS_PRINT_PREVIEW_NO_DESTS_PROMO_TITLE);
321 source->AddLocalizedString("noDestsPromoBody",
322 IDS_PRINT_PREVIEW_NO_DESTS_PROMO_BODY);
323 source->AddLocalizedString("noDestsPromoGcpDesc",
324 IDS_PRINT_PREVIEW_NO_DESTS_GCP_DESC);
325 source->AddLocalizedString("learnMore",
326 IDS_LEARN_MORE);
327 source->AddLocalizedString(
328 "noDestsPromoAddPrinterButtonLabel",
329 IDS_PRINT_PREVIEW_NO_DESTS_PROMO_ADD_PRINTER_BUTTON_LABEL);
330 source->AddLocalizedString(
331 "noDestsPromoNotNowButtonLabel",
332 IDS_PRINT_PREVIEW_NO_DESTS_PROMO_NOT_NOW_BUTTON_LABEL);
333 source->AddLocalizedString("couldNotPrint",
334 IDS_PRINT_PREVIEW_COULD_NOT_PRINT);
335 source->AddLocalizedString("registerPromoButtonText",
336 IDS_PRINT_PREVIEW_REGISTER_PROMO_BUTTON_TEXT);
337 source->AddLocalizedString(
338 "extensionDestinationIconTooltip",
339 IDS_PRINT_PREVIEW_EXTENSION_DESTINATION_ICON_TOOLTIP);
340 source->AddLocalizedString(
341 "advancedSettingsSearchBoxPlaceholder",
342 IDS_PRINT_PREVIEW_ADVANCED_SETTINGS_SEARCH_BOX_PLACEHOLDER);
343 source->AddLocalizedString("advancedSettingsDialogTitle",
344 IDS_PRINT_PREVIEW_ADVANCED_SETTINGS_DIALOG_TITLE);
345 source->AddLocalizedString(
346 "noAdvancedSettingsMatchSearchHint",
347 IDS_PRINT_PREVIEW_NO_ADVANCED_SETTINGS_MATCH_SEARCH_HINT);
348 source->AddLocalizedString(
349 "advancedSettingsDialogConfirm",
350 IDS_PRINT_PREVIEW_ADVANCED_SETTINGS_DIALOG_CONFIRM);
351 source->AddLocalizedString("cancel", IDS_CANCEL);
352 source->AddLocalizedString("advancedOptionsLabel",
353 IDS_PRINT_PREVIEW_ADVANCED_OPTIONS_LABEL);
354 source->AddLocalizedString("showAdvancedOptions",
355 IDS_PRINT_PREVIEW_SHOW_ADVANCED_OPTIONS);
357 source->AddLocalizedString("accept", IDS_PRINT_PREVIEW_ACCEPT_INVITE);
358 source->AddLocalizedString(
359 "acceptForGroup", IDS_PRINT_PREVIEW_ACCEPT_GROUP_INVITE);
360 source->AddLocalizedString("reject", IDS_PRINT_PREVIEW_REJECT_INVITE);
361 source->AddLocalizedString(
362 "groupPrinterSharingInviteText", IDS_PRINT_PREVIEW_GROUP_INVITE_TEXT);
363 source->AddLocalizedString(
364 "printerSharingInviteText", IDS_PRINT_PREVIEW_INVITE_TEXT);
366 source->SetJsonPath("strings.js");
367 source->AddResourcePath("print_preview.js", IDR_PRINT_PREVIEW_JS);
368 source->AddResourcePath("images/printer.png",
369 IDR_PRINT_PREVIEW_IMAGES_PRINTER);
370 source->AddResourcePath("images/printer_shared.png",
371 IDR_PRINT_PREVIEW_IMAGES_PRINTER_SHARED);
372 source->AddResourcePath("images/third_party.png",
373 IDR_PRINT_PREVIEW_IMAGES_THIRD_PARTY);
374 source->AddResourcePath("images/third_party_fedex.png",
375 IDR_PRINT_PREVIEW_IMAGES_THIRD_PARTY_FEDEX);
376 source->AddResourcePath("images/google_doc.png",
377 IDR_PRINT_PREVIEW_IMAGES_GOOGLE_DOC);
378 source->AddResourcePath("images/pdf.png", IDR_PRINT_PREVIEW_IMAGES_PDF);
379 source->AddResourcePath("images/mobile.png", IDR_PRINT_PREVIEW_IMAGES_MOBILE);
380 source->AddResourcePath("images/mobile_shared.png",
381 IDR_PRINT_PREVIEW_IMAGES_MOBILE_SHARED);
382 source->SetDefaultResource(IDR_PRINT_PREVIEW_HTML);
383 source->SetRequestFilter(base::Bind(&HandleRequestCallback));
384 source->OverrideContentSecurityPolicyObjectSrc("object-src 'self';");
385 source->AddLocalizedString("moreOptionsLabel", IDS_MORE_OPTIONS_LABEL);
386 source->AddLocalizedString("lessOptionsLabel", IDS_LESS_OPTIONS_LABEL);
387 return source;
390 PrintPreviewUI::TestingDelegate* g_testing_delegate = NULL;
392 } // namespace
394 PrintPreviewUI::PrintPreviewUI(content::WebUI* web_ui)
395 : ConstrainedWebDialogUI(web_ui),
396 initial_preview_start_time_(base::TimeTicks::Now()),
397 id_(g_print_preview_ui_id_map.Get().Add(this)),
398 handler_(NULL),
399 source_is_modifiable_(true),
400 source_has_selection_(false),
401 dialog_closed_(false),
402 weak_ptr_factory_(this) {
403 // Set up the chrome://print/ data source.
404 Profile* profile = Profile::FromWebUI(web_ui);
405 content::WebUIDataSource::Add(profile, CreatePrintPreviewUISource());
407 // Set up the chrome://theme/ source.
408 content::URLDataSource::Add(profile, new ThemeSource(profile));
410 // WebUI owns |handler_|.
411 handler_ = new PrintPreviewHandler();
412 web_ui->AddMessageHandler(handler_);
414 web_ui->AddMessageHandler(new MetricsHandler());
416 g_print_preview_request_id_map.Get().Set(id_, -1);
419 PrintPreviewUI::~PrintPreviewUI() {
420 print_preview_data_service()->RemoveEntry(id_);
421 g_print_preview_request_id_map.Get().Erase(id_);
422 g_print_preview_ui_id_map.Get().Remove(id_);
425 void PrintPreviewUI::GetPrintPreviewDataForIndex(
426 int index,
427 scoped_refptr<base::RefCountedBytes>* data) {
428 print_preview_data_service()->GetDataEntry(id_, index, data);
431 void PrintPreviewUI::SetPrintPreviewDataForIndex(
432 int index,
433 const base::RefCountedBytes* data) {
434 print_preview_data_service()->SetDataEntry(id_, index, data);
437 void PrintPreviewUI::ClearAllPreviewData() {
438 print_preview_data_service()->RemoveEntry(id_);
441 int PrintPreviewUI::GetAvailableDraftPageCount() {
442 return print_preview_data_service()->GetAvailableDraftPageCount(id_);
445 void PrintPreviewUI::SetInitiatorTitle(
446 const base::string16& job_title) {
447 initiator_title_ = job_title;
450 // static
451 void PrintPreviewUI::SetInitialParams(
452 content::WebContents* print_preview_dialog,
453 const PrintHostMsg_RequestPrintPreview_Params& params) {
454 if (!print_preview_dialog || !print_preview_dialog->GetWebUI())
455 return;
456 PrintPreviewUI* print_preview_ui = static_cast<PrintPreviewUI*>(
457 print_preview_dialog->GetWebUI()->GetController());
458 print_preview_ui->source_is_modifiable_ = params.is_modifiable;
459 print_preview_ui->source_has_selection_ = params.has_selection;
460 print_preview_ui->print_selection_only_ = params.selection_only;
463 // static
464 void PrintPreviewUI::GetCurrentPrintPreviewStatus(int32 preview_ui_id,
465 int request_id,
466 bool* cancel) {
467 int current_id = -1;
468 if (!g_print_preview_request_id_map.Get().Get(preview_ui_id, &current_id)) {
469 *cancel = true;
470 return;
472 *cancel = (request_id != current_id);
475 int32 PrintPreviewUI::GetIDForPrintPreviewUI() const {
476 return id_;
479 void PrintPreviewUI::OnPrintPreviewDialogClosed() {
480 WebContents* preview_dialog = web_ui()->GetWebContents();
481 printing::BackgroundPrintingManager* background_printing_manager =
482 g_browser_process->background_printing_manager();
483 if (background_printing_manager->HasPrintPreviewDialog(preview_dialog))
484 return;
485 OnClosePrintPreviewDialog();
488 void PrintPreviewUI::OnInitiatorClosed() {
489 WebContents* preview_dialog = web_ui()->GetWebContents();
490 printing::BackgroundPrintingManager* background_printing_manager =
491 g_browser_process->background_printing_manager();
492 if (background_printing_manager->HasPrintPreviewDialog(preview_dialog))
493 web_ui()->CallJavascriptFunction("cancelPendingPrintRequest");
494 else
495 OnClosePrintPreviewDialog();
498 void PrintPreviewUI::OnPrintPreviewRequest(int request_id) {
499 if (!initial_preview_start_time_.is_null()) {
500 UMA_HISTOGRAM_TIMES("PrintPreview.InitializationTime",
501 base::TimeTicks::Now() - initial_preview_start_time_);
503 g_print_preview_request_id_map.Get().Set(id_, request_id);
506 #if defined(ENABLE_BASIC_PRINTING)
507 void PrintPreviewUI::OnShowSystemDialog() {
508 web_ui()->CallJavascriptFunction("onSystemDialogLinkClicked");
510 #endif // ENABLE_BASIC_PRINTING
512 void PrintPreviewUI::OnDidGetPreviewPageCount(
513 const PrintHostMsg_DidGetPreviewPageCount_Params& params) {
514 DCHECK_GT(params.page_count, 0);
515 if (g_testing_delegate)
516 g_testing_delegate->DidGetPreviewPageCount(params.page_count);
517 base::FundamentalValue count(params.page_count);
518 base::FundamentalValue request_id(params.preview_request_id);
519 web_ui()->CallJavascriptFunction("onDidGetPreviewPageCount",
520 count,
521 request_id);
524 void PrintPreviewUI::OnDidGetDefaultPageLayout(
525 const PageSizeMargins& page_layout, const gfx::Rect& printable_area,
526 bool has_custom_page_size_style) {
527 if (page_layout.margin_top < 0 || page_layout.margin_left < 0 ||
528 page_layout.margin_bottom < 0 || page_layout.margin_right < 0 ||
529 page_layout.content_width < 0 || page_layout.content_height < 0 ||
530 printable_area.width() <= 0 || printable_area.height() <= 0) {
531 NOTREACHED();
532 return;
535 base::DictionaryValue layout;
536 layout.SetDouble(printing::kSettingMarginTop, page_layout.margin_top);
537 layout.SetDouble(printing::kSettingMarginLeft, page_layout.margin_left);
538 layout.SetDouble(printing::kSettingMarginBottom, page_layout.margin_bottom);
539 layout.SetDouble(printing::kSettingMarginRight, page_layout.margin_right);
540 layout.SetDouble(printing::kSettingContentWidth, page_layout.content_width);
541 layout.SetDouble(printing::kSettingContentHeight, page_layout.content_height);
542 layout.SetInteger(printing::kSettingPrintableAreaX, printable_area.x());
543 layout.SetInteger(printing::kSettingPrintableAreaY, printable_area.y());
544 layout.SetInteger(printing::kSettingPrintableAreaWidth,
545 printable_area.width());
546 layout.SetInteger(printing::kSettingPrintableAreaHeight,
547 printable_area.height());
549 base::FundamentalValue has_page_size_style(has_custom_page_size_style);
550 web_ui()->CallJavascriptFunction("onDidGetDefaultPageLayout", layout,
551 has_page_size_style);
554 void PrintPreviewUI::OnDidPreviewPage(int page_number,
555 int preview_request_id) {
556 DCHECK_GE(page_number, 0);
557 base::FundamentalValue number(page_number);
558 base::FundamentalValue ui_identifier(id_);
559 base::FundamentalValue request_id(preview_request_id);
560 if (g_testing_delegate)
561 g_testing_delegate->DidRenderPreviewPage(web_ui()->GetWebContents());
562 web_ui()->CallJavascriptFunction(
563 "onDidPreviewPage", number, ui_identifier, request_id);
564 if (g_testing_delegate && g_testing_delegate->IsAutoCancelEnabled())
565 web_ui()->CallJavascriptFunction("autoCancelForTesting");
568 void PrintPreviewUI::OnPreviewDataIsAvailable(int expected_pages_count,
569 int preview_request_id) {
570 VLOG(1) << "Print preview request finished with "
571 << expected_pages_count << " pages";
573 if (!initial_preview_start_time_.is_null()) {
574 UMA_HISTOGRAM_TIMES("PrintPreview.InitialDisplayTime",
575 base::TimeTicks::Now() - initial_preview_start_time_);
576 UMA_HISTOGRAM_COUNTS("PrintPreview.PageCount.Initial",
577 expected_pages_count);
578 UMA_HISTOGRAM_COUNTS(
579 "PrintPreview.RegeneratePreviewRequest.BeforeFirstData",
580 handler_->regenerate_preview_request_count());
581 initial_preview_start_time_ = base::TimeTicks();
583 base::FundamentalValue ui_identifier(id_);
584 base::FundamentalValue ui_preview_request_id(preview_request_id);
585 web_ui()->CallJavascriptFunction("updatePrintPreview", ui_identifier,
586 ui_preview_request_id);
589 void PrintPreviewUI::OnPrintPreviewDialogDestroyed() {
590 handler_->OnPrintPreviewDialogDestroyed();
593 void PrintPreviewUI::OnFileSelectionCancelled() {
594 web_ui()->CallJavascriptFunction("fileSelectionCancelled");
597 void PrintPreviewUI::OnCancelPendingPreviewRequest() {
598 g_print_preview_request_id_map.Get().Set(id_, -1);
601 void PrintPreviewUI::OnPrintPreviewFailed() {
602 handler_->OnPrintPreviewFailed();
603 web_ui()->CallJavascriptFunction("printPreviewFailed");
606 void PrintPreviewUI::OnInvalidPrinterSettings() {
607 web_ui()->CallJavascriptFunction("invalidPrinterSettings");
610 PrintPreviewDataService* PrintPreviewUI::print_preview_data_service() {
611 return PrintPreviewDataService::GetInstance();
614 void PrintPreviewUI::OnHidePreviewDialog() {
615 WebContents* preview_dialog = web_ui()->GetWebContents();
616 printing::BackgroundPrintingManager* background_printing_manager =
617 g_browser_process->background_printing_manager();
618 if (background_printing_manager->HasPrintPreviewDialog(preview_dialog))
619 return;
621 ConstrainedWebDialogDelegate* delegate = GetConstrainedDelegate();
622 if (!delegate)
623 return;
624 delegate->ReleaseWebContentsOnDialogClose();
625 background_printing_manager->OwnPrintPreviewDialog(preview_dialog);
626 OnClosePrintPreviewDialog();
629 void PrintPreviewUI::OnClosePrintPreviewDialog() {
630 if (dialog_closed_)
631 return;
632 dialog_closed_ = true;
633 ConstrainedWebDialogDelegate* delegate = GetConstrainedDelegate();
634 if (!delegate)
635 return;
636 delegate->GetWebDialogDelegate()->OnDialogClosed(std::string());
637 delegate->OnDialogCloseFromWebUI();
640 void PrintPreviewUI::OnReloadPrintersList() {
641 web_ui()->CallJavascriptFunction("reloadPrintersList");
644 void PrintPreviewUI::OnSetOptionsFromDocument(
645 const PrintHostMsg_SetOptionsFromDocument_Params& params) {
646 base::DictionaryValue options;
647 options.SetBoolean(printing::kSettingDisableScaling,
648 params.is_scaling_disabled);
649 options.SetInteger(printing::kSettingCopies, params.copies);
650 options.SetInteger(printing::kSettingDuplexMode, params.duplex);
651 web_ui()->CallJavascriptFunction("printPresetOptionsFromDocument", options);
654 // static
655 void PrintPreviewUI::SetDelegateForTesting(TestingDelegate* delegate) {
656 g_testing_delegate = delegate;
659 void PrintPreviewUI::SetSelectedFileForTesting(const base::FilePath& path) {
660 handler_->FileSelected(path, 0, NULL);
663 void PrintPreviewUI::SetPdfSavedClosureForTesting(
664 const base::Closure& closure) {
665 handler_->SetPdfSavedClosureForTesting(closure);
668 base::WeakPtr<PrintPreviewUI> PrintPreviewUI::GetWeakPtr() {
669 return weak_ptr_factory_.GetWeakPtr();