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 } | |
108 | |
109 } // namespace | |
110 | |
111 PrintPreviewUIHTMLSource::PrintPreviewUIHTMLSource() | |
112 : DataSource(chrome::kChromeUIPrintHost, MessageLoop::current()), | |
113 data_(std::make_pair(static_cast<base::SharedMemory*>(NULL), 0U)) { | |
114 } | |
115 | |
116 PrintPreviewUIHTMLSource::~PrintPreviewUIHTMLSource() { | |
117 delete data_.first; | |
118 } | |
119 | |
120 void PrintPreviewUIHTMLSource::GetPrintPreviewData(PrintPreviewData* data) { | |
121 *data = data_; | |
122 } | |
123 | |
124 void PrintPreviewUIHTMLSource::SetPrintPreviewData( | |
125 const PrintPreviewData& data) { | |
126 delete data_.first; | |
127 data_ = data; | |
128 } | |
129 | |
130 void PrintPreviewUIHTMLSource::StartDataRequest(const std::string& path, | |
131 bool is_incognito, | |
132 int request_id) { | |
133 if (path.empty()) { | |
134 // Print Preview Index page. | |
135 DictionaryValue localized_strings; | |
136 SetLocalizedStrings(&localized_strings); | |
137 SetFontAndTextDirection(&localized_strings); | |
138 | |
139 static const base::StringPiece print_html( | |
140 ResourceBundle::GetSharedInstance().GetRawDataResource( | |
141 IDR_PRINT_PREVIEW_HTML)); | |
142 const std::string full_html = jstemplate_builder::GetI18nTemplateHtml( | |
143 print_html, &localized_strings); | |
144 | |
145 scoped_refptr<RefCountedBytes> html_bytes(new RefCountedBytes); | |
146 html_bytes->data.resize(full_html.size()); | |
147 std::copy(full_html.begin(), full_html.end(), html_bytes->data.begin()); | |
148 | |
149 SendResponse(request_id, html_bytes); | |
150 return; | |
151 } else if (path == "print.pdf" && data_.first) { | |
152 // Print Preview data. | |
153 char* preview_data = reinterpret_cast<char*>(data_.first->memory()); | |
154 uint32 preview_data_size = data_.second; | |
155 | |
156 scoped_refptr<RefCountedBytes> html_bytes(new RefCountedBytes); | |
157 html_bytes->data.resize(preview_data_size); | |
158 std::vector<unsigned char>::iterator it = html_bytes->data.begin(); | |
159 for (uint32 i = 0; i < preview_data_size; ++i, ++it) | |
160 *it = *(preview_data + i); | |
161 SendResponse(request_id, html_bytes); | |
162 return; | |
163 } else { | |
164 // Invalid request. | |
165 scoped_refptr<RefCountedBytes> empty_bytes(new RefCountedBytes); | |
166 SendResponse(request_id, empty_bytes); | |
167 return; | |
168 } | |
169 } | |
170 | |
171 std::string PrintPreviewUIHTMLSource::GetMimeType( | |
172 const std::string& path) const { | |
173 // Print Preview Index page. | |
174 if (path.empty()) | |
175 return "text/html"; | |
176 // Print Preview data. | |
177 return "application/pdf"; | |
178 } | |
OLD | NEW |