Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / ui / app_list / search / dictionary_data_store.h
blob1a882c13ac37fd9c15ae2d12a3b4644f94f58a89
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.
5 #ifndef UI_APP_LIST_SEARCH_DICTIONARY_DATA_STORE_H_
6 #define UI_APP_LIST_SEARCH_DICTIONARY_DATA_STORE_H_
8 #include <string>
9 #include <vector>
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 #include "ui/app_list/app_list_export.h"
19 namespace base {
20 class DictionaryValue;
21 class SequencedTaskRunner;
22 class SequencedWorkerPool;
25 namespace app_list {
27 // A simple JSON store to persist a dictionary.
28 class APP_LIST_EXPORT DictionaryDataStore
29 : public base::RefCountedThreadSafe<DictionaryDataStore>,
30 public base::ImportantFileWriter::DataSerializer {
31 public:
32 typedef base::Callback<void(scoped_ptr<base::DictionaryValue>)>
33 OnLoadedCallback;
34 typedef base::Closure OnFlushedCallback;
36 DictionaryDataStore(const base::FilePath& data_file,
37 base::SequencedWorkerPool* worker_pool);
39 // Flushes pending writes.
40 void Flush(const OnFlushedCallback& on_flushed);
42 // Reads the persisted data from disk asynchronously. |on_read| is called
43 // with the loaded and parsed data. If there is an error, |on_read| is called
44 // without data.
45 void Load(const OnLoadedCallback& on_loaded);
47 // Schedule a job to persist the cached dictionary.
48 void ScheduleWrite();
50 // Used to get a pointer to the cached dictionary. Changes to this dictionary
51 // will not be persisted unless ScheduleWrite() is called.
52 base::DictionaryValue* cached_dict() { return cached_dict_.get(); }
54 private:
55 friend class base::RefCountedThreadSafe<DictionaryDataStore>;
57 ~DictionaryDataStore() override;
59 // Reads data from backing file.
60 scoped_ptr<base::DictionaryValue> LoadOnBlockingPool();
62 // ImportantFileWriter::DataSerializer overrides:
63 bool SerializeData(std::string* data) override;
65 base::FilePath data_file_;
66 scoped_refptr<base::SequencedTaskRunner> file_task_runner_;
67 scoped_ptr<base::ImportantFileWriter> writer_;
69 // Cached JSON dictionary to serve read and incremental change calls.
70 scoped_ptr<base::DictionaryValue> cached_dict_;
72 base::SequencedWorkerPool* worker_pool_;
74 DISALLOW_COPY_AND_ASSIGN(DictionaryDataStore);
77 } // namespace app_list
79 #endif // UI_APP_LIST_SEARCH_DICTIONARY_DATA_STORE_H_