Cast: Stop logging kVideoFrameSentToEncoder and rename a couple events.
[chromium-blink-merge.git] / chrome / browser / ui / webui / history_ui.cc
blob24f4f78671e7c73f9fb81899b81b8bd6376d2493
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 #include "chrome/browser/ui/webui/history_ui.h"
7 #include <set>
9 #include "base/bind.h"
10 #include "base/bind_helpers.h"
11 #include "base/command_line.h"
12 #include "base/i18n/rtl.h"
13 #include "base/i18n/time_formatting.h"
14 #include "base/memory/singleton.h"
15 #include "base/message_loop/message_loop.h"
16 #include "base/metrics/histogram.h"
17 #include "base/prefs/pref_service.h"
18 #include "base/strings/string16.h"
19 #include "base/strings/string_number_conversions.h"
20 #include "base/strings/utf_string_conversions.h"
21 #include "base/time/time.h"
22 #include "base/values.h"
23 #include "chrome/browser/bookmarks/bookmark_model_factory.h"
24 #include "chrome/browser/chrome_notification_types.h"
25 #include "chrome/browser/extensions/activity_log/activity_log.h"
26 #include "chrome/browser/history/history_notifications.h"
27 #include "chrome/browser/history/history_service_factory.h"
28 #include "chrome/browser/history/history_types.h"
29 #include "chrome/browser/history/web_history_service.h"
30 #include "chrome/browser/history/web_history_service_factory.h"
31 #include "chrome/browser/profiles/profile.h"
32 #include "chrome/browser/search/search.h"
33 #include "chrome/browser/sync/glue/device_info.h"
34 #include "chrome/browser/sync/profile_sync_service.h"
35 #include "chrome/browser/sync/profile_sync_service_factory.h"
36 #include "chrome/browser/ui/browser_finder.h"
37 #include "chrome/browser/ui/chrome_pages.h"
38 #include "chrome/browser/ui/webui/favicon_source.h"
39 #include "chrome/browser/ui/webui/metrics_handler.h"
40 #include "chrome/common/chrome_switches.h"
41 #include "chrome/common/pref_names.h"
42 #include "chrome/common/url_constants.h"
43 #include "components/bookmarks/core/browser/bookmark_model.h"
44 #include "components/bookmarks/core/browser/bookmark_utils.h"
45 #include "content/public/browser/notification_details.h"
46 #include "content/public/browser/notification_source.h"
47 #include "content/public/browser/url_data_source.h"
48 #include "content/public/browser/web_ui.h"
49 #include "content/public/browser/web_ui_data_source.h"
50 #include "grit/browser_resources.h"
51 #include "grit/generated_resources.h"
52 #include "grit/theme_resources.h"
53 #include "net/base/escape.h"
54 #include "net/base/net_util.h"
55 #include "sync/protocol/history_delete_directive_specifics.pb.h"
56 #include "ui/base/l10n/l10n_util.h"
57 #include "ui/base/l10n/time_format.h"
58 #include "ui/base/resource/resource_bundle.h"
60 #if defined(ENABLE_MANAGED_USERS)
61 #include "chrome/browser/managed_mode/managed_mode_navigation_observer.h"
62 #include "chrome/browser/managed_mode/managed_mode_url_filter.h"
63 #include "chrome/browser/managed_mode/managed_user_service.h"
64 #include "chrome/browser/managed_mode/managed_user_service_factory.h"
65 #endif
67 #if defined(OS_ANDROID)
68 #include "chrome/browser/ui/android/tab_model/tab_model.h"
69 #include "chrome/browser/ui/android/tab_model/tab_model_list.h"
70 #endif
72 #if !defined(OS_ANDROID) && !defined(OS_IOS)
73 #include "chrome/browser/ui/webui/ntp/foreign_session_handler.h"
74 #include "chrome/browser/ui/webui/ntp/ntp_login_handler.h"
75 #endif
77 static const char kStringsJsFile[] = "strings.js";
78 static const char kHistoryJsFile[] = "history.js";
79 static const char kOtherDevicesJsFile[] = "other_devices.js";
81 // The amount of time to wait for a response from the WebHistoryService.
82 static const int kWebHistoryTimeoutSeconds = 3;
84 namespace {
86 // Buckets for UMA histograms.
87 enum WebHistoryQueryBuckets {
88 WEB_HISTORY_QUERY_FAILED = 0,
89 WEB_HISTORY_QUERY_SUCCEEDED,
90 WEB_HISTORY_QUERY_TIMED_OUT,
91 NUM_WEB_HISTORY_QUERY_BUCKETS
94 #if defined(OS_MACOSX)
95 const char kIncognitoModeShortcut[] = "("
96 "\xE2\x87\xA7" // Shift symbol (U+21E7 'UPWARDS WHITE ARROW').
97 "\xE2\x8C\x98" // Command symbol (U+2318 'PLACE OF INTEREST SIGN').
98 "N)";
99 #elif defined(OS_WIN)
100 const char kIncognitoModeShortcut[] = "(Ctrl+Shift+N)";
101 #else
102 const char kIncognitoModeShortcut[] = "(Shift+Ctrl+N)";
103 #endif
105 // Identifiers for the type of device from which a history entry originated.
106 static const char kDeviceTypeLaptop[] = "laptop";
107 static const char kDeviceTypePhone[] = "phone";
108 static const char kDeviceTypeTablet[] = "tablet";
110 content::WebUIDataSource* CreateHistoryUIHTMLSource(Profile* profile) {
111 PrefService* prefs = profile->GetPrefs();
113 content::WebUIDataSource* source =
114 content::WebUIDataSource::Create(chrome::kChromeUIHistoryFrameHost);
115 source->AddBoolean("isUserSignedIn",
116 !prefs->GetString(prefs::kGoogleServicesUsername).empty());
117 source->AddLocalizedString("collapseSessionMenuItemText",
118 IDS_NEW_TAB_OTHER_SESSIONS_COLLAPSE_SESSION);
119 source->AddLocalizedString("expandSessionMenuItemText",
120 IDS_NEW_TAB_OTHER_SESSIONS_EXPAND_SESSION);
121 source->AddLocalizedString("restoreSessionMenuItemText",
122 IDS_NEW_TAB_OTHER_SESSIONS_OPEN_ALL);
123 source->AddLocalizedString("xMore", IDS_OTHER_DEVICES_X_MORE);
124 source->AddLocalizedString("loading", IDS_HISTORY_LOADING);
125 source->AddLocalizedString("title", IDS_HISTORY_TITLE);
126 source->AddLocalizedString("newest", IDS_HISTORY_NEWEST);
127 source->AddLocalizedString("newer", IDS_HISTORY_NEWER);
128 source->AddLocalizedString("older", IDS_HISTORY_OLDER);
129 source->AddLocalizedString("searchResultsFor", IDS_HISTORY_SEARCHRESULTSFOR);
130 source->AddLocalizedString("history", IDS_HISTORY_BROWSERESULTS);
131 source->AddLocalizedString("cont", IDS_HISTORY_CONTINUED);
132 source->AddLocalizedString("searchButton", IDS_HISTORY_SEARCH_BUTTON);
133 source->AddLocalizedString("noSearchResults", IDS_HISTORY_NO_SEARCH_RESULTS);
134 source->AddLocalizedString("noResults", IDS_HISTORY_NO_RESULTS);
135 source->AddLocalizedString("historyInterval", IDS_HISTORY_INTERVAL);
136 source->AddLocalizedString("removeSelected",
137 IDS_HISTORY_REMOVE_SELECTED_ITEMS);
138 source->AddLocalizedString("clearAllHistory",
139 IDS_HISTORY_OPEN_CLEAR_BROWSING_DATA_DIALOG);
140 source->AddString(
141 "deleteWarning",
142 l10n_util::GetStringFUTF16(IDS_HISTORY_DELETE_PRIOR_VISITS_WARNING,
143 base::UTF8ToUTF16(kIncognitoModeShortcut)));
144 source->AddLocalizedString("actionMenuDescription",
145 IDS_HISTORY_ACTION_MENU_DESCRIPTION);
146 source->AddLocalizedString("removeFromHistory", IDS_HISTORY_REMOVE_PAGE);
147 source->AddLocalizedString("moreFromSite", IDS_HISTORY_MORE_FROM_SITE);
148 source->AddLocalizedString("groupByDomainLabel", IDS_GROUP_BY_DOMAIN_LABEL);
149 source->AddLocalizedString("rangeLabel", IDS_HISTORY_RANGE_LABEL);
150 source->AddLocalizedString("rangeAllTime", IDS_HISTORY_RANGE_ALL_TIME);
151 source->AddLocalizedString("rangeWeek", IDS_HISTORY_RANGE_WEEK);
152 source->AddLocalizedString("rangeMonth", IDS_HISTORY_RANGE_MONTH);
153 source->AddLocalizedString("rangeToday", IDS_HISTORY_RANGE_TODAY);
154 source->AddLocalizedString("rangeNext", IDS_HISTORY_RANGE_NEXT);
155 source->AddLocalizedString("rangePrevious", IDS_HISTORY_RANGE_PREVIOUS);
156 source->AddLocalizedString("numberVisits", IDS_HISTORY_NUMBER_VISITS);
157 source->AddLocalizedString("filterAllowed", IDS_HISTORY_FILTER_ALLOWED);
158 source->AddLocalizedString("filterBlocked", IDS_HISTORY_FILTER_BLOCKED);
159 source->AddLocalizedString("inContentPack", IDS_HISTORY_IN_CONTENT_PACK);
160 source->AddLocalizedString("allowItems", IDS_HISTORY_FILTER_ALLOW_ITEMS);
161 source->AddLocalizedString("blockItems", IDS_HISTORY_FILTER_BLOCK_ITEMS);
162 source->AddLocalizedString("lockButton", IDS_HISTORY_LOCK_BUTTON);
163 source->AddLocalizedString("blockedVisitText",
164 IDS_HISTORY_BLOCKED_VISIT_TEXT);
165 source->AddLocalizedString("unlockButton", IDS_HISTORY_UNLOCK_BUTTON);
166 source->AddLocalizedString("hasSyncedResults",
167 IDS_HISTORY_HAS_SYNCED_RESULTS);
168 source->AddLocalizedString("noSyncedResults", IDS_HISTORY_NO_SYNCED_RESULTS);
169 source->AddLocalizedString("cancel", IDS_CANCEL);
170 source->AddLocalizedString("deleteConfirm",
171 IDS_HISTORY_DELETE_PRIOR_VISITS_CONFIRM_BUTTON);
172 source->AddBoolean("isFullHistorySyncEnabled",
173 WebHistoryServiceFactory::GetForProfile(profile) != NULL);
174 source->AddBoolean("groupByDomain",
175 CommandLine::ForCurrentProcess()->HasSwitch(
176 switches::kHistoryEnableGroupByDomain));
177 source->AddBoolean("allowDeletingHistory",
178 prefs->GetBoolean(prefs::kAllowDeletingBrowserHistory));
179 source->AddBoolean("isInstantExtendedApiEnabled",
180 chrome::IsInstantExtendedAPIEnabled());
182 source->SetJsonPath(kStringsJsFile);
183 source->AddResourcePath(kHistoryJsFile, IDR_HISTORY_JS);
184 source->AddResourcePath(kOtherDevicesJsFile, IDR_OTHER_DEVICES_JS);
185 source->SetDefaultResource(IDR_HISTORY_HTML);
186 source->SetUseJsonJSFormatV2();
187 source->DisableDenyXFrameOptions();
188 source->AddBoolean("isManagedProfile", profile->IsManaged());
189 source->AddBoolean("showDeleteVisitUI", !profile->IsManaged());
191 return source;
194 // Returns a localized version of |visit_time| including a relative
195 // indicator (e.g. today, yesterday).
196 base::string16 getRelativeDateLocalized(const base::Time& visit_time) {
197 base::Time midnight = base::Time::Now().LocalMidnight();
198 base::string16 date_str = ui::TimeFormat::RelativeDate(visit_time, &midnight);
199 if (date_str.empty()) {
200 date_str = base::TimeFormatFriendlyDate(visit_time);
201 } else {
202 date_str = l10n_util::GetStringFUTF16(
203 IDS_HISTORY_DATE_WITH_RELATIVE_TIME,
204 date_str,
205 base::TimeFormatFriendlyDate(visit_time));
207 return date_str;
211 // Sets the correct year when substracting months from a date.
212 void normalizeMonths(base::Time::Exploded* exploded) {
213 // Decrease a year at a time until we have a proper date.
214 while (exploded->month < 1) {
215 exploded->month += 12;
216 exploded->year--;
220 // Returns true if |entry| represents a local visit that had no corresponding
221 // visit on the server.
222 bool IsLocalOnlyResult(const BrowsingHistoryHandler::HistoryEntry& entry) {
223 return entry.entry_type == BrowsingHistoryHandler::HistoryEntry::LOCAL_ENTRY;
226 // Gets the name and type of a device for the given sync client ID.
227 // |name| and |type| are out parameters.
228 void GetDeviceNameAndType(const ProfileSyncService* sync_service,
229 const std::string& client_id,
230 std::string* name,
231 std::string* type) {
232 if (sync_service && sync_service->sync_initialized()) {
233 scoped_ptr<browser_sync::DeviceInfo> device_info =
234 sync_service->GetDeviceInfo(client_id);
235 if (device_info.get()) {
236 *name = device_info->client_name();
237 switch (device_info->device_type()) {
238 case sync_pb::SyncEnums::TYPE_PHONE:
239 *type = kDeviceTypePhone;
240 break;
241 case sync_pb::SyncEnums::TYPE_TABLET:
242 *type = kDeviceTypeTablet;
243 break;
244 default:
245 *type = kDeviceTypeLaptop;
247 return;
249 } else {
250 NOTREACHED() << "Got a remote history entry but no ProfileSyncService.";
252 *name = l10n_util::GetStringUTF8(IDS_HISTORY_UNKNOWN_DEVICE);
253 *type = kDeviceTypeLaptop;
256 } // namespace
258 ////////////////////////////////////////////////////////////////////////////////
260 // BrowsingHistoryHandler
262 ////////////////////////////////////////////////////////////////////////////////
264 BrowsingHistoryHandler::HistoryEntry::HistoryEntry(
265 BrowsingHistoryHandler::HistoryEntry::EntryType entry_type,
266 const GURL& url, const base::string16& title, base::Time time,
267 const std::string& client_id, bool is_search_result,
268 const base::string16& snippet, bool blocked_visit,
269 const std::string& accept_languages) {
270 this->entry_type = entry_type;
271 this->url = url;
272 this->title = title;
273 this->time = time;
274 this->client_id = client_id;
275 all_timestamps.insert(time.ToInternalValue());
276 this->is_search_result = is_search_result;
277 this->snippet = snippet;
278 this->blocked_visit = blocked_visit;
279 this->accept_languages = accept_languages;
282 BrowsingHistoryHandler::HistoryEntry::HistoryEntry()
283 : entry_type(EMPTY_ENTRY), is_search_result(false), blocked_visit(false) {
286 BrowsingHistoryHandler::HistoryEntry::~HistoryEntry() {
289 void BrowsingHistoryHandler::HistoryEntry::SetUrlAndTitle(
290 base::DictionaryValue* result) const {
291 result->SetString("url", url.spec());
293 bool using_url_as_the_title = false;
294 base::string16 title_to_set(title);
295 if (title.empty()) {
296 using_url_as_the_title = true;
297 title_to_set = base::UTF8ToUTF16(url.spec());
300 // Since the title can contain BiDi text, we need to mark the text as either
301 // RTL or LTR, depending on the characters in the string. If we use the URL
302 // as the title, we mark the title as LTR since URLs are always treated as
303 // left to right strings.
304 if (base::i18n::IsRTL()) {
305 if (using_url_as_the_title)
306 base::i18n::WrapStringWithLTRFormatting(&title_to_set);
307 else
308 base::i18n::AdjustStringForLocaleDirection(&title_to_set);
310 result->SetString("title", title_to_set);
313 scoped_ptr<base::DictionaryValue> BrowsingHistoryHandler::HistoryEntry::ToValue(
314 BookmarkModel* bookmark_model,
315 ManagedUserService* managed_user_service,
316 const ProfileSyncService* sync_service) const {
317 scoped_ptr<base::DictionaryValue> result(new base::DictionaryValue());
318 SetUrlAndTitle(result.get());
320 base::string16 domain = net::IDNToUnicode(url.host(), accept_languages);
321 // When the domain is empty, use the scheme instead. This allows for a
322 // sensible treatment of e.g. file: URLs when group by domain is on.
323 if (domain.empty())
324 domain = base::UTF8ToUTF16(url.scheme() + ":");
326 result->SetString("domain", domain);
327 result->SetDouble("time", time.ToJsTime());
329 // Pass the timestamps in a list.
330 scoped_ptr<base::ListValue> timestamps(new base::ListValue);
331 for (std::set<int64>::const_iterator it = all_timestamps.begin();
332 it != all_timestamps.end(); ++it) {
333 timestamps->AppendDouble(base::Time::FromInternalValue(*it).ToJsTime());
335 result->Set("allTimestamps", timestamps.release());
337 // Always pass the short date since it is needed both in the search and in
338 // the monthly view.
339 result->SetString("dateShort", base::TimeFormatShortDate(time));
341 // Only pass in the strings we need (search results need a shortdate
342 // and snippet, browse results need day and time information).
343 if (is_search_result) {
344 result->SetString("snippet", snippet);
345 } else {
346 base::Time midnight = base::Time::Now().LocalMidnight();
347 base::string16 date_str = ui::TimeFormat::RelativeDate(time, &midnight);
348 if (date_str.empty()) {
349 date_str = base::TimeFormatFriendlyDate(time);
350 } else {
351 date_str = l10n_util::GetStringFUTF16(
352 IDS_HISTORY_DATE_WITH_RELATIVE_TIME,
353 date_str,
354 base::TimeFormatFriendlyDate(time));
356 result->SetString("dateRelativeDay", date_str);
357 result->SetString("dateTimeOfDay", base::TimeFormatTimeOfDay(time));
359 result->SetBoolean("starred", bookmark_model->IsBookmarked(url));
361 std::string device_name;
362 std::string device_type;
363 if (!client_id.empty())
364 GetDeviceNameAndType(sync_service, client_id, &device_name, &device_type);
365 result->SetString("deviceName", device_name);
366 result->SetString("deviceType", device_type);
368 #if defined(ENABLE_MANAGED_USERS)
369 if (managed_user_service) {
370 const ManagedModeURLFilter* url_filter =
371 managed_user_service->GetURLFilterForUIThread();
372 int filtering_behavior =
373 url_filter->GetFilteringBehaviorForURL(url.GetWithEmptyPath());
374 result->SetInteger("hostFilteringBehavior", filtering_behavior);
376 result->SetBoolean("blockedVisit", blocked_visit);
378 #endif
380 return result.Pass();
383 bool BrowsingHistoryHandler::HistoryEntry::SortByTimeDescending(
384 const BrowsingHistoryHandler::HistoryEntry& entry1,
385 const BrowsingHistoryHandler::HistoryEntry& entry2) {
386 return entry1.time > entry2.time;
389 BrowsingHistoryHandler::BrowsingHistoryHandler()
390 : has_pending_delete_request_(false),
391 weak_factory_(this) {
394 BrowsingHistoryHandler::~BrowsingHistoryHandler() {
395 history_request_consumer_.CancelAllRequests();
396 web_history_request_.reset();
399 void BrowsingHistoryHandler::RegisterMessages() {
400 // Create our favicon data source.
401 Profile* profile = Profile::FromWebUI(web_ui());
402 content::URLDataSource::Add(
403 profile, new FaviconSource(profile, FaviconSource::ANY));
405 // Get notifications when history is cleared.
406 registrar_.Add(this, chrome::NOTIFICATION_HISTORY_URLS_DELETED,
407 content::Source<Profile>(profile->GetOriginalProfile()));
409 web_ui()->RegisterMessageCallback("queryHistory",
410 base::Bind(&BrowsingHistoryHandler::HandleQueryHistory,
411 base::Unretained(this)));
412 web_ui()->RegisterMessageCallback("removeVisits",
413 base::Bind(&BrowsingHistoryHandler::HandleRemoveVisits,
414 base::Unretained(this)));
415 web_ui()->RegisterMessageCallback("clearBrowsingData",
416 base::Bind(&BrowsingHistoryHandler::HandleClearBrowsingData,
417 base::Unretained(this)));
418 web_ui()->RegisterMessageCallback("removeBookmark",
419 base::Bind(&BrowsingHistoryHandler::HandleRemoveBookmark,
420 base::Unretained(this)));
423 bool BrowsingHistoryHandler::ExtractIntegerValueAtIndex(
424 const base::ListValue* value,
425 int index,
426 int* out_int) {
427 double double_value;
428 if (value->GetDouble(index, &double_value)) {
429 *out_int = static_cast<int>(double_value);
430 return true;
432 NOTREACHED();
433 return false;
436 void BrowsingHistoryHandler::WebHistoryTimeout() {
437 // TODO(dubroy): Communicate the failure to the front end.
438 if (!history_request_consumer_.HasPendingRequests())
439 ReturnResultsToFrontEnd();
441 UMA_HISTOGRAM_ENUMERATION(
442 "WebHistory.QueryCompletion",
443 WEB_HISTORY_QUERY_TIMED_OUT, NUM_WEB_HISTORY_QUERY_BUCKETS);
446 void BrowsingHistoryHandler::QueryHistory(
447 base::string16 search_text, const history::QueryOptions& options) {
448 Profile* profile = Profile::FromWebUI(web_ui());
450 // Anything in-flight is invalid.
451 history_request_consumer_.CancelAllRequests();
452 web_history_request_.reset();
454 query_results_.clear();
455 results_info_value_.Clear();
457 HistoryService* hs = HistoryServiceFactory::GetForProfile(
458 profile, Profile::EXPLICIT_ACCESS);
459 hs->QueryHistory(search_text,
460 options,
461 &history_request_consumer_,
462 base::Bind(&BrowsingHistoryHandler::QueryComplete,
463 base::Unretained(this), search_text, options));
465 history::WebHistoryService* web_history =
466 WebHistoryServiceFactory::GetForProfile(profile);
467 if (web_history) {
468 web_history_query_results_.clear();
469 web_history_request_ = web_history->QueryHistory(
470 search_text,
471 options,
472 base::Bind(&BrowsingHistoryHandler::WebHistoryQueryComplete,
473 base::Unretained(this),
474 search_text, options,
475 base::TimeTicks::Now()));
476 // Start a timer so we know when to give up.
477 web_history_timer_.Start(
478 FROM_HERE, base::TimeDelta::FromSeconds(kWebHistoryTimeoutSeconds),
479 this, &BrowsingHistoryHandler::WebHistoryTimeout);
481 // Set this to false until the results actually arrive.
482 results_info_value_.SetBoolean("hasSyncedResults", false);
486 void BrowsingHistoryHandler::HandleQueryHistory(const base::ListValue* args) {
487 history::QueryOptions options;
489 // Parse the arguments from JavaScript. There are five required arguments:
490 // - the text to search for (may be empty)
491 // - the offset from which the search should start (in multiples of week or
492 // month, set by the next argument).
493 // - the range (BrowsingHistoryHandler::Range) Enum value that sets the range
494 // of the query.
495 // - the end time for the query. Only results older than this time will be
496 // returned.
497 // - the maximum number of results to return (may be 0, meaning that there
498 // is no maximum).
499 base::string16 search_text = ExtractStringValue(args);
500 int offset;
501 if (!args->GetInteger(1, &offset)) {
502 NOTREACHED() << "Failed to convert argument 1. ";
503 return;
505 int range;
506 if (!args->GetInteger(2, &range)) {
507 NOTREACHED() << "Failed to convert argument 2. ";
508 return;
511 if (range == BrowsingHistoryHandler::MONTH)
512 SetQueryTimeInMonths(offset, &options);
513 else if (range == BrowsingHistoryHandler::WEEK)
514 SetQueryTimeInWeeks(offset, &options);
516 double end_time;
517 if (!args->GetDouble(3, &end_time)) {
518 NOTREACHED() << "Failed to convert argument 3. ";
519 return;
521 if (end_time)
522 options.end_time = base::Time::FromJsTime(end_time);
524 if (!ExtractIntegerValueAtIndex(args, 4, &options.max_count)) {
525 NOTREACHED() << "Failed to convert argument 4.";
526 return;
529 options.duplicate_policy = history::QueryOptions::REMOVE_DUPLICATES_PER_DAY;
530 QueryHistory(search_text, options);
533 void BrowsingHistoryHandler::HandleRemoveVisits(const base::ListValue* args) {
534 Profile* profile = Profile::FromWebUI(web_ui());
535 // TODO(davidben): history.js is not aware of this failure and will still
536 // override |deleteCompleteCallback_|.
537 if (delete_task_tracker_.HasTrackedTasks() ||
538 has_pending_delete_request_ ||
539 !profile->GetPrefs()->GetBoolean(prefs::kAllowDeletingBrowserHistory)) {
540 web_ui()->CallJavascriptFunction("deleteFailed");
541 return;
544 HistoryService* history_service =
545 HistoryServiceFactory::GetForProfile(profile, Profile::EXPLICIT_ACCESS);
546 history::WebHistoryService* web_history =
547 WebHistoryServiceFactory::GetForProfile(profile);
549 base::Time now = base::Time::Now();
550 std::vector<history::ExpireHistoryArgs> expire_list;
551 expire_list.reserve(args->GetSize());
553 DCHECK(urls_to_be_deleted_.empty());
554 for (base::ListValue::const_iterator it = args->begin();
555 it != args->end(); ++it) {
556 base::DictionaryValue* deletion = NULL;
557 base::string16 url;
558 base::ListValue* timestamps = NULL;
560 // Each argument is a dictionary with properties "url" and "timestamps".
561 if (!((*it)->GetAsDictionary(&deletion) &&
562 deletion->GetString("url", &url) &&
563 deletion->GetList("timestamps", &timestamps))) {
564 NOTREACHED() << "Unable to extract arguments";
565 return;
567 DCHECK(timestamps->GetSize() > 0);
569 // In order to ensure that visits will be deleted from the server and other
570 // clients (even if they are offline), create a sync delete directive for
571 // each visit to be deleted.
572 sync_pb::HistoryDeleteDirectiveSpecifics delete_directive;
573 sync_pb::GlobalIdDirective* global_id_directive =
574 delete_directive.mutable_global_id_directive();
576 double timestamp;
577 history::ExpireHistoryArgs* expire_args = NULL;
578 for (base::ListValue::const_iterator ts_iterator = timestamps->begin();
579 ts_iterator != timestamps->end(); ++ts_iterator) {
580 if (!(*ts_iterator)->GetAsDouble(&timestamp)) {
581 NOTREACHED() << "Unable to extract visit timestamp.";
582 continue;
584 base::Time visit_time = base::Time::FromJsTime(timestamp);
585 if (!expire_args) {
586 GURL gurl(url);
587 expire_list.resize(expire_list.size() + 1);
588 expire_args = &expire_list.back();
589 expire_args->SetTimeRangeForOneDay(visit_time);
590 expire_args->urls.insert(gurl);
591 urls_to_be_deleted_.insert(gurl);
593 // The local visit time is treated as a global ID for the visit.
594 global_id_directive->add_global_id(visit_time.ToInternalValue());
597 // Set the start and end time in microseconds since the Unix epoch.
598 global_id_directive->set_start_time_usec(
599 (expire_args->begin_time - base::Time::UnixEpoch()).InMicroseconds());
601 // Delete directives shouldn't have an end time in the future.
602 // TODO(dubroy): Use sane time (crbug.com/146090) here when it's ready.
603 base::Time end_time = std::min(expire_args->end_time, now);
605 // -1 because end time in delete directives is inclusive.
606 global_id_directive->set_end_time_usec(
607 (end_time - base::Time::UnixEpoch()).InMicroseconds() - 1);
609 // TODO(dubroy): Figure out the proper way to handle an error here.
610 if (web_history)
611 history_service->ProcessLocalDeleteDirective(delete_directive);
614 history_service->ExpireHistory(
615 expire_list,
616 base::Bind(&BrowsingHistoryHandler::RemoveComplete,
617 base::Unretained(this)),
618 &delete_task_tracker_);
620 if (web_history) {
621 has_pending_delete_request_ = true;
622 web_history->ExpireHistory(
623 expire_list,
624 base::Bind(&BrowsingHistoryHandler::RemoveWebHistoryComplete,
625 weak_factory_.GetWeakPtr()));
628 #if defined(ENABLE_EXTENSIONS)
629 // If the profile has activity logging enabled also clean up any URLs from
630 // the extension activity log. The extension activity log contains URLS
631 // which websites an extension has activity on so it will indirectly
632 // contain websites that a user has visited.
633 extensions::ActivityLog* activity_log =
634 extensions::ActivityLog::GetInstance(profile);
635 for (std::vector<history::ExpireHistoryArgs>::const_iterator it =
636 expire_list.begin(); it != expire_list.end(); ++it) {
637 activity_log->RemoveURLs(it->urls);
639 #endif
642 void BrowsingHistoryHandler::HandleClearBrowsingData(
643 const base::ListValue* args) {
644 #if defined(OS_ANDROID)
645 const TabModel* tab_model = TabModelList::GetTabModelForWebContents(
646 web_ui()->GetWebContents());
647 if (tab_model)
648 tab_model->OpenClearBrowsingData();
649 #else
650 // TODO(beng): This is an improper direct dependency on Browser. Route this
651 // through some sort of delegate.
652 Browser* browser = chrome::FindBrowserWithWebContents(
653 web_ui()->GetWebContents());
654 chrome::ShowClearBrowsingDataDialog(browser);
655 #endif
658 void BrowsingHistoryHandler::HandleRemoveBookmark(const base::ListValue* args) {
659 base::string16 url = ExtractStringValue(args);
660 Profile* profile = Profile::FromWebUI(web_ui());
661 BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile);
662 bookmark_utils::RemoveAllBookmarks(model, GURL(url));
665 // static
666 void BrowsingHistoryHandler::MergeDuplicateResults(
667 std::vector<BrowsingHistoryHandler::HistoryEntry>* results) {
668 std::vector<BrowsingHistoryHandler::HistoryEntry> new_results;
669 // Pre-reserve the size of the new vector. Since we're working with pointers
670 // later on not doing this could lead to the vector being resized and to
671 // pointers to invalid locations.
672 new_results.reserve(results->size());
673 // Maps a URL to the most recent entry on a particular day.
674 std::map<GURL,BrowsingHistoryHandler::HistoryEntry*>
675 current_day_entries;
677 // Keeps track of the day that |current_day_urls| is holding the URLs for,
678 // in order to handle removing per-day duplicates.
679 base::Time current_day_midnight;
681 std::sort(
682 results->begin(), results->end(), HistoryEntry::SortByTimeDescending);
684 for (std::vector<BrowsingHistoryHandler::HistoryEntry>::const_iterator it =
685 results->begin(); it != results->end(); ++it) {
686 // Reset the list of found URLs when a visit from a new day is encountered.
687 if (current_day_midnight != it->time.LocalMidnight()) {
688 current_day_entries.clear();
689 current_day_midnight = it->time.LocalMidnight();
692 // Keep this visit if it's the first visit to this URL on the current day.
693 if (current_day_entries.count(it->url) == 0) {
694 new_results.push_back(*it);
695 current_day_entries[it->url] = &new_results.back();
696 } else {
697 // Keep track of the timestamps of all visits to the URL on the same day.
698 BrowsingHistoryHandler::HistoryEntry* entry =
699 current_day_entries[it->url];
700 entry->all_timestamps.insert(
701 it->all_timestamps.begin(), it->all_timestamps.end());
703 if (entry->entry_type != it->entry_type) {
704 entry->entry_type =
705 BrowsingHistoryHandler::HistoryEntry::COMBINED_ENTRY;
709 results->swap(new_results);
712 void BrowsingHistoryHandler::ReturnResultsToFrontEnd() {
713 Profile* profile = Profile::FromWebUI(web_ui());
714 BookmarkModel* bookmark_model = BookmarkModelFactory::GetForProfile(profile);
715 ManagedUserService* managed_user_service = NULL;
716 #if defined(ENABLE_MANAGED_USERS)
717 if (profile->IsManaged())
718 managed_user_service = ManagedUserServiceFactory::GetForProfile(profile);
719 #endif
720 ProfileSyncService* sync_service =
721 ProfileSyncServiceFactory::GetInstance()->GetForProfile(profile);
723 // Combine the local and remote results into |query_results_|, and remove
724 // any duplicates.
725 if (!web_history_query_results_.empty()) {
726 int local_result_count = query_results_.size();
727 query_results_.insert(query_results_.end(),
728 web_history_query_results_.begin(),
729 web_history_query_results_.end());
730 MergeDuplicateResults(&query_results_);
732 if (local_result_count) {
733 // In the best case, we expect that all local results are duplicated on
734 // the server. Keep track of how many are missing.
735 int missing_count = std::count_if(
736 query_results_.begin(), query_results_.end(), IsLocalOnlyResult);
737 UMA_HISTOGRAM_PERCENTAGE("WebHistory.LocalResultMissingOnServer",
738 missing_count * 100.0 / local_result_count);
742 // Convert the result vector into a ListValue.
743 base::ListValue results_value;
744 for (std::vector<BrowsingHistoryHandler::HistoryEntry>::iterator it =
745 query_results_.begin(); it != query_results_.end(); ++it) {
746 scoped_ptr<base::Value> value(
747 it->ToValue(bookmark_model, managed_user_service, sync_service));
748 results_value.Append(value.release());
751 web_ui()->CallJavascriptFunction(
752 "historyResult", results_info_value_, results_value);
753 results_info_value_.Clear();
754 query_results_.clear();
755 web_history_query_results_.clear();
758 void BrowsingHistoryHandler::QueryComplete(
759 const base::string16& search_text,
760 const history::QueryOptions& options,
761 HistoryService::Handle request_handle,
762 history::QueryResults* results) {
763 DCHECK_EQ(0U, query_results_.size());
764 query_results_.reserve(results->size());
765 const std::string accept_languages = GetAcceptLanguages();
767 for (size_t i = 0; i < results->size(); ++i) {
768 history::URLResult const &page = (*results)[i];
769 // TODO(dubroy): Use sane time (crbug.com/146090) here when it's ready.
770 query_results_.push_back(
771 HistoryEntry(
772 HistoryEntry::LOCAL_ENTRY,
773 page.url(),
774 page.title(),
775 page.visit_time(),
776 std::string(),
777 !search_text.empty(),
778 page.snippet().text(),
779 page.blocked_visit(),
780 accept_languages));
783 results_info_value_.SetString("term", search_text);
784 results_info_value_.SetBoolean("finished", results->reached_beginning());
786 // Add the specific dates that were searched to display them.
787 // TODO(sergiu): Put today if the start is in the future.
788 results_info_value_.SetString("queryStartTime",
789 getRelativeDateLocalized(options.begin_time));
790 if (!options.end_time.is_null()) {
791 results_info_value_.SetString("queryEndTime",
792 getRelativeDateLocalized(options.end_time -
793 base::TimeDelta::FromDays(1)));
794 } else {
795 results_info_value_.SetString("queryEndTime",
796 getRelativeDateLocalized(base::Time::Now()));
798 if (!web_history_timer_.IsRunning())
799 ReturnResultsToFrontEnd();
802 void BrowsingHistoryHandler::WebHistoryQueryComplete(
803 const base::string16& search_text,
804 const history::QueryOptions& options,
805 base::TimeTicks start_time,
806 history::WebHistoryService::Request* request,
807 const base::DictionaryValue* results_value) {
808 base::TimeDelta delta = base::TimeTicks::Now() - start_time;
809 UMA_HISTOGRAM_TIMES("WebHistory.ResponseTime", delta);
810 const std::string accept_languages = GetAcceptLanguages();
812 // If the response came in too late, do nothing.
813 // TODO(dubroy): Maybe show a banner, and prompt the user to reload?
814 if (!web_history_timer_.IsRunning())
815 return;
816 web_history_timer_.Stop();
818 UMA_HISTOGRAM_ENUMERATION(
819 "WebHistory.QueryCompletion",
820 results_value ? WEB_HISTORY_QUERY_SUCCEEDED : WEB_HISTORY_QUERY_FAILED,
821 NUM_WEB_HISTORY_QUERY_BUCKETS);
823 DCHECK_EQ(0U, web_history_query_results_.size());
824 const base::ListValue* events = NULL;
825 if (results_value && results_value->GetList("event", &events)) {
826 web_history_query_results_.reserve(events->GetSize());
827 for (unsigned int i = 0; i < events->GetSize(); ++i) {
828 const base::DictionaryValue* event = NULL;
829 const base::DictionaryValue* result = NULL;
830 const base::ListValue* results = NULL;
831 const base::ListValue* ids = NULL;
832 base::string16 url;
833 base::string16 title;
834 base::Time visit_time;
836 if (!(events->GetDictionary(i, &event) &&
837 event->GetList("result", &results) &&
838 results->GetDictionary(0, &result) &&
839 result->GetString("url", &url) &&
840 result->GetList("id", &ids) &&
841 ids->GetSize() > 0)) {
842 LOG(WARNING) << "Improperly formed JSON response from history server.";
843 continue;
845 // Title is optional, so the return value is ignored here.
846 result->GetString("title", &title);
848 // Extract the timestamps of all the visits to this URL.
849 // They are referred to as "IDs" by the server.
850 for (int j = 0; j < static_cast<int>(ids->GetSize()); ++j) {
851 const base::DictionaryValue* id = NULL;
852 std::string timestamp_string;
853 int64 timestamp_usec;
855 if (!(ids->GetDictionary(j, &id) &&
856 id->GetString("timestamp_usec", &timestamp_string) &&
857 base::StringToInt64(timestamp_string, &timestamp_usec))) {
858 NOTREACHED() << "Unable to extract timestamp.";
859 continue;
861 // The timestamp on the server is a Unix time.
862 base::Time time = base::Time::UnixEpoch() +
863 base::TimeDelta::FromMicroseconds(timestamp_usec);
865 // Get the ID of the client that this visit came from.
866 std::string client_id;
867 id->GetString("client_id", &client_id);
869 web_history_query_results_.push_back(
870 HistoryEntry(
871 HistoryEntry::REMOTE_ENTRY,
872 GURL(url),
873 title,
874 time,
875 client_id,
876 !search_text.empty(),
877 base::string16(),
878 /* blocked_visit */ false,
879 accept_languages));
882 } else if (results_value) {
883 NOTREACHED() << "Failed to parse JSON response.";
885 results_info_value_.SetBoolean("hasSyncedResults", results_value != NULL);
886 if (!history_request_consumer_.HasPendingRequests())
887 ReturnResultsToFrontEnd();
890 void BrowsingHistoryHandler::RemoveComplete() {
891 urls_to_be_deleted_.clear();
893 // Notify the page that the deletion request is complete, but only if a web
894 // history delete request is not still pending.
895 if (!has_pending_delete_request_)
896 web_ui()->CallJavascriptFunction("deleteComplete");
899 void BrowsingHistoryHandler::RemoveWebHistoryComplete(bool success) {
900 has_pending_delete_request_ = false;
901 // TODO(dubroy): Should we handle failure somehow? Delete directives will
902 // ensure that the visits are eventually deleted, so maybe it's not necessary.
903 if (!delete_task_tracker_.HasTrackedTasks())
904 RemoveComplete();
907 void BrowsingHistoryHandler::SetQueryTimeInWeeks(
908 int offset, history::QueryOptions* options) {
909 // LocalMidnight returns the beginning of the current day so get the
910 // beginning of the next one.
911 base::Time midnight = base::Time::Now().LocalMidnight() +
912 base::TimeDelta::FromDays(1);
913 options->end_time = midnight -
914 base::TimeDelta::FromDays(7 * offset);
915 options->begin_time = midnight -
916 base::TimeDelta::FromDays(7 * (offset + 1));
919 void BrowsingHistoryHandler::SetQueryTimeInMonths(
920 int offset, history::QueryOptions* options) {
921 // Configure the begin point of the search to the start of the
922 // current month.
923 base::Time::Exploded exploded;
924 base::Time::Now().LocalMidnight().LocalExplode(&exploded);
925 exploded.day_of_month = 1;
927 if (offset == 0) {
928 options->begin_time = base::Time::FromLocalExploded(exploded);
930 // Set the end time of this first search to null (which will
931 // show results from the future, should the user's clock have
932 // been set incorrectly).
933 options->end_time = base::Time();
934 } else {
935 // Go back |offset| months in the past. The end time is not inclusive, so
936 // use the first day of the |offset| - 1 and |offset| months (e.g. for
937 // the last month, |offset| = 1, use the first days of the last month and
938 // the current month.
939 exploded.month -= offset - 1;
940 // Set the correct year.
941 normalizeMonths(&exploded);
942 options->end_time = base::Time::FromLocalExploded(exploded);
944 exploded.month -= 1;
945 // Set the correct year
946 normalizeMonths(&exploded);
947 options->begin_time = base::Time::FromLocalExploded(exploded);
951 // Helper function for Observe that determines if there are any differences
952 // between the URLs noticed for deletion and the ones we are expecting.
953 static bool DeletionsDiffer(const history::URLRows& deleted_rows,
954 const std::set<GURL>& urls_to_be_deleted) {
955 if (deleted_rows.size() != urls_to_be_deleted.size())
956 return true;
957 for (history::URLRows::const_iterator i = deleted_rows.begin();
958 i != deleted_rows.end(); ++i) {
959 if (urls_to_be_deleted.find(i->url()) == urls_to_be_deleted.end())
960 return true;
962 return false;
965 void BrowsingHistoryHandler::Observe(
966 int type,
967 const content::NotificationSource& source,
968 const content::NotificationDetails& details) {
969 if (type != chrome::NOTIFICATION_HISTORY_URLS_DELETED) {
970 NOTREACHED();
971 return;
973 history::URLsDeletedDetails* deletedDetails =
974 content::Details<history::URLsDeletedDetails>(details).ptr();
975 if (deletedDetails->all_history ||
976 DeletionsDiffer(deletedDetails->rows, urls_to_be_deleted_))
977 web_ui()->CallJavascriptFunction("historyDeleted");
980 std::string BrowsingHistoryHandler::GetAcceptLanguages() const {
981 Profile* profile = Profile::FromWebUI(web_ui());
982 return profile->GetPrefs()->GetString(prefs::kAcceptLanguages);
985 ////////////////////////////////////////////////////////////////////////////////
987 // HistoryUI
989 ////////////////////////////////////////////////////////////////////////////////
991 HistoryUI::HistoryUI(content::WebUI* web_ui) : WebUIController(web_ui) {
992 web_ui->AddMessageHandler(new BrowsingHistoryHandler());
993 web_ui->AddMessageHandler(new MetricsHandler());
995 // On mobile we deal with foreign sessions differently.
996 #if !defined(OS_ANDROID) && !defined(OS_IOS)
997 if (chrome::IsInstantExtendedAPIEnabled()) {
998 web_ui->AddMessageHandler(new browser_sync::ForeignSessionHandler());
999 web_ui->AddMessageHandler(new NTPLoginHandler());
1001 #endif
1003 // Set up the chrome://history-frame/ source.
1004 Profile* profile = Profile::FromWebUI(web_ui);
1005 content::WebUIDataSource::Add(profile, CreateHistoryUIHTMLSource(profile));
1008 // static
1009 base::RefCountedMemory* HistoryUI::GetFaviconResourceBytes(
1010 ui::ScaleFactor scale_factor) {
1011 return ResourceBundle::GetSharedInstance().
1012 LoadDataResourceBytesForScale(IDR_HISTORY_FAVICON, scale_factor);