Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(397)

Side by Side Diff: chrome/browser/ui/webui/print_preview_ui.cc

Issue 7621087: Print Preview: Go from event driven print preview back to print preview with sync messages. The s... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: fix comment Created 9 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 most recent print preview request id.
25 typedef std::map<std::string, int> PrintPreviewRequestIdMap;
26
27 struct PrintPreviewRequestIdMapWithLock {
vandebo (ex-Chrome) 2011/08/23 04:30:03 nit: Is there a reason this can't be a class with
Lei Zhang 2011/08/23 04:58:17 Done.
28 PrintPreviewRequestIdMap map;
29 base::Lock lock;
30 };
31
32 // Written to on the UI thread, read from any thread.
33 base::LazyInstance<PrintPreviewRequestIdMapWithLock>
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 base::AutoLock lock(g_print_preview_request_id_map.Get().lock);
34 base::snprintf(preview_ui_addr, sizeof(preview_ui_addr), "%p", this); 53 g_print_preview_request_id_map.Get().map[preview_ui_addr_str_] = -1;
35 preview_ui_addr_str_ = preview_ui_addr;
36 } 54 }
37 55
38 PrintPreviewUI::~PrintPreviewUI() { 56 PrintPreviewUI::~PrintPreviewUI() {
39 print_preview_data_service()->RemoveEntry(preview_ui_addr_str_); 57 print_preview_data_service()->RemoveEntry(preview_ui_addr_str_);
58
59 base::AutoLock lock(g_print_preview_request_id_map.Get().lock);
60 PrintPreviewRequestIdMap* map = &g_print_preview_request_id_map.Get().map;
61 map->erase(preview_ui_addr_str_);
40 } 62 }
41 63
42 void PrintPreviewUI::GetPrintPreviewDataForIndex( 64 void PrintPreviewUI::GetPrintPreviewDataForIndex(
43 int index, 65 int index,
44 scoped_refptr<RefCountedBytes>* data) { 66 scoped_refptr<RefCountedBytes>* data) {
45 print_preview_data_service()->GetDataEntry(preview_ui_addr_str_, index, data); 67 print_preview_data_service()->GetDataEntry(preview_ui_addr_str_, index, data);
46 } 68 }
47 69
48 void PrintPreviewUI::SetPrintPreviewDataForIndex(int index, 70 void PrintPreviewUI::SetPrintPreviewDataForIndex(int index,
49 const RefCountedBytes* data) { 71 const RefCountedBytes* data) {
50 print_preview_data_service()->SetDataEntry(preview_ui_addr_str_, index, data); 72 print_preview_data_service()->SetDataEntry(preview_ui_addr_str_, index, data);
51 } 73 }
52 74
53 void PrintPreviewUI::ClearAllPreviewData() { 75 void PrintPreviewUI::ClearAllPreviewData() {
54 print_preview_data_service()->RemoveEntry(preview_ui_addr_str_); 76 print_preview_data_service()->RemoveEntry(preview_ui_addr_str_);
55 } 77 }
56 78
57 void PrintPreviewUI::SetInitiatorTabURLAndTitle( 79 void PrintPreviewUI::SetInitiatorTabURLAndTitle(
58 const std::string& initiator_url, 80 const std::string& initiator_url,
59 const string16& job_title) { 81 const string16& job_title) {
60 initiator_url_ = initiator_url; 82 initiator_url_ = initiator_url;
61 initiator_tab_title_ = job_title; 83 initiator_tab_title_ = job_title;
62 } 84 }
63 85
86 // static
87 void PrintPreviewUI::GetCurrentPrintPreviewStatus(
88 const std::string& preview_ui_addr,
89 int request_id,
90 bool* cancel) {
91 base::AutoLock lock(g_print_preview_request_id_map.Get().lock);
92 PrintPreviewRequestIdMap::const_iterator it =
93 g_print_preview_request_id_map.Get().map.find(preview_ui_addr);
94 if (it == g_print_preview_request_id_map.Get().map.end()) {
95 *cancel = true;
96 return;
97 }
98 *cancel = (request_id != it->second);
99 }
100
101 std::string PrintPreviewUI::GetPrintPreviewUIAddress() const {
102 // Store the PrintPreviewUIAddress as a string.
103 // "0x" + deadc0de + '\0' = 2 + 2 * sizeof(this) + 1;
104 char preview_ui_addr[2 + (2 * sizeof(this)) + 1];
105 base::snprintf(preview_ui_addr, sizeof(preview_ui_addr), "%p", this);
106 return preview_ui_addr;
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[preview_ui_addr_str_] = 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 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698