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_HISTORY_DATA_H_
6 #define 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 "ui/app_list/app_list_export.h"
18 #include "ui/app_list/search/history_types.h"
22 class HistoryDataObserver
;
23 class HistoryDataStore
;
25 // HistoryData stores the associations of the user typed queries and launched
26 // search result id. There are two types of association: primary and secondary.
27 // Primary is a 1-to-1 mapping between the query and result id. Secondary
28 // is a 1-to-many mapping and only kept the last 5 to limit the data size.
29 // If an association is added for the first time, it is added as a primary
30 // association. Further associations added to the same query are added as
31 // secondary. However, if a secondary association is added twice in a row, it
32 // is promoted to primary and the current primary mapping is demoted into
34 class APP_LIST_EXPORT HistoryData
: public base::SupportsWeakPtr
<HistoryData
> {
36 typedef std::deque
<std::string
> SecondaryDeque
;
38 // Defines data to be associated with a query.
39 struct APP_LIST_EXPORT Data
{
43 // Primary result associated with the query.
46 // Secondary results associated with the query from oldest to latest.
47 SecondaryDeque secondary
;
50 base::Time update_time
;
52 typedef std::map
<std::string
, Data
> Associations
;
54 // Constructor of HistoryData. |store| is the storage to persist the data.
55 // |max_primary| is the maximum number of the most recent primary associations
56 // to keep. |max_secondary| is the maximum number of secondary associations to
58 HistoryData(HistoryDataStore
* store
,
60 size_t max_secondary
);
63 // Adds an association.
64 void Add(const std::string
& query
, const std::string
& result_id
);
66 // Gets all known search results that were launched using the given |query|
67 // or the queries that |query| is a prefix of.
68 scoped_ptr
<KnownResults
> GetKnownResults(const std::string
& query
) const;
70 void AddObserver(HistoryDataObserver
* observer
);
71 void RemoveObserver(HistoryDataObserver
* observer
);
73 const Associations
& associations() const { return associations_
; }
76 // Invoked from |store| with loaded data.
77 void OnStoreLoaded(scoped_ptr
<Associations
> loaded_data
);
79 // Trims the data to keep the most recent |max_primary_| queries.
82 HistoryDataStore
* store_
; // Not owned.
83 const size_t max_primary_
;
84 const size_t max_secondary_
;
85 ObserverList
<HistoryDataObserver
, true> observers_
;
87 Associations associations_
;
89 DISALLOW_COPY_AND_ASSIGN(HistoryData
);
92 } // namespace app_list
94 #endif // UI_APP_LIST_SEARCH_HISTORY_DATA_H_