OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "blimp/engine/renderer/blob_channel_sender_proxy.h" | 5 #include "blimp/engine/renderer/blob_channel_sender_proxy.h" |
6 | 6 |
7 #include "content/public/common/service_registry.h" | 7 #include "content/public/common/service_registry.h" |
8 #include "content/public/renderer/render_thread.h" | 8 #include "content/public/renderer/render_thread.h" |
9 | 9 |
10 namespace blimp { | 10 namespace blimp { |
11 namespace engine { | 11 namespace engine { |
12 namespace { | 12 namespace { |
13 | 13 |
14 mojom::BlobChannelPtr GetConnectedBlobChannel() { | 14 mojom::BlobChannelPtr GetConnectedBlobChannel() { |
15 mojom::BlobChannelPtr blob_channel_ptr; | 15 mojom::BlobChannelPtr blob_channel_ptr; |
16 content::RenderThread::Get()->GetServiceRegistry()->ConnectToRemoteService( | 16 content::RenderThread::Get()->GetServiceRegistry()->ConnectToRemoteService( |
17 mojo::GetProxy(&blob_channel_ptr)); | 17 mojo::GetProxy(&blob_channel_ptr)); |
18 CHECK(blob_channel_ptr) << "Could not connect to BlobChannel Mojo service."; | 18 CHECK(blob_channel_ptr) << "Could not connect to BlobChannel Mojo service."; |
19 return blob_channel_ptr; | 19 return blob_channel_ptr; |
20 } | 20 } |
21 | 21 |
| 22 // Manages the creation and lifetime of Mojo shared memory buffers for blobs. |
| 23 // Cleans up the shared memory state when deleted. |
| 24 // Caller is responsible for ensuring that |this| is not deleted until the |
| 25 // remote side has acknowledged that it is finished using the buffer. |
| 26 class SharedMemoryBlob { |
| 27 public: |
| 28 explicit SharedMemoryBlob(BlobDataPtr data) { |
| 29 mojo::ScopedSharedBufferHandle local_handle; |
| 30 MojoResult result = |
| 31 mojo::CreateSharedBuffer(NULL, data->data.size(), &local_handle); |
| 32 CHECK_EQ(MOJO_RESULT_OK, result) |
| 33 << "Mojo error when creating shared buffer: " << result; |
| 34 |
| 35 result = mojo::MapBuffer(local_handle.get(), 0, data->data.size(), |
| 36 &mapped_data_, MOJO_MAP_BUFFER_FLAG_NONE); |
| 37 CHECK_EQ(MOJO_RESULT_OK, result) |
| 38 << "Mojo error when memory mapping shared buffer: " << result; |
| 39 memcpy(mapped_data_, data->data.data(), data->data.size()); |
| 40 |
| 41 // Create read-only handle for browser-side consumption. |
| 42 MojoDuplicateBufferHandleOptions options{ |
| 43 sizeof(MojoDuplicateBufferHandleOptions), |
| 44 MOJO_DUPLICATE_BUFFER_HANDLE_OPTIONS_FLAG_READ_ONLY}; |
| 45 result = |
| 46 mojo::DuplicateBuffer(local_handle.get(), &options, &remote_handle_); |
| 47 CHECK_EQ(MOJO_RESULT_OK, result) |
| 48 << "Mojo error when creating read-only buffer handle."; |
| 49 DCHECK(remote_handle_.is_valid()); |
| 50 } |
| 51 |
| 52 ~SharedMemoryBlob() { |
| 53 MojoResult result = mojo::UnmapBuffer(mapped_data_); |
| 54 CHECK_EQ(MOJO_RESULT_OK, result); |
| 55 } |
| 56 |
| 57 mojo::ScopedSharedBufferHandle take_remote_handle() { |
| 58 return std::move(remote_handle_); |
| 59 } |
| 60 |
| 61 private: |
| 62 // Pointer to shared memory buffer. |
| 63 void* mapped_data_; |
| 64 |
| 65 // Handle to be passed to the remote end of the BlobChannel Mojo service. |
| 66 mojo::ScopedSharedBufferHandle remote_handle_; |
| 67 |
| 68 DISALLOW_COPY_AND_ASSIGN(SharedMemoryBlob); |
| 69 }; |
| 70 |
| 71 void PutComplete(std::unique_ptr<SharedMemoryBlob>) { |
| 72 // Allow the blob to go out of scope and be deleted. |
| 73 } |
| 74 |
22 } // namespace | 75 } // namespace |
23 | 76 |
24 BlobChannelSenderProxy::BlobChannelSenderProxy() | 77 BlobChannelSenderProxy::BlobChannelSenderProxy() |
25 : blob_channel_(GetConnectedBlobChannel()) {} | 78 : blob_channel_(GetConnectedBlobChannel()) {} |
26 | 79 |
27 BlobChannelSenderProxy::~BlobChannelSenderProxy() {} | 80 BlobChannelSenderProxy::~BlobChannelSenderProxy() {} |
28 | 81 |
29 bool BlobChannelSenderProxy::IsInEngineCache(const std::string& id) const { | 82 bool BlobChannelSenderProxy::IsInEngineCache(const std::string& id) const { |
30 return replication_state_.find(id) != replication_state_.end(); | 83 return replication_state_.find(id) != replication_state_.end(); |
31 } | 84 } |
32 | 85 |
33 bool BlobChannelSenderProxy::IsInClientCache(const std::string& id) const { | 86 bool BlobChannelSenderProxy::IsInClientCache(const std::string& id) const { |
34 return replication_state_.find(id)->second; | 87 return replication_state_.find(id)->second; |
35 } | 88 } |
36 | 89 |
37 void BlobChannelSenderProxy::PutBlob(const BlobId& id, BlobDataPtr data) { | 90 void BlobChannelSenderProxy::PutBlob(const BlobId& id, BlobDataPtr data) { |
38 DCHECK(!IsInEngineCache(id)); | 91 DCHECK(!IsInEngineCache(id)); |
39 | 92 |
| 93 if (data->data.size() == 0) { |
| 94 return; |
| 95 } |
| 96 |
| 97 size_t size = data->data.size(); |
40 replication_state_[id] = false; | 98 replication_state_[id] = false; |
41 blob_channel_->PutBlob(id, data->data); | 99 std::unique_ptr<SharedMemoryBlob> shared_mem_blob( |
| 100 new SharedMemoryBlob(std::move(data))); |
| 101 blob_channel_->PutBlob( |
| 102 id, shared_mem_blob->take_remote_handle(), size, |
| 103 base::Bind(&PutComplete, base::Passed(std::move(shared_mem_blob)))); |
42 } | 104 } |
43 | 105 |
44 void BlobChannelSenderProxy::DeliverBlob(const std::string& id) { | 106 void BlobChannelSenderProxy::DeliverBlob(const std::string& id) { |
45 DCHECK(!IsInClientCache(id)); | 107 DCHECK(!IsInClientCache(id)); |
46 | 108 |
47 // We assume that the client will have the blob if we push it. | 109 // We assume that the client will have the blob if we push it. |
48 // TODO(kmarshall): Revisit this assumption when asynchronous blob transport | 110 // TODO(kmarshall): Revisit this assumption when asynchronous blob transport |
49 // is supported. | 111 // is supported. |
50 replication_state_[id] = true; | 112 replication_state_[id] = true; |
51 | 113 |
52 blob_channel_->DeliverBlob(id); | 114 blob_channel_->DeliverBlob(id); |
53 } | 115 } |
54 | 116 |
55 } // namespace engine | 117 } // namespace engine |
56 } // namespace blimp | 118 } // namespace blimp |
OLD | NEW |