| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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 "components/printing/service/pdf_compositor_impl.h" | 5 #include "components/printing/service/pdf_compositor_impl.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| (...skipping 25 matching lines...) Expand all Loading... |
| 36 | 36 |
| 37 const MojoResult result = mojo::UnwrapSharedMemoryHandle( | 37 const MojoResult result = mojo::UnwrapSharedMemoryHandle( |
| 38 std::move(sk_handle), &memory_handle, &memory_size, &read_only_flag); | 38 std::move(sk_handle), &memory_handle, &memory_size, &read_only_flag); |
| 39 DCHECK_EQ(MOJO_RESULT_OK, result); | 39 DCHECK_EQ(MOJO_RESULT_OK, result); |
| 40 DCHECK_GT(memory_size, 0u); | 40 DCHECK_GT(memory_size, 0u); |
| 41 | 41 |
| 42 std::unique_ptr<base::SharedMemory> shm = | 42 std::unique_ptr<base::SharedMemory> shm = |
| 43 base::MakeUnique<base::SharedMemory>(memory_handle, true); | 43 base::MakeUnique<base::SharedMemory>(memory_handle, true); |
| 44 if (!shm->Map(memory_size)) { | 44 if (!shm->Map(memory_size)) { |
| 45 DLOG(ERROR) << "CompositePdf: Shared memory map failed."; | 45 DLOG(ERROR) << "CompositePdf: Shared memory map failed."; |
| 46 std::move(callback).Run(mojo::ScopedSharedBufferHandle()); | 46 std::move(callback).Run(mojom::PdfCompositor::Status::HANDLE_MAP_ERROR, |
| 47 mojo::ScopedSharedBufferHandle()); |
| 47 return; | 48 return; |
| 48 } | 49 } |
| 49 | 50 |
| 50 SkMemoryStream stream(shm->memory(), memory_size); | 51 SkMemoryStream stream(shm->memory(), memory_size); |
| 51 int page_count = SkMultiPictureDocumentReadPageCount(&stream); | 52 int page_count = SkMultiPictureDocumentReadPageCount(&stream); |
| 52 if (!page_count) { | 53 if (!page_count) { |
| 53 DLOG(ERROR) << "CompositePdf: No page is read."; | 54 DLOG(ERROR) << "CompositePdf: No page is read."; |
| 54 std::move(callback).Run(mojo::ScopedSharedBufferHandle()); | 55 std::move(callback).Run(mojom::PdfCompositor::Status::CONTENT_FORMAT_ERROR, |
| 56 mojo::ScopedSharedBufferHandle()); |
| 55 return; | 57 return; |
| 56 } | 58 } |
| 57 | 59 |
| 58 std::vector<SkDocumentPage> pages(page_count); | 60 std::vector<SkDocumentPage> pages(page_count); |
| 59 if (!SkMultiPictureDocumentRead(&stream, pages.data(), page_count)) { | 61 if (!SkMultiPictureDocumentRead(&stream, pages.data(), page_count)) { |
| 60 DLOG(ERROR) << "CompositePdf: Page reading failed."; | 62 DLOG(ERROR) << "CompositePdf: Page reading failed."; |
| 61 std::move(callback).Run(mojo::ScopedSharedBufferHandle()); | 63 std::move(callback).Run(mojom::PdfCompositor::Status::CONTENT_FORMAT_ERROR, |
| 64 mojo::ScopedSharedBufferHandle()); |
| 62 return; | 65 return; |
| 63 } | 66 } |
| 64 | 67 |
| 65 SkDynamicMemoryWStream wstream; | 68 SkDynamicMemoryWStream wstream; |
| 66 sk_sp<SkDocument> doc = MakePdfDocument(creator_, &wstream); | 69 sk_sp<SkDocument> doc = MakePdfDocument(creator_, &wstream); |
| 67 | 70 |
| 68 for (const auto& page : pages) { | 71 for (const auto& page : pages) { |
| 69 SkCanvas* canvas = doc->beginPage(page.fSize.width(), page.fSize.height()); | 72 SkCanvas* canvas = doc->beginPage(page.fSize.width(), page.fSize.height()); |
| 70 canvas->drawPicture(page.fPicture); | 73 canvas->drawPicture(page.fPicture); |
| 71 doc->endPage(); | 74 doc->endPage(); |
| 72 } | 75 } |
| 73 doc->close(); | 76 doc->close(); |
| 74 | 77 |
| 75 mojo::ScopedSharedBufferHandle buffer = | 78 mojo::ScopedSharedBufferHandle buffer = |
| 76 mojo::SharedBufferHandle::Create(wstream.bytesWritten()); | 79 mojo::SharedBufferHandle::Create(wstream.bytesWritten()); |
| 77 DCHECK(buffer.is_valid()); | 80 DCHECK(buffer.is_valid()); |
| 78 | 81 |
| 79 mojo::ScopedSharedBufferMapping mapping = buffer->Map(wstream.bytesWritten()); | 82 mojo::ScopedSharedBufferMapping mapping = buffer->Map(wstream.bytesWritten()); |
| 80 DCHECK(mapping); | 83 DCHECK(mapping); |
| 81 wstream.copyToAndReset(mapping.get()); | 84 wstream.copyToAndReset(mapping.get()); |
| 82 | 85 |
| 83 std::move(callback).Run(std::move(buffer)); | 86 std::move(callback).Run(mojom::PdfCompositor::Status::SUCCESS, |
| 87 std::move(buffer)); |
| 84 } | 88 } |
| 85 | 89 |
| 86 } // namespace printing | 90 } // namespace printing |
| OLD | NEW |