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_ui_html_source.h" | 5 #include "chrome/browser/ui/webui/print_preview_data_source.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <vector> | |
9 | 8 |
10 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
11 #include "base/shared_memory.h" | |
12 #include "base/string_piece.h" | 10 #include "base/string_piece.h" |
| 11 #include "base/string_util.h" |
13 #include "base/utf_string_conversions.h" | 12 #include "base/utf_string_conversions.h" |
14 #include "base/values.h" | 13 #include "base/values.h" |
| 14 #include "chrome/browser/printing/print_preview_data_service.h" |
15 #include "chrome/common/jstemplate_builder.h" | 15 #include "chrome/common/jstemplate_builder.h" |
16 #include "chrome/common/url_constants.h" | 16 #include "chrome/common/url_constants.h" |
17 #include "grit/browser_resources.h" | 17 #include "grit/browser_resources.h" |
18 #include "grit/chromium_strings.h" | 18 #include "grit/chromium_strings.h" |
19 #include "grit/generated_resources.h" | 19 #include "grit/generated_resources.h" |
20 #include "grit/google_chrome_strings.h" | 20 #include "grit/google_chrome_strings.h" |
21 #include "ui/base/l10n/l10n_util.h" | 21 #include "ui/base/l10n/l10n_util.h" |
22 #include "ui/base/resource/resource_bundle.h" | 22 #include "ui/base/resource/resource_bundle.h" |
23 | 23 |
24 namespace { | 24 namespace { |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
101 localized_strings->SetString(std::string("pageRangeInstruction"), | 101 localized_strings->SetString(std::string("pageRangeInstruction"), |
102 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_PAGE_RANGE_INSTRUCTION)); | 102 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_PAGE_RANGE_INSTRUCTION)); |
103 localized_strings->SetString(std::string("copiesInstruction"), | 103 localized_strings->SetString(std::string("copiesInstruction"), |
104 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_COPIES_INSTRUCTION)); | 104 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_COPIES_INSTRUCTION)); |
105 localized_strings->SetString(std::string("managePrinters"), | 105 localized_strings->SetString(std::string("managePrinters"), |
106 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_MANAGE_PRINTERS)); | 106 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_MANAGE_PRINTERS)); |
107 } | 107 } |
108 | 108 |
109 } // namespace | 109 } // namespace |
110 | 110 |
111 PrintPreviewUIHTMLSource::PrintPreviewUIHTMLSource() | 111 PrintPreviewDataSource::PrintPreviewDataSource() |
112 : DataSource(chrome::kChromeUIPrintHost, MessageLoop::current()), | 112 : DataSource(chrome::kChromeUIPrintHost, MessageLoop::current()) { |
113 data_(std::make_pair(static_cast<base::SharedMemory*>(NULL), 0U)) { | |
114 } | 113 } |
115 | 114 |
116 PrintPreviewUIHTMLSource::~PrintPreviewUIHTMLSource() { | 115 PrintPreviewDataSource::~PrintPreviewDataSource() { |
117 delete data_.first; | |
118 } | 116 } |
119 | 117 |
120 void PrintPreviewUIHTMLSource::GetPrintPreviewData(PrintPreviewData* data) { | 118 void PrintPreviewDataSource::StartDataRequest(const std::string& path, |
121 *data = data_; | 119 bool is_incognito, |
122 } | 120 int request_id) { |
| 121 scoped_refptr<RefCountedBytes> data(new RefCountedBytes); |
123 | 122 |
124 void PrintPreviewUIHTMLSource::SetPrintPreviewData( | 123 bool preview_data_requested = EndsWith(path, "/print.pdf", true); |
125 const PrintPreviewData& data) { | 124 if (preview_data_requested) { |
126 delete data_.first; | 125 size_t index = path.rfind("/print.pdf"); |
127 data_ = data; | 126 PrintPreviewDataService::GetInstance()->GetDataEntry(path.substr(0, index), |
128 } | 127 &data); |
| 128 } |
129 | 129 |
130 void PrintPreviewUIHTMLSource::StartDataRequest(const std::string& path, | |
131 bool is_incognito, | |
132 int request_id) { | |
133 if (path.empty()) { | 130 if (path.empty()) { |
134 // Print Preview Index page. | 131 // Print Preview Index page. |
135 DictionaryValue localized_strings; | 132 DictionaryValue localized_strings; |
136 SetLocalizedStrings(&localized_strings); | 133 SetLocalizedStrings(&localized_strings); |
137 SetFontAndTextDirection(&localized_strings); | 134 SetFontAndTextDirection(&localized_strings); |
138 | 135 |
139 static const base::StringPiece print_html( | 136 static const base::StringPiece print_html( |
140 ResourceBundle::GetSharedInstance().GetRawDataResource( | 137 ResourceBundle::GetSharedInstance().GetRawDataResource( |
141 IDR_PRINT_PREVIEW_HTML)); | 138 IDR_PRINT_PREVIEW_HTML)); |
142 const std::string full_html = jstemplate_builder::GetI18nTemplateHtml( | 139 const std::string full_html = jstemplate_builder::GetI18nTemplateHtml( |
143 print_html, &localized_strings); | 140 print_html, &localized_strings); |
144 | 141 |
145 scoped_refptr<RefCountedBytes> html_bytes(new RefCountedBytes); | 142 scoped_refptr<RefCountedBytes> html_bytes(new RefCountedBytes); |
146 html_bytes->data.resize(full_html.size()); | 143 html_bytes->data.resize(full_html.size()); |
147 std::copy(full_html.begin(), full_html.end(), html_bytes->data.begin()); | 144 std::copy(full_html.begin(), full_html.end(), html_bytes->data.begin()); |
148 | 145 |
149 SendResponse(request_id, html_bytes); | 146 SendResponse(request_id, html_bytes); |
150 return; | 147 return; |
151 } else if (path == "print.pdf" && data_.first) { | 148 } else if (preview_data_requested && data->front()) { |
152 // Print Preview data. | 149 // Print Preview data. |
153 char* preview_data = reinterpret_cast<char*>(data_.first->memory()); | 150 SendResponse(request_id, data); |
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; | 151 return; |
163 } else { | 152 } else { |
164 // Invalid request. | 153 // Invalid request. |
165 scoped_refptr<RefCountedBytes> empty_bytes(new RefCountedBytes); | 154 scoped_refptr<RefCountedBytes> empty_bytes(new RefCountedBytes); |
166 SendResponse(request_id, empty_bytes); | 155 SendResponse(request_id, empty_bytes); |
167 return; | 156 return; |
168 } | 157 } |
169 } | 158 } |
170 | 159 |
171 std::string PrintPreviewUIHTMLSource::GetMimeType( | 160 std::string PrintPreviewDataSource::GetMimeType(const std::string& path) const { |
172 const std::string& path) const { | |
173 // Print Preview Index page. | |
174 if (path.empty()) | 161 if (path.empty()) |
175 return "text/html"; | 162 return "text/html"; // Print Preview Index Page. |
176 // Print Preview data. | 163 return "application/pdf"; // Print Preview data |
177 return "application/pdf"; | |
178 } | 164 } |
OLD | NEW |