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 = context_->start_offset - mention_start; | |
161 start_adjust = std::max(0, start_adjust); | |
162 end_adjust = mention_end - context_->end_offset; | |
163 end_adjust = std::max(0, end_adjust); | |
pedro (no code reviews)
2015/07/01 22:18:57
Nit: Put a TODO here to investigate why sometimes
aurimas (slooooooooow)
2015/07/01 23:05:44
Done
| |
164 } | |
153 } | 165 } |
154 } | 166 } |
155 bool is_invalid = response_code == net::URLFetcher::RESPONSE_CODE_INVALID; | 167 bool is_invalid = response_code == net::URLFetcher::RESPONSE_CODE_INVALID; |
156 search_term_callback_.Run( | 168 search_term_callback_.Run( |
157 is_invalid, response_code, search_term, display_text, alternate_term, | 169 is_invalid, response_code, search_term, display_text, alternate_term, |
158 prevent_preload == kDoPreventPreloadValue); | 170 prevent_preload == kDoPreventPreloadValue, start_adjust, end_adjust); |
159 | 171 |
160 // The ContextualSearchContext is consumed once the request has completed. | 172 // The ContextualSearchContext is consumed once the request has completed. |
161 context_.reset(); | 173 context_.reset(); |
162 } | 174 } |
163 | 175 |
164 // TODO(jeremycho): Remove selected_text and base_page_url CGI parameters. | 176 // TODO(jeremycho): Remove selected_text and base_page_url CGI parameters. |
165 GURL ContextualSearchDelegate::BuildRequestUrl() { | 177 GURL ContextualSearchDelegate::BuildRequestUrl() { |
166 // TODO(jeremycho): Confirm this is the right way to handle TemplateURL fails. | 178 // TODO(jeremycho): Confirm this is the right way to handle TemplateURL fails. |
167 if (!template_url_service_ || | 179 if (!template_url_service_ || |
168 !template_url_service_->GetDefaultSearchProvider()) { | 180 !template_url_service_->GetDefaultSearchProvider()) { |
(...skipping 223 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
392 return true; | 404 return true; |
393 } | 405 } |
394 | 406 |
395 // Decodes the given response from the search term resolution request and sets | 407 // Decodes the given response from the search term resolution request and sets |
396 // the value of the given parameters. | 408 // the value of the given parameters. |
397 void ContextualSearchDelegate::DecodeSearchTermsFromJsonResponse( | 409 void ContextualSearchDelegate::DecodeSearchTermsFromJsonResponse( |
398 const std::string& response, | 410 const std::string& response, |
399 std::string* search_term, | 411 std::string* search_term, |
400 std::string* display_text, | 412 std::string* display_text, |
401 std::string* alternate_term, | 413 std::string* alternate_term, |
402 std::string* prevent_preload) { | 414 std::string* prevent_preload, |
415 size_t* mention_start, | |
416 size_t* mention_end) { | |
403 bool contains_xssi_escape = response.find(kXssiEscape) == 0; | 417 bool contains_xssi_escape = response.find(kXssiEscape) == 0; |
404 const std::string& proper_json = | 418 const std::string& proper_json = |
405 contains_xssi_escape ? response.substr(strlen(kXssiEscape)) : response; | 419 contains_xssi_escape ? response.substr(strlen(kXssiEscape)) : response; |
406 JSONStringValueDeserializer deserializer(proper_json); | 420 JSONStringValueDeserializer deserializer(proper_json); |
407 scoped_ptr<base::Value> root(deserializer.Deserialize(NULL, NULL)); | 421 scoped_ptr<base::Value> root(deserializer.Deserialize(NULL, NULL)); |
408 | 422 |
409 if (root.get() != NULL && root->IsType(base::Value::TYPE_DICTIONARY)) { | 423 if (root.get() != NULL && root->IsType(base::Value::TYPE_DICTIONARY)) { |
410 base::DictionaryValue* dict = | 424 base::DictionaryValue* dict = |
411 static_cast<base::DictionaryValue*>(root.get()); | 425 static_cast<base::DictionaryValue*>(root.get()); |
412 dict->GetString(kContextualSearchPreventPreload, prevent_preload); | 426 dict->GetString(kContextualSearchPreventPreload, prevent_preload); |
413 dict->GetString(kContextualSearchResponseSearchTermParam, search_term); | 427 dict->GetString(kContextualSearchResponseSearchTermParam, search_term); |
414 // For the display_text, if not present fall back to the "search_term". | 428 // For the display_text, if not present fall back to the "search_term". |
415 if (!dict->GetString(kContextualSearchResponseDisplayTextParam, | 429 if (!dict->GetString(kContextualSearchResponseDisplayTextParam, |
416 display_text)) { | 430 display_text)) { |
417 *display_text = *search_term; | 431 *display_text = *search_term; |
418 } | 432 } |
433 // Extract mentions for selection expansion. | |
434 base::ListValue* mentions_list; | |
435 dict->GetList(kContextualSearchMentions, &mentions_list); | |
436 if (mentions_list != NULL && mentions_list->GetSize() >= 2) | |
437 ExtractMentionsStartEnd(*mentions_list, mention_start, mention_end); | |
419 // If either the selected text or the resolved term is not the search term, | 438 // If either the selected text or the resolved term is not the search term, |
420 // use it as the alternate term. | 439 // use it as the alternate term. |
421 std::string selected_text; | 440 std::string selected_text; |
422 dict->GetString(kContextualSearchResponseSelectedTextParam, &selected_text); | 441 dict->GetString(kContextualSearchResponseSelectedTextParam, &selected_text); |
423 if (selected_text != *search_term) { | 442 if (selected_text != *search_term) { |
424 *alternate_term = selected_text; | 443 *alternate_term = selected_text; |
425 } else { | 444 } else { |
426 std::string resolved_term; | 445 std::string resolved_term; |
427 dict->GetString(kContextualSearchResponseResolvedTermParam, | 446 dict->GetString(kContextualSearchResponseResolvedTermParam, |
428 &resolved_term); | 447 &resolved_term); |
429 if (resolved_term != *search_term) { | 448 if (resolved_term != *search_term) { |
430 *alternate_term = resolved_term; | 449 *alternate_term = resolved_term; |
431 } | 450 } |
432 } | 451 } |
433 } | 452 } |
434 } | 453 } |
435 | 454 |
436 // Returns the size of the surroundings to be sent to the server for search term | 455 // Returns the size of the surroundings to be sent to the server for search term |
437 // resolution. | 456 // resolution. |
438 int ContextualSearchDelegate::GetSearchTermSurroundingSize() { | 457 int ContextualSearchDelegate::GetSearchTermSurroundingSize() { |
439 const std::string param_value = variations::GetVariationParamValue( | 458 const std::string param_value = variations::GetVariationParamValue( |
440 kContextualSearchFieldTrialName, | 459 kContextualSearchFieldTrialName, |
441 kContextualSearchSurroundingSizeParamName); | 460 kContextualSearchSurroundingSizeParamName); |
442 int param_length; | 461 int param_length; |
443 if (!param_value.empty() && base::StringToInt(param_value, ¶m_length)) | 462 if (!param_value.empty() && base::StringToInt(param_value, ¶m_length)) |
444 return param_length; | 463 return param_length; |
445 return kContextualSearchDefaultContentSize; | 464 return kContextualSearchDefaultContentSize; |
446 } | 465 } |
447 | 466 |
467 // Extract the Start/End of the mentions in the surrounding text | |
468 // for selection-expansion. | |
469 void ContextualSearchDelegate::ExtractMentionsStartEnd( | |
470 const base::ListValue& mentions_list, | |
471 size_t* startResult, | |
472 size_t* endResult) { | |
473 int int_value; | |
474 if (mentions_list.GetInteger(0, &int_value)) | |
475 *startResult = int_value; | |
476 if (mentions_list.GetInteger(1, &int_value)) | |
477 *endResult = int_value; | |
478 } | |
479 | |
448 // Returns the size of the surroundings to be sent to Icing. | 480 // Returns the size of the surroundings to be sent to Icing. |
449 int ContextualSearchDelegate::GetIcingSurroundingSize() { | 481 int ContextualSearchDelegate::GetIcingSurroundingSize() { |
450 std::string param_string = variations::GetVariationParamValue( | 482 std::string param_string = variations::GetVariationParamValue( |
451 kContextualSearchFieldTrialName, | 483 kContextualSearchFieldTrialName, |
452 kContextualSearchIcingSurroundingSizeParamName); | 484 kContextualSearchIcingSurroundingSizeParamName); |
453 if (base::CommandLine::ForCurrentProcess()->HasSwitch( | 485 if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
454 kContextualSearchIcingSurroundingSizeParamName)) { | 486 kContextualSearchIcingSurroundingSizeParamName)) { |
455 param_string = base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( | 487 param_string = base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( |
456 kContextualSearchIcingSurroundingSizeParamName); | 488 kContextualSearchIcingSurroundingSizeParamName); |
457 } | 489 } |
(...skipping 23 matching lines...) Expand all Loading... | |
481 end_offset -= trim; | 513 end_offset -= trim; |
482 } | 514 } |
483 if (result_text.length() > end_offset + padding_each_side_pinned) { | 515 if (result_text.length() > end_offset + padding_each_side_pinned) { |
484 // Trim the end. | 516 // Trim the end. |
485 result_text = result_text.substr(0, end_offset + padding_each_side_pinned); | 517 result_text = result_text.substr(0, end_offset + padding_each_side_pinned); |
486 } | 518 } |
487 *start = start_offset; | 519 *start = start_offset; |
488 *end = end_offset; | 520 *end = end_offset; |
489 return result_text; | 521 return result_text; |
490 } | 522 } |
OLD | NEW |