diff options
-rw-r--r-- | chrome/browser/printing/print_preview_data_service.cc | 39 | ||||
-rw-r--r-- | chrome/browser/printing/print_preview_data_service.h | 56 | ||||
-rw-r--r-- | chrome/browser/printing/print_preview_message_handler.cc | 16 | ||||
-rw-r--r-- | chrome/browser/resources/print_preview.js | 11 | ||||
-rw-r--r-- | chrome/browser/ui/webui/print_preview_data_source.cc (renamed from chrome/browser/ui/webui/print_preview_ui_html_source.cc) | 58 | ||||
-rw-r--r-- | chrome/browser/ui/webui/print_preview_data_source.h | 41 | ||||
-rw-r--r-- | chrome/browser/ui/webui/print_preview_handler.cc | 10 | ||||
-rw-r--r-- | chrome/browser/ui/webui/print_preview_ui.cc | 35 | ||||
-rw-r--r-- | chrome/browser/ui/webui/print_preview_ui.h | 19 | ||||
-rw-r--r-- | chrome/browser/ui/webui/print_preview_ui_html_source.h | 51 | ||||
-rw-r--r-- | chrome/browser/ui/webui/print_preview_ui_unittest.cc (renamed from chrome/browser/ui/webui/print_preview_ui_html_source_unittest.cc) | 38 | ||||
-rw-r--r-- | chrome/chrome_browser.gypi | 6 | ||||
-rw-r--r-- | chrome/chrome_tests.gypi | 6 |
13 files changed, 252 insertions, 134 deletions
diff --git a/chrome/browser/printing/print_preview_data_service.cc b/chrome/browser/printing/print_preview_data_service.cc new file mode 100644 index 0000000..2199e73 --- /dev/null +++ b/chrome/browser/printing/print_preview_data_service.cc @@ -0,0 +1,39 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "chrome/browser/printing/print_preview_data_service.h" + +#include "base/memory/singleton.h" + +// static +PrintPreviewDataService* PrintPreviewDataService::GetInstance() { + return Singleton<PrintPreviewDataService>::get(); +} + +PrintPreviewDataService::PrintPreviewDataService() { +} + +PrintPreviewDataService::~PrintPreviewDataService() { +} + +void PrintPreviewDataService::GetDataEntry( + const std::string& preview_ui_addr_str, + scoped_refptr<RefCountedBytes>* data_bytes) { + PreviewDataSourceMap::iterator it = data_src_map_.find(preview_ui_addr_str); + if (it != data_src_map_.end()) + *data_bytes = it->second.get(); +} + +void PrintPreviewDataService::SetDataEntry( + const std::string& preview_ui_addr_str, const RefCountedBytes* data_bytes) { + RemoveEntry(preview_ui_addr_str); + data_src_map_[preview_ui_addr_str] = const_cast<RefCountedBytes*>(data_bytes); +} + +void PrintPreviewDataService::RemoveEntry( + const std::string& preview_ui_addr_str) { + PreviewDataSourceMap::iterator it = data_src_map_.find(preview_ui_addr_str); + if (it != data_src_map_.end()) + data_src_map_.erase(it); +} diff --git a/chrome/browser/printing/print_preview_data_service.h b/chrome/browser/printing/print_preview_data_service.h new file mode 100644 index 0000000..63618ac --- /dev/null +++ b/chrome/browser/printing/print_preview_data_service.h @@ -0,0 +1,56 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef CHROME_BROWSER_PRINTING_PRINT_PREVIEW_DATA_SERVICE_H_ +#define CHROME_BROWSER_PRINTING_PRINT_PREVIEW_DATA_SERVICE_H_ +#pragma once + +#include <map> +#include <string> + +#include "base/memory/ref_counted.h" +#include "base/memory/ref_counted_memory.h" + +template<typename T> struct DefaultSingletonTraits; + +// PrintPreviewDataService manages data for chrome://print requests. +// +// PrintPreviewDataService owns the data and is responsible for freeing it when +// either: +// a) There is a new data. +// b) When PrintPreviewDataService is destroyed. +// +class PrintPreviewDataService { + public: + static PrintPreviewDataService* GetInstance(); + + // Get the data entry from PreviewDataSourceMap. + void GetDataEntry(const std::string& preview_ui_addr_str, + scoped_refptr<RefCountedBytes>* data); + + // Set/Update the data entry in PreviewDataSourceMap. + void SetDataEntry(const std::string& preview_ui_addr_str, + const RefCountedBytes* data); + + // Remove the corresponding PrintPreviewUI entry from the map. + void RemoveEntry(const std::string& preview_ui_addr_str); + + private: + friend struct DefaultSingletonTraits<PrintPreviewDataService>; + + // 1:1 relationship between PrintPreviewUI and preview data. + // Key: Print preview UI address string. + // Value: Preview data. + typedef std::map<std::string, scoped_refptr<RefCountedBytes> > + PreviewDataSourceMap; + + PrintPreviewDataService(); + virtual ~PrintPreviewDataService(); + + PreviewDataSourceMap data_src_map_; + + DISALLOW_COPY_AND_ASSIGN(PrintPreviewDataService); +}; + +#endif // CHROME_BROWSER_PRINTING_PRINT_PREVIEW_DATA_SERVICE_H_ diff --git a/chrome/browser/printing/print_preview_message_handler.cc b/chrome/browser/printing/print_preview_message_handler.cc index 0b1494c..d293d6f 100644 --- a/chrome/browser/printing/print_preview_message_handler.cc +++ b/chrome/browser/printing/print_preview_message_handler.cc @@ -4,6 +4,7 @@ #include "chrome/browser/printing/print_preview_message_handler.h" +#include "base/memory/ref_counted.h" #include "chrome/browser/browser_process.h" #include "chrome/browser/printing/print_job_manager.h" #include "chrome/browser/printing/print_preview_tab_controller.h" @@ -12,7 +13,6 @@ #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" #include "chrome/browser/ui/webui/print_preview_handler.h" #include "chrome/browser/ui/webui/print_preview_ui.h" -#include "chrome/browser/ui/webui/print_preview_ui_html_source.h" #include "chrome/common/print_messages.h" #include "content/browser/browser_thread.h" #include "content/browser/renderer_host/render_view_host.h" @@ -88,9 +88,17 @@ void PrintPreviewMessageHandler::OnPagesReadyForPreview( PrintPreviewUI* print_preview_ui = static_cast<PrintPreviewUI*>(print_preview_tab->web_ui()); - PrintPreviewUIHTMLSource* html_source = print_preview_ui->html_source(); - html_source->SetPrintPreviewData( - std::make_pair(shared_buf, params.data_size)); + + char* preview_data = static_cast<char*>(shared_buf->memory()); + uint32 preview_data_size = params.data_size; + + scoped_refptr<RefCountedBytes> html_bytes(new RefCountedBytes); + html_bytes->data.resize(preview_data_size); + std::vector<unsigned char>::iterator it = html_bytes->data.begin(); + for (uint32 i = 0; i < preview_data_size; ++i, ++it) + *it = *(preview_data + i); + + print_preview_ui->SetPrintPreviewData(html_bytes.get()); print_preview_ui->OnPreviewDataIsAvailable( params.expected_pages_count, wrapper->print_view_manager()->RenderSourceName(), diff --git a/chrome/browser/resources/print_preview.js b/chrome/browser/resources/print_preview.js index c37fc14b..c5f081f 100644 --- a/chrome/browser/resources/print_preview.js +++ b/chrome/browser/resources/print_preview.js @@ -504,9 +504,9 @@ function onPDFLoad() { * @param {number} pageCount The expected total pages count. * @param {string} jobTitle The print job title. * @param {boolean} modifiable If the preview is modifiable. - * + * @param {string} previewUid Preview unique identifier. */ -function updatePrintPreview(pageCount, jobTitle, modifiable) { +function updatePrintPreview(pageCount, jobTitle, modifiable, previewUid) { var tempPrintSettings = new PrintSettings(); tempPrintSettings.save(); @@ -540,7 +540,7 @@ function updatePrintPreview(pageCount, jobTitle, modifiable) { // Update the current tab title. document.title = localStrings.getStringF('printPreviewTitleFormat', jobTitle); - createPDFPlugin(); + createPDFPlugin(previewUid); updatePrintSummary(); updatePrintButtonState(); addEventListeners(); @@ -548,8 +548,9 @@ function updatePrintPreview(pageCount, jobTitle, modifiable) { /** * Create the PDF plugin or reload the existing one. + * @param {string} previewUid Preview unique identifier. */ -function createPDFPlugin() { +function createPDFPlugin(previewUid) { // Enable the print button. if (!$('printer-list').disabled) { $('print-button').disabled = false; @@ -572,7 +573,7 @@ function createPDFPlugin() { var pdfPlugin = document.createElement('embed'); pdfPlugin.setAttribute('id', 'pdf-viewer'); pdfPlugin.setAttribute('type', 'application/pdf'); - pdfPlugin.setAttribute('src', 'chrome://print/print.pdf'); + pdfPlugin.setAttribute('src', 'chrome://print/' + previewUid + '/print.pdf'); var mainView = $('mainview'); mainView.appendChild(pdfPlugin); pdfPlugin.onload('onPDFLoad()'); diff --git a/chrome/browser/ui/webui/print_preview_ui_html_source.cc b/chrome/browser/ui/webui/print_preview_data_source.cc index 34f7b4c..3c5fb71 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,16 @@ // 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/message_loop.h" -#include "base/shared_memory.h" #include "base/string_piece.h" +#include "base/string_util.h" #include "base/utf_string_conversions.h" #include "base/values.h" +#include "chrome/browser/printing/print_preview_data_service.h" #include "chrome/common/jstemplate_builder.h" #include "chrome/common/url_constants.h" #include "grit/browser_resources.h" @@ -108,28 +108,25 @@ void SetLocalizedStrings(DictionaryValue* localized_strings) { } // namespace -PrintPreviewUIHTMLSource::PrintPreviewUIHTMLSource() - : DataSource(chrome::kChromeUIPrintHost, MessageLoop::current()), - data_(std::make_pair(static_cast<base::SharedMemory*>(NULL), 0U)) { +PrintPreviewDataSource::PrintPreviewDataSource() + : DataSource(chrome::kChromeUIPrintHost, MessageLoop::current()) { } -PrintPreviewUIHTMLSource::~PrintPreviewUIHTMLSource() { - delete data_.first; +PrintPreviewDataSource::~PrintPreviewDataSource() { } -void PrintPreviewUIHTMLSource::GetPrintPreviewData(PrintPreviewData* data) { - *data = data_; -} +void PrintPreviewDataSource::StartDataRequest(const std::string& path, + bool is_incognito, + int request_id) { + scoped_refptr<RefCountedBytes> data(new RefCountedBytes); -void PrintPreviewUIHTMLSource::SetPrintPreviewData( - const PrintPreviewData& data) { - delete data_.first; - data_ = data; -} + bool preview_data_requested = EndsWith(path, "/print.pdf", true); + if (preview_data_requested) { + size_t index = path.rfind("/print.pdf"); + PrintPreviewDataService::GetInstance()->GetDataEntry(path.substr(0, index), + &data); + } -void PrintPreviewUIHTMLSource::StartDataRequest(const std::string& path, - bool is_incognito, - int request_id) { if (path.empty()) { // Print Preview Index page. DictionaryValue localized_strings; @@ -148,19 +145,11 @@ void PrintPreviewUIHTMLSource::StartDataRequest(const std::string& path, SendResponse(request_id, html_bytes); return; - } else if (path == "print.pdf" && data_.first) { + } else if (preview_data_requested && data->front()) { // Print Preview data. - 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); - std::vector<unsigned char>::iterator it = html_bytes->data.begin(); - for (uint32 i = 0; i < preview_data_size; ++i, ++it) - *it = *(preview_data + i); - SendResponse(request_id, html_bytes); + SendResponse(request_id, data); return; - } else { + } else { // Invalid request. scoped_refptr<RefCountedBytes> empty_bytes(new RefCountedBytes); SendResponse(request_id, empty_bytes); @@ -168,11 +157,8 @@ 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 } diff --git a/chrome/browser/ui/webui/print_preview_data_source.h b/chrome/browser/ui/webui/print_preview_data_source.h new file mode 100644 index 0000000..a6b02a1 --- /dev/null +++ b/chrome/browser/ui/webui/print_preview_data_source.h @@ -0,0 +1,41 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef CHROME_BROWSER_UI_WEBUI_PRINT_PREVIEW_DATA_SOURCE_H_ +#define CHROME_BROWSER_UI_WEBUI_PRINT_PREVIEW_DATA_SOURCE_H_ +#pragma once + +#include <string> + +#include "base/compiler_specific.h" +#include "chrome/browser/ui/webui/chrome_url_data_manager.h" + +// PrintPreviewDataSource serves data for chrome://print requests. +// +// The format for requesting data is as follows: +// chrome://print/<PrintPreviewUIAddrStr>/print.pdf +// +// Parameters (< > required): +// <PrintPreviewUIAddrStr> = Print preview UI identifier. +// +// Example: +// chrome://print/0xab0123ef/print.pdf + +class PrintPreviewDataSource : public ChromeURLDataManager::DataSource { + public: + PrintPreviewDataSource(); + + // ChromeURLDataManager::DataSource implementation. + virtual void StartDataRequest(const std::string& path, + bool is_incognito, + int request_id) OVERRIDE; + virtual std::string GetMimeType(const std::string& path) const OVERRIDE; + + private: + virtual ~PrintPreviewDataSource(); + + DISALLOW_COPY_AND_ASSIGN(PrintPreviewDataSource); +}; + +#endif // CHROME_BROWSER_UI_WEBUI_PRINT_PREVIEW_DATA_SOURCE_H_ diff --git a/chrome/browser/ui/webui/print_preview_handler.cc b/chrome/browser/ui/webui/print_preview_handler.cc index ff8c16c2..70b2b48 100644 --- a/chrome/browser/ui/webui/print_preview_handler.cc +++ b/chrome/browser/ui/webui/print_preview_handler.cc @@ -8,6 +8,7 @@ #include "base/i18n/file_util_icu.h" #include "base/json/json_reader.h" +#include "base/memory/ref_counted.h" #include "base/metrics/histogram.h" #include "base/path_service.h" #include "base/threading/thread.h" @@ -22,7 +23,6 @@ #include "chrome/browser/tabs/tab_strip_model.h" #include "chrome/browser/ui/browser_list.h" #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" -#include "chrome/browser/ui/webui/print_preview_ui_html_source.h" #include "chrome/browser/ui/webui/print_preview_ui.h" #include "chrome/common/chrome_paths.h" #include "chrome/common/print_messages.h" @@ -619,16 +619,16 @@ void PrintPreviewHandler::SelectFile(const FilePath& default_filename) { void PrintPreviewHandler::FileSelected(const FilePath& path, int index, void* params) { - PrintPreviewUIHTMLSource::PrintPreviewData data; PrintPreviewUI* print_preview_ui = static_cast<PrintPreviewUI*>(web_ui_); - print_preview_ui->html_source()->GetPrintPreviewData(&data); - if (!data.first || !data.second) { + scoped_refptr<RefCountedBytes> data(new RefCountedBytes()); + print_preview_ui->GetPrintPreviewData(&data); + if (!data->front()) { NOTREACHED(); return; } printing::PreviewMetafile* metafile = new printing::PreviewMetafile; - metafile->InitFromData(data.first->memory(), data.second); + metafile->InitFromData(static_cast<const void*>(data->front()), data->size()); // Updating last_saved_path_ to the newly selected folder. *last_saved_path_ = path.DirName(); diff --git a/chrome/browser/ui/webui/print_preview_ui.cc b/chrome/browser/ui/webui/print_preview_ui.cc index 90e09e5..a9e3327 100644 --- a/chrome/browser/ui/webui/print_preview_ui.cc +++ b/chrome/browser/ui/webui/print_preview_ui.cc @@ -4,31 +4,43 @@ #include "chrome/browser/ui/webui/print_preview_ui.h" -#include "base/values.h" #include "base/metrics/histogram.h" +#include "base/string_util.h" +#include "base/values.h" +#include "chrome/browser/printing/print_preview_data_service.h" #include "chrome/browser/profiles/profile.h" +#include "chrome/browser/ui/webui/print_preview_data_source.h" #include "chrome/browser/ui/webui/print_preview_handler.h" -#include "chrome/browser/ui/webui/print_preview_ui_html_source.h" -#include "content/browser/browser_thread.h" #include "content/browser/tab_contents/tab_contents.h" PrintPreviewUI::PrintPreviewUI(TabContents* contents) : WebUI(contents), - html_source_(new PrintPreviewUIHTMLSource()), initial_preview_start_time_(base::TimeTicks::Now()) { // PrintPreviewUI owns |handler|. PrintPreviewHandler* handler = new PrintPreviewHandler(); AddMessageHandler(handler->Attach(this)); - // Set up the chrome://print/ source. - contents->profile()->GetChromeURLDataManager()->AddDataSource(html_source_); + // Set up the chrome://print/ data source. + contents->profile()->GetChromeURLDataManager()->AddDataSource( + new PrintPreviewDataSource()); + + // Store the PrintPreviewUIAddress as a string. + // "0x" + deadc0de + '\0' = 2 + 2 * sizeof(this) + 1; + char preview_ui_addr[2 + (2 * sizeof(this)) + 1]; + base::snprintf(preview_ui_addr, sizeof(preview_ui_addr), "%p", this); + preview_ui_addr_str_ = preview_ui_addr; } PrintPreviewUI::~PrintPreviewUI() { + print_preview_data_service()->RemoveEntry(preview_ui_addr_str_); } -PrintPreviewUIHTMLSource* PrintPreviewUI::html_source() { - return html_source_.get(); +void PrintPreviewUI::GetPrintPreviewData(scoped_refptr<RefCountedBytes>* data) { + print_preview_data_service()->GetDataEntry(preview_ui_addr_str_, data); +} + +void PrintPreviewUI::SetPrintPreviewData(const RefCountedBytes* data) { + print_preview_data_service()->SetDataEntry(preview_ui_addr_str_, data); } void PrintPreviewUI::OnInitiatorTabClosed( @@ -52,6 +64,11 @@ void PrintPreviewUI::OnPreviewDataIsAvailable(int expected_pages_count, FundamentalValue pages_count(expected_pages_count); StringValue title(job_title); FundamentalValue is_preview_modifiable(modifiable); + StringValue ui_identifier(preview_ui_addr_str_); CallJavascriptFunction("updatePrintPreview", pages_count, title, - is_preview_modifiable); + is_preview_modifiable, ui_identifier); +} + +PrintPreviewDataService* PrintPreviewUI::print_preview_data_service() { + return PrintPreviewDataService::GetInstance(); } diff --git a/chrome/browser/ui/webui/print_preview_ui.h b/chrome/browser/ui/webui/print_preview_ui.h index ad2f8bd..dc6d691 100644 --- a/chrome/browser/ui/webui/print_preview_ui.h +++ b/chrome/browser/ui/webui/print_preview_ui.h @@ -9,17 +9,25 @@ #include <string> #include "base/memory/ref_counted.h" +#include "base/memory/ref_counted_memory.h" #include "base/time.h" +#include "chrome/browser/printing/print_preview_data_service.h" #include "content/browser/webui/web_ui.h" -class PrintPreviewUIHTMLSource; +class PrintPreviewDataService; class PrintPreviewUI : public WebUI { public: explicit PrintPreviewUI(TabContents* contents); virtual ~PrintPreviewUI(); - PrintPreviewUIHTMLSource* html_source(); + // Gets the print preview |data|. The data is valid as long as the + // PrintPreviewDataService is valid and SetPrintPreviewData() does not get + // called. + void GetPrintPreviewData(scoped_refptr<RefCountedBytes>* data); + + // Sets the print preview |data|. + void SetPrintPreviewData(const RefCountedBytes* data); // Notify the Web UI renderer that preview data is available. // |expected_pages_count| specifies the total number of pages. @@ -37,9 +45,14 @@ class PrintPreviewUI : public WebUI { void OnInitiatorTabClosed(const std::string& initiator_tab_url); private: - scoped_refptr<PrintPreviewUIHTMLSource> html_source_; + // Helper function + PrintPreviewDataService* print_preview_data_service(); + base::TimeTicks initial_preview_start_time_; + // Store the PrintPreviewUI address string. + std::string preview_ui_addr_str_; + DISALLOW_COPY_AND_ASSIGN(PrintPreviewUI); }; diff --git a/chrome/browser/ui/webui/print_preview_ui_html_source.h b/chrome/browser/ui/webui/print_preview_ui_html_source.h deleted file mode 100644 index da17532..0000000 --- a/chrome/browser/ui/webui/print_preview_ui_html_source.h +++ /dev/null @@ -1,51 +0,0 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef CHROME_BROWSER_UI_WEBUI_PRINT_PREVIEW_UI_HTML_SOURCE_H_ -#define CHROME_BROWSER_UI_WEBUI_PRINT_PREVIEW_UI_HTML_SOURCE_H_ -#pragma once - -#include <string> -#include <utility> - -#include "chrome/browser/ui/webui/chrome_url_data_manager.h" - -namespace base { -class SharedMemory; -} - -class PrintPreviewUIHTMLSource : public ChromeURLDataManager::DataSource { - public: - // A SharedMemory that contains the data for print preview, - // and the size of the print preview data in bytes. - typedef std::pair<base::SharedMemory*, uint32> PrintPreviewData; - - PrintPreviewUIHTMLSource(); - virtual ~PrintPreviewUIHTMLSource(); - - // Gets the print preview |data|. The data is valid as long as the - // PrintPreviewHandler is valid and SetPrintPreviewData() does not get called. - void GetPrintPreviewData(PrintPreviewData* data); - - // Sets the print preview |data|. PrintPreviewHandler owns the data and is - // responsible for freeing it when either: - // a) there is new data. - // b) when PrintPreviewHandler is destroyed. - void SetPrintPreviewData(const PrintPreviewData& data); - - // ChromeURLDataManager::DataSource implementation. - virtual void StartDataRequest(const std::string& path, - bool is_incognito, - int request_id); - virtual std::string GetMimeType(const std::string&) const; - - private: - // Current print preview data, the contents of which are owned by - // PrintPreviewHandler. - PrintPreviewData data_; - - DISALLOW_COPY_AND_ASSIGN(PrintPreviewUIHTMLSource); -}; - -#endif // CHROME_BROWSER_UI_WEBUI_PRINT_PREVIEW_UI_HTML_SOURCE_H_ diff --git a/chrome/browser/ui/webui/print_preview_ui_html_source_unittest.cc b/chrome/browser/ui/webui/print_preview_ui_unittest.cc index 5653bc2..65613d3 100644 --- a/chrome/browser/ui/webui/print_preview_ui_html_source_unittest.cc +++ b/chrome/browser/ui/webui/print_preview_ui_unittest.cc @@ -3,20 +3,26 @@ // found in the LICENSE file. #include "base/command_line.h" -#include "base/shared_memory.h" +#include "base/memory/ref_counted_memory.h" #include "chrome/browser/printing/print_preview_tab_controller.h" #include "chrome/browser/ui/browser_list.h" #include "chrome/browser/ui/webui/print_preview_ui.h" -#include "chrome/browser/ui/webui/print_preview_ui_html_source.h" #include "chrome/common/chrome_switches.h" #include "chrome/test/browser_with_test_window_test.h" #include "chrome/test/testing_profile.h" #include "content/browser/tab_contents/tab_contents.h" -typedef BrowserWithTestWindowTest PrintPreviewUIHTMLSourceTest; +namespace { + +const unsigned char blob1[] = + "12346102356120394751634516591348710478123649165419234519234512349134"; + +} // namespace + +typedef BrowserWithTestWindowTest PrintPreviewUITest; // Create/Get a preview tab for initiator tab. -TEST_F(PrintPreviewUIHTMLSourceTest, PrintPreviewData) { +TEST_F(PrintPreviewUITest, PrintPreviewData) { #if !defined(GOOGLE_CHROME_BUILD) || defined(OS_CHROMEOS) CommandLine::ForCurrentProcess()->AppendSwitch(switches::kEnablePrintPreview); #endif @@ -40,21 +46,21 @@ TEST_F(PrintPreviewUIHTMLSourceTest, PrintPreviewData) { PrintPreviewUI* preview_ui = reinterpret_cast<PrintPreviewUI*>(preview_tab->web_ui()); ASSERT_TRUE(preview_ui != NULL); - PrintPreviewUIHTMLSource* html_source = preview_ui->html_source(); - PrintPreviewUIHTMLSource::PrintPreviewData data; - html_source->GetPrintPreviewData(&data); - EXPECT_EQ(NULL, data.first); - EXPECT_EQ(0U, data.second); + scoped_refptr<RefCountedBytes> data(new RefCountedBytes); + preview_ui->GetPrintPreviewData(&data); + EXPECT_EQ(NULL, data->front()); + EXPECT_EQ(0U, data->size()); - PrintPreviewUIHTMLSource::PrintPreviewData dummy_data = - std::make_pair(new base::SharedMemory(), 1234); + std::vector<unsigned char> preview_data(blob1, blob1 + sizeof(blob1)); + scoped_refptr<RefCountedBytes> dummy_data(new RefCountedBytes(preview_data)); - html_source->SetPrintPreviewData(dummy_data); - html_source->GetPrintPreviewData(&data); - EXPECT_EQ(dummy_data, data); + preview_ui->SetPrintPreviewData(dummy_data.get()); + preview_ui->GetPrintPreviewData(&data); + EXPECT_EQ(dummy_data->size(), data->size()); + EXPECT_EQ(dummy_data.get(), data.get()); // This should not cause any memory leaks. - dummy_data.first = new base::SharedMemory(); - html_source->SetPrintPreviewData(dummy_data); + dummy_data = new RefCountedBytes(); + preview_ui->SetPrintPreviewData(dummy_data); } diff --git a/chrome/chrome_browser.gypi b/chrome/chrome_browser.gypi index fec6400..24c57cc 100644 --- a/chrome/chrome_browser.gypi +++ b/chrome/chrome_browser.gypi @@ -1699,6 +1699,8 @@ 'browser/printing/print_job_worker.cc', 'browser/printing/print_job_worker.h', 'browser/printing/print_job_worker_owner.h', + 'browser/printing/print_preview_data_service.cc', + 'browser/printing/print_preview_data_service.h', 'browser/printing/print_preview_message_handler.cc', 'browser/printing/print_preview_message_handler.h', 'browser/printing/print_preview_tab_controller.cc', @@ -3459,12 +3461,12 @@ 'browser/ui/webui/options/sync_setup_handler.h', 'browser/ui/webui/plugins_ui.cc', 'browser/ui/webui/plugins_ui.h', + 'browser/ui/webui/print_preview_data_source.cc', + 'browser/ui/webui/print_preview_data_source.h', 'browser/ui/webui/print_preview_handler.cc', 'browser/ui/webui/print_preview_handler.h', 'browser/ui/webui/print_preview_ui.cc', 'browser/ui/webui/print_preview_ui.h', - 'browser/ui/webui/print_preview_ui_html_source.cc', - 'browser/ui/webui/print_preview_ui_html_source.h', 'browser/ui/webui/screenshot_source.cc', 'browser/ui/webui/screenshot_source.h', 'browser/ui/webui/shared_resources_data_source.cc', diff --git a/chrome/chrome_tests.gypi b/chrome/chrome_tests.gypi index 9faad86..28f4bf3 100644 --- a/chrome/chrome_tests.gypi +++ b/chrome/chrome_tests.gypi @@ -1813,7 +1813,7 @@ 'browser/ui/webui/html_dialog_tab_contents_delegate_unittest.cc', 'browser/ui/webui/ntp/shown_sections_handler_unittest.cc', 'browser/ui/webui/options/language_options_handler_unittest.cc', - 'browser/ui/webui/print_preview_ui_html_source_unittest.cc', + 'browser/ui/webui/print_preview_ui_unittest.cc', 'browser/ui/webui/sync_internals_ui_unittest.cc', 'browser/ui/webui/theme_source_unittest.cc', 'browser/ui/webui/web_ui_unittest.cc', @@ -1990,10 +1990,10 @@ }], ['chromeos==1', { 'sources/': [ - # TODO(thestig) Enable PrintPreviewUIHTMLSource tests on CrOS when + # TODO(thestig) Enable PrintPreviewUI tests on CrOS when # print preview is enabled on CrOS. ['exclude', 'browser/notifications/desktop_notifications_unittest.cc'], - ['exclude', 'browser/ui/webui/print_preview_ui_html_source_unittest.cc'], + ['exclude', 'browser/ui/webui/print_preview_ui_unittest.cc'], ], }, { # else: chromeos == 0 'sources/': [ |