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" | |
8 #include "content/public/common/service_registry.h" | |
7 #include "content/public/renderer/render_thread.h" | 9 #include "content/public/renderer/render_thread.h" |
8 #include "services/shell/public/cpp/interface_provider.h" | 10 #include "services/shell/public/cpp/interface_provider.h" |
9 | 11 |
10 namespace blimp { | 12 namespace blimp { |
11 namespace engine { | 13 namespace engine { |
12 namespace { | 14 namespace { |
13 | 15 |
14 mojom::BlobChannelPtr GetConnectedBlobChannel() { | 16 mojom::BlobChannelPtr GetConnectedBlobChannel() { |
15 mojom::BlobChannelPtr blob_channel_ptr; | 17 mojom::BlobChannelPtr blob_channel_ptr; |
16 content::RenderThread::Get()->GetRemoteInterfaces()->GetInterface( | 18 content::RenderThread::Get()->GetRemoteInterfaces()->GetInterface( |
17 &blob_channel_ptr); | 19 &blob_channel_ptr); |
18 CHECK(blob_channel_ptr) << "Could not connect to BlobChannel Mojo interface."; | 20 CHECK(blob_channel_ptr) << "Could not connect to BlobChannel Mojo interface."; |
19 return blob_channel_ptr; | 21 return blob_channel_ptr; |
20 } | 22 } |
21 | 23 |
24 // Manages the creation and lifetime of Mojo shared memory buffers for blobs. | |
25 // Cleans up the shared memory state when deleted. | |
26 // The SharedMemoryBlob must not be deleted until the data consumer has | |
27 // acknowledged that it is finished using the buffer. | |
Wez
2016/07/01 01:06:56
Why not? Isn't it the case that the buffer will pe
Kevin M
2016/07/18 16:35:16
Yes. I should've removed this comment; I updated t
| |
28 class SharedMemoryBlob { | |
29 public: | |
30 explicit SharedMemoryBlob(BlobDataPtr data); | |
31 ~SharedMemoryBlob(); | |
32 | |
33 mojo::ScopedSharedBufferHandle GetRemoteHandle(); | |
Wez
2016/07/01 01:06:57
nit: Add comments to explain why we e.g. have a lo
Kevin M
2016/07/18 16:35:16
Create is better, done.
| |
34 | |
35 private: | |
36 mojo::ScopedSharedBufferHandle local_handle_; | |
37 | |
38 DISALLOW_COPY_AND_ASSIGN(SharedMemoryBlob); | |
39 }; | |
40 | |
41 SharedMemoryBlob::SharedMemoryBlob(BlobDataPtr data) { | |
42 DCHECK_LT(data->data.size(), kMaxBlobSizeBytes); | |
Wez
2016/07/01 01:06:57
nit: We tend to try to keep these (expectation, ac
Kevin M
2016/07/18 16:35:16
Done.
| |
43 | |
44 local_handle_ = mojo::SharedBufferHandle::Create(data->data.size()); | |
45 DCHECK(local_handle_.is_valid()) << "Mojo error when creating shared buffer."; | |
Wez
2016/07/01 01:06:56
nit: Here and below, does the message text help? I
Kevin M
2016/07/18 16:35:16
Done.
| |
46 | |
47 auto mapped = local_handle_->Map(data->data.size()); | |
48 DCHECK(mapped) << "Mojo error when memory mapping shared buffer."; | |
49 memcpy(mapped.get(), data->data.data(), data->data.size()); | |
50 } | |
51 | |
52 SharedMemoryBlob::~SharedMemoryBlob() {} | |
53 | |
54 mojo::ScopedSharedBufferHandle SharedMemoryBlob::GetRemoteHandle() { | |
55 auto remote_handle = | |
Wez
2016/07/01 01:06:57
As noted elsewhere, it's best not to use auto unle
Kevin M
2016/07/18 16:35:16
Done.
| |
56 local_handle_->Clone(mojo::SharedBufferHandle::AccessMode::READ_ONLY); | |
57 CHECK(remote_handle.is_valid()) | |
58 << "Mojo error when creating read-only buffer handle."; | |
59 return remote_handle; | |
60 } | |
61 | |
22 } // namespace | 62 } // namespace |
23 | 63 |
24 BlobChannelSenderProxy::BlobChannelSenderProxy() | 64 BlobChannelSenderProxy::BlobChannelSenderProxy() |
25 : blob_channel_(GetConnectedBlobChannel()) {} | 65 : BlobChannelSenderProxy(GetConnectedBlobChannel()) {} |
26 | 66 |
27 BlobChannelSenderProxy::~BlobChannelSenderProxy() {} | 67 BlobChannelSenderProxy::~BlobChannelSenderProxy() {} |
28 | 68 |
69 BlobChannelSenderProxy::BlobChannelSenderProxy( | |
70 mojom::BlobChannelPtr blob_channel) | |
71 : blob_channel_(std::move(blob_channel)) {} | |
72 | |
73 // static | |
74 std::unique_ptr<BlobChannelSenderProxy> BlobChannelSenderProxy::CreateForTest( | |
75 mojom::BlobChannelPtr blob_channel) { | |
76 return base::WrapUnique(new BlobChannelSenderProxy(std::move(blob_channel))); | |
77 } | |
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 auto found = replication_state_.find(id); |
85 return found != replication_state_.end() && found->second; | |
35 } | 86 } |
36 | 87 |
37 void BlobChannelSenderProxy::PutBlob(const BlobId& id, BlobDataPtr data) { | 88 void BlobChannelSenderProxy::PutBlob(const BlobId& id, BlobDataPtr data) { |
38 DCHECK(!IsInEngineCache(id)); | 89 DCHECK(!IsInEngineCache(id)); |
39 | 90 |
91 size_t size = data->data.size(); | |
92 if (size == 0) { | |
Wez
2016/07/01 01:06:57
nit: It would be more readable to if(data->data.em
Kevin M
2016/07/18 16:35:16
Made this a CHECK and used empty()
| |
93 DLOG(FATAL) << "Zero length blob sent: " << BlobIdToString(id); | |
94 return; | |
95 } | |
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->GetRemoteHandle(), 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); | |
Wez
2016/07/01 01:06:57
Does this indicate a renderer misbehaviour?
Kevin M
2016/07/18 16:35:16
Yes, though since it's probably a deterministic co
| |
107 return; | |
108 } | |
109 if (IsInClientCache(id)) { | |
110 DVLOG(1) << "Blob is already in the remote cache:" << BlobIdToString(id); | |
Wez
2016/07/01 01:06:57
Similarly, why would we be asked to re-send?
dcheng
2016/07/01 02:04:28
I think these should probably be DCHECKs as well,
Kevin M
2016/07/18 16:35:16
Done.
Kevin M
2016/07/18 16:35:16
Done.
| |
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 |