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 { | |
maniscalco
2016/06/03 16:43:53
I saw your note about adding a test for the proxy
Kevin M
2016/06/03 17:14:57
Good idea.
I'll add it in this CL, I just wanted
| |
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() { mojo::UnmapBuffer(mapped_data_); } | |
maniscalco
2016/06/03 16:43:53
Check result of UnmapBuffer?
Kevin M
2016/06/03 17:14:57
Done.
| |
53 | |
54 mojo::ScopedSharedBufferHandle take_remote_handle() { | |
55 return std::move(remote_handle_); | |
56 } | |
57 | |
58 private: | |
59 // Pointer to shared memory buffer. | |
60 void* mapped_data_; | |
61 | |
62 // Handle to be passed to the remote end of the BlobChannel Mojo service. | |
63 mojo::ScopedSharedBufferHandle remote_handle_; | |
64 | |
65 DISALLOW_COPY_AND_ASSIGN(SharedMemoryBlob); | |
66 }; | |
67 | |
68 void PutComplete(std::unique_ptr<SharedMemoryBlob>) { | |
69 // Allow the blob to go out of scope and be deleted. | |
70 } | |
71 | |
22 } // namespace | 72 } // namespace |
23 | 73 |
24 BlobChannelSenderProxy::BlobChannelSenderProxy() | 74 BlobChannelSenderProxy::BlobChannelSenderProxy() |
25 : blob_channel_(GetConnectedBlobChannel()) {} | 75 : blob_channel_(GetConnectedBlobChannel()) {} |
26 | 76 |
27 BlobChannelSenderProxy::~BlobChannelSenderProxy() {} | 77 BlobChannelSenderProxy::~BlobChannelSenderProxy() {} |
28 | 78 |
29 bool BlobChannelSenderProxy::IsInEngineCache(const std::string& id) const { | 79 bool BlobChannelSenderProxy::IsInEngineCache(const std::string& id) const { |
30 return replication_state_.find(id) != replication_state_.end(); | 80 return replication_state_.find(id) != replication_state_.end(); |
31 } | 81 } |
32 | 82 |
33 bool BlobChannelSenderProxy::IsInClientCache(const std::string& id) const { | 83 bool BlobChannelSenderProxy::IsInClientCache(const std::string& id) const { |
34 return replication_state_.find(id)->second; | 84 return replication_state_.find(id)->second; |
35 } | 85 } |
36 | 86 |
37 void BlobChannelSenderProxy::PutBlob(const BlobId& id, BlobDataPtr data) { | 87 void BlobChannelSenderProxy::PutBlob(const BlobId& id, BlobDataPtr data) { |
38 DCHECK(!IsInEngineCache(id)); | 88 DCHECK(!IsInEngineCache(id)); |
39 | 89 |
90 size_t size = data->data.size(); | |
maniscalco
2016/06/03 16:43:53
I'm guessing it doesn't really make sense to const
Kevin M
2016/06/03 17:14:57
Should we prevent that? IIRC zero-length arrays ar
maniscalco
2016/06/06 15:43:56
I'm not real familiar with the applications that w
Kevin M
2016/06/09 00:08:32
You're right, thank you very much for looking into
| |
91 std::unique_ptr<SharedMemoryBlob> shared_mem_blob( | |
92 new SharedMemoryBlob(std::move(data))); | |
93 blob_channel_->PutBlob( | |
94 id, shared_mem_blob->take_remote_handle(), size, | |
95 base::Bind(&PutComplete, base::Passed(std::move(shared_mem_blob)))); | |
40 replication_state_[id] = false; | 96 replication_state_[id] = false; |
maniscalco
2016/06/03 16:43:53
Question about ordering. I noticed that the order
Kevin M
2016/06/03 17:14:57
Done.
I put it there to emphasize the postcondit
| |
41 blob_channel_->PutBlob(id, data->data); | |
42 } | 97 } |
43 | 98 |
44 void BlobChannelSenderProxy::DeliverBlob(const std::string& id) { | 99 void BlobChannelSenderProxy::DeliverBlob(const std::string& id) { |
45 DCHECK(!IsInClientCache(id)); | 100 DCHECK(!IsInClientCache(id)); |
46 | 101 |
47 // We assume that the client will have the blob if we push it. | 102 // We assume that the client will have the blob if we push it. |
48 // TODO(kmarshall): Revisit this assumption when asynchronous blob transport | 103 // TODO(kmarshall): Revisit this assumption when asynchronous blob transport |
49 // is supported. | 104 // is supported. |
50 replication_state_[id] = true; | 105 replication_state_[id] = true; |
51 | 106 |
52 blob_channel_->DeliverBlob(id); | 107 blob_channel_->DeliverBlob(id); |
53 } | 108 } |
54 | 109 |
55 } // namespace engine | 110 } // namespace engine |
56 } // namespace blimp | 111 } // namespace blimp |
OLD | NEW |