Pārlūkot izejas kodu

LibIPC: Support transferring String over IPC

Note that unlike the StringView encoder, we do not handle any "null"
state, as the new String cannot be null.
Timothy Flynn 2 gadi atpakaļ
vecāks
revīzija
a7bb72a3d6

+ 8 - 0
Userland/Libraries/LibIPC/Decoder.cpp

@@ -1,5 +1,6 @@
 /*
 /*
  * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
+ * Copyright (c) 2023, Tim Flynn <trflynn89@serenityos.org>
  *
  *
  * SPDX-License-Identifier: BSD-2-Clause
  * SPDX-License-Identifier: BSD-2-Clause
  */
  */
@@ -23,6 +24,13 @@ ErrorOr<size_t> Decoder::decode_size()
     return static_cast<size_t>(TRY(decode<u32>()));
     return static_cast<size_t>(TRY(decode<u32>()));
 }
 }
 
 
+template<>
+ErrorOr<String> decode(Decoder& decoder)
+{
+    auto length = TRY(decoder.decode_size());
+    return String::from_stream(decoder.stream(), length);
+}
+
 template<>
 template<>
 ErrorOr<DeprecatedString> decode(Decoder& decoder)
 ErrorOr<DeprecatedString> decode(Decoder& decoder)
 {
 {

+ 6 - 0
Userland/Libraries/LibIPC/Decoder.h

@@ -1,5 +1,6 @@
 /*
 /*
  * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
+ * Copyright (c) 2023, Tim Flynn <trflynn89@serenityos.org>
  *
  *
  * SPDX-License-Identifier: BSD-2-Clause
  * SPDX-License-Identifier: BSD-2-Clause
  */
  */
@@ -11,6 +12,7 @@
 #include <AK/Forward.h>
 #include <AK/Forward.h>
 #include <AK/NumericLimits.h>
 #include <AK/NumericLimits.h>
 #include <AK/StdLibExtras.h>
 #include <AK/StdLibExtras.h>
+#include <AK/String.h>
 #include <AK/Try.h>
 #include <AK/Try.h>
 #include <AK/TypeList.h>
 #include <AK/TypeList.h>
 #include <AK/Variant.h>
 #include <AK/Variant.h>
@@ -56,6 +58,7 @@ public:
 
 
     ErrorOr<size_t> decode_size();
     ErrorOr<size_t> decode_size();
 
 
+    Stream& stream() { return m_stream; }
     Core::LocalSocket& socket() { return m_socket; }
     Core::LocalSocket& socket() { return m_socket; }
 
 
 private:
 private:
@@ -78,6 +81,9 @@ ErrorOr<T> decode(Decoder& decoder)
     return static_cast<T>(value);
     return static_cast<T>(value);
 }
 }
 
 
+template<>
+ErrorOr<String> decode(Decoder&);
+
 template<>
 template<>
 ErrorOr<DeprecatedString> decode(Decoder&);
 ErrorOr<DeprecatedString> decode(Decoder&);
 
 

+ 11 - 0
Userland/Libraries/LibIPC/Encoder.cpp

@@ -1,6 +1,7 @@
 /*
 /*
  * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  * Copyright (c) 2021, kleines Filmröllchen <filmroellchen@serenityos.org>
  * Copyright (c) 2021, kleines Filmröllchen <filmroellchen@serenityos.org>
+ * Copyright (c) 2023, Tim Flynn <trflynn89@serenityos.org>
  *
  *
  * SPDX-License-Identifier: BSD-2-Clause
  * SPDX-License-Identifier: BSD-2-Clause
  */
  */
@@ -11,6 +12,7 @@
 #include <AK/JsonObject.h>
 #include <AK/JsonObject.h>
 #include <AK/JsonValue.h>
 #include <AK/JsonValue.h>
 #include <AK/NumericLimits.h>
 #include <AK/NumericLimits.h>
+#include <AK/String.h>
 #include <AK/Time.h>
 #include <AK/Time.h>
 #include <AK/URL.h>
 #include <AK/URL.h>
 #include <LibCore/AnonymousBuffer.h>
 #include <LibCore/AnonymousBuffer.h>
@@ -42,6 +44,15 @@ ErrorOr<void> encode(Encoder& encoder, double const& value)
     return encoder.encode(bit_cast<u64>(value));
     return encoder.encode(bit_cast<u64>(value));
 }
 }
 
 
+template<>
+ErrorOr<void> encode(Encoder& encoder, String const& value)
+{
+    auto bytes = value.bytes();
+    TRY(encoder.encode_size(bytes.size()));
+    TRY(encoder.append(bytes.data(), bytes.size()));
+    return {};
+}
+
 template<>
 template<>
 ErrorOr<void> encode(Encoder& encoder, StringView const& value)
 ErrorOr<void> encode(Encoder& encoder, StringView const& value)
 {
 {

+ 4 - 0
Userland/Libraries/LibIPC/Encoder.h

@@ -1,5 +1,6 @@
 /*
 /*
  * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
  * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
+ * Copyright (c) 2023, Tim Flynn <trflynn89@serenityos.org>
  *
  *
  * SPDX-License-Identifier: BSD-2-Clause
  * SPDX-License-Identifier: BSD-2-Clause
  */
  */
@@ -106,6 +107,9 @@ ErrorOr<void> encode(Encoder&, float const&);
 template<>
 template<>
 ErrorOr<void> encode(Encoder&, double const&);
 ErrorOr<void> encode(Encoder&, double const&);
 
 
+template<>
+ErrorOr<void> encode(Encoder&, String const&);
+
 template<>
 template<>
 ErrorOr<void> encode(Encoder&, StringView const&);
 ErrorOr<void> encode(Encoder&, StringView const&);