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