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

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: Tweaked API and finished unit tests 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..2df06705839c09a2deac55fea2ee66781438dd88
--- /dev/null
+++ b/components/omnibox/suggestion_answer.h
@@ -0,0 +1,133 @@
+// 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 http://goto.google.com/ais_api.
groby-ooo-7-16 2014/10/23 00:46:20 We probably shouldn't have that URL here.
Justin Donnelly 2014/10/23 17:59:37 Doesn't seem like this is a thing that's enforced:
groby-ooo-7-16 2014/10/23 21:59:54 You're right, it's not enforced. I'm probably over
+class SuggestionAnswer {
+ public:
+ class TextField {
+ public:
+ // Construct an empty, invalid field.
+ TextField();
+ ~TextField();
+
+ static bool ParseTextField(
+ const base::DictionaryValue* field_json, TextField* text_field);
+
+ bool operator==(const TextField& field) const;
+ 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_; }
+
+ // Remove 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:
+ // Construct an empty, invalid line.
+ ImageLine();
+ ~ImageLine();
+
+ static bool 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_; }
+
+ 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_; }
+
+ // Remove 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_;
+ };
+
+ // Construct an empty, invalid answer.
+ SuggestionAnswer();
+ ~SuggestionAnswer();
+
+ static bool ParseAnswer(
+ const std::string& answer_json, SuggestionAnswer* answer);
+ static bool 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
+ // 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_; }
+
+ // Specify 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
+ // google3/logs/gws/config/visual_element_configuration.
groby-ooo-7-16 2014/10/23 00:46:19 Probably also not :)
Justin Donnelly 2014/10/23 17:59:37 Ditto
groby-ooo-7-16 2014/10/23 21:59:54 Usually only referring to base or third_party. I'd
Justin Donnelly 2014/10/24 14:58:08 Switched to a go link. We should talk more offlin
+ void SetType(const std::string& type);
+ void SetType(const base::string16& type);
+
+ // Remove all data. After this call, the answer will be invalid.
+ void Clear();
+
+ private:
+ ImageLine first_line_;
+ ImageLine second_line_;
+ int type_;
+};
+
+#endif // COMPONENTS_OMNIBOX_SUGGESTION_ANSWER_H_

Powered by Google App Engine
This is Rietveld 408576698