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/values.h"
14 #include "chrome/common/jstemplate_builder.h"
15 #include "chrome/common/url_constants.h"
16 #include "grit/browser_resources.h"
17 #include "grit/generated_resources.h"
18 #include "ui/base/l10n/l10n_util.h"
19 #include "ui/base/resource/resource_bundle.h"
20
21 namespace {
22
SetLocalizedStrings(DictionaryValue * localized_strings)23 void SetLocalizedStrings(DictionaryValue* localized_strings) {
24 localized_strings->SetString(std::string("title"),
25 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_TITLE));
26 localized_strings->SetString(std::string("loading"),
27 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_LOADING));
28 localized_strings->SetString(std::string("noPlugin"),
29 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_NO_PLUGIN));
30
31 localized_strings->SetString(std::string("printButton"),
32 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_PRINT_BUTTON));
33 localized_strings->SetString(std::string("cancelButton"),
34 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_CANCEL_BUTTON));
35
36 localized_strings->SetString(std::string("destinationLabel"),
37 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_DESTINATION_LABEL));
38 localized_strings->SetString(std::string("copiesLabel"),
39 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_COPIES_LABEL));
40 localized_strings->SetString(std::string("examplePageRangeText"),
41 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_EXAMPLE_PAGE_RANGE_TEXT));
42 localized_strings->SetString(std::string("invalidNumberOfCopiesTitleToolTip"),
43 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_INVALID_COPIES_TOOL_TIP));
44 localized_strings->SetString(std::string("layoutLabel"),
45 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_LAYOUT_LABEL));
46 localized_strings->SetString(std::string("optionAllPages"),
47 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_OPTION_ALL_PAGES));
48 localized_strings->SetString(std::string("optionBw"),
49 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_OPTION_BW));
50 localized_strings->SetString(std::string("optionCollate"),
51 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_OPTION_COLLATE));
52 localized_strings->SetString(std::string("optionColor"),
53 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_OPTION_COLOR));
54 localized_strings->SetString(std::string("optionLandscape"),
55 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_OPTION_LANDSCAPE));
56 localized_strings->SetString(std::string("optionPortrait"),
57 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_OPTION_PORTRAIT));
58 localized_strings->SetString(std::string("optionTwoSided"),
59 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_OPTION_TWO_SIDED));
60 localized_strings->SetString(std::string("bindingLabel"),
61 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_BINDING_LABEL));
62 localized_strings->SetString(std::string("optionLongEdgeBinding"),
63 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_OPTION_LONG_EDGE_BINDING));
64 localized_strings->SetString(std::string("optionShortEdgeBinding"),
65 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_OPTION_SHORT_EDGE_BINDING));
66 localized_strings->SetString(std::string("pagesLabel"),
67 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_PAGES_LABEL));
68 localized_strings->SetString(std::string("pageRangeTextBox"),
69 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_PAGE_RANGE_TEXT));
70 localized_strings->SetString(std::string("pageRangeRadio"),
71 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_PAGE_RANGE_RADIO));
72 localized_strings->SetString(std::string("pageRangeInvalidTitleToolTip"),
73 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_PAGE_RANGE_INVALID_TOOL_TIP));
74 localized_strings->SetString(std::string("printToPDF"),
75 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_PRINT_TO_PDF));
76 localized_strings->SetString(std::string("printPreviewTitleFormat"),
77 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_TITLE_FORMAT));
78 localized_strings->SetString(std::string("printPreviewSummaryFormat"),
79 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_SUMMARY_FORMAT));
80 localized_strings->SetString(std::string("printPreviewSheetsLabel"),
81 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_SHEETS_LABEL));
82 localized_strings->SetString(std::string("printPreviewPageLabelSingular"),
83 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_PAGE_LABEL_SINGULAR));
84 localized_strings->SetString(std::string("printPreviewPageLabelPlural"),
85 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_PAGE_LABEL_PLURAL));
86 localized_strings->SetString(std::string("systemDialogOption"),
87 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_SYSTEM_DIALOG_OPTION));
88 }
89
90 } // namespace
91
PrintPreviewUIHTMLSource()92 PrintPreviewUIHTMLSource::PrintPreviewUIHTMLSource()
93 : DataSource(chrome::kChromeUIPrintHost, MessageLoop::current()),
94 data_(std::make_pair(static_cast<base::SharedMemory*>(NULL), 0U)) {
95 }
96
~PrintPreviewUIHTMLSource()97 PrintPreviewUIHTMLSource::~PrintPreviewUIHTMLSource() {
98 delete data_.first;
99 }
100
GetPrintPreviewData(PrintPreviewData * data)101 void PrintPreviewUIHTMLSource::GetPrintPreviewData(PrintPreviewData* data) {
102 *data = data_;
103 }
104
SetPrintPreviewData(const PrintPreviewData & data)105 void PrintPreviewUIHTMLSource::SetPrintPreviewData(
106 const PrintPreviewData& data) {
107 delete data_.first;
108 data_ = data;
109 }
110
StartDataRequest(const std::string & path,bool is_incognito,int request_id)111 void PrintPreviewUIHTMLSource::StartDataRequest(const std::string& path,
112 bool is_incognito,
113 int request_id) {
114 if (path.empty()) {
115 // Print Preview Index page.
116 DictionaryValue localized_strings;
117 SetLocalizedStrings(&localized_strings);
118 SetFontAndTextDirection(&localized_strings);
119
120 static const base::StringPiece print_html(
121 ResourceBundle::GetSharedInstance().GetRawDataResource(
122 IDR_PRINT_PREVIEW_HTML));
123 const std::string full_html = jstemplate_builder::GetI18nTemplateHtml(
124 print_html, &localized_strings);
125
126 scoped_refptr<RefCountedBytes> html_bytes(new RefCountedBytes);
127 html_bytes->data.resize(full_html.size());
128 std::copy(full_html.begin(), full_html.end(), html_bytes->data.begin());
129
130 SendResponse(request_id, html_bytes);
131 return;
132 } else if (path == "print.pdf" && data_.first) {
133 // Print Preview data.
134 char* preview_data = reinterpret_cast<char*>(data_.first->memory());
135 uint32 preview_data_size = data_.second;
136
137 scoped_refptr<RefCountedBytes> html_bytes(new RefCountedBytes);
138 html_bytes->data.resize(preview_data_size);
139 std::vector<unsigned char>::iterator it = html_bytes->data.begin();
140 for (uint32 i = 0; i < preview_data_size; ++i, ++it)
141 *it = *(preview_data + i);
142 SendResponse(request_id, html_bytes);
143 return;
144 } else {
145 // Invalid request.
146 scoped_refptr<RefCountedBytes> empty_bytes(new RefCountedBytes);
147 SendResponse(request_id, empty_bytes);
148 return;
149 }
150 }
151
GetMimeType(const std::string & path) const152 std::string PrintPreviewUIHTMLSource::GetMimeType(
153 const std::string& path) const {
154 // Print Preview Index page.
155 if (path.empty())
156 return "text/html";
157 // Print Preview data.
158 return "application/pdf";
159 }
160