forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Currently, the service converts one or multiple pages encapsulated in a SkMultiPictureDocument to a PDF file. It runs in a sandboxed utility process. This allows Chromium to move PDF generation code into a separate process, and eventually can support compositing content generated from multiple processes. BUG=455764 Review-Url: https://codereview.chromium.org/2832633002 Cr-Commit-Position: refs/heads/master@{#473644}
- Loading branch information
Showing
36 changed files
with
674 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# Copyright 2017 The Chromium Authors. All rights reserved. | ||
# Use of this source code is governed by a BSD-style license that can be | ||
# found in the LICENSE file. | ||
|
||
import("//services/service_manager/public/cpp/service.gni") | ||
import("//services/service_manager/public/service_manifest.gni") | ||
|
||
static_library("service") { | ||
sources = [ | ||
"pdf_compositor_impl.cc", | ||
"pdf_compositor_impl.h", | ||
"pdf_compositor_service.cc", | ||
"pdf_compositor_service.h", | ||
] | ||
|
||
deps = [ | ||
"//base", | ||
"//components/discardable_memory/client", | ||
"//components/discardable_memory/public/interfaces", | ||
"//content/public/common:service_names", | ||
"//content/public/utility", | ||
"//printing/common", | ||
"//skia", | ||
] | ||
|
||
public_deps = [ | ||
"//components/printing/service/public/interfaces", | ||
"//services/service_manager/public/cpp", | ||
] | ||
} | ||
|
||
service_manifest("pdf_compositor_manifest") { | ||
name = "pdf_compositor" | ||
source = "pdf_compositor_manifest.json" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
include_rules = [ | ||
"+components/discardable_memory/client", | ||
"+content/public/common", | ||
"+content/public/utility", | ||
"+mojo/public/cpp", | ||
"+printing/common", | ||
"+services/service_manager/public/cpp", | ||
"+skia", | ||
"+third_party/skia", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
The pdf_compositor service should composite multiple raw pictures from different | ||
frames into a complete one, then converts it into a pdf file within an isolated | ||
sandboxed process. Currently, it has no compositing functionality, just convert | ||
a set of raw pictures into a pdf file within the sandboxed process. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
// Copyright 2017 The Chromium Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#include "components/printing/service/pdf_compositor_impl.h" | ||
|
||
#include <utility> | ||
#include <vector> | ||
|
||
#include "base/logging.h" | ||
#include "base/memory/shared_memory.h" | ||
#include "base/memory/shared_memory_handle.h" | ||
#include "mojo/public/cpp/system/platform_handle.h" | ||
#include "printing/common/pdf_metafile_utils.h" | ||
#include "third_party/skia/include/core/SkCanvas.h" | ||
#include "third_party/skia/include/core/SkDocument.h" | ||
#include "third_party/skia/src/utils/SkMultiPictureDocument.h" | ||
|
||
namespace printing { | ||
|
||
PdfCompositorImpl::PdfCompositorImpl( | ||
const std::string& creator, | ||
std::unique_ptr<service_manager::ServiceContextRef> service_ref) | ||
: service_ref_(std::move(service_ref)), creator_(creator) {} | ||
|
||
PdfCompositorImpl::~PdfCompositorImpl() = default; | ||
|
||
void PdfCompositorImpl::CompositePdf( | ||
mojo::ScopedSharedBufferHandle sk_handle, | ||
mojom::PdfCompositor::CompositePdfCallback callback) { | ||
DCHECK(sk_handle.is_valid()); | ||
|
||
base::SharedMemoryHandle memory_handle; | ||
size_t memory_size = 0; | ||
bool read_only_flag = false; | ||
|
||
const MojoResult result = mojo::UnwrapSharedMemoryHandle( | ||
std::move(sk_handle), &memory_handle, &memory_size, &read_only_flag); | ||
DCHECK_EQ(MOJO_RESULT_OK, result); | ||
DCHECK_GT(memory_size, 0u); | ||
|
||
std::unique_ptr<base::SharedMemory> shm = | ||
base::MakeUnique<base::SharedMemory>(memory_handle, true); | ||
if (!shm->Map(memory_size)) { | ||
DLOG(ERROR) << "CompositePdf: Shared memory map failed."; | ||
std::move(callback).Run(mojo::ScopedSharedBufferHandle()); | ||
return; | ||
} | ||
|
||
SkMemoryStream stream(shm->memory(), memory_size); | ||
int page_count = SkMultiPictureDocumentReadPageCount(&stream); | ||
if (!page_count) { | ||
DLOG(ERROR) << "CompositePdf: No page is read."; | ||
std::move(callback).Run(mojo::ScopedSharedBufferHandle()); | ||
return; | ||
} | ||
|
||
std::vector<SkDocumentPage> pages(page_count); | ||
if (!SkMultiPictureDocumentRead(&stream, pages.data(), page_count)) { | ||
DLOG(ERROR) << "CompositePdf: Page reading failed."; | ||
std::move(callback).Run(mojo::ScopedSharedBufferHandle()); | ||
return; | ||
} | ||
|
||
SkDynamicMemoryWStream wstream; | ||
sk_sp<SkDocument> doc = MakePdfDocument(creator_, &wstream); | ||
|
||
for (const auto& page : pages) { | ||
SkCanvas* canvas = doc->beginPage(page.fSize.width(), page.fSize.height()); | ||
canvas->drawPicture(page.fPicture); | ||
doc->endPage(); | ||
} | ||
doc->close(); | ||
|
||
mojo::ScopedSharedBufferHandle buffer = | ||
mojo::SharedBufferHandle::Create(wstream.bytesWritten()); | ||
DCHECK(buffer.is_valid()); | ||
|
||
mojo::ScopedSharedBufferMapping mapping = buffer->Map(wstream.bytesWritten()); | ||
DCHECK(mapping); | ||
wstream.copyToAndReset(mapping.get()); | ||
|
||
std::move(callback).Run(std::move(buffer)); | ||
} | ||
|
||
} // namespace printing |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// Copyright 2017 The Chromium Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#ifndef COMPONENTS_PRINTING_SERVICE_PDF_COMPOSITOR_IMPL_H_ | ||
#define COMPONENTS_PRINTING_SERVICE_PDF_COMPOSITOR_IMPL_H_ | ||
|
||
#include <memory> | ||
#include <string> | ||
|
||
#include "base/macros.h" | ||
#include "components/printing/service/public/interfaces/pdf_compositor.mojom.h" | ||
#include "mojo/public/cpp/system/buffer.h" | ||
#include "services/service_manager/public/cpp/service_context_ref.h" | ||
|
||
namespace printing { | ||
|
||
class PdfCompositorImpl : public mojom::PdfCompositor { | ||
public: | ||
PdfCompositorImpl( | ||
const std::string& creator, | ||
std::unique_ptr<service_manager::ServiceContextRef> service_ref); | ||
~PdfCompositorImpl() override; | ||
|
||
void CompositePdf( | ||
mojo::ScopedSharedBufferHandle sk_handle, | ||
mojom::PdfCompositor::CompositePdfCallback callback) override; | ||
|
||
private: | ||
const std::unique_ptr<service_manager::ServiceContextRef> service_ref_; | ||
const std::string creator_; | ||
|
||
DISALLOW_COPY_AND_ASSIGN(PdfCompositorImpl); | ||
}; | ||
|
||
} // namespace printing | ||
|
||
#endif // COMPONENTS_PRINTING_SERVICE_PDF_COMPOSITOR_IMPL_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"name": "pdf_compositor", | ||
"display_name": "PDF Compositor Service", | ||
"interface_provider_specs": { | ||
"service_manager:connector": { | ||
"provides": { | ||
"composite": [ "printing::mojom::PdfCompositor" ] | ||
}, | ||
"requires": { | ||
"service_manager": [ "service_manager:all_users" ], | ||
"content_browser": [ "utility" ] | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// Copyright 2017 The Chromium Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#include "components/printing/service/pdf_compositor_service.h" | ||
|
||
#include <utility> | ||
|
||
#include "base/lazy_instance.h" | ||
#include "base/memory/discardable_memory.h" | ||
#include "base/memory/ptr_util.h" | ||
#include "components/printing/service/pdf_compositor_impl.h" | ||
#include "components/printing/service/public/interfaces/pdf_compositor.mojom.h" | ||
#include "content/public/common/service_names.mojom.h" | ||
#include "content/public/utility/utility_thread.h" | ||
#include "mojo/public/cpp/bindings/strong_binding.h" | ||
#include "services/service_manager/public/cpp/connector.h" | ||
#include "services/service_manager/public/cpp/service_context.h" | ||
|
||
namespace { | ||
|
||
void OnPdfCompositorRequest( | ||
const std::string& creator, | ||
service_manager::ServiceContextRefFactory* ref_factory, | ||
const service_manager::BindSourceInfo& source_info, | ||
printing::mojom::PdfCompositorRequest request) { | ||
mojo::MakeStrongBinding(base::MakeUnique<printing::PdfCompositorImpl>( | ||
creator, ref_factory->CreateRef()), | ||
std::move(request)); | ||
} | ||
} // namespace | ||
|
||
namespace printing { | ||
|
||
PdfCompositorService::PdfCompositorService(const std::string& creator) | ||
: creator_(creator.empty() ? "Chromium" : creator), weak_factory_(this) {} | ||
|
||
PdfCompositorService::~PdfCompositorService() = default; | ||
|
||
// static | ||
std::unique_ptr<service_manager::Service> PdfCompositorService::Create( | ||
const std::string& creator) { | ||
return base::MakeUnique<printing::PdfCompositorService>(creator); | ||
} | ||
|
||
void PdfCompositorService::OnStart() { | ||
// Set up discardable memory manager. | ||
discardable_memory::mojom::DiscardableSharedMemoryManagerPtr manager_ptr; | ||
context()->connector()->BindInterface(content::mojom::kBrowserServiceName, | ||
&manager_ptr); | ||
discardable_shared_memory_manager_ = base::MakeUnique< | ||
discardable_memory::ClientDiscardableSharedMemoryManager>( | ||
std::move(manager_ptr), content::UtilityThread::Get()->GetIOTaskRunner()); | ||
DCHECK(discardable_shared_memory_manager_); | ||
base::DiscardableMemoryAllocator::SetInstance( | ||
discardable_shared_memory_manager_.get()); | ||
|
||
ref_factory_ = base::MakeUnique<service_manager::ServiceContextRefFactory>( | ||
base::Bind(&service_manager::ServiceContext::RequestQuit, | ||
base::Unretained(context()))); | ||
registry_.AddInterface( | ||
base::Bind(&OnPdfCompositorRequest, creator_, ref_factory_.get())); | ||
} | ||
|
||
void PdfCompositorService::OnBindInterface( | ||
const service_manager::BindSourceInfo& source_info, | ||
const std::string& interface_name, | ||
mojo::ScopedMessagePipeHandle interface_pipe) { | ||
registry_.BindInterface(source_info, interface_name, | ||
std::move(interface_pipe)); | ||
} | ||
|
||
} // namespace printing |
Oops, something went wrong.