34f7730fdfa124bcdd461ba5d57256a3548044c6
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / webui / history_ui.cc
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 #include "chrome/browser/ui/webui/history_ui.h"
6
7 #include <set>
8
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.h"
24 #include "chrome/browser/bookmarks/bookmark_model_factory.h"
25 #include "chrome/browser/bookmarks/bookmark_utils.h"
26 #include "chrome/browser/chrome_notification_types.h"
27 #include "chrome/browser/extensions/activity_log/activity_log.h"
28 #include "chrome/browser/history/history_notifications.h"
29 #include "chrome/browser/history/history_service_factory.h"
30 #include "chrome/browser/history/history_types.h"
31 #include "chrome/browser/history/web_history_service.h"
32 #include "chrome/browser/history/web_history_service_factory.h"
33 #include "chrome/browser/profiles/profile.h"
34 #include "chrome/browser/search/search.h"
35 #include "chrome/browser/sync/glue/device_info.h"
36 #include "chrome/browser/sync/profile_sync_service.h"
37 #include "chrome/browser/sync/profile_sync_service_factory.h"
38 #include "chrome/browser/ui/browser_finder.h"
39 #include "chrome/browser/ui/chrome_pages.h"
40 #include "chrome/browser/ui/webui/favicon_source.h"
41 #include "chrome/browser/ui/webui/metrics_handler.h"
42 #include "chrome/common/chrome_switches.h"
43 #include "chrome/common/pref_names.h"
44 #include "chrome/common/url_constants.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"
59
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
66
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
71
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
76
77 static const char kStringsJsFile[] = "strings.js";
78 static const char kHistoryJsFile[] = "history.js";
79 static const char kOtherDevicesJsFile[] = "other_devices.js";
80
81 // The amount of time to wait for a response from the WebHistoryService.
82 static const int kWebHistoryTimeoutSeconds = 3;
83
84 namespace {
85
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
92 };
93
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
104
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";
109
110 content::WebUIDataSource* CreateHistoryUIHTMLSource(Profile* profile) {
111   PrefService* prefs = profile->GetPrefs();
112
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());
181
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());
190
191   return source;
192 }
193
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));
206   }
207   return date_str;
208 }
209
210
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--;
217   }
218 }
219
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;
224 }
225
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;
246       }
247       return;
248     }
249   } else {
250     NOTREACHED() << "Got a remote history entry but no ProfileSyncService.";
251   }
252   *name = l10n_util::GetStringUTF8(IDS_HISTORY_UNKNOWN_DEVICE);
253   *type = kDeviceTypeLaptop;
254 }
255
256 }  // namespace
257
258 ////////////////////////////////////////////////////////////////////////////////
259 //
260 // BrowsingHistoryHandler
261 //
262 ////////////////////////////////////////////////////////////////////////////////
263
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;
280 }
281
282 BrowsingHistoryHandler::HistoryEntry::HistoryEntry()
283     : entry_type(EMPTY_ENTRY), is_search_result(false), blocked_visit(false) {
284 }
285
286 BrowsingHistoryHandler::HistoryEntry::~HistoryEntry() {
287 }
288
289 void BrowsingHistoryHandler::HistoryEntry::SetUrlAndTitle(
290     base::DictionaryValue* result) const {
291   result->SetString("url", url.spec());
292
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());
298   }
299
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);
309   }
310   result->SetString("title", title_to_set);
311 }
312
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());
319
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() + ":");
325
326   result->SetString("domain", domain);
327   result->SetDouble("time", time.ToJsTime());
328
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());
334   }
335   result->Set("allTimestamps", timestamps.release());
336
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));
340
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));
355     }
356     result->SetString("dateRelativeDay", date_str);
357     result->SetString("dateTimeOfDay", base::TimeFormatTimeOfDay(time));
358   }
359   result->SetBoolean("starred", bookmark_model->IsBookmarked(url));
360
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);
367
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);
375
376     result->SetBoolean("blockedVisit", blocked_visit);
377   }
378 #endif
379
380   return result.Pass();
381 }
382
383 bool BrowsingHistoryHandler::HistoryEntry::SortByTimeDescending(
384     const BrowsingHistoryHandler::HistoryEntry& entry1,
385     const BrowsingHistoryHandler::HistoryEntry& entry2) {
386   return entry1.time > entry2.time;
387 }
388
389 BrowsingHistoryHandler::BrowsingHistoryHandler()
390     : has_pending_delete_request_(false),
391       weak_factory_(this) {
392 }
393
394 BrowsingHistoryHandler::~BrowsingHistoryHandler() {
395   history_request_consumer_.CancelAllRequests();
396   web_history_request_.reset();
397 }
398
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));
404
405   // Get notifications when history is cleared.
406   registrar_.Add(this, chrome::NOTIFICATION_HISTORY_URLS_DELETED,
407       content::Source<Profile>(profile->GetOriginalProfile()));
408
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)));
421 }
422
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;
431   }
432   NOTREACHED();
433   return false;
434 }
435
436 void BrowsingHistoryHandler::WebHistoryTimeout() {
437   // TODO(dubroy): Communicate the failure to the front end.
438   if (!history_request_consumer_.HasPendingRequests())
439     ReturnResultsToFrontEnd();
440
441   UMA_HISTOGRAM_ENUMERATION(
442       "WebHistory.QueryCompletion",
443       WEB_HISTORY_QUERY_TIMED_OUT, NUM_WEB_HISTORY_QUERY_BUCKETS);
444 }
445
446 void BrowsingHistoryHandler::QueryHistory(
447     base::string16 search_text, const history::QueryOptions& options) {
448   Profile* profile = Profile::FromWebUI(web_ui());
449
450   // Anything in-flight is invalid.
451   history_request_consumer_.CancelAllRequests();
452   web_history_request_.reset();
453
454   query_results_.clear();
455   results_info_value_.Clear();
456
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));
464
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);
480
481     // Set this to false until the results actually arrive.
482     results_info_value_.SetBoolean("hasSyncedResults", false);
483   }
484 }
485
486 void BrowsingHistoryHandler::HandleQueryHistory(const base::ListValue* args) {
487   history::QueryOptions options;
488
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;
504   }
505   int range;
506   if (!args->GetInteger(2, &range)) {
507     NOTREACHED() << "Failed to convert argument 2. ";
508     return;
509   }
510
511   if (range == BrowsingHistoryHandler::MONTH)
512     SetQueryTimeInMonths(offset, &options);
513   else if (range == BrowsingHistoryHandler::WEEK)
514     SetQueryTimeInWeeks(offset, &options);
515
516   double end_time;
517   if (!args->GetDouble(3, &end_time)) {
518     NOTREACHED() << "Failed to convert argument 3. ";
519     return;
520   }
521   if (end_time)
522     options.end_time = base::Time::FromJsTime(end_time);
523
524   if (!ExtractIntegerValueAtIndex(args, 4, &options.max_count)) {
525     NOTREACHED() << "Failed to convert argument 4.";
526     return;
527   }
528
529   options.duplicate_policy = history::QueryOptions::REMOVE_DUPLICATES_PER_DAY;
530   QueryHistory(search_text, options);
531 }
532
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;
542   }
543
544   HistoryService* history_service =
545       HistoryServiceFactory::GetForProfile(profile, Profile::EXPLICIT_ACCESS);
546   history::WebHistoryService* web_history =
547       WebHistoryServiceFactory::GetForProfile(profile);
548
549   base::Time now = base::Time::Now();
550   std::vector<history::ExpireHistoryArgs> expire_list;
551   expire_list.reserve(args->GetSize());
552
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;
559
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;
566     }
567     DCHECK(timestamps->GetSize() > 0);
568
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();
575
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;
583       }
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);
592       }
593       // The local visit time is treated as a global ID for the visit.
594       global_id_directive->add_global_id(visit_time.ToInternalValue());
595     }
596
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());
600
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);
604
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);
608
609     // TODO(dubroy): Figure out the proper way to handle an error here.
610     if (web_history)
611       history_service->ProcessLocalDeleteDirective(delete_directive);
612   }
613
614   history_service->ExpireHistory(
615       expire_list,
616       base::Bind(&BrowsingHistoryHandler::RemoveComplete,
617                  base::Unretained(this)),
618       &delete_task_tracker_);
619
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()));
626   }
627
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);
638   }
639 #endif
640 }
641
642 void BrowsingHistoryHandler::HandleClearBrowsingData(
643     const base::ListValue* args) {
644 #if defined(OS_ANDROID)
645   Profile* profile = Profile::FromWebUI(web_ui());
646   const TabModel* tab_model =
647       TabModelList::GetTabModelWithProfile(profile);
648   if (tab_model)
649     tab_model->OpenClearBrowsingData();
650 #else
651   // TODO(beng): This is an improper direct dependency on Browser. Route this
652   // through some sort of delegate.
653   Browser* browser = chrome::FindBrowserWithWebContents(
654       web_ui()->GetWebContents());
655   chrome::ShowClearBrowsingDataDialog(browser);
656 #endif
657 }
658
659 void BrowsingHistoryHandler::HandleRemoveBookmark(const base::ListValue* args) {
660   base::string16 url = ExtractStringValue(args);
661   Profile* profile = Profile::FromWebUI(web_ui());
662   BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile);
663   bookmark_utils::RemoveAllBookmarks(model, GURL(url));
664 }
665
666 // static
667 void BrowsingHistoryHandler::MergeDuplicateResults(
668     std::vector<BrowsingHistoryHandler::HistoryEntry>* results) {
669   std::vector<BrowsingHistoryHandler::HistoryEntry> new_results;
670   // Pre-reserve the size of the new vector. Since we're working with pointers
671   // later on not doing this could lead to the vector being resized and to
672   // pointers to invalid locations.
673   new_results.reserve(results->size());
674   // Maps a URL to the most recent entry on a particular day.
675   std::map<GURL,BrowsingHistoryHandler::HistoryEntry*>
676       current_day_entries;
677
678   // Keeps track of the day that |current_day_urls| is holding the URLs for,
679   // in order to handle removing per-day duplicates.
680   base::Time current_day_midnight;
681
682   std::sort(
683       results->begin(), results->end(), HistoryEntry::SortByTimeDescending);
684
685   for (std::vector<BrowsingHistoryHandler::HistoryEntry>::const_iterator it =
686            results->begin(); it != results->end(); ++it) {
687     // Reset the list of found URLs when a visit from a new day is encountered.
688     if (current_day_midnight != it->time.LocalMidnight()) {
689       current_day_entries.clear();
690       current_day_midnight = it->time.LocalMidnight();
691     }
692
693     // Keep this visit if it's the first visit to this URL on the current day.
694     if (current_day_entries.count(it->url) == 0) {
695       new_results.push_back(*it);
696       current_day_entries[it->url] = &new_results.back();
697     } else {
698       // Keep track of the timestamps of all visits to the URL on the same day.
699       BrowsingHistoryHandler::HistoryEntry* entry =
700           current_day_entries[it->url];
701       entry->all_timestamps.insert(
702           it->all_timestamps.begin(), it->all_timestamps.end());
703
704       if (entry->entry_type != it->entry_type) {
705         entry->entry_type =
706             BrowsingHistoryHandler::HistoryEntry::COMBINED_ENTRY;
707       }
708     }
709   }
710   results->swap(new_results);
711 }
712
713 void BrowsingHistoryHandler::ReturnResultsToFrontEnd() {
714   Profile* profile = Profile::FromWebUI(web_ui());
715   BookmarkModel* bookmark_model = BookmarkModelFactory::GetForProfile(profile);
716   ManagedUserService* managed_user_service = NULL;
717 #if defined(ENABLE_MANAGED_USERS)
718   if (profile->IsManaged())
719     managed_user_service = ManagedUserServiceFactory::GetForProfile(profile);
720 #endif
721   ProfileSyncService* sync_service =
722       ProfileSyncServiceFactory::GetInstance()->GetForProfile(profile);
723
724   // Combine the local and remote results into |query_results_|, and remove
725   // any duplicates.
726   if (!web_history_query_results_.empty()) {
727     int local_result_count = query_results_.size();
728     query_results_.insert(query_results_.end(),
729                           web_history_query_results_.begin(),
730                           web_history_query_results_.end());
731     MergeDuplicateResults(&query_results_);
732
733     if (local_result_count) {
734       // In the best case, we expect that all local results are duplicated on
735       // the server. Keep track of how many are missing.
736       int missing_count = std::count_if(
737           query_results_.begin(), query_results_.end(), IsLocalOnlyResult);
738       UMA_HISTOGRAM_PERCENTAGE("WebHistory.LocalResultMissingOnServer",
739                                missing_count * 100.0 / local_result_count);
740     }
741   }
742
743   // Convert the result vector into a ListValue.
744   base::ListValue results_value;
745   for (std::vector<BrowsingHistoryHandler::HistoryEntry>::iterator it =
746            query_results_.begin(); it != query_results_.end(); ++it) {
747     scoped_ptr<base::Value> value(
748         it->ToValue(bookmark_model, managed_user_service, sync_service));
749     results_value.Append(value.release());
750   }
751
752   web_ui()->CallJavascriptFunction(
753       "historyResult", results_info_value_, results_value);
754   results_info_value_.Clear();
755   query_results_.clear();
756   web_history_query_results_.clear();
757 }
758
759 void BrowsingHistoryHandler::QueryComplete(
760     const base::string16& search_text,
761     const history::QueryOptions& options,
762     HistoryService::Handle request_handle,
763     history::QueryResults* results) {
764   DCHECK_EQ(0U, query_results_.size());
765   query_results_.reserve(results->size());
766   const std::string accept_languages = GetAcceptLanguages();
767
768   for (size_t i = 0; i < results->size(); ++i) {
769     history::URLResult const &page = (*results)[i];
770     // TODO(dubroy): Use sane time (crbug.com/146090) here when it's ready.
771     query_results_.push_back(
772         HistoryEntry(
773             HistoryEntry::LOCAL_ENTRY,
774             page.url(),
775             page.title(),
776             page.visit_time(),
777             std::string(),
778             !search_text.empty(),
779             page.snippet().text(),
780             page.blocked_visit(),
781             accept_languages));
782   }
783
784   results_info_value_.SetString("term", search_text);
785   results_info_value_.SetBoolean("finished", results->reached_beginning());
786
787   // Add the specific dates that were searched to display them.
788   // TODO(sergiu): Put today if the start is in the future.
789   results_info_value_.SetString("queryStartTime",
790                                 getRelativeDateLocalized(options.begin_time));
791   if (!options.end_time.is_null()) {
792     results_info_value_.SetString("queryEndTime",
793         getRelativeDateLocalized(options.end_time -
794                                  base::TimeDelta::FromDays(1)));
795   } else {
796     results_info_value_.SetString("queryEndTime",
797         getRelativeDateLocalized(base::Time::Now()));
798   }
799   if (!web_history_timer_.IsRunning())
800     ReturnResultsToFrontEnd();
801 }
802
803 void BrowsingHistoryHandler::WebHistoryQueryComplete(
804     const base::string16& search_text,
805     const history::QueryOptions& options,
806     base::TimeTicks start_time,
807     history::WebHistoryService::Request* request,
808     const base::DictionaryValue* results_value) {
809   base::TimeDelta delta = base::TimeTicks::Now() - start_time;
810   UMA_HISTOGRAM_TIMES("WebHistory.ResponseTime", delta);
811   const std::string accept_languages = GetAcceptLanguages();
812
813   // If the response came in too late, do nothing.
814   // TODO(dubroy): Maybe show a banner, and prompt the user to reload?
815   if (!web_history_timer_.IsRunning())
816     return;
817   web_history_timer_.Stop();
818
819   UMA_HISTOGRAM_ENUMERATION(
820       "WebHistory.QueryCompletion",
821       results_value ? WEB_HISTORY_QUERY_SUCCEEDED : WEB_HISTORY_QUERY_FAILED,
822       NUM_WEB_HISTORY_QUERY_BUCKETS);
823
824   DCHECK_EQ(0U, web_history_query_results_.size());
825   const base::ListValue* events = NULL;
826   if (results_value && results_value->GetList("event", &events)) {
827     web_history_query_results_.reserve(events->GetSize());
828     for (unsigned int i = 0; i < events->GetSize(); ++i) {
829       const base::DictionaryValue* event = NULL;
830       const base::DictionaryValue* result = NULL;
831       const base::ListValue* results = NULL;
832       const base::ListValue* ids = NULL;
833       base::string16 url;
834       base::string16 title;
835       base::Time visit_time;
836
837       if (!(events->GetDictionary(i, &event) &&
838           event->GetList("result", &results) &&
839           results->GetDictionary(0, &result) &&
840           result->GetString("url", &url) &&
841           result->GetList("id", &ids) &&
842           ids->GetSize() > 0)) {
843         LOG(WARNING) << "Improperly formed JSON response from history server.";
844         continue;
845       }
846       // Title is optional, so the return value is ignored here.
847       result->GetString("title", &title);
848
849       // Extract the timestamps of all the visits to this URL.
850       // They are referred to as "IDs" by the server.
851       for (int j = 0; j < static_cast<int>(ids->GetSize()); ++j) {
852         const base::DictionaryValue* id = NULL;
853         std::string timestamp_string;
854         int64 timestamp_usec;
855
856         if (!(ids->GetDictionary(j, &id) &&
857             id->GetString("timestamp_usec", &timestamp_string) &&
858               base::StringToInt64(timestamp_string, &timestamp_usec))) {
859           NOTREACHED() << "Unable to extract timestamp.";
860           continue;
861         }
862         // The timestamp on the server is a Unix time.
863         base::Time time = base::Time::UnixEpoch() +
864                           base::TimeDelta::FromMicroseconds(timestamp_usec);
865
866         // Get the ID of the client that this visit came from.
867         std::string client_id;
868         id->GetString("client_id", &client_id);
869
870         web_history_query_results_.push_back(
871             HistoryEntry(
872                 HistoryEntry::REMOTE_ENTRY,
873                 GURL(url),
874                 title,
875                 time,
876                 client_id,
877                 !search_text.empty(),
878                 base::string16(),
879                 /* blocked_visit */ false,
880                 accept_languages));
881       }
882     }
883   } else if (results_value) {
884     NOTREACHED() << "Failed to parse JSON response.";
885   }
886   results_info_value_.SetBoolean("hasSyncedResults", results_value != NULL);
887   if (!history_request_consumer_.HasPendingRequests())
888     ReturnResultsToFrontEnd();
889 }
890
891 void BrowsingHistoryHandler::RemoveComplete() {
892   urls_to_be_deleted_.clear();
893
894   // Notify the page that the deletion request is complete, but only if a web
895   // history delete request is not still pending.
896   if (!has_pending_delete_request_)
897     web_ui()->CallJavascriptFunction("deleteComplete");
898 }
899
900 void BrowsingHistoryHandler::RemoveWebHistoryComplete(bool success) {
901   has_pending_delete_request_ = false;
902   // TODO(dubroy): Should we handle failure somehow? Delete directives will
903   // ensure that the visits are eventually deleted, so maybe it's not necessary.
904   if (!delete_task_tracker_.HasTrackedTasks())
905     RemoveComplete();
906 }
907
908 void BrowsingHistoryHandler::SetQueryTimeInWeeks(
909     int offset, history::QueryOptions* options) {
910   // LocalMidnight returns the beginning of the current day so get the
911   // beginning of the next one.
912   base::Time midnight = base::Time::Now().LocalMidnight() +
913                               base::TimeDelta::FromDays(1);
914   options->end_time = midnight -
915       base::TimeDelta::FromDays(7 * offset);
916   options->begin_time = midnight -
917       base::TimeDelta::FromDays(7 * (offset + 1));
918 }
919
920 void BrowsingHistoryHandler::SetQueryTimeInMonths(
921     int offset, history::QueryOptions* options) {
922   // Configure the begin point of the search to the start of the
923   // current month.
924   base::Time::Exploded exploded;
925   base::Time::Now().LocalMidnight().LocalExplode(&exploded);
926   exploded.day_of_month = 1;
927
928   if (offset == 0) {
929     options->begin_time = base::Time::FromLocalExploded(exploded);
930
931     // Set the end time of this first search to null (which will
932     // show results from the future, should the user's clock have
933     // been set incorrectly).
934     options->end_time = base::Time();
935   } else {
936     // Go back |offset| months in the past. The end time is not inclusive, so
937     // use the first day of the |offset| - 1 and |offset| months (e.g. for
938     // the last month, |offset| = 1, use the first days of the last month and
939     // the current month.
940     exploded.month -= offset - 1;
941     // Set the correct year.
942     normalizeMonths(&exploded);
943     options->end_time = base::Time::FromLocalExploded(exploded);
944
945     exploded.month -= 1;
946     // Set the correct year
947     normalizeMonths(&exploded);
948     options->begin_time = base::Time::FromLocalExploded(exploded);
949   }
950 }
951
952 // Helper function for Observe that determines if there are any differences
953 // between the URLs noticed for deletion and the ones we are expecting.
954 static bool DeletionsDiffer(const history::URLRows& deleted_rows,
955                             const std::set<GURL>& urls_to_be_deleted) {
956   if (deleted_rows.size() != urls_to_be_deleted.size())
957     return true;
958   for (history::URLRows::const_iterator i = deleted_rows.begin();
959        i != deleted_rows.end(); ++i) {
960     if (urls_to_be_deleted.find(i->url()) == urls_to_be_deleted.end())
961       return true;
962   }
963   return false;
964 }
965
966 void BrowsingHistoryHandler::Observe(
967     int type,
968     const content::NotificationSource& source,
969     const content::NotificationDetails& details) {
970   if (type != chrome::NOTIFICATION_HISTORY_URLS_DELETED) {
971     NOTREACHED();
972     return;
973   }
974   history::URLsDeletedDetails* deletedDetails =
975       content::Details<history::URLsDeletedDetails>(details).ptr();
976   if (deletedDetails->all_history ||
977       DeletionsDiffer(deletedDetails->rows, urls_to_be_deleted_))
978     web_ui()->CallJavascriptFunction("historyDeleted");
979 }
980
981 std::string BrowsingHistoryHandler::GetAcceptLanguages() const {
982   Profile* profile = Profile::FromWebUI(web_ui());
983   return profile->GetPrefs()->GetString(prefs::kAcceptLanguages);
984 }
985
986 ////////////////////////////////////////////////////////////////////////////////
987 //
988 // HistoryUI
989 //
990 ////////////////////////////////////////////////////////////////////////////////
991
992 HistoryUI::HistoryUI(content::WebUI* web_ui) : WebUIController(web_ui) {
993   web_ui->AddMessageHandler(new BrowsingHistoryHandler());
994   web_ui->AddMessageHandler(new MetricsHandler());
995
996 // On mobile we deal with foreign sessions differently.
997 #if !defined(OS_ANDROID) && !defined(OS_IOS)
998   if (chrome::IsInstantExtendedAPIEnabled()) {
999     web_ui->AddMessageHandler(new browser_sync::ForeignSessionHandler());
1000     web_ui->AddMessageHandler(new NTPLoginHandler());
1001   }
1002 #endif
1003
1004   // Set up the chrome://history-frame/ source.
1005   Profile* profile = Profile::FromWebUI(web_ui);
1006   content::WebUIDataSource::Add(profile, CreateHistoryUIHTMLSource(profile));
1007 }
1008
1009 // static
1010 const GURL HistoryUI::GetHistoryURLWithSearchText(const base::string16& text) {
1011   return GURL(std::string(chrome::kChromeUIHistoryURL) + "#q=" +
1012               net::EscapeQueryParamValue(base::UTF16ToUTF8(text), true));
1013 }
1014
1015 // static
1016 base::RefCountedMemory* HistoryUI::GetFaviconResourceBytes(
1017       ui::ScaleFactor scale_factor) {
1018   return ResourceBundle::GetSharedInstance().
1019       LoadDataResourceBytesForScale(IDR_HISTORY_FAVICON, scale_factor);
1020 }