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 // Thread-safe wrapper around a std::map to keep track of mappings from |
| 25 // PrintPreviewUI addresses to most recent print preview request ids. |
| 26 class PrintPreviewRequestIdMapWithLock { |
| 27 public: |
| 28 PrintPreviewRequestIdMapWithLock() {} |
| 29 ~PrintPreviewRequestIdMapWithLock() {} |
| 30 |
| 31 // Get the value for |addr|. Returns true and sets |out_value| on success. |
| 32 bool Get(const std::string& addr, int* out_value) { |
| 33 base::AutoLock lock(lock_); |
| 34 PrintPreviewRequestIdMap::const_iterator it = map_.find(addr); |
| 35 if (it == map_.end()) |
| 36 return false; |
| 37 *out_value = it->second; |
| 38 return true; |
| 39 } |
| 40 |
| 41 // Sets the |value| for |addr|. |
| 42 void Set(const std::string& addr, int value) { |
| 43 base::AutoLock lock(lock_); |
| 44 map_[addr] = value; |
| 45 } |
| 46 |
| 47 // Erase the entry for |addr|. |
| 48 void Erase(const std::string& addr) { |
| 49 base::AutoLock lock(lock_); |
| 50 map_.erase(addr); |
| 51 } |
| 52 |
| 53 private: |
| 54 typedef std::map<std::string, int> PrintPreviewRequestIdMap; |
| 55 |
| 56 PrintPreviewRequestIdMap map_; |
| 57 base::Lock lock_; |
| 58 }; |
| 59 |
| 60 // Written to on the UI thread, read from any thread. |
| 61 base::LazyInstance<PrintPreviewRequestIdMapWithLock> |
| 62 g_print_preview_request_id_map(base::LINKER_INITIALIZED); |
| 63 |
| 64 } // namespace |
16 | 65 |
17 PrintPreviewUI::PrintPreviewUI(TabContents* contents) | 66 PrintPreviewUI::PrintPreviewUI(TabContents* contents) |
18 : ChromeWebUI(contents), | 67 : ChromeWebUI(contents), |
19 initial_preview_start_time_(base::TimeTicks::Now()), | 68 initial_preview_start_time_(base::TimeTicks::Now()) { |
20 request_count_(0U), | |
21 document_cookie_(0) { | |
22 // WebUI owns |handler_|. | 69 // WebUI owns |handler_|. |
23 handler_ = new PrintPreviewHandler(); | 70 handler_ = new PrintPreviewHandler(); |
24 AddMessageHandler(handler_->Attach(this)); | 71 AddMessageHandler(handler_->Attach(this)); |
25 | 72 |
26 // Set up the chrome://print/ data source. | 73 // Set up the chrome://print/ data source. |
27 Profile* profile = Profile::FromBrowserContext(contents->browser_context()); | 74 Profile* profile = Profile::FromBrowserContext(contents->browser_context()); |
28 profile->GetChromeURLDataManager()->AddDataSource( | 75 profile->GetChromeURLDataManager()->AddDataSource( |
29 new PrintPreviewDataSource()); | 76 new PrintPreviewDataSource()); |
30 | 77 |
31 // Store the PrintPreviewUIAddress as a string. | 78 preview_ui_addr_str_ = GetPrintPreviewUIAddress(); |
32 // "0x" + deadc0de + '\0' = 2 + 2 * sizeof(this) + 1; | 79 |
33 char preview_ui_addr[2 + (2 * sizeof(this)) + 1]; | 80 g_print_preview_request_id_map.Get().Set(preview_ui_addr_str_, -1); |
34 base::snprintf(preview_ui_addr, sizeof(preview_ui_addr), "%p", this); | |
35 preview_ui_addr_str_ = preview_ui_addr; | |
36 } | 81 } |
37 | 82 |
38 PrintPreviewUI::~PrintPreviewUI() { | 83 PrintPreviewUI::~PrintPreviewUI() { |
39 print_preview_data_service()->RemoveEntry(preview_ui_addr_str_); | 84 print_preview_data_service()->RemoveEntry(preview_ui_addr_str_); |
| 85 |
| 86 g_print_preview_request_id_map.Get().Erase(preview_ui_addr_str_); |
40 } | 87 } |
41 | 88 |
42 void PrintPreviewUI::GetPrintPreviewDataForIndex( | 89 void PrintPreviewUI::GetPrintPreviewDataForIndex( |
43 int index, | 90 int index, |
44 scoped_refptr<RefCountedBytes>* data) { | 91 scoped_refptr<RefCountedBytes>* data) { |
45 print_preview_data_service()->GetDataEntry(preview_ui_addr_str_, index, data); | 92 print_preview_data_service()->GetDataEntry(preview_ui_addr_str_, index, data); |
46 } | 93 } |
47 | 94 |
48 void PrintPreviewUI::SetPrintPreviewDataForIndex(int index, | 95 void PrintPreviewUI::SetPrintPreviewDataForIndex(int index, |
49 const RefCountedBytes* data) { | 96 const RefCountedBytes* data) { |
50 print_preview_data_service()->SetDataEntry(preview_ui_addr_str_, index, data); | 97 print_preview_data_service()->SetDataEntry(preview_ui_addr_str_, index, data); |
51 } | 98 } |
52 | 99 |
53 void PrintPreviewUI::ClearAllPreviewData() { | 100 void PrintPreviewUI::ClearAllPreviewData() { |
54 print_preview_data_service()->RemoveEntry(preview_ui_addr_str_); | 101 print_preview_data_service()->RemoveEntry(preview_ui_addr_str_); |
55 } | 102 } |
56 | 103 |
57 void PrintPreviewUI::SetInitiatorTabURLAndTitle( | 104 void PrintPreviewUI::SetInitiatorTabURLAndTitle( |
58 const std::string& initiator_url, | 105 const std::string& initiator_url, |
59 const string16& job_title) { | 106 const string16& job_title) { |
60 initiator_url_ = initiator_url; | 107 initiator_url_ = initiator_url; |
61 initiator_tab_title_ = job_title; | 108 initiator_tab_title_ = job_title; |
62 } | 109 } |
63 | 110 |
| 111 // static |
| 112 void PrintPreviewUI::GetCurrentPrintPreviewStatus( |
| 113 const std::string& preview_ui_addr, |
| 114 int request_id, |
| 115 bool* cancel) { |
| 116 int current_id = -1; |
| 117 if (!g_print_preview_request_id_map.Get().Get(preview_ui_addr, ¤t_id)) { |
| 118 *cancel = true; |
| 119 return; |
| 120 } |
| 121 *cancel = (request_id != current_id); |
| 122 } |
| 123 |
| 124 std::string PrintPreviewUI::GetPrintPreviewUIAddress() const { |
| 125 // Store the PrintPreviewUIAddress as a string. |
| 126 // "0x" + deadc0de + '\0' = 2 + 2 * sizeof(this) + 1; |
| 127 char preview_ui_addr[2 + (2 * sizeof(this)) + 1]; |
| 128 base::snprintf(preview_ui_addr, sizeof(preview_ui_addr), "%p", this); |
| 129 return preview_ui_addr; |
| 130 } |
| 131 |
64 void PrintPreviewUI::OnInitiatorTabCrashed() { | 132 void PrintPreviewUI::OnInitiatorTabCrashed() { |
65 StringValue initiator_tab_url(initiator_url_); | 133 StringValue initiator_tab_url(initiator_url_); |
66 CallJavascriptFunction("onInitiatorTabCrashed", initiator_tab_url); | 134 CallJavascriptFunction("onInitiatorTabCrashed", initiator_tab_url); |
67 } | 135 } |
68 | 136 |
69 void PrintPreviewUI::OnInitiatorTabClosed() { | 137 void PrintPreviewUI::OnInitiatorTabClosed() { |
70 StringValue initiator_tab_url(initiator_url_); | 138 StringValue initiator_tab_url(initiator_url_); |
71 CallJavascriptFunction("onInitiatorTabClosed", initiator_tab_url); | 139 CallJavascriptFunction("onInitiatorTabClosed", initiator_tab_url); |
72 } | 140 } |
73 | 141 |
74 void PrintPreviewUI::OnPrintPreviewRequest() { | 142 void PrintPreviewUI::OnPrintPreviewRequest(int request_id) { |
75 request_count_++; | 143 g_print_preview_request_id_map.Get().Set(preview_ui_addr_str_, request_id); |
76 } | 144 } |
77 | 145 |
78 void PrintPreviewUI::OnDidGetPreviewPageCount( | 146 void PrintPreviewUI::OnDidGetPreviewPageCount( |
79 const PrintHostMsg_DidGetPreviewPageCount_Params& params) { | 147 const PrintHostMsg_DidGetPreviewPageCount_Params& params) { |
80 DCHECK_GT(params.page_count, 0); | 148 DCHECK_GT(params.page_count, 0); |
81 document_cookie_ = params.document_cookie; | |
82 base::FundamentalValue count(params.page_count); | 149 base::FundamentalValue count(params.page_count); |
83 base::FundamentalValue modifiable(params.is_modifiable); | 150 base::FundamentalValue modifiable(params.is_modifiable); |
84 base::FundamentalValue request_id(params.preview_request_id); | 151 base::FundamentalValue request_id(params.preview_request_id); |
85 StringValue title(initiator_tab_title_); | 152 StringValue title(initiator_tab_title_); |
86 CallJavascriptFunction("onDidGetPreviewPageCount", count, modifiable, | 153 CallJavascriptFunction("onDidGetPreviewPageCount", count, modifiable, |
87 request_id, title); | 154 request_id, title); |
88 } | 155 } |
89 | 156 |
90 void PrintPreviewUI::OnDidPreviewPage(int page_number, | 157 void PrintPreviewUI::OnDidPreviewPage(int page_number, |
91 int preview_request_id) { | 158 int preview_request_id) { |
92 DCHECK_GE(page_number, 0); | 159 DCHECK_GE(page_number, 0); |
93 base::FundamentalValue number(page_number); | 160 base::FundamentalValue number(page_number); |
94 StringValue ui_identifier(preview_ui_addr_str_); | 161 StringValue ui_identifier(preview_ui_addr_str_); |
95 base::FundamentalValue request_id(preview_request_id); | 162 base::FundamentalValue request_id(preview_request_id); |
96 CallJavascriptFunction("onDidPreviewPage", number, ui_identifier, request_id); | 163 CallJavascriptFunction("onDidPreviewPage", number, ui_identifier, request_id); |
97 } | 164 } |
98 | 165 |
99 void PrintPreviewUI::OnReusePreviewData(int preview_request_id) { | 166 void PrintPreviewUI::OnReusePreviewData(int preview_request_id) { |
100 DecrementRequestCount(); | |
101 | |
102 base::StringValue ui_identifier(preview_ui_addr_str_); | 167 base::StringValue ui_identifier(preview_ui_addr_str_); |
103 base::FundamentalValue ui_preview_request_id(preview_request_id); | 168 base::FundamentalValue ui_preview_request_id(preview_request_id); |
104 CallJavascriptFunction("reloadPreviewPages", ui_identifier, | 169 CallJavascriptFunction("reloadPreviewPages", ui_identifier, |
105 ui_preview_request_id); | 170 ui_preview_request_id); |
106 } | 171 } |
107 | 172 |
108 void PrintPreviewUI::OnPreviewDataIsAvailable(int expected_pages_count, | 173 void PrintPreviewUI::OnPreviewDataIsAvailable(int expected_pages_count, |
109 int preview_request_id) { | 174 int preview_request_id) { |
110 VLOG(1) << "Print preview request finished with " | 175 VLOG(1) << "Print preview request finished with " |
111 << expected_pages_count << " pages"; | 176 << expected_pages_count << " pages"; |
112 DecrementRequestCount(); | |
113 | 177 |
114 if (!initial_preview_start_time_.is_null()) { | 178 if (!initial_preview_start_time_.is_null()) { |
115 UMA_HISTOGRAM_TIMES("PrintPreview.InitalDisplayTime", | 179 UMA_HISTOGRAM_TIMES("PrintPreview.InitalDisplayTime", |
116 base::TimeTicks::Now() - initial_preview_start_time_); | 180 base::TimeTicks::Now() - initial_preview_start_time_); |
117 UMA_HISTOGRAM_COUNTS("PrintPreview.PageCount.Initial", | 181 UMA_HISTOGRAM_COUNTS("PrintPreview.PageCount.Initial", |
118 expected_pages_count); | 182 expected_pages_count); |
119 initial_preview_start_time_ = base::TimeTicks(); | 183 initial_preview_start_time_ = base::TimeTicks(); |
120 } | 184 } |
121 base::StringValue ui_identifier(preview_ui_addr_str_); | 185 base::StringValue ui_identifier(preview_ui_addr_str_); |
122 base::FundamentalValue ui_preview_request_id(preview_request_id); | 186 base::FundamentalValue ui_preview_request_id(preview_request_id); |
123 CallJavascriptFunction("updatePrintPreview", ui_identifier, | 187 CallJavascriptFunction("updatePrintPreview", ui_identifier, |
124 ui_preview_request_id); | 188 ui_preview_request_id); |
125 } | 189 } |
126 | 190 |
127 void PrintPreviewUI::OnTabDestroyed() { | 191 void PrintPreviewUI::OnTabDestroyed() { |
128 handler_->OnTabDestroyed(); | 192 handler_->OnTabDestroyed(); |
129 } | 193 } |
130 | 194 |
131 void PrintPreviewUI::OnFileSelectionCancelled() { | 195 void PrintPreviewUI::OnFileSelectionCancelled() { |
132 CallJavascriptFunction("fileSelectionCancelled"); | 196 CallJavascriptFunction("fileSelectionCancelled"); |
133 } | 197 } |
134 | 198 |
135 void PrintPreviewUI::OnPrintPreviewFailed() { | 199 void PrintPreviewUI::OnPrintPreviewFailed() { |
136 DecrementRequestCount(); | |
137 CallJavascriptFunction("printPreviewFailed"); | 200 CallJavascriptFunction("printPreviewFailed"); |
138 } | 201 } |
139 | 202 |
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() { | 203 PrintPreviewDataService* PrintPreviewUI::print_preview_data_service() { |
149 return PrintPreviewDataService::GetInstance(); | 204 return PrintPreviewDataService::GetInstance(); |
150 } | 205 } |
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 |