Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / history / url_index_private_data.cc
index 2c69fd0..cff1849 100644 (file)
@@ -4,7 +4,6 @@
 
 #include "chrome/browser/history/url_index_private_data.h"
 
-#include <algorithm>
 #include <functional>
 #include <iterator>
 #include <limits>
 #include <vector>
 
 #include "base/basictypes.h"
-#include "base/file_util.h"
+#include "base/files/file_util.h"
 #include "base/i18n/break_iterator.h"
 #include "base/i18n/case_conversion.h"
 #include "base/metrics/histogram.h"
 #include "base/strings/string_util.h"
 #include "base/strings/utf_string_conversions.h"
 #include "base/time/time.h"
-#include "chrome/browser/autocomplete/autocomplete_provider.h"
-#include "chrome/browser/autocomplete/url_prefix.h"
 #include "chrome/browser/history/history_database.h"
 #include "chrome/browser/history/history_db_task.h"
 #include "chrome/browser/history/history_service.h"
 #include "chrome/browser/history/in_memory_url_index.h"
-#include "components/bookmarks/core/browser/bookmark_service.h"
-#include "components/bookmarks/core/browser/bookmark_utils.h"
+#include "components/bookmarks/browser/bookmark_utils.h"
+#include "components/history/core/browser/history_client.h"
 #include "net/base/net_util.h"
 
 #if defined(USE_SYSTEM_PROTOBUF)
@@ -87,12 +84,12 @@ class UpdateRecentVisitsFromHistoryDBTask : public HistoryDBTask {
       URLIndexPrivateData* private_data,
       URLID url_id);
 
-  virtual bool RunOnDBThread(HistoryBackend* backend,
-                             history::HistoryDatabase* db) OVERRIDE;
-  virtual void DoneRunOnMainThread() OVERRIDE;
+  bool RunOnDBThread(HistoryBackend* backend,
+                     history::HistoryDatabase* db) override;
+  void DoneRunOnMainThread() override;
 
  private:
-  virtual ~UpdateRecentVisitsFromHistoryDBTask();
+  ~UpdateRecentVisitsFromHistoryDBTask() override;
 
   // The URLIndexPrivateData that gets updated after the historyDB
   // task returns.
