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/strings/string16.h"
12 #include "base/task/cancelable_task_tracker.h"
13 #include "base/timer/timer.h"
14 #include "base/values.h"
15 #include "chrome/browser/common/cancelable_request.h"
16 #include "chrome/browser/history/history_service.h"
17 #include "chrome/browser/history/web_history_service.h"
18 #include "content/public/browser/notification_registrar.h"
19 #include "content/public/browser/web_ui_controller.h"
20 #include "content/public/browser/web_ui_message_handler.h"
23 class ManagedUserService
;
24 class ProfileSyncService
;
26 // The handler for Javascript messages related to the "history" view.
27 class BrowsingHistoryHandler
: public content::WebUIMessageHandler
,
28 public content::NotificationObserver
{
30 // Represents a history entry to be shown to the user, representing either
31 // a local or remote visit. A single entry can represent multiple visits,
32 // since only the most recent visit on a particular day is shown.
34 // Values indicating whether an entry represents only local visits, only
35 // remote visits, or a mixture of both.
43 HistoryEntry(EntryType type
, const GURL
& url
, const base::string16
& title
,
44 base::Time time
, const std::string
& client_id
,
45 bool is_search_result
, const base::string16
& snippet
,
46 bool blocked_visit
, const std::string
& accept_languages
);
48 virtual ~HistoryEntry();
50 // Formats this entry's URL and title and adds them to |result|.
51 void SetUrlAndTitle(base::DictionaryValue
* result
) const;
53 // Converts the entry to a DictionaryValue to be owned by the caller.
54 scoped_ptr
<base::DictionaryValue
> ToValue(
55 BookmarkModel
* bookmark_model
,
56 ManagedUserService
* managed_user_service
,
57 const ProfileSyncService
* sync_service
) const;
59 // Comparison function for sorting HistoryEntries from newest to oldest.
60 static bool SortByTimeDescending(
61 const HistoryEntry
& entry1
, const HistoryEntry
& entry2
);
63 // The type of visits this entry represents: local, remote, or both.
67 base::string16 title
; // Title of the entry. May be empty.
69 // The time of the entry. Usually this will be the time of the most recent
70 // visit to |url| on a particular day as defined in the local timezone.
73 // The sync ID of the client on which the most recent visit occurred.
74 std::string client_id
;
76 // Timestamps of all local or remote visits the same URL on the same day.
77 std::set
<int64
> all_timestamps
;
79 // If true, this entry is a search result.
80 bool is_search_result
;
82 // The entry's search snippet, if this entry is a search result.
83 base::string16 snippet
;
85 // Whether this entry was blocked when it was attempted.
88 // kAcceptLanguages pref value.
89 std::string accept_languages
;
92 BrowsingHistoryHandler();
93 virtual ~BrowsingHistoryHandler();
95 // WebUIMessageHandler implementation.
96 virtual void RegisterMessages() OVERRIDE
;
98 // Handler for the "queryHistory" message.
99 void HandleQueryHistory(const base::ListValue
* args
);
101 // Handler for the "removeVisits" message.
102 void HandleRemoveVisits(const base::ListValue
* args
);
104 // Handler for "clearBrowsingData" message.
105 void HandleClearBrowsingData(const base::ListValue
* args
);
107 // Handler for "removeBookmark" message.
108 void HandleRemoveBookmark(const base::ListValue
* args
);
110 // content::NotificationObserver implementation.
111 virtual void Observe(int type
,
112 const content::NotificationSource
& source
,
113 const content::NotificationDetails
& details
) OVERRIDE
;
115 // Merges duplicate entries from the query results, only retaining the most
116 // recent visit to a URL on a particular day. That visit contains the
117 // timestamps of the other visits.
118 static void MergeDuplicateResults(
119 std::vector
<BrowsingHistoryHandler::HistoryEntry
>* results
);
122 // The range for which to return results:
123 // - ALLTIME: allows access to all the results in a paginated way.
124 // - WEEK: the last 7 days.
125 // - MONTH: the last calendar month.
132 // Core implementation of history querying.
133 void QueryHistory(base::string16 search_text
,
134 const history::QueryOptions
& options
);
136 // Combines the query results from the local history database and the history
137 // server, and sends the combined results to the front end.
138 void ReturnResultsToFrontEnd();
140 // Callback from |web_history_timer_| when a response from web history has
141 // not been received in time.
142 void WebHistoryTimeout();
144 // Callback from the history system when a history query has completed.
145 void QueryComplete(const base::string16
& search_text
,
146 const history::QueryOptions
& options
,
147 HistoryService::Handle request_handle
,
148 history::QueryResults
* results
);
150 // Callback from the WebHistoryService when a query has completed.
151 void WebHistoryQueryComplete(const base::string16
& search_text
,
152 const history::QueryOptions
& options
,
153 base::TimeTicks start_time
,
154 history::WebHistoryService::Request
* request
,
155 const base::DictionaryValue
* results_value
);
157 // Callback from the history system when visits were deleted.
158 void RemoveComplete();
160 // Callback from history server when visits were deleted.
161 void RemoveWebHistoryComplete(bool success
);
163 bool ExtractIntegerValueAtIndex(
164 const base::ListValue
* value
, int index
, int* out_int
);
166 // Sets the query options for a week-wide query, |offset| weeks ago.
167 void SetQueryTimeInWeeks(int offset
, history::QueryOptions
* options
);
169 // Sets the query options for a monthly query, |offset| months ago.
170 void SetQueryTimeInMonths(int offset
, history::QueryOptions
* options
);
172 // kAcceptLanguages pref value.
173 std::string
GetAcceptLanguages() const;
175 content::NotificationRegistrar registrar_
;
177 // Consumer for search requests to the history service.
178 CancelableRequestConsumerT
<int, 0> history_request_consumer_
;
180 // The currently-executing request for synced history results.
181 // Deleting the request will cancel it.
182 scoped_ptr
<history::WebHistoryService::Request
> web_history_request_
;
184 // True if there is a pending delete requests to the history service.
185 bool has_pending_delete_request_
;
187 // Tracker for delete requests to the history service.
188 base::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 base::WeakPtrFactory
<BrowsingHistoryHandler
> weak_factory_
;
207 DISALLOW_COPY_AND_ASSIGN(BrowsingHistoryHandler
);
210 class HistoryUI
: public content::WebUIController
{
212 explicit HistoryUI(content::WebUI
* web_ui
);
214 static base::RefCountedMemory
* GetFaviconResourceBytes(
215 ui::ScaleFactor scale_factor
);
218 DISALLOW_COPY_AND_ASSIGN(HistoryUI
);
221 #endif // CHROME_BROWSER_UI_WEBUI_HISTORY_UI_H_