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..96c3fec3bf510630c0626055b5a3e3d7f4317d8d |
--- /dev/null |
+++ b/mojo/public/cpp/bindings/lib/wtf_string_serialization.cc |
@@ -0,0 +1,103 @@ |
+// 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 <queue> |
+ |
+#include "base/logging.h" |
+#include "third_party/WebKit/Source/wtf/text/StringUTF8Adaptor.h" |
+#include "third_party/WebKit/Source/wtf/text/WTFString.h" |
+ |
+namespace WTF { |
+namespace { |
+ |
+struct UTF8AdaptorWithPointer { |
+ explicit UTF8AdaptorWithPointer(const WTF::String& input) |
+ : utf8_adaptor(input), original_input(&input) {} |
+ |
+ ~UTF8AdaptorWithPointer() {} |
+ |
+ WTF::StringUTF8Adaptor utf8_adaptor; |
esprehn
2016/03/04 04:14:48
I'd rather you just stored the StringUTF8Adaptor.
yzshen1
2016/03/04 17:28:49
If we move the serialization code of WTF::String i
|
+ // For sanity check. |
+ const WTF::String* original_input; |
+}; |
+ |
+class WTFStringContextImpl : public mojo::internal::WTFStringContext { |
+ public: |
+ WTFStringContextImpl() {} |
+ ~WTFStringContextImpl() override {} |
+ |
+ std::queue<UTF8AdaptorWithPointer>& utf8_adaptors() { return utf8_adaptors_; } |
+ |
+ private: |
+ std::queue<UTF8AdaptorWithPointer> utf8_adaptors_; |
haraken
2016/03/04 04:04:50
Just help me understand: Why do we need to queue t
esprehn
2016/03/04 04:14:48
Can we use a std::vector instead? std::deque is no
yzshen1
2016/03/04 17:28:49
Serialization consists of two passes: the first us
yzshen1
2016/03/04 17:28:49
I think std::queue is more efficient than std::vec
haraken
2016/03/07 02:04:29
Thanks, makes sense. Maybe worth adding a comment?
yzshen1
2016/03/07 18:26:47
Done.
|
+ |
+ DISALLOW_COPY_AND_ASSIGN(WTFStringContextImpl); |
+}; |
+ |
+} // namespace |
+ |
+size_t GetSerializedSize_(const WTF::String& input, |
+ mojo::internal::SerializationContext* context) { |
+ if (input.isNull()) |
+ return 0; |
+ |
+ if (!context->wtf_string_context) |
+ context->wtf_string_context.reset(new WTFStringContextImpl); |
+ |
+ auto& utf8_adaptors = |
+ static_cast<WTFStringContextImpl*>(context->wtf_string_context.get()) |
+ ->utf8_adaptors(); |
+ |
+ utf8_adaptors.emplace(input); |
+ |
+ return mojo::internal::Align(sizeof(mojo::internal::String_Data) + |
+ utf8_adaptors.back().utf8_adaptor.length()); |
+} |
+ |
+void Serialize_(const WTF::String& input, |
+ mojo::internal::Buffer* buf, |
+ mojo::internal::String_Data** output, |
+ mojo::internal::SerializationContext* context) { |
+ if (input.isNull()) { |
+ *output = nullptr; |
+ return; |
+ } |
+ |
+ auto& utf8_adaptors = |
+ static_cast<WTFStringContextImpl*>(context->wtf_string_context.get()) |
+ ->utf8_adaptors(); |
+ |
+ CHECK(!utf8_adaptors.empty()); |
+ CHECK_EQ(utf8_adaptors.front().original_input, &input); |
esprehn
2016/03/04 04:14:48
Is this really worth the overhead?
yzshen1
2016/03/04 17:28:49
Here we compare the string pointers, not the strin
|
+ |
+ const WTF::StringUTF8Adaptor& adaptor = utf8_adaptors.front().utf8_adaptor; |
+ |
+ mojo::internal::String_Data* result = |
+ mojo::internal::String_Data::New(adaptor.length(), buf); |
+ if (result) |
+ memcpy(result->storage(), adaptor.data(), adaptor.length()); |
+ |
+ utf8_adaptors.pop(); |
+ |
+ *output = result; |
+} |
+ |
+bool Deserialize_(mojo::internal::String_Data* input, |
+ WTF::String* output, |
+ mojo::internal::SerializationContext* context) { |
+ if (input) { |
+ WTF::String result = WTF::String::fromUTF8(input->storage(), input->size()); |
+ output->swap(result); |
+ } else if (!output->isNull()) { |
+ WTF::String result; |
+ output->swap(result); |
+ } |
+ return true; |
+} |
+ |
+} // namespace WTF |