Chromium Code Reviews| Index: chrome/browser/ui/webui/print_preview_ui.cc |
| =================================================================== |
| --- chrome/browser/ui/webui/print_preview_ui.cc (revision 97484) |
| +++ 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" |
| @@ -13,12 +17,27 @@ |
| #include "chrome/browser/ui/webui/print_preview_handler.h" |
| #include "chrome/common/print_messages.h" |
| #include "content/browser/tab_contents/tab_contents.h" |
| +#include "printing/print_job_constants.h" |
| +namespace { |
| + |
| +// Mapping from PrintPreviewUI address to print preview request id; |
|
kmadhusu
2011/08/19 19:51:03
Document that we always store the latest print pre
Lei Zhang
2011/08/19 22:50:28
Done.
|
| +typedef std::map<std::string, int> PrintPreviewRequestIdMap; |
| + |
| +struct PrintPreviewRequestIdMapWithLock { |
| + PrintPreviewRequestIdMap map; |
| + base::Lock lock; |
| +}; |
| + |
| +// Written to on the UI thread, read on IO thread. |
| +static base::LazyInstance<PrintPreviewRequestIdMapWithLock> |
|
kmadhusu
2011/08/19 19:51:03
static is not required here.
Lei Zhang
2011/08/19 22:50:28
Done.
|
| + 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)); |
| @@ -28,15 +47,20 @@ |
| 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(); |
| + |
| + // TODO(kmadhusu): Add code to update PrintPreviewStatus so the current |
|
kmadhusu
2011/08/19 19:51:03
Do you still need this TODO?
Lei Zhang
2011/08/19 22:50:28
Nope.
|
| + // requested page is not always INVALID_PAGE_INDEX. |
| + base::AutoLock lock(g_print_preview_request_id_map.Get().lock); |
| + g_print_preview_request_id_map.Get().map[preview_ui_addr_str_] = -1; |
| } |
| PrintPreviewUI::~PrintPreviewUI() { |
| print_preview_data_service()->RemoveEntry(preview_ui_addr_str_); |
| + |
| + base::AutoLock lock(g_print_preview_request_id_map.Get().lock); |
| + PrintPreviewRequestIdMap* map = &g_print_preview_request_id_map.Get().map; |
| + map->erase(preview_ui_addr_str_); |
| } |
| void PrintPreviewUI::GetPrintPreviewDataForIndex( |
| @@ -61,6 +85,30 @@ |
| initiator_tab_title_ = job_title; |
| } |
| +// static |
| +bool PrintPreviewUI::GetCurrentPrintPreviewStatus( |
| + const std::string& preview_ui_addr, |
| + int request_id, |
| + bool* cancel) { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| + |
| + base::AutoLock lock(g_print_preview_request_id_map.Get().lock); |
| + PrintPreviewRequestIdMap::const_iterator it = |
| + g_print_preview_request_id_map.Get().map.find(preview_ui_addr); |
| + if (it == g_print_preview_request_id_map.Get().map.end()) |
|
kmadhusu
2011/08/19 19:51:03
How about replacing line #98-101 with
*cancel = t
Lei Zhang
2011/08/19 22:50:28
Done.
|
| + return false; |
| + *cancel = (request_id != it->second); |
| + return true; |
| +} |
| + |
| +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::OnInitiatorTabCrashed() { |
| StringValue initiator_tab_url(initiator_url_); |
| CallJavascriptFunction("onInitiatorTabCrashed", initiator_tab_url); |
| @@ -71,14 +119,14 @@ |
| CallJavascriptFunction("onInitiatorTabClosed", initiator_tab_url); |
| } |
| -void PrintPreviewUI::OnPrintPreviewRequest() { |
| - request_count_++; |
| +void PrintPreviewUI::OnPrintPreviewRequest(int request_id) { |
| + base::AutoLock lock(g_print_preview_request_id_map.Get().lock); |
| + g_print_preview_request_id_map.Get().map[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); |
| @@ -97,8 +145,6 @@ |
| } |
| void PrintPreviewUI::OnReusePreviewData(int preview_request_id) { |
| - DecrementRequestCount(); |
| - |
| base::StringValue ui_identifier(preview_ui_addr_str_); |
| base::FundamentalValue ui_preview_request_id(preview_request_id); |
| CallJavascriptFunction("reloadPreviewPages", ui_identifier, |
| @@ -109,7 +155,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", |
| @@ -133,27 +178,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_; |
| -} |