Chromium Code Reviews| 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(content::RenderFrameHost* rfh) { |
| 146 print_preview_state_ = NOT_PREVIEWING; | 157 if (rfh == print_preview_rfh_) |
| 147 PrintViewManagerBase::RenderProcessGone(status); | 158 print_preview_state_ = NOT_PREVIEWING; |
| 159 PrintViewManagerBase::RenderFrameDeleted(rfh); | |
| 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 if (rfh != print_preview_rfh_) |
| 152 on_print_dialog_shown_callback_.Run(); | 164 return; |
| 165 | |
| 166 if (on_print_dialog_shown_callback_.is_null()) | |
| 167 return; | |
| 168 | |
| 169 on_print_dialog_shown_callback_.Run(); | |
| 153 on_print_dialog_shown_callback_.Reset(); | 170 on_print_dialog_shown_callback_.Reset(); |
| 154 } | 171 } |
| 155 | 172 |
| 156 void PrintViewManager::OnSetupScriptedPrintPreview(IPC::Message* reply_msg) { | 173 void PrintViewManager::OnSetupScriptedPrintPreview( |
| 174 content::RenderFrameHost* rfh, | |
| 175 IPC::Message* reply_msg) { | |
| 157 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 176 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 158 auto& map = g_scripted_print_preview_closure_map.Get(); | 177 auto& map = g_scripted_print_preview_closure_map.Get(); |
| 159 content::RenderProcessHost* rph = web_contents()->GetRenderProcessHost(); | 178 content::RenderProcessHost* rph = web_contents()->GetRenderProcessHost(); |
|
nasko
2016/11/14 20:28:52
This probably should be rfh->GetProcess() as it cu
Lei Zhang
2016/11/14 23:52:48
Done. I've made a separate patch set to specifical
| |
| 160 | 179 |
| 161 if (base::ContainsKey(map, rph)) { | 180 if (base::ContainsKey(map, rph)) { |
| 162 // Renderer already handling window.print() in another View. | 181 // Renderer already handling window.print() in another View. |
| 163 Send(reply_msg); | 182 rfh->Send(reply_msg); |
| 164 return; | 183 return; |
| 165 } | 184 } |
| 166 | 185 |
| 167 if (print_preview_state_ != NOT_PREVIEWING) { | 186 if (print_preview_state_ != NOT_PREVIEWING) { |
| 168 // 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 |
| 169 // print message. | 188 // print message. |
| 170 DCHECK_EQ(USER_INITIATED_PREVIEW, print_preview_state_); | 189 DCHECK_EQ(USER_INITIATED_PREVIEW, print_preview_state_); |
| 171 Send(reply_msg); | 190 rfh->Send(reply_msg); |
| 172 return; | 191 return; |
| 173 } | 192 } |
| 174 | 193 |
| 175 PrintPreviewDialogController* dialog_controller = | 194 PrintPreviewDialogController* dialog_controller = |
| 176 PrintPreviewDialogController::GetInstance(); | 195 PrintPreviewDialogController::GetInstance(); |
| 177 if (!dialog_controller) { | 196 if (!dialog_controller) { |
| 178 Send(reply_msg); | 197 rfh->Send(reply_msg); |
| 179 return; | 198 return; |
| 180 } | 199 } |
| 181 | 200 |
| 201 DCHECK(!print_preview_rfh_); | |
| 202 print_preview_rfh_ = rfh; | |
| 182 print_preview_state_ = SCRIPTED_PREVIEW; | 203 print_preview_state_ = SCRIPTED_PREVIEW; |
| 183 map[rph] = base::Bind(&PrintViewManager::OnScriptedPrintPreviewReply, | 204 map[rph] = base::Bind(&PrintViewManager::OnScriptedPrintPreviewReply, |
| 184 base::Unretained(this), reply_msg); | 205 base::Unretained(this), reply_msg); |
| 185 scripted_print_preview_rph_ = rph; | 206 scripted_print_preview_rph_ = rph; |
| 186 } | 207 } |
| 187 | 208 |
| 188 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 | |
| 189 PrintPreviewDialogController* dialog_controller = | 215 PrintPreviewDialogController* dialog_controller = |
| 190 PrintPreviewDialogController::GetInstance(); | 216 PrintPreviewDialogController::GetInstance(); |
| 191 if (!dialog_controller) { | 217 if (!dialog_controller) { |
| 192 PrintPreviewDone(); | 218 PrintPreviewDone(); |
| 193 return; | 219 return; |
| 194 } | 220 } |
| 195 | 221 |
| 196 dialog_controller->PrintPreview(web_contents()); | 222 dialog_controller->PrintPreview(web_contents()); |
| 197 PrintHostMsg_RequestPrintPreview_Params params; | 223 PrintHostMsg_RequestPrintPreview_Params params; |
| 198 params.is_modifiable = source_is_modifiable; | 224 params.is_modifiable = source_is_modifiable; |
| 199 PrintPreviewUI::SetInitialParams( | 225 PrintPreviewUI::SetInitialParams( |
| 200 dialog_controller->GetPrintPreviewForContents(web_contents()), params); | 226 dialog_controller->GetPrintPreviewForContents(web_contents()), params); |
| 201 } | 227 } |
| 202 | 228 |
| 203 void PrintViewManager::OnScriptedPrintPreviewReply(IPC::Message* reply_msg) { | 229 void PrintViewManager::OnScriptedPrintPreviewReply(IPC::Message* reply_msg) { |
| 204 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 230 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 205 Send(reply_msg); | 231 print_preview_rfh_->Send(reply_msg); |
| 206 } | 232 } |
| 207 | 233 |
| 208 bool PrintViewManager::OnMessageReceived(const IPC::Message& message) { | 234 bool PrintViewManager::OnMessageReceived( |
| 235 const IPC::Message& message, | |
| 236 content::RenderFrameHost* render_frame_host) { | |
| 209 bool handled = true; | 237 bool handled = true; |
| 210 IPC_BEGIN_MESSAGE_MAP(PrintViewManager, message) | 238 IPC_BEGIN_MESSAGE_MAP_WITH_PARAM(PrintViewManager, message, render_frame_host) |
| 211 IPC_MESSAGE_HANDLER(PrintHostMsg_DidShowPrintDialog, OnDidShowPrintDialog) | 239 IPC_MESSAGE_HANDLER(PrintHostMsg_DidShowPrintDialog, OnDidShowPrintDialog) |
| 212 IPC_MESSAGE_HANDLER_DELAY_REPLY(PrintHostMsg_SetupScriptedPrintPreview, | 240 IPC_MESSAGE_HANDLER_WITH_PARAM_DELAY_REPLY( |
| 213 OnSetupScriptedPrintPreview) | 241 PrintHostMsg_SetupScriptedPrintPreview, OnSetupScriptedPrintPreview) |
| 214 IPC_MESSAGE_HANDLER(PrintHostMsg_ShowScriptedPrintPreview, | 242 IPC_MESSAGE_HANDLER(PrintHostMsg_ShowScriptedPrintPreview, |
| 215 OnShowScriptedPrintPreview) | 243 OnShowScriptedPrintPreview) |
| 216 IPC_MESSAGE_UNHANDLED(handled = false) | 244 IPC_MESSAGE_UNHANDLED(handled = false) |
| 217 IPC_END_MESSAGE_MAP() | 245 IPC_END_MESSAGE_MAP() |
| 218 | 246 |
| 219 return handled || PrintViewManagerBase::OnMessageReceived(message); | 247 return handled || |
| 248 PrintViewManagerBase::OnMessageReceived(message, render_frame_host); | |
| 220 } | 249 } |
| 221 | 250 |
| 222 } // namespace printing | 251 } // namespace printing |
| OLD | NEW |