Chromium Code Reviews| Index: mojo/public/cpp/bindings/lib/wtf_string_serialization.cc |
| diff --git a/mojo/public/cpp/bindings/lib/wtf_string_serialization.cc b/mojo/public/cpp/bindings/lib/wtf_string_serialization.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..b866023ed182173cd44b96228597247b38c154f5 |
| --- /dev/null |
| +++ b/mojo/public/cpp/bindings/lib/wtf_string_serialization.cc |
| @@ -0,0 +1,52 @@ |
| +// 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 "mojo/public/cpp/bindings/lib/wtf_string_serialization.h" |
| + |
| +#include <string.h> |
| + |
| +#include "base/logging.h" |
| +#include "third_party/WebKit/Source/wtf/text/WTFString.h" |
| + |
| +namespace WTF { |
| + |
| +size_t GetSerializedSize_(const WTF::String& input) { |
| + if (input.isNull()) |
| + return 0; |
| + |
| + DCHECK(input.is8Bit()); |
|
haraken
2016/03/01 00:40:54
From the perspective of Blink, in common cases we
yzshen1
2016/03/01 00:51:53
If the receiver doesn't care whether the string wa
haraken
2016/03/01 01:56:46
Elliott: Do you have any preference on this?
esprehn
2016/03/01 02:03:32
wtf::String is 16 bit for many cases, the 8 bit st
yzshen1
2016/03/01 17:32:02
Mojo string is 8 bit but not enforced to be utf8.
|
| + return mojo::internal::Align(sizeof(mojo::internal::String_Data) + |
| + input.length()); |
| +} |
| + |
| +void Serialize_(const WTF::String& input, |
| + mojo::internal::Buffer* buf, |
| + mojo::internal::String_Data** output) { |
| + if (input.isNull()) { |
| + *output = nullptr; |
| + return; |
| + } |
| + |
| + DCHECK(input.is8Bit()); |
| + mojo::internal::String_Data* result = |
| + mojo::internal::String_Data::New(input.length(), buf); |
| + if (result) |
| + memcpy(result->storage(), input.characters8(), input.length()); |
| + *output = result; |
| +} |
| + |
| +bool Deserialize_(mojo::internal::String_Data* input, |
| + WTF::String* output, |
| + mojo::internal::SerializationContext* context) { |
| + if (input) { |
| + WTF::String result(input->storage(), input->size()); |
| + output->swap(result); |
| + } else if (!output->isNull()) { |
| + WTF::String result; |
| + output->swap(result); |
| + } |
| + return true; |
| +} |
| + |
| +} // namespace WTF |