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 "blimp/common/blob_cache/id_util.h" | |
7 #include "content/public/common/service_registry.h" | 8 #include "content/public/common/service_registry.h" |
8 #include "content/public/renderer/render_thread.h" | 9 #include "content/public/renderer/render_thread.h" |
9 | 10 |
10 namespace blimp { | 11 namespace blimp { |
11 namespace engine { | 12 namespace engine { |
12 namespace { | 13 namespace { |
13 | 14 |
14 mojom::BlobChannelPtr GetConnectedBlobChannel() { | 15 mojom::BlobChannelPtr GetConnectedBlobChannel() { |
15 mojom::BlobChannelPtr blob_channel_ptr; | 16 mojom::BlobChannelPtr blob_channel_ptr; |
16 content::RenderThread::Get()->GetServiceRegistry()->ConnectToRemoteService( | 17 content::RenderThread::Get()->GetServiceRegistry()->ConnectToRemoteService( |
17 mojo::GetProxy(&blob_channel_ptr)); | 18 mojo::GetProxy(&blob_channel_ptr)); |
18 CHECK(blob_channel_ptr) << "Could not connect to BlobChannel Mojo service."; | 19 CHECK(blob_channel_ptr) << "Could not connect to BlobChannel Mojo service."; |
19 return blob_channel_ptr; | 20 return blob_channel_ptr; |
20 } | 21 } |
21 | 22 |
23 // Manages the creation and lifetime of Mojo shared memory buffers for blobs. | |
24 // Cleans up the shared memory state when deleted. | |
25 // Caller is responsible for ensuring that |this| is not deleted until the | |
Wez
2016/06/11 00:16:38
From the caller's PoV, |this| is itself - I think
Kevin M
2016/06/16 21:00:38
Done.
| |
26 // remote side has acknowledged that it is finished using the buffer. | |
27 class SharedMemoryBlob { | |
28 public: | |
29 explicit SharedMemoryBlob(BlobDataPtr data) { | |
Wez
2016/06/11 00:16:38
As per style-guide, don't inline methods unless th
Kevin M
2016/06/16 21:00:38
Done.
| |
30 mojo::ScopedSharedBufferHandle local_handle; | |
31 MojoResult result = | |
32 mojo::CreateSharedBuffer(NULL, data->data.size(), &local_handle); | |
Wez
2016/06/11 00:16:38
nit: nullptr
Kevin M
2016/06/16 21:00:39
Done.
| |
33 CHECK_EQ(MOJO_RESULT_OK, result) | |
34 << "Mojo error when creating shared buffer: " << result; | |
Wez
2016/06/11 00:16:38
CHECK_EQ will already log |result| (and the expect
Kevin M
2016/06/16 21:00:39
Done.
| |
35 | |
36 result = mojo::MapBuffer(local_handle.get(), 0, data->data.size(), | |
37 &mapped_data_, MOJO_MAP_BUFFER_FLAG_NONE); | |
38 CHECK_EQ(MOJO_RESULT_OK, result) | |
39 << "Mojo error when memory mapping shared buffer: " << result; | |
40 memcpy(mapped_data_, data->data.data(), data->data.size()); | |
41 | |
42 // Create read-only handle for browser-side consumption. | |
Wez
2016/06/11 00:16:37
Why not do this on-the-fly in take_remote_handle()
Kevin M
2016/06/16 21:00:38
Uh, why not, indeed! Done.
| |
43 MojoDuplicateBufferHandleOptions options{ | |
44 sizeof(MojoDuplicateBufferHandleOptions), | |
45 MOJO_DUPLICATE_BUFFER_HANDLE_OPTIONS_FLAG_READ_ONLY}; | |
46 result = | |
47 mojo::DuplicateBuffer(local_handle.get(), &options, &remote_handle_); | |
48 CHECK_EQ(MOJO_RESULT_OK, result) | |
49 << "Mojo error when creating read-only buffer handle."; | |
50 DCHECK(remote_handle_.is_valid()); | |
51 } | |
52 | |
53 ~SharedMemoryBlob() { | |
54 MojoResult result = mojo::UnmapBuffer(mapped_data_); | |
55 CHECK_EQ(MOJO_RESULT_OK, result); | |
Wez
2016/06/11 00:16:37
Looks like you can move the un-mapping into the ct
Kevin M
2016/06/16 21:00:39
Done.
| |
56 } | |
57 | |
58 mojo::ScopedSharedBufferHandle take_remote_handle() { | |
59 return std::move(remote_handle_); | |
60 } | |
61 | |
62 private: | |
63 // Pointer to shared memory buffer. | |
64 void* mapped_data_; | |
Wez
2016/06/11 00:16:37
Why do we store this? It doesn't look like anythin
Kevin M
2016/06/16 21:00:38
Done.
| |
65 | |
66 // Handle to be passed to the remote end of the BlobChannel Mojo service. | |
67 mojo::ScopedSharedBufferHandle remote_handle_; | |
68 | |
69 DISALLOW_COPY_AND_ASSIGN(SharedMemoryBlob); | |
70 }; | |
71 | |
72 void PutComplete(std::unique_ptr<SharedMemoryBlob>) { | |
73 // Allow the blob to go out of scope and be deleted. | |
74 } | |
75 | |
22 } // namespace | 76 } // namespace |
23 | 77 |
24 BlobChannelSenderProxy::BlobChannelSenderProxy() | 78 BlobChannelSenderProxy::BlobChannelSenderProxy() |
25 : blob_channel_(GetConnectedBlobChannel()) {} | 79 : BlobChannelSenderProxy(GetConnectedBlobChannel()) {} |
26 | 80 |
27 BlobChannelSenderProxy::~BlobChannelSenderProxy() {} | 81 BlobChannelSenderProxy::~BlobChannelSenderProxy() {} |
28 | 82 |
83 BlobChannelSenderProxy::BlobChannelSenderProxy( | |
84 mojom::BlobChannelPtr blob_channel) | |
85 : blob_channel_(std::move(blob_channel)) {} | |
86 | |
29 bool BlobChannelSenderProxy::IsInEngineCache(const std::string& id) const { | 87 bool BlobChannelSenderProxy::IsInEngineCache(const std::string& id) const { |
30 return replication_state_.find(id) != replication_state_.end(); | 88 return replication_state_.find(id) != replication_state_.end(); |
31 } | 89 } |
32 | 90 |
33 bool BlobChannelSenderProxy::IsInClientCache(const std::string& id) const { | 91 bool BlobChannelSenderProxy::IsInClientCache(const std::string& id) const { |
34 return replication_state_.find(id)->second; | 92 auto found = replication_state_.find(id); |
93 return found != replication_state_.end() && found->second; | |
35 } | 94 } |
36 | 95 |
37 void BlobChannelSenderProxy::PutBlob(const BlobId& id, BlobDataPtr data) { | 96 void BlobChannelSenderProxy::PutBlob(const BlobId& id, BlobDataPtr data) { |
38 DCHECK(!IsInEngineCache(id)); | 97 if (IsInEngineCache(id)) { |
98 DLOG(ERROR) << "Redundant blob put attempted: " << BlobIdToString(id); | |
Wez
2016/06/11 00:16:38
nit: If this is a coding error, consider DLOG(FATA
Kevin M
2016/06/16 21:00:38
Done.
| |
99 return; | |
100 } | |
101 | |
102 size_t size = data->data.size(); | |
103 if (size == 0) { | |
104 DLOG(FATAL) << "Zero length blob requested: " << BlobIdToString(id); | |
Wez
2016/06/11 00:16:38
nit: This is PutBlob - surely the zero-length blob
Kevin M
2016/06/16 21:00:38
Done.
| |
105 return; | |
106 } | |
39 | 107 |
40 replication_state_[id] = false; | 108 replication_state_[id] = false; |
41 blob_channel_->PutBlob(id, data->data); | 109 std::unique_ptr<SharedMemoryBlob> shared_mem_blob( |
110 new SharedMemoryBlob(std::move(data))); | |
111 blob_channel_->PutBlob( | |
112 id, shared_mem_blob->take_remote_handle(), size, | |
113 base::Bind(&PutComplete, base::Passed(std::move(shared_mem_blob)))); | |
42 } | 114 } |
43 | 115 |
44 void BlobChannelSenderProxy::DeliverBlob(const std::string& id) { | 116 void BlobChannelSenderProxy::DeliverBlob(const std::string& id) { |
45 DCHECK(!IsInClientCache(id)); | 117 if (!IsInEngineCache(id)) { |
118 DLOG(ERROR) << "Attempted to deliver an invalid blob: " | |
119 << BlobIdToString(id); | |
Wez
2016/06/11 00:16:38
This seems like a serious coding error - caller ha
Kevin M
2016/06/16 21:00:39
Done.
| |
120 return; | |
121 } | |
122 if (IsInClientCache(id)) { | |
123 DLOG(ERROR) << "Blob is already in the remote cache:" << BlobIdToString(id); | |
Wez
2016/06/11 00:16:37
nit: As noted above, is it worth having this be an
Kevin M
2016/06/16 21:00:38
Done.
| |
124 return; | |
125 } | |
46 | 126 |
47 // We assume that the client will have the blob if we push it. | 127 // We assume that the client will have the blob if we push it. |
48 // TODO(kmarshall): Revisit this assumption when asynchronous blob transport | 128 // TODO(kmarshall): Revisit this assumption when asynchronous blob transport |
49 // is supported. | 129 // is supported. |
50 replication_state_[id] = true; | 130 replication_state_[id] = true; |
51 | 131 |
52 blob_channel_->DeliverBlob(id); | 132 blob_channel_->DeliverBlob(id); |
53 } | 133 } |
54 | 134 |
55 } // namespace engine | 135 } // namespace engine |
56 } // namespace blimp | 136 } // namespace blimp |
OLD | NEW |