| 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/memory/ptr_util.h" |
| 7 #include "base/values.h" | 8 #include "base/values.h" |
| 8 | 9 |
| 9 namespace doodle { | 10 namespace doodle { |
| 10 | 11 |
| 11 namespace { | 12 namespace { |
| 12 | 13 |
| 14 // String representations for the DoodleType enum. |
| 15 const char kDoodleTypeUnknown[] = "UNKNOWN"; |
| 16 const char kDoodleTypeSimple[] = "SIMPLE"; |
| 17 const char kDoodleTypeRandom[] = "RANDOM"; |
| 18 const char kDoodleTypeVideo[] = "VIDEO"; |
| 19 const char kDoodleTypeInteractive[] = "INTERACTIVE"; |
| 20 const char kDoodleTypeInlineInteractive[] = "INLINE_INTERACTIVE"; |
| 21 const char kDoodleTypeSlideshow[] = "SLIDESHOW"; |
| 22 |
| 23 // JSON keys for DoodleImage fields. |
| 24 const char kKeyUrl[] = "url"; |
| 25 const char kKeyHeight[] = "height"; |
| 26 const char kKeyWidth[] = "width"; |
| 27 const char kKeyIsAnimatedGif[] = "is_animated_gif"; |
| 28 const char kKeyIsCta[] = "is_cta"; |
| 29 |
| 30 // JSON keys for DoodleConfig fields. |
| 31 const char kKeyDoodleType[] = "doodle_type"; |
| 32 const char kKeyAltText[] = "alt_text"; |
| 33 const char kKeyInteractiveHtml[] = "interactive_html"; |
| 34 const char kKeySearchUrl[] = "search_url"; |
| 35 const char kKeyTargetUrl[] = "target_url"; |
| 36 const char kKeyFullpageInteractiveUrl[] = "fullpage_interactive_url"; |
| 37 const char kKeyLargeImage[] = "large_image"; |
| 38 const char kKeyLargeCtaImage[] = "large_cta_image"; |
| 39 const char kKeyTransparentLargeImage[] = "transparent_large_image"; |
| 40 |
| 41 std::string DoodleTypeToString(DoodleType type) { |
| 42 switch (type) { |
| 43 case DoodleType::UNKNOWN: |
| 44 return kDoodleTypeUnknown; |
| 45 case DoodleType::SIMPLE: |
| 46 return kDoodleTypeSimple; |
| 47 case DoodleType::RANDOM: |
| 48 return kDoodleTypeRandom; |
| 49 case DoodleType::VIDEO: |
| 50 return kDoodleTypeVideo; |
| 51 case DoodleType::INTERACTIVE: |
| 52 return kDoodleTypeInteractive; |
| 53 case DoodleType::INLINE_INTERACTIVE: |
| 54 return kDoodleTypeInlineInteractive; |
| 55 case DoodleType::SLIDESHOW: |
| 56 return kDoodleTypeSlideshow; |
| 57 } |
| 58 NOTREACHED(); |
| 59 return std::string(); |
| 60 } |
| 61 |
| 13 DoodleType DoodleTypeFromString(const std::string& type_str) { | 62 DoodleType DoodleTypeFromString(const std::string& type_str) { |
| 14 if (type_str == "SIMPLE") { | 63 if (type_str == kDoodleTypeSimple) { |
| 15 return DoodleType::SIMPLE; | 64 return DoodleType::SIMPLE; |
| 16 } | 65 } |
| 17 if (type_str == "RANDOM") { | 66 if (type_str == kDoodleTypeRandom) { |
| 18 return DoodleType::RANDOM; | 67 return DoodleType::RANDOM; |
| 19 } | 68 } |
| 20 if (type_str == "VIDEO") { | 69 if (type_str == kDoodleTypeVideo) { |
| 21 return DoodleType::VIDEO; | 70 return DoodleType::VIDEO; |
| 22 } | 71 } |
| 23 if (type_str == "INTERACTIVE") { | 72 if (type_str == kDoodleTypeInteractive) { |
| 24 return DoodleType::INTERACTIVE; | 73 return DoodleType::INTERACTIVE; |
| 25 } | 74 } |
| 26 if (type_str == "INLINE_INTERACTIVE") { | 75 if (type_str == kDoodleTypeInlineInteractive) { |
| 27 return DoodleType::INLINE_INTERACTIVE; | 76 return DoodleType::INLINE_INTERACTIVE; |
| 28 } | 77 } |
| 29 if (type_str == "SLIDESHOW") { | 78 if (type_str == kDoodleTypeSlideshow) { |
| 30 return DoodleType::SLIDESHOW; | 79 return DoodleType::SLIDESHOW; |
| 31 } | 80 } |
| 32 return DoodleType::UNKNOWN; | 81 return DoodleType::UNKNOWN; |
| 33 } | 82 } |
| 34 | 83 |
| 35 GURL ResolvePossiblyRelativeUrl(const std::string& url_str, | 84 GURL ResolvePossiblyRelativeUrl(const std::string& url_str, |
| 36 const base::Optional<GURL>& base_url) { | 85 const base::Optional<GURL>& base_url) { |
| 37 if (!base_url.has_value()) { | 86 if (!base_url.has_value()) { |
| 38 return GURL(url_str); | 87 return GURL(url_str); |
| 39 } | 88 } |
| (...skipping 20 matching lines...) Expand all Loading... |
| 60 return DoodleImage::FromDictionary(*image_dict, base_url); | 109 return DoodleImage::FromDictionary(*image_dict, base_url); |
| 61 } | 110 } |
| 62 | 111 |
| 63 } // namespace | 112 } // namespace |
| 64 | 113 |
| 65 // static | 114 // static |
| 66 base::Optional<DoodleImage> DoodleImage::FromDictionary( | 115 base::Optional<DoodleImage> DoodleImage::FromDictionary( |
| 67 const base::DictionaryValue& dict, | 116 const base::DictionaryValue& dict, |
| 68 const base::Optional<GURL>& base_url) { | 117 const base::Optional<GURL>& base_url) { |
| 69 // The URL is the only required field. | 118 // The URL is the only required field. |
| 70 GURL url = ParseUrl(dict, "url", base_url); | 119 GURL url = ParseUrl(dict, kKeyUrl, base_url); |
| 71 if (!url.is_valid()) { | 120 if (!url.is_valid()) { |
| 72 return base::nullopt; | 121 return base::nullopt; |
| 73 } | 122 } |
| 74 | 123 |
| 75 DoodleImage image(url); | 124 DoodleImage image(url); |
| 76 | 125 |
| 77 dict.GetInteger("height", &image.height); | 126 dict.GetInteger(kKeyHeight, &image.height); |
| 78 dict.GetInteger("width", &image.width); | 127 dict.GetInteger(kKeyWidth, &image.width); |
| 79 dict.GetBoolean("is_animated_gif", &image.is_animated_gif); | 128 dict.GetBoolean(kKeyIsAnimatedGif, &image.is_animated_gif); |
| 80 dict.GetBoolean("is_cta", &image.is_cta); | 129 dict.GetBoolean(kKeyIsCta, &image.is_cta); |
| 81 | 130 |
| 82 return image; | 131 return image; |
| 83 } | 132 } |
| 84 | 133 |
| 85 DoodleImage::DoodleImage(const GURL& url) | 134 DoodleImage::DoodleImage(const GURL& url) |
| 86 : url(url), height(0), width(0), is_animated_gif(false), is_cta(false) { | 135 : url(url), height(0), width(0), is_animated_gif(false), is_cta(false) { |
| 87 DCHECK(url.is_valid()); | 136 DCHECK(url.is_valid()); |
| 88 } | 137 } |
| 89 | 138 |
| 90 DoodleImage::~DoodleImage() = default; | 139 DoodleImage::~DoodleImage() = default; |
| 91 | 140 |
| 141 std::unique_ptr<base::DictionaryValue> DoodleImage::ToDictionary() const { |
| 142 auto dict = base::MakeUnique<base::DictionaryValue>(); |
| 143 dict->SetString(kKeyUrl, url.spec()); |
| 144 dict->SetInteger(kKeyHeight, height); |
| 145 dict->SetInteger(kKeyWidth, width); |
| 146 dict->SetBoolean(kKeyIsAnimatedGif, is_animated_gif); |
| 147 dict->SetBoolean(kKeyIsCta, is_cta); |
| 148 return dict; |
| 149 } |
| 150 |
| 92 bool DoodleImage::operator==(const DoodleImage& other) const { | 151 bool DoodleImage::operator==(const DoodleImage& other) const { |
| 93 return url == other.url && height == other.height && width == other.width && | 152 return url == other.url && height == other.height && width == other.width && |
| 94 is_animated_gif == other.is_animated_gif && is_cta == other.is_cta; | 153 is_animated_gif == other.is_animated_gif && is_cta == other.is_cta; |
| 95 } | 154 } |
| 96 | 155 |
| 97 bool DoodleImage::operator!=(const DoodleImage& other) const { | 156 bool DoodleImage::operator!=(const DoodleImage& other) const { |
| 98 return !(*this == other); | 157 return !(*this == other); |
| 99 } | 158 } |
| 100 | 159 |
| 101 DoodleConfig::DoodleConfig(DoodleType doodle_type, | 160 DoodleConfig::DoodleConfig(DoodleType doodle_type, |
| 102 const DoodleImage& large_image) | 161 const DoodleImage& large_image) |
| 103 : doodle_type(doodle_type), large_image(large_image) {} | 162 : doodle_type(doodle_type), large_image(large_image) {} |
| 104 DoodleConfig::DoodleConfig(const DoodleConfig& config) = default; | 163 DoodleConfig::DoodleConfig(const DoodleConfig& config) = default; |
| 105 DoodleConfig::~DoodleConfig() = default; | 164 DoodleConfig::~DoodleConfig() = default; |
| 106 | 165 |
| 107 // static | 166 // static |
| 108 base::Optional<DoodleConfig> DoodleConfig::FromDictionary( | 167 base::Optional<DoodleConfig> DoodleConfig::FromDictionary( |
| 109 const base::DictionaryValue& dict, | 168 const base::DictionaryValue& dict, |
| 110 const base::Optional<GURL>& base_url) { | 169 const base::Optional<GURL>& base_url) { |
| 111 // The |large_image| field is required (it's the "default" representation for | 170 // The |large_image| field is required (it's the "default" representation for |
| 112 // the doodle). | 171 // the doodle). |
| 113 base::Optional<DoodleImage> large_image = | 172 base::Optional<DoodleImage> large_image = |
| 114 ParseImage(dict, "large_image", base_url); | 173 ParseImage(dict, kKeyLargeImage, base_url); |
| 115 if (!large_image.has_value()) { | 174 if (!large_image.has_value()) { |
| 116 return base::nullopt; | 175 return base::nullopt; |
| 117 } | 176 } |
| 118 | 177 |
| 119 std::string type_str; | 178 std::string type_str; |
| 120 dict.GetString("doodle_type", &type_str); | 179 dict.GetString(kKeyDoodleType, &type_str); |
| 121 | 180 |
| 122 DoodleConfig doodle(DoodleTypeFromString(type_str), large_image.value()); | 181 DoodleConfig doodle(DoodleTypeFromString(type_str), large_image.value()); |
| 123 | 182 |
| 124 dict.GetString("alt_text", &doodle.alt_text); | 183 dict.GetString(kKeyAltText, &doodle.alt_text); |
| 125 | 184 |
| 126 dict.GetString("interactive_html", &doodle.interactive_html); | 185 dict.GetString(kKeyInteractiveHtml, &doodle.interactive_html); |
| 127 | 186 |
| 128 doodle.search_url = ParseUrl(dict, "search_url", base_url); | 187 doodle.search_url = ParseUrl(dict, kKeySearchUrl, base_url); |
| 129 doodle.target_url = ParseUrl(dict, "target_url", base_url); | 188 doodle.target_url = ParseUrl(dict, kKeyTargetUrl, base_url); |
| 130 doodle.fullpage_interactive_url = | 189 doodle.fullpage_interactive_url = |
| 131 ParseUrl(dict, "fullpage_interactive_url", base_url); | 190 ParseUrl(dict, kKeyFullpageInteractiveUrl, base_url); |
| 132 | 191 |
| 133 auto large_cta_image = ParseImage(dict, "large_cta_image", base_url); | 192 doodle.large_cta_image = ParseImage(dict, kKeyLargeCtaImage, base_url); |
| 134 if (large_cta_image.has_value()) { | 193 doodle.transparent_large_image = |
| 135 doodle.large_cta_image = large_cta_image.value(); | 194 ParseImage(dict, kKeyTransparentLargeImage, base_url); |
| 136 } | |
| 137 auto transparent_large_image = | |
| 138 ParseImage(dict, "transparent_large_image", base_url); | |
| 139 if (transparent_large_image.has_value()) { | |
| 140 doodle.transparent_large_image = transparent_large_image.value(); | |
| 141 } | |
| 142 | 195 |
| 143 return doodle; | 196 return doodle; |
| 144 } | 197 } |
| 145 | 198 |
| 199 std::unique_ptr<base::DictionaryValue> DoodleConfig::ToDictionary() const { |
| 200 auto dict = base::MakeUnique<base::DictionaryValue>(); |
| 201 dict->SetString(kKeyDoodleType, DoodleTypeToString(doodle_type)); |
| 202 dict->SetString(kKeyAltText, alt_text); |
| 203 dict->SetString(kKeyInteractiveHtml, interactive_html); |
| 204 dict->SetString(kKeySearchUrl, search_url.spec()); |
| 205 dict->SetString(kKeyTargetUrl, target_url.spec()); |
| 206 dict->SetString(kKeyFullpageInteractiveUrl, fullpage_interactive_url.spec()); |
| 207 dict->Set(kKeyLargeImage, large_image.ToDictionary()); |
| 208 if (large_cta_image.has_value()) { |
| 209 dict->Set(kKeyLargeCtaImage, large_cta_image->ToDictionary()); |
| 210 } |
| 211 if (transparent_large_image.has_value()) { |
| 212 dict->Set(kKeyTransparentLargeImage, |
| 213 transparent_large_image->ToDictionary()); |
| 214 } |
| 215 return dict; |
| 216 } |
| 217 |
| 146 bool DoodleConfig::operator==(const DoodleConfig& other) const { | 218 bool DoodleConfig::operator==(const DoodleConfig& other) const { |
| 147 return doodle_type == other.doodle_type && alt_text == other.alt_text && | 219 return doodle_type == other.doodle_type && alt_text == other.alt_text && |
| 148 interactive_html == other.interactive_html && | 220 interactive_html == other.interactive_html && |
| 149 search_url == other.search_url && target_url == other.target_url && | 221 search_url == other.search_url && target_url == other.target_url && |
| 150 fullpage_interactive_url == other.fullpage_interactive_url && | 222 fullpage_interactive_url == other.fullpage_interactive_url && |
| 151 large_image == other.large_image && | 223 large_image == other.large_image && |
| 152 large_cta_image == other.large_cta_image && | 224 large_cta_image == other.large_cta_image && |
| 153 transparent_large_image == other.transparent_large_image; | 225 transparent_large_image == other.transparent_large_image; |
| 154 } | 226 } |
| 155 | 227 |
| 156 bool DoodleConfig::operator!=(const DoodleConfig& other) const { | 228 bool DoodleConfig::operator!=(const DoodleConfig& other) const { |
| 157 return !(*this == other); | 229 return !(*this == other); |
| 158 } | 230 } |
| 159 | 231 |
| 160 } // namespace doodle | 232 } // namespace doodle |
| OLD | NEW |