| 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 409 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 420 // the value of the given parameters. | 420 // the value of the given parameters. |
| 421 void ContextualSearchDelegate::DecodeSearchTermFromJsonResponse( | 421 void ContextualSearchDelegate::DecodeSearchTermFromJsonResponse( |
| 422 const std::string& response, | 422 const std::string& response, |
| 423 std::string* search_term, | 423 std::string* search_term, |
| 424 std::string* display_text, | 424 std::string* display_text, |
| 425 std::string* alternate_term, | 425 std::string* alternate_term, |
| 426 std::string* prevent_preload, | 426 std::string* prevent_preload, |
| 427 int* mention_start, | 427 int* mention_start, |
| 428 int* mention_end, | 428 int* mention_end, |
| 429 std::string* lang) { | 429 std::string* lang) { |
| 430 bool contains_xssi_escape = response.find(kXssiEscape) == 0; | 430 bool contains_xssi_escape = |
| 431 base::StartsWith(response, kXssiEscape, base::CompareCase::SENSITIVE); |
| 431 const std::string& proper_json = | 432 const std::string& proper_json = |
| 432 contains_xssi_escape ? response.substr(strlen(kXssiEscape)) : response; | 433 contains_xssi_escape ? response.substr(sizeof(kXssiEscape) - 1) |
| 434 : response; |
| 433 JSONStringValueDeserializer deserializer(proper_json); | 435 JSONStringValueDeserializer deserializer(proper_json); |
| 434 std::unique_ptr<base::Value> root = deserializer.Deserialize(NULL, NULL); | 436 std::unique_ptr<base::Value> root = |
| 437 deserializer.Deserialize(nullptr, nullptr); |
| 438 std::unique_ptr<base::DictionaryValue> dict = |
| 439 base::DictionaryValue::From(std::move(root)); |
| 440 if (!root_dict) |
| 441 return; |
| 435 | 442 |
| 436 if (root.get() != NULL && root->IsType(base::Value::TYPE_DICTIONARY)) { | 443 dict->GetString(kContextualSearchPreventPreload, prevent_preload); |
| 437 base::DictionaryValue* dict = | 444 dict->GetString(kContextualSearchResponseSearchTermParam, search_term); |
| 438 static_cast<base::DictionaryValue*>(root.get()); | 445 dict->GetString(kContextualSearchResponseLanguageParam, lang); |
| 439 dict->GetString(kContextualSearchPreventPreload, prevent_preload); | 446 |
| 440 dict->GetString(kContextualSearchResponseSearchTermParam, search_term); | 447 // For the display_text, if not present fall back to the "search_term". |
| 441 dict->GetString(kContextualSearchResponseLanguageParam, lang); | 448 if (!dict->GetString(kContextualSearchResponseDisplayTextParam, |
| 442 // For the display_text, if not present fall back to the "search_term". | 449 display_text)) { |
| 443 if (!dict->GetString(kContextualSearchResponseDisplayTextParam, | 450 *display_text = *search_term; |
| 444 display_text)) { | 451 } |
| 445 *display_text = *search_term; | 452 |
| 446 } | 453 // Extract mentions for selection expansion. |
| 447 // Extract mentions for selection expansion. | 454 if (!field_trial_->IsDecodeMentionsDisabled()) { |
| 448 if (!field_trial_->IsDecodeMentionsDisabled()) { | 455 base::ListValue* mentions_list = nullptr; |
| 449 base::ListValue* mentions_list = NULL; | 456 dict->GetList(kContextualSearchMentions, &mentions_list); |
| 450 dict->GetList(kContextualSearchMentions, &mentions_list); | 457 if (mentions_list && mentions_list->GetSize() >= 2) |
| 451 if (mentions_list != NULL && mentions_list->GetSize() >= 2) | 458 ExtractMentionsStartEnd(*mentions_list, mention_start, mention_end); |
| 452 ExtractMentionsStartEnd(*mentions_list, mention_start, mention_end); | 459 } |
| 453 } | 460 |
| 454 // If either the selected text or the resolved term is not the search term, | 461 // If either the selected text or the resolved term is not the search term, |
| 455 // use it as the alternate term. | 462 // use it as the alternate term. |
| 456 std::string selected_text; | 463 std::string selected_text; |
| 457 dict->GetString(kContextualSearchResponseSelectedTextParam, &selected_text); | 464 dict->GetString(kContextualSearchResponseSelectedTextParam, &selected_text); |
| 458 if (selected_text != *search_term) { | 465 if (selected_text != *search_term) { |
| 459 *alternate_term = selected_text; | 466 *alternate_term = selected_text; |
| 460 } else { | 467 } else { |
| 461 std::string resolved_term; | 468 std::string resolved_term; |
| 462 dict->GetString(kContextualSearchResponseResolvedTermParam, | 469 dict->GetString(kContextualSearchResponseResolvedTermParam, &resolved_term); |
| 463 &resolved_term); | 470 if (resolved_term != *search_term) { |
| 464 if (resolved_term != *search_term) { | 471 *alternate_term = resolved_term; |
| 465 *alternate_term = resolved_term; | |
| 466 } | |
| 467 } | 472 } |
| 468 } | 473 } |
| 469 } | 474 } |
| 470 | 475 |
| 471 // Extract the Start/End of the mentions in the surrounding text | 476 // Extract the Start/End of the mentions in the surrounding text |
| 472 // for selection-expansion. | 477 // for selection-expansion. |
| 473 void ContextualSearchDelegate::ExtractMentionsStartEnd( | 478 void ContextualSearchDelegate::ExtractMentionsStartEnd( |
| 474 const base::ListValue& mentions_list, | 479 const base::ListValue& mentions_list, |
| 475 int* startResult, | 480 int* startResult, |
| 476 int* endResult) { | 481 int* endResult) { |
| (...skipping 24 matching lines...) Expand all Loading... |
| 501 end_offset -= trim; | 506 end_offset -= trim; |
| 502 } | 507 } |
| 503 if (result_text.length() > end_offset + padding_each_side_pinned) { | 508 if (result_text.length() > end_offset + padding_each_side_pinned) { |
| 504 // Trim the end. | 509 // Trim the end. |
| 505 result_text = result_text.substr(0, end_offset + padding_each_side_pinned); | 510 result_text = result_text.substr(0, end_offset + padding_each_side_pinned); |
| 506 } | 511 } |
| 507 *start = start_offset; | 512 *start = start_offset; |
| 508 *end = end_offset; | 513 *end = end_offset; |
| 509 return result_text; | 514 return result_text; |
| 510 } | 515 } |
| OLD | NEW |