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

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: 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 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 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.
38 bool operator!=(const TextField& field) const;
39
40 bool is_valid() const { return !text_.empty() && type_ >= 0; }
41
42 const std::string& text() const { return text_; }
43 int type() const { return type_; }
44
45 // Removes all data. After this call, the field will be invalid.
46 void Clear();
47
48 private:
49 std::string text_;
50 int type_;
51 };
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
52
53 typedef std::vector<TextField> TextFields;
54
55 class ImageLine {
56 public:
57 // Constructs an empty, invalid line.
58 ImageLine();
59 ~ImageLine();
60
61 static void ParseImageLine(
62 const base::DictionaryValue* line_json, ImageLine* image_line);
63
64 bool operator==(const ImageLine& line) const;
65 bool operator!=(const ImageLine& line) const;
66
67 // A line is only valid if the JSON representation was successfully parsed
68 // and all expected constraints were met (primarily, that there was at least
69 // one text field in the repeated text field property).
70 bool is_valid() const { return is_valid_; }
71
72 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.
73
74 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
75 const TextField& additional_text() const { return additional_text_; }
76
77 bool has_status_text() const { return status_text_.is_valid(); }
78 const TextField& status_text() const { return status_text_; }
79
80 bool has_image_url() const { return image_url_.is_valid(); }
81 const GURL& image_url() const { return image_url_; }
82
83 // Removes all data. After this call, the line will be invalid.
84 void Clear();
85
86 private:
87 TextFields text_fields_;
88 TextField additional_text_;
89 TextField status_text_;
90 GURL image_url_;
91 bool is_valid_;
92 };
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.
93
94 // Constructs an empty, invalid answer.
95 SuggestionAnswer();
96 ~SuggestionAnswer();
97
98 static void ParseAnswer(
99 const std::string& answer_json, SuggestionAnswer* answer);
100 static void ParseAnswer(
101 const base::DictionaryValue* answer_json, SuggestionAnswer* answer);
102
103 bool operator==(const SuggestionAnswer& answer) const;
104 bool operator!=(const SuggestionAnswer& answer) const;
105
106 // 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.
107 // representation was successfully parsed and all expected constraints were
108 // met (primarily that there were exactly two properly formed image lines).
109 bool is_valid() const {
110 return type_ >= 0 && first_line_.is_valid() && second_line_.is_valid();
111 }
112
113 const ImageLine& first_line() const { return first_line_; }
114 const ImageLine& second_line() const { return second_line_; }
115 int type() const { return type_; }
116
117 // Specifies the type of this answer. The string will be interpreted as a
118 // base10 integer. If it can't be parsed as such, the type will be set to -1.
119 // Valid types must be non-negatiave and are defined in
120 // https://goto.google.com/visual_element_configuration.
121 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.
122
123 // Removes all data. After this call, the answer will be invalid.
124 void Clear();
125
126 // Gets any URLs of any images in Answers results. Any existing URLs in
127 // |urls| will be removed.
128 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
129
130 private:
131 ImageLine first_line_;
132 ImageLine second_line_;
133 int type_;
134 };
135
136 #endif // COMPONENTS_OMNIBOX_SUGGESTION_ANSWER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698