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

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, fix a copy bug 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 "base/gtest_prod_util.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "url/gurl.h"
14
15 namespace base {
16 class DictionaryValue;
17 }
18
19 // Structured representation of the JSON payload of a suggestion with an answer.
20 // An answer has exactly two image lines, so called because they are a
21 // combination of text and an optional image URL. Each image line has 1 or more
22 // text fields, each of which is required to contain a string and an integer
23 // type. The text fields are contained in a non-empty vector and two optional
24 // named properties, referred to as "additional text" and "status text".
25 //
26 // When represented in the UI, these elements should be styled and laid out
27 // according to the specification at https://goto.google.com/ais_api.
28 //
29 // Each of the three classes has either an explicit or implicity copy
30 // constructor to support copying answer values (via SuggestionAnswer::copy) as
31 // members of SuggestResult and AutocompleteMatch.
32 class SuggestionAnswer {
33 public:
34 class TextField;
35 typedef std::vector<TextField> TextFields;
36 typedef std::vector<GURL> URLs;
37
38 class TextField {
39 public:
40 TextField();
41 ~TextField();
42
43 static bool ParseTextField(
groby-ooo-7-16 2014/10/30 20:28:29 should probably document that Parse can leave the
Justin Donnelly 2014/10/30 21:38:04 Done.
44 const base::DictionaryValue* field_json, TextField* text_field);
groby-ooo-7-16 2014/10/30 20:28:29 minor style nit: static bool ParseTextField(const
Justin Donnelly 2014/10/30 21:38:03 Done.
45
46 const std::string& text() const { return text_; }
47 int type() const { return type_; }
48
49 bool Equals(const TextField& field) const;
50
51 private:
52 std::string text_;
53 int type_;
54
55 FRIEND_TEST_ALL_PREFIXES(SuggestionAnswerTest, DifferentValuesAreUnequal);
groby-ooo-7-16 2014/10/30 20:28:30 I commented in reverse - see below.
Justin Donnelly 2014/10/30 21:38:04 Acknowledged.
56 };
57
58 class ImageLine {
59 public:
60 ImageLine();
61 explicit ImageLine(const ImageLine& line);
62 ~ImageLine();
63
64 static bool ParseImageLine(
65 const base::DictionaryValue* line_json, ImageLine* image_line);
groby-ooo-7-16 2014/10/30 20:28:29 See above
Justin Donnelly 2014/10/30 21:38:03 Done.
66
67 const TextFields& text_fields() const { return text_fields_; }
68 const TextField* additional_text() const { return additional_text_.get(); }
69 const TextField* status_text() const { return status_text_.get(); }
70 const GURL& image_url() const { return image_url_; }
71
72 bool Equals(const ImageLine& line) const;
73
74 private:
75 TextFields text_fields_;
76 scoped_ptr<TextField> additional_text_;
77 scoped_ptr<TextField> status_text_;
groby-ooo-7-16 2014/10/30 20:28:30 Since this has copy ctor/assignment op allowed, yo
Justin Donnelly 2014/10/30 21:38:03 Done.
78 GURL image_url_;
79
80 FRIEND_TEST_ALL_PREFIXES(SuggestionAnswerTest, DifferentValuesAreUnequal);
groby-ooo-7-16 2014/10/30 20:28:30 Do you need this? All fields are accessible via ge
Justin Donnelly 2014/10/30 21:38:03 Yes, the getters return const values. If you read
groby-ooo-7-16 2014/10/30 22:13:58 Acknowledged.
81 };
82
83 SuggestionAnswer();
84 SuggestionAnswer(const SuggestionAnswer& answer);
85 ~SuggestionAnswer();
86
87 // Parses |answer_json| and populates |answer| with the contents if and only
88 // if the data is well formed and populated with all required elements.
89 static scoped_ptr<SuggestionAnswer> ParseAnswer(
90 const base::DictionaryValue* answer_json);
91
92 // TODO(jdonnelly): Once something like std::optional<T> is available in base/
93 // (see discussion at http://goo.gl/zN2GNy) remove this in favor of having
94 // SuggestResult and AutocompleteMatch use optional<SuggestionAnswer>.
95 static scoped_ptr<SuggestionAnswer> copy(const SuggestionAnswer* source) {
96 return make_scoped_ptr(source ? new SuggestionAnswer(*source) : nullptr);
97 }
98
99 const ImageLine& first_line() const { return first_line_; }
100 const ImageLine& second_line() const { return second_line_; }
101
102 // Answer type accessors. Valid types are non-negative and defined at
103 // https://goto.google.com/visual_element_configuration.
104 int type() const { return type_; }
105 void set_type(int type) { type_ = type; }
106
107 bool Equals(const SuggestionAnswer& answer) const;
108
109 // Retrieves any image URLs appearing in this answer and adds them to |urls|.
110 void AddImageURLsTo(URLs* urls) const;
111
112 private:
113 ImageLine first_line_;
114 ImageLine second_line_;
115 int type_;
116
117 FRIEND_TEST_ALL_PREFIXES(SuggestionAnswerTest, DifferentValuesAreUnequal);
groby-ooo-7-16 2014/10/30 20:28:30 Do you need this? You can access all variables via
Justin Donnelly 2014/10/30 21:38:03 Acknowledged.
118 };
119
120 #endif // COMPONENTS_OMNIBOX_SUGGESTION_ANSWER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698