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

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

Issue 2426503002: Make printing work better with OOPIF. (Closed)
Patch Set: Fix build, nits Created 4 years, 1 month 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
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 #include <utility>
8 9
9 #include "base/bind.h" 10 #include "base/bind.h"
10 #include "base/lazy_instance.h" 11 #include "base/lazy_instance.h"
12 #include "base/memory/ptr_util.h"
11 #include "chrome/browser/plugins/chrome_plugin_service_filter.h" 13 #include "chrome/browser/plugins/chrome_plugin_service_filter.h"
12 #include "chrome/browser/printing/print_preview_dialog_controller.h" 14 #include "chrome/browser/printing/print_preview_dialog_controller.h"
13 #include "chrome/browser/ui/webui/print_preview/print_preview_ui.h" 15 #include "chrome/browser/ui/webui/print_preview/print_preview_ui.h"
14 #include "chrome/common/chrome_content_client.h" 16 #include "chrome/common/chrome_content_client.h"
15 #include "components/printing/common/print_messages.h" 17 #include "components/printing/common/print_messages.h"
16 #include "content/public/browser/browser_thread.h" 18 #include "content/public/browser/browser_thread.h"
17 #include "content/public/browser/plugin_service.h" 19 #include "content/public/browser/plugin_service.h"
18 #include "content/public/browser/render_frame_host.h" 20 #include "content/public/browser/render_frame_host.h"
19 #include "content/public/browser/render_process_host.h" 21 #include "content/public/browser/render_process_host.h"
20 #include "content/public/browser/web_contents.h" 22 #include "content/public/browser/web_contents.h"
(...skipping 26 matching lines...) Expand all
47 render_process_id, render_frame_id, GURL(), pdf_plugin); 49 render_process_id, render_frame_id, GURL(), pdf_plugin);
48 } 50 }
49 51
50 } // namespace 52 } // namespace
51 53
52 namespace printing { 54 namespace printing {
53 55
54 PrintViewManager::PrintViewManager(content::WebContents* web_contents) 56 PrintViewManager::PrintViewManager(content::WebContents* web_contents)
55 : PrintViewManagerBase(web_contents), 57 : PrintViewManagerBase(web_contents),
56 print_preview_state_(NOT_PREVIEWING), 58 print_preview_state_(NOT_PREVIEWING),
59 print_preview_rfh_(nullptr),
60 scripted_print_rfh_(nullptr),
57 scripted_print_preview_rph_(nullptr) { 61 scripted_print_preview_rph_(nullptr) {
58 if (PrintPreviewDialogController::IsPrintPreviewDialog(web_contents)) { 62 if (PrintPreviewDialogController::IsPrintPreviewDialog(web_contents)) {
59 EnableInternalPDFPluginForContents( 63 EnableInternalPDFPluginForContents(
60 web_contents->GetRenderProcessHost()->GetID(), 64 web_contents->GetRenderProcessHost()->GetID(),
61 web_contents->GetMainFrame()->GetRoutingID()); 65 web_contents->GetMainFrame()->GetRoutingID());
62 } 66 }
63 } 67 }
64 68
65 PrintViewManager::~PrintViewManager() { 69 PrintViewManager::~PrintViewManager() {
66 DCHECK_EQ(NOT_PREVIEWING, print_preview_state_); 70 DCHECK_EQ(NOT_PREVIEWING, print_preview_state_);
67 } 71 }
68 72
69 #if defined(ENABLE_BASIC_PRINTING) 73 #if defined(ENABLE_BASIC_PRINTING)
70 bool PrintViewManager::PrintForSystemDialogNow( 74 bool PrintViewManager::PrintForSystemDialogNow(
71 const base::Closure& dialog_shown_callback) { 75 const base::Closure& dialog_shown_callback) {
72 DCHECK(!dialog_shown_callback.is_null()); 76 DCHECK(!dialog_shown_callback.is_null());
73 DCHECK(on_print_dialog_shown_callback_.is_null()); 77 DCHECK(on_print_dialog_shown_callback_.is_null());
74 on_print_dialog_shown_callback_ = dialog_shown_callback; 78 on_print_dialog_shown_callback_ = dialog_shown_callback;
75 return PrintNowInternal(new PrintMsg_PrintForSystemDialog(routing_id())); 79
80 int32_t id = print_preview_rfh_->GetRoutingID();
81 return PrintNowInternal(base::MakeUnique<PrintMsg_PrintForSystemDialog>(id));
76 } 82 }
77 83
78 bool PrintViewManager::BasicPrint() { 84 bool PrintViewManager::BasicPrint(content::RenderFrameHost* rfh) {
79 PrintPreviewDialogController* dialog_controller = 85 PrintPreviewDialogController* dialog_controller =
80 PrintPreviewDialogController::GetInstance(); 86 PrintPreviewDialogController::GetInstance();
81 if (!dialog_controller) 87 if (!dialog_controller)
82 return false; 88 return false;
83 89
84 content::WebContents* print_preview_dialog = 90 content::WebContents* print_preview_dialog =
85 dialog_controller->GetPrintPreviewForContents(web_contents()); 91 dialog_controller->GetPrintPreviewForContents(web_contents());
86 if (!print_preview_dialog) 92 if (!print_preview_dialog)
87 return PrintNow(); 93 return PrintNow(rfh);
88 94
89 if (!print_preview_dialog->GetWebUI()) 95 if (!print_preview_dialog->GetWebUI())
90 return false; 96 return false;
91 97
92 PrintPreviewUI* print_preview_ui = static_cast<PrintPreviewUI*>( 98 PrintPreviewUI* print_preview_ui = static_cast<PrintPreviewUI*>(
93 print_preview_dialog->GetWebUI()->GetController()); 99 print_preview_dialog->GetWebUI()->GetController());
94 print_preview_ui->OnShowSystemDialog(); 100 print_preview_ui->OnShowSystemDialog();
95 return true; 101 return true;
96 } 102 }
97 #endif // defined(ENABLE_BASIC_PRINTING) 103 #endif // defined(ENABLE_BASIC_PRINTING)
98 104
99 bool PrintViewManager::PrintPreviewNow(bool selection_only) { 105 bool PrintViewManager::PrintPreviewNow(content::RenderFrameHost* rfh,
106 bool has_selection) {
100 // Users can send print commands all they want and it is beyond 107 // Users can send print commands all they want and it is beyond
101 // PrintViewManager's control. Just ignore the extra commands. 108 // PrintViewManager's control. Just ignore the extra commands.
102 // See http://crbug.com/136842 for example. 109 // See http://crbug.com/136842 for example.
103 if (print_preview_state_ != NOT_PREVIEWING) 110 if (print_preview_state_ != NOT_PREVIEWING)
104 return false; 111 return false;
105 112
106 if (!PrintNowInternal(new PrintMsg_InitiatePrintPreview(routing_id(), 113 auto message = base::MakeUnique<PrintMsg_InitiatePrintPreview>(
107 selection_only))) { 114 rfh->GetRoutingID(), has_selection);
115 if (!PrintNowInternal(std::move(message)))
108 return false; 116 return false;
109 }
110 117
118 DCHECK(!print_preview_rfh_);
119 print_preview_rfh_ = rfh;
111 print_preview_state_ = USER_INITIATED_PREVIEW; 120 print_preview_state_ = USER_INITIATED_PREVIEW;
112 return true; 121 return true;
113 } 122 }
114 123
115 void PrintViewManager::PrintPreviewForWebNode() { 124 void PrintViewManager::PrintPreviewForWebNode() {
116 if (print_preview_state_ != NOT_PREVIEWING) 125 if (print_preview_state_ != NOT_PREVIEWING)
117 return; 126 return;
118 print_preview_state_ = USER_INITIATED_PREVIEW; 127 print_preview_state_ = USER_INITIATED_PREVIEW;
119 } 128 }
120 129
121 void PrintViewManager::PrintPreviewDone() { 130 void PrintViewManager::PrintPreviewDone() {
122 DCHECK_CURRENTLY_ON(BrowserThread::UI); 131 DCHECK_CURRENTLY_ON(BrowserThread::UI);
123 DCHECK_NE(NOT_PREVIEWING, print_preview_state_); 132 DCHECK_NE(NOT_PREVIEWING, print_preview_state_);
124 133
125 if (print_preview_state_ == SCRIPTED_PREVIEW) { 134 if (print_preview_state_ == SCRIPTED_PREVIEW) {
126 auto& map = g_scripted_print_preview_closure_map.Get(); 135 auto& map = g_scripted_print_preview_closure_map.Get();
127 auto it = map.find(scripted_print_preview_rph_); 136 auto it = map.find(scripted_print_preview_rph_);
128 CHECK(it != map.end()); 137 CHECK(it != map.end());
129 it->second.Run(); 138 it->second.Run();
130 map.erase(it); 139 map.erase(it);
131 scripted_print_preview_rph_ = nullptr; 140 scripted_print_preview_rph_ = nullptr;
132 } 141 }
133 print_preview_state_ = NOT_PREVIEWING; 142 print_preview_state_ = NOT_PREVIEWING;
143 print_preview_rfh_ = nullptr;
134 } 144 }
135 145
136 void PrintViewManager::RenderFrameCreated( 146 void PrintViewManager::RenderFrameCreated(
137 content::RenderFrameHost* render_frame_host) { 147 content::RenderFrameHost* render_frame_host) {
138 if (PrintPreviewDialogController::IsPrintPreviewDialog(web_contents())) { 148 if (PrintPreviewDialogController::IsPrintPreviewDialog(web_contents())) {
139 EnableInternalPDFPluginForContents(render_frame_host->GetProcess()->GetID(), 149 EnableInternalPDFPluginForContents(render_frame_host->GetProcess()->GetID(),
140 render_frame_host->GetRoutingID()); 150 render_frame_host->GetRoutingID());
141 } 151 }
142 } 152 }
143 153
144 void PrintViewManager::RenderProcessGone(base::TerminationStatus status) { 154 void PrintViewManager::RenderProcessGone(base::TerminationStatus status) {
nasko 2016/11/02 04:50:37 Should this change to RenderFrameDeleted?
Lei Zhang 2016/11/08 11:13:22 Not sure. Added a TODO and will get back to it lat
nasko 2016/11/08 22:18:35 Acknowledged.
145 print_preview_state_ = NOT_PREVIEWING; 155 print_preview_state_ = NOT_PREVIEWING;
146 PrintViewManagerBase::RenderProcessGone(status); 156 PrintViewManagerBase::RenderProcessGone(status);
nasko 2016/11/02 04:50:37 Isn't that gone from the base object?
Lei Zhang 2016/11/08 11:13:22 The inheritance tree is several levels deep.
nasko 2016/11/08 22:18:35 Acknowledged.
147 } 157 }
148 158
149 void PrintViewManager::OnDidShowPrintDialog() { 159 void PrintViewManager::OnDidShowPrintDialog(content::RenderFrameHost* rfh) {
150 if (!on_print_dialog_shown_callback_.is_null()) 160 bool has_callback = !on_print_dialog_shown_callback_.is_null();
151 on_print_dialog_shown_callback_.Run(); 161 if (!has_callback)
162 return;
163
164 if (rfh != print_preview_rfh_)
165 return;
166
167 on_print_dialog_shown_callback_.Run();
152 on_print_dialog_shown_callback_.Reset(); 168 on_print_dialog_shown_callback_.Reset();
153 } 169 }
154 170
155 void PrintViewManager::OnSetupScriptedPrintPreview(IPC::Message* reply_msg) { 171 void PrintViewManager::OnSetupScriptedPrintPreview(IPC::Message* reply_msg) {
172 DCHECK(scripted_print_rfh_);
173 content::RenderFrameHost* rfh = scripted_print_rfh_;
174 scripted_print_rfh_ = nullptr;
175
156 DCHECK_CURRENTLY_ON(BrowserThread::UI); 176 DCHECK_CURRENTLY_ON(BrowserThread::UI);
157 auto& map = g_scripted_print_preview_closure_map.Get(); 177 auto& map = g_scripted_print_preview_closure_map.Get();
158 content::RenderProcessHost* rph = web_contents()->GetRenderProcessHost(); 178 content::RenderProcessHost* rph = web_contents()->GetRenderProcessHost();
159 179
160 if (base::ContainsKey(map, rph)) { 180 if (base::ContainsKey(map, rph)) {
161 // Renderer already handling window.print() in another View. 181 // Renderer already handling window.print() in another View.
162 Send(reply_msg); 182 Send(reply_msg);
163 return; 183 return;
164 } 184 }
165 185
166 if (print_preview_state_ != NOT_PREVIEWING) { 186 if (print_preview_state_ != NOT_PREVIEWING) {
167 // If a user initiated print dialog is already open, ignore the scripted 187 // If a user initiated print dialog is already open, ignore the scripted
168 // print message. 188 // print message.
169 DCHECK_EQ(USER_INITIATED_PREVIEW, print_preview_state_); 189 DCHECK_EQ(USER_INITIATED_PREVIEW, print_preview_state_);
170 Send(reply_msg); 190 Send(reply_msg);
171 return; 191 return;
172 } 192 }
173 193
174 PrintPreviewDialogController* dialog_controller = 194 PrintPreviewDialogController* dialog_controller =
175 PrintPreviewDialogController::GetInstance(); 195 PrintPreviewDialogController::GetInstance();
176 if (!dialog_controller) { 196 if (!dialog_controller) {
177 Send(reply_msg); 197 Send(reply_msg);
178 return; 198 return;
179 } 199 }
180 200
201 DCHECK(!print_preview_rfh_);
202 print_preview_rfh_ = rfh;
181 print_preview_state_ = SCRIPTED_PREVIEW; 203 print_preview_state_ = SCRIPTED_PREVIEW;
182 map[rph] = base::Bind(&PrintViewManager::OnScriptedPrintPreviewReply, 204 map[rph] = base::Bind(&PrintViewManager::OnScriptedPrintPreviewReply,
183 base::Unretained(this), reply_msg); 205 base::Unretained(this), reply_msg);
184 scripted_print_preview_rph_ = rph; 206 scripted_print_preview_rph_ = rph;
185 } 207 }
186 208
187 void PrintViewManager::OnShowScriptedPrintPreview(bool source_is_modifiable) { 209 void PrintViewManager::OnShowScriptedPrintPreview(content::RenderFrameHost* rfh,
210 bool source_is_modifiable) {
211 DCHECK(print_preview_rfh_);
212 if (rfh != print_preview_rfh_)
213 return;
214
188 PrintPreviewDialogController* dialog_controller = 215 PrintPreviewDialogController* dialog_controller =
189 PrintPreviewDialogController::GetInstance(); 216 PrintPreviewDialogController::GetInstance();
190 if (!dialog_controller) { 217 if (!dialog_controller) {
191 PrintPreviewDone(); 218 PrintPreviewDone();
192 return; 219 return;
193 } 220 }
194 221
195 dialog_controller->PrintPreview(web_contents()); 222 dialog_controller->PrintPreview(web_contents());
196 PrintHostMsg_RequestPrintPreview_Params params; 223 PrintHostMsg_RequestPrintPreview_Params params;
197 params.is_modifiable = source_is_modifiable; 224 params.is_modifiable = source_is_modifiable;
198 PrintPreviewUI::SetInitialParams( 225 PrintPreviewUI::SetInitialParams(
199 dialog_controller->GetPrintPreviewForContents(web_contents()), params); 226 dialog_controller->GetPrintPreviewForContents(web_contents()), params);
200 } 227 }
201 228
202 void PrintViewManager::OnScriptedPrintPreviewReply(IPC::Message* reply_msg) { 229 void PrintViewManager::OnScriptedPrintPreviewReply(IPC::Message* reply_msg) {
203 DCHECK_CURRENTLY_ON(BrowserThread::UI); 230 DCHECK_CURRENTLY_ON(BrowserThread::UI);
204 Send(reply_msg); 231 Send(reply_msg);
205 } 232 }
206 233
207 bool PrintViewManager::OnMessageReceived(const IPC::Message& message) { 234 bool PrintViewManager::OnMessageReceived(
235 const IPC::Message& message,
236 content::RenderFrameHost* render_frame_host) {
237 if (message.type() == PrintHostMsg_SetupScriptedPrintPreview::ID) {
238 DCHECK(!scripted_print_rfh_);
239 scripted_print_rfh_ = render_frame_host;
nasko 2016/11/02 04:50:37 Is this just a hack to "pass" the RFH as parameter
Lei Zhang 2016/11/08 11:13:22 Yes. IPC_MESSAGE_HANDLER_DELAY_REPLY doesn't seem
nasko 2016/11/08 22:18:35 That macro is a wrapper for IPC_MESSAGE_FORWARD_DE
Lei Zhang 2016/11/11 21:06:48 I ventured into ipc/ land and came back with a new
240 }
241
208 bool handled = true; 242 bool handled = true;
209 IPC_BEGIN_MESSAGE_MAP(PrintViewManager, message) 243 IPC_BEGIN_MESSAGE_MAP_WITH_PARAM(PrintViewManager, message, render_frame_host)
210 IPC_MESSAGE_HANDLER(PrintHostMsg_DidShowPrintDialog, OnDidShowPrintDialog) 244 IPC_MESSAGE_HANDLER(PrintHostMsg_DidShowPrintDialog, OnDidShowPrintDialog)
211 IPC_MESSAGE_HANDLER_DELAY_REPLY(PrintHostMsg_SetupScriptedPrintPreview, 245 IPC_MESSAGE_HANDLER_DELAY_REPLY(PrintHostMsg_SetupScriptedPrintPreview,
212 OnSetupScriptedPrintPreview) 246 OnSetupScriptedPrintPreview)
213 IPC_MESSAGE_HANDLER(PrintHostMsg_ShowScriptedPrintPreview, 247 IPC_MESSAGE_HANDLER(PrintHostMsg_ShowScriptedPrintPreview,
214 OnShowScriptedPrintPreview) 248 OnShowScriptedPrintPreview)
215 IPC_MESSAGE_UNHANDLED(handled = false) 249 IPC_MESSAGE_UNHANDLED(handled = false)
216 IPC_END_MESSAGE_MAP() 250 IPC_END_MESSAGE_MAP()
217 251
218 return handled || PrintViewManagerBase::OnMessageReceived(message); 252 return handled ||
253 PrintViewManagerBase::OnMessageReceived(message, render_frame_host);
219 } 254 }
220 255
221 } // namespace printing 256 } // namespace printing
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698