history_data.h revision eb525c5499e34cc9c4b825d6d9e75bb07cc06ace
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_H_
6#define CHROME_BROWSER_UI_APP_LIST_SEARCH_HISTORY_DATA_H_
7
8#include <deque>
9#include <map>
10#include <string>
11
12#include "base/basictypes.h"
13#include "base/memory/scoped_ptr.h"
14#include "base/memory/weak_ptr.h"
15#include "base/observer_list.h"
16#include "base/time/time.h"
17#include "chrome/browser/ui/app_list/search/history_types.h"
18
19namespace app_list {
20
21class HistoryDataObserver;
22class HistoryDataStore;
23
24// HistoryData stores the associations of the user typed queries and launched
25// search result id. There are two types of association: primary and secondary.
26// Primary is a 1-to-1 mapping between the query and result id. Secondary
27// is a 1-to-many mapping and only kept the last 5 to limit the data size.
28// If an association is added for the first time, it is added as a primary
29// association. Further associations added to the same query are added as
30// secondary. However, if a secondary association is added twice in a row, it
31// is promoted to primary and the current primary mapping is demoted into
32// secondary.
33class HistoryData : public base::SupportsWeakPtr<HistoryData> {
34 public:
35  typedef std::deque<std::string> SecondaryDeque;
36
37  // Defines data to be associated with a query.
38  struct Data {
39    Data();
40    ~Data();
41
42    // Primary result associated with the query.
43    std::string primary;
44
45    // Secondary results associated with the query from oldest to latest.
46    SecondaryDeque secondary;
47
48    // Last update time.
49    base::Time update_time;
50  };
51  typedef std::map<std::string, Data> Associations;
52
53  // Constructor of HistoryData. |store| is the storage to persist the data.
54  // |max_primary| is the maximum number of the most recent primary associations
55  // to keep. |max_secondary| is the maximum number of secondary associations to
56  // keep.
57  HistoryData(HistoryDataStore* store,
58              size_t max_primary,
59              size_t max_secondary);
60  ~HistoryData();
61
62  // Adds an association.
63  void Add(const std::string& query, const std::string& result_id);
64
65  // Gets all known search results that were launched using the given |query|
66  // or the queries that |query| is a prefix of.
67  scoped_ptr<KnownResults> GetKnownResults(const std::string& query) const;
68
69  void AddObserver(HistoryDataObserver* observer);
70  void RemoveObserver(HistoryDataObserver* observer);
71
72  const Associations& associations() const { return associations_; }
73
74 private:
75  // Invoked from |store| with loaded data.
76  void OnStoreLoaded(scoped_ptr<Associations> loaded_data);
77
78  // Trims the data to keep the most recent |max_primary_| queries.
79  void TrimEntries();
80
81  HistoryDataStore* store_;  // Not owned.
82  const size_t max_primary_;
83  const size_t max_secondary_;
84  ObserverList<HistoryDataObserver, true> observers_;
85
86  Associations associations_;
87
88  DISALLOW_COPY_AND_ASSIGN(HistoryData);
89};
90
91}  // namespace app_list
92
93#endif  // CHROME_BROWSER_UI_APP_LIST_SEARCH_HISTORY_DATA_H_
94