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

Side by Side 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, 1 month 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 unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifndef COMPONENTS_OMNIBOX_SUGGESTION_ANSWER_H_
6 #define COMPONENTS_OMNIBOX_SUGGESTION_ANSWER_H_
7
8 #include <string>
9 #include <vector>
10
11 #include "url/gurl.h"
12
13 namespace base {
14 class DictionaryValue;
15 }
16
17 // Structured representation of the JSON payload of a suggestion with an answer.
18 // An answer has exactly two image lines, so called because they are a
19 // combination of text and an optional image URL. Each image line has 1 or more
20 // text fields, each of which is required to contain a string and an integer
21 // type. The text fields are contained in a non-empty vector and two optional
22 // named properties, referred to as "additional text" and "status text".
23 //
24 // When represented in the UI, these elements should be styled and laid out
25 // according to the specification at https://goto.google.com/ais_api.
26 class SuggestionAnswer {
27 public:
28 class TextField {
29 public:
30 // Constructs an empty, invalid field.
31 TextField();
32 ~TextField();
33
34 static void ParseTextField(
35 const base::DictionaryValue* field_json, TextField* text_field);
36
37 bool is_valid() const { return !text_.empty() && type_ >= 0; }
38
39 const std::string& text() const { return text_; }
40 int type() const { return type_; }
41
42 bool Equals(const TextField& field) const;
43
44 // Removes all data. After this call, the field will be invalid.
45 void Clear();
46
47 private:
48 std::string text_;
49 int type_;
50 };
51
52 typedef std::vector<TextField> TextFields;
53
54 class ImageLine {
55 public:
56 // Constructs an empty, invalid line.
57 ImageLine();
58 ~ImageLine();
59
60 static void ParseImageLine(
61 const base::DictionaryValue* line_json, ImageLine* image_line);
62
63 // A line is only valid if the JSON representation was successfully parsed
64 // and all expected constraints were met (primarily, that there was at least
65 // one text field in the repeated text field property).
66 bool is_valid() const { return is_valid_; }
67
68 const TextFields& text_fields() const { return text_fields_; }
69
70 bool has_additional_text() const { return additional_text_.is_valid(); }
71 const TextField& additional_text() const { return additional_text_; }
72
73 bool has_status_text() const { return status_text_.is_valid(); }
74 const TextField& status_text() const { return status_text_; }
75
76 bool has_image_url() const { return image_url_.is_valid(); }
77 const GURL& image_url() const { return image_url_; }
78
79 bool Equals(const ImageLine& line) const;
80
81 // Removes all data. After this call, the line will be invalid.
82 void Clear();
83
84 private:
85 TextFields text_fields_;
86 TextField additional_text_;
87 TextField status_text_;
88 GURL image_url_;
89 bool is_valid_;
90 };
91
92 // Constructs an empty, invalid answer.
93 SuggestionAnswer();
94 ~SuggestionAnswer();
95
96 // Parses |answer_json| and populates |answer| with the contents. When this
97 // method returns, |answer| is guaranteed to either be valid and containing
98 // the contents of |answer_json| or empty (from a call to Clear()) if the data
99 // is malformed or missing required elements.
100 static void ParseAnswer(
101 const std::string& answer_json, SuggestionAnswer* answer);
102 static void ParseAnswer(
103 const base::DictionaryValue* answer_json, SuggestionAnswer* answer);
104
105 // An answer is valid only if a type has been set, the JSON representation was
106 // successfully parsed, and all expected constraints were met (primarily that
107 // there were exactly two properly formed image lines).
108 bool is_valid() const {
109 return type_ >= 0 && first_line_.is_valid() && second_line_.is_valid();
110 }
111
112 const ImageLine& first_line() const { return first_line_; }
113 const ImageLine& second_line() const { return second_line_; }
114
115 // Answer type accessors. Valid types must be non-negative and
116 // are defined at https://goto.google.com/visual_element_configuration.
117 int type() const { return type_; }
118 void set_type(int type) { type_ = type; }
119
120 bool Equals(const SuggestionAnswer& answer) const;
121
122 // Removes all data. After this call, the answer will be invalid.
123 void Clear();
124
125 typedef std::vector<GURL> URLs;
126
127 // Gets any URLs of any images in Answers results. Any existing URLs in
128 // |urls| will be removed.
129 void GetImageURLs(URLs* urls) const;
130
131 private:
132 ImageLine first_line_;
133 ImageLine second_line_;
134 int type_;
135
136 // No DISALLOW_COPY_AND_ASSIGN because the copy and assignment operators for
137 // SuggestionAnswer (and its inner classes) are used in AutocompleteMatch's
138 // copy constructor and assignment operator as well as in search provider
139 // logic.
140 };
141
142 #endif // COMPONENTS_OMNIBOX_SUGGESTION_ANSWER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698