Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(151)

Side by Side Diff: chrome/browser/android/contextualsearch/contextual_search_delegate.cc

Issue 1205033005: Adds selection expansion support for Contextual Search. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: creis nits Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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
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 end_adjust = mention_end - context_->end_offset;
palmer 2015/07/07 18:39:59 I'm finding the implications of the implicit type
aurimas (slooooooooow) 2015/07/09 00:20:50 I added limits to make sure selection is non-zero
162
163 // TODO(aurimas): look into why sometimes we try to adjust by -1.
164 // See crbug.com/506381
165 start_adjust = std::max(0, start_adjust);
166 end_adjust = std::max(0, end_adjust);
167 }
153 } 168 }
154 } 169 }
155 bool is_invalid = response_code == net::URLFetcher::RESPONSE_CODE_INVALID; 170 bool is_invalid = response_code == net::URLFetcher::RESPONSE_CODE_INVALID;
156 search_term_callback_.Run( 171 search_term_callback_.Run(
157 is_invalid, response_code, search_term, display_text, alternate_term, 172 is_invalid, response_code, search_term, display_text, alternate_term,
158 prevent_preload == kDoPreventPreloadValue); 173 prevent_preload == kDoPreventPreloadValue, start_adjust, end_adjust);
159 174
160 // The ContextualSearchContext is consumed once the request has completed. 175 // The ContextualSearchContext is consumed once the request has completed.
161 context_.reset(); 176 context_.reset();
162 } 177 }
163 178
164 // TODO(jeremycho): Remove selected_text and base_page_url CGI parameters. 179 // TODO(jeremycho): Remove selected_text and base_page_url CGI parameters.
165 GURL ContextualSearchDelegate::BuildRequestUrl() { 180 GURL ContextualSearchDelegate::BuildRequestUrl() {
166 // TODO(jeremycho): Confirm this is the right way to handle TemplateURL fails. 181 // TODO(jeremycho): Confirm this is the right way to handle TemplateURL fails.
167 if (!template_url_service_ || 182 if (!template_url_service_ ||
168 !template_url_service_->GetDefaultSearchProvider()) { 183 !template_url_service_->GetDefaultSearchProvider()) {
(...skipping 223 matching lines...) Expand 10 before | Expand all | Expand 10 after
392 return true; 407 return true;
393 } 408 }
394 409
395 // Decodes the given response from the search term resolution request and sets 410 // Decodes the given response from the search term resolution request and sets
396 // the value of the given parameters. 411 // the value of the given parameters.
397 void ContextualSearchDelegate::DecodeSearchTermsFromJsonResponse( 412 void ContextualSearchDelegate::DecodeSearchTermsFromJsonResponse(
398 const std::string& response, 413 const std::string& response,
399 std::string* search_term, 414 std::string* search_term,
400 std::string* display_text, 415 std::string* display_text,
401 std::string* alternate_term, 416 std::string* alternate_term,
402 std::string* prevent_preload) { 417 std::string* prevent_preload,
418 size_t* mention_start,
419 size_t* mention_end) {
403 bool contains_xssi_escape = response.find(kXssiEscape) == 0; 420 bool contains_xssi_escape = response.find(kXssiEscape) == 0;
404 const std::string& proper_json = 421 const std::string& proper_json =
405 contains_xssi_escape ? response.substr(strlen(kXssiEscape)) : response; 422 contains_xssi_escape ? response.substr(strlen(kXssiEscape)) : response;
406 JSONStringValueDeserializer deserializer(proper_json); 423 JSONStringValueDeserializer deserializer(proper_json);
407 scoped_ptr<base::Value> root(deserializer.Deserialize(NULL, NULL)); 424 scoped_ptr<base::Value> root(deserializer.Deserialize(NULL, NULL));
408 425
409 if (root.get() != NULL && root->IsType(base::Value::TYPE_DICTIONARY)) { 426 if (root.get() != NULL && root->IsType(base::Value::TYPE_DICTIONARY)) {
410 base::DictionaryValue* dict = 427 base::DictionaryValue* dict =
411 static_cast<base::DictionaryValue*>(root.get()); 428 static_cast<base::DictionaryValue*>(root.get());
412 dict->GetString(kContextualSearchPreventPreload, prevent_preload); 429 dict->GetString(kContextualSearchPreventPreload, prevent_preload);
413 dict->GetString(kContextualSearchResponseSearchTermParam, search_term); 430 dict->GetString(kContextualSearchResponseSearchTermParam, search_term);
414 // For the display_text, if not present fall back to the "search_term". 431 // For the display_text, if not present fall back to the "search_term".
415 if (!dict->GetString(kContextualSearchResponseDisplayTextParam, 432 if (!dict->GetString(kContextualSearchResponseDisplayTextParam,
416 display_text)) { 433 display_text)) {
417 *display_text = *search_term; 434 *display_text = *search_term;
418 } 435 }
436 // Extract mentions for selection expansion.
437 base::ListValue* mentions_list;
438 dict->GetList(kContextualSearchMentions, &mentions_list);
439 if (mentions_list != NULL && mentions_list->GetSize() >= 2)
440 ExtractMentionsStartEnd(*mentions_list, mention_start, mention_end);
419 // If either the selected text or the resolved term is not the search term, 441 // If either the selected text or the resolved term is not the search term,
420 // use it as the alternate term. 442 // use it as the alternate term.
421 std::string selected_text; 443 std::string selected_text;
422 dict->GetString(kContextualSearchResponseSelectedTextParam, &selected_text); 444 dict->GetString(kContextualSearchResponseSelectedTextParam, &selected_text);
423 if (selected_text != *search_term) { 445 if (selected_text != *search_term) {
424 *alternate_term = selected_text; 446 *alternate_term = selected_text;
425 } else { 447 } else {
426 std::string resolved_term; 448 std::string resolved_term;
427 dict->GetString(kContextualSearchResponseResolvedTermParam, 449 dict->GetString(kContextualSearchResponseResolvedTermParam,
428 &resolved_term); 450 &resolved_term);
429 if (resolved_term != *search_term) { 451 if (resolved_term != *search_term) {
430 *alternate_term = resolved_term; 452 *alternate_term = resolved_term;
431 } 453 }
432 } 454 }
433 } 455 }
434 } 456 }
435 457
436 // Returns the size of the surroundings to be sent to the server for search term 458 // Returns the size of the surroundings to be sent to the server for search term
437 // resolution. 459 // resolution.
438 int ContextualSearchDelegate::GetSearchTermSurroundingSize() { 460 int ContextualSearchDelegate::GetSearchTermSurroundingSize() {
439 const std::string param_value = variations::GetVariationParamValue( 461 const std::string param_value = variations::GetVariationParamValue(
440 kContextualSearchFieldTrialName, 462 kContextualSearchFieldTrialName,
441 kContextualSearchSurroundingSizeParamName); 463 kContextualSearchSurroundingSizeParamName);
442 int param_length; 464 int param_length;
443 if (!param_value.empty() && base::StringToInt(param_value, &param_length)) 465 if (!param_value.empty() && base::StringToInt(param_value, &param_length))
444 return param_length; 466 return param_length;
445 return kContextualSearchDefaultContentSize; 467 return kContextualSearchDefaultContentSize;
446 } 468 }
447 469
470 // Extract the Start/End of the mentions in the surrounding text
471 // for selection-expansion.
472 void ContextualSearchDelegate::ExtractMentionsStartEnd(
473 const base::ListValue& mentions_list,
474 size_t* startResult,
475 size_t* endResult) {
476 int int_value;
477 if (mentions_list.GetInteger(0, &int_value))
478 *startResult = int_value;
palmer 2015/07/07 18:39:59 More strange type-cast semantics. It's OK if the u
aurimas (slooooooooow) 2015/07/09 00:20:50 It now makes it 0 or larger.
479 if (mentions_list.GetInteger(1, &int_value))
480 *endResult = int_value;
481 }
482
448 // Returns the size of the surroundings to be sent to Icing. 483 // Returns the size of the surroundings to be sent to Icing.
449 int ContextualSearchDelegate::GetIcingSurroundingSize() { 484 int ContextualSearchDelegate::GetIcingSurroundingSize() {
450 std::string param_string = variations::GetVariationParamValue( 485 std::string param_string = variations::GetVariationParamValue(
451 kContextualSearchFieldTrialName, 486 kContextualSearchFieldTrialName,
452 kContextualSearchIcingSurroundingSizeParamName); 487 kContextualSearchIcingSurroundingSizeParamName);
453 if (base::CommandLine::ForCurrentProcess()->HasSwitch( 488 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
454 kContextualSearchIcingSurroundingSizeParamName)) { 489 kContextualSearchIcingSurroundingSizeParamName)) {
455 param_string = base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII( 490 param_string = base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
456 kContextualSearchIcingSurroundingSizeParamName); 491 kContextualSearchIcingSurroundingSizeParamName);
457 } 492 }
(...skipping 23 matching lines...) Expand all
481 end_offset -= trim; 516 end_offset -= trim;
482 } 517 }
483 if (result_text.length() > end_offset + padding_each_side_pinned) { 518 if (result_text.length() > end_offset + padding_each_side_pinned) {
484 // Trim the end. 519 // Trim the end.
485 result_text = result_text.substr(0, end_offset + padding_each_side_pinned); 520 result_text = result_text.substr(0, end_offset + padding_each_side_pinned);
486 } 521 }
487 *start = start_offset; 522 *start = start_offset;
488 *end = end_offset; 523 *end = end_offset;
489 return result_text; 524 return result_text;
490 } 525 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698