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/memory/weak_ptr.h"
11 #include "base/scoped_observer.h"
12 #include "base/strings/string16.h"
13 #include "base/task/cancelable_task_tracker.h"
14 #include "base/timer/timer.h"
15 #include "base/values.h"
16 #include "components/history/core/browser/history_service_observer.h"
17 #include "components/history/core/browser/web_history_service.h"
18 #include "content/public/browser/web_ui_controller.h"
19 #include "content/public/browser/web_ui_message_handler.h"
20 #include "ui/base/layout.h"
22 class ProfileSyncService
;
23 class SupervisedUserService
;
33 // The handler for Javascript messages related to the "history" view.
34 class BrowsingHistoryHandler
: public content::WebUIMessageHandler
,
35 public history::HistoryServiceObserver
{
37 // Represents a history entry to be shown to the user, representing either
38 // a local or remote visit. A single entry can represent multiple visits,
39 // since only the most recent visit on a particular day is shown.
41 // Values indicating whether an entry represents only local visits, only
42 // remote visits, or a mixture of both.
50 HistoryEntry(EntryType type
, const GURL
& url
, const base::string16
& title
,
51 base::Time time
, const std::string
& client_id
,
52 bool is_search_result
, const base::string16
& snippet
,
53 bool blocked_visit
, const std::string
& accept_languages
);
55 virtual ~HistoryEntry();
57 // Formats this entry's URL and title and adds them to |result|.
58 void SetUrlAndTitle(base::DictionaryValue
* result
) const;
60 // Converts the entry to a DictionaryValue to be owned by the caller.
61 scoped_ptr
<base::DictionaryValue
> ToValue(
62 bookmarks::BookmarkModel
* bookmark_model
,
63 SupervisedUserService
* supervised_user_service
,
64 const ProfileSyncService
* sync_service
) const;
66 // Comparison function for sorting HistoryEntries from newest to oldest.
67 static bool SortByTimeDescending(
68 const HistoryEntry
& entry1
, const HistoryEntry
& entry2
);
70 // The type of visits this entry represents: local, remote, or both.
74 base::string16 title
; // Title of the entry. May be empty.
76 // The time of the entry. Usually this will be the time of the most recent
77 // visit to |url| on a particular day as defined in the local timezone.
80 // The sync ID of the client on which the most recent visit occurred.
81 std::string client_id
;
83 // Timestamps of all local or remote visits the same URL on the same day.
84 std::set
<int64
> all_timestamps
;
86 // If true, this entry is a search result.
87 bool is_search_result
;
89 // The entry's search snippet, if this entry is a search result.
90 base::string16 snippet
;
92 // Whether this entry was blocked when it was attempted.
95 // kAcceptLanguages pref value.
96 std::string accept_languages
;
99 BrowsingHistoryHandler();
100 ~BrowsingHistoryHandler() override
;
102 // WebUIMessageHandler implementation.
103 void RegisterMessages() override
;
105 // Handler for the "queryHistory" message.
106 void HandleQueryHistory(const base::ListValue
* args
);
108 // Handler for the "removeVisits" message.
109 void HandleRemoveVisits(const base::ListValue
* args
);
111 // Handler for "clearBrowsingData" message.
112 void HandleClearBrowsingData(const base::ListValue
* args
);
114 // Handler for "removeBookmark" message.
115 void HandleRemoveBookmark(const base::ListValue
* args
);
117 // Merges duplicate entries from the query results, only retaining the most
118 // recent visit to a URL on a particular day. That visit contains the
119 // timestamps of the other visits.
120 static void MergeDuplicateResults(
121 std::vector
<BrowsingHistoryHandler::HistoryEntry
>* results
);
124 // The range for which to return results:
125 // - ALLTIME: allows access to all the results in a paginated way.
126 // - WEEK: the last 7 days.
127 // - MONTH: the last calendar month.
134 // Core implementation of history querying.
135 void QueryHistory(base::string16 search_text
,
136 const history::QueryOptions
& options
);
138 // Combines the query results from the local history database and the history
139 // server, and sends the combined results to the front end.
140 void ReturnResultsToFrontEnd();
142 // Callback from |web_history_timer_| when a response from web history has
143 // not been received in time.
144 void WebHistoryTimeout();
146 // Callback from the history system when a history query has completed.
147 void QueryComplete(const base::string16
& search_text
,
148 const history::QueryOptions
& options
,
149 history::QueryResults
* results
);
151 // Callback from the WebHistoryService when a query has completed.
152 void WebHistoryQueryComplete(const base::string16
& search_text
,
153 const history::QueryOptions
& options
,
154 base::TimeTicks start_time
,
155 history::WebHistoryService::Request
* request
,
156 const base::DictionaryValue
* results_value
);
158 // Callback from the history system when visits were deleted.
159 void RemoveComplete();
161 // Callback from history server when visits were deleted.
162 void RemoveWebHistoryComplete(bool success
);
164 bool ExtractIntegerValueAtIndex(
165 const base::ListValue
* value
, int index
, int* out_int
);
167 // Sets the query options for a week-wide query, |offset| weeks ago.
168 void SetQueryTimeInWeeks(int offset
, history::QueryOptions
* options
);
170 // Sets the query options for a monthly query, |offset| months ago.
171 void SetQueryTimeInMonths(int offset
, history::QueryOptions
* options
);
173 // kAcceptLanguages pref value.
174 std::string
GetAcceptLanguages() const;
176 // history::HistoryServiceObserver:
177 void OnURLsDeleted(history::HistoryService
* history_service
,
180 const history::URLRows
& deleted_rows
,
181 const std::set
<GURL
>& favicon_urls
) override
;
183 // Tracker for search requests to the history service.
184 base::CancelableTaskTracker query_task_tracker_
;
186 // The currently-executing request for synced history results.
187 // Deleting the request will cancel it.
188 scoped_ptr
<history::WebHistoryService::Request
> web_history_request_
;
190 // True if there is a pending delete requests to the history service.
191 bool has_pending_delete_request_
;
193 // Tracker for delete requests to the history service.
194 base::CancelableTaskTracker delete_task_tracker_
;
196 // The list of URLs that are in the process of being deleted.
197 std::set
<GURL
> urls_to_be_deleted_
;
199 // The info value that is returned to the front end with the query results.
200 base::DictionaryValue results_info_value_
;
202 // The list of query results received from the history service.
203 std::vector
<HistoryEntry
> query_results_
;
205 // The list of query results received from the history server.
206 std::vector
<HistoryEntry
> web_history_query_results_
;
208 // Timer used to implement a timeout on a Web History response.
209 base::OneShotTimer
<BrowsingHistoryHandler
> web_history_timer_
;
211 ScopedObserver
<history::HistoryService
, history::HistoryServiceObserver
>
212 history_service_observer_
;
214 base::WeakPtrFactory
<BrowsingHistoryHandler
> weak_factory_
;
216 DISALLOW_COPY_AND_ASSIGN(BrowsingHistoryHandler
);
219 class HistoryUI
: public content::WebUIController
{
221 explicit HistoryUI(content::WebUI
* web_ui
);
223 static base::RefCountedMemory
* GetFaviconResourceBytes(
224 ui::ScaleFactor scale_factor
);
227 DISALLOW_COPY_AND_ASSIGN(HistoryUI
);
230 #endif // CHROME_BROWSER_UI_WEBUI_HISTORY_UI_H_