| OLD | NEW | 
|    1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |    1 // Copyright (c) 2010 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/search_provider.h" |    5 #include "chrome/browser/autocomplete/search_provider.h" | 
|    6  |    6  | 
|    7 #include <algorithm> |    7 #include <algorithm> | 
|    8  |    8  | 
|    9 #include "app/l10n_util.h" |    9 #include "app/l10n_util.h" | 
|   10 #include "base/callback.h" |   10 #include "base/callback.h" | 
| (...skipping 310 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|  321   have_suggest_results_ = false; |  321   have_suggest_results_ = false; | 
|  322 } |  322 } | 
|  323  |  323  | 
|  324 void SearchProvider::ScheduleHistoryQuery(TemplateURL::IDType search_id, |  324 void SearchProvider::ScheduleHistoryQuery(TemplateURL::IDType search_id, | 
|  325                                           const std::wstring& text) { |  325                                           const std::wstring& text) { | 
|  326   DCHECK(!text.empty()); |  326   DCHECK(!text.empty()); | 
|  327   HistoryService* const history_service = |  327   HistoryService* const history_service = | 
|  328       profile_->GetHistoryService(Profile::EXPLICIT_ACCESS); |  328       profile_->GetHistoryService(Profile::EXPLICIT_ACCESS); | 
|  329   HistoryService::Handle request_handle = |  329   HistoryService::Handle request_handle = | 
|  330       history_service->GetMostRecentKeywordSearchTerms( |  330       history_service->GetMostRecentKeywordSearchTerms( | 
|  331           search_id, text, static_cast<int>(max_matches()), |  331           search_id, text, static_cast<int>(kMaxMatches), | 
|  332           &history_request_consumer_, |  332           &history_request_consumer_, | 
|  333           NewCallback(this, |  333           NewCallback(this, | 
|  334                       &SearchProvider::OnGotMostRecentKeywordSearchTerms)); |  334                       &SearchProvider::OnGotMostRecentKeywordSearchTerms)); | 
|  335   history_request_consumer_.SetClientData(history_service, request_handle, |  335   history_request_consumer_.SetClientData(history_service, request_handle, | 
|  336                                           search_id); |  336                                           search_id); | 
|  337   history_request_pending_ = true; |  337   history_request_pending_ = true; | 
|  338 } |  338 } | 
|  339  |  339  | 
|  340 void SearchProvider::OnGotMostRecentKeywordSearchTerms( |  340 void SearchProvider::OnGotMostRecentKeywordSearchTerms( | 
|  341     CancelableRequestProvider::Handle handle, |  341     CancelableRequestProvider::Handle handle, | 
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|  433  |  433  | 
|  434     Value* type_val; |  434     Value* type_val; | 
|  435     std::wstring type_str; |  435     std::wstring type_str; | 
|  436     if (type_list && type_list->Get(i, &type_val) && |  436     if (type_list && type_list->Get(i, &type_val) && | 
|  437         type_val->GetAsString(&type_str) && (type_str == L"NAVIGATION")) { |  437         type_val->GetAsString(&type_str) && (type_str == L"NAVIGATION")) { | 
|  438       Value* site_val; |  438       Value* site_val; | 
|  439       std::wstring site_name; |  439       std::wstring site_name; | 
|  440       NavigationResults& navigation_results = |  440       NavigationResults& navigation_results = | 
|  441           is_keyword ? keyword_navigation_results_ : |  441           is_keyword ? keyword_navigation_results_ : | 
|  442                        default_navigation_results_; |  442                        default_navigation_results_; | 
|  443       if ((navigation_results.size() < max_matches()) && |  443       if ((navigation_results.size() < kMaxMatches) && | 
|  444           description_list && description_list->Get(i, &site_val) && |  444           description_list && description_list->Get(i, &site_val) && | 
|  445           site_val->IsType(Value::TYPE_STRING) && |  445           site_val->IsType(Value::TYPE_STRING) && | 
|  446           site_val->GetAsString(&site_name)) { |  446           site_val->GetAsString(&site_name)) { | 
|  447         // We can't blindly trust the URL coming from the server to be valid. |  447         // We can't blindly trust the URL coming from the server to be valid. | 
|  448         GURL result_url = |  448         GURL result_url = | 
|  449             GURL(URLFixerUpper::FixupURL(WideToUTF8(suggestion_str), |  449             GURL(URLFixerUpper::FixupURL(WideToUTF8(suggestion_str), | 
|  450                                          std::string())); |  450                                          std::string())); | 
|  451         if (result_url.is_valid()) |  451         if (result_url.is_valid()) | 
|  452           navigation_results.push_back(NavigationResult(result_url, site_name)); |  452           navigation_results.push_back(NavigationResult(result_url, site_name)); | 
|  453       } |  453       } | 
|  454     } else { |  454     } else { | 
|  455       // TODO(kochi): Currently we treat a calculator result as a query, but it |  455       // TODO(kochi): Currently we treat a calculator result as a query, but it | 
|  456       // is better to have better presentation for caluculator results. |  456       // is better to have better presentation for caluculator results. | 
|  457       if (suggest_results->size() < max_matches()) |  457       if (suggest_results->size() < kMaxMatches) | 
|  458         suggest_results->push_back(suggestion_str); |  458         suggest_results->push_back(suggestion_str); | 
|  459     } |  459     } | 
|  460   } |  460   } | 
|  461  |  461  | 
|  462   return true; |  462   return true; | 
|  463 } |  463 } | 
|  464  |  464  | 
|  465 void SearchProvider::ConvertResultsToAutocompleteMatches() { |  465 void SearchProvider::ConvertResultsToAutocompleteMatches() { | 
|  466   // Convert all the results to matches and add them to a map, so we can keep |  466   // Convert all the results to matches and add them to a map, so we can keep | 
|  467   // the most relevant match for each result. |  467   // the most relevant match for each result. | 
| (...skipping 24 matching lines...) Expand all  Loading... | 
|  492                          did_not_accept_default_suggestion, &map); |  492                          did_not_accept_default_suggestion, &map); | 
|  493  |  493  | 
|  494   // Now add the most relevant matches from the map to |matches_|. |  494   // Now add the most relevant matches from the map to |matches_|. | 
|  495   matches_.clear(); |  495   matches_.clear(); | 
|  496   for (MatchMap::const_iterator i(map.begin()); i != map.end(); ++i) |  496   for (MatchMap::const_iterator i(map.begin()); i != map.end(); ++i) | 
|  497     matches_.push_back(i->second); |  497     matches_.push_back(i->second); | 
|  498  |  498  | 
|  499   AddNavigationResultsToMatches(keyword_navigation_results_, true); |  499   AddNavigationResultsToMatches(keyword_navigation_results_, true); | 
|  500   AddNavigationResultsToMatches(default_navigation_results_, false); |  500   AddNavigationResultsToMatches(default_navigation_results_, false); | 
|  501  |  501  | 
|  502   const size_t max_total_matches = max_matches() + 1;  // 1 for "what you typed" |  502   const size_t max_total_matches = kMaxMatches + 1;  // 1 for "what you typed" | 
|  503   std::partial_sort(matches_.begin(), |  503   std::partial_sort(matches_.begin(), | 
|  504       matches_.begin() + std::min(max_total_matches, matches_.size()), |  504       matches_.begin() + std::min(max_total_matches, matches_.size()), | 
|  505       matches_.end(), &AutocompleteMatch::MoreRelevant); |  505       matches_.end(), &AutocompleteMatch::MoreRelevant); | 
|  506   if (matches_.size() > max_total_matches) |  506   if (matches_.size() > max_total_matches) | 
|  507     matches_.erase(matches_.begin() + max_total_matches, matches_.end()); |  507     matches_.erase(matches_.begin() + max_total_matches, matches_.end()); | 
|  508  |  508  | 
|  509   UpdateStarredStateOfMatches(); |  509   UpdateStarredStateOfMatches(); | 
|  510  |  510  | 
|  511   // We're done when both asynchronous subcomponents have finished.  We can't |  511   // We're done when both asynchronous subcomponents have finished.  We can't | 
|  512   // use CancelableRequestConsumer.HasPendingRequests() for history requests |  512   // use CancelableRequestConsumer.HasPendingRequests() for history requests | 
| (...skipping 240 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|  753   // values preserve that property.  Otherwise, if the user starts editing a |  753   // values preserve that property.  Otherwise, if the user starts editing a | 
|  754   // suggestion, non-Search results will suddenly appear. |  754   // suggestion, non-Search results will suddenly appear. | 
|  755   if (input_.type() == AutocompleteInput::FORCED_QUERY) |  755   if (input_.type() == AutocompleteInput::FORCED_QUERY) | 
|  756     match.fill_into_edit.assign(L"?"); |  756     match.fill_into_edit.assign(L"?"); | 
|  757   match.fill_into_edit.append(match.contents); |  757   match.fill_into_edit.append(match.contents); | 
|  758   // TODO(pkasting): http://b/1112879 These should perhaps be |  758   // TODO(pkasting): http://b/1112879 These should perhaps be | 
|  759   // inline-autocompletable? |  759   // inline-autocompletable? | 
|  760  |  760  | 
|  761   return match; |  761   return match; | 
|  762 } |  762 } | 
| OLD | NEW |