Skip to content

Commit

Permalink
Print preview: Use an ID instead of memory pointer string in WebUI.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
thestig@chromium.org committed Aug 24, 2012
1 parent 0ccc7e3 commit 116d096
Show file tree
Hide file tree
Showing 20 changed files with 187 additions and 178 deletions.
48 changes: 21 additions & 27 deletions chrome/browser/printing/print_preview_data_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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())
Expand All @@ -42,21 +41,17 @@ 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);
}

// 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;
}

Expand All @@ -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);
Expand All @@ -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();
}
12 changes: 6 additions & 6 deletions chrome/browser/printing/print_preview_data_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,30 +28,30 @@ 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
// or |printing::COMPLETE_PREVIEW_DOCUMENT_INDEX| to represent complete
// 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();
Expand Down
4 changes: 2 additions & 2 deletions chrome/browser/printing/printing_message_filter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
2 changes: 1 addition & 1 deletion chrome/browser/printing/printing_message_filter.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
6 changes: 3 additions & 3 deletions chrome/browser/resources/print_preview/native_layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
13 changes: 6 additions & 7 deletions chrome/browser/resources/print_preview/preview_generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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);
},

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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_;
Expand Down
Loading

0 comments on commit 116d096

Please sign in to comment.