OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 #include <cmath> | 8 #include <cmath> |
9 | 9 |
10 #include "base/callback.h" | 10 #include "base/callback.h" |
11 #include "base/i18n/break_iterator.h" | 11 #include "base/i18n/break_iterator.h" |
12 #include "base/i18n/case_conversion.h" | 12 #include "base/i18n/case_conversion.h" |
13 #include "base/i18n/icu_string_conversions.h" | 13 #include "base/i18n/icu_string_conversions.h" |
14 #include "base/message_loop.h" | 14 #include "base/message_loop.h" |
15 #include "base/string16.h" | 15 #include "base/string16.h" |
16 #include "base/utf_string_conversions.h" | 16 #include "base/utf_string_conversions.h" |
17 #include "chrome/browser/autocomplete/autocomplete_classifier.h" | 17 #include "chrome/browser/autocomplete/autocomplete_classifier.h" |
18 #include "chrome/browser/autocomplete/autocomplete_match.h" | 18 #include "chrome/browser/autocomplete/autocomplete_match.h" |
19 #include "chrome/browser/autocomplete/keyword_provider.h" | 19 #include "chrome/browser/autocomplete/keyword_provider.h" |
20 #include "chrome/browser/history/history.h" | 20 #include "chrome/browser/history/history.h" |
| 21 #include "chrome/browser/history/in_memory_database.h" |
21 #include "chrome/browser/instant/instant_controller.h" | 22 #include "chrome/browser/instant/instant_controller.h" |
22 #include "chrome/browser/net/url_fixer_upper.h" | 23 #include "chrome/browser/net/url_fixer_upper.h" |
23 #include "chrome/browser/prefs/pref_service.h" | 24 #include "chrome/browser/prefs/pref_service.h" |
24 #include "chrome/browser/profiles/profile.h" | 25 #include "chrome/browser/profiles/profile.h" |
25 #include "chrome/browser/history/in_memory_database.h" | |
26 #include "chrome/browser/search_engines/template_url_service.h" | 26 #include "chrome/browser/search_engines/template_url_service.h" |
27 #include "chrome/browser/search_engines/template_url_service_factory.h" | 27 #include "chrome/browser/search_engines/template_url_service_factory.h" |
28 #include "chrome/common/pref_names.h" | 28 #include "chrome/common/pref_names.h" |
29 #include "chrome/common/url_constants.h" | 29 #include "chrome/common/url_constants.h" |
30 #include "content/common/json_value_serializer.h" | 30 #include "content/common/json_value_serializer.h" |
31 #include "googleurl/src/url_util.h" | 31 #include "googleurl/src/url_util.h" |
32 #include "grit/generated_resources.h" | 32 #include "grit/generated_resources.h" |
33 #include "net/base/escape.h" | 33 #include "net/base/escape.h" |
34 #include "net/http/http_response_headers.h" | 34 #include "net/http/http_response_headers.h" |
35 #include "net/url_request/url_request_status.h" | 35 #include "net/url_request/url_request_status.h" |
(...skipping 409 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
445 URLFetcher::GET, this); | 445 URLFetcher::GET, this); |
446 fetcher->set_request_context(profile_->GetRequestContext()); | 446 fetcher->set_request_context(profile_->GetRequestContext()); |
447 fetcher->Start(); | 447 fetcher->Start(); |
448 return fetcher; | 448 return fetcher; |
449 } | 449 } |
450 | 450 |
451 bool SearchProvider::ParseSuggestResults(Value* root_val, | 451 bool SearchProvider::ParseSuggestResults(Value* root_val, |
452 bool is_keyword, | 452 bool is_keyword, |
453 const string16& input_text, | 453 const string16& input_text, |
454 SuggestResults* suggest_results) { | 454 SuggestResults* suggest_results) { |
455 if (!root_val->IsType(Value::TYPE_LIST)) | 455 if (!root_val->IsList()) |
456 return false; | 456 return false; |
457 ListValue* root_list = static_cast<ListValue*>(root_val); | 457 ListValue* root_list = static_cast<ListValue*>(root_val); |
458 | 458 |
459 Value* query_val; | 459 Value* query_val; |
460 string16 query_str; | 460 string16 query_str; |
461 Value* result_val; | 461 Value* result_val; |
462 if ((root_list->GetSize() < 2) || !root_list->Get(0, &query_val) || | 462 if ((root_list->GetSize() < 2) || !root_list->Get(0, &query_val) || |
463 !query_val->GetAsString(&query_str) || | 463 !query_val->GetAsString(&query_str) || |
464 (query_str != input_text) || | 464 (query_str != input_text) || |
465 !root_list->Get(1, &result_val) || !result_val->IsType(Value::TYPE_LIST)) | 465 !root_list->Get(1, &result_val) || !result_val->IsList()) |
466 return false; | 466 return false; |
467 | 467 |
468 ListValue* description_list = NULL; | 468 ListValue* description_list = NULL; |
469 if (root_list->GetSize() > 2) { | 469 if (root_list->GetSize() > 2) { |
470 // 3rd element: Description list. | 470 // 3rd element: Description list. |
471 Value* description_val; | 471 Value* description_val; |
472 if (root_list->Get(2, &description_val) && | 472 if (root_list->Get(2, &description_val) && description_val->IsList()) |
473 description_val->IsType(Value::TYPE_LIST)) | |
474 description_list = static_cast<ListValue*>(description_val); | 473 description_list = static_cast<ListValue*>(description_val); |
475 } | 474 } |
476 | 475 |
477 // We don't care about the query URL list (the fourth element in the | 476 // We don't care about the query URL list (the fourth element in the |
478 // response) for now. | 477 // response) for now. |
479 | 478 |
480 // Parse optional data in the results from the Suggest server if any. | 479 // Parse optional data in the results from the Suggest server if any. |
481 ListValue* type_list = NULL; | 480 ListValue* type_list = NULL; |
482 // 5th argument: Optional key-value pairs. | 481 // 5th argument: Optional key-value pairs. |
483 // TODO: We may iterate the 5th+ arguments of the root_list if any other | 482 // TODO: We may iterate the 5th+ arguments of the root_list if any other |
484 // optional data are defined. | 483 // optional data are defined. |
485 if (root_list->GetSize() > 4) { | 484 if (root_list->GetSize() > 4) { |
486 Value* optional_val; | 485 Value* optional_val; |
487 if (root_list->Get(4, &optional_val) && | 486 if (root_list->Get(4, &optional_val) && optional_val->IsDictionary()) { |
488 optional_val->IsType(Value::TYPE_DICTIONARY)) { | |
489 DictionaryValue* dict_val = static_cast<DictionaryValue*>(optional_val); | 487 DictionaryValue* dict_val = static_cast<DictionaryValue*>(optional_val); |
490 | 488 |
491 // Parse Google Suggest specific type extension. | 489 // Parse Google Suggest specific type extension. |
492 static const std::string kGoogleSuggestType("google:suggesttype"); | 490 static const std::string kGoogleSuggestType("google:suggesttype"); |
493 if (dict_val->HasKey(kGoogleSuggestType)) | 491 if (dict_val->HasKey(kGoogleSuggestType)) |
494 dict_val->GetList(kGoogleSuggestType, &type_list); | 492 dict_val->GetList(kGoogleSuggestType, &type_list); |
495 } | 493 } |
496 } | 494 } |
497 | 495 |
498 ListValue* result_list = static_cast<ListValue*>(result_val); | 496 ListValue* result_list = static_cast<ListValue*>(result_val); |
(...skipping 14 matching lines...) Expand all Loading... |
513 std::string type_str; | 511 std::string type_str; |
514 if (type_list && type_list->Get(i, &type_val) && | 512 if (type_list && type_list->Get(i, &type_val) && |
515 type_val->GetAsString(&type_str) && (type_str == "NAVIGATION")) { | 513 type_val->GetAsString(&type_str) && (type_str == "NAVIGATION")) { |
516 Value* site_val; | 514 Value* site_val; |
517 string16 site_name; | 515 string16 site_name; |
518 NavigationResults& navigation_results = | 516 NavigationResults& navigation_results = |
519 is_keyword ? keyword_navigation_results_ : | 517 is_keyword ? keyword_navigation_results_ : |
520 default_navigation_results_; | 518 default_navigation_results_; |
521 if ((navigation_results.size() < kMaxMatches) && | 519 if ((navigation_results.size() < kMaxMatches) && |
522 description_list && description_list->Get(i, &site_val) && | 520 description_list && description_list->Get(i, &site_val) && |
523 site_val->IsType(Value::TYPE_STRING) && | 521 site_val->IsString() && site_val->GetAsString(&site_name)) { |
524 site_val->GetAsString(&site_name)) { | |
525 // We can't blindly trust the URL coming from the server to be valid. | 522 // We can't blindly trust the URL coming from the server to be valid. |
526 GURL result_url(URLFixerUpper::FixupURL(UTF16ToUTF8(suggestion_str), | 523 GURL result_url(URLFixerUpper::FixupURL(UTF16ToUTF8(suggestion_str), |
527 std::string())); | 524 std::string())); |
528 if (result_url.is_valid()) { | 525 if (result_url.is_valid()) { |
529 navigation_results.push_back(NavigationResult(result_url, site_name)); | 526 navigation_results.push_back(NavigationResult(result_url, site_name)); |
530 } | 527 } |
531 } | 528 } |
532 } else { | 529 } else { |
533 // TODO(kochi): Currently we treat a calculator result as a query, but it | 530 // TODO(kochi): Currently we treat a calculator result as a query, but it |
534 // is better to have better presentation for caluculator results. | 531 // is better to have better presentation for caluculator results. |
(...skipping 409 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
944 | 941 |
945 return match; | 942 return match; |
946 } | 943 } |
947 | 944 |
948 void SearchProvider::UpdateDone() { | 945 void SearchProvider::UpdateDone() { |
949 // We're done when there are no more suggest queries pending (this is set to 1 | 946 // We're done when there are no more suggest queries pending (this is set to 1 |
950 // when the timer is started) and we're not waiting on instant. | 947 // when the timer is started) and we're not waiting on instant. |
951 done_ = ((suggest_results_pending_ == 0) && | 948 done_ = ((suggest_results_pending_ == 0) && |
952 (instant_finalized_ || !InstantController::IsEnabled(profile_))); | 949 (instant_finalized_ || !InstantController::IsEnabled(profile_))); |
953 } | 950 } |
OLD | NEW |