| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CHROME_BROWSER_UI_WEBUI_HISTORY_UI_H_ | 5 #ifndef CHROME_BROWSER_UI_WEBUI_HISTORY_UI_H_ |
| 6 #define CHROME_BROWSER_UI_WEBUI_HISTORY_UI_H_ | 6 #define CHROME_BROWSER_UI_WEBUI_HISTORY_UI_H_ |
| 7 | 7 |
| 8 #include <string> | |
| 9 | |
| 10 #include "base/memory/scoped_ptr.h" | |
| 11 #include "base/memory/weak_ptr.h" | |
| 12 #include "base/scoped_observer.h" | |
| 13 #include "base/strings/string16.h" | |
| 14 #include "base/task/cancelable_task_tracker.h" | |
| 15 #include "base/timer/timer.h" | |
| 16 #include "base/values.h" | |
| 17 #include "components/history/core/browser/history_service_observer.h" | |
| 18 #include "components/history/core/browser/web_history_service.h" | |
| 19 #include "content/public/browser/web_ui_controller.h" | 8 #include "content/public/browser/web_ui_controller.h" |
| 20 #include "content/public/browser/web_ui_message_handler.h" | |
| 21 #include "ui/base/layout.h" | 9 #include "ui/base/layout.h" |
| 22 | 10 |
| 23 class ProfileSyncService; | 11 namespace base { |
| 24 class SupervisedUserService; | 12 class RefCountedMemory; |
| 25 | |
| 26 namespace bookmarks { | |
| 27 class BookmarkModel; | |
| 28 } | 13 } |
| 29 | 14 |
| 30 namespace history { | |
| 31 class HistoryService; | |
| 32 } | |
| 33 | |
| 34 // The handler for Javascript messages related to the "history" view. | |
| 35 class BrowsingHistoryHandler : public content::WebUIMessageHandler, | |
| 36 public history::HistoryServiceObserver { | |
| 37 public: | |
| 38 // Represents a history entry to be shown to the user, representing either | |
| 39 // a local or remote visit. A single entry can represent multiple visits, | |
| 40 // since only the most recent visit on a particular day is shown. | |
| 41 struct HistoryEntry { | |
| 42 // Values indicating whether an entry represents only local visits, only | |
| 43 // remote visits, or a mixture of both. | |
| 44 enum EntryType { | |
| 45 EMPTY_ENTRY = 0, | |
| 46 LOCAL_ENTRY, | |
| 47 REMOTE_ENTRY, | |
| 48 COMBINED_ENTRY | |
| 49 }; | |
| 50 | |
| 51 HistoryEntry(EntryType type, const GURL& url, const base::string16& title, | |
| 52 base::Time time, const std::string& client_id, | |
| 53 bool is_search_result, const base::string16& snippet, | |
| 54 bool blocked_visit, const std::string& accept_languages); | |
| 55 HistoryEntry(); | |
| 56 virtual ~HistoryEntry(); | |
| 57 | |
| 58 // Formats this entry's URL and title and adds them to |result|. | |
| 59 void SetUrlAndTitle(base::DictionaryValue* result) const; | |
| 60 | |
| 61 // Converts the entry to a DictionaryValue to be owned by the caller. | |
| 62 scoped_ptr<base::DictionaryValue> ToValue( | |
| 63 bookmarks::BookmarkModel* bookmark_model, | |
| 64 SupervisedUserService* supervised_user_service, | |
| 65 const ProfileSyncService* sync_service) const; | |
| 66 | |
| 67 // Comparison function for sorting HistoryEntries from newest to oldest. | |
| 68 static bool SortByTimeDescending( | |
| 69 const HistoryEntry& entry1, const HistoryEntry& entry2); | |
| 70 | |
| 71 // The type of visits this entry represents: local, remote, or both. | |
| 72 EntryType entry_type; | |
| 73 | |
| 74 GURL url; | |
| 75 base::string16 title; // Title of the entry. May be empty. | |
| 76 | |
| 77 // The time of the entry. Usually this will be the time of the most recent | |
| 78 // visit to |url| on a particular day as defined in the local timezone. | |
| 79 base::Time time; | |
| 80 | |
| 81 // The sync ID of the client on which the most recent visit occurred. | |
| 82 std::string client_id; | |
| 83 | |
| 84 // Timestamps of all local or remote visits the same URL on the same day. | |
| 85 std::set<int64> all_timestamps; | |
| 86 | |
| 87 // If true, this entry is a search result. | |
| 88 bool is_search_result; | |
| 89 | |
| 90 // The entry's search snippet, if this entry is a search result. | |
| 91 base::string16 snippet; | |
| 92 | |
| 93 // Whether this entry was blocked when it was attempted. | |
| 94 bool blocked_visit; | |
| 95 | |
| 96 // kAcceptLanguages pref value. | |
| 97 std::string accept_languages; | |
| 98 }; | |
| 99 | |
| 100 BrowsingHistoryHandler(); | |
| 101 ~BrowsingHistoryHandler() override; | |
| 102 | |
| 103 // WebUIMessageHandler implementation. | |
| 104 void RegisterMessages() override; | |
| 105 | |
| 106 // Handler for the "queryHistory" message. | |
| 107 void HandleQueryHistory(const base::ListValue* args); | |
| 108 | |
| 109 // Handler for the "removeVisits" message. | |
| 110 void HandleRemoveVisits(const base::ListValue* args); | |
| 111 | |
| 112 // Handler for "clearBrowsingData" message. | |
| 113 void HandleClearBrowsingData(const base::ListValue* args); | |
| 114 | |
| 115 // Handler for "removeBookmark" message. | |
| 116 void HandleRemoveBookmark(const base::ListValue* args); | |
| 117 | |
| 118 // Merges duplicate entries from the query results, only retaining the most | |
| 119 // recent visit to a URL on a particular day. That visit contains the | |
| 120 // timestamps of the other visits. | |
| 121 static void MergeDuplicateResults( | |
| 122 std::vector<BrowsingHistoryHandler::HistoryEntry>* results); | |
| 123 | |
| 124 private: | |
| 125 // The range for which to return results: | |
| 126 // - ALLTIME: allows access to all the results in a paginated way. | |
| 127 // - WEEK: the last 7 days. | |
| 128 // - MONTH: the last calendar month. | |
| 129 enum Range { | |
| 130 ALL_TIME = 0, | |
| 131 WEEK = 1, | |
| 132 MONTH = 2 | |
| 133 }; | |
| 134 | |
| 135 // Core implementation of history querying. | |
| 136 void QueryHistory(const base::string16& search_text, | |
| 137 const history::QueryOptions& options); | |
| 138 | |
| 139 // Combines the query results from the local history database and the history | |
| 140 // server, and sends the combined results to the front end. | |
| 141 void ReturnResultsToFrontEnd(); | |
| 142 | |
| 143 // Callback from |web_history_timer_| when a response from web history has | |
| 144 // not been received in time. | |
| 145 void WebHistoryTimeout(); | |
| 146 | |
| 147 // Callback from the history system when a history query has completed. | |
| 148 void QueryComplete(const base::string16& search_text, | |
| 149 const history::QueryOptions& options, | |
| 150 history::QueryResults* results); | |
| 151 | |
| 152 // Callback from the WebHistoryService when a query has completed. | |
| 153 void WebHistoryQueryComplete(const base::string16& search_text, | |
| 154 const history::QueryOptions& options, | |
| 155 base::TimeTicks start_time, | |
| 156 history::WebHistoryService::Request* request, | |
| 157 const base::DictionaryValue* results_value); | |
| 158 | |
| 159 // Callback from the history system when visits were deleted. | |
| 160 void RemoveComplete(); | |
| 161 | |
| 162 // Callback from history server when visits were deleted. | |
| 163 void RemoveWebHistoryComplete(bool success); | |
| 164 | |
| 165 bool ExtractIntegerValueAtIndex( | |
| 166 const base::ListValue* value, int index, int* out_int); | |
| 167 | |
| 168 // Sets the query options for a week-wide query, |offset| weeks ago. | |
| 169 void SetQueryTimeInWeeks(int offset, history::QueryOptions* options); | |
| 170 | |
| 171 // Sets the query options for a monthly query, |offset| months ago. | |
| 172 void SetQueryTimeInMonths(int offset, history::QueryOptions* options); | |
| 173 | |
| 174 // kAcceptLanguages pref value. | |
| 175 std::string GetAcceptLanguages() const; | |
| 176 | |
| 177 // history::HistoryServiceObserver: | |
| 178 void OnURLsDeleted(history::HistoryService* history_service, | |
| 179 bool all_history, | |
| 180 bool expired, | |
| 181 const history::URLRows& deleted_rows, | |
| 182 const std::set<GURL>& favicon_urls) override; | |
| 183 | |
| 184 // Tracker for search requests to the history service. | |
| 185 base::CancelableTaskTracker query_task_tracker_; | |
| 186 | |
| 187 // The currently-executing request for synced history results. | |
| 188 // Deleting the request will cancel it. | |
| 189 scoped_ptr<history::WebHistoryService::Request> web_history_request_; | |
| 190 | |
| 191 // True if there is a pending delete requests to the history service. | |
| 192 bool has_pending_delete_request_; | |
| 193 | |
| 194 // Tracker for delete requests to the history service. | |
| 195 base::CancelableTaskTracker delete_task_tracker_; | |
| 196 | |
| 197 // The list of URLs that are in the process of being deleted. | |
| 198 std::set<GURL> urls_to_be_deleted_; | |
| 199 | |
| 200 // The info value that is returned to the front end with the query results. | |
| 201 base::DictionaryValue results_info_value_; | |
| 202 | |
| 203 // The list of query results received from the history service. | |
| 204 std::vector<HistoryEntry> query_results_; | |
| 205 | |
| 206 // The list of query results received from the history server. | |
| 207 std::vector<HistoryEntry> web_history_query_results_; | |
| 208 | |
| 209 // Timer used to implement a timeout on a Web History response. | |
| 210 base::OneShotTimer web_history_timer_; | |
| 211 | |
| 212 ScopedObserver<history::HistoryService, history::HistoryServiceObserver> | |
| 213 history_service_observer_; | |
| 214 | |
| 215 base::WeakPtrFactory<BrowsingHistoryHandler> weak_factory_; | |
| 216 | |
| 217 DISALLOW_COPY_AND_ASSIGN(BrowsingHistoryHandler); | |
| 218 }; | |
| 219 | |
| 220 class HistoryUI : public content::WebUIController { | 15 class HistoryUI : public content::WebUIController { |
| 221 public: | 16 public: |
| 222 explicit HistoryUI(content::WebUI* web_ui); | 17 explicit HistoryUI(content::WebUI* web_ui); |
| 223 ~HistoryUI() override; | 18 ~HistoryUI() override; |
| 224 | 19 |
| 225 static base::RefCountedMemory* GetFaviconResourceBytes( | 20 static base::RefCountedMemory* GetFaviconResourceBytes( |
| 226 ui::ScaleFactor scale_factor); | 21 ui::ScaleFactor scale_factor); |
| 227 | 22 |
| 228 private: | 23 private: |
| 229 DISALLOW_COPY_AND_ASSIGN(HistoryUI); | 24 DISALLOW_COPY_AND_ASSIGN(HistoryUI); |
| 230 }; | 25 }; |
| 231 | 26 |
| 232 #endif // CHROME_BROWSER_UI_WEBUI_HISTORY_UI_H_ | 27 #endif // CHROME_BROWSER_UI_WEBUI_HISTORY_UI_H_ |
| OLD | NEW |