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_
10 #include "base/strings/string16.h"
11 #include "base/timer/timer.h"
12 #include "base/values.h"
13 #include "chrome/browser/common/cancelable_request.h"
14 #include "chrome/browser/history/history_service.h"
15 #include "chrome/browser/history/web_history_service.h"
16 #include "chrome/common/cancelable_task_tracker.h"
17 #include "content/public/browser/notification_registrar.h"
18 #include "content/public/browser/web_ui_controller.h"
19 #include "content/public/browser/web_ui_message_handler.h"
22 class ManagedUserService;
23 class ProfileSyncService;
25 // The handler for Javascript messages related to the "history" view.
26 class BrowsingHistoryHandler : public content::WebUIMessageHandler,
27 public content::NotificationObserver {
29 // Represents a history entry to be shown to the user, representing either
30 // a local or remote visit. A single entry can represent multiple visits,
31 // since only the most recent visit on a particular day is shown.
33 // Values indicating whether an entry represents only local visits, only
34 // remote visits, or a mixture of both.
42 HistoryEntry(EntryType type, const GURL& url, const string16& title,
43 base::Time time, const std::string& client_id,
44 bool is_search_result, const string16& snippet,
45 bool blocked_visit, const std::string& accept_languages);
47 virtual ~HistoryEntry();
49 // Formats this entry's URL and title and adds them to |result|.
50 void SetUrlAndTitle(DictionaryValue* result) const;
52 // Converts the entry to a DictionaryValue to be owned by the caller.
53 scoped_ptr<DictionaryValue> ToValue(
54 BookmarkModel* bookmark_model,
55 ManagedUserService* managed_user_service,
56 const ProfileSyncService* sync_service) const;
58 // Comparison function for sorting HistoryEntries from newest to oldest.
59 static bool SortByTimeDescending(
60 const HistoryEntry& entry1, const HistoryEntry& entry2);
62 // The type of visits this entry represents: local, remote, or both.
66 string16 title; // Title of the entry. May be empty.
68 // The time of the entry. Usually this will be the time of the most recent
69 // visit to |url| on a particular day as defined in the local timezone.
72 // The sync ID of the client on which the most recent visit occurred.
73 std::string client_id;
75 // Timestamps of all local or remote visits the same URL on the same day.
76 std::set<int64> all_timestamps;
78 // If true, this entry is a search result.
79 bool is_search_result;
81 // The entry's search snippet, if this entry is a search result.
84 // Whether this entry was blocked when it was attempted.
87 // kAcceptLanguages pref value.
88 std::string accept_languages;
91 BrowsingHistoryHandler();
92 virtual ~BrowsingHistoryHandler();
94 // WebUIMessageHandler implementation.
95 virtual void RegisterMessages() OVERRIDE;
97 // Handler for the "queryHistory" message.
98 void HandleQueryHistory(const base::ListValue* args);
100 // Handler for the "removeVisits" message.
101 void HandleRemoveVisits(const base::ListValue* args);
103 // Handler for "clearBrowsingData" message.
104 void HandleClearBrowsingData(const base::ListValue* args);
106 // Handler for "removeBookmark" message.
107 void HandleRemoveBookmark(const base::ListValue* args);
109 // content::NotificationObserver implementation.
110 virtual void Observe(int type,
111 const content::NotificationSource& source,
112 const content::NotificationDetails& details) OVERRIDE;
114 // Merges duplicate entries from the query results, only retaining the most
115 // recent visit to a URL on a particular day. That visit contains the
116 // timestamps of the other visits.
117 static void MergeDuplicateResults(
118 std::vector<BrowsingHistoryHandler::HistoryEntry>* results);
121 // The range for which to return results:
122 // - ALLTIME: allows access to all the results in a paginated way.
123 // - WEEK: the last 7 days.
124 // - MONTH: the last calendar month.
131 // Core implementation of history querying.
132 void QueryHistory(string16 search_text, const history::QueryOptions& options);
134 // Combines the query results from the local history database and the history
135 // server, and sends the combined results to the front end.
136 void ReturnResultsToFrontEnd();
138 // Callback from |web_history_timer_| when a response from web history has
139 // not been received in time.
140 void WebHistoryTimeout();
142 // Callback from the history system when a history query has completed.
143 void QueryComplete(const string16& search_text,
144 const history::QueryOptions& options,
145 HistoryService::Handle request_handle,
146 history::QueryResults* results);
148 // Callback from the WebHistoryService when a query has completed.
149 void WebHistoryQueryComplete(const string16& search_text,
150 const history::QueryOptions& options,
151 base::TimeTicks start_time,
152 history::WebHistoryService::Request* request,
153 const base::DictionaryValue* results_value);
155 // Callback from the history system when visits were deleted.
156 void RemoveComplete();
158 // Callback from history server when visits were deleted.
159 void RemoveWebHistoryComplete(history::WebHistoryService::Request* request,
162 bool ExtractIntegerValueAtIndex(
163 const base::ListValue* value, int index, int* out_int);
165 // Sets the query options for a week-wide query, |offset| weeks ago.
166 void SetQueryTimeInWeeks(int offset, history::QueryOptions* options);
168 // Sets the query options for a monthly query, |offset| months ago.
169 void SetQueryTimeInMonths(int offset, history::QueryOptions* options);
171 // kAcceptLanguages pref value.
172 std::string GetAcceptLanguages() const;
174 content::NotificationRegistrar registrar_;
176 // Consumer for search requests to the history service.
177 CancelableRequestConsumerT<int, 0> history_request_consumer_;
179 // The currently-executing request for synced history results.
180 // Deleting the request will cancel it.
181 scoped_ptr<history::WebHistoryService::Request> web_history_request_;
183 // The currently-executing delete request for synced history.
184 // Deleting the request will cancel it.
185 scoped_ptr<history::WebHistoryService::Request> web_history_delete_request_;
187 // Tracker for delete requests to the history service.
188 CancelableTaskTracker delete_task_tracker_;
190 // The list of URLs that are in the process of being deleted.
191 std::set<GURL> urls_to_be_deleted_;
193 // The info value that is returned to the front end with the query results.
194 base::DictionaryValue results_info_value_;
196 // The list of query results received from the history service.
197 std::vector<HistoryEntry> query_results_;
199 // The list of query results received from the history server.
200 std::vector<HistoryEntry> web_history_query_results_;
202 // Timer used to implement a timeout on a Web History response.
203 base::OneShotTimer<BrowsingHistoryHandler> web_history_timer_;
205 DISALLOW_COPY_AND_ASSIGN(BrowsingHistoryHandler);
208 class HistoryUI : public content::WebUIController {
210 explicit HistoryUI(content::WebUI* web_ui);
212 // Return the URL for a given search term.
213 static const GURL GetHistoryURLWithSearchText(const string16& text);
215 static base::RefCountedMemory* GetFaviconResourceBytes(
216 ui::ScaleFactor scale_factor);
219 DISALLOW_COPY_AND_ASSIGN(HistoryUI);
222 #endif // CHROME_BROWSER_UI_WEBUI_HISTORY_UI_H_