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 <utility> |
| 8 |
| 9 #include "blimp/common/blob_cache/id_util.h" |
7 #include "content/public/renderer/render_thread.h" | 10 #include "content/public/renderer/render_thread.h" |
8 #include "services/shell/public/cpp/interface_provider.h" | 11 #include "services/shell/public/cpp/interface_provider.h" |
9 | 12 |
10 namespace blimp { | 13 namespace blimp { |
11 namespace engine { | 14 namespace engine { |
12 namespace { | 15 namespace { |
13 | 16 |
14 mojom::BlobChannelPtr GetConnectedBlobChannel() { | 17 mojom::BlobChannelPtr GetConnectedBlobChannel() { |
15 mojom::BlobChannelPtr blob_channel_ptr; | 18 mojom::BlobChannelPtr blob_channel_ptr; |
16 content::RenderThread::Get()->GetRemoteInterfaces()->GetInterface( | 19 content::RenderThread::Get()->GetRemoteInterfaces()->GetInterface( |
17 &blob_channel_ptr); | 20 &blob_channel_ptr); |
18 CHECK(blob_channel_ptr) << "Could not connect to BlobChannel Mojo interface."; | 21 CHECK(blob_channel_ptr) << "Could not connect to BlobChannel Mojo interface."; |
19 return blob_channel_ptr; | 22 return blob_channel_ptr; |
20 } | 23 } |
21 | 24 |
| 25 // Manages the creation and lifetime of Mojo shared memory buffers for blobs. |
| 26 class SharedMemoryBlob { |
| 27 public: |
| 28 explicit SharedMemoryBlob(BlobDataPtr data); |
| 29 ~SharedMemoryBlob(); |
| 30 |
| 31 mojo::ScopedSharedBufferHandle CreateRemoteHandle(); |
| 32 |
| 33 private: |
| 34 mojo::ScopedSharedBufferHandle local_handle_; |
| 35 |
| 36 DISALLOW_COPY_AND_ASSIGN(SharedMemoryBlob); |
| 37 }; |
| 38 |
| 39 SharedMemoryBlob::SharedMemoryBlob(BlobDataPtr data) { |
| 40 DCHECK_GE(kMaxBlobSizeBytes, data->data.size()); |
| 41 |
| 42 local_handle_ = mojo::SharedBufferHandle::Create(data->data.size()); |
| 43 DCHECK(local_handle_.is_valid()); |
| 44 |
| 45 mojo::ScopedSharedBufferMapping mapped = |
| 46 local_handle_->Map(data->data.size()); |
| 47 DCHECK(mapped); |
| 48 memcpy(mapped.get(), data->data.data(), data->data.size()); |
| 49 } |
| 50 |
| 51 SharedMemoryBlob::~SharedMemoryBlob() {} |
| 52 |
| 53 mojo::ScopedSharedBufferHandle SharedMemoryBlob::CreateRemoteHandle() { |
| 54 mojo::ScopedSharedBufferHandle remote_handle = |
| 55 local_handle_->Clone(mojo::SharedBufferHandle::AccessMode::READ_ONLY); |
| 56 CHECK(remote_handle.is_valid()) |
| 57 << "Mojo error when creating read-only buffer handle."; |
| 58 return remote_handle; |
| 59 } |
| 60 |
22 } // namespace | 61 } // namespace |
23 | 62 |
24 BlobChannelSenderProxy::BlobChannelSenderProxy() | 63 BlobChannelSenderProxy::BlobChannelSenderProxy() |
25 : blob_channel_(GetConnectedBlobChannel()) {} | 64 : BlobChannelSenderProxy(GetConnectedBlobChannel()) {} |
26 | 65 |
27 BlobChannelSenderProxy::~BlobChannelSenderProxy() {} | 66 BlobChannelSenderProxy::~BlobChannelSenderProxy() {} |
28 | 67 |
| 68 BlobChannelSenderProxy::BlobChannelSenderProxy( |
| 69 mojom::BlobChannelPtr blob_channel) |
| 70 : blob_channel_(std::move(blob_channel)) {} |
| 71 |
| 72 // static |
| 73 std::unique_ptr<BlobChannelSenderProxy> BlobChannelSenderProxy::CreateForTest( |
| 74 mojom::BlobChannelPtr blob_channel) { |
| 75 return base::WrapUnique(new BlobChannelSenderProxy(std::move(blob_channel))); |
| 76 } |
| 77 |
29 bool BlobChannelSenderProxy::IsInEngineCache(const std::string& id) const { | 78 bool BlobChannelSenderProxy::IsInEngineCache(const std::string& id) const { |
30 return replication_state_.find(id) != replication_state_.end(); | 79 return replication_state_.find(id) != replication_state_.end(); |
31 } | 80 } |
32 | 81 |
33 bool BlobChannelSenderProxy::IsInClientCache(const std::string& id) const { | 82 bool BlobChannelSenderProxy::IsInClientCache(const std::string& id) const { |
34 return replication_state_.find(id)->second; | 83 auto found = replication_state_.find(id); |
| 84 return found != replication_state_.end() && found->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(); |
| 91 CHECK(!data->data.empty()) << "Zero length blob sent: " << BlobIdToString(id); |
| 92 |
40 replication_state_[id] = false; | 93 replication_state_[id] = false; |
41 blob_channel_->PutBlob(id, data->data); | 94 std::unique_ptr<SharedMemoryBlob> shared_mem_blob( |
| 95 new SharedMemoryBlob(std::move(data))); |
| 96 blob_channel_->PutBlob(id, shared_mem_blob->CreateRemoteHandle(), size); |
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(IsInEngineCache(id)) << "Attempted to deliver an invalid blob: " |
| 101 << BlobIdToString(id); |
| 102 DCHECK(!IsInClientCache(id)) << "Blob is already in the remote cache:" |
| 103 << BlobIdToString(id); |
46 | 104 |
47 // We assume that the client will have the blob if we push it. | 105 // We assume that the client will have the blob if we push it. |
48 // TODO(kmarshall): Revisit this assumption when asynchronous blob transport | 106 // TODO(kmarshall): Revisit this assumption when asynchronous blob transport |
49 // is supported. | 107 // is supported. |
50 replication_state_[id] = true; | 108 replication_state_[id] = true; |
51 | 109 |
52 blob_channel_->DeliverBlob(id); | 110 blob_channel_->DeliverBlob(id); |
53 } | 111 } |
54 | 112 |
55 } // namespace engine | 113 } // namespace engine |
56 } // namespace blimp | 114 } // namespace blimp |
OLD | NEW |