Popular sites on the NTP: check that experiment group StartsWith (rather than IS...
[chromium-blink-merge.git] / chrome / browser / ui / webui / history_ui.h
blob10d1abc4128a665c3a97d878648807f0292200ff
1 // Copyright (c) 2012 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_WEBUI_HISTORY_UI_H_
6 #define CHROME_BROWSER_UI_WEBUI_HISTORY_UI_H_
8 #include <string>
10 #include "base/memory/scoped_ptr.h"
11 #include "base/memory/weak_ptr.h"
12 #include "base/scoped_observer.h"
13 #include "base/strings/string16.h"
14 #include "base/task/cancelable_task_tracker.h"
15 #include "base/timer/timer.h"
16 #include "base/values.h"
17 #include "components/history/core/browser/history_service_observer.h"
18 #include "components/history/core/browser/web_history_service.h"
19 #include "content/public/browser/web_ui_controller.h"
20 #include "content/public/browser/web_ui_message_handler.h"
21 #include "ui/base/layout.h"
23 class ProfileSyncService;
24 class SupervisedUserService;
26 namespace bookmarks {
27 class BookmarkModel;
30 namespace history {
31 class HistoryService;
34 // The handler for Javascript messages related to the "history" view.
35 class BrowsingHistoryHandler : public content::WebUIMessageHandler,
36 public history::HistoryServiceObserver {
37 public:
38 // Represents a history entry to be shown to the user, representing either
39 // a local or remote visit. A single entry can represent multiple visits,
40 // since only the most recent visit on a particular day is shown.
41 struct HistoryEntry {
42 // Values indicating whether an entry represents only local visits, only
43 // remote visits, or a mixture of both.
44 enum EntryType {
45 EMPTY_ENTRY = 0,
46 LOCAL_ENTRY,
47 REMOTE_ENTRY,
48 COMBINED_ENTRY
51 HistoryEntry(EntryType type, const GURL& url, const base::string16& title,
52 base::Time time, const std::string& client_id,
53 bool is_search_result, const base::string16& snippet,
54 bool blocked_visit, const std::string& accept_languages);
55 HistoryEntry();
56 virtual ~HistoryEntry();
58 // Formats this entry's URL and title and adds them to |result|.
59 void SetUrlAndTitle(base::DictionaryValue* result) const;
61 // Converts the entry to a DictionaryValue to be owned by the caller.
62 scoped_ptr<base::DictionaryValue> ToValue(
63 bookmarks::BookmarkModel* bookmark_model,
64 SupervisedUserService* supervised_user_service,
65 const ProfileSyncService* sync_service) const;
67 // Comparison function for sorting HistoryEntries from newest to oldest.
68 static bool SortByTimeDescending(
69 const HistoryEntry& entry1, const HistoryEntry& entry2);
71 // The type of visits this entry represents: local, remote, or both.
72 EntryType entry_type;
74 GURL url;
75 base::string16 title; // Title of the entry. May be empty.
77 // The time of the entry. Usually this will be the time of the most recent
78 // visit to |url| on a particular day as defined in the local timezone.
79 base::Time time;
81 // The sync ID of the client on which the most recent visit occurred.
82 std::string client_id;
84 // Timestamps of all local or remote visits the same URL on the same day.
85 std::set<int64> all_timestamps;
87 // If true, this entry is a search result.
88 bool is_search_result;
90 // The entry's search snippet, if this entry is a search result.
91 base::string16 snippet;
93 // Whether this entry was blocked when it was attempted.
94 bool blocked_visit;
96 // kAcceptLanguages pref value.
97 std::string accept_languages;
100 BrowsingHistoryHandler();
101 ~BrowsingHistoryHandler() override;
103 // WebUIMessageHandler implementation.
104 void RegisterMessages() override;
106 // Handler for the "queryHistory" message.
107 void HandleQueryHistory(const base::ListValue* args);
109 // Handler for the "removeVisits" message.
110 void HandleRemoveVisits(const base::ListValue* args);
112 // Handler for "clearBrowsingData" message.
113 void HandleClearBrowsingData(const base::ListValue* args);
115 // Handler for "removeBookmark" message.
116 void HandleRemoveBookmark(const base::ListValue* args);
118 // Merges duplicate entries from the query results, only retaining the most
119 // recent visit to a URL on a particular day. That visit contains the
120 // timestamps of the other visits.
121 static void MergeDuplicateResults(
122 std::vector<BrowsingHistoryHandler::HistoryEntry>* results);
124 private:
125 // The range for which to return results:
126 // - ALLTIME: allows access to all the results in a paginated way.
127 // - WEEK: the last 7 days.
128 // - MONTH: the last calendar month.
129 enum Range {
130 ALL_TIME = 0,
131 WEEK = 1,
132 MONTH = 2
135 // Core implementation of history querying.
136 void QueryHistory(const base::string16& search_text,
137 const history::QueryOptions& options);
139 // Combines the query results from the local history database and the history
140 // server, and sends the combined results to the front end.
141 void ReturnResultsToFrontEnd();
143 // Callback from |web_history_timer_| when a response from web history has
144 // not been received in time.
145 void WebHistoryTimeout();
147 // Callback from the history system when a history query has completed.
148 void QueryComplete(const base::string16& search_text,
149 const history::QueryOptions& options,
150 history::QueryResults* results);
152 // Callback from the WebHistoryService when a query has completed.
153 void WebHistoryQueryComplete(const base::string16& search_text,
154 const history::QueryOptions& options,
155 base::TimeTicks start_time,
156 history::WebHistoryService::Request* request,
157 const base::DictionaryValue* results_value);
159 // Callback from the history system when visits were deleted.
160 void RemoveComplete();
162 // Callback from history server when visits were deleted.
163 void RemoveWebHistoryComplete(bool success);
165 bool ExtractIntegerValueAtIndex(
166 const base::ListValue* value, int index, int* out_int);
168 // Sets the query options for a week-wide query, |offset| weeks ago.
169 void SetQueryTimeInWeeks(int offset, history::QueryOptions* options);
171 // Sets the query options for a monthly query, |offset| months ago.
172 void SetQueryTimeInMonths(int offset, history::QueryOptions* options);
174 // kAcceptLanguages pref value.
175 std::string GetAcceptLanguages() const;
177 // history::HistoryServiceObserver:
178 void OnURLsDeleted(history::HistoryService* history_service,
179 bool all_history,
180 bool expired,
181 const history::URLRows& deleted_rows,
182 const std::set<GURL>& favicon_urls) override;
184 // Tracker for search requests to the history service.
185 base::CancelableTaskTracker query_task_tracker_;
187 // The currently-executing request for synced history results.
188 // Deleting the request will cancel it.
189 scoped_ptr<history::WebHistoryService::Request> web_history_request_;
191 // True if there is a pending delete requests to the history service.
192 bool has_pending_delete_request_;
194 // Tracker for delete requests to the history service.
195 base::CancelableTaskTracker delete_task_tracker_;
197 // The list of URLs that are in the process of being deleted.
198 std::set<GURL> urls_to_be_deleted_;
200 // The info value that is returned to the front end with the query results.
201 base::DictionaryValue results_info_value_;
203 // The list of query results received from the history service.
204 std::vector<HistoryEntry> query_results_;
206 // The list of query results received from the history server.
207 std::vector<HistoryEntry> web_history_query_results_;
209 // Timer used to implement a timeout on a Web History response.
210 base::OneShotTimer<BrowsingHistoryHandler> web_history_timer_;
212 ScopedObserver<history::HistoryService, history::HistoryServiceObserver>
213 history_service_observer_;
215 base::WeakPtrFactory<BrowsingHistoryHandler> weak_factory_;
217 DISALLOW_COPY_AND_ASSIGN(BrowsingHistoryHandler);
220 class HistoryUI : public content::WebUIController {
221 public:
222 explicit HistoryUI(content::WebUI* web_ui);
223 ~HistoryUI() override;
225 static base::RefCountedMemory* GetFaviconResourceBytes(
226 ui::ScaleFactor scale_factor);
228 private:
229 DISALLOW_COPY_AND_ASSIGN(HistoryUI);
232 #endif // CHROME_BROWSER_UI_WEBUI_HISTORY_UI_H_