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_preview_message_handler.h" | 5 #include "chrome/browser/printing/print_preview_message_handler.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include <memory> | 9 #include <memory> |
10 #include <utility> | 10 #include <utility> |
11 #include <vector> | 11 #include <vector> |
12 | 12 |
13 #include "base/bind.h" | 13 #include "base/bind.h" |
14 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
15 #include "base/memory/ref_counted_memory.h" | 15 #include "base/memory/ref_counted_memory.h" |
16 #include "base/memory/shared_memory.h" | 16 #include "base/memory/shared_memory.h" |
| 17 #include "base/memory/shared_memory_handle.h" |
17 #include "chrome/browser/browser_process.h" | 18 #include "chrome/browser/browser_process.h" |
18 #include "chrome/browser/printing/print_job_manager.h" | 19 #include "chrome/browser/printing/print_job_manager.h" |
19 #include "chrome/browser/printing/print_preview_dialog_controller.h" | 20 #include "chrome/browser/printing/print_preview_dialog_controller.h" |
20 #include "chrome/browser/printing/print_view_manager.h" | 21 #include "chrome/browser/printing/print_view_manager.h" |
21 #include "chrome/browser/printing/printer_query.h" | 22 #include "chrome/browser/printing/printer_query.h" |
22 #include "chrome/browser/ui/webui/print_preview/print_preview_ui.h" | 23 #include "chrome/browser/ui/webui/print_preview/print_preview_ui.h" |
| 24 #include "components/printing/browser/print_composite_client.h" |
| 25 #include "components/printing/browser/print_manager_utils.h" |
23 #include "components/printing/common/print_messages.h" | 26 #include "components/printing/common/print_messages.h" |
24 #include "content/public/browser/browser_thread.h" | 27 #include "content/public/browser/browser_thread.h" |
25 #include "content/public/browser/web_contents.h" | 28 #include "content/public/browser/web_contents.h" |
26 #include "content/public/browser/web_ui.h" | 29 #include "content/public/browser/web_ui.h" |
27 #include "printing/page_size_margins.h" | 30 #include "printing/page_size_margins.h" |
28 #include "printing/print_job_constants.h" | 31 #include "printing/print_job_constants.h" |
| 32 #include "printing/print_settings.h" |
29 | 33 |
30 using content::BrowserThread; | 34 using content::BrowserThread; |
31 using content::WebContents; | 35 using content::WebContents; |
32 | 36 |
33 DEFINE_WEB_CONTENTS_USER_DATA_KEY(printing::PrintPreviewMessageHandler); | 37 DEFINE_WEB_CONTENTS_USER_DATA_KEY(printing::PrintPreviewMessageHandler); |
34 | 38 |
35 namespace printing { | 39 namespace printing { |
36 | 40 |
37 namespace { | 41 namespace { |
38 | 42 |
(...skipping 23 matching lines...) Expand all Loading... |
62 | 66 |
63 unsigned char* data_begin = static_cast<unsigned char*>(shared_buf->memory()); | 67 unsigned char* data_begin = static_cast<unsigned char*>(shared_buf->memory()); |
64 std::vector<unsigned char> data(data_begin, data_begin + data_size); | 68 std::vector<unsigned char> data(data_begin, data_begin + data_size); |
65 return base::RefCountedBytes::TakeVector(&data); | 69 return base::RefCountedBytes::TakeVector(&data); |
66 } | 70 } |
67 | 71 |
68 } // namespace | 72 } // namespace |
69 | 73 |
70 PrintPreviewMessageHandler::PrintPreviewMessageHandler( | 74 PrintPreviewMessageHandler::PrintPreviewMessageHandler( |
71 WebContents* web_contents) | 75 WebContents* web_contents) |
72 : content::WebContentsObserver(web_contents) { | 76 : content::WebContentsObserver(web_contents), weak_ptr_factory_(this) { |
73 DCHECK(web_contents); | 77 DCHECK(web_contents); |
74 } | 78 } |
75 | 79 |
76 PrintPreviewMessageHandler::~PrintPreviewMessageHandler() { | 80 PrintPreviewMessageHandler::~PrintPreviewMessageHandler() { |
77 } | 81 } |
78 | 82 |
79 WebContents* PrintPreviewMessageHandler::GetPrintPreviewDialog() { | 83 WebContents* PrintPreviewMessageHandler::GetPrintPreviewDialog() { |
80 PrintPreviewDialogController* dialog_controller = | 84 PrintPreviewDialogController* dialog_controller = |
81 PrintPreviewDialogController::GetInstance(); | 85 PrintPreviewDialogController::GetInstance(); |
82 if (!dialog_controller) | 86 if (!dialog_controller) |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
118 | 122 |
119 print_preview_ui->OnDidGetPreviewPageCount(params); | 123 print_preview_ui->OnDidGetPreviewPageCount(params); |
120 } | 124 } |
121 | 125 |
122 void PrintPreviewMessageHandler::OnDidPreviewPage( | 126 void PrintPreviewMessageHandler::OnDidPreviewPage( |
123 const PrintHostMsg_DidPreviewPage_Params& params) { | 127 const PrintHostMsg_DidPreviewPage_Params& params) { |
124 int page_number = params.page_number; | 128 int page_number = params.page_number; |
125 if (page_number < FIRST_PAGE_INDEX || !params.data_size) | 129 if (page_number < FIRST_PAGE_INDEX || !params.data_size) |
126 return; | 130 return; |
127 | 131 |
128 PrintPreviewUI* print_preview_ui = GetPrintPreviewUI(); | 132 if (!GetPrintPreviewUI()) |
129 if (!print_preview_ui) | |
130 return; | 133 return; |
131 | 134 |
132 scoped_refptr<base::RefCountedBytes> data_bytes = | 135 if (IsOopifEnabled()) { |
133 GetDataFromHandle(params.metafile_data_handle, params.data_size); | 136 auto* client = PrintCompositeClient::FromWebContents(web_contents()); |
134 DCHECK(data_bytes); | 137 DCHECK(client); |
135 | 138 |
136 print_preview_ui->SetPrintPreviewDataForIndex(page_number, | 139 // Use utility process to convert skia metafile to pdf. |
137 std::move(data_bytes)); | 140 client->DoComposite( |
138 print_preview_ui->OnDidPreviewPage(page_number, params.preview_request_id); | 141 params.metafile_data_handle, params.data_size, |
| 142 base::BindOnce(&PrintPreviewMessageHandler::OnCompositePdfPageDone, |
| 143 weak_ptr_factory_.GetWeakPtr(), params.page_number, |
| 144 params.preview_request_id)); |
| 145 } else { |
| 146 NotifyUIPreviewPageReady( |
| 147 page_number, params.preview_request_id, |
| 148 GetDataFromHandle(params.metafile_data_handle, params.data_size)); |
| 149 } |
139 } | 150 } |
140 | 151 |
141 void PrintPreviewMessageHandler::OnMetafileReadyForPrinting( | 152 void PrintPreviewMessageHandler::OnMetafileReadyForPrinting( |
142 const PrintHostMsg_DidPreviewDocument_Params& params) { | 153 const PrintHostMsg_DidPreviewDocument_Params& params) { |
143 // Always try to stop the worker. | 154 // Always try to stop the worker. |
144 StopWorker(params.document_cookie); | 155 StopWorker(params.document_cookie); |
145 | 156 |
146 if (params.expected_pages_count <= 0) { | 157 if (params.expected_pages_count <= 0) { |
147 NOTREACHED(); | 158 NOTREACHED(); |
148 return; | 159 return; |
149 } | 160 } |
150 | 161 |
151 PrintPreviewUI* print_preview_ui = GetPrintPreviewUI(); | 162 if (!GetPrintPreviewUI()) |
152 if (!print_preview_ui) | |
153 return; | 163 return; |
154 | 164 |
155 // TODO(joth): This seems like a good match for using RefCountedStaticMemory | 165 if (IsOopifEnabled()) { |
156 // to avoid the memory copy, but the SetPrintPreviewData call chain below | 166 auto* client = PrintCompositeClient::FromWebContents(web_contents()); |
157 // needs updating to accept the RefCountedMemory* base class. | 167 DCHECK(client); |
158 scoped_refptr<base::RefCountedBytes> data_bytes = | |
159 GetDataFromHandle(params.metafile_data_handle, params.data_size); | |
160 if (!data_bytes || !data_bytes->size()) | |
161 return; | |
162 | 168 |
163 print_preview_ui->SetPrintPreviewDataForIndex(COMPLETE_PREVIEW_DOCUMENT_INDEX, | 169 client->DoComposite( |
164 std::move(data_bytes)); | 170 params.metafile_data_handle, params.data_size, |
165 print_preview_ui->OnPreviewDataIsAvailable( | 171 base::BindOnce(&PrintPreviewMessageHandler::OnCompositePdfDocumentDone, |
166 params.expected_pages_count, params.preview_request_id); | 172 weak_ptr_factory_.GetWeakPtr(), |
| 173 params.expected_pages_count, params.preview_request_id)); |
| 174 } else { |
| 175 NotifyUIPreviewDocumentReady( |
| 176 params.expected_pages_count, params.preview_request_id, |
| 177 GetDataFromHandle(params.metafile_data_handle, params.data_size)); |
| 178 } |
167 } | 179 } |
168 | 180 |
169 void PrintPreviewMessageHandler::OnPrintPreviewFailed(int document_cookie) { | 181 void PrintPreviewMessageHandler::OnPrintPreviewFailed(int document_cookie) { |
170 StopWorker(document_cookie); | 182 StopWorker(document_cookie); |
171 | 183 |
172 PrintPreviewUI* print_preview_ui = GetPrintPreviewUI(); | 184 PrintPreviewUI* print_preview_ui = GetPrintPreviewUI(); |
173 if (!print_preview_ui) | 185 if (!print_preview_ui) |
174 return; | 186 return; |
175 print_preview_ui->OnPrintPreviewFailed(); | 187 print_preview_ui->OnPrintPreviewFailed(); |
176 } | 188 } |
(...skipping 30 matching lines...) Expand all Loading... |
207 } | 219 } |
208 | 220 |
209 void PrintPreviewMessageHandler::OnSetOptionsFromDocument( | 221 void PrintPreviewMessageHandler::OnSetOptionsFromDocument( |
210 const PrintHostMsg_SetOptionsFromDocument_Params& params) { | 222 const PrintHostMsg_SetOptionsFromDocument_Params& params) { |
211 PrintPreviewUI* print_preview_ui = GetPrintPreviewUI(); | 223 PrintPreviewUI* print_preview_ui = GetPrintPreviewUI(); |
212 if (!print_preview_ui) | 224 if (!print_preview_ui) |
213 return; | 225 return; |
214 print_preview_ui->OnSetOptionsFromDocument(params); | 226 print_preview_ui->OnSetOptionsFromDocument(params); |
215 } | 227 } |
216 | 228 |
| 229 void PrintPreviewMessageHandler::NotifyUIPreviewPageReady( |
| 230 int page_number, |
| 231 int request_id, |
| 232 scoped_refptr<base::RefCountedBytes> data_bytes) { |
| 233 DCHECK(data_bytes); |
| 234 |
| 235 PrintPreviewUI* print_preview_ui = GetPrintPreviewUI(); |
| 236 if (!print_preview_ui) |
| 237 return; |
| 238 print_preview_ui->SetPrintPreviewDataForIndex(page_number, |
| 239 std::move(data_bytes)); |
| 240 print_preview_ui->OnDidPreviewPage(page_number, request_id); |
| 241 } |
| 242 |
| 243 void PrintPreviewMessageHandler::NotifyUIPreviewDocumentReady( |
| 244 int page_count, |
| 245 int request_id, |
| 246 scoped_refptr<base::RefCountedBytes> data_bytes) { |
| 247 if (!data_bytes || !data_bytes->size()) |
| 248 return; |
| 249 |
| 250 PrintPreviewUI* print_preview_ui = GetPrintPreviewUI(); |
| 251 if (!print_preview_ui) |
| 252 return; |
| 253 print_preview_ui->SetPrintPreviewDataForIndex(COMPLETE_PREVIEW_DOCUMENT_INDEX, |
| 254 std::move(data_bytes)); |
| 255 print_preview_ui->OnPreviewDataIsAvailable(page_count, request_id); |
| 256 } |
| 257 |
| 258 void PrintPreviewMessageHandler::OnCompositePdfPageDone( |
| 259 int page_number, |
| 260 int request_id, |
| 261 mojom::PdfCompositor::Status status, |
| 262 mojo::ScopedSharedBufferHandle handle) { |
| 263 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 264 if (status != mojom::PdfCompositor::Status::SUCCESS) { |
| 265 DLOG(ERROR) << "Compositing pdf failed with error " << status; |
| 266 return; |
| 267 } |
| 268 NotifyUIPreviewPageReady( |
| 269 page_number, request_id, |
| 270 PrintCompositeClient::GetDataFromMojoHandle(std::move(handle))); |
| 271 } |
| 272 |
| 273 void PrintPreviewMessageHandler::OnCompositePdfDocumentDone( |
| 274 int page_count, |
| 275 int request_id, |
| 276 mojom::PdfCompositor::Status status, |
| 277 mojo::ScopedSharedBufferHandle handle) { |
| 278 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 279 if (status != mojom::PdfCompositor::Status::SUCCESS) { |
| 280 DLOG(ERROR) << "Compositing pdf failed with error " << status; |
| 281 return; |
| 282 } |
| 283 NotifyUIPreviewDocumentReady( |
| 284 page_count, request_id, |
| 285 PrintCompositeClient::GetDataFromMojoHandle(std::move(handle))); |
| 286 } |
| 287 |
217 bool PrintPreviewMessageHandler::OnMessageReceived( | 288 bool PrintPreviewMessageHandler::OnMessageReceived( |
218 const IPC::Message& message, | 289 const IPC::Message& message, |
219 content::RenderFrameHost* render_frame_host) { | 290 content::RenderFrameHost* render_frame_host) { |
220 bool handled = true; | 291 bool handled = true; |
221 IPC_BEGIN_MESSAGE_MAP_WITH_PARAM(PrintPreviewMessageHandler, message, | 292 IPC_BEGIN_MESSAGE_MAP_WITH_PARAM(PrintPreviewMessageHandler, message, |
222 render_frame_host) | 293 render_frame_host) |
223 IPC_MESSAGE_HANDLER(PrintHostMsg_RequestPrintPreview, | 294 IPC_MESSAGE_HANDLER(PrintHostMsg_RequestPrintPreview, |
224 OnRequestPrintPreview) | 295 OnRequestPrintPreview) |
225 IPC_MESSAGE_UNHANDLED(handled = false) | 296 IPC_MESSAGE_UNHANDLED(handled = false) |
226 IPC_END_MESSAGE_MAP() | 297 IPC_END_MESSAGE_MAP() |
227 if (handled) | 298 if (handled) |
228 return true; | 299 return true; |
229 | 300 |
230 handled = true; | 301 handled = true; |
231 IPC_BEGIN_MESSAGE_MAP(PrintPreviewMessageHandler, message) | 302 IPC_BEGIN_MESSAGE_MAP(PrintPreviewMessageHandler, message) |
232 IPC_MESSAGE_HANDLER(PrintHostMsg_DidGetPreviewPageCount, | 303 IPC_MESSAGE_HANDLER(PrintHostMsg_DidGetPreviewPageCount, |
233 OnDidGetPreviewPageCount) | 304 OnDidGetPreviewPageCount) |
234 IPC_MESSAGE_HANDLER(PrintHostMsg_DidPreviewPage, | 305 IPC_MESSAGE_HANDLER(PrintHostMsg_DidPreviewPage, OnDidPreviewPage) |
235 OnDidPreviewPage) | |
236 IPC_MESSAGE_HANDLER(PrintHostMsg_MetafileReadyForPrinting, | 306 IPC_MESSAGE_HANDLER(PrintHostMsg_MetafileReadyForPrinting, |
237 OnMetafileReadyForPrinting) | 307 OnMetafileReadyForPrinting) |
238 IPC_MESSAGE_HANDLER(PrintHostMsg_PrintPreviewFailed, | 308 IPC_MESSAGE_HANDLER(PrintHostMsg_PrintPreviewFailed, |
239 OnPrintPreviewFailed) | 309 OnPrintPreviewFailed) |
240 IPC_MESSAGE_HANDLER(PrintHostMsg_DidGetDefaultPageLayout, | 310 IPC_MESSAGE_HANDLER(PrintHostMsg_DidGetDefaultPageLayout, |
241 OnDidGetDefaultPageLayout) | 311 OnDidGetDefaultPageLayout) |
242 IPC_MESSAGE_HANDLER(PrintHostMsg_PrintPreviewCancelled, | 312 IPC_MESSAGE_HANDLER(PrintHostMsg_PrintPreviewCancelled, |
243 OnPrintPreviewCancelled) | 313 OnPrintPreviewCancelled) |
244 IPC_MESSAGE_HANDLER(PrintHostMsg_PrintPreviewInvalidPrinterSettings, | 314 IPC_MESSAGE_HANDLER(PrintHostMsg_PrintPreviewInvalidPrinterSettings, |
245 OnInvalidPrinterSettings) | 315 OnInvalidPrinterSettings) |
246 IPC_MESSAGE_HANDLER(PrintHostMsg_SetOptionsFromDocument, | 316 IPC_MESSAGE_HANDLER(PrintHostMsg_SetOptionsFromDocument, |
247 OnSetOptionsFromDocument) | 317 OnSetOptionsFromDocument) |
248 IPC_MESSAGE_UNHANDLED(handled = false) | 318 IPC_MESSAGE_UNHANDLED(handled = false) |
249 IPC_END_MESSAGE_MAP() | 319 IPC_END_MESSAGE_MAP() |
250 return handled; | 320 return handled; |
251 } | 321 } |
252 | 322 |
253 } // namespace printing | 323 } // namespace printing |
OLD | NEW |