Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/ui/webui/print_preview_ui.h" | 5 #include "chrome/browser/ui/webui/print_preview_ui.h" |
| 6 | 6 |
| 7 #include <map> | |
| 8 | |
| 9 #include "base/lazy_instance.h" | |
| 7 #include "base/metrics/histogram.h" | 10 #include "base/metrics/histogram.h" |
| 8 #include "base/string_util.h" | 11 #include "base/string_util.h" |
| 12 #include "base/synchronization/lock.h" | |
| 9 #include "base/values.h" | 13 #include "base/values.h" |
| 10 #include "chrome/browser/printing/print_preview_data_service.h" | 14 #include "chrome/browser/printing/print_preview_data_service.h" |
| 11 #include "chrome/browser/profiles/profile.h" | 15 #include "chrome/browser/profiles/profile.h" |
| 12 #include "chrome/browser/ui/webui/print_preview_data_source.h" | 16 #include "chrome/browser/ui/webui/print_preview_data_source.h" |
| 13 #include "chrome/browser/ui/webui/print_preview_handler.h" | 17 #include "chrome/browser/ui/webui/print_preview_handler.h" |
| 14 #include "chrome/common/print_messages.h" | 18 #include "chrome/common/print_messages.h" |
| 15 #include "content/browser/tab_contents/tab_contents.h" | 19 #include "content/browser/tab_contents/tab_contents.h" |
| 20 #include "printing/print_job_constants.h" | |
| 21 | |
| 22 namespace { | |
| 23 | |
| 24 // 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.
| |
| 25 typedef std::map<std::string, int> PrintPreviewRequestIdMap; | |
| 26 | |
| 27 struct PrintPreviewRequestIdMapWithLock { | |
| 28 PrintPreviewRequestIdMap map; | |
| 29 base::Lock lock; | |
| 30 }; | |
| 31 | |
| 32 // Written to on the UI thread, read on IO thread. | |
| 33 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.
| |
| 34 g_print_preview_request_id_map(base::LINKER_INITIALIZED); | |
| 35 | |
| 36 } // namespace | |
| 16 | 37 |
| 17 PrintPreviewUI::PrintPreviewUI(TabContents* contents) | 38 PrintPreviewUI::PrintPreviewUI(TabContents* contents) |
| 18 : ChromeWebUI(contents), | 39 : ChromeWebUI(contents), |
| 19 initial_preview_start_time_(base::TimeTicks::Now()), | 40 initial_preview_start_time_(base::TimeTicks::Now()) { |
| 20 request_count_(0U), | |
| 21 document_cookie_(0) { | |
| 22 // WebUI owns |handler_|. | 41 // WebUI owns |handler_|. |
| 23 handler_ = new PrintPreviewHandler(); | 42 handler_ = new PrintPreviewHandler(); |
| 24 AddMessageHandler(handler_->Attach(this)); | 43 AddMessageHandler(handler_->Attach(this)); |
| 25 | 44 |
| 26 // Set up the chrome://print/ data source. | 45 // Set up the chrome://print/ data source. |
| 27 Profile* profile = Profile::FromBrowserContext(contents->browser_context()); | 46 Profile* profile = Profile::FromBrowserContext(contents->browser_context()); |
| 28 profile->GetChromeURLDataManager()->AddDataSource( | 47 profile->GetChromeURLDataManager()->AddDataSource( |
| 29 new PrintPreviewDataSource()); | 48 new PrintPreviewDataSource()); |
| 30 | 49 |
| 31 // Store the PrintPreviewUIAddress as a string. | 50 preview_ui_addr_str_ = GetPrintPreviewUIAddress(); |
| 32 // "0x" + deadc0de + '\0' = 2 + 2 * sizeof(this) + 1; | 51 |
| 33 char preview_ui_addr[2 + (2 * sizeof(this)) + 1]; | 52 // 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.
| |
| 34 base::snprintf(preview_ui_addr, sizeof(preview_ui_addr), "%p", this); | 53 // requested page is not always INVALID_PAGE_INDEX. |
| 35 preview_ui_addr_str_ = preview_ui_addr; | 54 base::AutoLock lock(g_print_preview_request_id_map.Get().lock); |
| 55 g_print_preview_request_id_map.Get().map[preview_ui_addr_str_] = -1; | |
| 36 } | 56 } |
| 37 | 57 |
| 38 PrintPreviewUI::~PrintPreviewUI() { | 58 PrintPreviewUI::~PrintPreviewUI() { |
| 39 print_preview_data_service()->RemoveEntry(preview_ui_addr_str_); | 59 print_preview_data_service()->RemoveEntry(preview_ui_addr_str_); |
| 60 | |
| 61 base::AutoLock lock(g_print_preview_request_id_map.Get().lock); | |
| 62 PrintPreviewRequestIdMap* map = &g_print_preview_request_id_map.Get().map; | |
| 63 map->erase(preview_ui_addr_str_); | |
| 40 } | 64 } |
| 41 | 65 |
| 42 void PrintPreviewUI::GetPrintPreviewDataForIndex( | 66 void PrintPreviewUI::GetPrintPreviewDataForIndex( |
| 43 int index, | 67 int index, |
| 44 scoped_refptr<RefCountedBytes>* data) { | 68 scoped_refptr<RefCountedBytes>* data) { |
| 45 print_preview_data_service()->GetDataEntry(preview_ui_addr_str_, index, data); | 69 print_preview_data_service()->GetDataEntry(preview_ui_addr_str_, index, data); |
| 46 } | 70 } |
| 47 | 71 |
| 48 void PrintPreviewUI::SetPrintPreviewDataForIndex(int index, | 72 void PrintPreviewUI::SetPrintPreviewDataForIndex(int index, |
| 49 const RefCountedBytes* data) { | 73 const RefCountedBytes* data) { |
| 50 print_preview_data_service()->SetDataEntry(preview_ui_addr_str_, index, data); | 74 print_preview_data_service()->SetDataEntry(preview_ui_addr_str_, index, data); |
| 51 } | 75 } |
| 52 | 76 |
| 53 void PrintPreviewUI::ClearAllPreviewData() { | 77 void PrintPreviewUI::ClearAllPreviewData() { |
| 54 print_preview_data_service()->RemoveEntry(preview_ui_addr_str_); | 78 print_preview_data_service()->RemoveEntry(preview_ui_addr_str_); |
| 55 } | 79 } |
| 56 | 80 |
| 57 void PrintPreviewUI::SetInitiatorTabURLAndTitle( | 81 void PrintPreviewUI::SetInitiatorTabURLAndTitle( |
| 58 const std::string& initiator_url, | 82 const std::string& initiator_url, |
| 59 const string16& job_title) { | 83 const string16& job_title) { |
| 60 initiator_url_ = initiator_url; | 84 initiator_url_ = initiator_url; |
| 61 initiator_tab_title_ = job_title; | 85 initiator_tab_title_ = job_title; |
| 62 } | 86 } |
| 63 | 87 |
| 88 // static | |
| 89 bool PrintPreviewUI::GetCurrentPrintPreviewStatus( | |
| 90 const std::string& preview_ui_addr, | |
| 91 int request_id, | |
| 92 bool* cancel) { | |
| 93 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 94 | |
| 95 base::AutoLock lock(g_print_preview_request_id_map.Get().lock); | |
| 96 PrintPreviewRequestIdMap::const_iterator it = | |
| 97 g_print_preview_request_id_map.Get().map.find(preview_ui_addr); | |
| 98 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.
| |
| 99 return false; | |
| 100 *cancel = (request_id != it->second); | |
| 101 return true; | |
| 102 } | |
| 103 | |
| 104 std::string PrintPreviewUI::GetPrintPreviewUIAddress() const { | |
| 105 // Store the PrintPreviewUIAddress as a string. | |
| 106 // "0x" + deadc0de + '\0' = 2 + 2 * sizeof(this) + 1; | |
| 107 char preview_ui_addr[2 + (2 * sizeof(this)) + 1]; | |
| 108 base::snprintf(preview_ui_addr, sizeof(preview_ui_addr), "%p", this); | |
| 109 return preview_ui_addr; | |
| 110 } | |
| 111 | |
| 64 void PrintPreviewUI::OnInitiatorTabCrashed() { | 112 void PrintPreviewUI::OnInitiatorTabCrashed() { |
| 65 StringValue initiator_tab_url(initiator_url_); | 113 StringValue initiator_tab_url(initiator_url_); |
| 66 CallJavascriptFunction("onInitiatorTabCrashed", initiator_tab_url); | 114 CallJavascriptFunction("onInitiatorTabCrashed", initiator_tab_url); |
| 67 } | 115 } |
| 68 | 116 |
| 69 void PrintPreviewUI::OnInitiatorTabClosed() { | 117 void PrintPreviewUI::OnInitiatorTabClosed() { |
| 70 StringValue initiator_tab_url(initiator_url_); | 118 StringValue initiator_tab_url(initiator_url_); |
| 71 CallJavascriptFunction("onInitiatorTabClosed", initiator_tab_url); | 119 CallJavascriptFunction("onInitiatorTabClosed", initiator_tab_url); |
| 72 } | 120 } |
| 73 | 121 |
| 74 void PrintPreviewUI::OnPrintPreviewRequest() { | 122 void PrintPreviewUI::OnPrintPreviewRequest(int request_id) { |
| 75 request_count_++; | 123 base::AutoLock lock(g_print_preview_request_id_map.Get().lock); |
| 124 g_print_preview_request_id_map.Get().map[preview_ui_addr_str_] = request_id; | |
| 76 } | 125 } |
| 77 | 126 |
| 78 void PrintPreviewUI::OnDidGetPreviewPageCount( | 127 void PrintPreviewUI::OnDidGetPreviewPageCount( |
| 79 const PrintHostMsg_DidGetPreviewPageCount_Params& params) { | 128 const PrintHostMsg_DidGetPreviewPageCount_Params& params) { |
| 80 DCHECK_GT(params.page_count, 0); | 129 DCHECK_GT(params.page_count, 0); |
| 81 document_cookie_ = params.document_cookie; | |
| 82 base::FundamentalValue count(params.page_count); | 130 base::FundamentalValue count(params.page_count); |
| 83 base::FundamentalValue modifiable(params.is_modifiable); | 131 base::FundamentalValue modifiable(params.is_modifiable); |
| 84 base::FundamentalValue request_id(params.preview_request_id); | 132 base::FundamentalValue request_id(params.preview_request_id); |
| 85 StringValue title(initiator_tab_title_); | 133 StringValue title(initiator_tab_title_); |
| 86 CallJavascriptFunction("onDidGetPreviewPageCount", count, modifiable, | 134 CallJavascriptFunction("onDidGetPreviewPageCount", count, modifiable, |
| 87 request_id, title); | 135 request_id, title); |
| 88 } | 136 } |
| 89 | 137 |
| 90 void PrintPreviewUI::OnDidPreviewPage(int page_number, | 138 void PrintPreviewUI::OnDidPreviewPage(int page_number, |
| 91 int preview_request_id) { | 139 int preview_request_id) { |
| 92 DCHECK_GE(page_number, 0); | 140 DCHECK_GE(page_number, 0); |
| 93 base::FundamentalValue number(page_number); | 141 base::FundamentalValue number(page_number); |
| 94 StringValue ui_identifier(preview_ui_addr_str_); | 142 StringValue ui_identifier(preview_ui_addr_str_); |
| 95 base::FundamentalValue request_id(preview_request_id); | 143 base::FundamentalValue request_id(preview_request_id); |
| 96 CallJavascriptFunction("onDidPreviewPage", number, ui_identifier, request_id); | 144 CallJavascriptFunction("onDidPreviewPage", number, ui_identifier, request_id); |
| 97 } | 145 } |
| 98 | 146 |
| 99 void PrintPreviewUI::OnReusePreviewData(int preview_request_id) { | 147 void PrintPreviewUI::OnReusePreviewData(int preview_request_id) { |
| 100 DecrementRequestCount(); | |
| 101 | |
| 102 base::StringValue ui_identifier(preview_ui_addr_str_); | 148 base::StringValue ui_identifier(preview_ui_addr_str_); |
| 103 base::FundamentalValue ui_preview_request_id(preview_request_id); | 149 base::FundamentalValue ui_preview_request_id(preview_request_id); |
| 104 CallJavascriptFunction("reloadPreviewPages", ui_identifier, | 150 CallJavascriptFunction("reloadPreviewPages", ui_identifier, |
| 105 ui_preview_request_id); | 151 ui_preview_request_id); |
| 106 } | 152 } |
| 107 | 153 |
| 108 void PrintPreviewUI::OnPreviewDataIsAvailable(int expected_pages_count, | 154 void PrintPreviewUI::OnPreviewDataIsAvailable(int expected_pages_count, |
| 109 int preview_request_id) { | 155 int preview_request_id) { |
| 110 VLOG(1) << "Print preview request finished with " | 156 VLOG(1) << "Print preview request finished with " |
| 111 << expected_pages_count << " pages"; | 157 << expected_pages_count << " pages"; |
| 112 DecrementRequestCount(); | |
| 113 | 158 |
| 114 if (!initial_preview_start_time_.is_null()) { | 159 if (!initial_preview_start_time_.is_null()) { |
| 115 UMA_HISTOGRAM_TIMES("PrintPreview.InitalDisplayTime", | 160 UMA_HISTOGRAM_TIMES("PrintPreview.InitalDisplayTime", |
| 116 base::TimeTicks::Now() - initial_preview_start_time_); | 161 base::TimeTicks::Now() - initial_preview_start_time_); |
| 117 UMA_HISTOGRAM_COUNTS("PrintPreview.PageCount.Initial", | 162 UMA_HISTOGRAM_COUNTS("PrintPreview.PageCount.Initial", |
| 118 expected_pages_count); | 163 expected_pages_count); |
| 119 initial_preview_start_time_ = base::TimeTicks(); | 164 initial_preview_start_time_ = base::TimeTicks(); |
| 120 } | 165 } |
| 121 base::StringValue ui_identifier(preview_ui_addr_str_); | 166 base::StringValue ui_identifier(preview_ui_addr_str_); |
| 122 base::FundamentalValue ui_preview_request_id(preview_request_id); | 167 base::FundamentalValue ui_preview_request_id(preview_request_id); |
| 123 CallJavascriptFunction("updatePrintPreview", ui_identifier, | 168 CallJavascriptFunction("updatePrintPreview", ui_identifier, |
| 124 ui_preview_request_id); | 169 ui_preview_request_id); |
| 125 } | 170 } |
| 126 | 171 |
| 127 void PrintPreviewUI::OnTabDestroyed() { | 172 void PrintPreviewUI::OnTabDestroyed() { |
| 128 handler_->OnTabDestroyed(); | 173 handler_->OnTabDestroyed(); |
| 129 } | 174 } |
| 130 | 175 |
| 131 void PrintPreviewUI::OnFileSelectionCancelled() { | 176 void PrintPreviewUI::OnFileSelectionCancelled() { |
| 132 CallJavascriptFunction("fileSelectionCancelled"); | 177 CallJavascriptFunction("fileSelectionCancelled"); |
| 133 } | 178 } |
| 134 | 179 |
| 135 void PrintPreviewUI::OnPrintPreviewFailed() { | 180 void PrintPreviewUI::OnPrintPreviewFailed() { |
| 136 DecrementRequestCount(); | |
| 137 CallJavascriptFunction("printPreviewFailed"); | 181 CallJavascriptFunction("printPreviewFailed"); |
| 138 } | 182 } |
| 139 | 183 |
| 140 void PrintPreviewUI::OnPrintPreviewCancelled() { | |
| 141 DecrementRequestCount(); | |
| 142 } | |
| 143 | |
| 144 bool PrintPreviewUI::HasPendingRequests() { | |
| 145 return request_count_ > 1; | |
| 146 } | |
| 147 | |
| 148 PrintPreviewDataService* PrintPreviewUI::print_preview_data_service() { | 184 PrintPreviewDataService* PrintPreviewUI::print_preview_data_service() { |
| 149 return PrintPreviewDataService::GetInstance(); | 185 return PrintPreviewDataService::GetInstance(); |
| 150 } | 186 } |
| 151 | |
| 152 void PrintPreviewUI::DecrementRequestCount() { | |
| 153 if (request_count_ > 0) | |
| 154 request_count_--; | |
| 155 } | |
| 156 | |
| 157 int PrintPreviewUI::document_cookie() { | |
| 158 return document_cookie_; | |
| 159 } | |
| OLD | NEW |