summaryrefslogtreecommitdiffstats
path: root/extensions/browser/value_store/value_store_frontend.h
blob: 55253fb4218f3324ce97bafa841e1588dc2d3143 (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
// 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_VALUE_STORE_VALUE_STORE_FRONTEND_H_
#define EXTENSIONS_BROWSER_VALUE_STORE_VALUE_STORE_FRONTEND_H_

#include <string>

#include "base/callback.h"
#include "base/macros.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/threading/non_thread_safe.h"
#include "base/values.h"
#include "extensions/browser/value_store/value_store.h"

namespace base {
class FilePath;
}

namespace extensions {
class ValueStoreFactory;
}  // namespace extensions

// A frontend for a LeveldbValueStore, for use on the UI thread.
class ValueStoreFrontend
    : public base::SupportsWeakPtr<ValueStoreFrontend>,
      public base::NonThreadSafe {
 public:
  // The kind of extensions data stored in a backend.
  enum class BackendType { RULES, STATE };

  typedef base::Callback<void(scoped_ptr<base::Value>)> ReadCallback;

  ValueStoreFrontend(
      const scoped_refptr<extensions::ValueStoreFactory>& store_factory,
      BackendType backend_type);
  ~ValueStoreFrontend();

  // Retrieves a value from the database asynchronously, passing a copy to
  // |callback| when ready. NULL is passed if no matching entry is found.
  void Get(const std::string& key, const ReadCallback& callback);

  // Sets a value with the given key.
  void Set(const std::string& key, scoped_ptr<base::Value> value);

  // Removes the value with the given key.
  void Remove(const std::string& key);

 private:
  class Backend;

  // A helper class to manage lifetime of the backing ValueStore, which lives
  // on the FILE thread.
  scoped_refptr<Backend> backend_;

  DISALLOW_COPY_AND_ASSIGN(ValueStoreFrontend);
};

#endif  // EXTENSIONS_BROWSER_VALUE_STORE_VALUE_STORE_FRONTEND_H_