summaryrefslogtreecommitdiffstats
path: root/extensions/browser/mojo/stash_backend.cc
blob: 8d3eb40dbee7344f59e31f84eb51406781c424e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "extensions/browser/mojo/stash_backend.h"

#include <stddef.h>

#include <utility>
#include <vector>

#include "base/bind.h"
#include "base/macros.h"
#include "mojo/message_pump/handle_watcher.h"
#include "mojo/public/cpp/bindings/strong_binding.h"

namespace extensions {
namespace {

// An implementation of StashService that forwards calls to a StashBackend.
class StashServiceImpl : public StashService {
 public:
  StashServiceImpl(mojo::InterfaceRequest<StashService> request,
                   base::WeakPtr<StashBackend> backend);
  ~StashServiceImpl() override;

  // StashService overrides.
  void AddToStash(mojo::Array<StashedObjectPtr> stash) override;
  void RetrieveStash(
      const mojo::Callback<void(mojo::Array<StashedObjectPtr> stash)>& callback)
      override;

 private:
  mojo::StrongBinding<StashService> binding_;
  base::WeakPtr<StashBackend> backend_;

  DISALLOW_COPY_AND_ASSIGN(StashServiceImpl);
};

StashServiceImpl::StashServiceImpl(mojo::InterfaceRequest<StashService> request,
                                   base::WeakPtr<StashBackend> backend)
    : binding_(this, std::move(request)), backend_(backend) {}

StashServiceImpl::~StashServiceImpl() {
}

void StashServiceImpl::AddToStash(
    mojo::Array<StashedObjectPtr> stashed_objects) {
  if (!backend_)
    return;
  backend_->AddToStash(std::move(stashed_objects));
}

void StashServiceImpl::RetrieveStash(
    const mojo::Callback<void(mojo::Array<StashedObjectPtr>)>& callback) {
  if (!backend_) {
    callback.Run(mojo::Array<StashedObjectPtr>());
    return;
  }
  callback.Run(backend_->RetrieveStash());
}

}  // namespace

// A stash entry for a stashed object. This handles notifications if a handle
// within the stashed object is readable.
class StashBackend::StashEntry {
 public:
  // Construct a StashEntry for |stashed_object|. If |on_handle_readable| is
  // non-null, it will be invoked when any handle on |stashed_object| is
  // readable.
  StashEntry(StashedObjectPtr stashed_object,
             const base::Closure& on_handle_readable);
  ~StashEntry();

  // Returns the stashed object.
  StashedObjectPtr Release();

  // Cancels notifications for handles becoming readable.
  void CancelHandleNotifications();

 private:
  // Invoked when a handle within |stashed_object_| is readable.
  void OnHandleReady(MojoResult result);

  // The waiters that are waiting for handles to be readable.
  std::vector<scoped_ptr<mojo::common::HandleWatcher>> waiters_;

  StashedObjectPtr stashed_object_;

  // If non-null, a callback to call when a handle contained within
  // |stashed_object_| is readable.
  const base::Closure on_handle_readable_;
};

StashBackend::StashBackend(const base::Closure& on_handle_readable)
    : on_handle_readable_(on_handle_readable),
      has_notified_(false),
      weak_factory_(this) {
}

StashBackend::~StashBackend() {
}

void StashBackend::AddToStash(mojo::Array<StashedObjectPtr> stashed_objects) {
  for (size_t i = 0; i < stashed_objects.size(); i++) {
    stashed_objects_.push_back(make_scoped_ptr(new StashEntry(
        std::move(stashed_objects[i]),
        has_notified_ ? base::Closure()
                      : base::Bind(&StashBackend::OnHandleReady,
                                   weak_factory_.GetWeakPtr()))));
  }
}

mojo::Array<StashedObjectPtr> StashBackend::RetrieveStash() {
  has_notified_ = false;
  mojo::Array<StashedObjectPtr> result;
  for (auto& entry : stashed_objects_) {
    result.push_back(entry->Release());
  }
  stashed_objects_.clear();
  return result;
}

void StashBackend::BindToRequest(mojo::InterfaceRequest<StashService> request) {
  new StashServiceImpl(std::move(request), weak_factory_.GetWeakPtr());
}

void StashBackend::OnHandleReady() {
  DCHECK(!has_notified_);
  has_notified_ = true;
  for (auto& entry : stashed_objects_) {
    entry->CancelHandleNotifications();
  }
  if (!on_handle_readable_.is_null())
    on_handle_readable_.Run();
}

StashBackend::StashEntry::StashEntry(StashedObjectPtr stashed_object,
                                     const base::Closure& on_handle_readable)
    : stashed_object_(std::move(stashed_object)),
      on_handle_readable_(on_handle_readable) {
  if (on_handle_readable_.is_null() || !stashed_object_->monitor_handles)
    return;

  for (size_t i = 0; i < stashed_object_->stashed_handles.size(); i++) {
    scoped_ptr<mojo::common::HandleWatcher> watcher(
        new mojo::common::HandleWatcher());
    watcher->Start(stashed_object_->stashed_handles[i].get(),
                   MOJO_HANDLE_SIGNAL_READABLE, MOJO_DEADLINE_INDEFINITE,
                   base::Bind(&StashBackend::StashEntry::OnHandleReady,
                              base::Unretained(this)));
    waiters_.push_back(std::move(watcher));
  }
}

StashBackend::StashEntry::~StashEntry() {
}

StashedObjectPtr StashBackend::StashEntry::Release() {
  waiters_.clear();
  return std::move(stashed_object_);
}

void StashBackend::StashEntry::CancelHandleNotifications() {
  waiters_.clear();
}

void StashBackend::StashEntry::OnHandleReady(MojoResult result) {
  if (result != MOJO_RESULT_OK)
    return;
  on_handle_readable_.Run();
}

}  // namespace extensions