| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/autocomplete/history_contents_provider.h" | 5 #include "chrome/browser/autocomplete/history_contents_provider.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
| 9 #include "base/metrics/histogram.h" | 9 #include "base/metrics/histogram.h" |
| 10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
| 11 #include "base/utf_string_conversions.h" | 11 #include "base/utf_string_conversions.h" |
| 12 #include "chrome/browser/autocomplete/autocomplete_match.h" | 12 #include "chrome/browser/autocomplete/autocomplete_match.h" |
| 13 #include "chrome/browser/autocomplete/autocomplete_provider_listener.h" | 13 #include "chrome/browser/autocomplete/autocomplete_provider_listener.h" |
| 14 #include "chrome/browser/bookmarks/bookmark_model.h" | 14 #include "chrome/browser/bookmarks/bookmark_model.h" |
| 15 #include "chrome/browser/bookmarks/bookmark_model_factory.h" |
| 15 #include "chrome/browser/bookmarks/bookmark_utils.h" | 16 #include "chrome/browser/bookmarks/bookmark_utils.h" |
| 16 #include "chrome/browser/history/history_service_factory.h" | 17 #include "chrome/browser/history/history_service_factory.h" |
| 17 #include "chrome/browser/profiles/profile.h" | 18 #include "chrome/browser/profiles/profile.h" |
| 18 #include "chrome/common/url_constants.h" | 19 #include "chrome/common/url_constants.h" |
| 19 #include "googleurl/src/url_util.h" | 20 #include "googleurl/src/url_util.h" |
| 20 #include "net/base/net_util.h" | 21 #include "net/base/net_util.h" |
| 21 | 22 |
| 22 using base::TimeTicks; | 23 using base::TimeTicks; |
| 23 using history::HistoryDatabase; | 24 using history::HistoryDatabase; |
| 24 | 25 |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 64 void HistoryContentsProvider::Start(const AutocompleteInput& input, | 65 void HistoryContentsProvider::Start(const AutocompleteInput& input, |
| 65 bool minimal_changes) { | 66 bool minimal_changes) { |
| 66 matches_.clear(); | 67 matches_.clear(); |
| 67 | 68 |
| 68 if (input.text().empty() || (input.type() == AutocompleteInput::INVALID) || | 69 if (input.text().empty() || (input.type() == AutocompleteInput::INVALID) || |
| 69 (input.type() == AutocompleteInput::FORCED_QUERY) || | 70 (input.type() == AutocompleteInput::FORCED_QUERY) || |
| 70 !profile_ || | 71 !profile_ || |
| 71 // The history service or bookmark bar model must exist. | 72 // The history service or bookmark bar model must exist. |
| 72 !(HistoryServiceFactory::GetForProfile(profile_, | 73 !(HistoryServiceFactory::GetForProfile(profile_, |
| 73 Profile::EXPLICIT_ACCESS) || | 74 Profile::EXPLICIT_ACCESS) || |
| 74 profile_->GetBookmarkModel())) { | 75 BookmarkModelFactory::GetForProfile(profile_))) { |
| 75 Stop(false); | 76 Stop(false); |
| 76 return; | 77 return; |
| 77 } | 78 } |
| 78 | 79 |
| 79 // TODO(pkasting): http://b/888148 We disallow URL input and "URL-like" input | 80 // TODO(pkasting): http://b/888148 We disallow URL input and "URL-like" input |
| 80 // (REQUESTED_URL or UNKNOWN with dots) because we get poor results for it, | 81 // (REQUESTED_URL or UNKNOWN with dots) because we get poor results for it, |
| 81 // but we could get better results if we did better tokenizing instead. | 82 // but we could get better results if we did better tokenizing instead. |
| 82 if ((input.type() == AutocompleteInput::URL) || | 83 if ((input.type() == AutocompleteInput::URL) || |
| 83 (((input.type() == AutocompleteInput::REQUESTED_URL) || | 84 (((input.type() == AutocompleteInput::REQUESTED_URL) || |
| 84 (input.type() == AutocompleteInput::UNKNOWN)) && | 85 (input.type() == AutocompleteInput::UNKNOWN)) && |
| (...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 215 MatchInTitle(result) ? | 216 MatchInTitle(result) ? |
| 216 AutocompleteMatch::HISTORY_TITLE : AutocompleteMatch::HISTORY_BODY); | 217 AutocompleteMatch::HISTORY_TITLE : AutocompleteMatch::HISTORY_BODY); |
| 217 match.contents = StringForURLDisplay(result.url(), true, trim_http_); | 218 match.contents = StringForURLDisplay(result.url(), true, trim_http_); |
| 218 match.fill_into_edit = | 219 match.fill_into_edit = |
| 219 AutocompleteInput::FormattedStringWithEquivalentMeaning(result.url(), | 220 AutocompleteInput::FormattedStringWithEquivalentMeaning(result.url(), |
| 220 match.contents); | 221 match.contents); |
| 221 match.destination_url = result.url(); | 222 match.destination_url = result.url(); |
| 222 match.contents_class.push_back( | 223 match.contents_class.push_back( |
| 223 ACMatchClassification(0, ACMatchClassification::URL)); | 224 ACMatchClassification(0, ACMatchClassification::URL)); |
| 224 match.description = result.title(); | 225 match.description = result.title(); |
| 225 match.starred = | 226 BookmarkModel* bm_model = BookmarkModelFactory::GetForProfile(profile_); |
| 226 (profile_->GetBookmarkModel() && | 227 match.starred = (bm_model && bm_model->IsBookmarked(result.url())); |
| 227 profile_->GetBookmarkModel()->IsBookmarked(result.url())); | |
| 228 | 228 |
| 229 ClassifyDescription(result, &match); | 229 ClassifyDescription(result, &match); |
| 230 return match; | 230 return match; |
| 231 } | 231 } |
| 232 | 232 |
| 233 void HistoryContentsProvider::ClassifyDescription( | 233 void HistoryContentsProvider::ClassifyDescription( |
| 234 const history::URLResult& result, | 234 const history::URLResult& result, |
| 235 AutocompleteMatch* match) const { | 235 AutocompleteMatch* match) const { |
| 236 const Snippet::MatchPositions& title_matches = result.title_match_positions(); | 236 const Snippet::MatchPositions& title_matches = result.title_match_positions(); |
| 237 | 237 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 251 } | 251 } |
| 252 if (offset != result.title().length()) { | 252 if (offset != result.title().length()) { |
| 253 match->description_class.push_back( | 253 match->description_class.push_back( |
| 254 ACMatchClassification(offset, ACMatchClassification::NONE)); | 254 ACMatchClassification(offset, ACMatchClassification::NONE)); |
| 255 } | 255 } |
| 256 } | 256 } |
| 257 | 257 |
| 258 int HistoryContentsProvider::CalculateRelevance( | 258 int HistoryContentsProvider::CalculateRelevance( |
| 259 const history::URLResult& result) { | 259 const history::URLResult& result) { |
| 260 const bool in_title = MatchInTitle(result); | 260 const bool in_title = MatchInTitle(result); |
| 261 if (!profile_->GetBookmarkModel() || | 261 BookmarkModel* bm_model = BookmarkModelFactory::GetForProfile(profile_); |
| 262 !profile_->GetBookmarkModel()->IsBookmarked(result.url())) | 262 if (!bm_model || !bm_model->IsBookmarked(result.url())) |
| 263 return in_title ? (700 + title_count_++) : (500 + contents_count_++); | 263 return in_title ? (700 + title_count_++) : (500 + contents_count_++); |
| 264 return in_title ? | 264 return in_title ? |
| 265 (1000 + star_title_count_++) : (550 + star_contents_count_++); | 265 (1000 + star_title_count_++) : (550 + star_contents_count_++); |
| 266 } | 266 } |
| 267 | 267 |
| 268 void HistoryContentsProvider::QueryBookmarks(const AutocompleteInput& input) { | 268 void HistoryContentsProvider::QueryBookmarks(const AutocompleteInput& input) { |
| 269 BookmarkModel* bookmark_model = profile_->GetBookmarkModel(); | 269 BookmarkModel* bookmark_model = |
| 270 BookmarkModelFactory::GetForProfile(profile_); |
| 270 if (!bookmark_model) | 271 if (!bookmark_model) |
| 271 return; | 272 return; |
| 272 | 273 |
| 273 DCHECK(results_.empty()); | 274 DCHECK(results_.empty()); |
| 274 | 275 |
| 275 TimeTicks start_time = TimeTicks::Now(); | 276 TimeTicks start_time = TimeTicks::Now(); |
| 276 std::vector<bookmark_utils::TitleMatch> matches; | 277 std::vector<bookmark_utils::TitleMatch> matches; |
| 277 bookmark_model->GetBookmarksWithTitlesMatching(input.text(), | 278 bookmark_model->GetBookmarksWithTitlesMatching(input.text(), |
| 278 kMaxMatches, &matches); | 279 kMaxMatches, &matches); |
| 279 for (size_t i = 0; i < matches.size(); ++i) | 280 for (size_t i = 0; i < matches.size(); ++i) |
| 280 AddBookmarkTitleMatchToResults(matches[i]); | 281 AddBookmarkTitleMatchToResults(matches[i]); |
| 281 UMA_HISTOGRAM_TIMES("Omnibox.QueryBookmarksTime", | 282 UMA_HISTOGRAM_TIMES("Omnibox.QueryBookmarksTime", |
| 282 TimeTicks::Now() - start_time); | 283 TimeTicks::Now() - start_time); |
| 283 } | 284 } |
| 284 | 285 |
| 285 void HistoryContentsProvider::AddBookmarkTitleMatchToResults( | 286 void HistoryContentsProvider::AddBookmarkTitleMatchToResults( |
| 286 const bookmark_utils::TitleMatch& match) { | 287 const bookmark_utils::TitleMatch& match) { |
| 287 history::URLResult url_result(match.node->url(), match.match_positions); | 288 history::URLResult url_result(match.node->url(), match.match_positions); |
| 288 url_result.set_title(match.node->GetTitle()); | 289 url_result.set_title(match.node->GetTitle()); |
| 289 results_.AppendURLBySwapping(&url_result); | 290 results_.AppendURLBySwapping(&url_result); |
| 290 } | 291 } |
| OLD | NEW |