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

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: 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..db73dbe0163d659f2973428ee960d0fdf788ef8b
--- /dev/null
+++ b/components/omnibox/suggestion_answer.h
@@ -0,0 +1,107 @@
+// 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 "base/memory/scoped_ptr.h"
+#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 layed out
+// according to the specification at http://goto.google.com/ais_api.
+class SuggestionAnswer {
+ public:
+ class TextField {
+ public:
+ TextField();
+ ~TextField();
+
+ static bool ParseTextField(
groby-ooo-7-16 2014/10/21 00:35:54 Why not have a simple struct, plus a factory? Save
Justin Donnelly 2014/10/21 21:43:54 I like the consistency of design across these thre
+ const base::DictionaryValue* field_json, TextField* text_field);
+
+ bool operator==(const TextField& field) const;
+ bool operator!=(const TextField& field) const;
+
+ const std::string& text() const { return text_; }
+ int type() const { return type_; }
+
+ void Clear();
+
+ private:
+ std::string text_;
+ int type_;
+ };
+
+ typedef std::vector<TextField> TextFields;
groby-ooo-7-16 2014/10/21 00:35:54 I wonder if TextFields should be a single string +
Justin Donnelly 2014/10/21 21:43:54 My concern with this is that we discussed a simila
+
+ class ImageLine {
+ public:
+ 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;
+
+ const std::vector<TextField>& text_fields() const { return text_fields_; }
+ const TextField& additional_text() const { return additional_text_; }
+ const TextField& status_text() const { return status_text_; }
+
+ bool HasImageURL() const;
+ const GURL& image_url() const { return image_url_; }
+
+ void Clear();
+
+ private:
+ TextFields text_fields_;
+ TextField additional_text_;
+ TextField status_text_;
+ GURL image_url_;
+ };
+
+ 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;
+
+ const ImageLine& first_line() const { return first_line_; }
+ const ImageLine& second_line() const { return second_line_; }
+ int type() const { return type_; }
+ bool is_valid() const { return is_valid_; }
+
+ // 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 0.
+ void SetType(const base::string16& type);
+ void Clear();
+
+ private:
+ ImageLine first_line_;
+ ImageLine second_line_;
+ int type_;
+ bool is_valid_;
+};
+
+#endif // COMPONENTS_OMNIBOX_SUGGESTION_ANSWER_H_

Powered by Google App Engine
This is Rietveld 408576698