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 |
16 // Sets a finite upper limit for blob sizes, to prevent massive memory | |
17 // allocations from taking place. | |
18 const size_t kMaxBlobSizeBytes = 10 * 1024 * 1024; | |
19 | |
14 mojom::BlobChannelPtr GetConnectedBlobChannel() { | 20 mojom::BlobChannelPtr GetConnectedBlobChannel() { |
15 mojom::BlobChannelPtr blob_channel_ptr; | 21 mojom::BlobChannelPtr blob_channel_ptr; |
16 content::RenderThread::Get()->GetRemoteInterfaces()->GetInterface( | 22 content::RenderThread::Get()->GetRemoteInterfaces()->GetInterface( |
17 &blob_channel_ptr); | 23 &blob_channel_ptr); |
18 CHECK(blob_channel_ptr) << "Could not connect to BlobChannel Mojo interface."; | 24 CHECK(blob_channel_ptr) << "Could not connect to BlobChannel Mojo interface."; |
19 return blob_channel_ptr; | 25 return blob_channel_ptr; |
20 } | 26 } |
21 | 27 |
28 // Manages the creation and lifetime of Mojo shared memory buffers for blobs. | |
29 // Cleans up the shared memory state when deleted. | |
30 // The SharedMemoryBlob must not be deleted until the data consumer has | |
31 // acknowledged that it is finished using the buffer. | |
32 class SharedMemoryBlob { | |
33 public: | |
34 explicit SharedMemoryBlob(BlobDataPtr data); | |
35 ~SharedMemoryBlob(); | |
36 | |
37 mojo::ScopedSharedBufferHandle GetRemoteHandle(); | |
38 | |
39 private: | |
40 mojo::ScopedSharedBufferHandle local_handle_; | |
41 | |
42 DISALLOW_COPY_AND_ASSIGN(SharedMemoryBlob); | |
43 }; | |
44 | |
45 SharedMemoryBlob::SharedMemoryBlob(BlobDataPtr data) { | |
46 CHECK_LT(data->data.size(), kMaxBlobSizeBytes); | |
dcheng
2016/06/28 06:26:18
I think this should be a DCHECK(), with a browser-
Kevin M
2016/06/28 18:18:53
Done and done.
| |
47 | |
48 local_handle_ = mojo::SharedBufferHandle::Create(data->data.size()); | |
49 DCHECK(local_handle_.is_valid()) << "Mojo error when creating shared buffer."; | |
50 | |
51 auto mapped = local_handle_->Map(data->data.size()); | |
52 DCHECK(mapped) << "Mojo error when memory mapping shared buffer."; | |
53 memcpy(mapped.get(), data->data.data(), data->data.size()); | |
54 } | |
55 | |
56 SharedMemoryBlob::~SharedMemoryBlob() {} | |
57 | |
58 mojo::ScopedSharedBufferHandle SharedMemoryBlob::GetRemoteHandle() { | |
59 auto remote_handle = | |
60 local_handle_->Clone(mojo::SharedBufferHandle::AccessMode::READ_ONLY); | |
61 CHECK(remote_handle.is_valid()) | |
62 << "Mojo error when creating read-only buffer handle."; | |
63 return remote_handle; | |
64 } | |
65 | |
22 } // namespace | 66 } // namespace |
23 | 67 |
24 BlobChannelSenderProxy::BlobChannelSenderProxy() | 68 BlobChannelSenderProxy::BlobChannelSenderProxy() |
25 : blob_channel_(GetConnectedBlobChannel()) {} | 69 : BlobChannelSenderProxy(GetConnectedBlobChannel()) {} |
26 | 70 |
27 BlobChannelSenderProxy::~BlobChannelSenderProxy() {} | 71 BlobChannelSenderProxy::~BlobChannelSenderProxy() {} |
28 | 72 |
73 BlobChannelSenderProxy::BlobChannelSenderProxy( | |
74 mojom::BlobChannelPtr blob_channel) | |
75 : blob_channel_(std::move(blob_channel)) {} | |
76 | |
77 // static | |
78 std::unique_ptr<BlobChannelSenderProxy> BlobChannelSenderProxy::CreateForTest( | |
79 mojom::BlobChannelPtr blob_channel) { | |
80 return base::WrapUnique(new BlobChannelSenderProxy(std::move(blob_channel))); | |
81 } | |
82 | |
29 bool BlobChannelSenderProxy::IsInEngineCache(const std::string& id) const { | 83 bool BlobChannelSenderProxy::IsInEngineCache(const std::string& id) const { |
30 return replication_state_.find(id) != replication_state_.end(); | 84 return replication_state_.find(id) != replication_state_.end(); |
31 } | 85 } |
32 | 86 |
33 bool BlobChannelSenderProxy::IsInClientCache(const std::string& id) const { | 87 bool BlobChannelSenderProxy::IsInClientCache(const std::string& id) const { |
34 return replication_state_.find(id)->second; | 88 auto found = replication_state_.find(id); |
89 return found != replication_state_.end() && found->second; | |
35 } | 90 } |
36 | 91 |
37 void BlobChannelSenderProxy::PutBlob(const BlobId& id, BlobDataPtr data) { | 92 void BlobChannelSenderProxy::PutBlob(const BlobId& id, BlobDataPtr data) { |
38 DCHECK(!IsInEngineCache(id)); | 93 if (IsInEngineCache(id)) { |
94 DLOG(FATAL) << "Redundant blob put attempted: " << BlobIdToString(id); | |
95 return; | |
96 } | |
97 | |
98 size_t size = data->data.size(); | |
99 if (size == 0) { | |
100 DLOG(FATAL) << "Zero length blob sent: " << BlobIdToString(id); | |
101 return; | |
102 } | |
39 | 103 |
40 replication_state_[id] = false; | 104 replication_state_[id] = false; |
41 blob_channel_->PutBlob(id, data->data); | 105 std::unique_ptr<SharedMemoryBlob> shared_mem_blob( |
106 new SharedMemoryBlob(std::move(data))); | |
107 blob_channel_->PutBlob(id, shared_mem_blob->GetRemoteHandle(), size); | |
42 } | 108 } |
43 | 109 |
44 void BlobChannelSenderProxy::DeliverBlob(const std::string& id) { | 110 void BlobChannelSenderProxy::DeliverBlob(const std::string& id) { |
45 DCHECK(!IsInClientCache(id)); | 111 if (!IsInEngineCache(id)) { |
112 DLOG(FATAL) << "Attempted to deliver an invalid blob: " | |
113 << BlobIdToString(id); | |
114 return; | |
115 } | |
116 if (IsInClientCache(id)) { | |
117 DVLOG(1) << "Blob is already in the remote cache:" << BlobIdToString(id); | |
118 return; | |
119 } | |
46 | 120 |
47 // We assume that the client will have the blob if we push it. | 121 // We assume that the client will have the blob if we push it. |
48 // TODO(kmarshall): Revisit this assumption when asynchronous blob transport | 122 // TODO(kmarshall): Revisit this assumption when asynchronous blob transport |
49 // is supported. | 123 // is supported. |
50 replication_state_[id] = true; | 124 replication_state_[id] = true; |
51 | 125 |
52 blob_channel_->DeliverBlob(id); | 126 blob_channel_->DeliverBlob(id); |
53 } | 127 } |
54 | 128 |
55 } // namespace engine | 129 } // namespace engine |
56 } // namespace blimp | 130 } // namespace blimp |
OLD | NEW |