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

Side by Side Diff: chrome/browser/printing/print_view_manager.cc

Issue 2272933002: Cleanup nits in PrintViewManager. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 3 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
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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/printing/print_view_manager.h" 5 #include "chrome/browser/printing/print_view_manager.h"
6 6
7 #include <map> 7 #include <map>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/lazy_instance.h" 10 #include "base/lazy_instance.h"
11 #include "base/metrics/histogram.h"
12 #include "chrome/browser/browser_process.h"
13 #include "chrome/browser/plugins/chrome_plugin_service_filter.h" 11 #include "chrome/browser/plugins/chrome_plugin_service_filter.h"
14 #include "chrome/browser/printing/print_job_manager.h"
15 #include "chrome/browser/printing/print_preview_dialog_controller.h" 12 #include "chrome/browser/printing/print_preview_dialog_controller.h"
16 #include "chrome/browser/ui/webui/print_preview/print_preview_ui.h" 13 #include "chrome/browser/ui/webui/print_preview/print_preview_ui.h"
17 #include "chrome/common/chrome_content_client.h" 14 #include "chrome/common/chrome_content_client.h"
18 #include "components/printing/common/print_messages.h" 15 #include "components/printing/common/print_messages.h"
19 #include "content/public/browser/browser_thread.h" 16 #include "content/public/browser/browser_thread.h"
20 #include "content/public/browser/plugin_service.h" 17 #include "content/public/browser/plugin_service.h"
21 #include "content/public/browser/render_frame_host.h" 18 #include "content/public/browser/render_frame_host.h"
22 #include "content/public/browser/render_process_host.h" 19 #include "content/public/browser/render_process_host.h"
23 #include "content/public/browser/web_contents.h" 20 #include "content/public/browser/web_contents.h"
24 #include "content/public/common/webplugininfo.h" 21 #include "content/public/common/webplugininfo.h"
25 22
26 using content::BrowserThread; 23 using content::BrowserThread;
27 24
28 DEFINE_WEB_CONTENTS_USER_DATA_KEY(printing::PrintViewManager); 25 DEFINE_WEB_CONTENTS_USER_DATA_KEY(printing::PrintViewManager);
29 26
30 namespace { 27 namespace {
31 28
32 // Keeps track of pending scripted print preview closures. 29 // Keeps track of pending scripted print preview closures.
33 // No locking, only access on the UI thread. 30 // No locking, only access on the UI thread.
34 typedef std::map<content::RenderProcessHost*, base::Closure> 31 base::LazyInstance<std::map<content::RenderProcessHost*, base::Closure>>
35 ScriptedPrintPreviewClosureMap;
36 static base::LazyInstance<ScriptedPrintPreviewClosureMap>
37 g_scripted_print_preview_closure_map = LAZY_INSTANCE_INITIALIZER; 32 g_scripted_print_preview_closure_map = LAZY_INSTANCE_INITIALIZER;
38 33
39 void EnableInternalPDFPluginForContents(int render_process_id, 34 void EnableInternalPDFPluginForContents(int render_process_id,
40 int render_frame_id) { 35 int render_frame_id) {
41 // Always enable the internal PDF plugin for the print preview page. 36 // Always enable the internal PDF plugin for the print preview page.
42 base::FilePath pdf_plugin_path = base::FilePath::FromUTF8Unsafe( 37 base::FilePath pdf_plugin_path = base::FilePath::FromUTF8Unsafe(
43 ChromeContentClient::kPDFPluginPath); 38 ChromeContentClient::kPDFPluginPath);
44 39
45 content::WebPluginInfo pdf_plugin; 40 content::WebPluginInfo pdf_plugin;
46 if (!content::PluginService::GetInstance()->GetPluginInfoByPath( 41 if (!content::PluginService::GetInstance()->GetPluginInfoByPath(
47 pdf_plugin_path, &pdf_plugin)) { 42 pdf_plugin_path, &pdf_plugin)) {
48 return; 43 return;
49 } 44 }
50 45
51 ChromePluginServiceFilter::GetInstance()->OverridePluginForFrame( 46 ChromePluginServiceFilter::GetInstance()->OverridePluginForFrame(
52 render_process_id, render_frame_id, GURL(), pdf_plugin); 47 render_process_id, render_frame_id, GURL(), pdf_plugin);
53 } 48 }
54 49
55 } // namespace 50 } // namespace
56 51
57 namespace printing { 52 namespace printing {
58 53
59 PrintViewManager::PrintViewManager(content::WebContents* web_contents) 54 PrintViewManager::PrintViewManager(content::WebContents* web_contents)
60 : PrintViewManagerBase(web_contents), 55 : PrintViewManagerBase(web_contents),
61 print_preview_state_(NOT_PREVIEWING), 56 print_preview_state_(NOT_PREVIEWING),
62 scripted_print_preview_rph_(NULL) { 57 scripted_print_preview_rph_(nullptr) {
63 if (PrintPreviewDialogController::IsPrintPreviewDialog(web_contents)) { 58 if (PrintPreviewDialogController::IsPrintPreviewDialog(web_contents)) {
64 EnableInternalPDFPluginForContents( 59 EnableInternalPDFPluginForContents(
65 web_contents->GetRenderProcessHost()->GetID(), 60 web_contents->GetRenderProcessHost()->GetID(),
66 web_contents->GetMainFrame()->GetRoutingID()); 61 web_contents->GetMainFrame()->GetRoutingID());
67 } 62 }
68 } 63 }
69 64
70 PrintViewManager::~PrintViewManager() { 65 PrintViewManager::~PrintViewManager() {
71 DCHECK_EQ(NOT_PREVIEWING, print_preview_state_); 66 DCHECK_EQ(NOT_PREVIEWING, print_preview_state_);
72 } 67 }
73 68
74 #if defined(ENABLE_BASIC_PRINTING) 69 #if defined(ENABLE_BASIC_PRINTING)
75 bool PrintViewManager::PrintForSystemDialogNow( 70 bool PrintViewManager::PrintForSystemDialogNow(
76 const base::Closure& dialog_shown_callback) { 71 const base::Closure& dialog_shown_callback) {
77 DCHECK(!dialog_shown_callback.is_null()); 72 DCHECK(!dialog_shown_callback.is_null());
78 DCHECK(on_print_dialog_shown_callback_.is_null()); 73 DCHECK(on_print_dialog_shown_callback_.is_null());
79 on_print_dialog_shown_callback_ = dialog_shown_callback; 74 on_print_dialog_shown_callback_ = dialog_shown_callback;
80 return PrintNowInternal(new PrintMsg_PrintForSystemDialog(routing_id())); 75 return PrintNowInternal(new PrintMsg_PrintForSystemDialog(routing_id()));
81 } 76 }
82 77
83 bool PrintViewManager::BasicPrint() { 78 bool PrintViewManager::BasicPrint() {
84 PrintPreviewDialogController* dialog_controller = 79 PrintPreviewDialogController* dialog_controller =
85 PrintPreviewDialogController::GetInstance(); 80 PrintPreviewDialogController::GetInstance();
86 if (!dialog_controller) 81 if (!dialog_controller)
87 return false; 82 return false;
83
88 content::WebContents* print_preview_dialog = 84 content::WebContents* print_preview_dialog =
89 dialog_controller->GetPrintPreviewForContents(web_contents()); 85 dialog_controller->GetPrintPreviewForContents(web_contents());
90 if (print_preview_dialog) { 86 if (!print_preview_dialog)
91 if (!print_preview_dialog->GetWebUI())
92 return false;
93 PrintPreviewUI* print_preview_ui = static_cast<PrintPreviewUI*>(
94 print_preview_dialog->GetWebUI()->GetController());
95 print_preview_ui->OnShowSystemDialog();
96 return true;
97 } else {
98 return PrintNow(); 87 return PrintNow();
99 } 88
89 if (!print_preview_dialog->GetWebUI())
90 return false;
91
92 PrintPreviewUI* print_preview_ui = static_cast<PrintPreviewUI*>(
93 print_preview_dialog->GetWebUI()->GetController());
94 print_preview_ui->OnShowSystemDialog();
95 return true;
100 } 96 }
101 #endif // ENABLE_BASIC_PRINTING 97 #endif // defined(ENABLE_BASIC_PRINTING)
102 98
103 bool PrintViewManager::PrintPreviewNow(bool selection_only) { 99 bool PrintViewManager::PrintPreviewNow(bool selection_only) {
104 // Users can send print commands all they want and it is beyond 100 // Users can send print commands all they want and it is beyond
105 // PrintViewManager's control. Just ignore the extra commands. 101 // PrintViewManager's control. Just ignore the extra commands.
106 // See http://crbug.com/136842 for example. 102 // See http://crbug.com/136842 for example.
107 if (print_preview_state_ != NOT_PREVIEWING) 103 if (print_preview_state_ != NOT_PREVIEWING)
108 return false; 104 return false;
109 105
110 if (!PrintNowInternal(new PrintMsg_InitiatePrintPreview(routing_id(), 106 if (!PrintNowInternal(new PrintMsg_InitiatePrintPreview(routing_id(),
111 selection_only))) { 107 selection_only))) {
112 return false; 108 return false;
113 } 109 }
114 110
115 print_preview_state_ = USER_INITIATED_PREVIEW; 111 print_preview_state_ = USER_INITIATED_PREVIEW;
116 return true; 112 return true;
117 } 113 }
118 114
119 void PrintViewManager::PrintPreviewForWebNode() { 115 void PrintViewManager::PrintPreviewForWebNode() {
120 if (print_preview_state_ != NOT_PREVIEWING) 116 if (print_preview_state_ != NOT_PREVIEWING)
121 return; 117 return;
122 print_preview_state_ = USER_INITIATED_PREVIEW; 118 print_preview_state_ = USER_INITIATED_PREVIEW;
123 } 119 }
124 120
125 void PrintViewManager::PrintPreviewDone() { 121 void PrintViewManager::PrintPreviewDone() {
126 DCHECK_CURRENTLY_ON(BrowserThread::UI); 122 DCHECK_CURRENTLY_ON(BrowserThread::UI);
127 DCHECK_NE(NOT_PREVIEWING, print_preview_state_); 123 DCHECK_NE(NOT_PREVIEWING, print_preview_state_);
128 124
129 if (print_preview_state_ == SCRIPTED_PREVIEW) { 125 if (print_preview_state_ == SCRIPTED_PREVIEW) {
130 ScriptedPrintPreviewClosureMap& map = 126 auto& map = g_scripted_print_preview_closure_map.Get();
131 g_scripted_print_preview_closure_map.Get(); 127 auto it = map.find(scripted_print_preview_rph_);
132 ScriptedPrintPreviewClosureMap::iterator it =
133 map.find(scripted_print_preview_rph_);
134 CHECK(it != map.end()); 128 CHECK(it != map.end());
135 it->second.Run(); 129 it->second.Run();
136 map.erase(scripted_print_preview_rph_); 130 map.erase(it);
137 scripted_print_preview_rph_ = NULL; 131 scripted_print_preview_rph_ = nullptr;
138 } 132 }
139 print_preview_state_ = NOT_PREVIEWING; 133 print_preview_state_ = NOT_PREVIEWING;
140 } 134 }
141 135
142 void PrintViewManager::RenderFrameCreated( 136 void PrintViewManager::RenderFrameCreated(
143 content::RenderFrameHost* render_frame_host) { 137 content::RenderFrameHost* render_frame_host) {
144 if (PrintPreviewDialogController::IsPrintPreviewDialog(web_contents())) { 138 if (PrintPreviewDialogController::IsPrintPreviewDialog(web_contents())) {
145 EnableInternalPDFPluginForContents(render_frame_host->GetProcess()->GetID(), 139 EnableInternalPDFPluginForContents(render_frame_host->GetProcess()->GetID(),
146 render_frame_host->GetRoutingID()); 140 render_frame_host->GetRoutingID());
147 } 141 }
148 } 142 }
149 143
150 void PrintViewManager::RenderProcessGone(base::TerminationStatus status) { 144 void PrintViewManager::RenderProcessGone(base::TerminationStatus status) {
151 print_preview_state_ = NOT_PREVIEWING; 145 print_preview_state_ = NOT_PREVIEWING;
152 PrintViewManagerBase::RenderProcessGone(status); 146 PrintViewManagerBase::RenderProcessGone(status);
153 } 147 }
154 148
155 void PrintViewManager::OnDidShowPrintDialog() { 149 void PrintViewManager::OnDidShowPrintDialog() {
156 if (!on_print_dialog_shown_callback_.is_null()) 150 if (!on_print_dialog_shown_callback_.is_null())
157 on_print_dialog_shown_callback_.Run(); 151 on_print_dialog_shown_callback_.Run();
158 on_print_dialog_shown_callback_.Reset(); 152 on_print_dialog_shown_callback_.Reset();
159 } 153 }
160 154
161 void PrintViewManager::OnSetupScriptedPrintPreview(IPC::Message* reply_msg) { 155 void PrintViewManager::OnSetupScriptedPrintPreview(IPC::Message* reply_msg) {
162 DCHECK_CURRENTLY_ON(BrowserThread::UI); 156 DCHECK_CURRENTLY_ON(BrowserThread::UI);
163 ScriptedPrintPreviewClosureMap& map = 157 auto& map = g_scripted_print_preview_closure_map.Get();
164 g_scripted_print_preview_closure_map.Get();
165 content::RenderProcessHost* rph = web_contents()->GetRenderProcessHost(); 158 content::RenderProcessHost* rph = web_contents()->GetRenderProcessHost();
166 159
167 // This should always be 0 once we get modal window.print(). 160 if (base::ContainsKey(map, rph)) {
tommycli 2016/08/26 16:40:13 nit: no need for comment anymore?
Lei Zhang 2016/08/26 16:42:19 Are you referring to the old line 167 comment? Sli
tommycli 2016/08/26 16:42:59 yeah. okay cool
168 if (map.count(rph) != 0) {
169 // Renderer already handling window.print() in another View. 161 // Renderer already handling window.print() in another View.
170 Send(reply_msg); 162 Send(reply_msg);
171 return; 163 return;
172 } 164 }
165
173 if (print_preview_state_ != NOT_PREVIEWING) { 166 if (print_preview_state_ != NOT_PREVIEWING) {
174 // If a user initiated print dialog is already open, ignore the scripted 167 // If a user initiated print dialog is already open, ignore the scripted
175 // print message. 168 // print message.
176 DCHECK_EQ(USER_INITIATED_PREVIEW, print_preview_state_); 169 DCHECK_EQ(USER_INITIATED_PREVIEW, print_preview_state_);
177 Send(reply_msg); 170 Send(reply_msg);
178 return; 171 return;
179 } 172 }
180 173
181 PrintPreviewDialogController* dialog_controller = 174 PrintPreviewDialogController* dialog_controller =
182 PrintPreviewDialogController::GetInstance(); 175 PrintPreviewDialogController::GetInstance();
183 if (!dialog_controller) { 176 if (!dialog_controller) {
184 Send(reply_msg); 177 Send(reply_msg);
185 return; 178 return;
186 } 179 }
187 180
188 print_preview_state_ = SCRIPTED_PREVIEW; 181 print_preview_state_ = SCRIPTED_PREVIEW;
189 base::Closure callback = 182 map[rph] = base::Bind(&PrintViewManager::OnScriptedPrintPreviewReply,
190 base::Bind(&PrintViewManager::OnScriptedPrintPreviewReply, 183 base::Unretained(this), reply_msg);
191 base::Unretained(this),
192 reply_msg);
193 map[rph] = callback;
194 scripted_print_preview_rph_ = rph; 184 scripted_print_preview_rph_ = rph;
195 } 185 }
196 186
197 void PrintViewManager::OnShowScriptedPrintPreview(bool source_is_modifiable) { 187 void PrintViewManager::OnShowScriptedPrintPreview(bool source_is_modifiable) {
198 PrintPreviewDialogController* dialog_controller = 188 PrintPreviewDialogController* dialog_controller =
199 PrintPreviewDialogController::GetInstance(); 189 PrintPreviewDialogController::GetInstance();
200 if (!dialog_controller) { 190 if (!dialog_controller) {
201 PrintPreviewDone(); 191 PrintPreviewDone();
202 return; 192 return;
203 } 193 }
194
204 dialog_controller->PrintPreview(web_contents()); 195 dialog_controller->PrintPreview(web_contents());
205 PrintHostMsg_RequestPrintPreview_Params params; 196 PrintHostMsg_RequestPrintPreview_Params params;
206 params.is_modifiable = source_is_modifiable; 197 params.is_modifiable = source_is_modifiable;
207 PrintPreviewUI::SetInitialParams( 198 PrintPreviewUI::SetInitialParams(
208 dialog_controller->GetPrintPreviewForContents(web_contents()), params); 199 dialog_controller->GetPrintPreviewForContents(web_contents()), params);
209 } 200 }
210 201
211 void PrintViewManager::OnScriptedPrintPreviewReply(IPC::Message* reply_msg) { 202 void PrintViewManager::OnScriptedPrintPreviewReply(IPC::Message* reply_msg) {
212 DCHECK_CURRENTLY_ON(BrowserThread::UI); 203 DCHECK_CURRENTLY_ON(BrowserThread::UI);
213 Send(reply_msg); 204 Send(reply_msg);
214 } 205 }
215 206
216 bool PrintViewManager::OnMessageReceived(const IPC::Message& message) { 207 bool PrintViewManager::OnMessageReceived(const IPC::Message& message) {
217 bool handled = true; 208 bool handled = true;
218 IPC_BEGIN_MESSAGE_MAP(PrintViewManager, message) 209 IPC_BEGIN_MESSAGE_MAP(PrintViewManager, message)
219 IPC_MESSAGE_HANDLER(PrintHostMsg_DidShowPrintDialog, OnDidShowPrintDialog) 210 IPC_MESSAGE_HANDLER(PrintHostMsg_DidShowPrintDialog, OnDidShowPrintDialog)
220 IPC_MESSAGE_HANDLER_DELAY_REPLY(PrintHostMsg_SetupScriptedPrintPreview, 211 IPC_MESSAGE_HANDLER_DELAY_REPLY(PrintHostMsg_SetupScriptedPrintPreview,
221 OnSetupScriptedPrintPreview) 212 OnSetupScriptedPrintPreview)
222 IPC_MESSAGE_HANDLER(PrintHostMsg_ShowScriptedPrintPreview, 213 IPC_MESSAGE_HANDLER(PrintHostMsg_ShowScriptedPrintPreview,
223 OnShowScriptedPrintPreview) 214 OnShowScriptedPrintPreview)
224 IPC_MESSAGE_UNHANDLED(handled = false) 215 IPC_MESSAGE_UNHANDLED(handled = false)
225 IPC_END_MESSAGE_MAP() 216 IPC_END_MESSAGE_MAP()
226 217
227 return handled ? true : PrintViewManagerBase::OnMessageReceived(message); 218 return handled || PrintViewManagerBase::OnMessageReceived(message);
228 } 219 }
229 220
230 } // namespace printing 221 } // namespace printing
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698