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 // static |
| 114 : DataSource(chrome::kChromeUIPrintHost, MessageLoop::current()), | 119 PrintPreviewDataSource* PrintPreviewDataSource::GetInstance() { |
| 115 data_(std::make_pair(static_cast<base::SharedMemory*>(NULL), 0U)) { | 120 // Uses the LeakySingletonTrait because the base class will do the cleanup. |
| 121 return Singleton<PrintPreviewDataSource, | |
| 122 LeakySingletonTraits<PrintPreviewDataSource> >::get(); | |
| 116 } | 123 } |
| 117 | 124 |
| 118 PrintPreviewUIHTMLSource::~PrintPreviewUIHTMLSource() { | 125 PrintPreviewDataSource::PrintPreviewDataSource() |
| 119 delete data_.first; | 126 : DataSource(chrome::kChromeUIPrintHost, NULL) { |
|
sky
2011/05/25 20:57:35
Passing in NULL means StartDataRequest is most lik
kmadhusu
2011/05/25 23:45:21
Fixed. Changed Null to "MessageLoop::current()".
| |
| 120 } | 127 } |
| 121 | 128 |
| 122 void PrintPreviewUIHTMLSource::GetPrintPreviewData(PrintPreviewData* data) { | 129 PrintPreviewDataSource::~PrintPreviewDataSource() { |
| 123 *data = data_; | |
| 124 } | 130 } |
| 125 | 131 |
| 126 void PrintPreviewUIHTMLSource::SetPrintPreviewData( | 132 void PrintPreviewDataSource::StartDataRequest(const std::string& path, |
| 127 const PrintPreviewData& data) { | 133 bool is_incognito, |
| 128 delete data_.first; | 134 int request_id) { |
| 129 data_ = data; | 135 PrintPreviewData data; |
| 130 } | 136 if (path.size() > 10 && path.substr(0,10) == "print.pdf/") { |
|
sky
2011/05/25 20:57:35
nit:'0,10' -> '0, 10'
kmadhusu
2011/05/25 23:45:21
Done.
| |
| 137 // Get the print preview data. | |
| 138 GetDataEntry(path.substr(10), &data); | |
| 139 } | |
| 131 | 140 |
| 132 void PrintPreviewUIHTMLSource::StartDataRequest(const std::string& path, | |
| 133 bool is_incognito, | |
| 134 int request_id) { | |
| 135 if (path.empty()) { | 141 if (path.empty()) { |
| 136 // Print Preview Index page. | 142 // Print Preview Index page. |
| 137 DictionaryValue localized_strings; | 143 DictionaryValue localized_strings; |
| 138 SetLocalizedStrings(&localized_strings); | 144 SetLocalizedStrings(&localized_strings); |
| 139 SetFontAndTextDirection(&localized_strings); | 145 SetFontAndTextDirection(&localized_strings); |
| 140 | 146 |
| 141 static const base::StringPiece print_html( | 147 static const base::StringPiece print_html( |
| 142 ResourceBundle::GetSharedInstance().GetRawDataResource( | 148 ResourceBundle::GetSharedInstance().GetRawDataResource( |
| 143 IDR_PRINT_PREVIEW_HTML)); | 149 IDR_PRINT_PREVIEW_HTML)); |
| 144 const std::string full_html = jstemplate_builder::GetI18nTemplateHtml( | 150 const std::string full_html = jstemplate_builder::GetI18nTemplateHtml( |
| 145 print_html, &localized_strings); | 151 print_html, &localized_strings); |
| 146 | 152 |
| 147 scoped_refptr<RefCountedBytes> html_bytes(new RefCountedBytes); | 153 scoped_refptr<RefCountedBytes> html_bytes(new RefCountedBytes); |
| 148 html_bytes->data.resize(full_html.size()); | 154 html_bytes->data.resize(full_html.size()); |
| 149 std::copy(full_html.begin(), full_html.end(), html_bytes->data.begin()); | 155 std::copy(full_html.begin(), full_html.end(), html_bytes->data.begin()); |
| 150 | 156 |
| 151 SendResponse(request_id, html_bytes); | 157 SendResponse(request_id, html_bytes); |
| 152 return; | 158 return; |
| 153 } else if (path == "print.pdf" && data_.first) { | 159 } else if (path.size() > 10 && |
| 160 path.substr(0,10) == "print.pdf/" && | |
|
sky
2011/05/25 20:57:35
nit: '0,10' -> '0, 10'
kmadhusu
2011/05/25 23:45:21
Done.
| |
| 161 data.first) { | |
| 154 // Print Preview data. | 162 // Print Preview data. |
| 155 char* preview_data = reinterpret_cast<char*>(data_.first->memory()); | 163 char* preview_data = reinterpret_cast<char*>(data.first->memory()); |
| 156 uint32 preview_data_size = data_.second; | 164 uint32 preview_data_size = data.second; |
| 157 | 165 |
| 158 scoped_refptr<RefCountedBytes> html_bytes(new RefCountedBytes); | 166 scoped_refptr<RefCountedBytes> html_bytes(new RefCountedBytes); |
| 159 html_bytes->data.resize(preview_data_size); | 167 html_bytes->data.resize(preview_data_size); |
| 160 std::vector<unsigned char>::iterator it = html_bytes->data.begin(); | 168 std::vector<unsigned char>::iterator it = html_bytes->data.begin(); |
| 161 for (uint32 i = 0; i < preview_data_size; ++i, ++it) | 169 for (uint32 i = 0; i < preview_data_size; ++i, ++it) |
| 162 *it = *(preview_data + i); | 170 *it = *(preview_data + i); |
| 163 SendResponse(request_id, html_bytes); | 171 SendResponse(request_id, html_bytes); |
| 164 return; | 172 return; |
| 165 } else { | 173 } else { |
| 166 // Invalid request. | 174 // Invalid request. |
| 167 scoped_refptr<RefCountedBytes> empty_bytes(new RefCountedBytes); | 175 scoped_refptr<RefCountedBytes> empty_bytes(new RefCountedBytes); |
| 168 SendResponse(request_id, empty_bytes); | 176 SendResponse(request_id, empty_bytes); |
| 169 return; | 177 return; |
| 170 } | 178 } |
| 171 } | 179 } |
| 172 | 180 |
| 173 std::string PrintPreviewUIHTMLSource::GetMimeType( | 181 std::string PrintPreviewDataSource::GetMimeType(const std::string& path) const { |
| 174 const std::string& path) const { | |
| 175 // Print Preview Index page. | |
| 176 if (path.empty()) | 182 if (path.empty()) |
| 177 return "text/html"; | 183 return "text/html"; // Print Preview Index Page. |
| 178 // Print Preview data. | 184 return "application/pdf"; // Print Preview data |
| 179 return "application/pdf"; | |
| 180 } | 185 } |
| 186 | |
| 187 void PrintPreviewDataSource::SetDataEntry( | |
|
sky
2011/05/25 20:57:35
nit: position in file should match position in hea
kmadhusu
2011/05/25 23:45:21
Done.
| |
| 188 const PreviewUIAddrStr& preview_ui_addr_str, const PrintPreviewData& data) { | |
| 189 PreviewDataSrcMap::iterator i = data_source_map_.find(preview_ui_addr_str); | |
|
sky
2011/05/25 20:57:35
Replace 189-191 with call to RemoveEntry.
kmadhusu
2011/05/25 23:45:21
Done.
| |
| 190 if (i != data_source_map_.end()) | |
| 191 delete i->second.first; | |
| 192 data_source_map_[preview_ui_addr_str] = data; | |
| 193 } | |
| 194 | |
| 195 void PrintPreviewDataSource::GetDataEntry( | |
| 196 const PreviewUIAddrStr& preview_ui_addr_str, PrintPreviewData* data) { | |
| 197 PreviewDataSrcMap::iterator i = data_source_map_.find(preview_ui_addr_str); | |
| 198 if (i != data_source_map_.end()) | |
| 199 *data = data_source_map_[preview_ui_addr_str]; | |
| 200 } | |
| 201 | |
| 202 void PrintPreviewDataSource::RemoveEntry( | |
| 203 const PreviewUIAddrStr& preview_ui_addr_str) { | |
| 204 PreviewDataSrcMap::iterator it = data_source_map_.find(preview_ui_addr_str); | |
| 205 if (it == data_source_map_.end()) | |
| 206 return; | |
| 207 | |
| 208 if (it->second.first) | |
| 209 delete it->second.first; | |
| 210 data_source_map_.erase(it); | |
| 211 } | |
| 212 | |
| OLD | NEW |