<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE bugzilla SYSTEM "https://bugs.webkit.org/page.cgi?id=bugzilla.dtd">

<bugzilla version="5.0.4.1"
          urlbase="https://bugs.webkit.org/"
          
          maintainer="admin@webkit.org"
>

    <bug>
          <bug_id>241407</bug_id>
          
          <creation_ts>2022-06-08 01:28:54 -0700</creation_ts>
          <short_desc>SharedBuffer IPC encoders have duplicated and redundant code</short_desc>
          <delta_ts>2022-06-13 05:13:22 -0700</delta_ts>
          <reporter_accessible>1</reporter_accessible>
          <cclist_accessible>1</cclist_accessible>
          <classification_id>1</classification_id>
          <classification>Unclassified</classification>
          <product>WebKit</product>
          <component>WebKit2</component>
          <version>WebKit Nightly Build</version>
          <rep_platform>Unspecified</rep_platform>
          <op_sys>Unspecified</op_sys>
          <bug_status>RESOLVED</bug_status>
          <resolution>FIXED</resolution>
          
          <see_also>https://bugs.webkit.org/show_bug.cgi?id=241416</see_also>
          <bug_file_loc></bug_file_loc>
          <status_whiteboard></status_whiteboard>
          <keywords>InRadar</keywords>
          <priority>P2</priority>
          <bug_severity>Normal</bug_severity>
          <target_milestone>---</target_milestone>
          
          
          <everconfirmed>1</everconfirmed>
          <reporter name="Kimmo Kinnunen">kkinnunen</reporter>
          <assigned_to name="Jean-Yves Avenard [:jya]">jean-yves.avenard</assigned_to>
          <cc>jean-yves.avenard</cc>
    
    <cc>kkinnunen</cc>
    
    <cc>webkit-bug-importer</cc>
          

      

      

      

          <comment_sort_order>oldest_to_newest</comment_sort_order>  
          <long_desc isprivate="0" >
    <commentid>1874667</commentid>
    <comment_count>0</comment_count>
    <who name="Kimmo Kinnunen">kkinnunen</who>
    <bug_when>2022-06-08 01:28:54 -0700</bug_when>
    <thetext>SharedBuffer IPC encoders have duplicated and redundant code

There are multiple redundant top-level argument coders specialised for Ref,RefPtr:

template&lt;&gt; struct ArgumentCoder&lt;RefPtr&lt;WebCore::FragmentedSharedBuffer&gt;&gt; {
    static void encode(Encoder&amp;, const RefPtr&lt;WebCore::FragmentedSharedBuffer&gt;&amp;);
    static std::optional&lt;RefPtr&lt;WebCore::FragmentedSharedBuffer&gt;&gt; decode(Decoder&amp;);
};

template&lt;&gt; struct ArgumentCoder&lt;Ref&lt;WebCore::FragmentedSharedBuffer&gt;&gt; {
    static void encode(Encoder&amp;, const Ref&lt;WebCore::FragmentedSharedBuffer&gt;&amp;);
    static std::optional&lt;Ref&lt;WebCore::FragmentedSharedBuffer&gt;&gt; decode(Decoder&amp;);
};

template&lt;&gt; struct ArgumentCoder&lt;RefPtr&lt;WebCore::SharedBuffer&gt;&gt; {
    static void encode(Encoder&amp;, const RefPtr&lt;WebCore::SharedBuffer&gt;&amp;);
    static std::optional&lt;RefPtr&lt;WebCore::SharedBuffer&gt;&gt; decode(Decoder&amp;);
};


Instead, there should be top-level argument coders only for the root types:

template&lt;&gt; struct ArgumentCoder&lt;WebCore::FragmentedSharedBuffer&gt; {
    static void encode(Encoder&amp;, const WebCore::FragmentedSharedBuffer&amp;);
    static std::optional&lt;Ref&lt;WebCore::FragmentedSharedBuffer&gt;&gt; decode(Decoder&amp;);
};

template&lt;&gt; struct ArgumentCoder&lt;WebCore::SharedBuffer&gt; {
    static void encode(Encoder&amp;, const WebCore::SharedBuffer&amp;);
    static std::optional&lt;Ref&lt;WebCore::SharedBuffer&gt;&gt; decode(Decoder&amp;);
};



The encoder implementations use multiple redundant functions and duplicated logic for encoding and decoding

static void encodeSharedBuffer(Encoder&amp; encoder, const FragmentedSharedBuffer* buffer)
static void encodeTypesAndData(Encoder&amp; encoder, const Vector&lt;String&gt;&amp; types, const Vector&lt;RefPtr&lt;SharedBuffer&gt;&gt;&amp; data)


Instead, the implementations should be of form:
ArgumentCoder&lt;WebCore::FragmentedSharedBuffer&gt;::encode(Encoder&amp;, const WebCore::FragmentedSharedBuffer&amp; buffer) 
{
    encode the buffer
}

Generic Vector&lt;T&gt; encoders should be used to encode vectors of buffers, avoiding duplication of &quot;encode size, for each buffer encode the buffer&quot;.
Generic RefPtr&lt;T&gt; encoders should be used  to encode the refptrs of buffers, avoiding duplication of &quot;if exists then encode true and buffer else encode false&quot;.

The decoder implementations use multiple redundant functions and duplicated logic

static WARN_UNUSED_RETURN bool decodeSharedBuffer(Decoder&amp; decoder, RefPtr&lt;SharedBuffer&gt;&amp; buffer)

     if (!decodeSharedBuffer(decoder, content.dataInWebArchiveFormat))
        return false;

instead, the decoding should be of form:

std::optional&lt;PasteboardWebContent&gt; ArgumentCoder&lt;PasteboardWebContent&gt;::decode(Decoder&amp; decoder)
{
     auto dataInWebArchievFormat = decoder.decode&lt;RefPtr&lt;SharedBuffer&gt;&gt;();
     .....
     if (UNLIKELY(!decoder.isValid())
          return std::nullopt;
     return PasteboardWebContent { ...., WTFMove(*dataInWebArchiveFormat), ... };
}</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1874690</commentid>
    <comment_count>1</comment_count>
    <who name="Radar WebKit Bug Importer">webkit-bug-importer</who>
    <bug_when>2022-06-08 04:28:05 -0700</bug_when>
    <thetext>&lt;rdar://problem/94621576&gt;</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1874709</commentid>
    <comment_count>2</comment_count>
    <who name="Jean-Yves Avenard [:jya]">jean-yves.avenard</who>
    <bug_when>2022-06-08 06:43:47 -0700</bug_when>
    <thetext>Pull request: https://github.com/WebKit/WebKit/pull/1377</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1875506</commentid>
    <comment_count>3</comment_count>
    <who name="EWS">ews-feeder</who>
    <bug_when>2022-06-13 05:13:21 -0700</bug_when>
    <thetext>Committed r295487 (251492@main): &lt;https://commits.webkit.org/251492@main&gt;

Reviewed commits have been landed. Closing PR #1377 and removing active labels.</thetext>
  </long_desc>
      
      

    </bug>

</bugzilla>