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_ui_html_source.h" | |
6 | |
7 #include <algorithm> | |
8 #include <vector> | |
9 | |
10 #include "base/message_loop.h" | |
11 #include "base/shared_memory.h" | |
12 #include "base/string_piece.h" | |
13 #include "base/utf_string_conversions.h" | |
14 #include "base/values.h" | |
15 #include "chrome/common/jstemplate_builder.h" | |
16 #include "chrome/common/url_constants.h" | |
17 #include "grit/browser_resources.h" | |
18 #include "grit/chromium_strings.h" | |
19 #include "grit/generated_resources.h" | |
20 #include "grit/google_chrome_strings.h" | |
21 #include "ui/base/l10n/l10n_util.h" | |
22 #include "ui/base/resource/resource_bundle.h" | |
23 | |
24 namespace { | |
25 | |
26 void SetLocalizedStrings(DictionaryValue* localized_strings) { | |
27 localized_strings->SetString(std::string("title"), | |
28 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_TITLE)); | |
29 localized_strings->SetString(std::string("loading"), | |
30 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_LOADING)); | |
31 #if defined(GOOGLE_CHROME_BUILD) | |
32 localized_strings->SetString(std::string("noPlugin"), | |
33 l10n_util::GetStringFUTF8(IDS_PRINT_PREVIEW_NO_PLUGIN, | |
34 ASCIIToUTF16("chrome://plugins/"))); | |
35 #else | |
36 localized_strings->SetString(std::string("noPlugin"), | |
37 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_NO_PLUGIN)); | |
38 #endif | |
39 localized_strings->SetString(std::string("previewFailed"), | |
40 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_FAILED)); | |
41 localized_strings->SetString(std::string("initiatorTabClosed"), | |
42 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_INITIATOR_TAB_CLOSED)); | |
43 localized_strings->SetString(std::string("reopenPage"), | |
44 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_REOPEN_PAGE)); | |
45 | |
46 localized_strings->SetString(std::string("printButton"), | |
47 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_PRINT_BUTTON)); | |
48 localized_strings->SetString(std::string("cancelButton"), | |
49 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_CANCEL_BUTTON)); | |
50 localized_strings->SetString(std::string("printing"), | |
51 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_PRINTING)); | |
52 | |
53 localized_strings->SetString(std::string("destinationLabel"), | |
54 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_DESTINATION_LABEL)); | |
55 localized_strings->SetString(std::string("copiesLabel"), | |
56 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_COPIES_LABEL)); | |
57 localized_strings->SetString(std::string("examplePageRangeText"), | |
58 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_EXAMPLE_PAGE_RANGE_TEXT)); | |
59 localized_strings->SetString(std::string("invalidNumberOfCopies"), | |
60 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_INVALID_NUMBER_OF_COPIES)); | |
61 localized_strings->SetString(std::string("layoutLabel"), | |
62 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_LAYOUT_LABEL)); | |
63 localized_strings->SetString(std::string("optionAllPages"), | |
64 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_OPTION_ALL_PAGES)); | |
65 localized_strings->SetString(std::string("optionBw"), | |
66 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_OPTION_BW)); | |
67 localized_strings->SetString(std::string("optionCollate"), | |
68 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_OPTION_COLLATE)); | |
69 localized_strings->SetString(std::string("optionColor"), | |
70 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_OPTION_COLOR)); | |
71 localized_strings->SetString(std::string("optionLandscape"), | |
72 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_OPTION_LANDSCAPE)); | |
73 localized_strings->SetString(std::string("optionPortrait"), | |
74 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_OPTION_PORTRAIT)); | |
75 localized_strings->SetString(std::string("optionTwoSided"), | |
76 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_OPTION_TWO_SIDED)); | |
77 localized_strings->SetString(std::string("pagesLabel"), | |
78 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_PAGES_LABEL)); | |
79 localized_strings->SetString(std::string("pageRangeTextBox"), | |
80 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_PAGE_RANGE_TEXT)); | |
81 localized_strings->SetString(std::string("pageRangeRadio"), | |
82 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_PAGE_RANGE_RADIO)); | |
83 localized_strings->SetString(std::string("printToPDF"), | |
84 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_PRINT_TO_PDF)); | |
85 localized_strings->SetString(std::string("printPreviewTitleFormat"), | |
86 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_TITLE_FORMAT)); | |
87 localized_strings->SetString(std::string("printPreviewSummaryFormatShort"), | |
88 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_SUMMARY_FORMAT_SHORT)); | |
89 localized_strings->SetString(std::string("printPreviewSummaryFormatLong"), | |
90 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_SUMMARY_FORMAT_LONG)); | |
91 localized_strings->SetString(std::string("printPreviewSheetsLabelSingular"), | |
92 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_SHEETS_LABEL_SINGULAR)); | |
93 localized_strings->SetString(std::string("printPreviewSheetsLabelPlural"), | |
94 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_SHEETS_LABEL_PLURAL)); | |
95 localized_strings->SetString(std::string("printPreviewPageLabelSingular"), | |
96 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_PAGE_LABEL_SINGULAR)); | |
97 localized_strings->SetString(std::string("printPreviewPageLabelPlural"), | |
98 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_PAGE_LABEL_PLURAL)); | |
99 localized_strings->SetString(std::string("systemDialogOption"), | |
100 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_SYSTEM_DIALOG_OPTION)); | |
101 localized_strings->SetString(std::string("pageRangeInstruction"), | |
102 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_PAGE_RANGE_INSTRUCTION)); | |
103 localized_strings->SetString(std::string("copiesInstruction"), | |
104 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_COPIES_INSTRUCTION)); | |
105 localized_strings->SetString(std::string("managePrinters"), | |
106 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_MANAGE_PRINTERS)); | |
107 localized_strings->SetString(std::string("didYouMean"), | |
108 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_DID_YOU_MEAN)); | |
109 } | |
110 | |
111 } // namespace | |
112 | |
113 PrintPreviewUIHTMLSource::PrintPreviewUIHTMLSource() | |
114 : DataSource(chrome::kChromeUIPrintHost, MessageLoop::current()), | |
115 data_(std::make_pair(static_cast<base::SharedMemory*>(NULL), 0U)) { | |
116 } | |
117 | |
118 PrintPreviewUIHTMLSource::~PrintPreviewUIHTMLSource() { | |
119 delete data_.first; | |
120 } | |
121 | |
122 void PrintPreviewUIHTMLSource::GetPrintPreviewData(PrintPreviewData* data) { | |
123 *data = data_; | |
124 } | |
125 | |
126 void PrintPreviewUIHTMLSource::SetPrintPreviewData( | |
127 const PrintPreviewData& data) { | |
128 delete data_.first; | |
129 data_ = data; | |
130 } | |
131 | |
132 void PrintPreviewUIHTMLSource::StartDataRequest(const std::string& path, | |
133 bool is_incognito, | |
134 int request_id) { | |
135 if (path.empty()) { | |
136 // Print Preview Index page. | |
137 DictionaryValue localized_strings; | |
138 SetLocalizedStrings(&localized_strings); | |
139 SetFontAndTextDirection(&localized_strings); | |
140 | |
141 static const base::StringPiece print_html( | |
142 ResourceBundle::GetSharedInstance().GetRawDataResource( | |
143 IDR_PRINT_PREVIEW_HTML)); | |
144 const std::string full_html = jstemplate_builder::GetI18nTemplateHtml( | |
145 print_html, &localized_strings); | |
146 | |
147 scoped_refptr<RefCountedBytes> html_bytes(new RefCountedBytes); | |
148 html_bytes->data.resize(full_html.size()); | |
149 std::copy(full_html.begin(), full_html.end(), html_bytes->data.begin()); | |
150 | |
151 SendResponse(request_id, html_bytes); | |
152 return; | |
153 } else if (path == "print.pdf" && data_.first) { | |
154 // Print Preview data. | |
155 char* preview_data = reinterpret_cast<char*>(data_.first->memory()); | |
156 uint32 preview_data_size = data_.second; | |
157 | |
158 scoped_refptr<RefCountedBytes> html_bytes(new RefCountedBytes); | |
159 html_bytes->data.resize(preview_data_size); | |
160 std::vector<unsigned char>::iterator it = html_bytes->data.begin(); | |
161 for (uint32 i = 0; i < preview_data_size; ++i, ++it) | |
162 *it = *(preview_data + i); | |
163 SendResponse(request_id, html_bytes); | |
164 return; | |
165 } else { | |
166 // Invalid request. | |
167 scoped_refptr<RefCountedBytes> empty_bytes(new RefCountedBytes); | |
168 SendResponse(request_id, empty_bytes); | |
169 return; | |
170 } | |
171 } | |
172 | |
173 std::string PrintPreviewUIHTMLSource::GetMimeType( | |
174 const std::string& path) const { | |
175 // Print Preview Index page. | |
176 if (path.empty()) | |
177 return "text/html"; | |
178 // Print Preview data. | |
179 return "application/pdf"; | |
180 } | |
OLD | NEW |