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.
5 #ifndef COMPONENTS_OMNIBOX_SUGGESTION_ANSWER_H_
6 #define COMPONENTS_OMNIBOX_SUGGESTION_ANSWER_H_
11 #include "base/gtest_prod_util.h"
12 #include "base/memory/scoped_ptr.h"
16 class DictionaryValue
;
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".
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.
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
{
35 typedef std::vector
<TextField
> TextFields
;
36 typedef std::vector
<GURL
> URLs
;
38 // These values are named and numbered to match a specification at go/ais_api.
39 // The values are only used for answer results.
45 DESCRIPTION_NEGATIVE
= 5,
46 DESCRIPTION_POSITIVE
= 6,
49 SUGGESTION_POSITIVE
= 9,
50 SUGGESTION_NEGATIVE
= 10,
53 PERSONALIZED_SUGGESTION
= 13,
61 // Parses |field_json| and populates |text_field| with the contents. If any
62 // of the required elements is missing, returns false and leaves text_field
63 // in a partially populated state.
64 static bool ParseTextField(const base::DictionaryValue
* field_json
,
65 TextField
* text_field
);
67 const base::string16
& text() const { return text_
; }
68 int type() const { return type_
; }
70 bool Equals(const TextField
& field
) const;
76 FRIEND_TEST_ALL_PREFIXES(SuggestionAnswerTest
, DifferentValuesAreUnequal
);
78 // No DISALLOW_COPY_AND_ASSIGN since we depend on the copy constructor in
79 // SuggestionAnswer::copy and the assigment operator as values in vector.
85 explicit ImageLine(const ImageLine
& line
);
88 // Parses |line_json| and populates |image_line| with the contents. If any
89 // of the required elements is missing, returns false and leaves text_field
90 // in a partially populated state.
91 static bool ParseImageLine(const base::DictionaryValue
* line_json
,
92 ImageLine
* image_line
);
94 const TextFields
& text_fields() const { return text_fields_
; }
95 const TextField
* additional_text() const { return additional_text_
.get(); }
96 const TextField
* status_text() const { return status_text_
.get(); }
97 const GURL
& image_url() const { return image_url_
; }
99 bool Equals(const ImageLine
& line
) const;
102 // Forbid assignment.
103 ImageLine
& operator=(const ImageLine
&);
105 TextFields text_fields_
;
106 scoped_ptr
<TextField
> additional_text_
;
107 scoped_ptr
<TextField
> status_text_
;
110 FRIEND_TEST_ALL_PREFIXES(SuggestionAnswerTest
, DifferentValuesAreUnequal
);
114 SuggestionAnswer(const SuggestionAnswer
& answer
);
117 // Parses |answer_json| and returns a SuggestionAnswer containing the
118 // contents. If the supplied data is not well formed or is missing required
119 // elements, returns nullptr instead.
120 static scoped_ptr
<SuggestionAnswer
> ParseAnswer(
121 const base::DictionaryValue
* answer_json
);
123 // TODO(jdonnelly): Once something like std::optional<T> is available in base/
124 // (see discussion at http://goo.gl/zN2GNy) remove this in favor of having
125 // SuggestResult and AutocompleteMatch use optional<SuggestionAnswer>.
126 static scoped_ptr
<SuggestionAnswer
> copy(const SuggestionAnswer
* source
) {
127 return make_scoped_ptr(source
? new SuggestionAnswer(*source
) : nullptr);
130 const ImageLine
& first_line() const { return first_line_
; }
131 const ImageLine
& second_line() const { return second_line_
; }
133 // Answer type accessors. Valid types are non-negative and defined at
134 // https://goto.google.com/visual_element_configuration.
135 int type() const { return type_
; }
136 void set_type(int type
) { type_
= type
; }
138 bool Equals(const SuggestionAnswer
& answer
) const;
140 // Retrieves any image URLs appearing in this answer and adds them to |urls|.
141 void AddImageURLsTo(URLs
* urls
) const;
144 // Forbid assignment.
145 SuggestionAnswer
& operator=(const SuggestionAnswer
&);
147 ImageLine first_line_
;
148 ImageLine second_line_
;
151 FRIEND_TEST_ALL_PREFIXES(SuggestionAnswerTest
, DifferentValuesAreUnequal
);
154 #endif // COMPONENTS_OMNIBOX_SUGGESTION_ANSWER_H_