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

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: Tweaked API and finished unit tests 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 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
26 class SuggestionAnswer {
27 public:
28 class TextField {
29 public:
30 // Construct an empty, invalid field.
31 TextField();
32 ~TextField();
33
34 static bool ParseTextField(
35 const base::DictionaryValue* field_json, TextField* text_field);
36
37 bool operator==(const TextField& field) const;
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 // Remove all data. After this call, the field will be invalid.
46 void Clear();
47
48 private:
49 std::string text_;
50 int type_;
51 };
52
53 typedef std::vector<TextField> TextFields;
54
55 class ImageLine {
56 public:
57 // Construct an empty, invalid line.
58 ImageLine();
59 ~ImageLine();
60
61 static bool 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_; }
73
74 bool has_additional_text() const { return additional_text_.is_valid(); }
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 // Remove 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 };
93
94 // Construct an empty, invalid answer.
95 SuggestionAnswer();
96 ~SuggestionAnswer();
97
98 static bool ParseAnswer(
99 const std::string& answer_json, SuggestionAnswer* answer);
100 static bool 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
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 // Specify 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 // 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
121 void SetType(const std::string& type);
122 void SetType(const base::string16& type);
123
124 // Remove all data. After this call, the answer will be invalid.
125 void Clear();
126
127 private:
128 ImageLine first_line_;
129 ImageLine second_line_;
130 int type_;
131 };
132
133 #endif // COMPONENTS_OMNIBOX_SUGGESTION_ANSWER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698