Chromium Code Reviews| 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/memory/singleton.h" | |
| 10 #include "base/message_loop.h" | 11 #include "base/message_loop.h" |
| 11 #include "base/shared_memory.h" | 12 #include "base/shared_memory.h" |
| 12 #include "base/string_piece.h" | 13 #include "base/string_piece.h" |
| 13 #include "base/utf_string_conversions.h" | 14 #include "base/utf_string_conversions.h" |
| 14 #include "base/values.h" | 15 #include "base/values.h" |
| 16 #include "chrome/browser/profiles/profile.h" | |
| 17 #include "chrome/browser/ui/webui/chrome_url_data_manager.h" | |
| 18 #include "chrome/browser/ui/webui/chrome_url_data_manager_backend.h" | |
| 19 #include "chrome/browser/ui/webui/print_preview_ui.h" | |
| 15 #include "chrome/common/jstemplate_builder.h" | 20 #include "chrome/common/jstemplate_builder.h" |
| 16 #include "chrome/common/url_constants.h" | 21 #include "chrome/common/url_constants.h" |
| 17 #include "grit/browser_resources.h" | 22 #include "grit/browser_resources.h" |
| 18 #include "grit/chromium_strings.h" | 23 #include "grit/chromium_strings.h" |
| 19 #include "grit/generated_resources.h" | 24 #include "grit/generated_resources.h" |
| 20 #include "grit/google_chrome_strings.h" | 25 #include "grit/google_chrome_strings.h" |
| 21 #include "ui/base/l10n/l10n_util.h" | 26 #include "ui/base/l10n/l10n_util.h" |
| 22 #include "ui/base/resource/resource_bundle.h" | 27 #include "ui/base/resource/resource_bundle.h" |
| 23 | 28 |
| 24 namespace { | 29 namespace { |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 103 localized_strings->SetString(std::string("copiesInstruction"), | 108 localized_strings->SetString(std::string("copiesInstruction"), |
| 104 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_COPIES_INSTRUCTION)); | 109 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_COPIES_INSTRUCTION)); |
| 105 localized_strings->SetString(std::string("managePrinters"), | 110 localized_strings->SetString(std::string("managePrinters"), |
| 106 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_MANAGE_PRINTERS)); | 111 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_MANAGE_PRINTERS)); |
| 107 localized_strings->SetString(std::string("didYouMean"), | 112 localized_strings->SetString(std::string("didYouMean"), |
| 108 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_DID_YOU_MEAN)); | 113 l10n_util::GetStringUTF8(IDS_PRINT_PREVIEW_DID_YOU_MEAN)); |
| 109 } | 114 } |
| 110 | 115 |
| 111 } // namespace | 116 } // namespace |
| 112 | 117 |
| 113 PrintPreviewUIHTMLSource::PrintPreviewUIHTMLSource() | 118 PrintPreviewDataSource::PrintPreviewDataSource() |
| 114 : DataSource(chrome::kChromeUIPrintHost, MessageLoop::current()), | 119 : DataSource(chrome::kChromeUIPrintHost, MessageLoop::current()) { |
| 115 data_(std::make_pair(static_cast<base::SharedMemory*>(NULL), 0U)) { | |
| 116 } | 120 } |
| 117 | 121 |
| 118 PrintPreviewUIHTMLSource::~PrintPreviewUIHTMLSource() { | 122 PrintPreviewDataSource::~PrintPreviewDataSource() { |
| 119 delete data_.first; | |
| 120 } | 123 } |
| 121 | 124 |
| 122 void PrintPreviewUIHTMLSource::GetPrintPreviewData(PrintPreviewData* data) { | 125 // static |
| 123 *data = data_; | 126 PrintPreviewDataSource* PrintPreviewDataSource::GetInstance() { |
| 127 // Uses the LeakySingletonTrait because the base class will do the cleanup. | |
| 128 return Singleton<PrintPreviewDataSource, | |
| 129 LeakySingletonTraits<PrintPreviewDataSource> >::get(); | |
| 124 } | 130 } |
| 125 | 131 |
| 126 void PrintPreviewUIHTMLSource::SetPrintPreviewData( | 132 void PrintPreviewDataSource::RemoveEntry( |
| 127 const PrintPreviewData& data) { | 133 const PreviewUIAddrStr& preview_ui_addr_str) { |
| 128 delete data_.first; | 134 PreviewDataSrcMap::iterator it = data_source_map_.find(preview_ui_addr_str); |
| 129 data_ = data; | 135 if (it == data_source_map_.end()) |
| 136 return; | |
| 137 | |
| 138 if (it->second.first) | |
| 139 delete it->second.first; | |
| 140 data_source_map_.erase(it); | |
| 130 } | 141 } |
| 131 | 142 |
| 132 void PrintPreviewUIHTMLSource::StartDataRequest(const std::string& path, | 143 void PrintPreviewDataSource::SetDataEntry( |
| 133 bool is_incognito, | 144 const PreviewUIAddrStr& preview_ui_addr_str, const PrintPreviewData& data) { |
| 134 int request_id) { | 145 RemoveEntry(preview_ui_addr_str); |
| 146 data_source_map_[preview_ui_addr_str] = data; | |
| 147 } | |
| 148 | |
| 149 void PrintPreviewDataSource::GetDataEntry( | |
| 150 const PreviewUIAddrStr& preview_ui_addr_str, PrintPreviewData* data) { | |
| 151 PreviewDataSrcMap::iterator i = data_source_map_.find(preview_ui_addr_str); | |
|
Lei Zhang
2011/05/26 01:48:31
nit: name the iterator "it"
kmadhusu
2011/05/26 15:51:22
Done.
| |
| 152 if (i != data_source_map_.end()) | |
| 153 *data = data_source_map_[preview_ui_addr_str]; | |
| 154 } | |
| 155 | |
| 156 void PrintPreviewDataSource::StartDataRequest(const std::string& path, | |
| 157 bool is_incognito, | |
| 158 int request_id) { | |
| 159 PrintPreviewData data; | |
| 160 if (path.size() > 10 && path.substr(0, 10) == "print.pdf/") { | |
| 161 // Get the print preview data. | |
| 162 GetDataEntry(path.substr(10), &data); | |
|
Lei Zhang
2011/05/26 01:48:31
Why not just put this in the else if block below?
kmadhusu
2011/05/26 15:51:22
If data.first is null, I want to send empty_bytes
| |
| 163 } | |
| 164 | |
| 135 if (path.empty()) { | 165 if (path.empty()) { |
| 136 // Print Preview Index page. | 166 // Print Preview Index page. |
| 137 DictionaryValue localized_strings; | 167 DictionaryValue localized_strings; |
| 138 SetLocalizedStrings(&localized_strings); | 168 SetLocalizedStrings(&localized_strings); |
| 139 SetFontAndTextDirection(&localized_strings); | 169 SetFontAndTextDirection(&localized_strings); |
| 140 | 170 |
| 141 static const base::StringPiece print_html( | 171 static const base::StringPiece print_html( |
| 142 ResourceBundle::GetSharedInstance().GetRawDataResource( | 172 ResourceBundle::GetSharedInstance().GetRawDataResource( |
| 143 IDR_PRINT_PREVIEW_HTML)); | 173 IDR_PRINT_PREVIEW_HTML)); |
| 144 const std::string full_html = jstemplate_builder::GetI18nTemplateHtml( | 174 const std::string full_html = jstemplate_builder::GetI18nTemplateHtml( |
| 145 print_html, &localized_strings); | 175 print_html, &localized_strings); |
| 146 | 176 |
| 147 scoped_refptr<RefCountedBytes> html_bytes(new RefCountedBytes); | 177 scoped_refptr<RefCountedBytes> html_bytes(new RefCountedBytes); |
| 148 html_bytes->data.resize(full_html.size()); | 178 html_bytes->data.resize(full_html.size()); |
| 149 std::copy(full_html.begin(), full_html.end(), html_bytes->data.begin()); | 179 std::copy(full_html.begin(), full_html.end(), html_bytes->data.begin()); |
| 150 | 180 |
| 151 SendResponse(request_id, html_bytes); | 181 SendResponse(request_id, html_bytes); |
| 152 return; | 182 return; |
| 153 } else if (path == "print.pdf" && data_.first) { | 183 } else if (path.size() > 10 && |
| 184 path.substr(0, 10) == "print.pdf/" && | |
| 185 data.first) { | |
| 154 // Print Preview data. | 186 // Print Preview data. |
| 155 char* preview_data = reinterpret_cast<char*>(data_.first->memory()); | 187 char* preview_data = reinterpret_cast<char*>(data.first->memory()); |
| 156 uint32 preview_data_size = data_.second; | 188 uint32 preview_data_size = data.second; |
| 157 | 189 |
| 158 scoped_refptr<RefCountedBytes> html_bytes(new RefCountedBytes); | 190 scoped_refptr<RefCountedBytes> html_bytes(new RefCountedBytes); |
| 159 html_bytes->data.resize(preview_data_size); | 191 html_bytes->data.resize(preview_data_size); |
| 160 std::vector<unsigned char>::iterator it = html_bytes->data.begin(); | 192 std::vector<unsigned char>::iterator it = html_bytes->data.begin(); |
| 161 for (uint32 i = 0; i < preview_data_size; ++i, ++it) | 193 for (uint32 i = 0; i < preview_data_size; ++i, ++it) |
| 162 *it = *(preview_data + i); | 194 *it = *(preview_data + i); |
| 163 SendResponse(request_id, html_bytes); | 195 SendResponse(request_id, html_bytes); |
| 164 return; | 196 return; |
| 165 } else { | 197 } else { |
| 166 // Invalid request. | 198 // Invalid request. |
| 167 scoped_refptr<RefCountedBytes> empty_bytes(new RefCountedBytes); | 199 scoped_refptr<RefCountedBytes> empty_bytes(new RefCountedBytes); |
| 168 SendResponse(request_id, empty_bytes); | 200 SendResponse(request_id, empty_bytes); |
| 169 return; | 201 return; |
| 170 } | 202 } |
| 171 } | 203 } |
| 172 | 204 |
| 173 std::string PrintPreviewUIHTMLSource::GetMimeType( | 205 std::string PrintPreviewDataSource::GetMimeType(const std::string& path) const { |
| 174 const std::string& path) const { | |
| 175 // Print Preview Index page. | |
| 176 if (path.empty()) | 206 if (path.empty()) |
| 177 return "text/html"; | 207 return "text/html"; // Print Preview Index Page. |
| 178 // Print Preview data. | 208 return "application/pdf"; // Print Preview data |
| 179 return "application/pdf"; | |
| 180 } | 209 } |
| OLD | NEW |