| OLD | NEW |
| (Empty) |
| 1 // Copyright 2014 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 "extensions/browser/mojo/stash_backend.h" | |
| 6 | |
| 7 #include <stddef.h> | |
| 8 | |
| 9 #include <utility> | |
| 10 #include <vector> | |
| 11 | |
| 12 #include "base/bind.h" | |
| 13 #include "base/macros.h" | |
| 14 #include "base/memory/ptr_util.h" | |
| 15 #include "mojo/public/cpp/bindings/strong_binding.h" | |
| 16 #include "mojo/public/cpp/system/watcher.h" | |
| 17 | |
| 18 namespace extensions { | |
| 19 namespace { | |
| 20 | |
| 21 // An implementation of StashService that forwards calls to a StashBackend. | |
| 22 class StashServiceImpl : public StashService { | |
| 23 public: | |
| 24 explicit StashServiceImpl(base::WeakPtr<StashBackend> backend); | |
| 25 ~StashServiceImpl() override; | |
| 26 | |
| 27 // StashService overrides. | |
| 28 void AddToStash(mojo::Array<StashedObjectPtr> stash) override; | |
| 29 void RetrieveStash(const RetrieveStashCallback& callback) override; | |
| 30 | |
| 31 private: | |
| 32 base::WeakPtr<StashBackend> backend_; | |
| 33 | |
| 34 DISALLOW_COPY_AND_ASSIGN(StashServiceImpl); | |
| 35 }; | |
| 36 | |
| 37 StashServiceImpl::StashServiceImpl(base::WeakPtr<StashBackend> backend) | |
| 38 : backend_(backend) {} | |
| 39 | |
| 40 StashServiceImpl::~StashServiceImpl() {} | |
| 41 | |
| 42 void StashServiceImpl::AddToStash( | |
| 43 mojo::Array<StashedObjectPtr> stashed_objects) { | |
| 44 if (!backend_) | |
| 45 return; | |
| 46 backend_->AddToStash(std::move(stashed_objects)); | |
| 47 } | |
| 48 | |
| 49 void StashServiceImpl::RetrieveStash(const RetrieveStashCallback& callback) { | |
| 50 if (!backend_) { | |
| 51 callback.Run(mojo::Array<StashedObjectPtr>()); | |
| 52 return; | |
| 53 } | |
| 54 callback.Run(backend_->RetrieveStash()); | |
| 55 } | |
| 56 | |
| 57 } // namespace | |
| 58 | |
| 59 // A stash entry for a stashed object. This handles notifications if a handle | |
| 60 // within the stashed object is readable. | |
| 61 class StashBackend::StashEntry { | |
| 62 public: | |
| 63 // Construct a StashEntry for |stashed_object|. If |on_handle_readable| is | |
| 64 // non-null, it will be invoked when any handle on |stashed_object| is | |
| 65 // readable. | |
| 66 StashEntry(StashedObjectPtr stashed_object, | |
| 67 const base::Closure& on_handle_readable); | |
| 68 ~StashEntry(); | |
| 69 | |
| 70 // Returns the stashed object. | |
| 71 StashedObjectPtr Release(); | |
| 72 | |
| 73 // Cancels notifications for handles becoming readable. | |
| 74 void CancelHandleNotifications(); | |
| 75 | |
| 76 private: | |
| 77 // Invoked when a handle within |stashed_object_| is readable. | |
| 78 void OnHandleReady(MojoResult result); | |
| 79 | |
| 80 // The waiters that are waiting for handles to be readable. | |
| 81 std::vector<std::unique_ptr<mojo::Watcher>> waiters_; | |
| 82 | |
| 83 StashedObjectPtr stashed_object_; | |
| 84 | |
| 85 // If non-null, a callback to call when a handle contained within | |
| 86 // |stashed_object_| is readable. | |
| 87 const base::Closure on_handle_readable_; | |
| 88 }; | |
| 89 | |
| 90 StashBackend::StashBackend(const base::Closure& on_handle_readable) | |
| 91 : on_handle_readable_(on_handle_readable), | |
| 92 has_notified_(false), | |
| 93 weak_factory_(this) { | |
| 94 } | |
| 95 | |
| 96 StashBackend::~StashBackend() { | |
| 97 } | |
| 98 | |
| 99 void StashBackend::AddToStash(mojo::Array<StashedObjectPtr> stashed_objects) { | |
| 100 for (size_t i = 0; i < stashed_objects.size(); i++) { | |
| 101 stashed_objects_.push_back(base::MakeUnique<StashEntry>( | |
| 102 std::move(stashed_objects[i]), | |
| 103 has_notified_ ? base::Closure() | |
| 104 : base::Bind(&StashBackend::OnHandleReady, | |
| 105 weak_factory_.GetWeakPtr()))); | |
| 106 } | |
| 107 } | |
| 108 | |
| 109 mojo::Array<StashedObjectPtr> StashBackend::RetrieveStash() { | |
| 110 has_notified_ = false; | |
| 111 mojo::Array<StashedObjectPtr> result; | |
| 112 for (auto& entry : stashed_objects_) { | |
| 113 result.push_back(entry->Release()); | |
| 114 } | |
| 115 stashed_objects_.clear(); | |
| 116 return result; | |
| 117 } | |
| 118 | |
| 119 void StashBackend::BindToRequest(mojo::InterfaceRequest<StashService> request) { | |
| 120 mojo::MakeStrongBinding( | |
| 121 base::MakeUnique<StashServiceImpl>(weak_factory_.GetWeakPtr()), | |
| 122 std::move(request)); | |
| 123 } | |
| 124 | |
| 125 void StashBackend::OnHandleReady() { | |
| 126 DCHECK(!has_notified_); | |
| 127 has_notified_ = true; | |
| 128 for (auto& entry : stashed_objects_) { | |
| 129 entry->CancelHandleNotifications(); | |
| 130 } | |
| 131 if (!on_handle_readable_.is_null()) | |
| 132 on_handle_readable_.Run(); | |
| 133 } | |
| 134 | |
| 135 StashBackend::StashEntry::StashEntry(StashedObjectPtr stashed_object, | |
| 136 const base::Closure& on_handle_readable) | |
| 137 : stashed_object_(std::move(stashed_object)), | |
| 138 on_handle_readable_(on_handle_readable) { | |
| 139 if (on_handle_readable_.is_null() || !stashed_object_->monitor_handles) | |
| 140 return; | |
| 141 | |
| 142 for (size_t i = 0; i < stashed_object_->stashed_handles.size(); i++) { | |
| 143 std::unique_ptr<mojo::Watcher> watcher(new mojo::Watcher); | |
| 144 watcher->Start(stashed_object_->stashed_handles[i].get(), | |
| 145 MOJO_HANDLE_SIGNAL_READABLE, | |
| 146 base::Bind(&StashBackend::StashEntry::OnHandleReady, | |
| 147 base::Unretained(this))); | |
| 148 waiters_.push_back(std::move(watcher)); | |
| 149 } | |
| 150 } | |
| 151 | |
| 152 StashBackend::StashEntry::~StashEntry() { | |
| 153 } | |
| 154 | |
| 155 StashedObjectPtr StashBackend::StashEntry::Release() { | |
| 156 waiters_.clear(); | |
| 157 return std::move(stashed_object_); | |
| 158 } | |
| 159 | |
| 160 void StashBackend::StashEntry::CancelHandleNotifications() { | |
| 161 waiters_.clear(); | |
| 162 } | |
| 163 | |
| 164 void StashBackend::StashEntry::OnHandleReady(MojoResult result) { | |
| 165 if (result != MOJO_RESULT_OK) | |
| 166 return; | |
| 167 on_handle_readable_.Run(); | |
| 168 } | |
| 169 | |
| 170 } // namespace extensions | |
| OLD | NEW |