1 // Copyright (c) 2012 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 CHROME_BROWSER_VALUE_STORE_VALUE_STORE_FRONTEND_H_ 6 #define CHROME_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 17 class ValueStore; 18 19 namespace base { 20 class FilePath; 21 } 22 23 // A frontend for a LeveldbValueStore, for use on the UI thread. 24 class ValueStoreFrontend 25 : public base::SupportsWeakPtr<ValueStoreFrontend>, 26 public base::NonThreadSafe { 27 public: 28 typedef base::Callback<void(scoped_ptr<base::Value>)> ReadCallback; 29 30 ValueStoreFrontend(); 31 explicit ValueStoreFrontend(const base::FilePath& db_path); 32 // This variant is useful for testing (using a mock ValueStore). 33 explicit ValueStoreFrontend(ValueStore* value_store); 34 ~ValueStoreFrontend(); 35 36 void Init(const base::FilePath& db_path); 37 38 // Retrieves a value from the database asynchronously, passing a copy to 39 // |callback| when ready. NULL is passed if no matching entry is found. 40 void Get(const std::string& key, const ReadCallback& callback); 41 42 // Sets a value with the given key. 43 void Set(const std::string& key, scoped_ptr<base::Value> value); 44 45 // Removes the value with the given key. 46 void Remove(const std::string& key); 47 48 private: 49 class Backend; 50 51 // A helper class to manage lifetime of the backing ValueStore, which lives 52 // on the FILE thread. 53 scoped_refptr<Backend> backend_; 54 55 DISALLOW_COPY_AND_ASSIGN(ValueStoreFrontend); 56 }; 57 58 #endif // CHROME_BROWSER_VALUE_STORE_VALUE_STORE_FRONTEND_H_ 59