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