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.
5 #include "chrome/browser/ui/webui/print_preview_data_source.h"
9 #include "base/message_loop.h"
10 #include "base/string_piece.h"
11 #include "base/string_util.h"
12 #include "base/utf_string_conversions.h"
13 #include "base/values.h"
14 #include "chrome/browser/printing/print_preview_data_service.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"
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/")));
36 localized_strings
->SetString(std::string("noPlugin"),
37 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_NO_PLUGIN
));
39 localized_strings
->SetString(std::string("launchNativeDialog"),
40 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_NATIVE_DIALOG
));
41 localized_strings
->SetString(std::string("previewFailed"),
42 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_FAILED
));
43 localized_strings
->SetString(std::string("initiatorTabClosed"),
44 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_INITIATOR_TAB_CLOSED
));
45 localized_strings
->SetString(std::string("reopenPage"),
46 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_REOPEN_PAGE
));
48 localized_strings
->SetString(std::string("printButton"),
49 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_PRINT_BUTTON
));
50 localized_strings
->SetString(std::string("cancelButton"),
51 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_CANCEL_BUTTON
));
52 localized_strings
->SetString(std::string("printing"),
53 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_PRINTING
));
55 localized_strings
->SetString(std::string("destinationLabel"),
56 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_DESTINATION_LABEL
));
57 localized_strings
->SetString(std::string("copiesLabel"),
58 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_COPIES_LABEL
));
59 localized_strings
->SetString(std::string("examplePageRangeText"),
60 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_EXAMPLE_PAGE_RANGE_TEXT
));
61 localized_strings
->SetString(std::string("invalidNumberOfCopies"),
62 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_INVALID_NUMBER_OF_COPIES
));
63 localized_strings
->SetString(std::string("layoutLabel"),
64 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_LAYOUT_LABEL
));
65 localized_strings
->SetString(std::string("optionAllPages"),
66 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_OPTION_ALL_PAGES
));
67 localized_strings
->SetString(std::string("optionBw"),
68 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_OPTION_BW
));
69 localized_strings
->SetString(std::string("optionCollate"),
70 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_OPTION_COLLATE
));
71 localized_strings
->SetString(std::string("optionColor"),
72 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_OPTION_COLOR
));
73 localized_strings
->SetString(std::string("optionLandscape"),
74 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_OPTION_LANDSCAPE
));
75 localized_strings
->SetString(std::string("optionPortrait"),
76 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_OPTION_PORTRAIT
));
77 localized_strings
->SetString(std::string("optionTwoSided"),
78 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_OPTION_TWO_SIDED
));
79 localized_strings
->SetString(std::string("pagesLabel"),
80 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_PAGES_LABEL
));
81 localized_strings
->SetString(std::string("pageRangeTextBox"),
82 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_PAGE_RANGE_TEXT
));
83 localized_strings
->SetString(std::string("pageRangeRadio"),
84 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_PAGE_RANGE_RADIO
));
85 localized_strings
->SetString(std::string("printToPDF"),
86 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_PRINT_TO_PDF
));
87 localized_strings
->SetString(std::string("printPreviewTitleFormat"),
88 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_TITLE_FORMAT
));
89 localized_strings
->SetString(std::string("printPreviewSummaryFormatShort"),
90 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_SUMMARY_FORMAT_SHORT
));
91 localized_strings
->SetString(std::string("printPreviewSummaryFormatLong"),
92 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_SUMMARY_FORMAT_LONG
));
93 localized_strings
->SetString(std::string("printPreviewSheetsLabelSingular"),
94 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_SHEETS_LABEL_SINGULAR
));
95 localized_strings
->SetString(std::string("printPreviewSheetsLabelPlural"),
96 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_SHEETS_LABEL_PLURAL
));
97 localized_strings
->SetString(std::string("printPreviewPageLabelSingular"),
98 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_PAGE_LABEL_SINGULAR
));
99 localized_strings
->SetString(std::string("printPreviewPageLabelPlural"),
100 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_PAGE_LABEL_PLURAL
));
101 localized_strings
->SetString(std::string("systemDialogOption"),
102 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_SYSTEM_DIALOG_OPTION
));
103 localized_strings
->SetString(std::string("pageRangeInstruction"),
104 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_PAGE_RANGE_INSTRUCTION
));
105 localized_strings
->SetString(std::string("copiesInstruction"),
106 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_COPIES_INSTRUCTION
));
107 localized_strings
->SetString(std::string("managePrinters"),
108 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_MANAGE_PRINTERS
));
113 PrintPreviewDataSource::PrintPreviewDataSource()
114 : DataSource(chrome::kChromeUIPrintHost
, MessageLoop::current()) {
117 PrintPreviewDataSource::~PrintPreviewDataSource() {
120 void PrintPreviewDataSource::StartDataRequest(const std::string
& path
,
123 scoped_refptr
<RefCountedBytes
> data(new RefCountedBytes
);
125 bool preview_data_requested
= EndsWith(path
, "/print.pdf", true);
126 if (preview_data_requested
) {
127 size_t index
= path
.rfind("/print.pdf");
128 PrintPreviewDataService::GetInstance()->GetDataEntry(path
.substr(0, index
),
133 // Print Preview Index page.
134 DictionaryValue localized_strings
;
135 SetLocalizedStrings(&localized_strings
);
136 SetFontAndTextDirection(&localized_strings
);
138 static const base::StringPiece
print_html(
139 ResourceBundle::GetSharedInstance().GetRawDataResource(
140 IDR_PRINT_PREVIEW_HTML
));
141 const std::string full_html
= jstemplate_builder::GetI18nTemplateHtml(
142 print_html
, &localized_strings
);
144 scoped_refptr
<RefCountedBytes
> html_bytes(new RefCountedBytes
);
145 html_bytes
->data
.resize(full_html
.size());
146 std::copy(full_html
.begin(), full_html
.end(), html_bytes
->data
.begin());
148 SendResponse(request_id
, html_bytes
);
150 } else if (preview_data_requested
&& data
->front()) {
151 // Print Preview data.
152 SendResponse(request_id
, data
);
156 scoped_refptr
<RefCountedBytes
> empty_bytes(new RefCountedBytes
);
157 SendResponse(request_id
, empty_bytes
);
162 std::string
PrintPreviewDataSource::GetMimeType(const std::string
& path
) const {
164 return "text/html"; // Print Preview Index Page.
165 return "application/pdf"; // Print Preview data