Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/android/contextualsearch/contextual_search_delegate.h" | 5 #include "chrome/browser/android/contextualsearch/contextual_search_delegate.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/base64.h" | 9 #include "base/base64.h" |
| 10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 34 const char kContextualSearchSurroundingSizeParamName[] = "surrounding_size"; | 34 const char kContextualSearchSurroundingSizeParamName[] = "surrounding_size"; |
| 35 const char kContextualSearchIcingSurroundingSizeParamName[] = | 35 const char kContextualSearchIcingSurroundingSizeParamName[] = |
| 36 "icing_surrounding_size"; | 36 "icing_surrounding_size"; |
| 37 const char kContextualSearchResolverURLParamName[] = "resolver_url"; | 37 const char kContextualSearchResolverURLParamName[] = "resolver_url"; |
| 38 const char kContextualSearchDoNotSendURLParamName[] = "do_not_send_url"; | 38 const char kContextualSearchDoNotSendURLParamName[] = "do_not_send_url"; |
| 39 const char kContextualSearchResponseDisplayTextParam[] = "display_text"; | 39 const char kContextualSearchResponseDisplayTextParam[] = "display_text"; |
| 40 const char kContextualSearchResponseSelectedTextParam[] = "selected_text"; | 40 const char kContextualSearchResponseSelectedTextParam[] = "selected_text"; |
| 41 const char kContextualSearchResponseSearchTermParam[] = "search_term"; | 41 const char kContextualSearchResponseSearchTermParam[] = "search_term"; |
| 42 const char kContextualSearchResponseResolvedTermParam[] = "resolved_term"; | 42 const char kContextualSearchResponseResolvedTermParam[] = "resolved_term"; |
| 43 const char kContextualSearchPreventPreload[] = "prevent_preload"; | 43 const char kContextualSearchPreventPreload[] = "prevent_preload"; |
| 44 const char kContextualSearchMentions[] = "mentions"; | |
| 44 const char kContextualSearchServerEndpoint[] = "_/contextualsearch?"; | 45 const char kContextualSearchServerEndpoint[] = "_/contextualsearch?"; |
| 45 const int kContextualSearchRequestVersion = 2; | 46 const int kContextualSearchRequestVersion = 2; |
| 46 const char kContextualSearchResolverUrl[] = | 47 const char kContextualSearchResolverUrl[] = |
| 47 "contextual-search-resolver-url"; | 48 "contextual-search-resolver-url"; |
| 48 // The default size of the content surrounding the selection to gather, allowing | 49 // The default size of the content surrounding the selection to gather, allowing |
| 49 // room for other parameters. | 50 // room for other parameters. |
| 50 const int kContextualSearchDefaultContentSize = 1536; | 51 const int kContextualSearchDefaultContentSize = 1536; |
| 51 const int kContextualSearchDefaultIcingSurroundingSize = 400; | 52 const int kContextualSearchDefaultIcingSurroundingSize = 400; |
| 52 // The maximum length of a URL to build. | 53 // The maximum length of a URL to build. |
| 53 const int kMaxURLSize = 2048; | 54 const int kMaxURLSize = 2048; |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 135 } | 136 } |
| 136 | 137 |
| 137 void ContextualSearchDelegate::OnURLFetchComplete( | 138 void ContextualSearchDelegate::OnURLFetchComplete( |
| 138 const net::URLFetcher* source) { | 139 const net::URLFetcher* source) { |
| 139 DCHECK(source == search_term_fetcher_.get()); | 140 DCHECK(source == search_term_fetcher_.get()); |
| 140 int response_code = source->GetResponseCode(); | 141 int response_code = source->GetResponseCode(); |
| 141 std::string search_term; | 142 std::string search_term; |
| 142 std::string display_text; | 143 std::string display_text; |
| 143 std::string alternate_term; | 144 std::string alternate_term; |
| 144 std::string prevent_preload; | 145 std::string prevent_preload; |
| 146 size_t mention_start = 0; | |
| 147 size_t mention_end = 0; | |
| 148 int start_adjust = 0; | |
| 149 int end_adjust = 0; | |
| 145 | 150 |
| 146 if (source->GetStatus().is_success() && response_code == 200) { | 151 if (source->GetStatus().is_success() && response_code == 200) { |
| 147 std::string response; | 152 std::string response; |
| 148 bool has_string_response = source->GetResponseAsString(&response); | 153 bool has_string_response = source->GetResponseAsString(&response); |
| 149 DCHECK(has_string_response); | 154 DCHECK(has_string_response); |
| 150 if (has_string_response) { | 155 if (has_string_response) { |
| 151 DecodeSearchTermsFromJsonResponse(response, &search_term, &display_text, | 156 DecodeSearchTermsFromJsonResponse(response, &search_term, &display_text, |
| 152 &alternate_term, &prevent_preload); | 157 &alternate_term, &prevent_preload, |
| 158 &mention_start, &mention_end); | |
| 159 if (mention_start != 0 || mention_end != 0) { | |
| 160 start_adjust = mention_start - context_->start_offset; | |
| 161 end_adjust = mention_end - context_->end_offset; | |
| 162 } | |
| 153 } | 163 } |
| 154 } | 164 } |
| 155 bool is_invalid = response_code == net::URLFetcher::RESPONSE_CODE_INVALID; | 165 bool is_invalid = response_code == net::URLFetcher::RESPONSE_CODE_INVALID; |
| 156 search_term_callback_.Run( | 166 search_term_callback_.Run( |
| 157 is_invalid, response_code, search_term, display_text, alternate_term, | 167 is_invalid, response_code, search_term, display_text, alternate_term, |
| 158 prevent_preload == kDoPreventPreloadValue); | 168 prevent_preload == kDoPreventPreloadValue, start_adjust, end_adjust); |
| 159 | 169 |
| 160 // The ContextualSearchContext is consumed once the request has completed. | 170 // The ContextualSearchContext is consumed once the request has completed. |
| 161 context_.reset(); | 171 context_.reset(); |
| 162 } | 172 } |
| 163 | 173 |
| 164 // TODO(jeremycho): Remove selected_text and base_page_url CGI parameters. | 174 // TODO(jeremycho): Remove selected_text and base_page_url CGI parameters. |
| 165 GURL ContextualSearchDelegate::BuildRequestUrl() { | 175 GURL ContextualSearchDelegate::BuildRequestUrl() { |
| 166 // TODO(jeremycho): Confirm this is the right way to handle TemplateURL fails. | 176 // TODO(jeremycho): Confirm this is the right way to handle TemplateURL fails. |
| 167 if (!template_url_service_ || | 177 if (!template_url_service_ || |
| 168 !template_url_service_->GetDefaultSearchProvider()) { | 178 !template_url_service_->GetDefaultSearchProvider()) { |
| (...skipping 223 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 392 return true; | 402 return true; |
| 393 } | 403 } |
| 394 | 404 |
| 395 // Decodes the given response from the search term resolution request and sets | 405 // Decodes the given response from the search term resolution request and sets |
| 396 // the value of the given parameters. | 406 // the value of the given parameters. |
| 397 void ContextualSearchDelegate::DecodeSearchTermsFromJsonResponse( | 407 void ContextualSearchDelegate::DecodeSearchTermsFromJsonResponse( |
| 398 const std::string& response, | 408 const std::string& response, |
| 399 std::string* search_term, | 409 std::string* search_term, |
| 400 std::string* display_text, | 410 std::string* display_text, |
| 401 std::string* alternate_term, | 411 std::string* alternate_term, |
| 402 std::string* prevent_preload) { | 412 std::string* prevent_preload, |
| 413 size_t* mention_start, | |
| 414 size_t* mention_end) { | |
| 403 bool contains_xssi_escape = response.find(kXssiEscape) == 0; | 415 bool contains_xssi_escape = response.find(kXssiEscape) == 0; |
| 404 const std::string& proper_json = | 416 const std::string& proper_json = |
| 405 contains_xssi_escape ? response.substr(strlen(kXssiEscape)) : response; | 417 contains_xssi_escape ? response.substr(strlen(kXssiEscape)) : response; |
| 406 JSONStringValueDeserializer deserializer(proper_json); | 418 JSONStringValueDeserializer deserializer(proper_json); |
| 407 scoped_ptr<base::Value> root(deserializer.Deserialize(NULL, NULL)); | 419 scoped_ptr<base::Value> root(deserializer.Deserialize(NULL, NULL)); |
| 408 | 420 |
| 409 if (root.get() != NULL && root->IsType(base::Value::TYPE_DICTIONARY)) { | 421 if (root.get() != NULL && root->IsType(base::Value::TYPE_DICTIONARY)) { |
| 410 base::DictionaryValue* dict = | 422 base::DictionaryValue* dict = |
| 411 static_cast<base::DictionaryValue*>(root.get()); | 423 static_cast<base::DictionaryValue*>(root.get()); |
| 412 dict->GetString(kContextualSearchPreventPreload, prevent_preload); | 424 dict->GetString(kContextualSearchPreventPreload, prevent_preload); |
| 413 dict->GetString(kContextualSearchResponseSearchTermParam, search_term); | 425 dict->GetString(kContextualSearchResponseSearchTermParam, search_term); |
| 414 // For the display_text, if not present fall back to the "search_term". | 426 // For the display_text, if not present fall back to the "search_term". |
| 415 if (!dict->GetString(kContextualSearchResponseDisplayTextParam, | 427 if (!dict->GetString(kContextualSearchResponseDisplayTextParam, |
| 416 display_text)) { | 428 display_text)) { |
| 417 *display_text = *search_term; | 429 *display_text = *search_term; |
| 418 } | 430 } |
| 431 // Extract mentions for seleciton-expansion | |
| 432 base::ListValue* mentions_list; | |
|
pedro (no code reviews)
2015/06/25 01:30:28
Does this represent multiple mentions. If so, are
| |
| 433 dict->GetList(kContextualSearchMentions, &mentions_list); | |
| 434 if (mentions_list != NULL && mentions_list->GetSize() >= 2) | |
| 435 ExtractMentionsStartEnd(*mentions_list, mention_start, mention_end); | |
| 419 // If either the selected text or the resolved term is not the search term, | 436 // If either the selected text or the resolved term is not the search term, |
| 420 // use it as the alternate term. | 437 // use it as the alternate term. |
| 421 std::string selected_text; | 438 std::string selected_text; |
| 422 dict->GetString(kContextualSearchResponseSelectedTextParam, &selected_text); | 439 dict->GetString(kContextualSearchResponseSelectedTextParam, &selected_text); |
| 423 if (selected_text != *search_term) { | 440 if (selected_text != *search_term) { |
| 424 *alternate_term = selected_text; | 441 *alternate_term = selected_text; |
| 425 } else { | 442 } else { |
| 426 std::string resolved_term; | 443 std::string resolved_term; |
| 427 dict->GetString(kContextualSearchResponseResolvedTermParam, | 444 dict->GetString(kContextualSearchResponseResolvedTermParam, |
| 428 &resolved_term); | 445 &resolved_term); |
| 429 if (resolved_term != *search_term) { | 446 if (resolved_term != *search_term) { |
| 430 *alternate_term = resolved_term; | 447 *alternate_term = resolved_term; |
| 431 } | 448 } |
| 432 } | 449 } |
| 433 } | 450 } |
| 434 } | 451 } |
| 435 | 452 |
| 436 // Returns the size of the surroundings to be sent to the server for search term | 453 // Returns the size of the surroundings to be sent to the server for search term |
| 437 // resolution. | 454 // resolution. |
| 438 int ContextualSearchDelegate::GetSearchTermSurroundingSize() { | 455 int ContextualSearchDelegate::GetSearchTermSurroundingSize() { |
| 439 const std::string param_value = variations::GetVariationParamValue( | 456 const std::string param_value = variations::GetVariationParamValue( |
| 440 kContextualSearchFieldTrialName, | 457 kContextualSearchFieldTrialName, |
| 441 kContextualSearchSurroundingSizeParamName); | 458 kContextualSearchSurroundingSizeParamName); |
| 442 int param_length; | 459 int param_length; |
| 443 if (!param_value.empty() && base::StringToInt(param_value, ¶m_length)) | 460 if (!param_value.empty() && base::StringToInt(param_value, ¶m_length)) |
| 444 return param_length; | 461 return param_length; |
| 445 return kContextualSearchDefaultContentSize; | 462 return kContextualSearchDefaultContentSize; |
| 446 } | 463 } |
| 447 | 464 |
| 465 // Extract the Start/End of the mentions in the surrounding text | |
| 466 // for selection-expansion. | |
| 467 void ContextualSearchDelegate::ExtractMentionsStartEnd( | |
| 468 const base::ListValue& mentions_list, | |
| 469 size_t* startResult, | |
| 470 size_t* endResult) { | |
| 471 int int_value; | |
| 472 if (mentions_list.GetInteger(0, &int_value)) | |
| 473 *startResult = int_value; | |
| 474 if (mentions_list.GetInteger(1, &int_value)) | |
| 475 *endResult = int_value; | |
| 476 } | |
| 477 | |
| 448 // Returns the size of the surroundings to be sent to Icing. | 478 // Returns the size of the surroundings to be sent to Icing. |
| 449 int ContextualSearchDelegate::GetIcingSurroundingSize() { | 479 int ContextualSearchDelegate::GetIcingSurroundingSize() { |
| 450 std::string param_string = variations::GetVariationParamValue( | 480 std::string param_string = variations::GetVariationParamValue( |
| 451 kContextualSearchFieldTrialName, | 481 kContextualSearchFieldTrialName, |
| 452 kContextualSearchIcingSurroundingSizeParamName); | 482 kContextualSearchIcingSurroundingSizeParamName); |
| 453 if (base::CommandLine::ForCurrentProcess()->HasSwitch( | 483 if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 454 kContextualSearchIcingSurroundingSizeParamName)) { | 484 kContextualSearchIcingSurroundingSizeParamName)) { |
| 455 param_string = base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( | 485 param_string = base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( |
| 456 kContextualSearchIcingSurroundingSizeParamName); | 486 kContextualSearchIcingSurroundingSizeParamName); |
| 457 } | 487 } |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 481 end_offset -= trim; | 511 end_offset -= trim; |
| 482 } | 512 } |
| 483 if (result_text.length() > end_offset + padding_each_side_pinned) { | 513 if (result_text.length() > end_offset + padding_each_side_pinned) { |
| 484 // Trim the end. | 514 // Trim the end. |
| 485 result_text = result_text.substr(0, end_offset + padding_each_side_pinned); | 515 result_text = result_text.substr(0, end_offset + padding_each_side_pinned); |
| 486 } | 516 } |
| 487 *start = start_offset; | 517 *start = start_offset; |
| 488 *end = end_offset; | 518 *end = end_offset; |
| 489 return result_text; | 519 return result_text; |
| 490 } | 520 } |
| OLD | NEW |