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

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

Issue 2127373006: Use base::StartWith() in more places when appropriate. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebase, resolve conflict Created 4 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
« no previous file with comments | « no previous file | chrome/browser/chromeos/extensions/device_local_account_management_policy_provider.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 409 matching lines...) Expand 10 before | Expand all | Expand 10 after
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 (!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
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 }
OLDNEW
« no previous file with comments | « no previous file | chrome/browser/chromeos/extensions/device_local_account_management_policy_provider.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698