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" |
13 #include "base/utf_string_conversions.h" | 11 #include "base/utf_string_conversions.h" |
14 #include "base/values.h" | 12 #include "base/values.h" |
13 #include "chrome/browser/printing/print_preview_data_service_factory.h" | |
14 #include "chrome/browser/profiles/profile.h" | |
Lei Zhang
2011/05/27 17:26:53
nit: ordering
kmadhusu
2011/05/27 23:44:42
Fixed.
| |
15 #include "chrome/browser/printing/print_preview_data_service.h" | |
15 #include "chrome/common/jstemplate_builder.h" | 16 #include "chrome/common/jstemplate_builder.h" |
16 #include "chrome/common/url_constants.h" | 17 #include "chrome/common/url_constants.h" |
17 #include "grit/browser_resources.h" | 18 #include "grit/browser_resources.h" |
18 #include "grit/chromium_strings.h" | 19 #include "grit/chromium_strings.h" |
19 #include "grit/generated_resources.h" | 20 #include "grit/generated_resources.h" |
20 #include "grit/google_chrome_strings.h" | 21 #include "grit/google_chrome_strings.h" |
21 #include "ui/base/l10n/l10n_util.h" | 22 #include "ui/base/l10n/l10n_util.h" |
22 #include "ui/base/resource/resource_bundle.h" | 23 #include "ui/base/resource/resource_bundle.h" |
23 | 24 |
24 namespace { | 25 namespace { |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
103 localized_strings->SetString(std::string("copiesInstruction"), | 104 localized_strings->SetString(std::string("copiesInstruction"), |
104 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_COPIES_INSTRUCTION)); | 105 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_COPIES_INSTRUCTION)); |
105 localized_strings->SetString(std::string("managePrinters"), | 106 localized_strings->SetString(std::string("managePrinters"), |
106 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_MANAGE_PRINTERS)); | 107 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_MANAGE_PRINTERS)); |
107 localized_strings->SetString(std::string("didYouMean"), | 108 localized_strings->SetString(std::string("didYouMean"), |
108 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_DID_YOU_MEAN)); | 109 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_DID_YOU_MEAN)); |
109 } | 110 } |
110 | 111 |
111 } // namespace | 112 } // namespace |
112 | 113 |
113 PrintPreviewUIHTMLSource::PrintPreviewUIHTMLSource() | 114 PrintPreviewDataSource::PrintPreviewDataSource(Profile* profile) |
114 : DataSource(chrome::kChromeUIPrintHost, MessageLoop::current()), | 115 : DataSource(chrome::kChromeUIPrintHost, MessageLoop::current()), |
115 data_(std::make_pair(static_cast<base::SharedMemory*>(NULL), 0U)) { | 116 profile_(profile) { |
116 } | 117 } |
117 | 118 |
118 PrintPreviewUIHTMLSource::~PrintPreviewUIHTMLSource() { | 119 PrintPreviewDataSource::~PrintPreviewDataSource() { |
119 delete data_.first; | |
120 } | 120 } |
121 | 121 |
122 void PrintPreviewUIHTMLSource::GetPrintPreviewData(PrintPreviewData* data) { | 122 void PrintPreviewDataSource::StartDataRequest(const std::string& path, |
123 *data = data_; | 123 bool is_incognito, |
124 } | 124 int request_id) { |
125 scoped_refptr<RefCountedBytes> data(new RefCountedBytes); | |
126 if (path.size() > 10 && path.substr(0, 10) == "print.pdf/") { | |
Lei Zhang
2011/05/27 17:26:53
if you set a boolean to true here, then you don't
kmadhusu
2011/05/27 23:44:42
Done.
| |
127 PrintPreviewDataServiceFactory::GetForProfile(profile_)-> | |
128 GetDataEntry(path.substr(10), &data); | |
129 } | |
125 | 130 |
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()) { | 131 if (path.empty()) { |
136 // Print Preview Index page. | 132 // Print Preview Index page. |
137 DictionaryValue localized_strings; | 133 DictionaryValue localized_strings; |
138 SetLocalizedStrings(&localized_strings); | 134 SetLocalizedStrings(&localized_strings); |
139 SetFontAndTextDirection(&localized_strings); | 135 SetFontAndTextDirection(&localized_strings); |
140 | 136 |
141 static const base::StringPiece print_html( | 137 static const base::StringPiece print_html( |
142 ResourceBundle::GetSharedInstance().GetRawDataResource( | 138 ResourceBundle::GetSharedInstance().GetRawDataResource( |
143 IDR_PRINT_PREVIEW_HTML)); | 139 IDR_PRINT_PREVIEW_HTML)); |
144 const std::string full_html = jstemplate_builder::GetI18nTemplateHtml( | 140 const std::string full_html = jstemplate_builder::GetI18nTemplateHtml( |
145 print_html, &localized_strings); | 141 print_html, &localized_strings); |
146 | 142 |
147 scoped_refptr<RefCountedBytes> html_bytes(new RefCountedBytes); | 143 scoped_refptr<RefCountedBytes> html_bytes(new RefCountedBytes); |
148 html_bytes->data.resize(full_html.size()); | 144 html_bytes->data.resize(full_html.size()); |
149 std::copy(full_html.begin(), full_html.end(), html_bytes->data.begin()); | 145 std::copy(full_html.begin(), full_html.end(), html_bytes->data.begin()); |
150 | 146 |
151 SendResponse(request_id, html_bytes); | 147 SendResponse(request_id, html_bytes); |
152 return; | 148 return; |
153 } else if (path == "print.pdf" && data_.first) { | 149 } else if (path.size() > 10 && |
150 path.substr(0, 10) == "print.pdf/" && | |
151 data->front()) { | |
154 // Print Preview data. | 152 // Print Preview data. |
155 char* preview_data = reinterpret_cast<char*>(data_.first->memory()); | 153 SendResponse(request_id, data); |
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; | 154 return; |
165 } else { | 155 } else { |
166 // Invalid request. | 156 // Invalid request. |
167 scoped_refptr<RefCountedBytes> empty_bytes(new RefCountedBytes); | 157 scoped_refptr<RefCountedBytes> empty_bytes(new RefCountedBytes); |
168 SendResponse(request_id, empty_bytes); | 158 SendResponse(request_id, empty_bytes); |
169 return; | 159 return; |
170 } | 160 } |
171 } | 161 } |
172 | 162 |
173 std::string PrintPreviewUIHTMLSource::GetMimeType( | 163 std::string PrintPreviewDataSource::GetMimeType(const std::string& path) const { |
174 const std::string& path) const { | |
175 // Print Preview Index page. | |
176 if (path.empty()) | 164 if (path.empty()) |
177 return "text/html"; | 165 return "text/html"; // Print Preview Index Page. |
178 // Print Preview data. | 166 return "application/pdf"; // Print Preview data |
179 return "application/pdf"; | |
180 } | 167 } |
OLD | NEW |