Chromium Code Reviews| OLD | NEW |
|---|---|
| (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) { | |
| 21 #if DCHECK_IS_ON() | |
| 22 original_input = reinterpret_cast<uintptr_t>(&input); | |
| 23 #endif | |
| 24 } | |
| 25 | |
| 26 ~UTF8AdaptorWithPointer() {} | |
| 27 | |
| 28 WTF::StringUTF8Adaptor utf8_adaptor; | |
| 29 | |
| 30 #if DCHECK_IS_ON() | |
| 31 // For sanity check only. Never dereferenced. | |
| 32 uintptr_t original_input; | |
| 33 #endif | |
| 34 }; | |
| 35 | |
| 36 class WTFStringContextImpl : public mojo::internal::WTFStringContext { | |
| 37 public: | |
| 38 WTFStringContextImpl() {} | |
| 39 ~WTFStringContextImpl() override {} | |
| 40 | |
| 41 std::queue<UTF8AdaptorWithPointer>& utf8_adaptors() { return utf8_adaptors_; } | |
| 42 | |
| 43 private: | |
| 44 // When serializing an object, we call GetSerializedSize_() recursively on | |
| 45 // all its elements/members to compute the total size, and then call | |
| 46 // 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 | |
| 48 // 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. | |
| 50 std::queue<UTF8AdaptorWithPointer> utf8_adaptors_; | |
| 51 | |
| 52 DISALLOW_COPY_AND_ASSIGN(WTFStringContextImpl); | |
| 53 }; | |
| 54 | |
| 55 } // namespace | |
| 56 | |
| 57 size_t GetSerializedSize_(const WTF::String& input, | |
| 58 mojo::internal::SerializationContext* context) { | |
| 59 if (input.isNull()) | |
| 60 return 0; | |
| 61 | |
| 62 if (!context->wtf_string_context) | |
| 63 context->wtf_string_context.reset(new WTFStringContextImpl); | |
| 64 | |
| 65 auto& utf8_adaptors = | |
| 66 static_cast<WTFStringContextImpl*>(context->wtf_string_context.get()) | |
| 67 ->utf8_adaptors(); | |
| 68 | |
| 69 utf8_adaptors.emplace(input); | |
| 70 | |
| 71 return mojo::internal::Align(sizeof(mojo::internal::String_Data) + | |
| 72 utf8_adaptors.back().utf8_adaptor.length()); | |
| 73 } | |
| 74 | |
| 75 void Serialize_(const WTF::String& input, | |
| 76 mojo::internal::Buffer* buf, | |
| 77 mojo::internal::String_Data** output, | |
| 78 mojo::internal::SerializationContext* context) { | |
| 79 if (input.isNull()) { | |
| 80 *output = nullptr; | |
| 81 return; | |
| 82 } | |
| 83 | |
| 84 auto& utf8_adaptors = | |
| 85 static_cast<WTFStringContextImpl*>(context->wtf_string_context.get()) | |
| 86 ->utf8_adaptors(); | |
| 87 | |
| 88 DCHECK(!utf8_adaptors.empty()); | |
| 89 #if DCHECK_IS_ON() | |
|
haraken
2016/03/13 16:21:07
Remove this macro?
yzshen1
2016/03/14 06:36:27
I had to add this #if because otherwise some bots
| |
| 90 DCHECK_EQ(utf8_adaptors.front().original_input, | |
| 91 reinterpret_cast<uintptr_t>(&input)); | |
| 92 #endif | |
| 93 | |
| 94 const WTF::StringUTF8Adaptor& adaptor = utf8_adaptors.front().utf8_adaptor; | |
| 95 | |
| 96 mojo::internal::String_Data* result = | |
| 97 mojo::internal::String_Data::New(adaptor.length(), buf); | |
| 98 if (result) | |
| 99 memcpy(result->storage(), adaptor.data(), adaptor.length()); | |
| 100 | |
| 101 utf8_adaptors.pop(); | |
| 102 | |
| 103 *output = result; | |
| 104 } | |
| 105 | |
| 106 bool Deserialize_(mojo::internal::String_Data* input, | |
| 107 WTF::String* output, | |
| 108 mojo::internal::SerializationContext* context) { | |
| 109 if (input) { | |
| 110 WTF::String result = WTF::String::fromUTF8(input->storage(), input->size()); | |
| 111 output->swap(result); | |
| 112 } else if (!output->isNull()) { | |
| 113 WTF::String result; | |
| 114 output->swap(result); | |
| 115 } | |
| 116 return true; | |
| 117 } | |
| 118 | |
| 119 } // namespace WTF | |
| OLD | NEW |