OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "modules/mediasession/MediaMetadataSanitizer.h" | 5 #include "modules/mediasession/MediaMetadataSanitizer.h" |
6 | 6 |
7 #include "core/dom/ExecutionContext.h" | 7 #include "core/dom/ExecutionContext.h" |
8 #include "core/inspector/ConsoleMessage.h" | 8 #include "core/inspector/ConsoleMessage.h" |
9 #include "modules/mediasession/MediaImage.h" | 9 #include "modules/mediasession/MediaImage.h" |
10 #include "modules/mediasession/MediaMetadata.h" | 10 #include "modules/mediasession/MediaMetadata.h" |
(...skipping 16 matching lines...) Expand all Loading... |
27 // (https://tools.ietf.org/html/rfc4288). | 27 // (https://tools.ietf.org/html/rfc4288). |
28 const size_t kMaxImageTypeLength = 2 * 127 + 1; | 28 const size_t kMaxImageTypeLength = 2 * 127 + 1; |
29 | 29 |
30 // Maximum number of MediaImages inside the MediaMetadata. | 30 // Maximum number of MediaImages inside the MediaMetadata. |
31 const size_t kMaxNumberOfMediaImages = 10; | 31 const size_t kMaxNumberOfMediaImages = 10; |
32 | 32 |
33 // Maximum of sizes in a MediaImage. | 33 // Maximum of sizes in a MediaImage. |
34 const size_t kMaxNumberOfImageSizes = 10; | 34 const size_t kMaxNumberOfImageSizes = 10; |
35 | 35 |
36 bool checkMediaImageSrcSanity(const KURL& src, ExecutionContext* context) { | 36 bool checkMediaImageSrcSanity(const KURL& src, ExecutionContext* context) { |
37 // Console warning for invalid src is printed upon MediaImage creation. | 37 // Invalid URLs will be rejected early on. |
38 if (!src.isValid()) | 38 DCHECK(src.isValid()); |
39 return false; | |
40 | 39 |
41 if (!src.protocolIs(url::kHttpScheme) && !src.protocolIs(url::kHttpsScheme) && | 40 if (!src.protocolIs(url::kHttpScheme) && !src.protocolIs(url::kHttpsScheme) && |
42 !src.protocolIs(url::kDataScheme) && !src.protocolIs(url::kBlobScheme)) { | 41 !src.protocolIs(url::kDataScheme) && !src.protocolIs(url::kBlobScheme)) { |
43 context->addConsoleMessage(ConsoleMessage::create( | 42 context->addConsoleMessage(ConsoleMessage::create( |
44 JSMessageSource, WarningMessageLevel, | 43 JSMessageSource, WarningMessageLevel, |
45 "MediaImage src can only be of http/https/data/blob scheme: " + | 44 "MediaImage src can only be of http/https/data/blob scheme: " + |
46 src.getString())); | 45 src.getString())); |
47 return false; | 46 return false; |
48 } | 47 } |
| 48 |
49 DCHECK(src.getString().is8Bit()); | 49 DCHECK(src.getString().is8Bit()); |
50 if (src.getString().length() > url::kMaxURLChars) { | 50 if (src.getString().length() > url::kMaxURLChars) { |
51 context->addConsoleMessage(ConsoleMessage::create( | 51 context->addConsoleMessage(ConsoleMessage::create( |
52 JSMessageSource, WarningMessageLevel, | 52 JSMessageSource, WarningMessageLevel, |
53 "MediaImage src exceeds maximum URL length: " + src.getString())); | 53 "MediaImage src exceeds maximum URL length: " + src.getString())); |
54 return false; | 54 return false; |
55 } | 55 } |
56 return true; | 56 return true; |
57 } | 57 } |
58 | 58 |
59 // Sanitize MediaImage and do mojo serialization. Returns null when | 59 // Sanitize MediaImage and do mojo serialization. Returns null when |
60 // |image.src()| is bad. | 60 // |image.src()| is bad. |
61 blink::mojom::blink::MediaImagePtr sanitizeMediaImageAndConvertToMojo( | 61 blink::mojom::blink::MediaImagePtr sanitizeMediaImageAndConvertToMojo( |
62 const MediaImage* image, | 62 const MediaImage& image, |
63 ExecutionContext* context) { | 63 ExecutionContext* context) { |
64 DCHECK(image); | |
65 | |
66 blink::mojom::blink::MediaImagePtr mojoImage; | 64 blink::mojom::blink::MediaImagePtr mojoImage; |
67 | 65 |
68 KURL url = KURL(ParsedURLString, image->src()); | 66 KURL url = KURL(ParsedURLString, image.src()); |
69 if (!checkMediaImageSrcSanity(url, context)) | 67 if (!checkMediaImageSrcSanity(url, context)) |
70 return mojoImage; | 68 return mojoImage; |
71 | 69 |
72 mojoImage = blink::mojom::blink::MediaImage::New(); | 70 mojoImage = blink::mojom::blink::MediaImage::New(); |
73 mojoImage->src = url; | 71 mojoImage->src = url; |
74 mojoImage->type = image->type().left(kMaxImageTypeLength); | 72 mojoImage->type = image.type().left(kMaxImageTypeLength); |
75 for (const auto& webSize : | 73 for (const auto& webSize : |
76 WebIconSizesParser::parseIconSizes(image->sizes())) { | 74 WebIconSizesParser::parseIconSizes(image.sizes())) { |
77 mojoImage->sizes.push_back(webSize); | 75 mojoImage->sizes.push_back(webSize); |
78 if (mojoImage->sizes.size() == kMaxNumberOfImageSizes) { | 76 if (mojoImage->sizes.size() == kMaxNumberOfImageSizes) { |
79 context->addConsoleMessage(ConsoleMessage::create( | 77 context->addConsoleMessage(ConsoleMessage::create( |
80 JSMessageSource, WarningMessageLevel, | 78 JSMessageSource, WarningMessageLevel, |
81 "The number of MediaImage sizes exceeds the upper limit. " | 79 "The number of MediaImage sizes exceeds the upper limit. " |
82 "All remaining MediaImage will be ignored")); | 80 "All remaining MediaImage will be ignored")); |
83 break; | 81 break; |
84 } | 82 } |
85 } | 83 } |
86 return mojoImage; | 84 return mojoImage; |
87 } | 85 } |
88 | 86 |
89 } // anonymous namespace | 87 } // anonymous namespace |
90 | 88 |
91 blink::mojom::blink::MediaMetadataPtr | 89 blink::mojom::blink::MediaMetadataPtr |
92 MediaMetadataSanitizer::sanitizeAndConvertToMojo(const MediaMetadata* metadata, | 90 MediaMetadataSanitizer::sanitizeAndConvertToMojo(const MediaMetadata* metadata, |
93 ExecutionContext* context) { | 91 ExecutionContext* context) { |
94 blink::mojom::blink::MediaMetadataPtr mojoMetadata; | 92 blink::mojom::blink::MediaMetadataPtr mojoMetadata; |
95 if (!metadata) | 93 if (!metadata) |
96 return mojoMetadata; | 94 return mojoMetadata; |
97 | 95 |
98 mojoMetadata = blink::mojom::blink::MediaMetadata::New(); | 96 mojoMetadata = blink::mojom::blink::MediaMetadata::New(); |
99 | 97 |
100 mojoMetadata->title = metadata->title().left(kMaxStringLength); | 98 mojoMetadata->title = metadata->title().left(kMaxStringLength); |
101 mojoMetadata->artist = metadata->artist().left(kMaxStringLength); | 99 mojoMetadata->artist = metadata->artist().left(kMaxStringLength); |
102 mojoMetadata->album = metadata->album().left(kMaxStringLength); | 100 mojoMetadata->album = metadata->album().left(kMaxStringLength); |
103 | 101 |
104 for (const auto image : metadata->artwork()) { | 102 for (const MediaImage& image : metadata->artwork()) { |
105 blink::mojom::blink::MediaImagePtr mojoImage = | 103 blink::mojom::blink::MediaImagePtr mojoImage = |
106 sanitizeMediaImageAndConvertToMojo(image.get(), context); | 104 sanitizeMediaImageAndConvertToMojo(image, context); |
107 if (!mojoImage.is_null()) | 105 if (!mojoImage.is_null()) |
108 mojoMetadata->artwork.push_back(std::move(mojoImage)); | 106 mojoMetadata->artwork.push_back(std::move(mojoImage)); |
109 if (mojoMetadata->artwork.size() == kMaxNumberOfMediaImages) { | 107 if (mojoMetadata->artwork.size() == kMaxNumberOfMediaImages) { |
110 context->addConsoleMessage(ConsoleMessage::create( | 108 context->addConsoleMessage(ConsoleMessage::create( |
111 JSMessageSource, WarningMessageLevel, | 109 JSMessageSource, WarningMessageLevel, |
112 "The number of MediaImage sizes exceeds the upper limit. " | 110 "The number of MediaImage sizes exceeds the upper limit. " |
113 "All remaining MediaImage will be ignored")); | 111 "All remaining MediaImage will be ignored")); |
114 break; | 112 break; |
115 } | 113 } |
116 } | 114 } |
117 return mojoMetadata; | 115 return mojoMetadata; |
118 } | 116 } |
119 | 117 |
120 } // namespace blink | 118 } // namespace blink |
OLD | NEW |