OLD | NEW |
---|---|
(Empty) | |
1 // 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.
| |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "components/printing/browser/print_composite_client.h" | |
6 | |
7 #include <memory> | |
8 #include <utility> | |
9 | |
10 #include "base/threading/thread_task_runner_handle.h" | |
11 #include "content/public/browser/browser_thread.h" | |
12 #include "content/public/common/service_manager_connection.h" | |
13 #include "mojo/public/cpp/system/platform_handle.h" | |
14 #include "services/service_manager/public/cpp/connector.h" | |
15 | |
16 DEFINE_WEB_CONTENTS_USER_DATA_KEY(printing::PrintCompositeClient); | |
17 | |
18 namespace printing { | |
19 | |
20 PrintCompositeClient::PrintCompositeClient(content::WebContents* web_contents) | |
21 : weak_factory_(this) {} | |
22 | |
23 PrintCompositeClient::~PrintCompositeClient() {} | |
24 | |
25 void PrintCompositeClient::CreateConnectorRequest() { | |
26 connector_ = service_manager::Connector::Create(&connector_request_); | |
27 content::ServiceManagerConnection::GetForProcess() | |
28 ->GetConnector() | |
29 ->BindConnectorRequest(std::move(connector_request_)); | |
30 } | |
31 | |
32 void PrintCompositeClient::DoComposite( | |
33 base::SharedMemoryHandle handle, | |
34 uint32_t data_size, | |
35 mojom::PdfCompositor::CompositePdfCallback callback) { | |
36 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
37 | |
38 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
| |
39 return; | |
40 | |
41 if (!connector_) | |
42 CreateConnectorRequest(); | |
43 Composite(connector_.get(), handle, data_size, std::move(callback), | |
44 base::ThreadTaskRunnerHandle::Get()); | |
45 } | |
46 | |
47 std::unique_ptr<base::SharedMemory> PrintCompositeClient::GetShmFromMojoHandle( | |
48 mojo::ScopedSharedBufferHandle handle) { | |
49 if (!handle.is_valid()) { | |
50 DLOG(ERROR) << "Invalid mojo handle."; | |
51 return nullptr; | |
52 } | |
53 | |
54 base::SharedMemoryHandle memory_handle; | |
55 size_t memory_size = 0; | |
56 bool read_only_flag = false; | |
57 | |
58 const MojoResult result = mojo::UnwrapSharedMemoryHandle( | |
59 std::move(handle), &memory_handle, &memory_size, &read_only_flag); | |
60 DCHECK_EQ(MOJO_RESULT_OK, result); | |
61 DCHECK_GT(memory_size, 0u); | |
62 | |
63 std::unique_ptr<base::SharedMemory> shm = | |
64 base::MakeUnique<base::SharedMemory>(memory_handle, true /* read_only */); | |
65 if (!shm->Map(memory_size)) { | |
66 DLOG(ERROR) << "Map shared memory failed."; | |
67 return nullptr; | |
68 } | |
69 return shm; | |
70 } | |
71 | |
72 scoped_refptr<base::RefCountedBytes> | |
73 PrintCompositeClient::GetDataFromMojoHandle( | |
74 mojo::ScopedSharedBufferHandle handle) { | |
75 std::unique_ptr<base::SharedMemory> shm = | |
76 GetShmFromMojoHandle(std::move(handle)); | |
77 if (!shm) | |
78 return nullptr; | |
79 | |
80 return base::MakeRefCounted<base::RefCountedBytes>( | |
81 reinterpret_cast<const unsigned char*>(shm->memory()), | |
82 shm->mapped_size()); | |
83 } | |
84 | |
85 } // namespace printing | |
OLD | NEW |