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 "modules/mediasession/MediaArtwork.h" | 7 #include "modules/mediasession/MediaImage.h" |
8 #include "modules/mediasession/MediaMetadata.h" | 8 #include "modules/mediasession/MediaMetadata.h" |
9 #include "public/platform/WebIconSizesParser.h" | 9 #include "public/platform/WebIconSizesParser.h" |
10 #include "public/platform/WebSize.h" | 10 #include "public/platform/WebSize.h" |
11 #include "url/url_constants.h" | 11 #include "url/url_constants.h" |
12 | 12 |
13 namespace blink { | 13 namespace blink { |
14 | 14 |
15 namespace { | 15 namespace { |
16 | 16 |
17 // Constants used by the sanitizer, must be consistent with | 17 // Constants used by the sanitizer, must be consistent with |
18 // content::MediaMetdataSanitizer. | 18 // content::MediaMetdataSanitizer. |
19 | 19 |
20 // Maximum length of all strings inside MediaMetadata when it is sent over mojo. | 20 // Maximum length of all strings inside MediaMetadata when it is sent over mojo. |
21 const size_t kMaxStringLength = 4 * 1024; | 21 const size_t kMaxStringLength = 4 * 1024; |
22 | 22 |
23 // Maximum type length of MediaArtwork, which conforms to RFC 4288 | 23 // Maximum type length of MediaImage, which conforms to RFC 4288 |
24 // (https://tools.ietf.org/html/rfc4288). | 24 // (https://tools.ietf.org/html/rfc4288). |
25 const size_t kMaxArtworkTypeLength = 2 * 127 + 1; | 25 const size_t kMaxImageTypeLength = 2 * 127 + 1; |
26 | 26 |
27 // Maximum number of artwork images inside the MediaMetadata. | 27 // Maximum number of MediaImages inside the MediaMetadata. |
28 const size_t kMaxNumberOfArtworkImages = 10; | 28 const size_t kMaxNumberOfMediaImages = 10; |
29 | 29 |
30 // Maximum of sizes in an artwork image. | 30 // Maximum of sizes in a MediaImage. |
31 const size_t kMaxNumberOfArtworkSizes = 10; | 31 const size_t kMaxNumberOfImageSizes = 10; |
32 | 32 |
33 bool checkArtworkSrcSanity(const KURL& src) { | 33 bool checkMediaImageSrcSanity(const KURL& src) { |
34 if (!src.isValid()) | 34 if (!src.isValid()) |
35 return false; | 35 return false; |
36 if (!src.protocolIs(url::kHttpScheme) && !src.protocolIs(url::kHttpsScheme) && | 36 if (!src.protocolIs(url::kHttpScheme) && !src.protocolIs(url::kHttpsScheme) && |
37 !src.protocolIs(url::kDataScheme)) { | 37 !src.protocolIs(url::kDataScheme)) { |
38 return false; | 38 return false; |
39 } | 39 } |
40 DCHECK(src.getString().is8Bit()); | 40 DCHECK(src.getString().is8Bit()); |
41 if (src.getString().length() > url::kMaxURLChars) | 41 if (src.getString().length() > url::kMaxURLChars) |
42 return false; | 42 return false; |
43 return true; | 43 return true; |
44 } | 44 } |
45 | 45 |
46 blink::mojom::blink::MediaImagePtr sanitizeArtworkAndConvertToMojo( | 46 // Sanitize MediaImage and do mojo serialization. Returns null when |
47 const MediaArtwork* artwork) { | 47 // |image.src()| is bad. |
48 DCHECK(artwork); | 48 blink::mojom::blink::MediaImagePtr sanitizeMediaImageAndConvertToMojo( |
| 49 const MediaImage* image) { |
| 50 DCHECK(image); |
49 | 51 |
50 blink::mojom::blink::MediaImagePtr mojoImage; | 52 blink::mojom::blink::MediaImagePtr mojoImage; |
51 | 53 |
52 KURL url = KURL(ParsedURLString, artwork->src()); | 54 KURL url = KURL(ParsedURLString, image->src()); |
53 if (!checkArtworkSrcSanity(url)) | 55 if (!checkMediaImageSrcSanity(url)) |
54 return mojoImage; | 56 return mojoImage; |
55 | 57 |
56 mojoImage = blink::mojom::blink::MediaImage::New(); | 58 mojoImage = blink::mojom::blink::MediaImage::New(); |
57 mojoImage->src = url; | 59 mojoImage->src = url; |
58 mojoImage->type = artwork->type().left(kMaxArtworkTypeLength); | 60 mojoImage->type = image->type().left(kMaxImageTypeLength); |
59 for (const auto& webSize : | 61 for (const auto& webSize : |
60 WebIconSizesParser::parseIconSizes(artwork->sizes())) { | 62 WebIconSizesParser::parseIconSizes(image->sizes())) { |
61 mojoImage->sizes.append(webSize); | 63 mojoImage->sizes.append(webSize); |
62 if (mojoImage->sizes.size() == kMaxNumberOfArtworkSizes) | 64 if (mojoImage->sizes.size() == kMaxNumberOfImageSizes) |
63 break; | 65 break; |
64 } | 66 } |
65 return mojoImage; | 67 return mojoImage; |
66 } | 68 } |
67 | 69 |
68 } // anonymous namespace | 70 } // anonymous namespace |
69 | 71 |
70 blink::mojom::blink::MediaMetadataPtr | 72 blink::mojom::blink::MediaMetadataPtr |
71 MediaMetadataSanitizer::sanitizeAndConvertToMojo( | 73 MediaMetadataSanitizer::sanitizeAndConvertToMojo( |
72 const MediaMetadata* metadata) { | 74 const MediaMetadata* metadata) { |
73 blink::mojom::blink::MediaMetadataPtr mojoMetadata; | 75 blink::mojom::blink::MediaMetadataPtr mojoMetadata; |
74 if (!metadata) | 76 if (!metadata) |
75 return mojoMetadata; | 77 return mojoMetadata; |
76 | 78 |
77 mojoMetadata = blink::mojom::blink::MediaMetadata::New(); | 79 mojoMetadata = blink::mojom::blink::MediaMetadata::New(); |
78 | 80 |
79 mojoMetadata->title = metadata->title().left(kMaxStringLength); | 81 mojoMetadata->title = metadata->title().left(kMaxStringLength); |
80 mojoMetadata->artist = metadata->artist().left(kMaxStringLength); | 82 mojoMetadata->artist = metadata->artist().left(kMaxStringLength); |
81 mojoMetadata->album = metadata->album().left(kMaxStringLength); | 83 mojoMetadata->album = metadata->album().left(kMaxStringLength); |
82 | 84 |
83 for (const auto artwork : metadata->artwork()) { | 85 for (const auto image : metadata->artwork()) { |
84 blink::mojom::blink::MediaImagePtr mojoImage = | 86 blink::mojom::blink::MediaImagePtr mojoImage = |
85 sanitizeArtworkAndConvertToMojo(artwork.get()); | 87 sanitizeMediaImageAndConvertToMojo(image.get()); |
86 if (!mojoImage.is_null()) | 88 if (!mojoImage.is_null()) |
87 mojoMetadata->artwork.append(std::move(mojoImage)); | 89 mojoMetadata->artwork.append(std::move(mojoImage)); |
88 if (mojoMetadata->artwork.size() == kMaxNumberOfArtworkImages) | 90 if (mojoMetadata->artwork.size() == kMaxNumberOfMediaImages) |
89 break; | 91 break; |
90 } | 92 } |
91 return mojoMetadata; | 93 return mojoMetadata; |
92 } | 94 } |
93 | 95 |
94 } // namespace blink | 96 } // namespace blink |
OLD | NEW |