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"
23 class ProfileSyncService
;
24 class SupervisedUserService
;
30 // The handler for Javascript messages related to the "history" view.
31 class BrowsingHistoryHandler
: public content::WebUIMessageHandler
,
32 public history::HistoryServiceObserver
{
34 // Represents a history entry to be shown to the user, representing either
35 // a local or remote visit. A single entry can represent multiple visits,
36 // since only the most recent visit on a particular day is shown.
38 // Values indicating whether an entry represents only local visits, only
39 // remote visits, or a mixture of both.
47 HistoryEntry(EntryType type
, const GURL
& url
, const base::string16
& title
,
48 base::Time time
, const std::string
& client_id
,
49 bool is_search_result
, const base::string16
& snippet
,
50 bool blocked_visit
, const std::string
& accept_languages
);
52 virtual ~HistoryEntry();
54 // Formats this entry's URL and title and adds them to |result|.
55 void SetUrlAndTitle(base::DictionaryValue
* result
) const;
57 // Converts the entry to a DictionaryValue to be owned by the caller.
58 scoped_ptr
<base::DictionaryValue
> ToValue(
59 bookmarks::BookmarkModel
* bookmark_model
,
60 SupervisedUserService
* supervised_user_service
,
61 const ProfileSyncService
* sync_service
) const;
63 // Comparison function for sorting HistoryEntries from newest to oldest.
64 static bool SortByTimeDescending(
65 const HistoryEntry
& entry1
, const HistoryEntry
& entry2
);
67 // The type of visits this entry represents: local, remote, or both.
71 base::string16 title
; // Title of the entry. May be empty.
73 // The time of the entry. Usually this will be the time of the most recent
74 // visit to |url| on a particular day as defined in the local timezone.
77 // The sync ID of the client on which the most recent visit occurred.
78 std::string client_id
;
80 // Timestamps of all local or remote visits the same URL on the same day.
81 std::set
<int64
> all_timestamps
;
83 // If true, this entry is a search result.
84 bool is_search_result
;
86 // The entry's search snippet, if this entry is a search result.
87 base::string16 snippet
;
89 // Whether this entry was blocked when it was attempted.
92 // kAcceptLanguages pref value.
93 std::string accept_languages
;
96 BrowsingHistoryHandler();
97 ~BrowsingHistoryHandler() override
;
99 // WebUIMessageHandler implementation.
100 void RegisterMessages() override
;
102 // Handler for the "queryHistory" message.
103 void HandleQueryHistory(const base::ListValue
* args
);
105 // Handler for the "removeVisits" message.
106 void HandleRemoveVisits(const base::ListValue
* args
);
108 // Handler for "clearBrowsingData" message.
109 void HandleClearBrowsingData(const base::ListValue
* args
);
111 // Handler for "removeBookmark" message.
112 void HandleRemoveBookmark(const base::ListValue
* args
);
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(base::string16 search_text
,
133 const history::QueryOptions
& options
);
135 // Combines the query results from the local history database and the history
136 // server, and sends the combined results to the front end.
137 void ReturnResultsToFrontEnd();
139 // Callback from |web_history_timer_| when a response from web history has
140 // not been received in time.
141 void WebHistoryTimeout();
143 // Callback from the history system when a history query has completed.
144 void QueryComplete(const base::string16
& search_text
,
145 const history::QueryOptions
& options
,
146 history::QueryResults
* results
);
148 // Callback from the WebHistoryService when a query has completed.
149 void WebHistoryQueryComplete(const base::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(bool success
);
161 bool ExtractIntegerValueAtIndex(
162 const base::ListValue
* value
, int index
, int* out_int
);
164 // Sets the query options for a week-wide query, |offset| weeks ago.
165 void SetQueryTimeInWeeks(int offset
, history::QueryOptions
* options
);
167 // Sets the query options for a monthly query, |offset| months ago.
168 void SetQueryTimeInMonths(int offset
, history::QueryOptions
* options
);
170 // kAcceptLanguages pref value.
171 std::string
GetAcceptLanguages() const;
173 // history::HistoryServiceObserver:
174 void OnURLsDeleted(HistoryService
* history_service
,
177 const history::URLRows
& deleted_rows
,
178 const std::set
<GURL
>& favicon_urls
) override
;
180 // Tracker for search requests to the history service.
181 base::CancelableTaskTracker query_task_tracker_
;
183 // The currently-executing request for synced history results.
184 // Deleting the request will cancel it.
185 scoped_ptr
<history::WebHistoryService::Request
> web_history_request_
;
187 // True if there is a pending delete requests to the history service.
188 bool has_pending_delete_request_
;
190 // Tracker for delete requests to the history service.
191 base::CancelableTaskTracker delete_task_tracker_
;
193 // The list of URLs that are in the process of being deleted.
194 std::set
<GURL
> urls_to_be_deleted_
;
196 // The info value that is returned to the front end with the query results.
197 base::DictionaryValue results_info_value_
;
199 // The list of query results received from the history service.
200 std::vector
<HistoryEntry
> query_results_
;
202 // The list of query results received from the history server.
203 std::vector
<HistoryEntry
> web_history_query_results_
;
205 // Timer used to implement a timeout on a Web History response.
206 base::OneShotTimer
<BrowsingHistoryHandler
> web_history_timer_
;
208 ScopedObserver
<HistoryService
, HistoryServiceObserver
>
209 history_service_observer_
;
211 base::WeakPtrFactory
<BrowsingHistoryHandler
> weak_factory_
;
213 DISALLOW_COPY_AND_ASSIGN(BrowsingHistoryHandler
);
216 class HistoryUI
: public content::WebUIController
{
218 explicit HistoryUI(content::WebUI
* web_ui
);
220 static base::RefCountedMemory
* GetFaviconResourceBytes(
221 ui::ScaleFactor scale_factor
);
224 DISALLOW_COPY_AND_ASSIGN(HistoryUI
);
227 #endif // CHROME_BROWSER_UI_WEBUI_HISTORY_UI_H_