190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved. 290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// found in the LICENSE file. 490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#ifndef CHROME_BROWSER_UI_APP_LIST_SEARCH_HISTORY_DATA_H_ 690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#define CHROME_BROWSER_UI_APP_LIST_SEARCH_HISTORY_DATA_H_ 790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include <deque> 990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include <map> 1090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include <string> 1190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 1290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "base/basictypes.h" 1390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "base/memory/scoped_ptr.h" 1490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "base/memory/weak_ptr.h" 1590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "base/observer_list.h" 16eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "base/time/time.h" 1790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "chrome/browser/ui/app_list/search/history_types.h" 1890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 1990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)namespace app_list { 2090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 2190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)class HistoryDataObserver; 2290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)class HistoryDataStore; 2390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 2490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// HistoryData stores the associations of the user typed queries and launched 2590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// search result id. There are two types of association: primary and secondary. 2690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Primary is a 1-to-1 mapping between the query and result id. Secondary 2790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// is a 1-to-many mapping and only kept the last 5 to limit the data size. 2890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// If an association is added for the first time, it is added as a primary 2990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// association. Further associations added to the same query are added as 3090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// secondary. However, if a secondary association is added twice in a row, it 3190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// is promoted to primary and the current primary mapping is demoted into 3290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// secondary. 3390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)class HistoryData : public base::SupportsWeakPtr<HistoryData> { 3490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) public: 3590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) typedef std::deque<std::string> SecondaryDeque; 3690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 3790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // Defines data to be associated with a query. 3890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) struct Data { 3990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) Data(); 4090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) ~Data(); 4190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 4290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // Primary result associated with the query. 4390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) std::string primary; 4490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 4590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // Secondary results associated with the query from oldest to latest. 4690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) SecondaryDeque secondary; 4790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 4890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // Last update time. 4990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) base::Time update_time; 5090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) }; 5190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) typedef std::map<std::string, Data> Associations; 5290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 5390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // Constructor of HistoryData. |store| is the storage to persist the data. 5490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // |max_primary| is the maximum number of the most recent primary associations 5590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // to keep. |max_secondary| is the maximum number of secondary associations to 5690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // keep. 5790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) HistoryData(HistoryDataStore* store, 5890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) size_t max_primary, 5990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) size_t max_secondary); 6090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) ~HistoryData(); 6190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 6290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // Adds an association. 6390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) void Add(const std::string& query, const std::string& result_id); 6490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 6590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // Gets all known search results that were launched using the given |query| 6690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // or the queries that |query| is a prefix of. 6790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) scoped_ptr<KnownResults> GetKnownResults(const std::string& query) const; 6890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 6990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) void AddObserver(HistoryDataObserver* observer); 7090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) void RemoveObserver(HistoryDataObserver* observer); 7190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 7290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) const Associations& associations() const { return associations_; } 7390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 7490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) private: 7590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // Invoked from |store| with loaded data. 7690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) void OnStoreLoaded(scoped_ptr<Associations> loaded_data); 7790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 7890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) // Trims the data to keep the most recent |max_primary_| queries. 7990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) void TrimEntries(); 8090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 8190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) HistoryDataStore* store_; // Not owned. 8290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) const size_t max_primary_; 8390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) const size_t max_secondary_; 8490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) ObserverList<HistoryDataObserver, true> observers_; 8590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 8690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) Associations associations_; 8790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 8890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) DISALLOW_COPY_AND_ASSIGN(HistoryData); 8990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}; 9090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 9190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)} // namespace app_list 9290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) 9390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#endif // CHROME_BROWSER_UI_APP_LIST_SEARCH_HISTORY_DATA_H_ 94