1// Copyright 2013 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_UI_APP_LIST_SEARCH_COMMON_DICTIONARY_DATA_STORE_H_ 6#define CHROME_BROWSER_UI_APP_LIST_SEARCH_COMMON_DICTIONARY_DATA_STORE_H_ 7 8#include <string> 9#include <vector> 10 11#include "base/basictypes.h" 12#include "base/callback_forward.h" 13#include "base/files/file_path.h" 14#include "base/files/important_file_writer.h" 15#include "base/memory/ref_counted.h" 16#include "base/memory/scoped_ptr.h" 17 18namespace base { 19class DictionaryValue; 20class SequencedTaskRunner; 21} 22 23namespace app_list { 24 25// A simple JSON store to persist a dictionary. 26class DictionaryDataStore 27 : public base::RefCountedThreadSafe<DictionaryDataStore>, 28 public base::ImportantFileWriter::DataSerializer { 29 public: 30 typedef base::Callback<void( 31 scoped_ptr<base::DictionaryValue>)> OnLoadedCallback; 32 typedef base::Closure OnFlushedCallback; 33 34 explicit DictionaryDataStore(const base::FilePath& data_file); 35 36 // Flushes pending writes. 37 void Flush(const OnFlushedCallback& on_flushed); 38 39 // Reads the persisted data from disk asynchronously. |on_read| is called 40 // with the loaded and parsed data. If there is an error, |on_read| is called 41 // without data. 42 void Load(const OnLoadedCallback& on_loaded); 43 44 // Schedule a job to persist the cached dictionary. 45 void ScheduleWrite(); 46 47 // Used to get a pointer to the cached dictionary. Changes to this dictionary 48 // will not be persisted unless ScheduleWrite() is called. 49 base::DictionaryValue* cached_dict() { return cached_dict_.get(); } 50 51 private: 52 friend class base::RefCountedThreadSafe<DictionaryDataStore>; 53 54 virtual ~DictionaryDataStore(); 55 56 // Reads data from backing file. 57 scoped_ptr<base::DictionaryValue> LoadOnBlockingPool(); 58 59 // ImportantFileWriter::DataSerializer overrides: 60 virtual bool SerializeData(std::string* data) OVERRIDE; 61 62 base::FilePath data_file_; 63 scoped_refptr<base::SequencedTaskRunner> file_task_runner_; 64 scoped_ptr<base::ImportantFileWriter> writer_; 65 66 // Cached JSON dictionary to serve read and incremental change calls. 67 scoped_ptr<base::DictionaryValue> cached_dict_; 68 69 DISALLOW_COPY_AND_ASSIGN(DictionaryDataStore); 70}; 71 72} // namespace app_list 73 74#endif // CHROME_BROWSER_UI_APP_LIST_SEARCH_COMMON_DICTIONARY_DATA_STORE_H_ 75