Chromium Code Reviews| Index: components/printing/browser/print_composite_client.cc |
| diff --git a/components/printing/browser/print_composite_client.cc b/components/printing/browser/print_composite_client.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..64d21679d264bcb97f2bb3e051ca06d17117630b |
| --- /dev/null |
| +++ b/components/printing/browser/print_composite_client.cc |
| @@ -0,0 +1,87 @@ |
| +// Copyright (c) 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/browser/print_composite_client.h" |
| + |
| +#include <memory> |
| +#include <utility> |
| + |
| +#include "base/threading/thread_task_runner_handle.h" |
| +#include "content/public/browser/browser_thread.h" |
| +#include "content/public/common/service_manager_connection.h" |
| +#include "mojo/public/cpp/system/platform_handle.h" |
| +#include "services/service_manager/public/cpp/connector.h" |
| + |
| +DEFINE_WEB_CONTENTS_USER_DATA_KEY(printing::PrintCompositeClient); |
| + |
| +namespace printing { |
| + |
| +PrintCompositeClient::PrintCompositeClient(content::WebContents* web_contents) |
| + : weak_factory_(this) {} |
| + |
| +PrintCompositeClient::~PrintCompositeClient() {} |
| + |
| +void PrintCompositeClient::CreateConnectorRequest() { |
| + connector_ = service_manager::Connector::Create(&connector_request_); |
| + content::ServiceManagerConnection::GetForProcess() |
| + ->GetConnector() |
| + ->BindConnectorRequest(std::move(connector_request_)); |
| +} |
| + |
| +void PrintCompositeClient::DoComposite( |
| + base::SharedMemoryHandle handle, |
| + uint32_t data_size, |
| + mojom::PdfCompositor::CompositePdfCallback callback, |
| + scoped_refptr<base::SequencedTaskRunner> callback_task_runner) { |
| + DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| + |
| + if (!data_size) |
| + return; |
| + |
| + // if (connector_request_.is_pending()) |
| + if (!connector_) |
| + CreateConnectorRequest(); |
| + Composite(connector_.get(), handle, data_size, std::move(callback), |
| + callback_task_runner); |
| +} |
| + |
| +std::unique_ptr<base::SharedMemory> PrintCompositeClient::GetShmFromMojoHandle( |
| + mojo::ScopedSharedBufferHandle handle) { |
| + if (!handle.is_valid()) { |
| + DLOG(ERROR) << "Invalid mojo handle."; |
| + return nullptr; |
| + } |
| + |
| + base::SharedMemoryHandle memory_handle; |
| + size_t memory_size = 0; |
| + bool read_only_flag = false; |
| + |
| + const MojoResult result = mojo::UnwrapSharedMemoryHandle( |
| + std::move(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( |
| + new base::SharedMemory(memory_handle, true /* read_only */)); |
|
Lei Zhang
2017/08/28 23:01:12
MakeUnique
Wei Li
2017/08/30 00:24:02
Done.
|
| + if (!shm->Map(memory_size)) { |
| + DLOG(ERROR) << "Map shared memory failed."; |
| + return nullptr; |
| + } |
| + return shm; |
| +} |
| + |
| +scoped_refptr<base::RefCountedBytes> |
| +PrintCompositeClient::GetDataFromMojoHandle( |
| + mojo::ScopedSharedBufferHandle handle) { |
| + std::unique_ptr<base::SharedMemory> shm = |
| + GetShmFromMojoHandle(std::move(handle)); |
| + if (!shm) |
| + return nullptr; |
| + |
| + return scoped_refptr<base::RefCountedBytes>(new base::RefCountedBytes( |
|
Lei Zhang
2017/08/28 23:01:12
MakeRefCounted
Wei Li
2017/08/30 00:24:02
RefCountedBytes doesn't have similar function defi
Lei Zhang
2017/08/30 02:00:35
return base::MakeRefCounted<base::RefCountedBytes>
Wei Li
2017/08/31 21:00:25
Thanks, done.
|
| + reinterpret_cast<const unsigned char*>(shm->memory()), |
| + shm->mapped_size())); |
| +} |
| + |
| +} // namespace printing |