| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/ui/webui/print_preview_data_source.h" | |
| 6 | |
| 7 #include <algorithm> | |
| 8 #include <vector> | |
| 9 | |
| 10 #include "base/message_loop.h" | |
| 11 #include "base/string_number_conversions.h" | |
| 12 #include "base/string_piece.h" | |
| 13 #include "base/string_split.h" | |
| 14 #include "base/string_util.h" | |
| 15 #include "base/utf_string_conversions.h" | |
| 16 #include "base/values.h" | |
| 17 #include "chrome/browser/printing/print_preview_data_service.h" | |
| 18 #include "chrome/common/url_constants.h" | |
| 19 #include "grit/browser_resources.h" | |
| 20 #include "grit/chromium_strings.h" | |
| 21 #include "grit/generated_resources.h" | |
| 22 #include "grit/google_chrome_strings.h" | |
| 23 #include "ui/base/l10n/l10n_util.h" | |
| 24 #include "ui/base/resource/resource_bundle.h" | |
| 25 | |
| 26 namespace { | |
| 27 | |
| 28 #if defined(OS_MACOSX) | |
| 29 // U+0028 U+21E7 U+2318 U+0050 U+0029 in UTF8 | |
| 30 const char kAdvancedPrintShortcut[] = "\x28\xE2\x8c\xA5\xE2\x8C\x98\x50\x29"; | |
| 31 #elif defined(OS_WIN) | |
| 32 const char kAdvancedPrintShortcut[] = "(Ctrl+Shift+P)"; | |
| 33 #else | |
| 34 const char kAdvancedPrintShortcut[] = "(Shift+Ctrl+P)"; | |
| 35 #endif | |
| 36 | |
| 37 }; // namespace | |
| 38 | |
| 39 PrintPreviewDataSource::PrintPreviewDataSource(bool is_dummy) | |
| 40 : ChromeWebUIDataSource(chrome::kChromeUIPrintHost) { | |
| 41 Init(is_dummy); | |
| 42 } | |
| 43 | |
| 44 void PrintPreviewDataSource::Init(bool is_dummy) { | |
| 45 if (is_dummy) { | |
| 46 AddLocalizedString("intentionallyBlankText", | |
| 47 IDS_PRINT_PREVIEW_INTENTIONALLY_BLANK); | |
| 48 } | |
| 49 | |
| 50 AddLocalizedString("title", IDS_PRINT_PREVIEW_TITLE); | |
| 51 AddLocalizedString("loading", IDS_PRINT_PREVIEW_LOADING); | |
| 52 #if defined(GOOGLE_CHROME_BUILD) | |
| 53 AddString("noPlugin", l10n_util::GetStringFUTF16( | |
| 54 IDS_PRINT_PREVIEW_NO_PLUGIN, ASCIIToUTF16("chrome://plugins/"))); | |
| 55 #else | |
| 56 AddLocalizedString("noPlugin", IDS_PRINT_PREVIEW_NO_PLUGIN); | |
| 57 #endif | |
| 58 AddLocalizedString("launchNativeDialog", IDS_PRINT_PREVIEW_NATIVE_DIALOG); | |
| 59 AddLocalizedString("previewFailed", IDS_PRINT_PREVIEW_FAILED); | |
| 60 AddLocalizedString("invalidPrinterSettings", | |
| 61 IDS_PRINT_PREVIEW_INVALID_PRINTER_SETTINGS); | |
| 62 AddLocalizedString("printButton", IDS_PRINT_PREVIEW_PRINT_BUTTON); | |
| 63 AddLocalizedString("cancelButton", IDS_PRINT_PREVIEW_CANCEL_BUTTON); | |
| 64 AddLocalizedString("printing", IDS_PRINT_PREVIEW_PRINTING); | |
| 65 AddLocalizedString("printingToPDFInProgress", | |
| 66 IDS_PRINT_PREVIEW_PRINTING_TO_PDF_IN_PROGRESS); | |
| 67 #if defined(OS_MACOSX) | |
| 68 AddLocalizedString("openingPDFInPreview", | |
| 69 IDS_PRINT_PREVIEW_OPENING_PDF_IN_PREVIEW); | |
| 70 #endif | |
| 71 AddLocalizedString("destinationLabel", IDS_PRINT_PREVIEW_DESTINATION_LABEL); | |
| 72 AddLocalizedString("copiesLabel", IDS_PRINT_PREVIEW_COPIES_LABEL); | |
| 73 AddLocalizedString("examplePageRangeText", | |
| 74 IDS_PRINT_PREVIEW_EXAMPLE_PAGE_RANGE_TEXT); | |
| 75 AddLocalizedString("layoutLabel", IDS_PRINT_PREVIEW_LAYOUT_LABEL); | |
| 76 AddLocalizedString("optionAllPages", IDS_PRINT_PREVIEW_OPTION_ALL_PAGES); | |
| 77 AddLocalizedString("optionBw", IDS_PRINT_PREVIEW_OPTION_BW); | |
| 78 AddLocalizedString("optionCollate", IDS_PRINT_PREVIEW_OPTION_COLLATE); | |
| 79 AddLocalizedString("optionColor", IDS_PRINT_PREVIEW_OPTION_COLOR); | |
| 80 AddLocalizedString("optionLandscape", IDS_PRINT_PREVIEW_OPTION_LANDSCAPE); | |
| 81 AddLocalizedString("optionPortrait", IDS_PRINT_PREVIEW_OPTION_PORTRAIT); | |
| 82 AddLocalizedString("optionTwoSided", IDS_PRINT_PREVIEW_OPTION_TWO_SIDED); | |
| 83 AddLocalizedString("pagesLabel", IDS_PRINT_PREVIEW_PAGES_LABEL); | |
| 84 AddLocalizedString("pageRangeTextBox", IDS_PRINT_PREVIEW_PAGE_RANGE_TEXT); | |
| 85 AddLocalizedString("pageRangeRadio", IDS_PRINT_PREVIEW_PAGE_RANGE_RADIO); | |
| 86 AddLocalizedString("printToPDF", IDS_PRINT_PREVIEW_PRINT_TO_PDF); | |
| 87 AddLocalizedString("printPreviewTitleFormat", IDS_PRINT_PREVIEW_TITLE_FORMAT); | |
| 88 AddLocalizedString("printPreviewSummaryFormatShort", | |
| 89 IDS_PRINT_PREVIEW_SUMMARY_FORMAT_SHORT); | |
| 90 AddLocalizedString("printPreviewSummaryFormatLong", | |
| 91 IDS_PRINT_PREVIEW_SUMMARY_FORMAT_LONG); | |
| 92 AddLocalizedString("printPreviewSheetsLabelSingular", | |
| 93 IDS_PRINT_PREVIEW_SHEETS_LABEL_SINGULAR); | |
| 94 AddLocalizedString("printPreviewSheetsLabelPlural", | |
| 95 IDS_PRINT_PREVIEW_SHEETS_LABEL_PLURAL); | |
| 96 AddLocalizedString("printPreviewPageLabelSingular", | |
| 97 IDS_PRINT_PREVIEW_PAGE_LABEL_SINGULAR); | |
| 98 AddLocalizedString("printPreviewPageLabelPlural", | |
| 99 IDS_PRINT_PREVIEW_PAGE_LABEL_PLURAL); | |
| 100 const string16 shortcut_text(UTF8ToUTF16(kAdvancedPrintShortcut)); | |
| 101 #if defined(OS_CHROMEOS) | |
| 102 AddString("cloudPrintDialogOption", l10n_util::GetStringFUTF16( | |
| 103 IDS_PRINT_PREVIEW_CLOUD_DIALOG_OPTION, | |
| 104 l10n_util::GetStringUTF16(IDS_GOOGLE_CLOUD_PRINT), | |
| 105 shortcut_text)); | |
| 106 AddLocalizedString("printWithCloudPrint", | |
| 107 IDS_PRINT_PREVIEW_MORE_PRINTERS); | |
| 108 #else | |
| 109 AddString("systemDialogOption", l10n_util::GetStringFUTF16( | |
| 110 IDS_PRINT_PREVIEW_SYSTEM_DIALOG_OPTION, | |
| 111 shortcut_text)); | |
| 112 AddString("printWithCloudPrint", l10n_util::GetStringFUTF16( | |
| 113 IDS_PRINT_PREVIEW_PRINT_WITH_CLOUD_PRINT, | |
| 114 l10n_util::GetStringUTF16(IDS_GOOGLE_CLOUD_PRINT))); | |
| 115 #endif | |
| 116 #if defined(OS_MACOSX) | |
| 117 AddLocalizedString("openPdfInPreviewOption", | |
| 118 IDS_PRINT_PREVIEW_OPEN_PDF_IN_PREVIEW_APP); | |
| 119 #endif | |
| 120 AddString("printWithCloudPrintWait", l10n_util::GetStringFUTF16( | |
| 121 IDS_PRINT_PREVIEW_PRINT_WITH_CLOUD_PRINT_WAIT, | |
| 122 l10n_util::GetStringUTF16(IDS_GOOGLE_CLOUD_PRINT))); | |
| 123 AddLocalizedString("pageRangeInstruction", | |
| 124 IDS_PRINT_PREVIEW_PAGE_RANGE_INSTRUCTION); | |
| 125 AddLocalizedString("copiesInstruction", IDS_PRINT_PREVIEW_COPIES_INSTRUCTION); | |
| 126 AddLocalizedString("signIn", IDS_PRINT_PREVIEW_SIGN_IN); | |
| 127 AddLocalizedString("managePrinters", IDS_PRINT_PREVIEW_MANAGE_PRINTERS); | |
| 128 AddLocalizedString("incrementTitle", IDS_PRINT_PREVIEW_INCREMENT_TITLE); | |
| 129 AddLocalizedString("decrementTitle", IDS_PRINT_PREVIEW_DECREMENT_TITLE); | |
| 130 AddLocalizedString("printPagesLabel", IDS_PRINT_PREVIEW_PRINT_PAGES_LABEL); | |
| 131 AddLocalizedString("optionsLabel", IDS_PRINT_PREVIEW_OPTIONS_LABEL); | |
| 132 AddLocalizedString("optionHeaderFooter", | |
| 133 IDS_PRINT_PREVIEW_OPTION_HEADER_FOOTER); | |
| 134 AddLocalizedString("marginsLabel", IDS_PRINT_PREVIEW_MARGINS_LABEL); | |
| 135 AddLocalizedString("defaultMargins", IDS_PRINT_PREVIEW_DEFAULT_MARGINS); | |
| 136 AddLocalizedString("noMargins", IDS_PRINT_PREVIEW_NO_MARGINS); | |
| 137 AddLocalizedString("customMargins", IDS_PRINT_PREVIEW_CUSTOM_MARGINS); | |
| 138 AddLocalizedString("minimumMargins", IDS_PRINT_PREVIEW_MINIMUM_MARGINS); | |
| 139 AddLocalizedString("top", IDS_PRINT_PREVIEW_TOP_MARGIN_LABEL); | |
| 140 AddLocalizedString("bottom", IDS_PRINT_PREVIEW_BOTTOM_MARGIN_LABEL); | |
| 141 AddLocalizedString("left", IDS_PRINT_PREVIEW_LEFT_MARGIN_LABEL); | |
| 142 AddLocalizedString("right", IDS_PRINT_PREVIEW_RIGHT_MARGIN_LABEL); | |
| 143 | |
| 144 set_json_path("strings.js"); | |
| 145 if (is_dummy) { | |
| 146 add_resource_path("print_preview_dummy.js", IDR_PRINT_PREVIEW_DUMMY_JS); | |
| 147 set_default_resource(IDR_PRINT_PREVIEW_DUMMY_HTML); | |
| 148 } else { | |
| 149 add_resource_path("print_preview.js", IDR_PRINT_PREVIEW_JS); | |
| 150 set_default_resource(IDR_PRINT_PREVIEW_HTML); | |
| 151 } | |
| 152 } | |
| 153 | |
| 154 PrintPreviewDataSource::~PrintPreviewDataSource() { | |
| 155 } | |
| 156 | |
| 157 void PrintPreviewDataSource::StartDataRequest(const std::string& path, | |
| 158 bool is_incognito, | |
| 159 int request_id) { | |
| 160 // Parent class handles most requests except for the print preview data. | |
| 161 if (!EndsWith(path, "/print.pdf", true)) { | |
| 162 ChromeWebUIDataSource::StartDataRequest(path, is_incognito, request_id); | |
| 163 return; | |
| 164 } | |
| 165 | |
| 166 // Print Preview data. | |
| 167 scoped_refptr<RefCountedBytes> data; | |
| 168 std::vector<std::string> url_substr; | |
| 169 base::SplitString(path, '/', &url_substr); | |
| 170 int page_index = 0; | |
| 171 if (url_substr.size() == 3 && base::StringToInt(url_substr[1], &page_index)) { | |
| 172 PrintPreviewDataService::GetInstance()->GetDataEntry( | |
| 173 url_substr[0], page_index, &data); | |
| 174 } | |
| 175 if (data.get()) { | |
| 176 SendResponse(request_id, data); | |
| 177 return; | |
| 178 } | |
| 179 // Invalid request. | |
| 180 scoped_refptr<RefCountedBytes> empty_bytes(new RefCountedBytes); | |
| 181 SendResponse(request_id, empty_bytes); | |
| 182 } | |
| OLD | NEW |