| Index: chrome/browser/ui/webui/print_preview_ui.cc
|
| ===================================================================
|
| --- chrome/browser/ui/webui/print_preview_ui.cc (revision 98330)
|
| +++ chrome/browser/ui/webui/print_preview_ui.cc (working copy)
|
| @@ -4,8 +4,12 @@
|
|
|
| #include "chrome/browser/ui/webui/print_preview_ui.h"
|
|
|
| +#include <map>
|
| +
|
| +#include "base/lazy_instance.h"
|
| #include "base/metrics/histogram.h"
|
| #include "base/string_util.h"
|
| +#include "base/synchronization/lock.h"
|
| #include "base/values.h"
|
| #include "chrome/browser/printing/print_preview_data_service.h"
|
| #include "chrome/browser/profiles/profile.h"
|
| @@ -14,11 +18,55 @@
|
| #include "chrome/common/print_messages.h"
|
| #include "content/browser/tab_contents/tab_contents.h"
|
|
|
| +#include "printing/print_job_constants.h"
|
| +
|
| +namespace {
|
| +
|
| +// Thread-safe wrapper around a std::map to keep track of mappings from
|
| +// PrintPreviewUI addresses 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) {
|
| + base::AutoLock lock(lock_);
|
| + PrintPreviewRequestIdMap::const_iterator it = map_.find(addr);
|
| + 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) {
|
| + base::AutoLock lock(lock_);
|
| + map_[addr] = value;
|
| + }
|
| +
|
| + // Erase the entry for |addr|.
|
| + void Erase(const std::string& addr) {
|
| + base::AutoLock lock(lock_);
|
| + map_.erase(addr);
|
| + }
|
| +
|
| + private:
|
| + typedef std::map<std::string, int> PrintPreviewRequestIdMap;
|
| +
|
| + PrintPreviewRequestIdMap map_;
|
| + base::Lock lock_;
|
| +};
|
| +
|
| +// Written to on the UI thread, read from any thread.
|
| +base::LazyInstance<PrintPreviewRequestIdMapWithLock>
|
| + g_print_preview_request_id_map(base::LINKER_INITIALIZED);
|
| +
|
| +} // namespace
|
| +
|
| PrintPreviewUI::PrintPreviewUI(TabContents* contents)
|
| : ChromeWebUI(contents),
|
| - initial_preview_start_time_(base::TimeTicks::Now()),
|
| - request_count_(0U),
|
| - document_cookie_(0) {
|
| + initial_preview_start_time_(base::TimeTicks::Now()) {
|
| // WebUI owns |handler_|.
|
| handler_ = new PrintPreviewHandler();
|
| AddMessageHandler(handler_->Attach(this));
|
| @@ -27,15 +75,15 @@
|
| 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;
|
| + preview_ui_addr_str_ = GetPrintPreviewUIAddress();
|
| +
|
| + g_print_preview_request_id_map.Get().Set(preview_ui_addr_str_, -1);
|
| }
|
|
|
| PrintPreviewUI::~PrintPreviewUI() {
|
| print_preview_data_service()->RemoveEntry(preview_ui_addr_str_);
|
| +
|
| + g_print_preview_request_id_map.Get().Erase(preview_ui_addr_str_);
|
| }
|
|
|
| void PrintPreviewUI::GetPrintPreviewDataForIndex(
|
| @@ -53,20 +101,40 @@
|
| print_preview_data_service()->RemoveEntry(preview_ui_addr_str_);
|
| }
|
|
|
| +// static
|
| +void PrintPreviewUI::GetCurrentPrintPreviewStatus(
|
| + const std::string& preview_ui_addr,
|
| + int request_id,
|
| + bool* cancel) {
|
| + int current_id = -1;
|
| + if (!g_print_preview_request_id_map.Get().Get(preview_ui_addr, ¤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;
|
| +}
|
| +
|
| void PrintPreviewUI::OnInitiatorTabClosed(
|
| const std::string& initiator_url) {
|
| StringValue initiator_tab_url(initiator_url);
|
| CallJavascriptFunction("onInitiatorTabClosed", initiator_tab_url);
|
| }
|
|
|
| -void PrintPreviewUI::OnPrintPreviewRequest() {
|
| - request_count_++;
|
| +void PrintPreviewUI::OnPrintPreviewRequest(int request_id) {
|
| + g_print_preview_request_id_map.Get().Set(preview_ui_addr_str_, request_id);
|
| }
|
|
|
| void PrintPreviewUI::OnDidGetPreviewPageCount(
|
| const PrintHostMsg_DidGetPreviewPageCount_Params& params) {
|
| DCHECK_GT(params.page_count, 0);
|
| - document_cookie_ = params.document_cookie;
|
| base::FundamentalValue count(params.page_count);
|
| base::FundamentalValue modifiable(params.is_modifiable);
|
| base::FundamentalValue request_id(params.preview_request_id);
|
| @@ -84,8 +152,6 @@
|
| }
|
|
|
| void PrintPreviewUI::OnReusePreviewData(int preview_request_id) {
|
| - DecrementRequestCount();
|
| -
|
| StringValue ui_identifier(preview_ui_addr_str_);
|
| FundamentalValue ui_preview_request_id(preview_request_id);
|
| CallJavascriptFunction("reloadPreviewPages", ui_identifier,
|
| @@ -97,7 +163,6 @@
|
| int preview_request_id) {
|
| VLOG(1) << "Print preview request finished with "
|
| << expected_pages_count << " pages";
|
| - DecrementRequestCount();
|
|
|
| if (!initial_preview_start_time_.is_null()) {
|
| UMA_HISTOGRAM_TIMES("PrintPreview.InitalDisplayTime",
|
| @@ -122,27 +187,9 @@
|
| }
|
|
|
| void PrintPreviewUI::OnPrintPreviewFailed() {
|
| - DecrementRequestCount();
|
| CallJavascriptFunction("printPreviewFailed");
|
| }
|
|
|
| -void PrintPreviewUI::OnPrintPreviewCancelled() {
|
| - DecrementRequestCount();
|
| -}
|
| -
|
| -bool PrintPreviewUI::HasPendingRequests() {
|
| - return request_count_ > 1;
|
| -}
|
| -
|
| PrintPreviewDataService* PrintPreviewUI::print_preview_data_service() {
|
| return PrintPreviewDataService::GetInstance();
|
| }
|
| -
|
| -void PrintPreviewUI::DecrementRequestCount() {
|
| - if (request_count_ > 0)
|
| - request_count_--;
|
| -}
|
| -
|
| -int PrintPreviewUI::document_cookie() {
|
| - return document_cookie_;
|
| -}
|
|
|