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