Chromium Code Reviews| OLD | NEW |
|---|---|
| 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; | |
| 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 auto large_image = ParseImage(dict, "large_image", base_url); | |
|
fhorschig
2017/03/10 17:54:01
nit: That's a borderline-use of auto since the ret
Marc Treib
2017/03/13 09:10:22
Good catch - this was mostly me being too lazy to
| |
| 111 if (!large_image.has_value()) { | |
| 112 return base::nullopt; | |
| 113 } | |
| 114 doodle.large_image = large_image.value(); | |
| 115 | |
| 116 std::string type_str; | |
| 117 dict.GetString("doodle_type", &type_str); | |
| 118 doodle.doodle_type = DoodleTypeFromString(type_str); | |
| 119 | |
| 120 dict.GetString("alt_text", &doodle.alt_text); | |
| 121 | |
| 122 dict.GetString("interactive_html", &doodle.interactive_html); | |
| 123 | |
| 124 doodle.search_url = ParseUrl(dict, "search_url", base_url); | |
| 125 doodle.target_url = ParseUrl(dict, "target_url", base_url); | |
| 126 doodle.fullpage_interactive_url = | |
| 127 ParseUrl(dict, "fullpage_interactive_url", base_url); | |
| 128 | |
| 129 auto large_cta_image = ParseImage(dict, "large_cta_image", base_url); | |
| 130 if (large_cta_image.has_value()) { | |
| 131 doodle.large_cta_image = large_cta_image.value(); | |
| 132 } | |
| 133 auto transparent_large_image = | |
| 134 ParseImage(dict, "transparent_large_image", base_url); | |
| 135 if (transparent_large_image.has_value()) { | |
| 136 doodle.transparent_large_image = transparent_large_image.value(); | |
| 137 } | |
| 138 | |
| 139 return doodle; | |
| 140 } | |
| 141 | |
| 18 bool DoodleConfig::operator==(const DoodleConfig& other) const { | 142 bool DoodleConfig::operator==(const DoodleConfig& other) const { |
| 19 return doodle_type == other.doodle_type && alt_text == other.alt_text && | 143 return doodle_type == other.doodle_type && alt_text == other.alt_text && |
| 20 interactive_html == other.interactive_html && | 144 interactive_html == other.interactive_html && |
| 21 search_url == other.search_url && target_url == other.target_url && | 145 search_url == other.search_url && target_url == other.target_url && |
| 22 fullpage_interactive_url == other.fullpage_interactive_url && | 146 fullpage_interactive_url == other.fullpage_interactive_url && |
| 23 large_image == other.large_image && | 147 large_image == other.large_image && |
| 24 large_cta_image == other.large_cta_image && | 148 large_cta_image == other.large_cta_image && |
| 25 transparent_large_image == other.transparent_large_image; | 149 transparent_large_image == other.transparent_large_image; |
| 26 } | 150 } |
| 27 | 151 |
| 28 bool DoodleConfig::operator!=(const DoodleConfig& other) const { | 152 bool DoodleConfig::operator!=(const DoodleConfig& other) const { |
| 29 return !(*this == other); | 153 return !(*this == other); |
| 30 } | 154 } |
| 31 | 155 |
| 32 } // namespace doodle | 156 } // namespace doodle |
| OLD | NEW |