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

Unified Diff: components/omnibox/suggestion_answer.h

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.h
diff --git a/components/omnibox/suggestion_answer.h b/components/omnibox/suggestion_answer.h
new file mode 100644
index 0000000000000000000000000000000000000000..1bfdc328428588a90cdd3000575c7ead4c00fbc5
--- /dev/null
+++ b/components/omnibox/suggestion_answer.h
@@ -0,0 +1,142 @@
+// 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.
+
+#ifndef COMPONENTS_OMNIBOX_SUGGESTION_ANSWER_H_
+#define COMPONENTS_OMNIBOX_SUGGESTION_ANSWER_H_
+
+#include <string>
+#include <vector>
+
+#include "url/gurl.h"
+
+namespace base {
+class DictionaryValue;
+}
+
+// Structured representation of the JSON payload of a suggestion with an answer.
+// An answer has exactly two image lines, so called because they are a
+// combination of text and an optional image URL. Each image line has 1 or more
+// text fields, each of which is required to contain a string and an integer
+// type. The text fields are contained in a non-empty vector and two optional
+// named properties, referred to as "additional text" and "status text".
+//
+// When represented in the UI, these elements should be styled and laid out
+// according to the specification at https://goto.google.com/ais_api.
+class SuggestionAnswer {
+ public:
+ class TextField {
+ public:
+ // Constructs an empty, invalid field.
+ TextField();
+ ~TextField();
+
+ static void ParseTextField(
+ const base::DictionaryValue* field_json, TextField* text_field);
+
+ bool is_valid() const { return !text_.empty() && type_ >= 0; }
+
+ const std::string& text() const { return text_; }
+ int type() const { return type_; }
+
+ bool Equals(const TextField& field) const;
+
+ // Removes all data. After this call, the field will be invalid.
+ void Clear();
+
+ private:
+ std::string text_;
+ int type_;
+ };
+
+ typedef std::vector<TextField> TextFields;
+
+ class ImageLine {
+ public:
+ // Constructs an empty, invalid line.
+ ImageLine();
+ ~ImageLine();
+
+ static void ParseImageLine(
+ const base::DictionaryValue* line_json, ImageLine* image_line);
+
+ // A line is only valid if the JSON representation was successfully parsed
+ // and all expected constraints were met (primarily, that there was at least
+ // one text field in the repeated text field property).
+ bool is_valid() const { return is_valid_; }
+
+ const TextFields& text_fields() const { return text_fields_; }
+
+ bool has_additional_text() const { return additional_text_.is_valid(); }
+ const TextField& additional_text() const { return additional_text_; }
+
+ bool has_status_text() const { return status_text_.is_valid(); }
+ const TextField& status_text() const { return status_text_; }
+
+ bool has_image_url() const { return image_url_.is_valid(); }
+ const GURL& image_url() const { return image_url_; }
+
+ bool Equals(const ImageLine& line) const;
+
+ // Removes all data. After this call, the line will be invalid.
+ void Clear();
+
+ private:
+ TextFields text_fields_;
+ TextField additional_text_;
+ TextField status_text_;
+ GURL image_url_;
+ bool is_valid_;
+ };
+
+ // Constructs an empty, invalid answer.
+ SuggestionAnswer();
+ ~SuggestionAnswer();
+
+ // Parses |answer_json| and populates |answer| with the contents. When this
+ // method returns, |answer| is guaranteed to either be valid and containing
+ // the contents of |answer_json| or empty (from a call to Clear()) if the data
+ // is malformed or missing required elements.
+ static void ParseAnswer(
+ const std::string& answer_json, SuggestionAnswer* answer);
+ static void ParseAnswer(
+ const base::DictionaryValue* answer_json, SuggestionAnswer* answer);
+
+ // An answer is valid only if a type has been set, the JSON representation was
+ // successfully parsed, and all expected constraints were met (primarily that
+ // there were exactly two properly formed image lines).
+ bool is_valid() const {
+ return type_ >= 0 && first_line_.is_valid() && second_line_.is_valid();
+ }
+
+ const ImageLine& first_line() const { return first_line_; }
+ const ImageLine& second_line() const { return second_line_; }
+
+ // Answer type accessors. Valid types must be non-negative and
+ // are defined at https://goto.google.com/visual_element_configuration.
+ int type() const { return type_; }
+ void set_type(int type) { type_ = type; }
+
+ bool Equals(const SuggestionAnswer& answer) const;
+
+ // Removes all data. After this call, the answer will be invalid.
+ void Clear();
+
+ typedef std::vector<GURL> URLs;
+
+ // Gets any URLs of any images in Answers results. Any existing URLs in
+ // |urls| will be removed.
+ void GetImageURLs(URLs* urls) const;
+
+ private:
+ ImageLine first_line_;
+ ImageLine second_line_;
+ int type_;
+
+ // No DISALLOW_COPY_AND_ASSIGN because the copy and assignment operators for
+ // SuggestionAnswer (and its inner classes) are used in AutocompleteMatch's
+ // copy constructor and assignment operator as well as in search provider
+ // logic.
+};
+
+#endif // COMPONENTS_OMNIBOX_SUGGESTION_ANSWER_H_

Powered by Google App Engine
This is Rietveld 408576698