Chromium Code Reviews| Index: content/public/common/manifest_struct_traits.cc |
| diff --git a/content/public/common/manifest_struct_traits.cc b/content/public/common/manifest_struct_traits.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..0aad3f0d764bd675d530d0a3973fca2fa572e8cf |
| --- /dev/null |
| +++ b/content/public/common/manifest_struct_traits.cc |
| @@ -0,0 +1,176 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "content/public/common/manifest_struct_traits.h" |
| + |
| +#include "mojo/common/common_type_converters.h" |
| +#include "mojo/converters/geometry/geometry_type_converters.h" |
| + |
| +namespace mojo { |
| +namespace { |
| + |
| +bool ValidateColor(int64_t color) { |
| + if (color < std::numeric_limits<int32_t>::min() && |
| + color > std::numeric_limits<int32_t>::max() && |
| + color != content::Manifest::kInvalidOrMissingColor) { |
| + return false; |
| + } |
| + return true; |
| +} |
| + |
| +// A wrapper around base::NullableString16 so a custom StringTraits |
| +// specialization can enforce maximum string length. |
| +struct TruncatedNullableString16 { |
| + base::NullableString16 string; |
| +}; |
| + |
| +base::NullableString16 TruncateString( |
| + const base::NullableString16& input) { |
| + if (input.is_null()) |
| + return input; |
| + |
| + return base::NullableString16( |
| + input.string().substr(0, content::Manifest::kMaxIPCStringLength), false); |
| +} |
| + |
| +} // namespace |
| + |
| +template <> |
| +struct StringTraits<TruncatedNullableString16> { |
| + static void SetToNull(TruncatedNullableString16* output) { |
| + output->string = base::NullableString16(); |
| + } |
| + |
| + static bool Read(StringDataView input, TruncatedNullableString16* output) { |
| + if (!StringTraits<base::NullableString16>::Read(input, &output->string)) |
| + return false; |
| + |
| + return (output->string.is_null() || |
| + output->string.string().size() <= |
| + content::Manifest::kMaxIPCStringLength); |
| + } |
| +}; |
| + |
| +base::NullableString16 |
| +StructTraits<blink::mojom::Manifest, content::Manifest>::name( |
| + const content::Manifest& m) { |
| + return TruncateString(m.name); |
| +} |
| +base::NullableString16 |
| +StructTraits<blink::mojom::Manifest, content::Manifest>::short_name( |
| + const content::Manifest& m) { |
| + return TruncateString(m.short_name); |
| +} |
| +base::NullableString16 |
| +StructTraits<blink::mojom::Manifest, content::Manifest>::gcm_sender_id( |
| + const content::Manifest& m) { |
| + return TruncateString(m.gcm_sender_id); |
| +} |
| + |
| +bool StructTraits<blink::mojom::Manifest, content::Manifest>::Read( |
| + blink::mojom::ManifestDataView data, |
| + content::Manifest* out) { |
| + TruncatedNullableString16 string; |
| + if (!data.ReadName(&string)) |
| + return false; |
| + out->name = string.string; |
| + |
| + if (!data.ReadShortName(&string)) |
| + return false; |
| + out->short_name = string.string; |
| + |
| + if (!data.ReadGcmSenderId(&string)) |
| + return false; |
| + out->gcm_sender_id = string.string; |
| + |
| + if (!data.ReadStartUrl(&out->start_url)) |
| + return false; |
| + |
| + if (!data.ReadIcons(&out->icons)) |
| + return false; |
| + |
| + if (!data.ReadRelatedApplications(&out->related_applications)) |
| + return false; |
| + |
| + out->prefer_related_applications = data.prefer_related_applications(); |
| + out->theme_color = data.theme_color(); |
| + if (!ValidateColor(out->theme_color)) |
| + return false; |
| + |
| + out->background_color = data.background_color(); |
| + if (!ValidateColor(out->background_color)) |
| + return false; |
| + |
| + out->display = static_cast<blink::WebDisplayMode>(data.display()); |
| + out->orientation = |
| + static_cast<blink::WebScreenOrientationLockType>(data.orientation()); |
| + |
| + return true; |
| +} |
| + |
| +mojo::Array<mojo::SizePtr> |
| +StructTraits<blink::mojom::ManifestIcon, content::Manifest::Icon>::sizes( |
| + const content::Manifest::Icon& m) { |
| + return mojo::Array<mojo::SizePtr>::From(m.sizes); |
| +} |
| + |
| +base::NullableString16 |
| +StructTraits<blink::mojom::ManifestIcon, content::Manifest::Icon>::type( |
| + const content::Manifest::Icon& m) { |
| + return TruncateString(m.type); |
| +} |
| + |
| +bool StructTraits<blink::mojom::ManifestIcon, content::Manifest::Icon>::Read( |
| + blink::mojom::ManifestIconDataView data, |
| + content::Manifest::Icon* out) { |
| + if (!data.ReadSrc(&out->src)) |
| + return false; |
| + |
| + TruncatedNullableString16 string; |
| + if (!data.ReadType(&string)) |
| + return false; |
| + |
| + out->type = string.string; |
| + |
| + mojo::Array<mojo::SizePtr> sizes; |
| + if (!data.ReadSizes(&sizes)) |
| + return false; |
| + |
| + 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
|
| + |
| + return true; |
| +} |
| + |
| +base::NullableString16 StructTraits<blink::mojom::RelatedApplication, |
| + content::Manifest::RelatedApplication>:: |
| + platform(const content::Manifest::RelatedApplication& m) { |
| + 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.
|
| +} |
| + |
| +base::NullableString16 StructTraits<blink::mojom::RelatedApplication, |
| + content::Manifest::RelatedApplication>:: |
| + id(const content::Manifest::RelatedApplication& m) { |
| + return TruncateString(m.id); |
| +} |
| + |
| +bool StructTraits<blink::mojom::RelatedApplication, |
| + content::Manifest::RelatedApplication>:: |
| + Read(blink::mojom::RelatedApplicationDataView data, |
| + content::Manifest::RelatedApplication* out) { |
| + TruncatedNullableString16 string; |
| + if (!data.ReadPlatform(&string)) |
| + return false; |
| + out->platform = string.string; |
| + |
| + if (!data.ReadUrl(&out->url)) |
| + return false; |
| + |
| + if (!data.ReadId(&string)) |
| + return false; |
| + out->id = string.string; |
| + |
| + return true; |
| +} |
| + |
| +} // namespace mojo |