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_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 79 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 if (!client) { |
| 138 DCHECK(client); | |
|
Lei Zhang
2017/08/28 23:01:12
Can this be reached?
Wei Li
2017/08/30 00:24:01
This essentially becomes NOTREACHED(). Now made it
| |
| 139 return; | |
| 140 } | |
| 135 | 141 |
| 136 print_preview_ui->SetPrintPreviewDataForIndex(page_number, | 142 // Use utility process to convert skia metafile to pdf. |
| 137 std::move(data_bytes)); | 143 client->DoComposite( |
|
Lei Zhang
2017/08/28 23:01:12
How does |client| know what this is compositing vs
Wei Li
2017/08/30 00:23:59
Compositing from multiple source is not yet added.
| |
| 138 print_preview_ui->OnDidPreviewPage(page_number, params.preview_request_id); | 144 params.metafile_data_handle, params.data_size, |
| 145 base::Bind(&PrintPreviewMessageHandler::OnCompositePdfPageDone, | |
| 146 base::Unretained(this), params.page_number, | |
|
Lei Zhang
2017/08/28 23:01:12
Is it safe to use Unretained() here?
Wei Li
2017/08/30 00:23:59
PrintPreviewMessageHandler and client should have
| |
| 147 params.preview_request_id), | |
| 148 base::ThreadTaskRunnerHandle::Get()); | |
| 149 } else { | |
| 150 NotifyUIPreviewPageReady( | |
| 151 page_number, params.preview_request_id, | |
| 152 GetDataFromHandle(params.metafile_data_handle, params.data_size)); | |
| 153 } | |
| 139 } | 154 } |
| 140 | 155 |
| 141 void PrintPreviewMessageHandler::OnMetafileReadyForPrinting( | 156 void PrintPreviewMessageHandler::OnMetafileReadyForPrinting( |
| 142 const PrintHostMsg_DidPreviewDocument_Params& params) { | 157 const PrintHostMsg_DidPreviewDocument_Params& params) { |
| 143 // Always try to stop the worker. | 158 // Always try to stop the worker. |
| 144 StopWorker(params.document_cookie); | 159 StopWorker(params.document_cookie); |
| 145 | 160 |
| 146 if (params.expected_pages_count <= 0) { | 161 if (params.expected_pages_count <= 0) { |
| 147 NOTREACHED(); | 162 NOTREACHED(); |
| 148 return; | 163 return; |
| 149 } | 164 } |
| 150 | 165 |
| 151 PrintPreviewUI* print_preview_ui = GetPrintPreviewUI(); | 166 if (!GetPrintPreviewUI()) |
| 152 if (!print_preview_ui) | |
| 153 return; | 167 return; |
| 154 | 168 |
| 155 // TODO(joth): This seems like a good match for using RefCountedStaticMemory | 169 if (IsOopifEnabled()) { |
| 156 // to avoid the memory copy, but the SetPrintPreviewData call chain below | 170 auto* client = PrintCompositeClient::FromWebContents(web_contents()); |
| 157 // needs updating to accept the RefCountedMemory* base class. | 171 if (!client) { |
| 158 scoped_refptr<base::RefCountedBytes> data_bytes = | 172 DCHECK(client); |
| 159 GetDataFromHandle(params.metafile_data_handle, params.data_size); | 173 return; |
| 160 if (!data_bytes || !data_bytes->size()) | 174 } |
| 175 | |
| 176 client->DoComposite( | |
| 177 params.metafile_data_handle, params.data_size, | |
| 178 base::Bind(&PrintPreviewMessageHandler::OnCompositePdfDocumentDone, | |
| 179 base::Unretained(this), params.expected_pages_count, | |
| 180 params.preview_request_id), | |
| 181 base::ThreadTaskRunnerHandle::Get()); | |
| 182 } else { | |
| 183 NotifyUIPreviewDocumentReady( | |
| 184 params.expected_pages_count, params.preview_request_id, | |
| 185 GetDataFromHandle(params.metafile_data_handle, params.data_size)); | |
| 186 } | |
| 187 } | |
| 188 | |
| 189 void PrintPreviewMessageHandler::OnCompositePdfPageDone( | |
| 190 int page_number, | |
| 191 int request_id, | |
| 192 mojom::PdfCompositor::Status status, | |
| 193 mojo::ScopedSharedBufferHandle handle) { | |
| 194 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 195 if (status != mojom::PdfCompositor::Status::SUCCESS) { | |
| 196 NOTREACHED() << "Compositing pdf failed"; | |
| 161 return; | 197 return; |
| 198 } | |
| 199 NotifyUIPreviewPageReady( | |
| 200 page_number, request_id, | |
| 201 PrintCompositeClient::GetDataFromMojoHandle(std::move(handle))); | |
| 202 } | |
| 162 | 203 |
| 163 print_preview_ui->SetPrintPreviewDataForIndex(COMPLETE_PREVIEW_DOCUMENT_INDEX, | 204 void PrintPreviewMessageHandler::OnCompositePdfDocumentDone( |
| 164 std::move(data_bytes)); | 205 int page_count, |
| 165 print_preview_ui->OnPreviewDataIsAvailable( | 206 int request_id, |
| 166 params.expected_pages_count, params.preview_request_id); | 207 mojom::PdfCompositor::Status status, |
| 208 mojo::ScopedSharedBufferHandle handle) { | |
| 209 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 210 if (status != mojom::PdfCompositor::Status::SUCCESS) { | |
| 211 NOTREACHED() << "Compositing pdf failed"; | |
| 212 return; | |
| 213 } | |
| 214 NotifyUIPreviewDocumentReady( | |
| 215 page_count, request_id, | |
| 216 PrintCompositeClient::GetDataFromMojoHandle(std::move(handle))); | |
| 167 } | 217 } |
| 168 | 218 |
| 169 void PrintPreviewMessageHandler::OnPrintPreviewFailed(int document_cookie) { | 219 void PrintPreviewMessageHandler::OnPrintPreviewFailed(int document_cookie) { |
| 170 StopWorker(document_cookie); | 220 StopWorker(document_cookie); |
| 171 | 221 |
| 172 PrintPreviewUI* print_preview_ui = GetPrintPreviewUI(); | 222 PrintPreviewUI* print_preview_ui = GetPrintPreviewUI(); |
| 173 if (!print_preview_ui) | 223 if (!print_preview_ui) |
| 174 return; | 224 return; |
| 175 print_preview_ui->OnPrintPreviewFailed(); | 225 print_preview_ui->OnPrintPreviewFailed(); |
| 176 } | 226 } |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 207 } | 257 } |
| 208 | 258 |
| 209 void PrintPreviewMessageHandler::OnSetOptionsFromDocument( | 259 void PrintPreviewMessageHandler::OnSetOptionsFromDocument( |
| 210 const PrintHostMsg_SetOptionsFromDocument_Params& params) { | 260 const PrintHostMsg_SetOptionsFromDocument_Params& params) { |
| 211 PrintPreviewUI* print_preview_ui = GetPrintPreviewUI(); | 261 PrintPreviewUI* print_preview_ui = GetPrintPreviewUI(); |
| 212 if (!print_preview_ui) | 262 if (!print_preview_ui) |
| 213 return; | 263 return; |
| 214 print_preview_ui->OnSetOptionsFromDocument(params); | 264 print_preview_ui->OnSetOptionsFromDocument(params); |
| 215 } | 265 } |
| 216 | 266 |
| 267 void PrintPreviewMessageHandler::NotifyUIPreviewPageReady( | |
| 268 int page_number, | |
| 269 int request_id, | |
| 270 scoped_refptr<base::RefCountedBytes> data_bytes) { | |
| 271 DCHECK(data_bytes); | |
| 272 | |
| 273 PrintPreviewUI* print_preview_ui = GetPrintPreviewUI(); | |
| 274 if (!print_preview_ui) | |
| 275 return; | |
| 276 print_preview_ui->SetPrintPreviewDataForIndex(page_number, | |
| 277 std::move(data_bytes)); | |
| 278 print_preview_ui->OnDidPreviewPage(page_number, request_id); | |
| 279 } | |
| 280 | |
| 281 void PrintPreviewMessageHandler::NotifyUIPreviewDocumentReady( | |
| 282 int page_count, | |
| 283 int request_id, | |
| 284 scoped_refptr<base::RefCountedBytes> data_bytes) { | |
| 285 if (!data_bytes || !data_bytes->size()) | |
| 286 return; | |
| 287 | |
| 288 PrintPreviewUI* print_preview_ui = GetPrintPreviewUI(); | |
| 289 if (!print_preview_ui) | |
| 290 return; | |
| 291 print_preview_ui->SetPrintPreviewDataForIndex(COMPLETE_PREVIEW_DOCUMENT_INDEX, | |
| 292 std::move(data_bytes)); | |
| 293 print_preview_ui->OnPreviewDataIsAvailable(page_count, request_id); | |
| 294 } | |
| 295 | |
| 217 bool PrintPreviewMessageHandler::OnMessageReceived( | 296 bool PrintPreviewMessageHandler::OnMessageReceived( |
| 218 const IPC::Message& message, | 297 const IPC::Message& message, |
| 219 content::RenderFrameHost* render_frame_host) { | 298 content::RenderFrameHost* render_frame_host) { |
| 220 bool handled = true; | 299 bool handled = true; |
| 221 IPC_BEGIN_MESSAGE_MAP_WITH_PARAM(PrintPreviewMessageHandler, message, | 300 IPC_BEGIN_MESSAGE_MAP_WITH_PARAM(PrintPreviewMessageHandler, message, |
| 222 render_frame_host) | 301 render_frame_host) |
| 223 IPC_MESSAGE_HANDLER(PrintHostMsg_RequestPrintPreview, | 302 IPC_MESSAGE_HANDLER(PrintHostMsg_RequestPrintPreview, |
| 224 OnRequestPrintPreview) | 303 OnRequestPrintPreview) |
| 225 IPC_MESSAGE_UNHANDLED(handled = false) | 304 IPC_MESSAGE_UNHANDLED(handled = false) |
| 226 IPC_END_MESSAGE_MAP() | 305 IPC_END_MESSAGE_MAP() |
| 227 if (handled) | 306 if (handled) |
| 228 return true; | 307 return true; |
| 229 | 308 |
| 230 IPC_BEGIN_MESSAGE_MAP(PrintPreviewMessageHandler, message) | 309 IPC_BEGIN_MESSAGE_MAP(PrintPreviewMessageHandler, message) |
| 231 IPC_MESSAGE_HANDLER(PrintHostMsg_DidGetPreviewPageCount, | 310 IPC_MESSAGE_HANDLER(PrintHostMsg_DidGetPreviewPageCount, |
| 232 OnDidGetPreviewPageCount) | 311 OnDidGetPreviewPageCount) |
| 233 IPC_MESSAGE_HANDLER(PrintHostMsg_DidPreviewPage, | 312 IPC_MESSAGE_HANDLER(PrintHostMsg_DidPreviewPage, OnDidPreviewPage) |
| 234 OnDidPreviewPage) | |
| 235 IPC_MESSAGE_HANDLER(PrintHostMsg_MetafileReadyForPrinting, | 313 IPC_MESSAGE_HANDLER(PrintHostMsg_MetafileReadyForPrinting, |
| 236 OnMetafileReadyForPrinting) | 314 OnMetafileReadyForPrinting) |
| 237 IPC_MESSAGE_HANDLER(PrintHostMsg_PrintPreviewFailed, | 315 IPC_MESSAGE_HANDLER(PrintHostMsg_PrintPreviewFailed, |
| 238 OnPrintPreviewFailed) | 316 OnPrintPreviewFailed) |
| 239 IPC_MESSAGE_HANDLER(PrintHostMsg_DidGetDefaultPageLayout, | 317 IPC_MESSAGE_HANDLER(PrintHostMsg_DidGetDefaultPageLayout, |
| 240 OnDidGetDefaultPageLayout) | 318 OnDidGetDefaultPageLayout) |
| 241 IPC_MESSAGE_HANDLER(PrintHostMsg_PrintPreviewCancelled, | 319 IPC_MESSAGE_HANDLER(PrintHostMsg_PrintPreviewCancelled, |
| 242 OnPrintPreviewCancelled) | 320 OnPrintPreviewCancelled) |
| 243 IPC_MESSAGE_HANDLER(PrintHostMsg_PrintPreviewInvalidPrinterSettings, | 321 IPC_MESSAGE_HANDLER(PrintHostMsg_PrintPreviewInvalidPrinterSettings, |
| 244 OnInvalidPrinterSettings) | 322 OnInvalidPrinterSettings) |
| 245 IPC_MESSAGE_HANDLER(PrintHostMsg_SetOptionsFromDocument, | 323 IPC_MESSAGE_HANDLER(PrintHostMsg_SetOptionsFromDocument, |
| 246 OnSetOptionsFromDocument) | 324 OnSetOptionsFromDocument) |
| 247 IPC_MESSAGE_UNHANDLED(handled = false) | 325 IPC_MESSAGE_UNHANDLED(handled = false) |
| 248 IPC_END_MESSAGE_MAP() | 326 IPC_END_MESSAGE_MAP() |
| 249 return handled; | 327 return handled; |
| 250 } | 328 } |
| 251 | 329 |
| 252 } // namespace printing | 330 } // namespace printing |
| OLD | NEW |