Home | History | Annotate | Download | only in value_store
      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
      3 // found in the LICENSE file.
      4 
      5 #ifndef EXTENSIONS_BROWSER_VALUE_STORE_VALUE_STORE_FRONTEND_H_
      6 #define EXTENSIONS_BROWSER_VALUE_STORE_VALUE_STORE_FRONTEND_H_
      7 
      8 #include <string>
      9 
     10 #include "base/callback.h"
     11 #include "base/memory/ref_counted.h"
     12 #include "base/memory/scoped_ptr.h"
     13 #include "base/memory/weak_ptr.h"
     14 #include "base/threading/non_thread_safe.h"
     15 #include "base/values.h"
     16 #include "extensions/browser/value_store/value_store.h"
     17 
     18 namespace base {
     19 class FilePath;
     20 }
     21 
     22 // A frontend for a LeveldbValueStore, for use on the UI thread.
     23 class ValueStoreFrontend
     24     : public base::SupportsWeakPtr<ValueStoreFrontend>,
     25       public base::NonThreadSafe {
     26  public:
     27   typedef base::Callback<void(scoped_ptr<base::Value>)> ReadCallback;
     28 
     29   ValueStoreFrontend();
     30   explicit ValueStoreFrontend(const base::FilePath& db_path);
     31   // This variant is useful for testing (using a mock ValueStore).
     32   explicit ValueStoreFrontend(scoped_ptr<ValueStore> value_store);
     33   ~ValueStoreFrontend();
     34 
     35   void Init(const base::FilePath& db_path);
     36 
     37   // Retrieves a value from the database asynchronously, passing a copy to
     38   // |callback| when ready. NULL is passed if no matching entry is found.
     39   void Get(const std::string& key, const ReadCallback& callback);
     40 
     41   // Sets a value with the given key.
     42   void Set(const std::string& key, scoped_ptr<base::Value> value);
     43 
     44   // Removes the value with the given key.
     45   void Remove(const std::string& key);
     46 
     47  private:
     48   class Backend;
     49 
     50   // A helper class to manage lifetime of the backing ValueStore, which lives
     51   // on the FILE thread.
     52   scoped_refptr<Backend> backend_;
     53 
     54   DISALLOW_COPY_AND_ASSIGN(ValueStoreFrontend);
     55 };
     56 
     57 #endif  // EXTENSIONS_BROWSER_VALUE_STORE_VALUE_STORE_FRONTEND_H_
     58