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

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: 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..6c993c1b4261ab404280fab14dd3fc1b580e2174
--- /dev/null
+++ b/components/omnibox/suggestion_answer.h
@@ -0,0 +1,136 @@
+// 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 operator==(const TextField& field) const;
Peter Kasting 2014/10/24 22:38:11 Per the style guide, avoid defining == and != for
Justin Donnelly 2014/10/27 21:41:16 Done.
+ bool operator!=(const TextField& field) const;
+
+ bool is_valid() const { return !text_.empty() && type_ >= 0; }
+
+ const std::string& text() const { return text_; }
+ int type() const { return type_; }
+
+ // Removes all data. After this call, the field will be invalid.
+ void Clear();
+
+ private:
+ std::string text_;
+ int type_;
+ };
Peter Kasting 2014/10/24 22:38:10 I assume you can't use DISALLOW_COPY_AND_ASSIGN he
Justin Donnelly 2014/10/27 21:41:16 It's because copies of answers are made in Autocom
+
+ 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);
+
+ bool operator==(const ImageLine& line) const;
+ bool operator!=(const ImageLine& line) const;
+
+ // 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 std::vector<TextField>& text_fields() const { return text_fields_; }
Peter Kasting 2014/10/24 22:38:10 Nit: Use TextFields
Justin Donnelly 2014/10/27 21:41:16 Done.
+
+ bool has_additional_text() const { return additional_text_.is_valid(); }
Peter Kasting 2014/10/24 22:38:11 Nit: I'm a bit on the fence as to whether these ha
Justin Donnelly 2014/10/27 21:41:16 Fair question, but there's a Java API with Has met
+ 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_; }
+
+ // 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_;
+ };
Peter Kasting 2014/10/24 22:38:11 Nit: DISALLOW_COPY_AND_ASSIGN (2 places)
Justin Donnelly 2014/10/27 21:41:16 See above comment re: answer copying.
+
+ // Constructs an empty, invalid answer.
+ SuggestionAnswer();
+ ~SuggestionAnswer();
+
+ static void ParseAnswer(
+ const std::string& answer_json, SuggestionAnswer* answer);
+ static void ParseAnswer(
+ const base::DictionaryValue* answer_json, SuggestionAnswer* answer);
+
+ bool operator==(const SuggestionAnswer& answer) const;
+ bool operator!=(const SuggestionAnswer& answer) const;
+
+ // An answer is valid only if both a type has been set and the JSON
Peter Kasting 2014/10/24 22:38:10 Nit: Eliminate "both", add commas (since list seem
Justin Donnelly 2014/10/27 21:41:16 Done.
+ // 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_; }
+ int type() const { return type_; }
+
+ // Specifies the type of this answer. The string will be interpreted as a
+ // base10 integer. If it can't be parsed as such, the type will be set to -1.
+ // Valid types must be non-negatiave and are defined in
+ // https://goto.google.com/visual_element_configuration.
+ void SetType(const base::string16& type);
Peter Kasting 2014/10/24 22:38:10 All but one caller of this has a number to begin w
Justin Donnelly 2014/10/27 21:41:16 Done.
+
+ // Removes all data. After this call, the answer will be invalid.
+ void Clear();
+
+ // Gets any URLs of any images in Answers results. Any existing URLs in
+ // |urls| will be removed.
+ void GetImageURLs(std::vector<GURL>* urls) const;
Peter Kasting 2014/10/24 22:38:11 Nit: I suggest making a typedef for this
Justin Donnelly 2014/10/27 21:41:16 Seems to me that in the client classes (SearchSugg
Peter Kasting 2014/10/27 21:50:09 Terseness isn't the goal; the goal is to insulate
Justin Donnelly 2014/10/28 00:39:34 Interesting. I didn't actually know that that was
+
+ private:
+ ImageLine first_line_;
+ ImageLine second_line_;
+ int type_;
+};
+
+#endif // COMPONENTS_OMNIBOX_SUGGESTION_ANSWER_H_

Powered by Google App Engine
This is Rietveld 408576698