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

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: Fixed some unit test issues 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/memory/scoped_ptr.h"
12 #include "url/gurl.h"
13
14 namespace base {
15 class DictionaryValue;
16 }
17
18 // Structured representation of the JSON payload of a suggestion with an answer.
19 // An answer has exactly two image lines, so called because they are a
20 // combination of text and an optional image URL. Each image line has 1 or more
21 // text fields, each of which is required to contain a string and an integer
22 // type. The text fields are contained in a non-empty vector and two optional
23 // named properties, referred to as "additional text" and "status text".
24 //
25 // When represented in the UI, these elements should be styled and laid out
26 // according to the specification at https://goto.google.com/ais_api.
27 class SuggestionAnswer {
28 public:
29 class TextField {
30 public:
31 TextField();
32 ~TextField();
33
34 static bool ParseTextField(
35 const base::DictionaryValue* field_json, TextField* text_field);
36
37 const std::string& text() const { return text_; }
38 int type() const { return type_; }
39
40 bool Equals(const TextField& field) const;
41
42 private:
43 std::string text_;
44 int type_;
45 };
Peter Kasting 2014/10/30 03:43:29 You still need all of these classes to be copyable
Justin Donnelly 2014/10/30 19:23:14 Done.
46
47 typedef std::vector<TextField> TextFields;
48
49 class ImageLine {
50 public:
51 ImageLine();
52 ImageLine(const ImageLine& line);
Peter Kasting 2014/10/30 03:43:29 Nit: Explicit
Justin Donnelly 2014/10/30 19:23:14 Done.
53 ~ImageLine();
54
55 static bool ParseImageLine(
56 const base::DictionaryValue* line_json, ImageLine* image_line);
57
58 const TextFields& text_fields() const { return text_fields_; }
59 const TextField* additional_text() const { return additional_text_.get(); }
60 const TextField* status_text() const { return status_text_.get(); }
61 const GURL& image_url() const { return image_url_; }
62
63 bool Equals(const ImageLine& line) const;
64
65 private:
66 TextFields text_fields_;
67 scoped_ptr<TextField> additional_text_;
68 scoped_ptr<TextField> status_text_;
69 GURL image_url_;
70 };
71
72 SuggestionAnswer();
73 SuggestionAnswer(const SuggestionAnswer& answer);
74 ~SuggestionAnswer();
75
76 // Parses |answer_json| and populates |answer| with the contents if and only
77 // if the data is well formed and populated with all required elements.
78 static bool ParseAnswer(
79 const std::string& answer_json, scoped_ptr<SuggestionAnswer>* answer);
Peter Kasting 2014/10/30 03:43:29 Is this wrapper just a test-only helper? If so, I
Justin Donnelly 2014/10/30 19:23:14 Done.
80 static bool ParseAnswer(
81 const base::DictionaryValue* answer_json,
82 scoped_ptr<SuggestionAnswer>* answer);
83
84 static scoped_ptr<SuggestionAnswer> Copy(const SuggestionAnswer* source) {
Peter Kasting 2014/10/30 03:43:29 Is it worth noting a TODO that we could eliminate
Justin Donnelly 2014/10/30 19:23:14 Done.
85 return make_scoped_ptr(source ? new SuggestionAnswer(*source) : nullptr);
Peter Kasting 2014/10/30 03:43:29 Nit: Either call this copy(), or don't inline it.
Justin Donnelly 2014/10/30 19:23:14 Done.
86 }
87
88 const ImageLine& first_line() const { return first_line_; }
89 const ImageLine& second_line() const { return second_line_; }
90
91 // Answer type accessors. Valid types are non-negative and defined at
92 // https://goto.google.com/visual_element_configuration.
93 int type() const { return type_; }
Peter Kasting 2014/10/30 03:43:29 Should we have an enum that we keep synced to said
groby-ooo-7-16 2014/10/30 16:21:14 It should be opaque to Chrome. We _might_ need to
Justin Donnelly 2014/10/30 19:23:14 Acknowledged.
94 void set_type(int type) { type_ = type; }
95
96 bool Equals(const SuggestionAnswer& answer) const;
97
98 typedef std::vector<GURL> URLs;
Peter Kasting 2014/10/30 03:43:30 Nit: typedefs and enums go near the top of the sec
Justin Donnelly 2014/10/30 19:23:14 Without forward declaration, TextFields has to go
99
100 // Gets any URLs of any images in Answers results. Any existing URLs in
101 // |urls| will be removed.
102 void GetImageURLs(URLs* urls) const;
103
104 private:
105 ImageLine first_line_;
106 ImageLine second_line_;
107 int type_;
108 };
109
110 #endif // COMPONENTS_OMNIBOX_SUGGESTION_ANSWER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698