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

Unified Diff: chrome/browser/android/contextualsearch/contextual_search_delegate.cc

Issue 2351283002: [TTS] Decode Now on Tap results for v1 integration. (Closed)
Patch Set: Used command-line switch for test, rollback changes needed for the field trial subclass. Created 4 years, 3 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
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 ed8c9f70227b1b5134d783bc938f0b1247449987..1ac367e4eb8b59a20e96f9e64c7a0a67d1a58e9b 100644
--- a/chrome/browser/android/contextualsearch/contextual_search_delegate.cc
+++ b/chrome/browser/android/contextualsearch/contextual_search_delegate.cc
@@ -55,6 +55,12 @@ const char kDoPreventPreloadValue[] = "1";
// The number of characters that should be shown after the selected expression.
const int kSurroundingSizeForUI = 60;
+// Contextual Cards (aka Now on Tap) integration.
+const char kContextualSearchContextualCards[] = "contextual_cards";
+const char kContextualSearchCards[] = "cards";
+const char kContextualSearchSingleCard[] = "singleCard";
+const char kContextualSearchSubTitle[] = "subtitle";
+const char kContextualSearchThumbnailUri[] = "thumbnail.uri";
// The version of the Now on Tap API that we want to invoke.
const int kNowOnTapVersion = 1;
@@ -457,7 +463,7 @@ void ContextualSearchDelegate::DecodeSearchTermFromJsonResponse(
JSONStringValueDeserializer deserializer(proper_json);
std::unique_ptr<base::Value> root =
deserializer.Deserialize(nullptr, nullptr);
- std::unique_ptr<base::DictionaryValue> dict =
+ const std::unique_ptr<base::DictionaryValue> dict =
base::DictionaryValue::From(std::move(root));
if (!dict)
return;
@@ -496,7 +502,7 @@ void ContextualSearchDelegate::DecodeSearchTermFromJsonResponse(
}
if (field_trial_->IsNowOnTapBarIntegrationEnabled()) {
- // TODO(donnd): extract thumbnail_url and caption.
+ DecodeContextualCardsResponse(*dict.get(), caption, thumbnail_url);
}
}
@@ -540,3 +546,36 @@ base::string16 ContextualSearchDelegate::SurroundingTextForIcing(
*end = end_offset;
return result_text;
}
+
+void ContextualSearchDelegate::DecodeContextualCardsResponse(
+ const base::DictionaryValue& dict,
+ std::string* subtitle,
+ std::string* thumbnail) {
+ const base::DictionaryValue* contextual_cards_dict = nullptr;
+ if (!dict.GetDictionary(kContextualSearchContextualCards,
+ &contextual_cards_dict))
+ return;
+
+ const base::ListValue* card_list = nullptr;
+ if (!contextual_cards_dict->GetList(kContextualSearchCards, &card_list))
+ return;
+
+ // TODO(donnd): remove these DCHECKS. They are not needed since we never
+ // dereference if the values are missing.
+ DCHECK(card_list);
+ for (const auto& card : *card_list) {
+ const base::DictionaryValue* card_dict = nullptr;
+ if (!card->GetAsDictionary(&card_dict))
+ continue;
+
+ DCHECK(card_dict);
+ const base::DictionaryValue* single_card = nullptr;
+ if (!card_dict->GetDictionary(kContextualSearchSingleCard, &single_card))
+ continue;
+
+ DCHECK(single_card);
+ single_card->GetString(kContextualSearchSubTitle, subtitle);
+ single_card->GetString(kContextualSearchThumbnailUri, thumbnail);
+ return;
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698