Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 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/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" |
| (...skipping 635 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 646 if (response_headers->GetCharset(&charset)) { | 646 if (response_headers->GetCharset(&charset)) { |
| 647 string16 data_16; | 647 string16 data_16; |
| 648 // TODO(jungshik): Switch to CodePageToUTF8 after it's added. | 648 // TODO(jungshik): Switch to CodePageToUTF8 after it's added. |
| 649 if (base::CodepageToUTF16(json_data, charset.c_str(), | 649 if (base::CodepageToUTF16(json_data, charset.c_str(), |
| 650 base::OnStringConversionError::FAIL, | 650 base::OnStringConversionError::FAIL, |
| 651 &data_16)) | 651 &data_16)) |
| 652 json_data = UTF16ToUTF8(data_16); | 652 json_data = UTF16ToUTF8(data_16); |
| 653 } | 653 } |
| 654 } | 654 } |
| 655 | 655 |
| 656 // The JSON response should be an array. | 656 scoped_ptr<Value> data(DeserializeJsonData(json_data)); |
| 657 for (size_t response_start_index = json_data.find("["), i = 0; | 657 results_updated = data.get() && ParseSuggestResults(data.get(), is_keyword); |
| 658 response_start_index != std::string::npos && i < 5; | |
| 659 response_start_index = json_data.find("[", 1), i++) { | |
| 660 // Remove any XSSI guards to allow for JSON parsing. | |
| 661 if (response_start_index > 0) | |
| 662 json_data.erase(0, response_start_index); | |
| 663 | |
| 664 JSONStringValueSerializer deserializer(json_data); | |
| 665 deserializer.set_allow_trailing_comma(true); | |
| 666 int error_code = 0; | |
| 667 scoped_ptr<Value> data(deserializer.Deserialize(&error_code, NULL)); | |
| 668 if (error_code == 0) { | |
| 669 results_updated = data.get() && | |
| 670 ParseSuggestResults(data.get(), is_keyword); | |
| 671 break; | |
| 672 } | |
| 673 } | |
| 674 } | 658 } |
| 675 | 659 |
| 676 UpdateMatches(); | 660 UpdateMatches(); |
| 677 if (done_ || results_updated) | 661 if (done_ || results_updated) |
| 678 listener_->OnProviderUpdate(results_updated); | 662 listener_->OnProviderUpdate(results_updated); |
| 679 } | 663 } |
| 680 | 664 |
| 681 void SearchProvider::Run() { | 665 void SearchProvider::Run() { |
| 682 // Start a new request with the current input. | 666 // Start a new request with the current input. |
| 683 suggest_results_pending_ = 0; | 667 suggest_results_pending_ = 0; |
| (...skipping 349 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1033 fetcher->SetLoadFlags(net::LOAD_DO_NOT_SAVE_COOKIES); | 1017 fetcher->SetLoadFlags(net::LOAD_DO_NOT_SAVE_COOKIES); |
| 1034 // Add Chrome experiment state to the request headers. | 1018 // Add Chrome experiment state to the request headers. |
| 1035 net::HttpRequestHeaders headers; | 1019 net::HttpRequestHeaders headers; |
| 1036 chrome_variations::VariationsHttpHeaderProvider::GetInstance()->AppendHeaders( | 1020 chrome_variations::VariationsHttpHeaderProvider::GetInstance()->AppendHeaders( |
| 1037 fetcher->GetOriginalURL(), profile_->IsOffTheRecord(), false, &headers); | 1021 fetcher->GetOriginalURL(), profile_->IsOffTheRecord(), false, &headers); |
| 1038 fetcher->SetExtraRequestHeaders(headers.ToString()); | 1022 fetcher->SetExtraRequestHeaders(headers.ToString()); |
| 1039 fetcher->Start(); | 1023 fetcher->Start(); |
| 1040 return fetcher; | 1024 return fetcher; |
| 1041 } | 1025 } |
| 1042 | 1026 |
| 1027 scoped_ptr<Value> SearchProvider::DeserializeJsonData(std::string json_data) { | |
| 1028 // The JSON response should be an array. | |
| 1029 for (size_t response_start_index = json_data.find("["), i = 0; | |
| 1030 response_start_index != std::string::npos && i < 5; | |
| 1031 response_start_index = json_data.find("[", 1), i++) { | |
| 1032 // Remove any XSSI guards to allow for JSON parsing. | |
| 1033 if (response_start_index > 0) | |
| 1034 json_data.erase(0, response_start_index); | |
| 1035 | |
| 1036 JSONStringValueSerializer deserializer(json_data); | |
| 1037 deserializer.set_allow_trailing_comma(true); | |
| 1038 int error_code = 0; | |
| 1039 scoped_ptr<Value> data(deserializer.Deserialize(&error_code, NULL)); | |
| 1040 if (error_code == 0) { | |
|
H Fung
2013/12/02 23:43:45
No curly braces since I think that's the conventio
Maria
2013/12/03 00:48:02
Done.
| |
| 1041 return data.Pass(); | |
| 1042 } | |
| 1043 } | |
| 1044 return scoped_ptr<Value>(); | |
| 1045 } | |
| 1046 | |
| 1043 bool SearchProvider::ParseSuggestResults(Value* root_val, bool is_keyword) { | 1047 bool SearchProvider::ParseSuggestResults(Value* root_val, bool is_keyword) { |
| 1044 string16 query; | 1048 string16 query; |
| 1045 ListValue* root_list = NULL; | 1049 ListValue* root_list = NULL; |
| 1046 ListValue* results_list = NULL; | 1050 ListValue* results_list = NULL; |
| 1047 const string16& input_text = | 1051 const string16& input_text = |
| 1048 is_keyword ? keyword_input_.text() : input_.text(); | 1052 is_keyword ? keyword_input_.text() : input_.text(); |
| 1049 if (!root_val->GetAsList(&root_list) || !root_list->GetString(0, &query) || | 1053 if (!root_val->GetAsList(&root_list) || !root_list->GetString(0, &query) || |
| 1050 (query != input_text) || !root_list->GetList(1, &results_list)) | 1054 (query != input_text) || !root_list->GetList(1, &results_list)) |
| 1051 return false; | 1055 return false; |
| 1052 | 1056 |
| (...skipping 892 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1945 it->set_relevance(max_query_relevance); | 1949 it->set_relevance(max_query_relevance); |
| 1946 it->set_relevance_from_server(relevance_from_server); | 1950 it->set_relevance_from_server(relevance_from_server); |
| 1947 } | 1951 } |
| 1948 } | 1952 } |
| 1949 | 1953 |
| 1950 void SearchProvider::UpdateDone() { | 1954 void SearchProvider::UpdateDone() { |
| 1951 // We're done when the timer isn't running, there are no suggest queries | 1955 // We're done when the timer isn't running, there are no suggest queries |
| 1952 // pending, and we're not waiting on Instant. | 1956 // pending, and we're not waiting on Instant. |
| 1953 done_ = !timer_.IsRunning() && (suggest_results_pending_ == 0); | 1957 done_ = !timer_.IsRunning() && (suggest_results_pending_ == 0); |
| 1954 } | 1958 } |
| OLD | NEW |