Don't show supervised user as "already on this device" while they're being imported.
[chromium-blink-merge.git] / chrome / browser / ui / webui / print_preview / print_preview_ui.cc
blob6d6ffae07cb9e97e70167b35b724c76c24eb7b3b
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 (!EndsWith(path, "/print.pdf", true))
128 return false;
130 // Print Preview data.
131 scoped_refptr<base::RefCountedBytes> data;
132 std::vector<std::string> url_substr;
133 base::SplitString(path, '/', &url_substr);
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 const base::string16 shortcut_text(base::UTF8ToUTF16(kBasicPrintShortcut));
214 #if !defined(OS_CHROMEOS)
215 source->AddString(
216 "systemDialogOption",
217 l10n_util::GetStringFUTF16(
218 IDS_PRINT_PREVIEW_SYSTEM_DIALOG_OPTION,
219 shortcut_text));
220 #endif
221 #if defined(OS_MACOSX)
222 source->AddLocalizedString("openPdfInPreviewOption",
223 IDS_PRINT_PREVIEW_OPEN_PDF_IN_PREVIEW_APP);
224 #endif
225 source->AddString(
226 "printWithCloudPrintWait",
227 l10n_util::GetStringFUTF16(
228 IDS_PRINT_PREVIEW_PRINT_WITH_CLOUD_PRINT_WAIT,
229 l10n_util::GetStringUTF16(IDS_GOOGLE_CLOUD_PRINT)));
230 source->AddString(
231 "noDestsPromoLearnMoreUrl",
232 chrome::kCloudPrintNoDestinationsLearnMoreURL);
233 source->AddLocalizedString("pageRangeInstruction",
234 IDS_PRINT_PREVIEW_PAGE_RANGE_INSTRUCTION);
235 source->AddLocalizedString("copiesInstruction",
236 IDS_PRINT_PREVIEW_COPIES_INSTRUCTION);
237 source->AddLocalizedString("incrementTitle",
238 IDS_PRINT_PREVIEW_INCREMENT_TITLE);
239 source->AddLocalizedString("decrementTitle",
240 IDS_PRINT_PREVIEW_DECREMENT_TITLE);
241 source->AddLocalizedString("printPagesLabel",
242 IDS_PRINT_PREVIEW_PRINT_PAGES_LABEL);
243 source->AddLocalizedString("optionsLabel", IDS_PRINT_PREVIEW_OPTIONS_LABEL);
244 source->AddLocalizedString("optionHeaderFooter",
245 IDS_PRINT_PREVIEW_OPTION_HEADER_FOOTER);
246 source->AddLocalizedString("optionFitToPage",
247 IDS_PRINT_PREVIEW_OPTION_FIT_TO_PAGE);
248 source->AddLocalizedString(
249 "optionBackgroundColorsAndImages",
250 IDS_PRINT_PREVIEW_OPTION_BACKGROUND_COLORS_AND_IMAGES);
251 source->AddLocalizedString("optionSelectionOnly",
252 IDS_PRINT_PREVIEW_OPTION_SELECTION_ONLY);
253 source->AddLocalizedString("marginsLabel", IDS_PRINT_PREVIEW_MARGINS_LABEL);
254 source->AddLocalizedString("defaultMargins",
255 IDS_PRINT_PREVIEW_DEFAULT_MARGINS);
256 source->AddLocalizedString("noMargins", IDS_PRINT_PREVIEW_NO_MARGINS);
257 source->AddLocalizedString("customMargins", IDS_PRINT_PREVIEW_CUSTOM_MARGINS);
258 source->AddLocalizedString("minimumMargins",
259 IDS_PRINT_PREVIEW_MINIMUM_MARGINS);
260 source->AddLocalizedString("top", IDS_PRINT_PREVIEW_TOP_MARGIN_LABEL);
261 source->AddLocalizedString("bottom", IDS_PRINT_PREVIEW_BOTTOM_MARGIN_LABEL);
262 source->AddLocalizedString("left", IDS_PRINT_PREVIEW_LEFT_MARGIN_LABEL);
263 source->AddLocalizedString("right", IDS_PRINT_PREVIEW_RIGHT_MARGIN_LABEL);
264 source->AddLocalizedString("mediaSizeLabel",
265 IDS_PRINT_PREVIEW_MEDIA_SIZE_LABEL);
266 source->AddLocalizedString("dpiLabel", IDS_PRINT_PREVIEW_DPI_LABEL);
267 source->AddLocalizedString("dpiItemLabel", IDS_PRINT_PREVIEW_DPI_ITEM_LABEL);
268 source->AddLocalizedString("nonIsotropicDpiItemLabel",
269 IDS_PRINT_PREVIEW_NON_ISOTROPIC_DPI_ITEM_LABEL);
270 source->AddLocalizedString("destinationSearchTitle",
271 IDS_PRINT_PREVIEW_DESTINATION_SEARCH_TITLE);
272 source->AddLocalizedString("accountSelectTitle",
273 IDS_PRINT_PREVIEW_ACCOUNT_SELECT_TITLE);
274 source->AddLocalizedString("addAccountTitle",
275 IDS_PRINT_PREVIEW_ADD_ACCOUNT_TITLE);
276 source->AddLocalizedString("cloudPrintPromotion",
277 IDS_PRINT_PREVIEW_CLOUD_PRINT_PROMOTION);
278 source->AddLocalizedString("searchBoxPlaceholder",
279 IDS_PRINT_PREVIEW_SEARCH_BOX_PLACEHOLDER);
280 source->AddLocalizedString("noDestinationsMessage",
281 IDS_PRINT_PREVIEW_NO_DESTINATIONS_MESSAGE);
282 source->AddLocalizedString("showAllButtonText",
283 IDS_PRINT_PREVIEW_SHOW_ALL_BUTTON_TEXT);
284 source->AddLocalizedString("destinationCount",
285 IDS_PRINT_PREVIEW_DESTINATION_COUNT);
286 source->AddLocalizedString("recentDestinationsTitle",
287 IDS_PRINT_PREVIEW_RECENT_DESTINATIONS_TITLE);
288 source->AddLocalizedString("localDestinationsTitle",
289 IDS_PRINT_PREVIEW_LOCAL_DESTINATIONS_TITLE);
290 source->AddLocalizedString("cloudDestinationsTitle",
291 IDS_PRINT_PREVIEW_CLOUD_DESTINATIONS_TITLE);
292 source->AddLocalizedString("manage", IDS_PRINT_PREVIEW_MANAGE);
293 source->AddLocalizedString("setupCloudPrinters",
294 IDS_PRINT_PREVIEW_SETUP_CLOUD_PRINTERS);
295 source->AddLocalizedString("changeDestination",
296 IDS_PRINT_PREVIEW_CHANGE_DESTINATION);
297 source->AddLocalizedString("offlineForYear",
298 IDS_PRINT_PREVIEW_OFFLINE_FOR_YEAR);
299 source->AddLocalizedString("offlineForMonth",
300 IDS_PRINT_PREVIEW_OFFLINE_FOR_MONTH);
301 source->AddLocalizedString("offlineForWeek",
302 IDS_PRINT_PREVIEW_OFFLINE_FOR_WEEK);
303 source->AddLocalizedString("offline", IDS_PRINT_PREVIEW_OFFLINE);
304 source->AddLocalizedString("fedexTos", IDS_PRINT_PREVIEW_FEDEX_TOS);
305 source->AddLocalizedString("tosCheckboxLabel",
306 IDS_PRINT_PREVIEW_TOS_CHECKBOX_LABEL);
307 source->AddLocalizedString("noDestsPromoTitle",
308 IDS_PRINT_PREVIEW_NO_DESTS_PROMO_TITLE);
309 source->AddLocalizedString("noDestsPromoBody",
310 IDS_PRINT_PREVIEW_NO_DESTS_PROMO_BODY);
311 source->AddLocalizedString("noDestsPromoGcpDesc",
312 IDS_PRINT_PREVIEW_NO_DESTS_GCP_DESC);
313 source->AddLocalizedString("learnMore",
314 IDS_LEARN_MORE);
315 source->AddLocalizedString(
316 "noDestsPromoAddPrinterButtonLabel",
317 IDS_PRINT_PREVIEW_NO_DESTS_PROMO_ADD_PRINTER_BUTTON_LABEL);
318 source->AddLocalizedString(
319 "noDestsPromoNotNowButtonLabel",
320 IDS_PRINT_PREVIEW_NO_DESTS_PROMO_NOT_NOW_BUTTON_LABEL);
321 source->AddLocalizedString("couldNotPrint",
322 IDS_PRINT_PREVIEW_COULD_NOT_PRINT);
323 source->AddLocalizedString("registerPromoButtonText",
324 IDS_PRINT_PREVIEW_REGISTER_PROMO_BUTTON_TEXT);
325 source->AddLocalizedString(
326 "extensionDestinationIconTooltip",
327 IDS_PRINT_PREVIEW_EXTENSION_DESTINATION_ICON_TOOLTIP);
328 source->AddLocalizedString(
329 "advancedSettingsSearchBoxPlaceholder",
330 IDS_PRINT_PREVIEW_ADVANCED_SETTINGS_SEARCH_BOX_PLACEHOLDER);
331 source->AddLocalizedString("advancedSettingsDialogTitle",
332 IDS_PRINT_PREVIEW_ADVANCED_SETTINGS_DIALOG_TITLE);
333 source->AddLocalizedString(
334 "noAdvancedSettingsMatchSearchHint",
335 IDS_PRINT_PREVIEW_NO_ADVANCED_SETTINGS_MATCH_SEARCH_HINT);
336 source->AddLocalizedString(
337 "advancedSettingsDialogConfirm",
338 IDS_PRINT_PREVIEW_ADVANCED_SETTINGS_DIALOG_CONFIRM);
339 source->AddLocalizedString("cancel", IDS_CANCEL);
340 source->AddLocalizedString("advancedOptionsLabel",
341 IDS_PRINT_PREVIEW_ADVANCED_OPTIONS_LABEL);
342 source->AddLocalizedString("showAdvancedOptions",
343 IDS_PRINT_PREVIEW_SHOW_ADVANCED_OPTIONS);
345 source->AddLocalizedString("accept", IDS_PRINT_PREVIEW_ACCEPT_INVITE);
346 source->AddLocalizedString(
347 "acceptForGroup", IDS_PRINT_PREVIEW_ACCEPT_GROUP_INVITE);
348 source->AddLocalizedString("reject", IDS_PRINT_PREVIEW_REJECT_INVITE);
349 source->AddLocalizedString(
350 "groupPrinterSharingInviteText", IDS_PRINT_PREVIEW_GROUP_INVITE_TEXT);
351 source->AddLocalizedString(
352 "printerSharingInviteText", IDS_PRINT_PREVIEW_INVITE_TEXT);
354 source->SetJsonPath("strings.js");
355 source->AddResourcePath("print_preview.js", IDR_PRINT_PREVIEW_JS);
356 source->AddResourcePath("images/printer.png",
357 IDR_PRINT_PREVIEW_IMAGES_PRINTER);
358 source->AddResourcePath("images/printer_shared.png",
359 IDR_PRINT_PREVIEW_IMAGES_PRINTER_SHARED);
360 source->AddResourcePath("images/third_party.png",
361 IDR_PRINT_PREVIEW_IMAGES_THIRD_PARTY);
362 source->AddResourcePath("images/third_party_fedex.png",
363 IDR_PRINT_PREVIEW_IMAGES_THIRD_PARTY_FEDEX);
364 source->AddResourcePath("images/google_doc.png",
365 IDR_PRINT_PREVIEW_IMAGES_GOOGLE_DOC);
366 source->AddResourcePath("images/pdf.png", IDR_PRINT_PREVIEW_IMAGES_PDF);
367 source->AddResourcePath("images/mobile.png", IDR_PRINT_PREVIEW_IMAGES_MOBILE);
368 source->AddResourcePath("images/mobile_shared.png",
369 IDR_PRINT_PREVIEW_IMAGES_MOBILE_SHARED);
370 source->SetDefaultResource(IDR_PRINT_PREVIEW_HTML);
371 source->SetRequestFilter(base::Bind(&HandleRequestCallback));
372 source->OverrideContentSecurityPolicyObjectSrc("object-src 'self';");
373 source->AddLocalizedString("moreOptionsLabel", IDS_MORE_OPTIONS_LABEL);
374 source->AddLocalizedString("lessOptionsLabel", IDS_LESS_OPTIONS_LABEL);
375 return source;
378 PrintPreviewUI::TestingDelegate* g_testing_delegate = NULL;
380 } // namespace
382 PrintPreviewUI::PrintPreviewUI(content::WebUI* web_ui)
383 : ConstrainedWebDialogUI(web_ui),
384 initial_preview_start_time_(base::TimeTicks::Now()),
385 id_(g_print_preview_ui_id_map.Get().Add(this)),
386 handler_(NULL),
387 source_is_modifiable_(true),
388 source_has_selection_(false),
389 dialog_closed_(false) {
390 // Set up the chrome://print/ data source.
391 Profile* profile = Profile::FromWebUI(web_ui);
392 content::WebUIDataSource::Add(profile, CreatePrintPreviewUISource());
394 // Set up the chrome://theme/ source.
395 content::URLDataSource::Add(profile, new ThemeSource(profile));
397 // WebUI owns |handler_|.
398 handler_ = new PrintPreviewHandler();
399 web_ui->AddMessageHandler(handler_);
401 web_ui->AddMessageHandler(new MetricsHandler());
403 g_print_preview_request_id_map.Get().Set(id_, -1);
406 PrintPreviewUI::~PrintPreviewUI() {
407 print_preview_data_service()->RemoveEntry(id_);
408 g_print_preview_request_id_map.Get().Erase(id_);
409 g_print_preview_ui_id_map.Get().Remove(id_);
412 void PrintPreviewUI::GetPrintPreviewDataForIndex(
413 int index,
414 scoped_refptr<base::RefCountedBytes>* data) {
415 print_preview_data_service()->GetDataEntry(id_, index, data);
418 void PrintPreviewUI::SetPrintPreviewDataForIndex(
419 int index,
420 const base::RefCountedBytes* data) {
421 print_preview_data_service()->SetDataEntry(id_, index, data);
424 void PrintPreviewUI::ClearAllPreviewData() {
425 print_preview_data_service()->RemoveEntry(id_);
428 int PrintPreviewUI::GetAvailableDraftPageCount() {
429 return print_preview_data_service()->GetAvailableDraftPageCount(id_);
432 void PrintPreviewUI::SetInitiatorTitle(
433 const base::string16& job_title) {
434 initiator_title_ = job_title;
437 // static
438 void PrintPreviewUI::SetInitialParams(
439 content::WebContents* print_preview_dialog,
440 const PrintHostMsg_RequestPrintPreview_Params& params) {
441 if (!print_preview_dialog || !print_preview_dialog->GetWebUI())
442 return;
443 PrintPreviewUI* print_preview_ui = static_cast<PrintPreviewUI*>(
444 print_preview_dialog->GetWebUI()->GetController());
445 print_preview_ui->source_is_modifiable_ = params.is_modifiable;
446 print_preview_ui->source_has_selection_ = params.has_selection;
447 print_preview_ui->print_selection_only_ = params.selection_only;
450 // static
451 void PrintPreviewUI::GetCurrentPrintPreviewStatus(int32 preview_ui_id,
452 int request_id,
453 bool* cancel) {
454 int current_id = -1;
455 if (!g_print_preview_request_id_map.Get().Get(preview_ui_id, &current_id)) {
456 *cancel = true;
457 return;
459 *cancel = (request_id != current_id);
462 int32 PrintPreviewUI::GetIDForPrintPreviewUI() const {
463 return id_;
466 void PrintPreviewUI::OnPrintPreviewDialogClosed() {
467 WebContents* preview_dialog = web_ui()->GetWebContents();
468 printing::BackgroundPrintingManager* background_printing_manager =
469 g_browser_process->background_printing_manager();
470 if (background_printing_manager->HasPrintPreviewDialog(preview_dialog))
471 return;
472 OnClosePrintPreviewDialog();
475 void PrintPreviewUI::OnInitiatorClosed() {
476 WebContents* preview_dialog = web_ui()->GetWebContents();
477 printing::BackgroundPrintingManager* background_printing_manager =
478 g_browser_process->background_printing_manager();
479 if (background_printing_manager->HasPrintPreviewDialog(preview_dialog))
480 web_ui()->CallJavascriptFunction("cancelPendingPrintRequest");
481 else
482 OnClosePrintPreviewDialog();
485 void PrintPreviewUI::OnPrintPreviewRequest(int request_id) {
486 if (!initial_preview_start_time_.is_null()) {
487 UMA_HISTOGRAM_TIMES("PrintPreview.InitializationTime",
488 base::TimeTicks::Now() - initial_preview_start_time_);
490 g_print_preview_request_id_map.Get().Set(id_, request_id);
493 #if defined(ENABLE_BASIC_PRINTING)
494 void PrintPreviewUI::OnShowSystemDialog() {
495 web_ui()->CallJavascriptFunction("onSystemDialogLinkClicked");
497 #endif // ENABLE_BASIC_PRINTING
499 void PrintPreviewUI::OnDidGetPreviewPageCount(
500 const PrintHostMsg_DidGetPreviewPageCount_Params& params) {
501 DCHECK_GT(params.page_count, 0);
502 if (g_testing_delegate)
503 g_testing_delegate->DidGetPreviewPageCount(params.page_count);
504 base::FundamentalValue count(params.page_count);
505 base::FundamentalValue request_id(params.preview_request_id);
506 web_ui()->CallJavascriptFunction("onDidGetPreviewPageCount",
507 count,
508 request_id);
511 void PrintPreviewUI::OnDidGetDefaultPageLayout(
512 const PageSizeMargins& page_layout, const gfx::Rect& printable_area,
513 bool has_custom_page_size_style) {
514 if (page_layout.margin_top < 0 || page_layout.margin_left < 0 ||
515 page_layout.margin_bottom < 0 || page_layout.margin_right < 0 ||
516 page_layout.content_width < 0 || page_layout.content_height < 0 ||
517 printable_area.width() <= 0 || printable_area.height() <= 0) {
518 NOTREACHED();
519 return;
522 base::DictionaryValue layout;
523 layout.SetDouble(printing::kSettingMarginTop, page_layout.margin_top);
524 layout.SetDouble(printing::kSettingMarginLeft, page_layout.margin_left);
525 layout.SetDouble(printing::kSettingMarginBottom, page_layout.margin_bottom);
526 layout.SetDouble(printing::kSettingMarginRight, page_layout.margin_right);
527 layout.SetDouble(printing::kSettingContentWidth, page_layout.content_width);
528 layout.SetDouble(printing::kSettingContentHeight, page_layout.content_height);
529 layout.SetInteger(printing::kSettingPrintableAreaX, printable_area.x());
530 layout.SetInteger(printing::kSettingPrintableAreaY, printable_area.y());
531 layout.SetInteger(printing::kSettingPrintableAreaWidth,
532 printable_area.width());
533 layout.SetInteger(printing::kSettingPrintableAreaHeight,
534 printable_area.height());
536 base::FundamentalValue has_page_size_style(has_custom_page_size_style);
537 web_ui()->CallJavascriptFunction("onDidGetDefaultPageLayout", layout,
538 has_page_size_style);
541 void PrintPreviewUI::OnDidPreviewPage(int page_number,
542 int preview_request_id) {
543 DCHECK_GE(page_number, 0);
544 base::FundamentalValue number(page_number);
545 base::FundamentalValue ui_identifier(id_);
546 base::FundamentalValue request_id(preview_request_id);
547 if (g_testing_delegate)
548 g_testing_delegate->DidRenderPreviewPage(web_ui()->GetWebContents());
549 web_ui()->CallJavascriptFunction(
550 "onDidPreviewPage", number, ui_identifier, request_id);
551 if (g_testing_delegate && g_testing_delegate->IsAutoCancelEnabled())
552 web_ui()->CallJavascriptFunction("autoCancelForTesting");
555 void PrintPreviewUI::OnPreviewDataIsAvailable(int expected_pages_count,
556 int preview_request_id) {
557 VLOG(1) << "Print preview request finished with "
558 << expected_pages_count << " pages";
560 if (!initial_preview_start_time_.is_null()) {
561 UMA_HISTOGRAM_TIMES("PrintPreview.InitialDisplayTime",
562 base::TimeTicks::Now() - initial_preview_start_time_);
563 UMA_HISTOGRAM_COUNTS("PrintPreview.PageCount.Initial",
564 expected_pages_count);
565 UMA_HISTOGRAM_COUNTS(
566 "PrintPreview.RegeneratePreviewRequest.BeforeFirstData",
567 handler_->regenerate_preview_request_count());
568 initial_preview_start_time_ = base::TimeTicks();
570 base::FundamentalValue ui_identifier(id_);
571 base::FundamentalValue ui_preview_request_id(preview_request_id);
572 web_ui()->CallJavascriptFunction("updatePrintPreview", ui_identifier,
573 ui_preview_request_id);
576 void PrintPreviewUI::OnPrintPreviewDialogDestroyed() {
577 handler_->OnPrintPreviewDialogDestroyed();
580 void PrintPreviewUI::OnFileSelectionCancelled() {
581 web_ui()->CallJavascriptFunction("fileSelectionCancelled");
584 void PrintPreviewUI::OnCancelPendingPreviewRequest() {
585 g_print_preview_request_id_map.Get().Set(id_, -1);
588 void PrintPreviewUI::OnPrintPreviewFailed() {
589 handler_->OnPrintPreviewFailed();
590 web_ui()->CallJavascriptFunction("printPreviewFailed");
593 void PrintPreviewUI::OnInvalidPrinterSettings() {
594 web_ui()->CallJavascriptFunction("invalidPrinterSettings");
597 PrintPreviewDataService* PrintPreviewUI::print_preview_data_service() {
598 return PrintPreviewDataService::GetInstance();
601 void PrintPreviewUI::OnHidePreviewDialog() {
602 WebContents* preview_dialog = web_ui()->GetWebContents();
603 printing::BackgroundPrintingManager* background_printing_manager =
604 g_browser_process->background_printing_manager();
605 if (background_printing_manager->HasPrintPreviewDialog(preview_dialog))
606 return;
608 ConstrainedWebDialogDelegate* delegate = GetConstrainedDelegate();
609 if (!delegate)
610 return;
611 delegate->ReleaseWebContentsOnDialogClose();
612 background_printing_manager->OwnPrintPreviewDialog(preview_dialog);
613 OnClosePrintPreviewDialog();
616 void PrintPreviewUI::OnClosePrintPreviewDialog() {
617 if (dialog_closed_)
618 return;
619 dialog_closed_ = true;
620 ConstrainedWebDialogDelegate* delegate = GetConstrainedDelegate();
621 if (!delegate)
622 return;
623 delegate->GetWebDialogDelegate()->OnDialogClosed(std::string());
624 delegate->OnDialogCloseFromWebUI();
627 void PrintPreviewUI::OnReloadPrintersList() {
628 web_ui()->CallJavascriptFunction("reloadPrintersList");
631 void PrintPreviewUI::OnSetOptionsFromDocument(
632 const PrintHostMsg_SetOptionsFromDocument_Params& params) {
633 base::DictionaryValue options;
634 options.SetBoolean(printing::kSettingDisableScaling,
635 params.is_scaling_disabled);
636 options.SetInteger(printing::kSettingCopies, params.copies);
637 options.SetInteger(printing::kSettingDuplexMode, params.duplex);
638 web_ui()->CallJavascriptFunction("printPresetOptionsFromDocument", options);
641 // static
642 void PrintPreviewUI::SetDelegateForTesting(TestingDelegate* delegate) {
643 g_testing_delegate = delegate;
646 void PrintPreviewUI::SetSelectedFileForTesting(const base::FilePath& path) {
647 handler_->FileSelected(path, 0, NULL);
650 void PrintPreviewUI::SetPdfSavedClosureForTesting(
651 const base::Closure& closure) {
652 handler_->SetPdfSavedClosureForTesting(closure);