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

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

Issue 1810253002: Mojo C++ bindings: fix sanity check of wtf string serialization. (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
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "mojo/public/cpp/bindings/lib/wtf_string_serialization.h" 5 #include "mojo/public/cpp/bindings/lib/wtf_string_serialization.h"
6 6
7 #include <string.h> 7 #include <string.h>
8 8
9 #include <queue> 9 #include <queue>
10 10
11 #include "base/logging.h" 11 #include "base/logging.h"
12 #include "third_party/WebKit/Source/wtf/text/StringUTF8Adaptor.h" 12 #include "third_party/WebKit/Source/wtf/text/StringUTF8Adaptor.h"
13 #include "third_party/WebKit/Source/wtf/text/WTFString.h" 13 #include "third_party/WebKit/Source/wtf/text/WTFString.h"
14 14
15 namespace WTF { 15 namespace WTF {
16 namespace { 16 namespace {
17 17
18 struct UTF8AdaptorWithPointer { 18 struct UTF8AdaptorInfo {
19 explicit UTF8AdaptorWithPointer(const WTF::String& input) 19 explicit UTF8AdaptorInfo(const WTF::String& input) : utf8_adaptor(input) {
20 : utf8_adaptor(input) {
21 #if DCHECK_IS_ON() 20 #if DCHECK_IS_ON()
22 original_input = reinterpret_cast<uintptr_t>(&input); 21 original_size_in_bytes = static_cast<size_t>(input.sizeInBytes());
23 #endif 22 #endif
24 } 23 }
25 24
26 ~UTF8AdaptorWithPointer() {} 25 ~UTF8AdaptorInfo() {}
27 26
28 WTF::StringUTF8Adaptor utf8_adaptor; 27 WTF::StringUTF8Adaptor utf8_adaptor;
29 28
30 #if DCHECK_IS_ON() 29 #if DCHECK_IS_ON()
31 // For sanity check only. Never dereferenced. 30 // For sanity check only.
32 uintptr_t original_input; 31 size_t original_size_in_bytes;
33 #endif 32 #endif
34 }; 33 };
35 34
36 class WTFStringContextImpl : public mojo::internal::WTFStringContext { 35 class WTFStringContextImpl : public mojo::internal::WTFStringContext {
37 public: 36 public:
38 WTFStringContextImpl() {} 37 WTFStringContextImpl() {}
39 ~WTFStringContextImpl() override {} 38 ~WTFStringContextImpl() override {}
40 39
41 std::queue<UTF8AdaptorWithPointer>& utf8_adaptors() { return utf8_adaptors_; } 40 std::queue<UTF8AdaptorInfo>& utf8_adaptors() { return utf8_adaptors_; }
42 41
43 private: 42 private:
44 // When serializing an object, we call GetSerializedSize_() recursively on 43 // When serializing an object, we call GetSerializedSize_() recursively on
45 // all its elements/members to compute the total size, and then call 44 // all its elements/members to compute the total size, and then call
46 // Serialize*_() recursively in the same order to do the actual 45 // Serialize*_() recursively in the same order to do the actual
47 // serialization. If some WTF::Strings need to be converted to UTF8, we don't 46 // serialization. If some WTF::Strings need to be converted to UTF8, we don't
48 // want to do that twice. Therefore, we store a WTF::StringUTF8Adaptor for 47 // want to do that twice. Therefore, we store a WTF::StringUTF8Adaptor for
49 // each in the first pass, and reuse it in the second pass. 48 // each in the first pass, and reuse it in the second pass.
50 std::queue<UTF8AdaptorWithPointer> utf8_adaptors_; 49 std::queue<UTF8AdaptorInfo> utf8_adaptors_;
51 50
52 DISALLOW_COPY_AND_ASSIGN(WTFStringContextImpl); 51 DISALLOW_COPY_AND_ASSIGN(WTFStringContextImpl);
53 }; 52 };
54 53
55 } // namespace 54 } // namespace
56 55
57 size_t GetSerializedSize_(const WTF::String& input, 56 size_t GetSerializedSize_(const WTF::String& input,
58 mojo::internal::SerializationContext* context) { 57 mojo::internal::SerializationContext* context) {
59 if (input.isNull()) 58 if (input.isNull())
60 return 0; 59 return 0;
(...skipping 19 matching lines...) Expand all
80 *output = nullptr; 79 *output = nullptr;
81 return; 80 return;
82 } 81 }
83 82
84 auto& utf8_adaptors = 83 auto& utf8_adaptors =
85 static_cast<WTFStringContextImpl*>(context->wtf_string_context.get()) 84 static_cast<WTFStringContextImpl*>(context->wtf_string_context.get())
86 ->utf8_adaptors(); 85 ->utf8_adaptors();
87 86
88 DCHECK(!utf8_adaptors.empty()); 87 DCHECK(!utf8_adaptors.empty());
89 #if DCHECK_IS_ON() 88 #if DCHECK_IS_ON()
90 DCHECK_EQ(utf8_adaptors.front().original_input, 89 DCHECK_EQ(utf8_adaptors.front().original_size_in_bytes,
91 reinterpret_cast<uintptr_t>(&input)); 90 static_cast<size_t>(input.sizeInBytes()));
92 #endif 91 #endif
93 92
94 const WTF::StringUTF8Adaptor& adaptor = utf8_adaptors.front().utf8_adaptor; 93 const WTF::StringUTF8Adaptor& adaptor = utf8_adaptors.front().utf8_adaptor;
95 94
96 mojo::internal::String_Data* result = 95 mojo::internal::String_Data* result =
97 mojo::internal::String_Data::New(adaptor.length(), buf); 96 mojo::internal::String_Data::New(adaptor.length(), buf);
98 if (result) 97 if (result)
99 memcpy(result->storage(), adaptor.data(), adaptor.length()); 98 memcpy(result->storage(), adaptor.data(), adaptor.length());
100 99
101 utf8_adaptors.pop(); 100 utf8_adaptors.pop();
102 101
103 *output = result; 102 *output = result;
104 } 103 }
105 104
106 bool Deserialize_(mojo::internal::String_Data* input, 105 bool Deserialize_(mojo::internal::String_Data* input,
107 WTF::String* output, 106 WTF::String* output,
108 mojo::internal::SerializationContext* context) { 107 mojo::internal::SerializationContext* context) {
109 if (input) { 108 if (input) {
110 WTF::String result = WTF::String::fromUTF8(input->storage(), input->size()); 109 WTF::String result = WTF::String::fromUTF8(input->storage(), input->size());
111 output->swap(result); 110 output->swap(result);
112 } else if (!output->isNull()) { 111 } else if (!output->isNull()) {
113 WTF::String result; 112 WTF::String result;
114 output->swap(result); 113 output->swap(result);
115 } 114 }
116 return true; 115 return true;
117 } 116 }
118 117
119 } // namespace WTF 118 } // namespace WTF
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698