OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/ui/webui/print_preview_data_source.h" | 5 #include "chrome/browser/ui/webui/print_preview_data_source.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
10 #include "base/string_number_conversions.h" | 10 #include "base/string_number_conversions.h" |
11 #include "base/string_piece.h" | 11 #include "base/string_piece.h" |
12 #include "base/string_split.h" | 12 #include "base/string_split.h" |
13 #include "base/string_util.h" | 13 #include "base/string_util.h" |
14 #include "base/utf_string_conversions.h" | 14 #include "base/utf_string_conversions.h" |
15 #include "base/values.h" | 15 #include "base/values.h" |
16 #include "chrome/browser/printing/print_preview_data_service.h" | 16 #include "chrome/browser/printing/print_preview_data_service.h" |
17 #include "chrome/common/jstemplate_builder.h" | 17 #include "chrome/common/jstemplate_builder.h" |
18 #include "chrome/common/url_constants.h" | 18 #include "chrome/common/url_constants.h" |
19 #include "grit/browser_resources.h" | 19 #include "grit/browser_resources.h" |
20 #include "grit/chromium_strings.h" | 20 #include "grit/chromium_strings.h" |
21 #include "grit/generated_resources.h" | 21 #include "grit/generated_resources.h" |
22 #include "grit/google_chrome_strings.h" | 22 #include "grit/google_chrome_strings.h" |
23 #include "ui/base/l10n/l10n_util.h" | 23 #include "ui/base/l10n/l10n_util.h" |
24 #include "ui/base/resource/resource_bundle.h" | 24 #include "ui/base/resource/resource_bundle.h" |
25 | 25 |
26 namespace { | 26 PrintPreviewDataSource::PrintPreviewDataSource() |
| 27 : ChromeWebUIDataSource(chrome::kChromeUIPrintHost) { |
27 | 28 |
28 void SetLocalizedStrings(DictionaryValue* localized_strings) { | 29 AddLocalizedString("title", IDS_PRINT_PREVIEW_TITLE); |
29 localized_strings->SetString(std::string("title"), | 30 AddLocalizedString("loading", IDS_PRINT_PREVIEW_LOADING); |
30 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_TITLE)); | |
31 localized_strings->SetString(std::string("loading"), | |
32 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_LOADING)); | |
33 #if defined(GOOGLE_CHROME_BUILD) | 31 #if defined(GOOGLE_CHROME_BUILD) |
34 localized_strings->SetString(std::string("noPlugin"), | 32 AddString("noPlugin", l10n_util::GetStringFUTF8( |
35 l10n_util::GetStringFUTF8(IDS_PRINT_PREVIEW_NO_PLUGIN, | 33 IDS_PRINT_PREVIEW_NO_PLUGIN, ASCIIToUTF16("chrome://plugins/")); |
36 ASCIIToUTF16("chrome://plugins/"))); | |
37 #else | 34 #else |
38 localized_strings->SetString(std::string("noPlugin"), | 35 AddLocalizedString("noPlugin", IDS_PRINT_PREVIEW_NO_PLUGIN); |
39 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_NO_PLUGIN)); | |
40 #endif | 36 #endif |
41 localized_strings->SetString(std::string("launchNativeDialog"), | 37 AddLocalizedString("launchNativeDialog", IDS_PRINT_PREVIEW_NATIVE_DIALOG); |
42 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_NATIVE_DIALOG)); | 38 AddLocalizedString("previewFailed", IDS_PRINT_PREVIEW_FAILED); |
43 localized_strings->SetString(std::string("previewFailed"), | 39 AddLocalizedString("initiatorTabClosed", |
44 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_FAILED)); | 40 IDS_PRINT_PREVIEW_INITIATOR_TAB_CLOSED); |
45 localized_strings->SetString(std::string("initiatorTabClosed"), | 41 AddLocalizedString("reopenPage", IDS_PRINT_PREVIEW_REOPEN_PAGE); |
46 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_INITIATOR_TAB_CLOSED)); | |
47 localized_strings->SetString(std::string("reopenPage"), | |
48 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_REOPEN_PAGE)); | |
49 | 42 |
50 localized_strings->SetString(std::string("printButton"), | 43 AddLocalizedString("printButton", IDS_PRINT_PREVIEW_PRINT_BUTTON); |
51 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_PRINT_BUTTON)); | 44 AddLocalizedString("cancelButton", IDS_PRINT_PREVIEW_CANCEL_BUTTON); |
52 localized_strings->SetString(std::string("cancelButton"), | 45 AddLocalizedString("printing", IDS_PRINT_PREVIEW_PRINTING); |
53 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_CANCEL_BUTTON)); | |
54 localized_strings->SetString(std::string("printing"), | |
55 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_PRINTING)); | |
56 | 46 |
57 localized_strings->SetString(std::string("destinationLabel"), | 47 AddLocalizedString("destinationLabel", IDS_PRINT_PREVIEW_DESTINATION_LABEL); |
58 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_DESTINATION_LABEL)); | 48 AddLocalizedString("copiesLabel", IDS_PRINT_PREVIEW_COPIES_LABEL); |
59 localized_strings->SetString(std::string("copiesLabel"), | 49 AddLocalizedString("examplePageRangeText", |
60 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_COPIES_LABEL)); | 50 IDS_PRINT_PREVIEW_EXAMPLE_PAGE_RANGE_TEXT); |
61 localized_strings->SetString(std::string("examplePageRangeText"), | 51 AddLocalizedString("invalidNumberOfCopies", |
62 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_EXAMPLE_PAGE_RANGE_TEXT)); | 52 IDS_PRINT_PREVIEW_INVALID_NUMBER_OF_COPIES); |
63 localized_strings->SetString(std::string("invalidNumberOfCopies"), | 53 AddLocalizedString("layoutLabel", IDS_PRINT_PREVIEW_LAYOUT_LABEL); |
64 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_INVALID_NUMBER_OF_COPIES)); | 54 AddLocalizedString("optionAllPages", IDS_PRINT_PREVIEW_OPTION_ALL_PAGES); |
65 localized_strings->SetString(std::string("layoutLabel"), | 55 AddLocalizedString("optionBw", IDS_PRINT_PREVIEW_OPTION_BW); |
66 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_LAYOUT_LABEL)); | 56 AddLocalizedString("optionCollate", IDS_PRINT_PREVIEW_OPTION_COLLATE); |
67 localized_strings->SetString(std::string("optionAllPages"), | 57 AddLocalizedString("optionColor", IDS_PRINT_PREVIEW_OPTION_COLOR); |
68 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_OPTION_ALL_PAGES)); | 58 AddLocalizedString("optionLandscape", IDS_PRINT_PREVIEW_OPTION_LANDSCAPE); |
69 localized_strings->SetString(std::string("optionBw"), | 59 AddLocalizedString("optionPortrait", IDS_PRINT_PREVIEW_OPTION_PORTRAIT); |
70 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_OPTION_BW)); | 60 AddLocalizedString("optionTwoSided", IDS_PRINT_PREVIEW_OPTION_TWO_SIDED); |
71 localized_strings->SetString(std::string("optionCollate"), | 61 AddLocalizedString("pagesLabel", IDS_PRINT_PREVIEW_PAGES_LABEL); |
72 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_OPTION_COLLATE)); | 62 AddLocalizedString("pageRangeTextBox", IDS_PRINT_PREVIEW_PAGE_RANGE_TEXT); |
73 localized_strings->SetString(std::string("optionColor"), | 63 AddLocalizedString("pageRangeRadio", IDS_PRINT_PREVIEW_PAGE_RANGE_RADIO); |
74 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_OPTION_COLOR)); | 64 AddLocalizedString("printToPDF", IDS_PRINT_PREVIEW_PRINT_TO_PDF); |
75 localized_strings->SetString(std::string("optionLandscape"), | 65 AddLocalizedString("printPreviewTitleFormat", IDS_PRINT_PREVIEW_TITLE_FORMAT); |
76 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_OPTION_LANDSCAPE)); | 66 AddLocalizedString("printPreviewSummaryFormatShort", |
77 localized_strings->SetString(std::string("optionPortrait"), | 67 IDS_PRINT_PREVIEW_SUMMARY_FORMAT_SHORT); |
78 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_OPTION_PORTRAIT)); | 68 AddLocalizedString("printPreviewSummaryFormatLong", |
79 localized_strings->SetString(std::string("optionTwoSided"), | 69 IDS_PRINT_PREVIEW_SUMMARY_FORMAT_LONG); |
80 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_OPTION_TWO_SIDED)); | 70 AddLocalizedString("printPreviewSheetsLabelSingular", |
81 localized_strings->SetString(std::string("pagesLabel"), | 71 IDS_PRINT_PREVIEW_SHEETS_LABEL_SINGULAR); |
82 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_PAGES_LABEL)); | 72 AddLocalizedString("printPreviewSheetsLabelPlural", |
83 localized_strings->SetString(std::string("pageRangeTextBox"), | 73 IDS_PRINT_PREVIEW_SHEETS_LABEL_PLURAL); |
84 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_PAGE_RANGE_TEXT)); | 74 AddLocalizedString("printPreviewPageLabelSingular", |
85 localized_strings->SetString(std::string("pageRangeRadio"), | 75 IDS_PRINT_PREVIEW_PAGE_LABEL_SINGULAR); |
86 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_PAGE_RANGE_RADIO)); | 76 AddLocalizedString("printPreviewPageLabelPlural", |
87 localized_strings->SetString(std::string("printToPDF"), | 77 IDS_PRINT_PREVIEW_PAGE_LABEL_PLURAL); |
88 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_PRINT_TO_PDF)); | 78 AddLocalizedString("systemDialogOption", |
89 localized_strings->SetString(std::string("printPreviewTitleFormat"), | 79 IDS_PRINT_PREVIEW_SYSTEM_DIALOG_OPTION); |
90 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_TITLE_FORMAT)); | 80 AddLocalizedString("pageRangeInstruction", |
91 localized_strings->SetString(std::string("printPreviewSummaryFormatShort"), | 81 IDS_PRINT_PREVIEW_PAGE_RANGE_INSTRUCTION); |
92 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_SUMMARY_FORMAT_SHORT)); | 82 AddLocalizedString("copiesInstruction", IDS_PRINT_PREVIEW_COPIES_INSTRUCTION); |
93 localized_strings->SetString(std::string("printPreviewSummaryFormatLong"), | 83 AddLocalizedString("signIn", IDS_PRINT_PREVIEW_SIGN_IN); |
94 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_SUMMARY_FORMAT_LONG)); | 84 AddLocalizedString("morePrinters", IDS_PRINT_PREVIEW_MORE_PRINTERS); |
95 localized_strings->SetString(std::string("printPreviewSheetsLabelSingular"), | 85 AddLocalizedString("addCloudPrinter", IDS_PRINT_PREVIEW_ADD_CLOUD_PRINTER); |
96 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_SHEETS_LABEL_SINGULAR)); | 86 AddLocalizedString("cloudPrinters", IDS_PRINT_PREVIEW_CLOUD_PRINTERS); |
97 localized_strings->SetString(std::string("printPreviewSheetsLabelPlural"), | 87 AddLocalizedString("localPrinters", IDS_PRINT_PREVIEW_LOCAL_PRINTERS); |
98 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_SHEETS_LABEL_PLURAL)); | 88 AddLocalizedString("manageCloudPrinters", |
99 localized_strings->SetString(std::string("printPreviewPageLabelSingular"), | 89 IDS_PRINT_PREVIEW_MANAGE_CLOUD_PRINTERS); |
100 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_PAGE_LABEL_SINGULAR)); | 90 AddLocalizedString("manageLocalPrinters", |
101 localized_strings->SetString(std::string("printPreviewPageLabelPlural"), | 91 IDS_PRINT_PREVIEW_MANAGE_LOCAL_PRINTERS); |
102 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_PAGE_LABEL_PLURAL)); | 92 AddLocalizedString("managePrinters", IDS_PRINT_PREVIEW_MANAGE_PRINTERS); |
103 localized_strings->SetString(std::string("systemDialogOption"), | 93 AddLocalizedString("incrementTitle", IDS_PRINT_PREVIEW_INCREMENT_TITLE); |
104 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_SYSTEM_DIALOG_OPTION)); | 94 AddLocalizedString("decrementTitle", IDS_PRINT_PREVIEW_DECREMENT_TITLE); |
105 localized_strings->SetString(std::string("pageRangeInstruction"), | 95 AddLocalizedString("printPagesLabel", IDS_PRINT_PREVIEW_PRINT_PAGES_LABEL); |
106 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_PAGE_RANGE_INSTRUCTION)); | |
107 localized_strings->SetString(std::string("copiesInstruction"), | |
108 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_COPIES_INSTRUCTION)); | |
109 localized_strings->SetString(std::string("signIn"), | |
110 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_SIGN_IN)); | |
111 localized_strings->SetString(std::string("morePrinters"), | |
112 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_MORE_PRINTERS)); | |
113 localized_strings->SetString(std::string("addCloudPrinter"), | |
114 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_ADD_CLOUD_PRINTER)); | |
115 localized_strings->SetString(std::string("cloudPrinters"), | |
116 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_CLOUD_PRINTERS)); | |
117 localized_strings->SetString(std::string("localPrinters"), | |
118 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_LOCAL_PRINTERS)); | |
119 localized_strings->SetString(std::string("manageCloudPrinters"), | |
120 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_MANAGE_CLOUD_PRINTERS)); | |
121 localized_strings->SetString(std::string("manageLocalPrinters"), | |
122 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_MANAGE_LOCAL_PRINTERS)); | |
123 localized_strings->SetString(std::string("managePrinters"), | |
124 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_MANAGE_PRINTERS)); | |
125 localized_strings->SetString(std::string("incrementTitle"), | |
126 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_INCREMENT_TITLE)); | |
127 localized_strings->SetString(std::string("decrementTitle"), | |
128 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_DECREMENT_TITLE)); | |
129 localized_strings->SetString(std::string("printPagesLabel"), | |
130 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_PRINT_PAGES_LABEL)); | |
131 } | |
132 | 96 |
133 } // namespace | 97 set_json_path("strings.js"); |
134 | 98 add_resource_path("print_preview.js", IDR_PRINT_PREVIEW_JS); |
135 PrintPreviewDataSource::PrintPreviewDataSource() | 99 set_default_resource(IDR_PRINT_PREVIEW_HTML); |
136 : DataSource(chrome::kChromeUIPrintHost, MessageLoop::current()) { | |
137 } | 100 } |
138 | 101 |
139 PrintPreviewDataSource::~PrintPreviewDataSource() { | 102 PrintPreviewDataSource::~PrintPreviewDataSource() { |
140 } | 103 } |
141 | 104 |
142 void PrintPreviewDataSource::StartDataRequest(const std::string& path, | 105 void PrintPreviewDataSource::StartDataRequest(const std::string& path, |
143 bool is_incognito, | 106 bool is_incognito, |
144 int request_id) { | 107 int request_id) { |
145 scoped_refptr<RefCountedBytes> data; | 108 // Parent class handles most requests except for the print preview data. |
146 | 109 if (!EndsWith(path, "/print.pdf", true)) { |
147 bool preview_data_requested = EndsWith(path, "/print.pdf", true); | 110 ChromeWebUIDataSource::StartDataRequest(path, is_incognito, request_id); |
148 if (preview_data_requested) { | 111 return; |
149 std::vector<std::string> url_substr; | |
150 base::SplitString(path, '/', &url_substr); | |
151 int page_index = 0; | |
152 if (url_substr.size() == 3 && base::StringToInt(url_substr[1], | |
153 &page_index)) { | |
154 PrintPreviewDataService::GetInstance()->GetDataEntry(url_substr[0], | |
155 page_index, &data); | |
156 } | |
157 } | 112 } |
158 | 113 |
159 if (path.empty()) { | 114 // Print Preview data. |
160 // Print Preview Index page. | 115 scoped_refptr<RefCountedBytes> data; |
161 DictionaryValue localized_strings; | 116 std::vector<std::string> url_substr; |
162 SetLocalizedStrings(&localized_strings); | 117 base::SplitString(path, '/', &url_substr); |
163 SetFontAndTextDirection(&localized_strings); | 118 int page_index = 0; |
164 | 119 if (url_substr.size() == 3 && base::StringToInt(url_substr[1], &page_index)) { |
165 static const base::StringPiece print_html( | 120 PrintPreviewDataService::GetInstance()->GetDataEntry( |
166 ResourceBundle::GetSharedInstance().GetRawDataResource( | 121 url_substr[0], page_index, &data); |
167 IDR_PRINT_PREVIEW_HTML)); | 122 } |
168 std::string full_html = jstemplate_builder::GetI18nTemplateHtml( | 123 if (data.get()) { |
169 print_html, &localized_strings); | |
170 | |
171 SendResponse(request_id, base::RefCountedString::TakeString(&full_html)); | |
172 return; | |
173 } else if (preview_data_requested && data.get()) { | |
174 // Print Preview data. | |
175 SendResponse(request_id, data); | 124 SendResponse(request_id, data); |
176 return; | 125 return; |
177 } else { | |
178 // Invalid request. | |
179 scoped_refptr<RefCountedBytes> empty_bytes(new RefCountedBytes); | |
180 SendResponse(request_id, empty_bytes); | |
181 return; | |
182 } | 126 } |
| 127 // Invalid request. |
| 128 scoped_refptr<RefCountedBytes> empty_bytes(new RefCountedBytes); |
| 129 SendResponse(request_id, empty_bytes); |
183 } | 130 } |
184 | |
185 std::string PrintPreviewDataSource::GetMimeType(const std::string& path) const { | |
186 return path.empty() ? "text/html" : "application/pdf"; | |
187 } | |
OLD | NEW |