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

Unified Diff: components/omnibox/suggestion_answer.cc

Issue 669573005: Add a class to parse answer json. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Sync and respond to comments Created 6 years, 2 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: components/omnibox/suggestion_answer.cc
diff --git a/components/omnibox/suggestion_answer.cc b/components/omnibox/suggestion_answer.cc
new file mode 100644
index 0000000000000000000000000000000000000000..fd7bb52f7789b75d315631da718c7ddd4c2ff181
--- /dev/null
+++ b/components/omnibox/suggestion_answer.cc
@@ -0,0 +1,211 @@
+// Copyright 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "components/omnibox/suggestion_answer.h"
+
+#include "base/json/json_reader.h"
+#include "base/strings/string_util.h"
+#include "base/strings/utf_string_conversions.h"
+#include "base/values.h"
+#include "url/url_constants.h"
+
+namespace {
+
+// All of these are defined here (even though they're only used once each) so
+// the format details are easy to locate and update or compare to the spec doc.
+static const char* kAnswerJsonLines = "l";
+static const char* kAnswerJsonImageLine = "il";
+static const char* kAnswerJsonText = "t";
+static const char* kAnswerJsonAdditionalText = "at";
+static const char* kAnswerJsonStatusText = "st";
+static const char* kAnswerJsonTextType = "tt";
+static const char* kAnswerJsonImage = "i";
+static const char* kAnswerJsonImageData = "i.d";
+
+} // namespace
+
+// SuggestionAnswer::TextField -------------------------------------------------
+
+SuggestionAnswer::TextField::TextField() : type_(-1) {}
+SuggestionAnswer::TextField::~TextField() {}
+
+// static
+void SuggestionAnswer::TextField::ParseTextField(
+ const base::DictionaryValue* field_json, TextField* text_field) {
+ if (!field_json->GetString(kAnswerJsonText, &text_field->text_) ||
+ text_field->text_.empty())
+ return;
+ field_json->GetInteger(kAnswerJsonTextType, &text_field->type_);
+}
+
+bool SuggestionAnswer::TextField::Equals(const TextField& field) const {
+ return type_ == field.type_ && text_ == field.text_;
+}
+
+void SuggestionAnswer::TextField::Clear() {
+ text_.clear();
+ type_ = -1;
+}
+
+// SuggestionAnswer::ImageLine -------------------------------------------------
+
+SuggestionAnswer::ImageLine::ImageLine() : is_valid_(false) {}
+SuggestionAnswer::ImageLine::~ImageLine() {}
+
+// static
+void SuggestionAnswer::ImageLine::ParseImageLine(
+ const base::DictionaryValue* line_json, ImageLine* image_line) {
+ image_line->is_valid_ = false;
+
+ const base::DictionaryValue* inner_json;
+ if (!line_json->GetDictionary(kAnswerJsonImageLine, &inner_json))
+ return;
+
+ const base::ListValue* fields_json;
+ if (!inner_json->GetList(kAnswerJsonText, &fields_json) ||
+ fields_json->GetSize() == 0)
+ return;
+
+ for (size_t i = 0; i < fields_json->GetSize(); ++i) {
+ const base::DictionaryValue* field_json;
+ TextField text_field;
+ if (!fields_json->GetDictionary(i, &field_json))
+ return;
+ TextField::ParseTextField(field_json, &text_field);
+ if (!text_field.is_valid())
+ return;
+ image_line->text_fields_.push_back(text_field);
+ }
+
+ if (inner_json->HasKey(kAnswerJsonAdditionalText)) {
+ const base::DictionaryValue* field_json;
+ if (!inner_json->GetDictionary(kAnswerJsonAdditionalText, &field_json))
+ return;
+ TextField::ParseTextField(field_json, &image_line->additional_text_);
+ if (!image_line->additional_text_.is_valid())
+ return;
+ }
+
+ if (inner_json->HasKey(kAnswerJsonStatusText)) {
+ const base::DictionaryValue* field_json;
+ if (!inner_json->GetDictionary(kAnswerJsonStatusText, &field_json))
+ return;
+ TextField::ParseTextField(field_json, &image_line->status_text_);
+ if (!image_line->status_text_.is_valid())
+ return;
+ }
+
+ if (inner_json->HasKey(kAnswerJsonImage)) {
+ base::string16 url_string;
+ if (!inner_json->GetString(kAnswerJsonImageData, &url_string) ||
+ url_string.empty())
+ return;
+ // If necessary, concatenate scheme and host/path using only ':' as
+ // separator. This is due to the results delivering strings of the form
+ // "//host/path", which is web-speak for "use the enclosing page's scheme",
+ // but not a valid path of an URL. The GWS frontend commonly (always?)
+ // redirects to HTTPS so we just default to that here.
+ image_line->image_url_ = GURL(
+ StartsWith(url_string, base::ASCIIToUTF16("//"), false) ?
+ (base::ASCIIToUTF16(url::kHttpsScheme) + base::ASCIIToUTF16(":") +
+ url_string) :
+ url_string);
+
+ if (!image_line->image_url_.is_valid())
+ return;
+ }
+
+ image_line->is_valid_ = true;
+}
+
+bool SuggestionAnswer::ImageLine::Equals(const ImageLine& line) const {
+ if (text_fields_.size() != line.text_fields_.size())
+ return false;
+ for (size_t i = 0; i < text_fields_.size(); ++i) {
+ if (!text_fields_[i].Equals(line.text_fields_[i]))
+ return false;
+ }
+ return additional_text_.Equals(line.additional_text_) &&
+ status_text_.Equals(line.status_text_) &&
+ image_url_ == line.image_url_;
+}
+
+void SuggestionAnswer::ImageLine::Clear() {
+ text_fields_.clear();
+ additional_text_.Clear();
+ status_text_.Clear();
+ image_url_ = GURL();
+ is_valid_ = false;
+}
+
+// SuggestionAnswer ------------------------------------------------------------
+
+SuggestionAnswer::SuggestionAnswer() : type_(-1) {}
+SuggestionAnswer::~SuggestionAnswer() {}
+
+// static
+void SuggestionAnswer::ParseAnswer(
+ const std::string& answer_json, SuggestionAnswer* answer) {
+ scoped_ptr<base::Value> value(base::JSONReader::Read(answer_json));
+ base::DictionaryValue* dict;
+ if (!value.get() || !value->GetAsDictionary(&dict))
+ return;
+
+ ParseAnswer(dict, answer);
+}
+
+// static
+void SuggestionAnswer::ParseAnswer(
+ const base::DictionaryValue* answer_json, SuggestionAnswer* answer) {
+ const base::ListValue* lines_json;
+ if (!answer_json->GetList(kAnswerJsonLines, &lines_json) ||
+ lines_json->GetSize() != 2)
+ return;
+
+ const base::DictionaryValue* first_line_json;
+ if (!lines_json->GetDictionary(0, &first_line_json)) {
+ answer->Clear();
+ return;
+ }
+ ImageLine::ParseImageLine(first_line_json, &answer->first_line_);
+ if (!answer->first_line_.is_valid()) {
+ answer->Clear();
+ return;
+ }
+
+ const base::DictionaryValue* second_line_json;
+ if (!lines_json->GetDictionary(1, &second_line_json)) {
+ answer->Clear();
+ return;
+ }
+ ImageLine::ParseImageLine(second_line_json, &answer->second_line_);
+ if (!answer->second_line_.is_valid()) {
+ answer->Clear();
+ return;
+ }
+}
+
+bool SuggestionAnswer::Equals(const SuggestionAnswer& answer) const {
+ return type_ == answer.type_ &&
+ first_line_.Equals(answer.first_line_) &&
+ second_line_.Equals(answer.second_line_);
+}
+
+void SuggestionAnswer::Clear() {
+ first_line_.Clear();
+ second_line_.Clear();
+ type_ = -1;
+}
+
+void SuggestionAnswer::GetImageURLs(std::vector<GURL>* urls) const {
+ urls->clear();
+
+ if (!is_valid())
+ return;
+
+ if (first_line_.has_image_url())
+ urls->push_back(first_line_.image_url());
+ if (second_line_.has_image_url())
+ urls->push_back(second_line_.image_url());
+}

Powered by Google App Engine
This is Rietveld 408576698