OLD | NEW |
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 27 matching lines...) Expand all Loading... |
48 render_process_id, render_frame_id, GURL(), pdf_plugin); | 50 render_process_id, render_frame_id, GURL(), pdf_plugin); |
49 } | 51 } |
50 | 52 |
51 } // namespace | 53 } // namespace |
52 | 54 |
53 namespace printing { | 55 namespace printing { |
54 | 56 |
55 PrintViewManager::PrintViewManager(content::WebContents* web_contents) | 57 PrintViewManager::PrintViewManager(content::WebContents* web_contents) |
56 : PrintViewManagerBase(web_contents), | 58 : PrintViewManagerBase(web_contents), |
57 print_preview_state_(NOT_PREVIEWING), | 59 print_preview_state_(NOT_PREVIEWING), |
| 60 print_preview_rfh_(nullptr), |
58 scripted_print_preview_rph_(nullptr) { | 61 scripted_print_preview_rph_(nullptr) { |
59 if (PrintPreviewDialogController::IsPrintPreviewDialog(web_contents)) { | 62 if (PrintPreviewDialogController::IsPrintPreviewDialog(web_contents)) { |
60 EnableInternalPDFPluginForContents( | 63 EnableInternalPDFPluginForContents( |
61 web_contents->GetRenderProcessHost()->GetID(), | 64 web_contents->GetRenderProcessHost()->GetID(), |
62 web_contents->GetMainFrame()->GetRoutingID()); | 65 web_contents->GetMainFrame()->GetRoutingID()); |
63 } | 66 } |
64 } | 67 } |
65 | 68 |
66 PrintViewManager::~PrintViewManager() { | 69 PrintViewManager::~PrintViewManager() { |
67 DCHECK_EQ(NOT_PREVIEWING, print_preview_state_); | 70 DCHECK_EQ(NOT_PREVIEWING, print_preview_state_); |
68 } | 71 } |
69 | 72 |
70 #if BUILDFLAG(ENABLE_BASIC_PRINTING) | 73 #if BUILDFLAG(ENABLE_BASIC_PRINTING) |
71 bool PrintViewManager::PrintForSystemDialogNow( | 74 bool PrintViewManager::PrintForSystemDialogNow( |
72 const base::Closure& dialog_shown_callback) { | 75 const base::Closure& dialog_shown_callback) { |
73 DCHECK(!dialog_shown_callback.is_null()); | 76 DCHECK(!dialog_shown_callback.is_null()); |
74 DCHECK(on_print_dialog_shown_callback_.is_null()); | 77 DCHECK(on_print_dialog_shown_callback_.is_null()); |
75 on_print_dialog_shown_callback_ = dialog_shown_callback; | 78 on_print_dialog_shown_callback_ = dialog_shown_callback; |
76 return PrintNowInternal(new PrintMsg_PrintForSystemDialog(routing_id())); | 79 |
| 80 SetPrintingRFH(print_preview_rfh_); |
| 81 int32_t id = print_preview_rfh_->GetRoutingID(); |
| 82 return PrintNowInternal(print_preview_rfh_, |
| 83 base::MakeUnique<PrintMsg_PrintForSystemDialog>(id)); |
77 } | 84 } |
78 | 85 |
79 bool PrintViewManager::BasicPrint() { | 86 bool PrintViewManager::BasicPrint(content::RenderFrameHost* rfh) { |
80 PrintPreviewDialogController* dialog_controller = | 87 PrintPreviewDialogController* dialog_controller = |
81 PrintPreviewDialogController::GetInstance(); | 88 PrintPreviewDialogController::GetInstance(); |
82 if (!dialog_controller) | 89 if (!dialog_controller) |
83 return false; | 90 return false; |
84 | 91 |
85 content::WebContents* print_preview_dialog = | 92 content::WebContents* print_preview_dialog = |
86 dialog_controller->GetPrintPreviewForContents(web_contents()); | 93 dialog_controller->GetPrintPreviewForContents(web_contents()); |
87 if (!print_preview_dialog) | 94 if (!print_preview_dialog) |
88 return PrintNow(); | 95 return PrintNow(rfh); |
89 | 96 |
90 if (!print_preview_dialog->GetWebUI()) | 97 if (!print_preview_dialog->GetWebUI()) |
91 return false; | 98 return false; |
92 | 99 |
93 PrintPreviewUI* print_preview_ui = static_cast<PrintPreviewUI*>( | 100 PrintPreviewUI* print_preview_ui = static_cast<PrintPreviewUI*>( |
94 print_preview_dialog->GetWebUI()->GetController()); | 101 print_preview_dialog->GetWebUI()->GetController()); |
95 print_preview_ui->OnShowSystemDialog(); | 102 print_preview_ui->OnShowSystemDialog(); |
96 return true; | 103 return true; |
97 } | 104 } |
98 #endif // BUILDFLAG(ENABLE_BASIC_PRINTING) | 105 #endif // BUILDFLAG(ENABLE_BASIC_PRINTING) |
99 | 106 |
100 bool PrintViewManager::PrintPreviewNow(bool selection_only) { | 107 bool PrintViewManager::PrintPreviewNow(content::RenderFrameHost* rfh, |
| 108 bool has_selection) { |
101 // Users can send print commands all they want and it is beyond | 109 // Users can send print commands all they want and it is beyond |
102 // PrintViewManager's control. Just ignore the extra commands. | 110 // PrintViewManager's control. Just ignore the extra commands. |
103 // See http://crbug.com/136842 for example. | 111 // See http://crbug.com/136842 for example. |
104 if (print_preview_state_ != NOT_PREVIEWING) | 112 if (print_preview_state_ != NOT_PREVIEWING) |
105 return false; | 113 return false; |
106 | 114 |
107 if (!PrintNowInternal(new PrintMsg_InitiatePrintPreview(routing_id(), | 115 auto message = base::MakeUnique<PrintMsg_InitiatePrintPreview>( |
108 selection_only))) { | 116 rfh->GetRoutingID(), has_selection); |
| 117 if (!PrintNowInternal(rfh, std::move(message))) |
109 return false; | 118 return false; |
110 } | |
111 | 119 |
| 120 DCHECK(!print_preview_rfh_); |
| 121 print_preview_rfh_ = rfh; |
112 print_preview_state_ = USER_INITIATED_PREVIEW; | 122 print_preview_state_ = USER_INITIATED_PREVIEW; |
113 return true; | 123 return true; |
114 } | 124 } |
115 | 125 |
116 void PrintViewManager::PrintPreviewForWebNode() { | 126 void PrintViewManager::PrintPreviewForWebNode() { |
117 if (print_preview_state_ != NOT_PREVIEWING) | 127 if (print_preview_state_ != NOT_PREVIEWING) |
118 return; | 128 return; |
119 print_preview_state_ = USER_INITIATED_PREVIEW; | 129 print_preview_state_ = USER_INITIATED_PREVIEW; |
120 } | 130 } |
121 | 131 |
122 void PrintViewManager::PrintPreviewDone() { | 132 void PrintViewManager::PrintPreviewDone() { |
123 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 133 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
124 DCHECK_NE(NOT_PREVIEWING, print_preview_state_); | 134 DCHECK_NE(NOT_PREVIEWING, print_preview_state_); |
125 | 135 |
126 if (print_preview_state_ == SCRIPTED_PREVIEW) { | 136 if (print_preview_state_ == SCRIPTED_PREVIEW) { |
127 auto& map = g_scripted_print_preview_closure_map.Get(); | 137 auto& map = g_scripted_print_preview_closure_map.Get(); |
128 auto it = map.find(scripted_print_preview_rph_); | 138 auto it = map.find(scripted_print_preview_rph_); |
129 CHECK(it != map.end()); | 139 CHECK(it != map.end()); |
130 it->second.Run(); | 140 it->second.Run(); |
131 map.erase(it); | 141 map.erase(it); |
132 scripted_print_preview_rph_ = nullptr; | 142 scripted_print_preview_rph_ = nullptr; |
133 } | 143 } |
134 print_preview_state_ = NOT_PREVIEWING; | 144 print_preview_state_ = NOT_PREVIEWING; |
| 145 print_preview_rfh_ = nullptr; |
135 } | 146 } |
136 | 147 |
137 void PrintViewManager::RenderFrameCreated( | 148 void PrintViewManager::RenderFrameCreated( |
138 content::RenderFrameHost* render_frame_host) { | 149 content::RenderFrameHost* render_frame_host) { |
139 if (PrintPreviewDialogController::IsPrintPreviewDialog(web_contents())) { | 150 if (PrintPreviewDialogController::IsPrintPreviewDialog(web_contents())) { |
140 EnableInternalPDFPluginForContents(render_frame_host->GetProcess()->GetID(), | 151 EnableInternalPDFPluginForContents(render_frame_host->GetProcess()->GetID(), |
141 render_frame_host->GetRoutingID()); | 152 render_frame_host->GetRoutingID()); |
142 } | 153 } |
143 } | 154 } |
144 | 155 |
145 void PrintViewManager::RenderProcessGone(base::TerminationStatus status) { | 156 void PrintViewManager::RenderFrameDeleted( |
146 print_preview_state_ = NOT_PREVIEWING; | 157 content::RenderFrameHost* render_frame_host) { |
147 PrintViewManagerBase::RenderProcessGone(status); | 158 if (render_frame_host == print_preview_rfh_) |
| 159 print_preview_state_ = NOT_PREVIEWING; |
| 160 PrintViewManagerBase::RenderFrameDeleted(render_frame_host); |
148 } | 161 } |
149 | 162 |
150 void PrintViewManager::OnDidShowPrintDialog() { | 163 void PrintViewManager::OnDidShowPrintDialog(content::RenderFrameHost* rfh) { |
151 if (!on_print_dialog_shown_callback_.is_null()) | 164 if (rfh != print_preview_rfh_) |
152 on_print_dialog_shown_callback_.Run(); | 165 return; |
| 166 |
| 167 if (on_print_dialog_shown_callback_.is_null()) |
| 168 return; |
| 169 |
| 170 on_print_dialog_shown_callback_.Run(); |
153 on_print_dialog_shown_callback_.Reset(); | 171 on_print_dialog_shown_callback_.Reset(); |
154 } | 172 } |
155 | 173 |
156 void PrintViewManager::OnSetupScriptedPrintPreview(IPC::Message* reply_msg) { | 174 void PrintViewManager::OnSetupScriptedPrintPreview( |
| 175 content::RenderFrameHost* rfh, |
| 176 IPC::Message* reply_msg) { |
157 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 177 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
158 auto& map = g_scripted_print_preview_closure_map.Get(); | 178 auto& map = g_scripted_print_preview_closure_map.Get(); |
159 content::RenderProcessHost* rph = web_contents()->GetRenderProcessHost(); | 179 content::RenderProcessHost* rph = rfh->GetProcess(); |
160 | 180 |
161 if (base::ContainsKey(map, rph)) { | 181 if (base::ContainsKey(map, rph)) { |
162 // Renderer already handling window.print() in another View. | 182 // Renderer already handling window.print(). Abort this attempt to prevent |
163 Send(reply_msg); | 183 // the renderer from having multiple nested loops. If multiple nested loops |
| 184 // existed, then they have to exit in the right order and that is messy. |
| 185 rfh->Send(reply_msg); |
164 return; | 186 return; |
165 } | 187 } |
166 | 188 |
167 if (print_preview_state_ != NOT_PREVIEWING) { | 189 if (print_preview_state_ != NOT_PREVIEWING) { |
168 // If a user initiated print dialog is already open, ignore the scripted | 190 // If a print dialog is already open for this tab, ignore the scripted print |
169 // print message. | 191 // message. |
170 DCHECK_EQ(USER_INITIATED_PREVIEW, print_preview_state_); | 192 rfh->Send(reply_msg); |
171 Send(reply_msg); | |
172 return; | 193 return; |
173 } | 194 } |
174 | 195 |
175 PrintPreviewDialogController* dialog_controller = | 196 PrintPreviewDialogController* dialog_controller = |
176 PrintPreviewDialogController::GetInstance(); | 197 PrintPreviewDialogController::GetInstance(); |
177 if (!dialog_controller) { | 198 if (!dialog_controller) { |
178 Send(reply_msg); | 199 rfh->Send(reply_msg); |
179 return; | 200 return; |
180 } | 201 } |
181 | 202 |
| 203 DCHECK(!print_preview_rfh_); |
| 204 print_preview_rfh_ = rfh; |
182 print_preview_state_ = SCRIPTED_PREVIEW; | 205 print_preview_state_ = SCRIPTED_PREVIEW; |
183 map[rph] = base::Bind(&PrintViewManager::OnScriptedPrintPreviewReply, | 206 map[rph] = base::Bind(&PrintViewManager::OnScriptedPrintPreviewReply, |
184 base::Unretained(this), reply_msg); | 207 base::Unretained(this), reply_msg); |
185 scripted_print_preview_rph_ = rph; | 208 scripted_print_preview_rph_ = rph; |
186 } | 209 } |
187 | 210 |
188 void PrintViewManager::OnShowScriptedPrintPreview(bool source_is_modifiable) { | 211 void PrintViewManager::OnShowScriptedPrintPreview(content::RenderFrameHost* rfh, |
| 212 bool source_is_modifiable) { |
| 213 DCHECK(print_preview_rfh_); |
| 214 if (rfh != print_preview_rfh_) |
| 215 return; |
| 216 |
189 PrintPreviewDialogController* dialog_controller = | 217 PrintPreviewDialogController* dialog_controller = |
190 PrintPreviewDialogController::GetInstance(); | 218 PrintPreviewDialogController::GetInstance(); |
191 if (!dialog_controller) { | 219 if (!dialog_controller) { |
192 PrintPreviewDone(); | 220 PrintPreviewDone(); |
193 return; | 221 return; |
194 } | 222 } |
195 | 223 |
196 dialog_controller->PrintPreview(web_contents()); | 224 dialog_controller->PrintPreview(web_contents()); |
197 PrintHostMsg_RequestPrintPreview_Params params; | 225 PrintHostMsg_RequestPrintPreview_Params params; |
198 params.is_modifiable = source_is_modifiable; | 226 params.is_modifiable = source_is_modifiable; |
199 PrintPreviewUI::SetInitialParams( | 227 PrintPreviewUI::SetInitialParams( |
200 dialog_controller->GetPrintPreviewForContents(web_contents()), params); | 228 dialog_controller->GetPrintPreviewForContents(web_contents()), params); |
201 } | 229 } |
202 | 230 |
203 void PrintViewManager::OnScriptedPrintPreviewReply(IPC::Message* reply_msg) { | 231 void PrintViewManager::OnScriptedPrintPreviewReply(IPC::Message* reply_msg) { |
204 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 232 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
205 Send(reply_msg); | 233 print_preview_rfh_->Send(reply_msg); |
206 } | 234 } |
207 | 235 |
208 bool PrintViewManager::OnMessageReceived(const IPC::Message& message) { | 236 bool PrintViewManager::OnMessageReceived( |
| 237 const IPC::Message& message, |
| 238 content::RenderFrameHost* render_frame_host) { |
209 bool handled = true; | 239 bool handled = true; |
210 IPC_BEGIN_MESSAGE_MAP(PrintViewManager, message) | 240 IPC_BEGIN_MESSAGE_MAP_WITH_PARAM(PrintViewManager, message, render_frame_host) |
211 IPC_MESSAGE_HANDLER(PrintHostMsg_DidShowPrintDialog, OnDidShowPrintDialog) | 241 IPC_MESSAGE_HANDLER(PrintHostMsg_DidShowPrintDialog, OnDidShowPrintDialog) |
212 IPC_MESSAGE_HANDLER_DELAY_REPLY(PrintHostMsg_SetupScriptedPrintPreview, | 242 IPC_MESSAGE_HANDLER_WITH_PARAM_DELAY_REPLY( |
213 OnSetupScriptedPrintPreview) | 243 PrintHostMsg_SetupScriptedPrintPreview, OnSetupScriptedPrintPreview) |
214 IPC_MESSAGE_HANDLER(PrintHostMsg_ShowScriptedPrintPreview, | 244 IPC_MESSAGE_HANDLER(PrintHostMsg_ShowScriptedPrintPreview, |
215 OnShowScriptedPrintPreview) | 245 OnShowScriptedPrintPreview) |
216 IPC_MESSAGE_UNHANDLED(handled = false) | 246 IPC_MESSAGE_UNHANDLED(handled = false) |
217 IPC_END_MESSAGE_MAP() | 247 IPC_END_MESSAGE_MAP() |
218 | 248 |
219 return handled || PrintViewManagerBase::OnMessageReceived(message); | 249 return handled || |
| 250 PrintViewManagerBase::OnMessageReceived(message, render_frame_host); |
220 } | 251 } |
221 | 252 |
222 } // namespace printing | 253 } // namespace printing |
OLD | NEW |