@@ -120,7 +117,7 @@ bool UpdateRecentVisitsFromHistoryDBTask::RunOnDBThread(
     HistoryBackend* backend,
     HistoryDatabase* db) {
   // Make sure the private data is going to get as many recent visits as
-  // ScoredHistoryMatch::GetFrecency() hopes to use.
+  // ScoredHistoryMatch::GetFrequency() hopes to use.
   DCHECK_GE(kMaxVisitsToStoreInCache, ScoredHistoryMatch::kMaxVisitsToScore);
   succeeded_ = db->GetMostRecentVisitsForURL(url_id_,
                                              kMaxVisitsToStoreInCache,
@@ -152,8 +149,9 @@ URLIndexPrivateData::URLIndexPrivateData()
 ScoredHistoryMatches URLIndexPrivateData::HistoryItemsForTerms(
     base::string16 search_string,
     size_t cursor_position,
+    size_t max_matches,
     const std::string& languages,
-    BookmarkService* bookmark_service) {
+    HistoryClient* history_client) {
   // If cursor position is set and useful (not at either end of the
   // string), allow the search string to be broken at cursor position.
   // We do this by pretending there's a space where the cursor is.
@@ -245,17 +243,17 @@ ScoredHistoryMatches URLIndexPrivateData::HistoryItemsForTerms(
     return scored_items;
   }
   scored_items = std::for_each(history_id_set.begin(), history_id_set.end(),
-      AddHistoryMatch(*this, languages, bookmark_service, lower_raw_string,
+      AddHistoryMatch(*this, languages, history_client, lower_raw_string,
                       lower_raw_terms, base::Time::Now())).ScoredMatches();
 
-  // Select and sort only the top kMaxMatches results.
-  if (scored_items.size() > AutocompleteProvider::kMaxMatches) {
+  // Select and sort only the top |max_matches| results.
+  if (scored_items.size() > max_matches) {
     std::partial_sort(scored_items.begin(),
                       scored_items.begin() +
-                          AutocompleteProvider::kMaxMatches,
+                          max_matches,
                       scored_items.end(),
                       ScoredHistoryMatch::MatchScoreGreater);
-      scored_items.resize(AutocompleteProvider::kMaxMatches);
+      scored_items.resize(max_matches);
   } else {
     std::sort(scored_items.begin(), scored_items.end(),
               ScoredHistoryMatch::MatchScoreGreater);
@@ -282,7 +280,8 @@ bool URLIndexPrivateData::UpdateURL(
     HistoryService* history_service,
     const URLRow& row,
     const std::string& languages,
-    const std::set<std::string>& scheme_whitelist) {
+    const std::set<std::string>& scheme_whitelist,
+    base::CancelableTaskTracker* tracker) {
   // The row may or may not already be in our index. If it is not already
   // indexed and it qualifies then it gets indexed. If it is already
   // indexed and still qualifies then it gets updated, otherwise it
@@ -295,7 +294,12 @@ bool URLIndexPrivateData::UpdateURL(
     URLRow new_row(row);
     new_row.set_id(row_id);
     row_was_updated = RowQualifiesAsSignificant(new_row, base::Time()) &&
-        IndexRow(NULL, history_service, new_row, languages, scheme_whitelist);
+                      IndexRow(NULL,
+                               history_service,
+                               new_row,
+                               languages,
+                               scheme_whitelist,
+                               tracker);
   } else if (RowQualifiesAsSignificant(row, base::Time())) {
     // This indexed row still qualifies and will be re-indexed.
     // The url won't have changed but the title, visit count, etc.
@@ -310,7 +314,7 @@ bool URLIndexPrivateData::UpdateURL(
       row_to_update.set_last_visit(row.last_visit());
       // If something appears to have changed, update the recent visits
       // information.
-      ScheduleUpdateRecentVisits(history_service, row_id);
+      ScheduleUpdateRecentVisits(history_service, row_id, tracker);
       // While the URL is guaranteed to remain stable, the title may have
       // changed. If so, then update the index with the changed words.
       if (title_updated) {
@@ -359,10 +363,11 @@ void URLIndexPrivateData::UpdateRecentVisits(
 
 void URLIndexPrivateData::ScheduleUpdateRecentVisits(
     HistoryService* history_service,
-    URLID url_id) {
+    URLID url_id,
+    base::CancelableTaskTracker* tracker) {
   history_service->ScheduleDBTask(
-      new UpdateRecentVisitsFromHistoryDBTask(this, url_id),
-      &recent_visits_consumer_);
+      scoped_ptr<history::HistoryDBTask>(
+          new UpdateRecentVisitsFromHistoryDBTask(this, url_id)), tracker);
 }
 
 // Helper functor for DeleteURL.
@@ -446,8 +451,8 @@ scoped_refptr<URLIndexPrivateData> URLIndexPrivateData::RebuildFromHistory(
     return NULL;
   rebuilt_data->last_time_rebuilt_from_history_ = base::Time::Now();
   for (URLRow row; history_enum.GetNextURL(&row); ) {
-    rebuilt_data->IndexRow(history_db, NULL, row, languages,
-                           scheme_whitelist);
+    rebuilt_data->IndexRow(
+        history_db, NULL, row, languages, scheme_whitelist, NULL);
   }
 
   UMA_HISTOGRAM_TIMES("History.InMemoryURLIndexingTime",
@@ -470,10 +475,6 @@ bool URLIndexPrivateData::WritePrivateDataToCacheFileTask(
   return private_data->SaveToFile(file_path);
 }
 
-void URLIndexPrivateData::CancelPendingUpdates() {
-  recent_visits_consumer_.CancelAllRequests();
-}
-
 scoped_refptr<URLIndexPrivateData> URLIndexPrivateData::Duplicate() const {
   scoped_refptr<URLIndexPrivateData> data_copy = new URLIndexPrivateData;
   data_copy->last_time_rebuilt_from_history_ = last_time_rebuilt_from_history_;
@@ -491,7 +492,7 @@ scoped_refptr<URLIndexPrivateData> URLIndexPrivateData::Duplicate() const {
   //    pre_filter_item_count_
   //    post_filter_item_count_
   //    post_scoring_item_count_
-};
+}
 
 bool URLIndexPrivateData::Empty() const {
   return history_info_map_.empty();
@@ -535,11 +536,8 @@ HistoryIDSet URLIndexPrivateData::HistoryIDSetFromWords(
     if (iter == words.begin()) {
       history_id_set.swap(term_history_set);
     } else {
-      HistoryIDSet new_history_id_set;
-      std::set_intersection(history_id_set.begin(), history_id_set.end(),
-                            term_history_set.begin(), term_history_set.end(),
-                            std::inserter(new_history_id_set,
-                                          new_history_id_set.begin()));
+      HistoryIDSet new_history_id_set = base::STLSetIntersection<HistoryIDSet>(
+          history_id_set, term_history_set);
       history_id_set.swap(new_history_id_set);
     }
   }
@@ -610,11 +608,8 @@ HistoryIDSet URLIndexPrivateData::HistoryIDsForTerm(
       if (prefix_chars.empty()) {
         word_id_set.swap(leftover_set);
       } else {
-        WordIDSet new_word_id_set;
-        std::set_intersection(word_id_set.begin(), word_id_set.end(),
-                              leftover_set.begin(), leftover_set.end(),
-                              std::inserter(new_word_id_set,
-                                            new_word_id_set.begin()));
+        WordIDSet new_word_id_set = base::STLSetIntersection<WordIDSet>(
+            word_id_set, leftover_set);
         word_id_set.swap(new_word_id_set);
       }
     }
@@ -680,11 +675,8 @@ WordIDSet URLIndexPrivateData::WordIDSetForTermChars(
       word_id_set = char_word_id_set;
     } else {
       // Subsequent character results get intersected in.
-      WordIDSet new_word_id_set;
-      std::set_intersection(word_id_set.begin(), word_id_set.end(),
-                            char_word_id_set.begin(), char_word_id_set.end(),
-                            std::inserter(new_word_id_set,
-                                          new_word_id_set.begin()));
+      WordIDSet new_word_id_set = base::STLSetIntersection<WordIDSet>(
+          word_id_set, char_word_id_set);
       word_id_set.swap(new_word_id_set);
     }
   }
@@ -696,7 +688,8 @@ bool URLIndexPrivateData::IndexRow(
     HistoryService* history_service,
     const URLRow& row,
     const std::string& languages,
-    const std::set<std::string>& scheme_whitelist) {
+    const std::set<std::string>& scheme_whitelist,
+    base::CancelableTaskTracker* tracker) {
   const GURL& gurl(row.url());
 
   // Index only URLs with a whitelisted scheme.
@@ -734,15 +727,16 @@ bool URLIndexPrivateData::IndexRow(
     // So we don't do any thread checks.
     VisitVector recent_visits;
     // Make sure the private data is going to get as many recent visits as
-    // ScoredHistoryMatch::GetFrecency() hopes to use.
+    // ScoredHistoryMatch::GetFrequency() hopes to use.
     DCHECK_GE(kMaxVisitsToStoreInCache, ScoredHistoryMatch::kMaxVisitsToScore);
     if (history_db->GetMostRecentVisitsForURL(row_id,
                                               kMaxVisitsToStoreInCache,
                                               &recent_visits))
       UpdateRecentVisits(row_id, recent_visits);
   } else {
+    DCHECK(tracker);
     DCHECK(history_service);
-    ScheduleUpdateRecentVisits(history_service, row_id);
+    ScheduleUpdateRecentVisits(history_service, row_id, tracker);
   }
 
   return true;
@@ -755,17 +749,13 @@ void URLIndexPrivateData::AddRowWordsToIndex(const URLRow& row,
   // Split URL into individual, unique words then add in the title words.
   const GURL& gurl(row.url());
   const base::string16& url =
-      bookmark_utils::CleanUpUrlForMatching(gurl, languages, NULL);
+      bookmarks::CleanUpUrlForMatching(gurl, languages, NULL);
   String16Set url_words = String16SetFromString16(url,
       word_starts ? &word_starts->url_word_starts_ : NULL);
-  const base::string16& title =
-      bookmark_utils::CleanUpTitleForMatching(row.title());
+  const base::string16& title = bookmarks::CleanUpTitleForMatching(row.title());
   String16Set title_words = String16SetFromString16(title,
       word_starts ? &word_starts->title_word_starts_ : NULL);
-  String16Set words;
-  std::set_union(url_words.begin(), url_words.end(),
-                 title_words.begin(), title_words.end(),
-                 std::insert_iterator<String16Set>(words, words.begin()));
+  String16Set words = base::STLSetUnion<String16Set>(url_words, title_words);
   for (String16Set::iterator word_iter = words.begin();
        word_iter != words.end(); ++word_iter)
     AddWordToIndex(*word_iter, history_id);
@@ -1192,8 +1182,7 @@ bool URLIndexPrivateData::RestoreHistoryInfoMap(
     for (int i = 0; i < iter->visits_size(); ++i) {
       visits.push_back(std::make_pair(
           base::Time::FromInternalValue(iter->visits(i).visit_time()),
-          static_cast<content::PageTransition>(iter->visits(i).
-                                               transition_type())));
+          ui::PageTransitionFromInt(iter->visits(i).transition_type())));
     }
     history_info_map_[history_id].visits = visits;
   }
@@ -1237,11 +1226,11 @@ bool URLIndexPrivateData::RestoreWordStartsMap(
          iter != history_info_map_.end(); ++iter) {
       RowWordStarts word_starts;
       const URLRow& row(iter->second.url_row);
-      const base::string16& url = bookmark_utils::CleanUpUrlForMatching(
-          row.url(), languages, NULL);
+      const base::string16& url =
+          bookmarks::CleanUpUrlForMatching(row.url(), languages, NULL);
       String16VectorFromString16(url, false, &word_starts.url_word_starts_);
       const base::string16& title =
-          bookmark_utils::CleanUpTitleForMatching(row.title());
+          bookmarks::CleanUpTitleForMatching(row.title());
       String16VectorFromString16(title, false, &word_starts.title_word_starts_);
       word_starts_map_[iter->first] = word_starts;
     }
@@ -1277,16 +1266,16 @@ URLIndexPrivateData::SearchTermCacheItem::~SearchTermCacheItem() {}
 URLIndexPrivateData::AddHistoryMatch::AddHistoryMatch(
     const URLIndexPrivateData& private_data,
     const std::string& languages,
-    BookmarkService* bookmark_service,
+    HistoryClient* history_client,
     const base::string16& lower_string,
     const String16Vector& lower_terms,
     const base::Time now)
-  : private_data_(private_data),
-    languages_(languages),
-    bookmark_service_(bookmark_service),
-    lower_string_(lower_string),
-    lower_terms_(lower_terms),
-    now_(now) {
+    : private_data_(private_data),
+      languages_(languages),
+      history_client_(history_client),
+      lower_string_(lower_string),
+      lower_terms_(lower_terms),
+      now_(now) {
   // Calculate offsets for each term.  For instance, the offset for
   // ".net" should be 1, indicating that the actual word-part of the term
   // starts at offset 1.
@@ -1319,7 +1308,7 @@ void URLIndexPrivateData::AddHistoryMatch::operator()(
     DCHECK(starts_pos != private_data_.word_starts_map_.end());
     ScoredHistoryMatch match(hist_item, visits, languages_, lower_string_,
                              lower_terms_, lower_terms_to_word_starts_offsets_,
-                             starts_pos->second, now_, bookmark_service_);
+                             starts_pos->second, now_, history_client_);
     if (match.raw_score() > 0)
       scored_matches_.push_back(match);
   }