Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "content/public/common/manifest_struct_traits.h" | |
| 6 | |
| 7 #include "mojo/common/common_type_converters.h" | |
| 8 #include "mojo/converters/geometry/geometry_type_converters.h" | |
| 9 | |
| 10 namespace mojo { | |
| 11 namespace { | |
| 12 | |
| 13 bool ValidateColor(int64_t color) { | |
| 14 if (color < std::numeric_limits<int32_t>::min() && | |
| 15 color > std::numeric_limits<int32_t>::max() && | |
| 16 color != content::Manifest::kInvalidOrMissingColor) { | |
| 17 return false; | |
| 18 } | |
| 19 return true; | |
| 20 } | |
| 21 | |
| 22 // A wrapper around base::NullableString16 so a custom StringTraits | |
| 23 // specialization can enforce maximum string length. | |
| 24 struct TruncatedNullableString16 { | |
| 25 base::NullableString16 string; | |
| 26 }; | |
| 27 | |
| 28 base::NullableString16 TruncateString( | |
| 29 const base::NullableString16& input) { | |
| 30 if (input.is_null()) | |
| 31 return input; | |
| 32 | |
| 33 return base::NullableString16( | |
| 34 input.string().substr(0, content::Manifest::kMaxIPCStringLength), false); | |
| 35 } | |
| 36 | |
| 37 } // namespace | |
| 38 | |
| 39 template <> | |
| 40 struct StringTraits<TruncatedNullableString16> { | |
| 41 static void SetToNull(TruncatedNullableString16* output) { | |
| 42 output->string = base::NullableString16(); | |
| 43 } | |
| 44 | |
| 45 static bool Read(StringDataView input, TruncatedNullableString16* output) { | |
| 46 if (!StringTraits<base::NullableString16>::Read(input, &output->string)) | |
| 47 return false; | |
| 48 | |
| 49 return (output->string.is_null() || | |
| 50 output->string.string().size() <= | |
| 51 content::Manifest::kMaxIPCStringLength); | |
| 52 } | |
| 53 }; | |
| 54 | |
| 55 base::NullableString16 | |
| 56 StructTraits<blink::mojom::Manifest, content::Manifest>::name( | |
| 57 const content::Manifest& m) { | |
| 58 return TruncateString(m.name); | |
| 59 } | |
| 60 base::NullableString16 | |
| 61 StructTraits<blink::mojom::Manifest, content::Manifest>::short_name( | |
| 62 const content::Manifest& m) { | |
| 63 return TruncateString(m.short_name); | |
| 64 } | |
| 65 base::NullableString16 | |
| 66 StructTraits<blink::mojom::Manifest, content::Manifest>::gcm_sender_id( | |
| 67 const content::Manifest& m) { | |
| 68 return TruncateString(m.gcm_sender_id); | |
| 69 } | |
| 70 | |
| 71 bool StructTraits<blink::mojom::Manifest, content::Manifest>::Read( | |
| 72 blink::mojom::ManifestDataView data, | |
| 73 content::Manifest* out) { | |
| 74 TruncatedNullableString16 string; | |
| 75 if (!data.ReadName(&string)) | |
| 76 return false; | |
| 77 out->name = string.string; | |
| 78 | |
| 79 if (!data.ReadShortName(&string)) | |
| 80 return false; | |
| 81 out->short_name = string.string; | |
| 82 | |
| 83 if (!data.ReadGcmSenderId(&string)) | |
| 84 return false; | |
| 85 out->gcm_sender_id = string.string; | |
| 86 | |
| 87 if (!data.ReadStartUrl(&out->start_url)) | |
| 88 return false; | |
| 89 | |
| 90 if (!data.ReadIcons(&out->icons)) | |
| 91 return false; | |
| 92 | |
| 93 if (!data.ReadRelatedApplications(&out->related_applications)) | |
| 94 return false; | |
| 95 | |
| 96 out->prefer_related_applications = data.prefer_related_applications(); | |
| 97 out->theme_color = data.theme_color(); | |
| 98 if (!ValidateColor(out->theme_color)) | |
| 99 return false; | |
| 100 | |
| 101 out->background_color = data.background_color(); | |
| 102 if (!ValidateColor(out->background_color)) | |
| 103 return false; | |
| 104 | |
| 105 out->display = static_cast<blink::WebDisplayMode>(data.display()); | |
| 106 out->orientation = | |
| 107 static_cast<blink::WebScreenOrientationLockType>(data.orientation()); | |
| 108 | |
| 109 return true; | |
| 110 } | |
| 111 | |
| 112 mojo::Array<mojo::SizePtr> | |
| 113 StructTraits<blink::mojom::ManifestIcon, content::Manifest::Icon>::sizes( | |
| 114 const content::Manifest::Icon& m) { | |
| 115 return mojo::Array<mojo::SizePtr>::From(m.sizes); | |
| 116 } | |
| 117 | |
| 118 base::NullableString16 | |
| 119 StructTraits<blink::mojom::ManifestIcon, content::Manifest::Icon>::type( | |
| 120 const content::Manifest::Icon& m) { | |
| 121 return TruncateString(m.type); | |
| 122 } | |
| 123 | |
| 124 bool StructTraits<blink::mojom::ManifestIcon, content::Manifest::Icon>::Read( | |
| 125 blink::mojom::ManifestIconDataView data, | |
| 126 content::Manifest::Icon* out) { | |
| 127 if (!data.ReadSrc(&out->src)) | |
| 128 return false; | |
| 129 | |
| 130 TruncatedNullableString16 string; | |
| 131 if (!data.ReadType(&string)) | |
| 132 return false; | |
| 133 | |
| 134 out->type = string.string; | |
| 135 | |
| 136 mojo::Array<mojo::SizePtr> sizes; | |
| 137 if (!data.ReadSizes(&sizes)) | |
| 138 return false; | |
| 139 | |
| 140 out->sizes = sizes.To<std::vector<gfx::Size>>(); | |
|
dcheng
2016/06/01 23:38:42
Do we need to manually do this because it's a mojo
Sam McNally
2016/06/02 07:02:40
This was from before mojo::Size was typemapped to
| |
| 141 | |
| 142 return true; | |
| 143 } | |
| 144 | |
| 145 base::NullableString16 StructTraits<blink::mojom::RelatedApplication, | |
| 146 content::Manifest::RelatedApplication>:: | |
| 147 platform(const content::Manifest::RelatedApplication& m) { | |
| 148 return TruncateString(m.platform); | |
|
dcheng
2016/06/01 23:38:42
Similar to the comments about color: why do we che
Sam McNally
2016/06/02 07:02:40
Done.
| |
| 149 } | |
| 150 | |
| 151 base::NullableString16 StructTraits<blink::mojom::RelatedApplication, | |
| 152 content::Manifest::RelatedApplication>:: | |
| 153 id(const content::Manifest::RelatedApplication& m) { | |
| 154 return TruncateString(m.id); | |
| 155 } | |
| 156 | |
| 157 bool StructTraits<blink::mojom::RelatedApplication, | |
| 158 content::Manifest::RelatedApplication>:: | |
| 159 Read(blink::mojom::RelatedApplicationDataView data, | |
| 160 content::Manifest::RelatedApplication* out) { | |
| 161 TruncatedNullableString16 string; | |
| 162 if (!data.ReadPlatform(&string)) | |
| 163 return false; | |
| 164 out->platform = string.string; | |
| 165 | |
| 166 if (!data.ReadUrl(&out->url)) | |
| 167 return false; | |
| 168 | |
| 169 if (!data.ReadId(&string)) | |
| 170 return false; | |
| 171 out->id = string.string; | |
| 172 | |
| 173 return true; | |
| 174 } | |
| 175 | |
| 176 } // namespace mojo | |
| OLD | NEW |