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