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..9dcaa9c7d5dde38316ce36ac8c8bdbd0891070f2 | 
| --- /dev/null | 
| +++ b/components/printing/browser/print_composite_client.cc | 
| @@ -0,0 +1,85 @@ | 
| +// Copyright (c) 2017 The Chromium Authors. All rights reserved. | 
| 
 
Lei Zhang
2017/09/08 07:50:53
No (c)
 
Wei Li
2017/09/08 22:57:47
Done.
 
 | 
| +// 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) { | 
| + DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 
| + | 
| + if (!data_size) | 
| 
 
Lei Zhang
2017/09/08 07:50:53
Don't we have to run |callback| here and indicate
 
Wei Li
2017/09/08 22:57:47
Changed to DCHECK
 
 | 
| + return; | 
| + | 
| + if (!connector_) | 
| + CreateConnectorRequest(); | 
| + Composite(connector_.get(), handle, data_size, std::move(callback), | 
| + base::ThreadTaskRunnerHandle::Get()); | 
| +} | 
| + | 
| +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 = | 
| + base::MakeUnique<base::SharedMemory>(memory_handle, true /* read_only */); | 
| + 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 base::MakeRefCounted<base::RefCountedBytes>( | 
| + reinterpret_cast<const unsigned char*>(shm->memory()), | 
| + shm->mapped_size()); | 
| +} | 
| + | 
| +} // namespace printing |