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 CHROME_BROWSER_UI_APP_LIST_SEARCH_HISTORY_DATA_H_
6 #define CHROME_BROWSER_UI_APP_LIST_SEARCH_HISTORY_DATA_H_
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"
21 class HistoryDataObserver
;
22 class HistoryDataStore
;
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
33 class HistoryData
: public base::SupportsWeakPtr
<HistoryData
> {
35 typedef std::deque
<std::string
> SecondaryDeque
;
37 // Defines data to be associated with a query.
42 // Primary result associated with the query.
45 // Secondary results associated with the query from oldest to latest.
46 SecondaryDeque secondary
;
49 base::Time update_time
;
51 typedef std::map
<std::string
, Data
> Associations
;
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
57 HistoryData(HistoryDataStore
* store
,
59 size_t max_secondary
);
62 // Adds an association.
63 void Add(const std::string
& query
, const std::string
& result_id
);
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;
69 void AddObserver(HistoryDataObserver
* observer
);
70 void RemoveObserver(HistoryDataObserver
* observer
);
72 const Associations
& associations() const { return associations_
; }
75 // Invoked from |store| with loaded data.
76 void OnStoreLoaded(scoped_ptr
<Associations
> loaded_data
);
78 // Trims the data to keep the most recent |max_primary_| queries.
81 HistoryDataStore
* store_
; // Not owned.
82 const size_t max_primary_
;
83 const size_t max_secondary_
;
84 ObserverList
<HistoryDataObserver
, true> observers_
;
86 Associations associations_
;
88 DISALLOW_COPY_AND_ASSIGN(HistoryData
);
91 } // namespace app_list
93 #endif // CHROME_BROWSER_UI_APP_LIST_SEARCH_HISTORY_DATA_H_