history_data_store.h revision 1320f92c476a1ad9d19dba2a48c72b75566198e9
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_HISTORY_DATA_STORE_H_
6#define CHROME_BROWSER_UI_APP_LIST_SEARCH_HISTORY_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/memory/ref_counted.h"
14#include "base/memory/scoped_ptr.h"
15#include "chrome/browser/ui/app_list/search/common/dictionary_data_store.h"
16#include "chrome/browser/ui/app_list/search/history_data.h"
17
18namespace base {
19class DictionaryValue;
20class SequencedTaskRunner;
21}
22
23namespace app_list {
24
25namespace test {
26class HistoryDataStoreTest;
27}
28
29// A simple json store to persist HistoryData.
30class HistoryDataStore : public base::RefCountedThreadSafe<HistoryDataStore> {
31 public:
32  typedef base::Callback<void(scoped_ptr<HistoryData::Associations>)>
33      OnLoadedCallback;
34
35  // A data store with no storage backend.
36  HistoryDataStore();
37
38  // |data_store| stores the history into the file.
39  explicit HistoryDataStore(scoped_refptr<DictionaryDataStore> data_store);
40
41  // Flushes pending writes. |on_flushed| is invoked when disk write is
42  // finished.
43  void Flush(const DictionaryDataStore::OnFlushedCallback& on_flushed);
44
45  // Reads the persisted data from disk asynchronously. |on_read| is called
46  // with the loaded and parsed data. If there is an error, |on_read| is called
47  // without data.
48  void Load(const OnLoadedCallback& on_loaded);
49
50  // Incremental changes allowed on the data store.
51  void SetPrimary(const std::string& query, const std::string& result);
52  void SetSecondary(const std::string& query,
53                    const HistoryData::SecondaryDeque& results);
54  void SetUpdateTime(const std::string& query, const base::Time& update_time);
55  void Delete(const std::string& query);
56
57 private:
58  friend class base::RefCountedThreadSafe<HistoryDataStore>;
59  friend class app_list::test::HistoryDataStoreTest;
60
61  virtual ~HistoryDataStore();
62
63  void Init(base::DictionaryValue* cached_dict);
64
65  // Gets the dictionary for "associations" key.
66  base::DictionaryValue* GetAssociationDict();
67
68  // Gets entry dictionary for given |query|. Creates one if necessary.
69  base::DictionaryValue* GetEntryDict(const std::string& query);
70
71  void OnDictionaryLoadedCallback(OnLoadedCallback callback,
72                                  scoped_ptr<base::DictionaryValue> dict);
73
74  // |cached_dict_| and |data_store_| is mutually exclusive. |data_store_| is
75  // used if it's backed by a file storage, otherwise |cache_dict_| keeps
76  // on-memory data.
77  scoped_ptr<base::DictionaryValue> cached_dict_;
78  scoped_refptr<DictionaryDataStore> data_store_;
79
80  DISALLOW_COPY_AND_ASSIGN(HistoryDataStore);
81};
82
83}  // namespace app_list
84
85#endif  // CHROME_BROWSER_UI_APP_LIST_SEARCH_HISTORY_DATA_STORE_H_
86