Upstream version 11.40.271.0
[platform/framework/web/crosswalk.git] / src / ui / app_list / search / dictionary_data_store.h
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 UI_APP_LIST_SEARCH_DICTIONARY_DATA_STORE_H_
6 #define UI_APP_LIST_SEARCH_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 #include "ui/app_list/app_list_export.h"
18
19 namespace base {
20 class DictionaryValue;
21 class SequencedTaskRunner;
22 class SequencedWorkerPool;
23 }
24
25 namespace app_list {
26
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;
35
36   DictionaryDataStore(const base::FilePath& data_file,
37                       base::SequencedWorkerPool* worker_pool);
38
39   // Flushes pending writes.
40   void Flush(const OnFlushedCallback& on_flushed);
41
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);
46
47   // Schedule a job to persist the cached dictionary.
48   void ScheduleWrite();
49
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(); }
53
54  private:
55   friend class base::RefCountedThreadSafe<DictionaryDataStore>;
56
57   ~DictionaryDataStore() override;
58
59   // Reads data from backing file.
60   scoped_ptr<base::DictionaryValue> LoadOnBlockingPool();
61
62   // ImportantFileWriter::DataSerializer overrides:
63   bool SerializeData(std::string* data) override;
64
65   base::FilePath data_file_;
66   scoped_refptr<base::SequencedTaskRunner> file_task_runner_;
67   scoped_ptr<base::ImportantFileWriter> writer_;
68
69   // Cached JSON dictionary to serve read and incremental change calls.
70   scoped_ptr<base::DictionaryValue> cached_dict_;
71
72   base::SequencedWorkerPool* worker_pool_;
73
74   DISALLOW_COPY_AND_ASSIGN(DictionaryDataStore);
75 };
76
77 }  // namespace app_list
78
79 #endif  // UI_APP_LIST_SEARCH_DICTIONARY_DATA_STORE_H_