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 // The SharedMemoryBlob must not be deleted until the data consumer has | |
26 // acknowledged that it is finished using the buffer. | |
27 class SharedMemoryBlob { | |
28 public: | |
29 explicit SharedMemoryBlob(BlobDataPtr data); | |
30 ~SharedMemoryBlob(); | |
31 | |
32 mojo::ScopedSharedBufferHandle take_remote_handle(); | |
33 | |
34 private: | |
35 mojo::ScopedSharedBufferHandle local_handle_; | |
36 | |
37 DISALLOW_COPY_AND_ASSIGN(SharedMemoryBlob); | |
38 }; | |
39 | |
40 SharedMemoryBlob::SharedMemoryBlob(BlobDataPtr data) { | |
41 local_handle_ = mojo::SharedBufferHandle::Create(data->data.size()); | |
dcheng
2016/06/25 01:06:31
Any idea how likely it is to run into shared memor
Kevin M
2016/06/27 17:31:12
Added size check. Done.
| |
42 CHECK(local_handle_.is_valid()) << "Mojo error when creating shared buffer."; | |
dcheng
2016/06/25 01:06:31
IMO, DCHECK() if this is something that should "ne
Kevin M
2016/06/27 17:31:12
Done.
| |
43 | |
44 auto mapped = local_handle_->Map(data->data.size()); | |
45 CHECK(mapped) << "Mojo error when memory mapping shared buffer."; | |
46 memcpy(mapped.get(), data->data.data(), data->data.size()); | |
47 } | |
48 | |
49 SharedMemoryBlob::~SharedMemoryBlob() {} | |
50 | |
51 mojo::ScopedSharedBufferHandle SharedMemoryBlob::take_remote_handle() { | |
dcheng
2016/06/25 01:06:31
Take implies that this is destructive, but I don't
Kevin M
2016/06/27 17:31:12
Done.
| |
52 auto remote_handle = | |
53 local_handle_->Clone(mojo::SharedBufferHandle::AccessMode::READ_ONLY); | |
54 CHECK(remote_handle.is_valid()) | |
55 << "Mojo error when creating read-only buffer handle."; | |
56 return remote_handle; | |
57 } | |
58 | |
22 } // namespace | 59 } // namespace |
23 | 60 |
24 BlobChannelSenderProxy::BlobChannelSenderProxy() | 61 BlobChannelSenderProxy::BlobChannelSenderProxy() |
25 : blob_channel_(GetConnectedBlobChannel()) {} | 62 : BlobChannelSenderProxy(GetConnectedBlobChannel()) {} |
26 | 63 |
27 BlobChannelSenderProxy::~BlobChannelSenderProxy() {} | 64 BlobChannelSenderProxy::~BlobChannelSenderProxy() {} |
28 | 65 |
66 BlobChannelSenderProxy::BlobChannelSenderProxy( | |
67 mojom::BlobChannelPtr blob_channel) | |
68 : blob_channel_(std::move(blob_channel)) {} | |
69 | |
70 // static | |
71 std::unique_ptr<BlobChannelSenderProxy> BlobChannelSenderProxy::CreateForTest( | |
72 mojom::BlobChannelPtr blob_channel) { | |
73 return base::WrapUnique(new BlobChannelSenderProxy(std::move(blob_channel))); | |
dcheng
2016/06/25 01:06:31
Nit: base::MakeUnique
Kevin M
2016/06/27 17:31:12
This is a cool method, but N/A in this case. The c
| |
74 } | |
75 | |
29 bool BlobChannelSenderProxy::IsInEngineCache(const std::string& id) const { | 76 bool BlobChannelSenderProxy::IsInEngineCache(const std::string& id) const { |
30 return replication_state_.find(id) != replication_state_.end(); | 77 return replication_state_.find(id) != replication_state_.end(); |
31 } | 78 } |
32 | 79 |
33 bool BlobChannelSenderProxy::IsInClientCache(const std::string& id) const { | 80 bool BlobChannelSenderProxy::IsInClientCache(const std::string& id) const { |
34 return replication_state_.find(id)->second; | 81 auto found = replication_state_.find(id); |
82 return found != replication_state_.end() && found->second; | |
35 } | 83 } |
36 | 84 |
37 void BlobChannelSenderProxy::PutBlob(const BlobId& id, BlobDataPtr data) { | 85 void BlobChannelSenderProxy::PutBlob(const BlobId& id, BlobDataPtr data) { |
38 DCHECK(!IsInEngineCache(id)); | 86 if (IsInEngineCache(id)) { |
dcheng
2016/06/25 01:06:31
How come these (and other checks) changed from DCH
Kevin M
2016/06/27 17:31:12
So that these conditions are correctly handled by
dcheng
2016/06/28 06:26:18
Right, but violating these conditions means that t
Kevin M
2016/06/28 18:18:53
Good point... done.
| |
87 DLOG(FATAL) << "Redundant blob put attempted: " << BlobIdToString(id); | |
88 return; | |
89 } | |
90 | |
91 size_t size = data->data.size(); | |
92 if (size == 0) { | |
93 DLOG(FATAL) << "Zero length blob sent: " << BlobIdToString(id); | |
94 return; | |
95 } | |
39 | 96 |
40 replication_state_[id] = false; | 97 replication_state_[id] = false; |
41 blob_channel_->PutBlob(id, data->data); | 98 std::unique_ptr<SharedMemoryBlob> shared_mem_blob( |
99 new SharedMemoryBlob(std::move(data))); | |
100 blob_channel_->PutBlob(id, shared_mem_blob->take_remote_handle(), size); | |
42 } | 101 } |
43 | 102 |
44 void BlobChannelSenderProxy::DeliverBlob(const std::string& id) { | 103 void BlobChannelSenderProxy::DeliverBlob(const std::string& id) { |
45 DCHECK(!IsInClientCache(id)); | 104 if (!IsInEngineCache(id)) { |
105 DLOG(FATAL) << "Attempted to deliver an invalid blob: " | |
106 << BlobIdToString(id); | |
107 return; | |
108 } | |
109 if (IsInClientCache(id)) { | |
110 DVLOG(1) << "Blob is already in the remote cache:" << BlobIdToString(id); | |
111 return; | |
112 } | |
46 | 113 |
47 // We assume that the client will have the blob if we push it. | 114 // We assume that the client will have the blob if we push it. |
48 // TODO(kmarshall): Revisit this assumption when asynchronous blob transport | 115 // TODO(kmarshall): Revisit this assumption when asynchronous blob transport |
49 // is supported. | 116 // is supported. |
50 replication_state_[id] = true; | 117 replication_state_[id] = true; |
51 | 118 |
52 blob_channel_->DeliverBlob(id); | 119 blob_channel_->DeliverBlob(id); |
53 } | 120 } |
54 | 121 |
55 } // namespace engine | 122 } // namespace engine |
56 } // namespace blimp | 123 } // namespace blimp |
OLD | NEW |