diff options
author | thestig@chromium.org <thestig@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-08-24 23:22:28 +0000 |
---|---|---|
committer | thestig@chromium.org <thestig@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-08-24 23:22:28 +0000 |
commit | 116d0963cadfbf55ef2ec3d13781987c4d80517a (patch) | |
tree | a7e5b7ceabdad7e6386a87b7f813a79dc57f94a0 | |
parent | 0ccc7e3af6db2fc2ce09ebcd780dace294adeb61 (diff) | |
download | chromium_src-116d0963cadfbf55ef2ec3d13781987c4d80517a.zip chromium_src-116d0963cadfbf55ef2ec3d13781987c4d80517a.tar.gz chromium_src-116d0963cadfbf55ef2ec3d13781987c4d80517a.tar.bz2 |
Print preview: Use an ID instead of memory pointer string in WebUI.
BUG=144051
Review URL: https://chromiumcodereview.appspot.com/10870003
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@153342 0039d316-1c4b-4281-b951-d872f2087c98
20 files changed, 187 insertions, 178 deletions
diff --git a/chrome/browser/printing/print_preview_data_service.cc b/chrome/browser/printing/print_preview_data_service.cc index 3550118..c03c519 100644 --- a/chrome/browser/printing/print_preview_data_service.cc +++ b/chrome/browser/printing/print_preview_data_service.cc @@ -6,6 +6,7 @@ #include "base/memory/ref_counted_memory.h" #include "base/memory/singleton.h" +#include "base/stl_util.h" #include "printing/print_job_constants.h" // PrintPreviewDataStore stores data for preview workflow and preview printing @@ -30,10 +31,8 @@ class PrintPreviewDataStore : public base::RefCounted<PrintPreviewDataStore> { // Get the preview page for the specified |index|. void GetPreviewDataForIndex(int index, scoped_refptr<base::RefCountedBytes>* data) { - if (index != printing::COMPLETE_PREVIEW_DOCUMENT_INDEX && - index < printing::FIRST_PAGE_INDEX) { + if (IsInvalidIndex(index)) return; - } PreviewPageDataMap::iterator it = page_data_map_.find(index); if (it != page_data_map_.end()) @@ -42,10 +41,8 @@ class PrintPreviewDataStore : public base::RefCounted<PrintPreviewDataStore> { // Set/Update the preview data entry for the specified |index|. void SetPreviewDataForIndex(int index, const base::RefCountedBytes* data) { - if (index != printing::COMPLETE_PREVIEW_DOCUMENT_INDEX && - index < printing::FIRST_PAGE_INDEX) { + if (IsInvalidIndex(index)) return; - } page_data_map_[index] = const_cast<base::RefCountedBytes*>(data); } @@ -53,10 +50,8 @@ class PrintPreviewDataStore : public base::RefCounted<PrintPreviewDataStore> { // Returns the available draft page count. int GetAvailableDraftPageCount() { int page_data_map_size = page_data_map_.size(); - if (page_data_map_.find(printing::COMPLETE_PREVIEW_DOCUMENT_INDEX) != - page_data_map_.end()) { + if (ContainsKey(page_data_map_, printing::COMPLETE_PREVIEW_DOCUMENT_INDEX)) page_data_map_size--; - } return page_data_map_size; } @@ -73,6 +68,11 @@ class PrintPreviewDataStore : public base::RefCounted<PrintPreviewDataStore> { ~PrintPreviewDataStore() {} + static bool IsInvalidIndex(int index) { + return (index != printing::COMPLETE_PREVIEW_DOCUMENT_INDEX && + index < printing::FIRST_PAGE_INDEX); + } + PreviewPageDataMap page_data_map_; DISALLOW_COPY_AND_ASSIGN(PrintPreviewDataStore); @@ -90,37 +90,31 @@ PrintPreviewDataService::~PrintPreviewDataService() { } void PrintPreviewDataService::GetDataEntry( - const std::string& preview_ui_addr_str, + int32 preview_ui_id, int index, scoped_refptr<base::RefCountedBytes>* data_bytes) { *data_bytes = NULL; - PreviewDataStoreMap::iterator it = data_store_map_.find(preview_ui_addr_str); + PreviewDataStoreMap::const_iterator it = data_store_map_.find(preview_ui_id); if (it != data_store_map_.end()) it->second->GetPreviewDataForIndex(index, data_bytes); } void PrintPreviewDataService::SetDataEntry( - const std::string& preview_ui_addr_str, + int32 preview_ui_id, int index, const base::RefCountedBytes* data_bytes) { - PreviewDataStoreMap::iterator it = data_store_map_.find(preview_ui_addr_str); - if (it == data_store_map_.end()) - data_store_map_[preview_ui_addr_str] = new PrintPreviewDataStore(); + if (!ContainsKey(data_store_map_, preview_ui_id)) + data_store_map_[preview_ui_id] = new PrintPreviewDataStore(); - data_store_map_[preview_ui_addr_str]->SetPreviewDataForIndex(index, - data_bytes); + data_store_map_[preview_ui_id]->SetPreviewDataForIndex(index, data_bytes); } -void PrintPreviewDataService::RemoveEntry( - const std::string& preview_ui_addr_str) { - PreviewDataStoreMap::iterator it = data_store_map_.find(preview_ui_addr_str); - if (it != data_store_map_.end()) - data_store_map_.erase(it); +void PrintPreviewDataService::RemoveEntry(int32 preview_ui_id) { + data_store_map_.erase(preview_ui_id); } -int PrintPreviewDataService::GetAvailableDraftPageCount( - const std::string& preview_ui_addr_str) { - if (data_store_map_.find(preview_ui_addr_str) != data_store_map_.end()) - return data_store_map_[preview_ui_addr_str]->GetAvailableDraftPageCount(); - return 0; +int PrintPreviewDataService::GetAvailableDraftPageCount(int32 preview_ui_id) { + PreviewDataStoreMap::const_iterator it = data_store_map_.find(preview_ui_id); + return (it == data_store_map_.end()) ? + 0 : it->second->GetAvailableDraftPageCount(); } diff --git a/chrome/browser/printing/print_preview_data_service.h b/chrome/browser/printing/print_preview_data_service.h index d319d07..ca2fe83 100644 --- a/chrome/browser/printing/print_preview_data_service.h +++ b/chrome/browser/printing/print_preview_data_service.h @@ -28,7 +28,7 @@ class PrintPreviewDataService { // |printing::COMPLETE_PREVIEW_DOCUMENT_INDEX| to represent complete preview // data. Use |index| to retrieve a specific preview page data. |data| is set // to NULL if the requested page is not yet available. - void GetDataEntry(const std::string& preview_ui_addr_str, int index, + void GetDataEntry(int32 preview_ui_id, int index, scoped_refptr<base::RefCountedBytes>* data); // Set/Update the data entry in PrintPreviewDataStore. |index| is zero-based @@ -36,22 +36,22 @@ class PrintPreviewDataService { // preview data. Use |index| to set/update a specific preview page data. // NOTE: PrintPreviewDataStore owns the data. Do not refcount |data| before // calling this function. It will be refcounted in PrintPreviewDataStore. - void SetDataEntry(const std::string& preview_ui_addr_str, int index, + void SetDataEntry(int32 preview_ui_id, int index, const base::RefCountedBytes* data); // Remove the corresponding PrintPreviewUI entry from the map. - void RemoveEntry(const std::string& preview_ui_addr_str); + void RemoveEntry(int32 preview_ui_id); // Returns the available draft page count. - int GetAvailableDraftPageCount(const std::string& preview_ui_addr_str); + int GetAvailableDraftPageCount(int32 preview_ui_id); private: friend struct DefaultSingletonTraits<PrintPreviewDataService>; // 1:1 relationship between PrintPreviewUI and data store object. - // Key: Print preview UI address string. + // Key: PrintPreviewUI ID. // Value: Print preview data store object. - typedef std::map<std::string, scoped_refptr<PrintPreviewDataStore> > + typedef std::map<int32, scoped_refptr<PrintPreviewDataStore> > PreviewDataStoreMap; PrintPreviewDataService(); diff --git a/chrome/browser/printing/printing_message_filter.cc b/chrome/browser/printing/printing_message_filter.cc index c525080..c4aedae 100644 --- a/chrome/browser/printing/printing_message_filter.cc +++ b/chrome/browser/printing/printing_message_filter.cc @@ -374,10 +374,10 @@ void PrintingMessageFilter::OnUpdatePrintSettingsReply( } } -void PrintingMessageFilter::OnCheckForCancel(const std::string& preview_ui_addr, +void PrintingMessageFilter::OnCheckForCancel(int32 preview_ui_id, int preview_request_id, bool* cancel) { - PrintPreviewUI::GetCurrentPrintPreviewStatus(preview_ui_addr, + PrintPreviewUI::GetCurrentPrintPreviewStatus(preview_ui_id, preview_request_id, cancel); } diff --git a/chrome/browser/printing/printing_message_filter.h b/chrome/browser/printing/printing_message_filter.h index a7e95dd..d8fdc92 100644 --- a/chrome/browser/printing/printing_message_filter.h +++ b/chrome/browser/printing/printing_message_filter.h @@ -107,7 +107,7 @@ class PrintingMessageFilter : public content::BrowserMessageFilter { IPC::Message* reply_msg); // Check to see if print preview has been cancelled. - void OnCheckForCancel(const std::string& preview_ui_addr, + void OnCheckForCancel(int32 preview_ui_id, int preview_request_id, bool* cancel); diff --git a/chrome/browser/resources/print_preview/native_layer.js b/chrome/browser/resources/print_preview/native_layer.js index 4b3aa57..90eafa8 100644 --- a/chrome/browser/resources/print_preview/native_layer.js +++ b/chrome/browser/resources/print_preview/native_layer.js @@ -546,7 +546,7 @@ cr.define('print_preview', function() { /** * Called when no pipelining previewed pages. - * @param {string} previewUid Preview unique identifier. + * @param {number} previewUid Preview unique identifier. * @param {number} previewResponseId The preview request id that resulted in * this response. * @private @@ -564,7 +564,7 @@ cr.define('print_preview', function() { * Check if the settings have changed and request a regeneration if needed. * Called from PrintPreviewUI::OnDidPreviewPage(). * @param {number} pageNumber The page number, 0-based. - * @param {string} previewUid Preview unique identifier. + * @param {number} previewUid Preview unique identifier. * @param {number} previewResponseId The preview request id that resulted in * this response. * @private @@ -582,7 +582,7 @@ cr.define('print_preview', function() { * Update the print preview when new preview data is available. * Create the PDF plugin as needed. * Called from PrintPreviewUI::PreviewDataIsAvailable(). - * @param {string} previewUid Preview unique identifier. + * @param {number} previewUid Preview unique identifier. * @param {number} previewResponseId The preview request id that resulted in * this response. * @private diff --git a/chrome/browser/resources/print_preview/preview_generator.js b/chrome/browser/resources/print_preview/preview_generator.js index b7f4ba6..ac33649 100644 --- a/chrome/browser/resources/print_preview/preview_generator.js +++ b/chrome/browser/resources/print_preview/preview_generator.js @@ -207,22 +207,21 @@ cr.define('print_preview', function() { * @param {number} pageNumber Number of the page with respect to the * document. A value of 3 means it's the third page of the original * document. - * @param {string} previewUid Unique identifier of the preview. + * @param {number} previewUid Unique identifier of the preview. * @private */ dispatchPageReadyEvent_: function(previewIndex, pageNumber, previewUid) { var pageGenEvent = new cr.Event(PreviewGenerator.EventType.PAGE_READY); pageGenEvent.previewIndex = previewIndex; - pageGenEvent.previewUrl = - 'chrome://print/' + previewUid + '/' + (pageNumber - 1) + - '/print.pdf'; + pageGenEvent.previewUrl = 'chrome://print/' + previewUid.toString() + + '/' + (pageNumber - 1) + '/print.pdf'; this.dispatchEvent(pageGenEvent); }, /** * Dispatches a PREVIEW_START event. Signals that the preview should be * reloaded. - * @param {string} previewUid Unique identifier of the preview. + * @param {number} previewUid Unique identifier of the preview. * @param {number} index Index of the first page of the preview. * @private */ @@ -232,8 +231,8 @@ cr.define('print_preview', function() { if (!this.printTicketStore_.isDocumentModifiable) { index = -1; } - previewStartEvent.previewUrl = - 'chrome://print/' + previewUid + '/' + index + '/print.pdf'; + previewStartEvent.previewUrl = 'chrome://print/' + + previewUid.toString() + '/' + index + '/print.pdf'; this.dispatchEvent(previewStartEvent); }, diff --git a/chrome/browser/ui/webui/print_preview/print_preview_data_source.cc b/chrome/browser/ui/webui/print_preview/print_preview_data_source.cc index 05a7df8..0e2ea49 100644 --- a/chrome/browser/ui/webui/print_preview/print_preview_data_source.cc +++ b/chrome/browser/ui/webui/print_preview/print_preview_data_source.cc @@ -195,10 +195,14 @@ void PrintPreviewDataSource::StartDataRequest(const std::string& path, scoped_refptr<base::RefCountedBytes> data; std::vector<std::string> url_substr; base::SplitString(path, '/', &url_substr); + int preview_ui_id = -1; int page_index = 0; - if (url_substr.size() == 3 && base::StringToInt(url_substr[1], &page_index)) { + if (url_substr.size() == 3 && + base::StringToInt(url_substr[0], &preview_ui_id), + base::StringToInt(url_substr[1], &page_index) && + preview_ui_id >= 0) { PrintPreviewDataService::GetInstance()->GetDataEntry( - url_substr[0], page_index, &data); + preview_ui_id, page_index, &data); } if (data.get()) { SendResponse(request_id, data); diff --git a/chrome/browser/ui/webui/print_preview/print_preview_data_source.h b/chrome/browser/ui/webui/print_preview/print_preview_data_source.h index 20ab6a6..f2298f9 100644 --- a/chrome/browser/ui/webui/print_preview/print_preview_data_source.h +++ b/chrome/browser/ui/webui/print_preview/print_preview_data_source.h @@ -14,16 +14,16 @@ // PrintPreviewDataSource serves data for chrome://print requests. // // The format for requesting PDF data is as follows: -// chrome://print/<PrintPreviewUIAddrStr>/<PageIndex>/print.pdf +// chrome://print/<PrintPreviewUIID>/<PageIndex>/print.pdf // // Parameters (< > required): -// <PrintPreviewUIAddrStr> = Print preview UI identifier. +// <PrintPreviewUIID> = PrintPreview UI ID // <PageIndex> = Page index is zero-based or // |printing::COMPLETE_PREVIEW_DOCUMENT_INDEX| to represent // a print ready PDF. // // Example: -// chrome://print/0xab0123ef/10/print.pdf +// chrome://print/123/10/print.pdf // // Requests to chrome://print with paths not ending in /print.pdf are used // to return the markup or other resources for the print preview page itself. @@ -35,6 +35,7 @@ class PrintPreviewDataSource : public ChromeWebUIDataSource { virtual void StartDataRequest(const std::string& path, bool is_incognito, int request_id) OVERRIDE; + private: virtual ~PrintPreviewDataSource(); void Init(); diff --git a/chrome/browser/ui/webui/print_preview/print_preview_handler.cc b/chrome/browser/ui/webui/print_preview/print_preview_handler.cc index ab769bc..4a3f820 100644 --- a/chrome/browser/ui/webui/print_preview/print_preview_handler.cc +++ b/chrome/browser/ui/webui/print_preview/print_preview_handler.cc @@ -343,8 +343,8 @@ void PrintPreviewHandler::HandleGetPreview(const ListValue* args) { // Add an additional key in order to identify |print_preview_ui| later on // when calling PrintPreviewUI::GetCurrentPrintPreviewStatus() on the IO // thread. - settings->SetString(printing::kPreviewUIAddr, - print_preview_ui->GetPrintPreviewUIAddress()); + settings->SetInteger(printing::kPreviewUIID, + print_preview_ui->GetIDForPrintPreviewUI()); // Increment request count. ++regenerate_preview_request_count_; diff --git a/chrome/browser/ui/webui/print_preview/print_preview_ui.cc b/chrome/browser/ui/webui/print_preview/print_preview_ui.cc index e9d89ad..25c01c2 100644 --- a/chrome/browser/ui/webui/print_preview/print_preview_ui.cc +++ b/chrome/browser/ui/webui/print_preview/print_preview_ui.cc @@ -6,6 +6,7 @@ #include <map> +#include "base/id_map.h" #include "base/lazy_instance.h" #include "base/memory/ref_counted_memory.h" #include "base/metrics/histogram.h" @@ -37,50 +38,60 @@ using ui::ConstrainedWebDialogUI; namespace { // Thread-safe wrapper around a std::map to keep track of mappings from -// PrintPreviewUI addresses to most recent print preview request ids. +// PrintPreviewUI IDs to most recent print preview request IDs. class PrintPreviewRequestIdMapWithLock { public: PrintPreviewRequestIdMapWithLock() {} ~PrintPreviewRequestIdMapWithLock() {} - // Get the value for |addr|. Returns true and sets |out_value| on success. - bool Get(const std::string& addr, int* out_value) { + // Gets the value for |preview_id|. + // Returns true and sets |out_value| on success. + bool Get(int32 preview_id, int* out_value) { base::AutoLock lock(lock_); - PrintPreviewRequestIdMap::const_iterator it = map_.find(addr); + PrintPreviewRequestIdMap::const_iterator it = map_.find(preview_id); if (it == map_.end()) return false; *out_value = it->second; return true; } - // Sets the |value| for |addr|. - void Set(const std::string& addr, int value) { + // Sets the |value| for |preview_id|. + void Set(int32 preview_id, int value) { base::AutoLock lock(lock_); - map_[addr] = value; + map_[preview_id] = value; } - // Erase the entry for |addr|. - void Erase(const std::string& addr) { + // Erases the entry for |preview_id|. + void Erase(int32 preview_id) { base::AutoLock lock(lock_); - map_.erase(addr); + map_.erase(preview_id); } private: - typedef std::map<std::string, int> PrintPreviewRequestIdMap; + // Mapping from PrintPreviewUI ID to print preview request ID. + typedef std::map<int, int> PrintPreviewRequestIdMap; PrintPreviewRequestIdMap map_; base::Lock lock_; + + DISALLOW_COPY_AND_ASSIGN(PrintPreviewRequestIdMapWithLock); }; // Written to on the UI thread, read from any thread. base::LazyInstance<PrintPreviewRequestIdMapWithLock> g_print_preview_request_id_map = LAZY_INSTANCE_INITIALIZER; +// PrintPreviewUI IDMap used to avoid exposing raw pointer addresses to WebUI. +// Only accessed on the UI thread. +base::LazyInstance<IDMap<PrintPreviewUI> > + g_print_preview_ui_id_map = LAZY_INSTANCE_INITIALIZER; + } // namespace PrintPreviewUI::PrintPreviewUI(content::WebUI* web_ui) : ConstrainedWebDialogUI(web_ui), initial_preview_start_time_(base::TimeTicks::Now()), + id_(g_print_preview_ui_id_map.Get().Add(this)), handler_(NULL), source_is_modifiable_(true), tab_closed_(false) { @@ -92,34 +103,33 @@ PrintPreviewUI::PrintPreviewUI(content::WebUI* web_ui) handler_ = new PrintPreviewHandler(); web_ui->AddMessageHandler(handler_); - preview_ui_addr_str_ = GetPrintPreviewUIAddress(); - g_print_preview_request_id_map.Get().Set(preview_ui_addr_str_, -1); + g_print_preview_request_id_map.Get().Set(id_, -1); } PrintPreviewUI::~PrintPreviewUI() { - print_preview_data_service()->RemoveEntry(preview_ui_addr_str_); - g_print_preview_request_id_map.Get().Erase(preview_ui_addr_str_); + print_preview_data_service()->RemoveEntry(id_); + g_print_preview_request_id_map.Get().Erase(id_); + g_print_preview_ui_id_map.Get().Remove(id_); } void PrintPreviewUI::GetPrintPreviewDataForIndex( int index, scoped_refptr<base::RefCountedBytes>* data) { - print_preview_data_service()->GetDataEntry(preview_ui_addr_str_, index, data); + print_preview_data_service()->GetDataEntry(id_, index, data); } void PrintPreviewUI::SetPrintPreviewDataForIndex( int index, const base::RefCountedBytes* data) { - print_preview_data_service()->SetDataEntry(preview_ui_addr_str_, index, data); + print_preview_data_service()->SetDataEntry(id_, index, data); } void PrintPreviewUI::ClearAllPreviewData() { - print_preview_data_service()->RemoveEntry(preview_ui_addr_str_); + print_preview_data_service()->RemoveEntry(id_); } int PrintPreviewUI::GetAvailableDraftPageCount() { - return print_preview_data_service()->GetAvailableDraftPageCount( - preview_ui_addr_str_); + return print_preview_data_service()->GetAvailableDraftPageCount(id_); } void PrintPreviewUI::SetInitiatorTabURLAndTitle( @@ -140,24 +150,19 @@ void PrintPreviewUI::SetSourceIsModifiable(TabContents* print_preview_tab, } // static -void PrintPreviewUI::GetCurrentPrintPreviewStatus( - const std::string& preview_ui_addr, - int request_id, - bool* cancel) { +void PrintPreviewUI::GetCurrentPrintPreviewStatus(int32 preview_ui_id, + int request_id, + bool* cancel) { int current_id = -1; - if (!g_print_preview_request_id_map.Get().Get(preview_ui_addr, ¤t_id)) { + if (!g_print_preview_request_id_map.Get().Get(preview_ui_id, ¤t_id)) { *cancel = true; return; } *cancel = (request_id != current_id); } -std::string PrintPreviewUI::GetPrintPreviewUIAddress() const { - // 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); - return preview_ui_addr; +int32 PrintPreviewUI::GetIDForPrintPreviewUI() const { + return id_; } void PrintPreviewUI::OnPrintPreviewTabClosed() { @@ -182,7 +187,7 @@ void PrintPreviewUI::OnInitiatorTabClosed() { } void PrintPreviewUI::OnPrintPreviewRequest(int request_id) { - g_print_preview_request_id_map.Get().Set(preview_ui_addr_str_, request_id); + g_print_preview_request_id_map.Get().Set(id_, request_id); } void PrintPreviewUI::OnShowSystemDialog() { @@ -233,14 +238,14 @@ void PrintPreviewUI::OnDidPreviewPage(int page_number, int preview_request_id) { DCHECK_GE(page_number, 0); base::FundamentalValue number(page_number); - StringValue ui_identifier(preview_ui_addr_str_); + base::FundamentalValue ui_identifier(id_); base::FundamentalValue request_id(preview_request_id); web_ui()->CallJavascriptFunction( "onDidPreviewPage", number, ui_identifier, request_id); } void PrintPreviewUI::OnReusePreviewData(int preview_request_id) { - base::StringValue ui_identifier(preview_ui_addr_str_); + base::FundamentalValue ui_identifier(id_); base::FundamentalValue ui_preview_request_id(preview_request_id); web_ui()->CallJavascriptFunction("reloadPreviewPages", ui_identifier, ui_preview_request_id); @@ -258,7 +263,7 @@ void PrintPreviewUI::OnPreviewDataIsAvailable(int expected_pages_count, expected_pages_count); initial_preview_start_time_ = base::TimeTicks(); } - base::StringValue ui_identifier(preview_ui_addr_str_); + base::FundamentalValue ui_identifier(id_); base::FundamentalValue ui_preview_request_id(preview_request_id); web_ui()->CallJavascriptFunction("updatePrintPreview", ui_identifier, ui_preview_request_id); @@ -273,7 +278,7 @@ void PrintPreviewUI::OnFileSelectionCancelled() { } void PrintPreviewUI::OnCancelPendingPreviewRequest() { - g_print_preview_request_id_map.Get().Set(preview_ui_addr_str_, -1); + g_print_preview_request_id_map.Get().Set(id_, -1); } void PrintPreviewUI::OnPrintPreviewFailed() { diff --git a/chrome/browser/ui/webui/print_preview/print_preview_ui.h b/chrome/browser/ui/webui/print_preview/print_preview_ui.h index 05ea0ee..6ff2995 100644 --- a/chrome/browser/ui/webui/print_preview/print_preview_ui.h +++ b/chrome/browser/ui/webui/print_preview/print_preview_ui.h @@ -10,7 +10,6 @@ #include "base/gtest_prod_util.h" #include "base/memory/ref_counted.h" #include "base/time.h" -#include "chrome/browser/printing/print_preview_data_service.h" #include "ui/web_dialogs/constrained_web_dialog_ui.h" class PrintPreviewDataService; @@ -65,14 +64,14 @@ class PrintPreviewUI : public ui::ConstrainedWebDialogUI { bool source_is_modifiable); // Determines whether to cancel a print preview request based on - // |preview_ui_addr| and |request_id|. + // |preview_ui_id| and |request_id|. // Can be called from any thread. - static void GetCurrentPrintPreviewStatus(const std::string& preview_ui_addr, + static void GetCurrentPrintPreviewStatus(int32 preview_ui_id, int request_id, bool* cancel); - // Returns a string to uniquely identify this PrintPreviewUI. - std::string GetPrintPreviewUIAddress() const; + // Returns an id to uniquely identify this PrintPreviewUI. + int32 GetIDForPrintPreviewUI() const; // Notifies the Web UI of a print preview request with |request_id|. void OnPrintPreviewRequest(int request_id); @@ -86,10 +85,9 @@ class PrintPreviewUI : public ui::ConstrainedWebDialogUI { // Notifies the Web UI of the default page layout according to the currently // selected printer and page size. - void OnDidGetDefaultPageLayout( - const printing::PageSizeMargins& page_layout, - const gfx::Rect& printable_area, - bool has_custom_page_size_style); + void OnDidGetDefaultPageLayout(const printing::PageSizeMargins& page_layout, + const gfx::Rect& printable_area, + bool has_custom_page_size_style); // Notifies the Web UI that the 0-based page |page_number| has been rendered. // |preview_request_id| indicates wich request resulted in this response. @@ -161,8 +159,9 @@ class PrintPreviewUI : public ui::ConstrainedWebDialogUI { base::TimeTicks initial_preview_start_time_; - // Store the PrintPreviewUI address string. - std::string preview_ui_addr_str_; + // The unique ID for this class instance. Stored here to avoid calling + // GetIDForPrintPreviewUI() everywhere. + const int32 id_; // Weak pointer to the WebUI handler. PrintPreviewHandler* handler_; diff --git a/chrome/browser/ui/webui/print_preview/print_preview_ui_unittest.cc b/chrome/browser/ui/webui/print_preview/print_preview_ui_unittest.cc index 106c7bc..0ff0896a 100644 --- a/chrome/browser/ui/webui/print_preview/print_preview_ui_unittest.cc +++ b/chrome/browser/ui/webui/print_preview/print_preview_ui_unittest.cc @@ -25,8 +25,12 @@ using content::WebContentsTester; namespace { -const unsigned char blob1[] = - "12346102356120394751634516591348710478123649165419234519234512349134"; +base::RefCountedBytes* CreateTestData() { + const unsigned char blob1[] = + "12346102356120394751634516591348710478123649165419234519234512349134"; + std::vector<unsigned char> preview_data(blob1, blob1 + sizeof(blob1)); + return new base::RefCountedBytes(preview_data); +} size_t GetConstrainedWindowCount(TabContents* tab) { return tab->constrained_window_tab_helper()->constrained_window_count(); @@ -76,9 +80,7 @@ TEST_F(PrintPreviewUIUnitTest, PrintPreviewData) { &data); EXPECT_EQ(NULL, data.get()); - std::vector<unsigned char> preview_data(blob1, blob1 + sizeof(blob1)); - scoped_refptr<base::RefCountedBytes> dummy_data = - new base::RefCountedBytes(preview_data); + scoped_refptr<base::RefCountedBytes> dummy_data = CreateTestData(); preview_ui->SetPrintPreviewDataForIndex( printing::COMPLETE_PREVIEW_DOCUMENT_INDEX, @@ -127,9 +129,7 @@ TEST_F(PrintPreviewUIUnitTest, PrintPreviewDraftPages) { preview_ui->GetPrintPreviewDataForIndex(printing::FIRST_PAGE_INDEX, &data); EXPECT_EQ(NULL, data.get()); - std::vector<unsigned char> preview_data(blob1, blob1 + sizeof(blob1)); - scoped_refptr<base::RefCountedBytes> dummy_data = - new base::RefCountedBytes(preview_data); + scoped_refptr<base::RefCountedBytes> dummy_data = CreateTestData(); preview_ui->SetPrintPreviewDataForIndex(printing::FIRST_PAGE_INDEX, dummy_data.get()); @@ -185,12 +185,13 @@ TEST_F(PrintPreviewUIUnitTest, GetCurrentPrintPreviewStatus) { // Test with invalid |preview_ui_addr|. bool cancel = false; - preview_ui->GetCurrentPrintPreviewStatus("invalid", 0, &cancel); + const int32 kInvalidId = -5; + preview_ui->GetCurrentPrintPreviewStatus(kInvalidId, 0, &cancel); EXPECT_TRUE(cancel); const int kFirstRequestId = 1000; const int kSecondRequestId = 1001; - const std::string preview_ui_addr = preview_ui->GetPrintPreviewUIAddress(); + const int32 preview_ui_addr = preview_ui->GetIDForPrintPreviewUI(); // Test with kFirstRequestId. preview_ui->OnPrintPreviewRequest(kFirstRequestId); diff --git a/chrome/common/print_messages.cc b/chrome/common/print_messages.cc index bc92d7767..126e248 100644 --- a/chrome/common/print_messages.cc +++ b/chrome/common/print_messages.cc @@ -21,7 +21,7 @@ PrintMsg_Print_Params::PrintMsg_Print_Params() document_cookie(0), selection_only(false), supports_alpha_blend(false), - preview_ui_addr(), + preview_ui_id(-1), preview_request_id(0), is_first_request(false), print_scaling_option(WebKit::WebPrintScalingOptionSourceSize), @@ -47,7 +47,7 @@ void PrintMsg_Print_Params::Reset() { document_cookie = 0; selection_only = false; supports_alpha_blend = false; - preview_ui_addr = std::string(); + preview_ui_id = -1; preview_request_id = 0; is_first_request = false; print_scaling_option = WebKit::WebPrintScalingOptionSourceSize; diff --git a/chrome/common/print_messages.h b/chrome/common/print_messages.h index 0efea96..bdd1fbe 100644 --- a/chrome/common/print_messages.h +++ b/chrome/common/print_messages.h @@ -39,7 +39,7 @@ struct PrintMsg_Print_Params { int document_cookie; bool selection_only; bool supports_alpha_blend; - std::string preview_ui_addr; + int32 preview_ui_id; int preview_request_id; bool is_first_request; WebKit::WebPrintScalingOption print_scaling_option; @@ -110,7 +110,7 @@ IPC_STRUCT_TRAITS_BEGIN(PrintMsg_Print_Params) // *** Parameters below are used only for print preview. *** // The print preview ui associated with this request. - IPC_STRUCT_TRAITS_MEMBER(preview_ui_addr) + IPC_STRUCT_TRAITS_MEMBER(preview_ui_id) // The id of the preview request. IPC_STRUCT_TRAITS_MEMBER(preview_request_id) @@ -386,7 +386,7 @@ IPC_MESSAGE_ROUTED1(PrintHostMsg_DidPreviewPage, // Asks the browser whether the print preview has been cancelled. IPC_SYNC_MESSAGE_ROUTED2_1(PrintHostMsg_CheckForCancel, - std::string /* print preview ui address */, + int32 /* PrintPreviewUI ID */, int /* request id */, bool /* print preview cancelled */) diff --git a/chrome/renderer/chrome_mock_render_thread.cc b/chrome/renderer/chrome_mock_render_thread.cc index b946dd6..dd72285 100644 --- a/chrome/renderer/chrome_mock_render_thread.cc +++ b/chrome/renderer/chrome_mock_render_thread.cc @@ -4,20 +4,23 @@ #include "chrome/renderer/chrome_mock_render_thread.h" -#include <fcntl.h> +#include <vector> -#include "base/file_util.h" -#include "base/process_util.h" +#include "base/values.h" #include "chrome/common/extensions/extension_messages.h" #include "chrome/common/print_messages.h" -#include "chrome/common/render_messages.h" -#include "chrome/common/url_constants.h" -#include "ipc/ipc_message_utils.h" +#include "chrome/renderer/mock_printer.h" #include "ipc/ipc_sync_message.h" #include "printing/print_job_constants.h" #include "printing/page_range.h" #include "testing/gtest/include/gtest/gtest.h" +#if defined(OS_CHROMEOS) +#include <fcntl.h> + +#include "base/file_util.h" +#endif + ChromeMockRenderThread::ChromeMockRenderThread() : printer_(new MockPrinter), print_dialog_user_response_(true), @@ -64,9 +67,11 @@ bool ChromeMockRenderThread::OnMessageReceived(const IPC::Message& msg) { } void ChromeMockRenderThread::OnMsgOpenChannelToExtension( - int routing_id, const std::string& source_extension_id, + int routing_id, + const std::string& source_extension_id, const std::string& target_extension_id, - const std::string& channel_name, int* port_id) { + const std::string& channel_name, + int* port_id) { *port_id = 0; } @@ -93,14 +98,13 @@ void ChromeMockRenderThread::OnTempFileForPrintingWritten(int render_view_id, void ChromeMockRenderThread::OnGetDefaultPrintSettings( PrintMsg_Print_Params* params) { - if (printer_.get()) - printer_->GetDefaultPrintSettings(params); + printer_->GetDefaultPrintSettings(params); } void ChromeMockRenderThread::OnScriptedPrint( const PrintHostMsg_ScriptedPrint_Params& params, PrintMsg_PrintPages_Params* settings) { - if (print_dialog_user_response_ && printer_.get()) { + if (print_dialog_user_response_) { printer_->ScriptedPrint(params.cookie, params.expected_pages_count, params.has_selection, @@ -110,14 +114,12 @@ void ChromeMockRenderThread::OnScriptedPrint( void ChromeMockRenderThread::OnDidGetPrintedPagesCount( int cookie, int number_pages) { - if (printer_.get()) - printer_->SetPrintedPagesCount(cookie, number_pages); + printer_->SetPrintedPagesCount(cookie, number_pages); } void ChromeMockRenderThread::OnDidPrintPage( const PrintHostMsg_DidPrintPage_Params& params) { - if (printer_.get()) - printer_->PrintPage(params); + printer_->PrintPage(params); } void ChromeMockRenderThread::OnDidGetPreviewPageCount( @@ -127,14 +129,13 @@ void ChromeMockRenderThread::OnDidGetPreviewPageCount( void ChromeMockRenderThread::OnDidPreviewPage( const PrintHostMsg_DidPreviewPage_Params& params) { - DCHECK(params.page_number >= printing::FIRST_PAGE_INDEX); + DCHECK_GE(params.page_number, printing::FIRST_PAGE_INDEX); print_preview_pages_remaining_--; } -void ChromeMockRenderThread::OnCheckForCancel( - const std::string& preview_ui_addr, - int preview_request_id, - bool* cancel) { +void ChromeMockRenderThread::OnCheckForCancel(int32 preview_ui_id, + int preview_request_id, + bool* cancel) { *cancel = (print_preview_pages_remaining_ == print_preview_cancel_page_number_); } @@ -155,36 +156,38 @@ void ChromeMockRenderThread::OnUpdatePrintSettings( !job_settings.GetString(printing::kSettingDeviceName, &dummy_string) || !job_settings.GetInteger(printing::kSettingDuplexMode, NULL) || !job_settings.GetInteger(printing::kSettingCopies, NULL) || - !job_settings.GetString(printing::kPreviewUIAddr, &dummy_string) || + !job_settings.GetInteger(printing::kPreviewUIID, NULL) || !job_settings.GetInteger(printing::kPreviewRequestID, NULL) || !job_settings.GetInteger(printing::kSettingMarginsType, &margins_type)) { return; } // Just return the default settings. - if (printer_.get()) { - const ListValue* page_range_array; - printing::PageRanges new_ranges; - if (job_settings.GetList(printing::kSettingPageRange, &page_range_array)) { - for (size_t index = 0; index < page_range_array->GetSize(); ++index) { - const base::DictionaryValue* dict; - if (!page_range_array->GetDictionary(index, &dict)) - continue; - printing::PageRange range; - if (!dict->GetInteger(printing::kSettingPageRangeFrom, &range.from) || - !dict->GetInteger(printing::kSettingPageRangeTo, &range.to)) { - continue; - } - // Page numbers are 1-based in the dictionary. - // Page numbers are 0-based for the printing context. - range.from--; - range.to--; - new_ranges.push_back(range); + const ListValue* page_range_array; + printing::PageRanges new_ranges; + if (job_settings.GetList(printing::kSettingPageRange, &page_range_array)) { + for (size_t index = 0; index < page_range_array->GetSize(); ++index) { + const base::DictionaryValue* dict; + if (!page_range_array->GetDictionary(index, &dict)) + continue; + printing::PageRange range; + if (!dict->GetInteger(printing::kSettingPageRangeFrom, &range.from) || + !dict->GetInteger(printing::kSettingPageRangeTo, &range.to)) { + continue; } + // Page numbers are 1-based in the dictionary. + // Page numbers are 0-based for the printing context. + range.from--; + range.to--; + new_ranges.push_back(range); } - std::vector<int> pages(printing::PageRange::GetPages(new_ranges)); - printer_->UpdateSettings(document_cookie, params, pages, margins_type); } + std::vector<int> pages(printing::PageRange::GetPages(new_ranges)); + printer_->UpdateSettings(document_cookie, params, pages, margins_type); +} + +MockPrinter* ChromeMockRenderThread::printer() { + return printer_.get(); } void ChromeMockRenderThread::set_print_dialog_user_response(bool response) { @@ -195,6 +198,6 @@ void ChromeMockRenderThread::set_print_preview_cancel_page_number(int page) { print_preview_cancel_page_number_ = page; } -int ChromeMockRenderThread::print_preview_pages_remaining() { +int ChromeMockRenderThread::print_preview_pages_remaining() const { return print_preview_pages_remaining_; } diff --git a/chrome/renderer/chrome_mock_render_thread.h b/chrome/renderer/chrome_mock_render_thread.h index f0bef66..5db8826 100644 --- a/chrome/renderer/chrome_mock_render_thread.h +++ b/chrome/renderer/chrome_mock_render_thread.h @@ -8,16 +8,16 @@ #include <string> #include "base/compiler_specific.h" -#include "chrome/common/extensions/extension_set.h" -#include "chrome/renderer/mock_printer.h" #include "content/public/test/mock_render_thread.h" namespace base { class DictionaryValue; } +class MockPrinter; struct PrintHostMsg_DidGetPreviewPageCount_Params; struct PrintHostMsg_DidPreviewPage_Params; +struct PrintHostMsg_DidPrintPage_Params; struct PrintHostMsg_ScriptedPrint_Params; struct PrintMsg_PrintPages_Params; struct PrintMsg_Print_Params; @@ -33,7 +33,7 @@ class ChromeMockRenderThread : public content::MockRenderThread { // The following functions are called by the test itself. // Returns the pseudo-printer instance. - MockPrinter* printer() const { return printer_.get(); } + MockPrinter* printer(); // Call with |response| set to true if the user wants to print. // False if the user decides to cancel. @@ -43,7 +43,7 @@ class ChromeMockRenderThread : public content::MockRenderThread { void set_print_preview_cancel_page_number(int page); // Get the number of pages to generate for print preview. - int print_preview_pages_remaining(); + int print_preview_pages_remaining() const; private: // Overrides base class implementation to add custom handling for @@ -51,10 +51,11 @@ class ChromeMockRenderThread : public content::MockRenderThread { virtual bool OnMessageReceived(const IPC::Message& msg) OVERRIDE; // The callee expects to be returned a valid channel_id. - void OnMsgOpenChannelToExtension( - int routing_id, const std::string& extension_id, - const std::string& source_extension_id, - const std::string& target_extension_id, int* port_id); + void OnMsgOpenChannelToExtension(int routing_id, + const std::string& extension_id, + const std::string& source_extension_id, + const std::string& target_extension_id, + int* port_id); #if defined(OS_CHROMEOS) void OnAllocateTempFileForPrinting(base::FileDescriptor* renderer_fd, @@ -74,7 +75,7 @@ class ChromeMockRenderThread : public content::MockRenderThread { void OnDidGetPreviewPageCount( const PrintHostMsg_DidGetPreviewPageCount_Params& params); void OnDidPreviewPage(const PrintHostMsg_DidPreviewPage_Params& params); - void OnCheckForCancel(const std::string& preview_ui_addr, + void OnCheckForCancel(int32 preview_ui_id, int preview_request_id, bool* cancel); diff --git a/chrome/renderer/print_web_view_helper.cc b/chrome/renderer/print_web_view_helper.cc index acbc450..0e03cc0 100644 --- a/chrome/renderer/print_web_view_helper.cc +++ b/chrome/renderer/print_web_view_helper.cc @@ -1437,8 +1437,8 @@ bool PrintWebViewHelper::UpdatePrintSettings( if (!print_for_preview_) { // Validate expected print preview settings. - if (!job_settings->GetString(printing::kPreviewUIAddr, - &(settings.params.preview_ui_addr)) || + if (!job_settings->GetInteger(printing::kPreviewUIID, + &(settings.params.preview_ui_id)) || !job_settings->GetInteger(printing::kPreviewRequestID, &(settings.params.preview_request_id)) || !job_settings->GetBoolean(printing::kIsFirstRequest, @@ -1608,12 +1608,12 @@ void PrintWebViewHelper::RequestPrintPreview(PrintPreviewRequestType type) { } bool PrintWebViewHelper::CheckForCancel() { + const PrintMsg_Print_Params& print_params = print_pages_params_->params; bool cancel = false; - Send(new PrintHostMsg_CheckForCancel( - routing_id(), - print_pages_params_->params.preview_ui_addr, - print_pages_params_->params.preview_request_id, - &cancel)); + Send(new PrintHostMsg_CheckForCancel(routing_id(), + print_params.preview_ui_id, + print_params.preview_request_id, + &cancel)); if (cancel) notify_browser_of_print_failure_ = false; return cancel; diff --git a/chrome/renderer/print_web_view_helper_browsertest.cc b/chrome/renderer/print_web_view_helper_browsertest.cc index b06418f..c494634 100644 --- a/chrome/renderer/print_web_view_helper_browsertest.cc +++ b/chrome/renderer/print_web_view_helper_browsertest.cc @@ -2,8 +2,10 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +#include "base/command_line.h" #include "chrome/common/chrome_switches.h" #include "chrome/common/print_messages.h" +#include "chrome/renderer/mock_printer.h" #include "chrome/renderer/print_web_view_helper.h" #include "chrome/test/base/chrome_render_view_test.h" #include "content/public/renderer/render_view.h" @@ -70,7 +72,7 @@ void CreatePrintSettingsDictionary(DictionaryValue* dict) { dict->SetInteger(printing::kSettingDuplexMode, printing::SIMPLEX); dict->SetInteger(printing::kSettingCopies, 1); dict->SetString(printing::kSettingDeviceName, "dummy"); - dict->SetString(printing::kPreviewUIAddr, "0xb33fbeef"); + dict->SetInteger(printing::kPreviewUIID, 4); dict->SetInteger(printing::kPreviewRequestID, 12345); dict->SetBoolean(printing::kIsFirstRequest, true); dict->SetInteger(printing::kSettingMarginsType, printing::DEFAULT_MARGINS); diff --git a/printing/print_job_constants.cc b/printing/print_job_constants.cc index 42a0d48..a93da56 100644 --- a/printing/print_job_constants.cc +++ b/printing/print_job_constants.cc @@ -13,7 +13,7 @@ const char kIsFirstRequest[] = "isFirstRequest"; const char kPreviewRequestID[] = "requestID"; // Unique ID to identify a print preview UI. -const char kPreviewUIAddr[] = "previewUIAddr"; +const char kPreviewUIID[] = "previewUIID"; // Print using cloud print: true if selected, false if not. const char kSettingCloudPrintId[] = "cloudPrintID"; diff --git a/printing/print_job_constants.h b/printing/print_job_constants.h index b5efef0..d9a6006 100644 --- a/printing/print_job_constants.h +++ b/printing/print_job_constants.h @@ -12,7 +12,7 @@ namespace printing { PRINTING_EXPORT extern const char kIsFirstRequest[]; PRINTING_EXPORT extern const char kPreviewRequestID[]; -PRINTING_EXPORT extern const char kPreviewUIAddr[]; +PRINTING_EXPORT extern const char kPreviewUIID[]; PRINTING_EXPORT extern const char kSettingCloudPrintId[]; PRINTING_EXPORT extern const char kSettingCloudPrintDialog[]; PRINTING_EXPORT extern const char kSettingCollate[]; @@ -116,8 +116,8 @@ enum ColorModels { RGBA, COLORMODE_COLOR, // Used in samsung printer ppds. COLORMODE_MONOCHROME, // Used in samsung printer ppds. - HP_COLOR_COLOR, // Used in HP color printer ppds. - HP_COLOR_BLACK, // Used in HP color printer ppds. + HP_COLOR_COLOR, // Used in HP color printer ppds. + HP_COLOR_BLACK, // Used in HP color printer ppds. PRINTOUTMODE_NORMAL, // Used in foomatic ppds. PRINTOUTMODE_NORMAL_GRAY, // Used in foomatic ppds. PROCESSCOLORMODEL_CMYK, // Used in canon printer ppds. |