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

Side by Side Diff: components/doodle/doodle_types.cc

Issue 2740013005: [Doodle] Move parsing from fetcher into the types themselves (Closed)
Patch Set: -auto Created 3 years, 9 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
« no previous file with comments | « components/doodle/doodle_types.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2017 The Chromium Authors. All rights reserved. 1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "components/doodle/doodle_types.h" 5 #include "components/doodle/doodle_types.h"
6 6
7 #include "base/values.h"
8
7 namespace doodle { 9 namespace doodle {
8 10
11 namespace {
12
13 DoodleType DoodleTypeFromString(const std::string& type_str) {
14 if (type_str == "SIMPLE") {
15 return DoodleType::SIMPLE;
16 }
17 if (type_str == "RANDOM") {
18 return DoodleType::RANDOM;
19 }
20 if (type_str == "VIDEO") {
21 return DoodleType::VIDEO;
22 }
23 if (type_str == "INTERACTIVE") {
24 return DoodleType::INTERACTIVE;
25 }
26 if (type_str == "INLINE_INTERACTIVE") {
27 return DoodleType::INLINE_INTERACTIVE;
28 }
29 if (type_str == "SLIDESHOW") {
30 return DoodleType::SLIDESHOW;
31 }
32 return DoodleType::UNKNOWN;
33 }
34
35 GURL ResolvePossiblyRelativeUrl(const std::string& url_str,
36 const base::Optional<GURL>& base_url) {
37 if (!base_url.has_value()) {
38 return GURL(url_str);
39 }
40 return base_url->Resolve(url_str);
41 }
42
43 GURL ParseUrl(const base::DictionaryValue& parent_dict,
44 const std::string& key,
45 const base::Optional<GURL>& base_url) {
46 std::string url_str;
47 if (!parent_dict.GetString(key, &url_str) || url_str.empty()) {
48 return GURL();
49 }
50 return ResolvePossiblyRelativeUrl(url_str, base_url);
51 }
52
53 base::Optional<DoodleImage> ParseImage(const base::DictionaryValue& parent_dict,
54 const std::string& key,
55 const base::Optional<GURL>& base_url) {
56 const base::DictionaryValue* image_dict = nullptr;
57 if (!parent_dict.GetDictionary(key, &image_dict)) {
58 return base::nullopt;
sfiera 2017/03/13 09:31:16 Doesn't seem like you're gaining anything by using
Marc Treib 2017/03/13 09:37:34 I was going for consistency with DoodleConfig. But
59 }
60 return DoodleImage::FromDictionary(*image_dict, base_url);
61 }
62
63 } // namespace
64
65 // static
66 base::Optional<DoodleImage> DoodleImage::FromDictionary(
67 const base::DictionaryValue& dict,
68 const base::Optional<GURL>& base_url) {
69 DoodleImage image;
70
71 // The URL is the only required field.
72 image.url = ParseUrl(dict, "url", base_url);
73 if (!image.url.is_valid()) {
74 return base::nullopt;
75 }
76
77 dict.GetInteger("height", &image.height);
78 dict.GetInteger("width", &image.width);
79 dict.GetBoolean("is_animated_gif", &image.is_animated_gif);
80 dict.GetBoolean("is_cta", &image.is_cta);
81
82 return image;
83 }
84
85 DoodleImage::DoodleImage()
86 : height(0), width(0), is_animated_gif(false), is_cta(false) {}
87 DoodleImage::~DoodleImage() = default;
88
9 bool DoodleImage::operator==(const DoodleImage& other) const { 89 bool DoodleImage::operator==(const DoodleImage& other) const {
10 return url == other.url && height == other.height && width == other.width && 90 return url == other.url && height == other.height && width == other.width &&
11 is_animated_gif == other.is_animated_gif && is_cta == other.is_cta; 91 is_animated_gif == other.is_animated_gif && is_cta == other.is_cta;
12 } 92 }
13 93
14 bool DoodleImage::operator!=(const DoodleImage& other) const { 94 bool DoodleImage::operator!=(const DoodleImage& other) const {
15 return !(*this == other); 95 return !(*this == other);
16 } 96 }
17 97
98 DoodleConfig::DoodleConfig() : doodle_type(DoodleType::UNKNOWN) {}
99 DoodleConfig::DoodleConfig(const DoodleConfig& config) = default;
100 DoodleConfig::~DoodleConfig() = default;
101
102 // static
103 base::Optional<DoodleConfig> DoodleConfig::FromDictionary(
104 const base::DictionaryValue& dict,
105 const base::Optional<GURL>& base_url) {
106 DoodleConfig doodle;
107
108 // The |large_image| field is required (it's the "default" representation for
109 // the doodle).
110 base::Optional<DoodleImage> large_image =
111 ParseImage(dict, "large_image", base_url);
112 if (!large_image.has_value()) {
113 return base::nullopt;
114 }
115 doodle.large_image = large_image.value();
116
117 std::string type_str;
118 dict.GetString("doodle_type", &type_str);
119 doodle.doodle_type = DoodleTypeFromString(type_str);
120
121 dict.GetString("alt_text", &doodle.alt_text);
122
123 dict.GetString("interactive_html", &doodle.interactive_html);
124
125 doodle.search_url = ParseUrl(dict, "search_url", base_url);
126 doodle.target_url = ParseUrl(dict, "target_url", base_url);
127 doodle.fullpage_interactive_url =
128 ParseUrl(dict, "fullpage_interactive_url", base_url);
129
130 auto large_cta_image = ParseImage(dict, "large_cta_image", base_url);
131 if (large_cta_image.has_value()) {
132 doodle.large_cta_image = large_cta_image.value();
133 }
134 auto transparent_large_image =
135 ParseImage(dict, "transparent_large_image", base_url);
136 if (transparent_large_image.has_value()) {
137 doodle.transparent_large_image = transparent_large_image.value();
138 }
139
140 return doodle;
141 }
142
18 bool DoodleConfig::operator==(const DoodleConfig& other) const { 143 bool DoodleConfig::operator==(const DoodleConfig& other) const {
19 return doodle_type == other.doodle_type && alt_text == other.alt_text && 144 return doodle_type == other.doodle_type && alt_text == other.alt_text &&
20 interactive_html == other.interactive_html && 145 interactive_html == other.interactive_html &&
21 search_url == other.search_url && target_url == other.target_url && 146 search_url == other.search_url && target_url == other.target_url &&
22 fullpage_interactive_url == other.fullpage_interactive_url && 147 fullpage_interactive_url == other.fullpage_interactive_url &&
23 large_image == other.large_image && 148 large_image == other.large_image &&
24 large_cta_image == other.large_cta_image && 149 large_cta_image == other.large_cta_image &&
25 transparent_large_image == other.transparent_large_image; 150 transparent_large_image == other.transparent_large_image;
26 } 151 }
27 152
28 bool DoodleConfig::operator!=(const DoodleConfig& other) const { 153 bool DoodleConfig::operator!=(const DoodleConfig& other) const {
29 return !(*this == other); 154 return !(*this == other);
30 } 155 }
31 156
32 } // namespace doodle 157 } // namespace doodle
OLDNEW
« no previous file with comments | « components/doodle/doodle_types.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698