Chromium Code Reviews| 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 { |
| 18 | 20 |
| 19 BlobChannelService::BlobChannelService(BlobChannelSender* blob_channel_sender) | 21 BlobChannelService::BlobChannelService( |
| 20 : blob_channel_sender_(blob_channel_sender) { | 22 base::WeakPtr<BlobChannelSender> blob_channel_sender, |
| 21 DCHECK(blob_channel_sender_); | 23 scoped_refptr<base::SingleThreadTaskRunner> task_runner) |
| 24 : blob_channel_sender_(blob_channel_sender), task_runner_(task_runner) { | |
| 25 DCHECK(task_runner_.get()); | |
| 22 } | 26 } |
| 23 | 27 |
| 24 BlobChannelService::~BlobChannelService() {} | 28 BlobChannelService::~BlobChannelService() { |
| 29 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 30 } | |
| 25 | 31 |
| 26 void BlobChannelService::GetCachedBlobIds( | 32 void BlobChannelService::GetCachedBlobIds( |
| 27 const BlobChannelService::GetCachedBlobIdsCallback& response_callback) { | 33 const BlobChannelService::GetCachedBlobIdsCallback& response_callback) { |
| 34 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 28 VLOG(1) << "BlobChannel::GetCachedBlobIds called."; | 35 VLOG(1) << "BlobChannel::GetCachedBlobIds called."; |
| 36 | |
| 37 // Pull the list of blob IDs from the UI thread. | |
| 38 base::PostTaskAndReplyWithResult( | |
| 39 task_runner_.get(), | |
| 40 FROM_HERE, | |
| 41 base::Bind(&BlobChannelService::GetCachedBlobIdsOnUiThread, | |
| 42 base::Unretained(this)), | |
|
Wez
2016/08/19 19:15:50
What happens if you just Bind(BlobChannelSender::G
Kevin M
2016/08/19 21:43:33
There's a static assert that prevents one from Bin
Wez
2016/08/20 00:40:24
Awww, sad panda. But also yay for static asserts p
| |
| 43 base::Bind(&BlobChannelService::ReceivedBlobIds, | |
| 44 base::Unretained(this), | |
| 45 response_callback)); | |
|
Wez
2016/08/19 19:15:50
What prevents BlobChannelService being torn-down b
Kevin M
2016/08/19 21:43:33
Nothing... switched to WeakPtr and moved the UI th
| |
| 46 } | |
| 47 | |
| 48 std::vector<BlobChannelSender::CacheStateEntry> | |
| 49 BlobChannelService::GetCachedBlobIdsOnUiThread() { | |
|
Wez
2016/08/19 19:15:50
If we must have this method (i.e if Bind()ing dire
Kevin M
2016/08/19 21:43:33
Done.
| |
| 50 DCHECK(task_runner_->BelongsToCurrentThread()); | |
| 51 | |
| 52 if (blob_channel_sender_) { | |
| 53 return blob_channel_sender_->GetCachedBlobIds(); | |
| 54 } else { | |
| 55 return std::vector<BlobChannelSender::CacheStateEntry>(); | |
| 56 } | |
| 57 } | |
| 58 | |
| 59 void BlobChannelService::ReceivedBlobIds( | |
| 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) { |
|
CJ
2016/08/19 23:22:50
I'd suggest renaming |next_entry| to just entry, s
| |
| 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 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 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 |