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), |
| 61 scripted_print_rfh_(nullptr), |
58 scripted_print_preview_rph_(nullptr) { | 62 scripted_print_preview_rph_(nullptr) { |
59 if (PrintPreviewDialogController::IsPrintPreviewDialog(web_contents)) { | 63 if (PrintPreviewDialogController::IsPrintPreviewDialog(web_contents)) { |
60 EnableInternalPDFPluginForContents( | 64 EnableInternalPDFPluginForContents( |
61 web_contents->GetRenderProcessHost()->GetID(), | 65 web_contents->GetRenderProcessHost()->GetID(), |
62 web_contents->GetMainFrame()->GetRoutingID()); | 66 web_contents->GetMainFrame()->GetRoutingID()); |
63 } | 67 } |
64 } | 68 } |
65 | 69 |
66 PrintViewManager::~PrintViewManager() { | 70 PrintViewManager::~PrintViewManager() { |
67 DCHECK_EQ(NOT_PREVIEWING, print_preview_state_); | 71 DCHECK_EQ(NOT_PREVIEWING, print_preview_state_); |
68 } | 72 } |
69 | 73 |
70 #if BUILDFLAG(ENABLE_BASIC_PRINTING) | 74 #if BUILDFLAG(ENABLE_BASIC_PRINTING) |
71 bool PrintViewManager::PrintForSystemDialogNow( | 75 bool PrintViewManager::PrintForSystemDialogNow( |
72 const base::Closure& dialog_shown_callback) { | 76 const base::Closure& dialog_shown_callback) { |
73 DCHECK(!dialog_shown_callback.is_null()); | 77 DCHECK(!dialog_shown_callback.is_null()); |
74 DCHECK(on_print_dialog_shown_callback_.is_null()); | 78 DCHECK(on_print_dialog_shown_callback_.is_null()); |
75 on_print_dialog_shown_callback_ = dialog_shown_callback; | 79 on_print_dialog_shown_callback_ = dialog_shown_callback; |
76 return PrintNowInternal(new PrintMsg_PrintForSystemDialog(routing_id())); | 80 |
| 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 |
| 156 // TODO(thestig): Should this be RenderFrameDeleted() instead? |
145 void PrintViewManager::RenderProcessGone(base::TerminationStatus status) { | 157 void PrintViewManager::RenderProcessGone(base::TerminationStatus status) { |
146 print_preview_state_ = NOT_PREVIEWING; | 158 print_preview_state_ = NOT_PREVIEWING; |
147 PrintViewManagerBase::RenderProcessGone(status); | 159 PrintViewManagerBase::RenderProcessGone(status); |
148 } | 160 } |
149 | 161 |
150 void PrintViewManager::OnDidShowPrintDialog() { | 162 void PrintViewManager::OnDidShowPrintDialog(content::RenderFrameHost* rfh) { |
151 if (!on_print_dialog_shown_callback_.is_null()) | 163 bool has_callback = !on_print_dialog_shown_callback_.is_null(); |
152 on_print_dialog_shown_callback_.Run(); | 164 if (!has_callback) |
| 165 return; |
| 166 |
| 167 if (rfh != print_preview_rfh_) |
| 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(IPC::Message* reply_msg) { |
| 175 DCHECK(scripted_print_rfh_); |
| 176 content::RenderFrameHost* rfh = scripted_print_rfh_; |
| 177 scripted_print_rfh_ = nullptr; |
| 178 |
157 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 179 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
158 auto& map = g_scripted_print_preview_closure_map.Get(); | 180 auto& map = g_scripted_print_preview_closure_map.Get(); |
159 content::RenderProcessHost* rph = web_contents()->GetRenderProcessHost(); | 181 content::RenderProcessHost* rph = web_contents()->GetRenderProcessHost(); |
160 | 182 |
161 if (base::ContainsKey(map, rph)) { | 183 if (base::ContainsKey(map, rph)) { |
162 // Renderer already handling window.print() in another View. | 184 // Renderer already handling window.print() in another View. |
163 Send(reply_msg); | 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 user initiated print dialog is already open, ignore the scripted |
169 // print message. | 191 // print message. |
170 DCHECK_EQ(USER_INITIATED_PREVIEW, print_preview_state_); | 192 DCHECK_EQ(USER_INITIATED_PREVIEW, print_preview_state_); |
171 Send(reply_msg); | 193 rfh->Send(reply_msg); |
172 return; | 194 return; |
173 } | 195 } |
174 | 196 |
175 PrintPreviewDialogController* dialog_controller = | 197 PrintPreviewDialogController* dialog_controller = |
176 PrintPreviewDialogController::GetInstance(); | 198 PrintPreviewDialogController::GetInstance(); |
177 if (!dialog_controller) { | 199 if (!dialog_controller) { |
178 Send(reply_msg); | 200 rfh->Send(reply_msg); |
179 return; | 201 return; |
180 } | 202 } |
181 | 203 |
| 204 DCHECK(!print_preview_rfh_); |
| 205 print_preview_rfh_ = rfh; |
182 print_preview_state_ = SCRIPTED_PREVIEW; | 206 print_preview_state_ = SCRIPTED_PREVIEW; |
183 map[rph] = base::Bind(&PrintViewManager::OnScriptedPrintPreviewReply, | 207 map[rph] = base::Bind(&PrintViewManager::OnScriptedPrintPreviewReply, |
184 base::Unretained(this), reply_msg); | 208 base::Unretained(this), reply_msg); |
185 scripted_print_preview_rph_ = rph; | 209 scripted_print_preview_rph_ = rph; |
186 } | 210 } |
187 | 211 |
188 void PrintViewManager::OnShowScriptedPrintPreview(bool source_is_modifiable) { | 212 void PrintViewManager::OnShowScriptedPrintPreview(content::RenderFrameHost* rfh, |
| 213 bool source_is_modifiable) { |
| 214 DCHECK(print_preview_rfh_); |
| 215 if (rfh != print_preview_rfh_) |
| 216 return; |
| 217 |
189 PrintPreviewDialogController* dialog_controller = | 218 PrintPreviewDialogController* dialog_controller = |
190 PrintPreviewDialogController::GetInstance(); | 219 PrintPreviewDialogController::GetInstance(); |
191 if (!dialog_controller) { | 220 if (!dialog_controller) { |
192 PrintPreviewDone(); | 221 PrintPreviewDone(); |
193 return; | 222 return; |
194 } | 223 } |
195 | 224 |
196 dialog_controller->PrintPreview(web_contents()); | 225 dialog_controller->PrintPreview(web_contents()); |
197 PrintHostMsg_RequestPrintPreview_Params params; | 226 PrintHostMsg_RequestPrintPreview_Params params; |
198 params.is_modifiable = source_is_modifiable; | 227 params.is_modifiable = source_is_modifiable; |
199 PrintPreviewUI::SetInitialParams( | 228 PrintPreviewUI::SetInitialParams( |
200 dialog_controller->GetPrintPreviewForContents(web_contents()), params); | 229 dialog_controller->GetPrintPreviewForContents(web_contents()), params); |
201 } | 230 } |
202 | 231 |
203 void PrintViewManager::OnScriptedPrintPreviewReply(IPC::Message* reply_msg) { | 232 void PrintViewManager::OnScriptedPrintPreviewReply(IPC::Message* reply_msg) { |
204 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 233 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
205 Send(reply_msg); | 234 print_preview_rfh_->Send(reply_msg); |
206 } | 235 } |
207 | 236 |
208 bool PrintViewManager::OnMessageReceived(const IPC::Message& message) { | 237 bool PrintViewManager::OnMessageReceived( |
| 238 const IPC::Message& message, |
| 239 content::RenderFrameHost* render_frame_host) { |
| 240 if (message.type() == PrintHostMsg_SetupScriptedPrintPreview::ID) { |
| 241 DCHECK(!scripted_print_rfh_); |
| 242 scripted_print_rfh_ = render_frame_host; |
| 243 } |
| 244 |
209 bool handled = true; | 245 bool handled = true; |
210 IPC_BEGIN_MESSAGE_MAP(PrintViewManager, message) | 246 IPC_BEGIN_MESSAGE_MAP_WITH_PARAM(PrintViewManager, message, render_frame_host) |
211 IPC_MESSAGE_HANDLER(PrintHostMsg_DidShowPrintDialog, OnDidShowPrintDialog) | 247 IPC_MESSAGE_HANDLER(PrintHostMsg_DidShowPrintDialog, OnDidShowPrintDialog) |
212 IPC_MESSAGE_HANDLER_DELAY_REPLY(PrintHostMsg_SetupScriptedPrintPreview, | 248 IPC_MESSAGE_HANDLER_DELAY_REPLY(PrintHostMsg_SetupScriptedPrintPreview, |
213 OnSetupScriptedPrintPreview) | 249 OnSetupScriptedPrintPreview) |
214 IPC_MESSAGE_HANDLER(PrintHostMsg_ShowScriptedPrintPreview, | 250 IPC_MESSAGE_HANDLER(PrintHostMsg_ShowScriptedPrintPreview, |
215 OnShowScriptedPrintPreview) | 251 OnShowScriptedPrintPreview) |
216 IPC_MESSAGE_UNHANDLED(handled = false) | 252 IPC_MESSAGE_UNHANDLED(handled = false) |
217 IPC_END_MESSAGE_MAP() | 253 IPC_END_MESSAGE_MAP() |
218 | 254 |
219 return handled || PrintViewManagerBase::OnMessageReceived(message); | 255 return handled || |
| 256 PrintViewManagerBase::OnMessageReceived(message, render_frame_host); |
220 } | 257 } |
221 | 258 |
222 } // namespace printing | 259 } // namespace printing |
OLD | NEW |