Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(304)

Side by Side Diff: mojo/public/cpp/bindings/lib/wtf_string_serialization.cc

Issue 1751563002: Mojo C++ bindings: support mapping mojo string to WTF::String. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 "mojo/public/cpp/bindings/lib/wtf_string_serialization.h"
6
7 #include <string.h>
8
9 #include <queue>
10
11 #include "base/logging.h"
12 #include "third_party/WebKit/Source/wtf/text/StringUTF8Adaptor.h"
13 #include "third_party/WebKit/Source/wtf/text/WTFString.h"
14
15 namespace WTF {
16 namespace {
17
18 struct UTF8AdaptorWithPointer {
19 explicit UTF8AdaptorWithPointer(const WTF::String& input)
20 : utf8_adaptor(input), original_input(&input) {}
21
22 ~UTF8AdaptorWithPointer() {}
23
24 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
25 // For sanity check.
26 const WTF::String* original_input;
27 };
28
29 class WTFStringContextImpl : public mojo::internal::WTFStringContext {
30 public:
31 WTFStringContextImpl() {}
32 ~WTFStringContextImpl() override {}
33
34 std::queue<UTF8AdaptorWithPointer>& utf8_adaptors() { return utf8_adaptors_; }
35
36 private:
37 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.
38
39 DISALLOW_COPY_AND_ASSIGN(WTFStringContextImpl);
40 };
41
42 } // namespace
43
44 size_t GetSerializedSize_(const WTF::String& input,
45 mojo::internal::SerializationContext* context) {
46 if (input.isNull())
47 return 0;
48
49 if (!context->wtf_string_context)
50 context->wtf_string_context.reset(new WTFStringContextImpl);
51
52 auto& utf8_adaptors =
53 static_cast<WTFStringContextImpl*>(context->wtf_string_context.get())
54 ->utf8_adaptors();
55
56 utf8_adaptors.emplace(input);
57
58 return mojo::internal::Align(sizeof(mojo::internal::String_Data) +
59 utf8_adaptors.back().utf8_adaptor.length());
60 }
61
62 void Serialize_(const WTF::String& input,
63 mojo::internal::Buffer* buf,
64 mojo::internal::String_Data** output,
65 mojo::internal::SerializationContext* context) {
66 if (input.isNull()) {
67 *output = nullptr;
68 return;
69 }
70
71 auto& utf8_adaptors =
72 static_cast<WTFStringContextImpl*>(context->wtf_string_context.get())
73 ->utf8_adaptors();
74
75 CHECK(!utf8_adaptors.empty());
76 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
77
78 const WTF::StringUTF8Adaptor& adaptor = utf8_adaptors.front().utf8_adaptor;
79
80 mojo::internal::String_Data* result =
81 mojo::internal::String_Data::New(adaptor.length(), buf);
82 if (result)
83 memcpy(result->storage(), adaptor.data(), adaptor.length());
84
85 utf8_adaptors.pop();
86
87 *output = result;
88 }
89
90 bool Deserialize_(mojo::internal::String_Data* input,
91 WTF::String* output,
92 mojo::internal::SerializationContext* context) {
93 if (input) {
94 WTF::String result = WTF::String::fromUTF8(input->storage(), input->size());
95 output->swap(result);
96 } else if (!output->isNull()) {
97 WTF::String result;
98 output->swap(result);
99 }
100 return true;
101 }
102
103 } // namespace WTF
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698