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

Unified 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 side-by-side diff with in-line comments
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 »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: chrome/browser/android/contextualsearch/contextual_search_delegate.cc
diff --git a/chrome/browser/android/contextualsearch/contextual_search_delegate.cc b/chrome/browser/android/contextualsearch/contextual_search_delegate.cc
index c4048e61a0eb6442a3c39abee759e2c4e72cce84..7c1d27a3030bf79bcc8f5e5618463d8504d597e7 100644
--- a/chrome/browser/android/contextualsearch/contextual_search_delegate.cc
+++ b/chrome/browser/android/contextualsearch/contextual_search_delegate.cc
@@ -427,43 +427,48 @@ void ContextualSearchDelegate::DecodeSearchTermFromJsonResponse(
int* mention_start,
int* mention_end,
std::string* lang) {
- bool contains_xssi_escape = response.find(kXssiEscape) == 0;
+ bool contains_xssi_escape =
+ base::StartsWith(response, kXssiEscape, base::CompareCase::SENSITIVE);
const std::string& proper_json =
- contains_xssi_escape ? response.substr(strlen(kXssiEscape)) : response;
+ contains_xssi_escape ? response.substr(sizeof(kXssiEscape) - 1)
+ : response;
JSONStringValueDeserializer deserializer(proper_json);
- std::unique_ptr<base::Value> root = deserializer.Deserialize(NULL, NULL);
-
- if (root.get() != NULL && root->IsType(base::Value::TYPE_DICTIONARY)) {
- base::DictionaryValue* dict =
- static_cast<base::DictionaryValue*>(root.get());
- dict->GetString(kContextualSearchPreventPreload, prevent_preload);
- dict->GetString(kContextualSearchResponseSearchTermParam, search_term);
- dict->GetString(kContextualSearchResponseLanguageParam, lang);
- // For the display_text, if not present fall back to the "search_term".
- if (!dict->GetString(kContextualSearchResponseDisplayTextParam,
- display_text)) {
- *display_text = *search_term;
- }
- // Extract mentions for selection expansion.
- if (!field_trial_->IsDecodeMentionsDisabled()) {
- base::ListValue* mentions_list = NULL;
- dict->GetList(kContextualSearchMentions, &mentions_list);
- if (mentions_list != NULL && mentions_list->GetSize() >= 2)
- ExtractMentionsStartEnd(*mentions_list, mention_start, mention_end);
- }
- // If either the selected text or the resolved term is not the search term,
- // use it as the alternate term.
- std::string selected_text;
- dict->GetString(kContextualSearchResponseSelectedTextParam, &selected_text);
- if (selected_text != *search_term) {
- *alternate_term = selected_text;
- } else {
- std::string resolved_term;
- dict->GetString(kContextualSearchResponseResolvedTermParam,
- &resolved_term);
- if (resolved_term != *search_term) {
- *alternate_term = resolved_term;
- }
+ std::unique_ptr<base::Value> root =
+ deserializer.Deserialize(nullptr, nullptr);
+ std::unique_ptr<base::DictionaryValue> dict =
+ base::DictionaryValue::From(std::move(root));
+ if (!dict)
+ return;
+
+ dict->GetString(kContextualSearchPreventPreload, prevent_preload);
+ dict->GetString(kContextualSearchResponseSearchTermParam, search_term);
+ dict->GetString(kContextualSearchResponseLanguageParam, lang);
+
+ // For the display_text, if not present fall back to the "search_term".
+ if (!dict->GetString(kContextualSearchResponseDisplayTextParam,
+ display_text)) {
+ *display_text = *search_term;
+ }
+
+ // Extract mentions for selection expansion.
+ if (!field_trial_->IsDecodeMentionsDisabled()) {
+ base::ListValue* mentions_list = nullptr;
+ dict->GetList(kContextualSearchMentions, &mentions_list);
+ if (mentions_list && mentions_list->GetSize() >= 2)
+ ExtractMentionsStartEnd(*mentions_list, mention_start, mention_end);
+ }
+
+ // If either the selected text or the resolved term is not the search term,
+ // use it as the alternate term.
+ std::string selected_text;
+ dict->GetString(kContextualSearchResponseSelectedTextParam, &selected_text);
+ if (selected_text != *search_term) {
+ *alternate_term = selected_text;
+ } else {
+ std::string resolved_term;
+ dict->GetString(kContextualSearchResponseResolvedTermParam, &resolved_term);
+ if (resolved_term != *search_term) {
+ *alternate_term = resolved_term;
}
}
}
« 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