Chromium Code Reviews| Index: chrome/browser/ui/webui/print_preview_data_source.cc |
| diff --git a/chrome/browser/ui/webui/print_preview_ui_html_source.cc b/chrome/browser/ui/webui/print_preview_data_source.cc |
| similarity index 74% |
| rename from chrome/browser/ui/webui/print_preview_ui_html_source.cc |
| rename to chrome/browser/ui/webui/print_preview_data_source.cc |
| index 7dad069677068dbf9886ed96881f6ae5c082512e..39b491cd986f375276ef0e95afc12df8199951ab 100644 |
| --- a/chrome/browser/ui/webui/print_preview_ui_html_source.cc |
| +++ b/chrome/browser/ui/webui/print_preview_data_source.cc |
| @@ -2,16 +2,21 @@ |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| -#include "chrome/browser/ui/webui/print_preview_ui_html_source.h" |
| +#include "chrome/browser/ui/webui/print_preview_data_source.h" |
| #include <algorithm> |
| #include <vector> |
| +#include "base/memory/singleton.h" |
| #include "base/message_loop.h" |
| #include "base/shared_memory.h" |
| #include "base/string_piece.h" |
| #include "base/utf_string_conversions.h" |
| #include "base/values.h" |
| +#include "chrome/browser/profiles/profile.h" |
| +#include "chrome/browser/ui/webui/chrome_url_data_manager.h" |
| +#include "chrome/browser/ui/webui/chrome_url_data_manager_backend.h" |
| +#include "chrome/browser/ui/webui/print_preview_ui.h" |
| #include "chrome/common/jstemplate_builder.h" |
| #include "chrome/common/url_constants.h" |
| #include "grit/browser_resources.h" |
| @@ -110,28 +115,29 @@ void SetLocalizedStrings(DictionaryValue* localized_strings) { |
| } // namespace |
| -PrintPreviewUIHTMLSource::PrintPreviewUIHTMLSource() |
| - : DataSource(chrome::kChromeUIPrintHost, MessageLoop::current()), |
| - data_(std::make_pair(static_cast<base::SharedMemory*>(NULL), 0U)) { |
| +// static |
| +PrintPreviewDataSource* PrintPreviewDataSource::GetInstance() { |
| + // Uses the LeakySingletonTrait because the base class will do the cleanup. |
| + return Singleton<PrintPreviewDataSource, |
| + LeakySingletonTraits<PrintPreviewDataSource> >::get(); |
| } |
| -PrintPreviewUIHTMLSource::~PrintPreviewUIHTMLSource() { |
| - delete data_.first; |
| +PrintPreviewDataSource::PrintPreviewDataSource() |
| + : 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()".
|
| } |
| -void PrintPreviewUIHTMLSource::GetPrintPreviewData(PrintPreviewData* data) { |
| - *data = data_; |
| +PrintPreviewDataSource::~PrintPreviewDataSource() { |
| } |
| -void PrintPreviewUIHTMLSource::SetPrintPreviewData( |
| - const PrintPreviewData& data) { |
| - delete data_.first; |
| - data_ = data; |
| -} |
| +void PrintPreviewDataSource::StartDataRequest(const std::string& path, |
| + bool is_incognito, |
| + int request_id) { |
| + PrintPreviewData data; |
| + 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.
|
| + // Get the print preview data. |
| + GetDataEntry(path.substr(10), &data); |
| + } |
| -void PrintPreviewUIHTMLSource::StartDataRequest(const std::string& path, |
| - bool is_incognito, |
| - int request_id) { |
| if (path.empty()) { |
| // Print Preview Index page. |
| DictionaryValue localized_strings; |
| @@ -150,10 +156,12 @@ void PrintPreviewUIHTMLSource::StartDataRequest(const std::string& path, |
| SendResponse(request_id, html_bytes); |
| return; |
| - } else if (path == "print.pdf" && data_.first) { |
| + } else 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.
|
| + data.first) { |
| // Print Preview data. |
| - char* preview_data = reinterpret_cast<char*>(data_.first->memory()); |
| - uint32 preview_data_size = data_.second; |
| + char* preview_data = reinterpret_cast<char*>(data.first->memory()); |
| + uint32 preview_data_size = data.second; |
| scoped_refptr<RefCountedBytes> html_bytes(new RefCountedBytes); |
| html_bytes->data.resize(preview_data_size); |
| @@ -162,7 +170,7 @@ void PrintPreviewUIHTMLSource::StartDataRequest(const std::string& path, |
| *it = *(preview_data + i); |
| SendResponse(request_id, html_bytes); |
| return; |
| - } else { |
| + } else { |
| // Invalid request. |
| scoped_refptr<RefCountedBytes> empty_bytes(new RefCountedBytes); |
| SendResponse(request_id, empty_bytes); |
| @@ -170,11 +178,35 @@ void PrintPreviewUIHTMLSource::StartDataRequest(const std::string& path, |
| } |
| } |
| -std::string PrintPreviewUIHTMLSource::GetMimeType( |
| - const std::string& path) const { |
| - // Print Preview Index page. |
| +std::string PrintPreviewDataSource::GetMimeType(const std::string& path) const { |
| if (path.empty()) |
| - return "text/html"; |
| - // Print Preview data. |
| - return "application/pdf"; |
| + return "text/html"; // Print Preview Index Page. |
| + return "application/pdf"; // Print Preview data |
| +} |
| + |
| +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.
|
| + const PreviewUIAddrStr& preview_ui_addr_str, const PrintPreviewData& data) { |
| + 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.
|
| + if (i != data_source_map_.end()) |
| + delete i->second.first; |
| + data_source_map_[preview_ui_addr_str] = data; |
| +} |
| + |
| +void PrintPreviewDataSource::GetDataEntry( |
| + const PreviewUIAddrStr& preview_ui_addr_str, PrintPreviewData* data) { |
| + PreviewDataSrcMap::iterator i = data_source_map_.find(preview_ui_addr_str); |
| + if (i != data_source_map_.end()) |
| + *data = data_source_map_[preview_ui_addr_str]; |
| } |
| + |
| +void PrintPreviewDataSource::RemoveEntry( |
| + const PreviewUIAddrStr& preview_ui_addr_str) { |
| + PreviewDataSrcMap::iterator it = data_source_map_.find(preview_ui_addr_str); |
| + if (it == data_source_map_.end()) |
| + return; |
| + |
| + if (it->second.first) |
| + delete it->second.first; |
| + data_source_map_.erase(it); |
| +} |
| + |