OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "blimp/engine/mojo/blob_channel_service.h" | |
6 | |
7 #include <string> | |
8 #include <unordered_map> | |
9 #include <utility> | |
10 #include <vector> | |
11 | |
12 #include "base/memory/ptr_util.h" | |
13 #include "base/task_runner_util.h" | |
14 #include "base/threading/thread.h" | |
15 #include "blimp/net/blob_channel/blob_channel_sender.h" | |
16 #include "mojo/public/cpp/bindings/message.h" | |
17 #include "mojo/public/cpp/system/buffer.h" | |
18 | |
19 namespace blimp { | |
20 namespace engine { | |
21 namespace { | |
22 | |
23 std::vector<BlobChannelSender::CacheStateEntry> | |
24 GetCachedBlobIdsFromWeakPtr(base::WeakPtr<BlobChannelSender> sender) { | |
25 if (sender) { | |
26 return sender->GetCachedBlobIds(); | |
27 } else { | |
28 return std::vector<BlobChannelSender::CacheStateEntry>(); | |
29 } | |
30 } | |
31 | |
32 } // namespace | |
33 | |
34 BlobChannelService::BlobChannelService( | |
35 base::WeakPtr<BlobChannelSender> blob_channel_sender, | |
36 scoped_refptr<base::SingleThreadTaskRunner> blob_sender_task_runner) | |
37 : blob_channel_sender_(blob_channel_sender), | |
38 blob_sender_task_runner_(blob_sender_task_runner), | |
39 weak_factory_(this) { | |
40 DCHECK(blob_sender_task_runner_.get()); | |
41 } | |
42 | |
43 BlobChannelService::~BlobChannelService() { | |
44 DCHECK(thread_checker_.CalledOnValidThread()); | |
45 } | |
46 | |
47 void BlobChannelService::GetCachedBlobIds( | |
48 const BlobChannelService::GetCachedBlobIdsCallback& response_callback) { | |
49 DCHECK(thread_checker_.CalledOnValidThread()); | |
50 VLOG(1) << "BlobChannel::GetCachedBlobIds called."; | |
51 | |
52 // Pull the list of blob IDs from the UI thread. | |
53 base::PostTaskAndReplyWithResult( | |
54 blob_sender_task_runner_.get(), FROM_HERE, | |
55 base::Bind(&GetCachedBlobIdsFromWeakPtr, blob_channel_sender_), | |
56 base::Bind(&BlobChannelService::OnGetCachedBlobsCompleted, | |
57 weak_factory_.GetWeakPtr(), response_callback)); | |
58 } | |
59 | |
60 void BlobChannelService::OnGetCachedBlobsCompleted( | |
61 const BlobChannelService::GetCachedBlobIdsCallback& response_callback, | |
62 const std::vector<BlobChannelSender::CacheStateEntry>& ids) { | |
63 std::unordered_map<std::string, bool> cache_state; | |
64 for (const auto& next_entry : ids) { | |
65 cache_state[next_entry.id] = next_entry.was_delivered; | |
66 } | |
67 response_callback.Run(std::move(cache_state)); | |
68 } | |
69 | |
70 void BlobChannelService::PutBlob(const std::string& id, | |
71 mojo::ScopedSharedBufferHandle data, | |
72 uint32_t size) { | |
73 DCHECK(thread_checker_.CalledOnValidThread()); | |
74 | |
75 // Map |data| into the address space and copy out its contents. | |
76 if (!data.is_valid()) { | |
77 mojo::ReportBadMessage( | |
78 "Invalid data handle received from renderer process."); | |
79 return; | |
80 } | |
81 | |
82 if (size > kMaxBlobSizeBytes) { | |
83 mojo::ReportBadMessage("Blob size too large."); | |
84 return; | |
85 } | |
86 | |
87 mojo::ScopedSharedBufferMapping mapping = data->Map(size); | |
88 CHECK(mapping) << "Failed to mmap region of " << size << " bytes."; | |
89 | |
90 scoped_refptr<BlobData> new_blob(new BlobData); | |
91 new_blob->data.assign(reinterpret_cast<const char*>(mapping.get()), size); | |
92 | |
93 blob_sender_task_runner_->PostTask(FROM_HERE, | |
94 base::Bind(&BlobChannelSender::PutBlob, | |
95 blob_channel_sender_, | |
96 id, | |
97 base::Passed(std::move(new_blob)))); | |
98 } | |
99 | |
100 void BlobChannelService::DeliverBlob(const std::string& id) { | |
101 DCHECK(thread_checker_.CalledOnValidThread()); | |
102 | |
103 blob_sender_task_runner_->PostTask(FROM_HERE, | |
104 base::Bind(&BlobChannelSender::DeliverBlob, | |
105 blob_channel_sender_, | |
106 id)); | |
107 } | |
108 | |
109 void BlobChannelService::BindRequest( | |
110 mojo::InterfaceRequest<mojom::BlobChannel> request) { | |
111 DCHECK(thread_checker_.CalledOnValidThread()); | |
112 | |
113 bindings_.AddBinding(this, std::move(request)); | |
114 } | |
115 | |
116 } // namespace engine | |
117 } // namespace blimp | |
OLD | NEW |