Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(90)

Side by Side Diff: extensions/browser/mojo/stash_backend.cc

Issue 2326913003: Privatize StrongBinding lifetime management (Closed)
Patch Set: rebase Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 "base/memory/ptr_util.h"
15 #include "mojo/public/cpp/bindings/strong_binding.h" 15 #include "mojo/public/cpp/bindings/strong_binding.h"
16 #include "mojo/public/cpp/system/watcher.h" 16 #include "mojo/public/cpp/system/watcher.h"
17 17
18 namespace extensions { 18 namespace extensions {
19 namespace { 19 namespace {
20 20
21 // An implementation of StashService that forwards calls to a StashBackend. 21 // An implementation of StashService that forwards calls to a StashBackend.
22 class StashServiceImpl : public StashService { 22 class StashServiceImpl : public StashService {
23 public: 23 public:
24 StashServiceImpl(mojo::InterfaceRequest<StashService> request, 24 explicit StashServiceImpl(base::WeakPtr<StashBackend> backend);
25 base::WeakPtr<StashBackend> backend);
26 ~StashServiceImpl() override; 25 ~StashServiceImpl() override;
27 26
28 // StashService overrides. 27 // StashService overrides.
29 void AddToStash(mojo::Array<StashedObjectPtr> stash) override; 28 void AddToStash(mojo::Array<StashedObjectPtr> stash) override;
30 void RetrieveStash(const RetrieveStashCallback& callback) override; 29 void RetrieveStash(const RetrieveStashCallback& callback) override;
31 30
32 private: 31 private:
33 mojo::StrongBinding<StashService> binding_;
34 base::WeakPtr<StashBackend> backend_; 32 base::WeakPtr<StashBackend> backend_;
35 33
36 DISALLOW_COPY_AND_ASSIGN(StashServiceImpl); 34 DISALLOW_COPY_AND_ASSIGN(StashServiceImpl);
37 }; 35 };
38 36
39 StashServiceImpl::StashServiceImpl(mojo::InterfaceRequest<StashService> request, 37 StashServiceImpl::StashServiceImpl(base::WeakPtr<StashBackend> backend)
40 base::WeakPtr<StashBackend> backend) 38 : backend_(backend) {}
41 : binding_(this, std::move(request)), backend_(backend) {}
42 39
43 StashServiceImpl::~StashServiceImpl() { 40 StashServiceImpl::~StashServiceImpl() {}
44 }
45 41
46 void StashServiceImpl::AddToStash( 42 void StashServiceImpl::AddToStash(
47 mojo::Array<StashedObjectPtr> stashed_objects) { 43 mojo::Array<StashedObjectPtr> stashed_objects) {
48 if (!backend_) 44 if (!backend_)
49 return; 45 return;
50 backend_->AddToStash(std::move(stashed_objects)); 46 backend_->AddToStash(std::move(stashed_objects));
51 } 47 }
52 48
53 void StashServiceImpl::RetrieveStash(const RetrieveStashCallback& callback) { 49 void StashServiceImpl::RetrieveStash(const RetrieveStashCallback& callback) {
54 if (!backend_) { 50 if (!backend_) {
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
114 has_notified_ = false; 110 has_notified_ = false;
115 mojo::Array<StashedObjectPtr> result; 111 mojo::Array<StashedObjectPtr> result;
116 for (auto& entry : stashed_objects_) { 112 for (auto& entry : stashed_objects_) {
117 result.push_back(entry->Release()); 113 result.push_back(entry->Release());
118 } 114 }
119 stashed_objects_.clear(); 115 stashed_objects_.clear();
120 return result; 116 return result;
121 } 117 }
122 118
123 void StashBackend::BindToRequest(mojo::InterfaceRequest<StashService> request) { 119 void StashBackend::BindToRequest(mojo::InterfaceRequest<StashService> request) {
124 new StashServiceImpl(std::move(request), weak_factory_.GetWeakPtr()); 120 mojo::MakeStrongBinding(
121 base::MakeUnique<StashServiceImpl>(weak_factory_.GetWeakPtr()),
122 std::move(request));
125 } 123 }
126 124
127 void StashBackend::OnHandleReady() { 125 void StashBackend::OnHandleReady() {
128 DCHECK(!has_notified_); 126 DCHECK(!has_notified_);
129 has_notified_ = true; 127 has_notified_ = true;
130 for (auto& entry : stashed_objects_) { 128 for (auto& entry : stashed_objects_) {
131 entry->CancelHandleNotifications(); 129 entry->CancelHandleNotifications();
132 } 130 }
133 if (!on_handle_readable_.is_null()) 131 if (!on_handle_readable_.is_null())
134 on_handle_readable_.Run(); 132 on_handle_readable_.Run();
(...skipping 28 matching lines...) Expand all
163 waiters_.clear(); 161 waiters_.clear();
164 } 162 }
165 163
166 void StashBackend::StashEntry::OnHandleReady(MojoResult result) { 164 void StashBackend::StashEntry::OnHandleReady(MojoResult result) {
167 if (result != MOJO_RESULT_OK) 165 if (result != MOJO_RESULT_OK)
168 return; 166 return;
169 on_handle_readable_.Run(); 167 on_handle_readable_.Run();
170 } 168 }
171 169
172 } // namespace extensions 170 } // namespace extensions
OLDNEW
« no previous file with comments | « extensions/browser/mojo/keep_alive_impl.cc ('k') | extensions/renderer/api/serial/serial_api_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698