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. 4 5#ifndef CHROME_BROWSER_UI_WEBUI_HISTORY_UI_H_ 6#define CHROME_BROWSER_UI_WEBUI_HISTORY_UI_H_ 7 8#include <string> 9 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" 21 22class BookmarkModel; 23class ProfileSyncService; 24class SupervisedUserService; 25 26// The handler for Javascript messages related to the "history" view. 27class BrowsingHistoryHandler : public content::WebUIMessageHandler, 28 public content::NotificationObserver { 29 public: 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. 33 struct HistoryEntry { 34 // Values indicating whether an entry represents only local visits, only 35 // remote visits, or a mixture of both. 36 enum EntryType { 37 EMPTY_ENTRY = 0, 38 LOCAL_ENTRY, 39 REMOTE_ENTRY, 40 COMBINED_ENTRY 41 }; 42 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); 47 HistoryEntry(); 48 virtual ~HistoryEntry(); 49 50 // Formats this entry's URL and title and adds them to |result|. 51 void SetUrlAndTitle(base::DictionaryValue* result) const; 52 53 // Converts the entry to a DictionaryValue to be owned by the caller. 54 scoped_ptr<base::DictionaryValue> ToValue( 55 BookmarkModel* bookmark_model, 56 SupervisedUserService* supervised_user_service, 57 const ProfileSyncService* sync_service) const; 58 59 // Comparison function for sorting HistoryEntries from newest to oldest. 60 static bool SortByTimeDescending( 61 const HistoryEntry& entry1, const HistoryEntry& entry2); 62 63 // The type of visits this entry represents: local, remote, or both. 64 EntryType entry_type; 65 66 GURL url; 67 base::string16 title; // Title of the entry. May be empty. 68 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. 71 base::Time time; 72 73 // The sync ID of the client on which the most recent visit occurred. 74 std::string client_id; 75 76 // Timestamps of all local or remote visits the same URL on the same day. 77 std::set<int64> all_timestamps; 78 79 // If true, this entry is a search result. 80 bool is_search_result; 81 82 // The entry's search snippet, if this entry is a search result. 83 base::string16 snippet; 84 85 // Whether this entry was blocked when it was attempted. 86 bool blocked_visit; 87 88 // kAcceptLanguages pref value. 89 std::string accept_languages; 90 }; 91 92 BrowsingHistoryHandler(); 93 virtual ~BrowsingHistoryHandler(); 94 95 // WebUIMessageHandler implementation. 96 virtual void RegisterMessages() OVERRIDE; 97 98 // Handler for the "queryHistory" message. 99 void HandleQueryHistory(const base::ListValue* args); 100 101 // Handler for the "removeVisits" message. 102 void HandleRemoveVisits(const base::ListValue* args); 103 104 // Handler for "clearBrowsingData" message. 105 void HandleClearBrowsingData(const base::ListValue* args); 106 107 // Handler for "removeBookmark" message. 108 void HandleRemoveBookmark(const base::ListValue* args); 109 110 // content::NotificationObserver implementation. 111 virtual void Observe(int type, 112 const content::NotificationSource& source, 113 const content::NotificationDetails& details) OVERRIDE; 114 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); 120 121 private: 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. 126 enum Range { 127 ALL_TIME = 0, 128 WEEK = 1, 129 MONTH = 2 130 }; 131 132 // Core implementation of history querying. 133 void QueryHistory(base::string16 search_text, 134 const history::QueryOptions& options); 135 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(); 139 140 // Callback from |web_history_timer_| when a response from web history has 141 // not been received in time. 142 void WebHistoryTimeout(); 143 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); 149 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); 156 157 // Callback from the history system when visits were deleted. 158 void RemoveComplete(); 159 160 // Callback from history server when visits were deleted. 161 void RemoveWebHistoryComplete(bool success); 162 163 bool ExtractIntegerValueAtIndex( 164 const base::ListValue* value, int index, int* out_int); 165 166 // Sets the query options for a week-wide query, |offset| weeks ago. 167 void SetQueryTimeInWeeks(int offset, history::QueryOptions* options); 168 169 // Sets the query options for a monthly query, |offset| months ago. 170 void SetQueryTimeInMonths(int offset, history::QueryOptions* options); 171 172 // kAcceptLanguages pref value. 173 std::string GetAcceptLanguages() const; 174 175 content::NotificationRegistrar registrar_; 176 177 // Consumer for search requests to the history service. 178 CancelableRequestConsumerT<int, 0> history_request_consumer_; 179 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_; 183 184 // True if there is a pending delete requests to the history service. 185 bool has_pending_delete_request_; 186 187 // Tracker for delete requests to the history service. 188 base::CancelableTaskTracker delete_task_tracker_; 189 190 // The list of URLs that are in the process of being deleted. 191 std::set<GURL> urls_to_be_deleted_; 192 193 // The info value that is returned to the front end with the query results. 194 base::DictionaryValue results_info_value_; 195 196 // The list of query results received from the history service. 197 std::vector<HistoryEntry> query_results_; 198 199 // The list of query results received from the history server. 200 std::vector<HistoryEntry> web_history_query_results_; 201 202 // Timer used to implement a timeout on a Web History response. 203 base::OneShotTimer<BrowsingHistoryHandler> web_history_timer_; 204 205 base::WeakPtrFactory<BrowsingHistoryHandler> weak_factory_; 206 207 DISALLOW_COPY_AND_ASSIGN(BrowsingHistoryHandler); 208}; 209 210class HistoryUI : public content::WebUIController { 211 public: 212 explicit HistoryUI(content::WebUI* web_ui); 213 214 static base::RefCountedMemory* GetFaviconResourceBytes( 215 ui::ScaleFactor scale_factor); 216 217 private: 218 DISALLOW_COPY_AND_ASSIGN(HistoryUI); 219}; 220 221#endif // CHROME_BROWSER_UI_WEBUI_HISTORY_UI_H_ 222