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

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

Issue 2743273002: [Doodle] Don't create invalid DoodleImages or DoodleConfigs (Closed)
Patch Set: add checks for absence of optional images 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" 7 #include "base/values.h"
8 8
9 namespace doodle { 9 namespace doodle {
10 10
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
59 } 59 }
60 return DoodleImage::FromDictionary(*image_dict, base_url); 60 return DoodleImage::FromDictionary(*image_dict, base_url);
61 } 61 }
62 62
63 } // namespace 63 } // namespace
64 64
65 // static 65 // static
66 base::Optional<DoodleImage> DoodleImage::FromDictionary( 66 base::Optional<DoodleImage> DoodleImage::FromDictionary(
67 const base::DictionaryValue& dict, 67 const base::DictionaryValue& dict,
68 const base::Optional<GURL>& base_url) { 68 const base::Optional<GURL>& base_url) {
69 DoodleImage image;
70
71 // The URL is the only required field. 69 // The URL is the only required field.
72 image.url = ParseUrl(dict, "url", base_url); 70 GURL url = ParseUrl(dict, "url", base_url);
73 if (!image.url.is_valid()) { 71 if (!url.is_valid()) {
74 return base::nullopt; 72 return base::nullopt;
75 } 73 }
76 74
75 DoodleImage image(url);
76
77 dict.GetInteger("height", &image.height); 77 dict.GetInteger("height", &image.height);
78 dict.GetInteger("width", &image.width); 78 dict.GetInteger("width", &image.width);
79 dict.GetBoolean("is_animated_gif", &image.is_animated_gif); 79 dict.GetBoolean("is_animated_gif", &image.is_animated_gif);
80 dict.GetBoolean("is_cta", &image.is_cta); 80 dict.GetBoolean("is_cta", &image.is_cta);
81 81
82 return image; 82 return image;
83 } 83 }
84 84
85 DoodleImage::DoodleImage() 85 DoodleImage::DoodleImage(const GURL& url)
86 : height(0), width(0), is_animated_gif(false), is_cta(false) {} 86 : url(url), height(0), width(0), is_animated_gif(false), is_cta(false) {
87 DCHECK(url.is_valid());
88 }
89
87 DoodleImage::~DoodleImage() = default; 90 DoodleImage::~DoodleImage() = default;
88 91
89 bool DoodleImage::operator==(const DoodleImage& other) const { 92 bool DoodleImage::operator==(const DoodleImage& other) const {
90 return url == other.url && height == other.height && width == other.width && 93 return url == other.url && height == other.height && width == other.width &&
91 is_animated_gif == other.is_animated_gif && is_cta == other.is_cta; 94 is_animated_gif == other.is_animated_gif && is_cta == other.is_cta;
92 } 95 }
93 96
94 bool DoodleImage::operator!=(const DoodleImage& other) const { 97 bool DoodleImage::operator!=(const DoodleImage& other) const {
95 return !(*this == other); 98 return !(*this == other);
96 } 99 }
97 100
98 DoodleConfig::DoodleConfig() : doodle_type(DoodleType::UNKNOWN) {} 101 DoodleConfig::DoodleConfig(DoodleType doodle_type,
102 const DoodleImage& large_image)
103 : doodle_type(doodle_type), large_image(large_image) {}
99 DoodleConfig::DoodleConfig(const DoodleConfig& config) = default; 104 DoodleConfig::DoodleConfig(const DoodleConfig& config) = default;
100 DoodleConfig::~DoodleConfig() = default; 105 DoodleConfig::~DoodleConfig() = default;
101 106
102 // static 107 // static
103 base::Optional<DoodleConfig> DoodleConfig::FromDictionary( 108 base::Optional<DoodleConfig> DoodleConfig::FromDictionary(
104 const base::DictionaryValue& dict, 109 const base::DictionaryValue& dict,
105 const base::Optional<GURL>& base_url) { 110 const base::Optional<GURL>& base_url) {
106 DoodleConfig doodle;
107
108 // The |large_image| field is required (it's the "default" representation for 111 // The |large_image| field is required (it's the "default" representation for
109 // the doodle). 112 // the doodle).
110 base::Optional<DoodleImage> large_image = 113 base::Optional<DoodleImage> large_image =
111 ParseImage(dict, "large_image", base_url); 114 ParseImage(dict, "large_image", base_url);
112 if (!large_image.has_value()) { 115 if (!large_image.has_value()) {
113 return base::nullopt; 116 return base::nullopt;
114 } 117 }
115 doodle.large_image = large_image.value();
116 118
117 std::string type_str; 119 std::string type_str;
118 dict.GetString("doodle_type", &type_str); 120 dict.GetString("doodle_type", &type_str);
119 doodle.doodle_type = DoodleTypeFromString(type_str); 121
122 DoodleConfig doodle(DoodleTypeFromString(type_str), large_image.value());
120 123
121 dict.GetString("alt_text", &doodle.alt_text); 124 dict.GetString("alt_text", &doodle.alt_text);
122 125
123 dict.GetString("interactive_html", &doodle.interactive_html); 126 dict.GetString("interactive_html", &doodle.interactive_html);
124 127
125 doodle.search_url = ParseUrl(dict, "search_url", base_url); 128 doodle.search_url = ParseUrl(dict, "search_url", base_url);
126 doodle.target_url = ParseUrl(dict, "target_url", base_url); 129 doodle.target_url = ParseUrl(dict, "target_url", base_url);
127 doodle.fullpage_interactive_url = 130 doodle.fullpage_interactive_url =
128 ParseUrl(dict, "fullpage_interactive_url", base_url); 131 ParseUrl(dict, "fullpage_interactive_url", base_url);
129 132
(...skipping 18 matching lines...) Expand all
148 large_image == other.large_image && 151 large_image == other.large_image &&
149 large_cta_image == other.large_cta_image && 152 large_cta_image == other.large_cta_image &&
150 transparent_large_image == other.transparent_large_image; 153 transparent_large_image == other.transparent_large_image;
151 } 154 }
152 155
153 bool DoodleConfig::operator!=(const DoodleConfig& other) const { 156 bool DoodleConfig::operator!=(const DoodleConfig& other) const {
154 return !(*this == other); 157 return !(*this == other);
155 } 158 }
156 159
157 } // namespace doodle 160 } // 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