summaryrefslogtreecommitdiffstats
path: root/extensions/browser/mojo/stash_backend.h
blob: f374e00f9574ecb07614d90c2c6830255ee40524 (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
// 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.

#ifndef EXTENSIONS_BROWSER_MOJO_STASH_BACKEND_H_
#define EXTENSIONS_BROWSER_MOJO_STASH_BACKEND_H_

#include <vector>

#include "base/callback.h"
#include "base/macros.h"
#include "base/memory/weak_ptr.h"
#include "extensions/common/mojo/stash.mojom.h"
#include "mojo/public/cpp/bindings/interface_request.h"

namespace extensions {

// A backend that provides access to StashService for a single extension.
class StashBackend {
 public:
  explicit StashBackend(const base::Closure& on_handle_readable);
  ~StashBackend();

  // Creates a StashService that forwards calls to this StashBackend and bind it
  // to |request|.
  void BindToRequest(mojo::InterfaceRequest<StashService> request);

  // Adds the StashedObjects contained within |stash| to the stash.
  void AddToStash(mojo::Array<StashedObjectPtr> stash);

  // Returns all StashedObjects added to the stash since the last call to
  // RetrieveStash.
  mojo::Array<StashedObjectPtr> RetrieveStash();

 private:
  class StashEntry;

  // Invoked when a handle is readable.
  void OnHandleReady();

  // The objects that have been stashed.
  std::vector<scoped_ptr<StashEntry>> stashed_objects_;

  // The callback to call when a handle is readable.
  const base::Closure on_handle_readable_;

  // Whether a handle has become readable since the last RetrieveStash() call.
  bool has_notified_;

  base::WeakPtrFactory<StashBackend> weak_factory_;

  DISALLOW_COPY_AND_ASSIGN(StashBackend);
};

}  // namespace extensions

#endif  // EXTENSIONS_BROWSER_MOJO_STASH_BACKEND_H_