| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 "extensions/browser/mojo/stash_backend.h" | 5 #include "extensions/browser/mojo/stash_backend.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <utility> | 9 #include <utility> |
| 10 #include <vector> | 10 #include <vector> |
| 11 | 11 |
| 12 #include "base/bind.h" | 12 #include "base/bind.h" |
| 13 #include "base/macros.h" | 13 #include "base/macros.h" |
| 14 #include "base/memory/ptr_util.h" |
| 14 #include "mojo/message_pump/handle_watcher.h" | 15 #include "mojo/message_pump/handle_watcher.h" |
| 15 #include "mojo/public/cpp/bindings/strong_binding.h" | 16 #include "mojo/public/cpp/bindings/strong_binding.h" |
| 16 | 17 |
| 17 namespace extensions { | 18 namespace extensions { |
| 18 namespace { | 19 namespace { |
| 19 | 20 |
| 20 // An implementation of StashService that forwards calls to a StashBackend. | 21 // An implementation of StashService that forwards calls to a StashBackend. |
| 21 class StashServiceImpl : public StashService { | 22 class StashServiceImpl : public StashService { |
| 22 public: | 23 public: |
| 23 StashServiceImpl(mojo::InterfaceRequest<StashService> request, | 24 StashServiceImpl(mojo::InterfaceRequest<StashService> request, |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 77 StashedObjectPtr Release(); | 78 StashedObjectPtr Release(); |
| 78 | 79 |
| 79 // Cancels notifications for handles becoming readable. | 80 // Cancels notifications for handles becoming readable. |
| 80 void CancelHandleNotifications(); | 81 void CancelHandleNotifications(); |
| 81 | 82 |
| 82 private: | 83 private: |
| 83 // Invoked when a handle within |stashed_object_| is readable. | 84 // Invoked when a handle within |stashed_object_| is readable. |
| 84 void OnHandleReady(MojoResult result); | 85 void OnHandleReady(MojoResult result); |
| 85 | 86 |
| 86 // The waiters that are waiting for handles to be readable. | 87 // The waiters that are waiting for handles to be readable. |
| 87 std::vector<scoped_ptr<mojo::common::HandleWatcher>> waiters_; | 88 std::vector<std::unique_ptr<mojo::common::HandleWatcher>> waiters_; |
| 88 | 89 |
| 89 StashedObjectPtr stashed_object_; | 90 StashedObjectPtr stashed_object_; |
| 90 | 91 |
| 91 // If non-null, a callback to call when a handle contained within | 92 // If non-null, a callback to call when a handle contained within |
| 92 // |stashed_object_| is readable. | 93 // |stashed_object_| is readable. |
| 93 const base::Closure on_handle_readable_; | 94 const base::Closure on_handle_readable_; |
| 94 }; | 95 }; |
| 95 | 96 |
| 96 StashBackend::StashBackend(const base::Closure& on_handle_readable) | 97 StashBackend::StashBackend(const base::Closure& on_handle_readable) |
| 97 : on_handle_readable_(on_handle_readable), | 98 : on_handle_readable_(on_handle_readable), |
| 98 has_notified_(false), | 99 has_notified_(false), |
| 99 weak_factory_(this) { | 100 weak_factory_(this) { |
| 100 } | 101 } |
| 101 | 102 |
| 102 StashBackend::~StashBackend() { | 103 StashBackend::~StashBackend() { |
| 103 } | 104 } |
| 104 | 105 |
| 105 void StashBackend::AddToStash(mojo::Array<StashedObjectPtr> stashed_objects) { | 106 void StashBackend::AddToStash(mojo::Array<StashedObjectPtr> stashed_objects) { |
| 106 for (size_t i = 0; i < stashed_objects.size(); i++) { | 107 for (size_t i = 0; i < stashed_objects.size(); i++) { |
| 107 stashed_objects_.push_back(make_scoped_ptr(new StashEntry( | 108 stashed_objects_.push_back(base::WrapUnique(new StashEntry( |
| 108 std::move(stashed_objects[i]), | 109 std::move(stashed_objects[i]), |
| 109 has_notified_ ? base::Closure() | 110 has_notified_ ? base::Closure() |
| 110 : base::Bind(&StashBackend::OnHandleReady, | 111 : base::Bind(&StashBackend::OnHandleReady, |
| 111 weak_factory_.GetWeakPtr())))); | 112 weak_factory_.GetWeakPtr())))); |
| 112 } | 113 } |
| 113 } | 114 } |
| 114 | 115 |
| 115 mojo::Array<StashedObjectPtr> StashBackend::RetrieveStash() { | 116 mojo::Array<StashedObjectPtr> StashBackend::RetrieveStash() { |
| 116 has_notified_ = false; | 117 has_notified_ = false; |
| 117 mojo::Array<StashedObjectPtr> result; | 118 mojo::Array<StashedObjectPtr> result; |
| (...skipping 19 matching lines...) Expand all Loading... |
| 137 } | 138 } |
| 138 | 139 |
| 139 StashBackend::StashEntry::StashEntry(StashedObjectPtr stashed_object, | 140 StashBackend::StashEntry::StashEntry(StashedObjectPtr stashed_object, |
| 140 const base::Closure& on_handle_readable) | 141 const base::Closure& on_handle_readable) |
| 141 : stashed_object_(std::move(stashed_object)), | 142 : stashed_object_(std::move(stashed_object)), |
| 142 on_handle_readable_(on_handle_readable) { | 143 on_handle_readable_(on_handle_readable) { |
| 143 if (on_handle_readable_.is_null() || !stashed_object_->monitor_handles) | 144 if (on_handle_readable_.is_null() || !stashed_object_->monitor_handles) |
| 144 return; | 145 return; |
| 145 | 146 |
| 146 for (size_t i = 0; i < stashed_object_->stashed_handles.size(); i++) { | 147 for (size_t i = 0; i < stashed_object_->stashed_handles.size(); i++) { |
| 147 scoped_ptr<mojo::common::HandleWatcher> watcher( | 148 std::unique_ptr<mojo::common::HandleWatcher> watcher( |
| 148 new mojo::common::HandleWatcher()); | 149 new mojo::common::HandleWatcher()); |
| 149 watcher->Start(stashed_object_->stashed_handles[i].get(), | 150 watcher->Start(stashed_object_->stashed_handles[i].get(), |
| 150 MOJO_HANDLE_SIGNAL_READABLE, MOJO_DEADLINE_INDEFINITE, | 151 MOJO_HANDLE_SIGNAL_READABLE, MOJO_DEADLINE_INDEFINITE, |
| 151 base::Bind(&StashBackend::StashEntry::OnHandleReady, | 152 base::Bind(&StashBackend::StashEntry::OnHandleReady, |
| 152 base::Unretained(this))); | 153 base::Unretained(this))); |
| 153 waiters_.push_back(std::move(watcher)); | 154 waiters_.push_back(std::move(watcher)); |
| 154 } | 155 } |
| 155 } | 156 } |
| 156 | 157 |
| 157 StashBackend::StashEntry::~StashEntry() { | 158 StashBackend::StashEntry::~StashEntry() { |
| 158 } | 159 } |
| 159 | 160 |
| 160 StashedObjectPtr StashBackend::StashEntry::Release() { | 161 StashedObjectPtr StashBackend::StashEntry::Release() { |
| 161 waiters_.clear(); | 162 waiters_.clear(); |
| 162 return std::move(stashed_object_); | 163 return std::move(stashed_object_); |
| 163 } | 164 } |
| 164 | 165 |
| 165 void StashBackend::StashEntry::CancelHandleNotifications() { | 166 void StashBackend::StashEntry::CancelHandleNotifications() { |
| 166 waiters_.clear(); | 167 waiters_.clear(); |
| 167 } | 168 } |
| 168 | 169 |
| 169 void StashBackend::StashEntry::OnHandleReady(MojoResult result) { | 170 void StashBackend::StashEntry::OnHandleReady(MojoResult result) { |
| 170 if (result != MOJO_RESULT_OK) | 171 if (result != MOJO_RESULT_OK) |
| 171 return; | 172 return; |
| 172 on_handle_readable_.Run(); | 173 on_handle_readable_.Run(); |
| 173 } | 174 } |
| 174 | 175 |
| 175 } // namespace extensions | 176 } // namespace extensions |
| OLD | NEW |