Source/WebKit/ChangeLog

 12021-11-15 Chris Dumez <cdumez@apple.com>
 2
 3 Decoder::unwrapForTesting() is unnecessarily inefficient
 4 https://bugs.webkit.org/show_bug.cgi?id=233145
 5
 6 Reviewed by NOBODY (OOPS!).
 7
 8 Decoder::unwrapForTesting() is unnecessarily inefficient. It can take the whole
 9 m_attachments data members from the decoder instead of calling removeAttachment()
 10 repeatedly on the Decoder.
 11
 12 Also rename removeAttachment() to takeAttachment() since it returns the Attachment.
 13 Update it to return a std::optional<Attachment> instead of using an out-parameter.
 14
 15 * Platform/IPC/Attachment.cpp:
 16 (IPC::Attachment::decode):
 17 * Platform/IPC/Attachment.h:
 18 * Platform/IPC/Decoder.cpp:
 19 (IPC::Decoder::unwrapForTesting):
 20 (IPC::Decoder::takeAttachment):
 21 (IPC::Decoder::removeAttachment): Deleted.
 22 * Platform/IPC/Decoder.h:
 23 * Platform/IPC/win/AttachmentWin.cpp:
 24 (IPC::Attachment::decode):
 25
1262021-11-15 Chris Dumez <cdumez@apple.com>
227
328 Remove some dead code from IPC::Encoder / IPC::Decoder

Source/WebKit/Platform/IPC/Attachment.cpp

@@void Attachment::encode(Encoder& encoder) const
5656 encoder.addAttachment(WTFMove(*const_cast<Attachment*>(this)));
5757}
5858
59 bool Attachment::decode(Decoder& decoder, Attachment& attachment)
 59std::optional<Attachment> Attachment::decode(Decoder& decoder)
6060{
61  if (!decoder.removeAttachment(attachment))
62  return false;
63  return true;
 61 return decoder.takeAttachment();
6462}
6563#endif
6664

Source/WebKit/Platform/IPC/Attachment.h

2626
2727#pragma once
2828
 29#include <optional>
 30
2931#if OS(DARWIN) && !USE(UNIX_DOMAIN_SOCKETS)
3032#include <mach/mach_init.h>
3133#include <mach/mach_traps.h>

@@public:
98100#endif
99101
100102 void encode(Encoder&) const;
101  static WARN_UNUSED_RETURN bool decode(Decoder&, Attachment&);
 103 static std::optional<Attachment> decode(Decoder&);
102104
103105private:
104106 Type m_type;

Source/WebKit/Platform/IPC/Decoder.cpp

3333#include <stdio.h>
3434#include <wtf/StdLibExtras.h>
3535
36 #if PLATFORM(MAC)
37 #include "ImportanceAssertion.h"
38 #endif
39 
4036namespace IPC {
4137
4238static const uint8_t* copyBuffer(const uint8_t* buffer, size_t bufferSize)

@@std::unique_ptr<Decoder> Decoder::unwrapForTesting(Decoder& decoder)
147143{
148144 ASSERT(decoder.isSyncMessage());
149145
150  Vector<Attachment> attachments;
151  Attachment attachment;
152  while (decoder.removeAttachment(attachment))
153  attachments.append(WTFMove(attachment));
154  attachments.reverse();
 146 Vector<Attachment> attachments = std::exchange(decoder.m_attachments, { });
155147
156148 DataReference wrappedMessage;
157149 if (!decoder.decode(wrappedMessage))

@@const uint8_t* Decoder::decodeFixedLengthReference(size_t size, size_t alignment
218210 return data;
219211}
220212
221 bool Decoder::removeAttachment(Attachment& attachment)
 213std::optional<Attachment> Decoder::takeAttachment()
222214{
223215 if (m_attachments.isEmpty())
224  return false;
225 
226  attachment = m_attachments.takeLast();
227  return true;
 216 return std::nullopt;
 217 return m_attachments.takeLast();
228218}
229219
230220} // namespace IPC

Source/WebKit/Platform/IPC/Decoder.h

2727
2828#include "Attachment.h"
2929#include "MessageNames.h"
30 #include "StringReference.h"
31 #include <WebCore/SharedBuffer.h>
3230#include <wtf/OptionSet.h>
3331#include <wtf/Vector.h>
3432

3634#include "ImportanceAssertion.h"
3735#endif
3836
39 #if HAVE(QOS_CLASSES)
40 #include <pthread/qos.h>
41 #endif
42 
4337namespace IPC {
4438
4539enum class MessageFlags : uint8_t;

@@public:
140134 return bufferIsLargeEnoughToContain(alignof(T), numElements * sizeof(T));
141135 }
142136
143  bool removeAttachment(Attachment&);
 137 std::optional<Attachment> takeAttachment();
144138
145139 static constexpr bool isIPCDecoder = true;
146140

Source/WebKit/Platform/IPC/win/AttachmentWin.cpp

@@static bool getDuplicatedHandle(HANDLE sourceHandle, DWORD sourcePID, HANDLE& du
6565 return success;
6666}
6767
68 bool Attachment::decode(Decoder& decoder, Attachment& attachment)
 68std::optional<Attachment> Attachment::decode(Decoder& decoder)
6969{
70  ASSERT_ARG(attachment, attachment.m_handle == INVALID_HANDLE_VALUE);
71 
7270 uint64_t sourceHandle;
7371 if (!decoder.decode(sourceHandle))
74  return false;
 72 return std::nullopt;
7573
7674 uint32_t sourcePID;
7775 if (!decoder.decode(sourcePID))
78  return false;
 76 return std::nullopt;
7977
8078 HANDLE duplicatedHandle;
8179 if (!getDuplicatedHandle(reinterpret_cast<HANDLE>(sourceHandle), sourcePID, duplicatedHandle))
82  return false;
 80 return std::nullopt;
8381
84  attachment.m_handle = duplicatedHandle;
85  return true;
 82 return Attachment { duplicatedHandle };
8683}
8784
8885} // namespace IPC