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

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: address comments 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;
kmadhusu 2011/08/20 00:07:11 nit: ";" -> "."
Lei Zhang 2011/08/20 03:25:52 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 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 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
92
93 base::AutoLock lock(g_print_preview_request_id_map.Get().lock);
94 PrintPreviewRequestIdMap::const_iterator it =
95 g_print_preview_request_id_map.Get().map.find(preview_ui_addr);
96 if (it == g_print_preview_request_id_map.Get().map.end()) {
97 *cancel = true;
98 return;
99 }
100 *cancel = (request_id != it->second);
101 }
102
103 std::string PrintPreviewUI::GetPrintPreviewUIAddress() const {
104 // Store the PrintPreviewUIAddress as a string.
105 // "0x" + deadc0de + '\0' = 2 + 2 * sizeof(this) + 1;
106 char preview_ui_addr[2 + (2 * sizeof(this)) + 1];
107 base::snprintf(preview_ui_addr, sizeof(preview_ui_addr), "%p", this);
108 return preview_ui_addr;
109 }
110
64 void PrintPreviewUI::OnInitiatorTabCrashed() { 111 void PrintPreviewUI::OnInitiatorTabCrashed() {
65 StringValue initiator_tab_url(initiator_url_); 112 StringValue initiator_tab_url(initiator_url_);
66 CallJavascriptFunction("onInitiatorTabCrashed", initiator_tab_url); 113 CallJavascriptFunction("onInitiatorTabCrashed", initiator_tab_url);
67 } 114 }
68 115
69 void PrintPreviewUI::OnInitiatorTabClosed() { 116 void PrintPreviewUI::OnInitiatorTabClosed() {
70 StringValue initiator_tab_url(initiator_url_); 117 StringValue initiator_tab_url(initiator_url_);
71 CallJavascriptFunction("onInitiatorTabClosed", initiator_tab_url); 118 CallJavascriptFunction("onInitiatorTabClosed", initiator_tab_url);
72 } 119 }
73 120
74 void PrintPreviewUI::OnPrintPreviewRequest() { 121 void PrintPreviewUI::OnPrintPreviewRequest(int request_id) {
75 request_count_++; 122 base::AutoLock lock(g_print_preview_request_id_map.Get().lock);
123 g_print_preview_request_id_map.Get().map[preview_ui_addr_str_] = request_id;
76 } 124 }
77 125
78 void PrintPreviewUI::OnDidGetPreviewPageCount( 126 void PrintPreviewUI::OnDidGetPreviewPageCount(
79 const PrintHostMsg_DidGetPreviewPageCount_Params& params) { 127 const PrintHostMsg_DidGetPreviewPageCount_Params& params) {
80 DCHECK_GT(params.page_count, 0); 128 DCHECK_GT(params.page_count, 0);
81 document_cookie_ = params.document_cookie;
82 base::FundamentalValue count(params.page_count); 129 base::FundamentalValue count(params.page_count);
83 base::FundamentalValue modifiable(params.is_modifiable); 130 base::FundamentalValue modifiable(params.is_modifiable);
84 base::FundamentalValue request_id(params.preview_request_id); 131 base::FundamentalValue request_id(params.preview_request_id);
85 StringValue title(initiator_tab_title_); 132 StringValue title(initiator_tab_title_);
86 CallJavascriptFunction("onDidGetPreviewPageCount", count, modifiable, 133 CallJavascriptFunction("onDidGetPreviewPageCount", count, modifiable,
87 request_id, title); 134 request_id, title);
88 } 135 }
89 136
90 void PrintPreviewUI::OnDidPreviewPage(int page_number, 137 void PrintPreviewUI::OnDidPreviewPage(int page_number,
91 int preview_request_id) { 138 int preview_request_id) {
92 DCHECK_GE(page_number, 0); 139 DCHECK_GE(page_number, 0);
93 base::FundamentalValue number(page_number); 140 base::FundamentalValue number(page_number);
94 StringValue ui_identifier(preview_ui_addr_str_); 141 StringValue ui_identifier(preview_ui_addr_str_);
95 base::FundamentalValue request_id(preview_request_id); 142 base::FundamentalValue request_id(preview_request_id);
96 CallJavascriptFunction("onDidPreviewPage", number, ui_identifier, request_id); 143 CallJavascriptFunction("onDidPreviewPage", number, ui_identifier, request_id);
97 } 144 }
98 145
99 void PrintPreviewUI::OnReusePreviewData(int preview_request_id) { 146 void PrintPreviewUI::OnReusePreviewData(int preview_request_id) {
100 DecrementRequestCount();
101
102 base::StringValue ui_identifier(preview_ui_addr_str_); 147 base::StringValue ui_identifier(preview_ui_addr_str_);
103 base::FundamentalValue ui_preview_request_id(preview_request_id); 148 base::FundamentalValue ui_preview_request_id(preview_request_id);
104 CallJavascriptFunction("reloadPreviewPages", ui_identifier, 149 CallJavascriptFunction("reloadPreviewPages", ui_identifier,
105 ui_preview_request_id); 150 ui_preview_request_id);
106 } 151 }
107 152
108 void PrintPreviewUI::OnPreviewDataIsAvailable(int expected_pages_count, 153 void PrintPreviewUI::OnPreviewDataIsAvailable(int expected_pages_count,
109 int preview_request_id) { 154 int preview_request_id) {
110 VLOG(1) << "Print preview request finished with " 155 VLOG(1) << "Print preview request finished with "
111 << expected_pages_count << " pages"; 156 << expected_pages_count << " pages";
112 DecrementRequestCount();
113 157
114 if (!initial_preview_start_time_.is_null()) { 158 if (!initial_preview_start_time_.is_null()) {
115 UMA_HISTOGRAM_TIMES("PrintPreview.InitalDisplayTime", 159 UMA_HISTOGRAM_TIMES("PrintPreview.InitalDisplayTime",
116 base::TimeTicks::Now() - initial_preview_start_time_); 160 base::TimeTicks::Now() - initial_preview_start_time_);
117 UMA_HISTOGRAM_COUNTS("PrintPreview.PageCount.Initial", 161 UMA_HISTOGRAM_COUNTS("PrintPreview.PageCount.Initial",
118 expected_pages_count); 162 expected_pages_count);
119 initial_preview_start_time_ = base::TimeTicks(); 163 initial_preview_start_time_ = base::TimeTicks();
120 } 164 }
121 base::StringValue ui_identifier(preview_ui_addr_str_); 165 base::StringValue ui_identifier(preview_ui_addr_str_);
122 base::FundamentalValue ui_preview_request_id(preview_request_id); 166 base::FundamentalValue ui_preview_request_id(preview_request_id);
123 CallJavascriptFunction("updatePrintPreview", ui_identifier, 167 CallJavascriptFunction("updatePrintPreview", ui_identifier,
124 ui_preview_request_id); 168 ui_preview_request_id);
125 } 169 }
126 170
127 void PrintPreviewUI::OnTabDestroyed() { 171 void PrintPreviewUI::OnTabDestroyed() {
128 handler_->OnTabDestroyed(); 172 handler_->OnTabDestroyed();
129 } 173 }
130 174
131 void PrintPreviewUI::OnFileSelectionCancelled() { 175 void PrintPreviewUI::OnFileSelectionCancelled() {
132 CallJavascriptFunction("fileSelectionCancelled"); 176 CallJavascriptFunction("fileSelectionCancelled");
133 } 177 }
134 178
135 void PrintPreviewUI::OnPrintPreviewFailed() { 179 void PrintPreviewUI::OnPrintPreviewFailed() {
136 DecrementRequestCount();
137 CallJavascriptFunction("printPreviewFailed"); 180 CallJavascriptFunction("printPreviewFailed");
138 } 181 }
139 182
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() { 183 PrintPreviewDataService* PrintPreviewUI::print_preview_data_service() {
149 return PrintPreviewDataService::GetInstance(); 184 return PrintPreviewDataService::GetInstance();
150 } 185 }
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