[Fetch API] Use Ref<const T> in FetchBody::m_data variant
authorcommit-queue@webkit.org <commit-queue@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 27 Sep 2016 16:47:49 +0000 (16:47 +0000)
committercommit-queue@webkit.org <commit-queue@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 27 Sep 2016 16:47:49 +0000 (16:47 +0000)
https://bugs.webkit.org/show_bug.cgi?id=162599

Patch by Youenn Fablet <youenn@apple.com> on 2016-09-27
Reviewed by Alex Christensen.

Source/WebCore:

Covered by existing tests.

Using Ref<const T> for all variants of m_data except for FormData since FetchBody is actually creating it and may modifiy it.
Updating blob loading code path to use a const Blob& instead of a Blob&.

* Modules/fetch/FetchBody.cpp:
(WebCore::FetchBody::FetchBody):
(WebCore::FetchBody::extract):
(WebCore::FetchBody::clone):
* Modules/fetch/FetchBody.h:
(WebCore::FetchBody::blobBody):
(WebCore::FetchBody::arrayBufferBody):
(WebCore::FetchBody::arrayBufferViewBody):
* Modules/fetch/FetchBodyOwner.cpp:
(WebCore::FetchBodyOwner::loadBlob):
* Modules/fetch/FetchBodyOwner.h:
* Modules/fetch/FetchLoader.cpp:
(WebCore::FetchLoader::start):
* Modules/fetch/FetchLoader.h:

Source/WTF:

Enabling to use DeferrableRefCounted<const T> by making m_refCount mutable.

* wtf/DeferrableRefCounted.h:
(WTF::DeferrableRefCountedBase::ref):
(WTF::DeferrableRefCountedBase::derefBase):
(WTF::DeferrableRefCounted::deref):

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@206438 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Source/WTF/ChangeLog
Source/WTF/wtf/DeferrableRefCounted.h
Source/WebCore/ChangeLog
Source/WebCore/Modules/fetch/FetchBody.cpp
Source/WebCore/Modules/fetch/FetchBody.h
Source/WebCore/Modules/fetch/FetchBodyOwner.cpp
Source/WebCore/Modules/fetch/FetchBodyOwner.h
Source/WebCore/Modules/fetch/FetchLoader.cpp
Source/WebCore/Modules/fetch/FetchLoader.h

index 76fc04b..2708a57 100644 (file)
@@ -1,3 +1,17 @@
+2016-09-27  Youenn Fablet  <youenn@apple.com>
+
+        [Fetch API] Use Ref<const T> in FetchBody::m_data variant
+        https://bugs.webkit.org/show_bug.cgi?id=162599
+
+        Reviewed by Alex Christensen.
+
+        Enabling to use DeferrableRefCounted<const T> by making m_refCount mutable.
+
+        * wtf/DeferrableRefCounted.h:
+        (WTF::DeferrableRefCountedBase::ref):
+        (WTF::DeferrableRefCountedBase::derefBase):
+        (WTF::DeferrableRefCounted::deref):
+
 2016-09-26  Daniel Bates  <dabates@apple.com>
 
         Rename IOS_TEXT_AUTOSIZING to TEXT_AUTOSIZING
index 6b92297..796481a 100644 (file)
  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-#ifndef DeferrableRefCounted_h
-#define DeferrableRefCounted_h
+#pragma once
 
 #include <wtf/Assertions.h>
 #include <wtf/FastMalloc.h>
@@ -41,39 +40,39 @@ namespace WTF {
 class DeferrableRefCountedBase {
     static const unsigned deferredFlag = 1;
     static const unsigned normalIncrement = 2;
-    
+
 public:
-    void ref()
+    void ref() const
     {
         m_refCount += normalIncrement;
     }
-    
+
     bool hasOneRef() const
     {
         return refCount() == 1;
     }
-    
+
     unsigned refCount() const
     {
         return m_refCount / normalIncrement;
     }
-    
+
     bool isDeferred() const
     {
         return !!(m_refCount & deferredFlag);
     }
-    
+
 protected:
     DeferrableRefCountedBase()
         : m_refCount(normalIncrement)
     {
     }
-    
+
     ~DeferrableRefCountedBase()
     {
     }
-    
-    bool derefBase()
+
+    bool derefBase() const
     {
         m_refCount -= normalIncrement;
         return !m_refCount;
@@ -88,21 +87,21 @@ protected:
         m_refCount &= ~deferredFlag;
         return !m_refCount;
     }
-    
+
 private:
-    unsigned m_refCount;
+    mutable unsigned m_refCount;
 };
 
 template<typename T>
 class DeferrableRefCounted : public DeferrableRefCountedBase {
     WTF_MAKE_NONCOPYABLE(DeferrableRefCounted); WTF_MAKE_FAST_ALLOCATED;
 public:
-    void deref()
+    void deref() const
     {
         if (derefBase())
-            delete static_cast<T*>(this);
+            delete static_cast<const T*>(this);
     }
-    
+
     bool setIsDeferred(bool value)
     {
         if (!setIsDeferredBase(value))
@@ -110,7 +109,7 @@ public:
         delete static_cast<T*>(this);
         return true;
     }
-    
+
 protected:
     DeferrableRefCounted() { }
     ~DeferrableRefCounted() { }
@@ -119,6 +118,3 @@ protected:
 } // namespace WTF
 
 using WTF::DeferrableRefCounted;
-
-#endif // DeferrableRefCounted_h
-
index 9c7f3fe..4582b84 100644 (file)
@@ -1,3 +1,30 @@
+2016-09-27  Youenn Fablet  <youenn@apple.com>
+
+        [Fetch API] Use Ref<const T> in FetchBody::m_data variant
+        https://bugs.webkit.org/show_bug.cgi?id=162599
+
+        Reviewed by Alex Christensen.
+
+        Covered by existing tests.
+
+        Using Ref<const T> for all variants of m_data except for FormData since FetchBody is actually creating it and may modifiy it.
+        Updating blob loading code path to use a const Blob& instead of a Blob&.
+
+        * Modules/fetch/FetchBody.cpp:
+        (WebCore::FetchBody::FetchBody):
+        (WebCore::FetchBody::extract):
+        (WebCore::FetchBody::clone):
+        * Modules/fetch/FetchBody.h:
+        (WebCore::FetchBody::blobBody):
+        (WebCore::FetchBody::arrayBufferBody):
+        (WebCore::FetchBody::arrayBufferViewBody):
+        * Modules/fetch/FetchBodyOwner.cpp:
+        (WebCore::FetchBodyOwner::loadBlob):
+        * Modules/fetch/FetchBodyOwner.h:
+        * Modules/fetch/FetchLoader.cpp:
+        (WebCore::FetchLoader::start):
+        * Modules/fetch/FetchLoader.h:
+
 2016-09-27  Michael Catanzaro  <mcatanzaro@igalia.com>
 
         Unreviewed typo fix
index 03525d2..a153ffd 100644 (file)
@@ -46,7 +46,7 @@
 
 namespace WebCore {
 
-FetchBody::FetchBody(Ref<Blob>&& blob)
+FetchBody::FetchBody(Ref<const Blob>&& blob)
     : m_type(Type::Blob)
     , m_data(WTFMove(blob))
     , m_contentType(this->blobBody().type())
@@ -67,13 +67,13 @@ FetchBody::FetchBody(String&& text)
 {
 }
 
-FetchBody::FetchBody(Ref<ArrayBuffer>&& data)
+FetchBody::FetchBody(Ref<const ArrayBuffer>&& data)
     : m_type(Type::ArrayBuffer)
     , m_data(WTFMove(data))
 {
 }
 
-FetchBody::FetchBody(Ref<ArrayBufferView>&& dataView)
+FetchBody::FetchBody(Ref<const ArrayBufferView>&& dataView)
     : m_type(Type::ArrayBufferView)
     , m_data(WTFMove(dataView))
 {
@@ -104,9 +104,10 @@ FetchBody FetchBody::extract(ScriptExecutionContext& context, JSC::ExecState& st
         return { *data };
     }
     if (value.inherits(JSC::JSArrayBufferView::info())) {
-        RefPtr<JSC::ArrayBufferView> data = toArrayBufferView(value);
+        auto data = toArrayBufferView(value);
         ASSERT(data);
-        return { data.releaseNonNull() };
+        // FIXME: We should be able to efficiently get a Ref<const T> from a RefPtr<T>.
+        return { *data };
     }
     return { };
 }
@@ -335,13 +336,13 @@ FetchBody FetchBody::clone() const
 
     switch (m_type) {
     case Type::ArrayBuffer:
-        clone.m_data = const_cast<ArrayBuffer&>(arrayBufferBody());
+        clone.m_data = arrayBufferBody();
         break;
     case Type::ArrayBufferView:
-        clone.m_data = const_cast<ArrayBufferView&>(arrayBufferViewBody());
+        clone.m_data = arrayBufferViewBody();
         break;
     case Type::Blob:
-        clone.m_data = const_cast<Blob&>(blobBody());
+        clone.m_data = blobBody();
         break;
     case Type::FormData:
         clone.m_data = const_cast<FormData&>(formDataBody());
index c0c9aa1..1badf3d 100644 (file)
@@ -89,9 +89,9 @@ public:
     FetchBody clone() const;
 
 private:
-    FetchBody(Ref<Blob>&&);
-    FetchBody(Ref<ArrayBuffer>&&);
-    FetchBody(Ref<ArrayBufferView>&&);
+    FetchBody(Ref<const Blob>&&);
+    FetchBody(Ref<const ArrayBuffer>&&);
+    FetchBody(Ref<const ArrayBufferView>&&);
     FetchBody(DOMFormData&, Document&);
     FetchBody(String&&);
     FetchBody(Type, const String&, const FetchBodyConsumer&);
@@ -105,21 +105,18 @@ private:
     void consumeText(Ref<DeferredPromise>&&);
     void consumeBlob(FetchBodyOwner&, Ref<DeferredPromise>&&);
 
-    Blob& blobBody() { return std::experimental::get<Ref<Blob>>(m_data).get(); }
-    const Blob& blobBody() const { return std::experimental::get<Ref<Blob>>(m_data).get(); }
+    const Blob& blobBody() const { return std::experimental::get<Ref<const Blob>>(m_data).get(); }
     FormData& formDataBody() { return std::experimental::get<Ref<FormData>>(m_data).get(); }
     const FormData& formDataBody() const { return std::experimental::get<Ref<FormData>>(m_data).get(); }
-    ArrayBuffer& arrayBufferBody() { return std::experimental::get<Ref<ArrayBuffer>>(m_data).get(); }
-    const ArrayBuffer& arrayBufferBody() const { return std::experimental::get<Ref<ArrayBuffer>>(m_data).get(); }
-    ArrayBufferView& arrayBufferViewBody() { return std::experimental::get<Ref<ArrayBufferView>>(m_data).get(); }
-    const ArrayBufferView& arrayBufferViewBody() const { return std::experimental::get<Ref<ArrayBufferView>>(m_data).get(); }
+    const ArrayBuffer& arrayBufferBody() const { return std::experimental::get<Ref<const ArrayBuffer>>(m_data).get(); }
+    const ArrayBufferView& arrayBufferViewBody() const { return std::experimental::get<Ref<const ArrayBufferView>>(m_data).get(); }
     String& textBody() { return std::experimental::get<String>(m_data); }
     const String& textBody() const { return std::experimental::get<String>(m_data); }
 
     Type m_type { Type::None };
 
     // FIXME: Add support for URLSearchParams.
-    std::experimental::variant<std::nullptr_t, Ref<Blob>, Ref<FormData>, Ref<ArrayBuffer>, Ref<ArrayBufferView>, String> m_data;
+    std::experimental::variant<std::nullptr_t, Ref<const Blob>, Ref<FormData>, Ref<const ArrayBuffer>, Ref<const ArrayBufferView>, String> m_data;
     String m_contentType;
 
     FetchBodyConsumer m_consumer { FetchBodyConsumer::Type::None };
index 6476094..14f6fc3 100644 (file)
@@ -141,7 +141,7 @@ void FetchBodyOwner::text(Ref<DeferredPromise>&& promise)
     m_body.text(*this, WTFMove(promise));
 }
 
-void FetchBodyOwner::loadBlob(Blob& blob, FetchBodyConsumer* consumer)
+void FetchBodyOwner::loadBlob(const Blob& blob, FetchBodyConsumer* consumer)
 {
     // Can only be called once for a body instance.
     ASSERT(isDisturbed());
index 53fd3d9..2f42a8e 100644 (file)
@@ -54,7 +54,7 @@ public:
 
     bool isDisturbedOrLocked() const;
 
-    void loadBlob(Blob&, FetchBodyConsumer*);
+    void loadBlob(const Blob&, FetchBodyConsumer*);
 
     bool isActive() const { return !!m_blobLoader; }
 
index 671a975..9113931 100644 (file)
@@ -47,7 +47,7 @@
 
 namespace WebCore {
 
-void FetchLoader::start(ScriptExecutionContext& context, Blob& blob)
+void FetchLoader::start(ScriptExecutionContext& context, const Blob& blob)
 {
     auto urlForReading = BlobURL::createPublicURL(context.securityOrigin());
     if (urlForReading.isEmpty()) {
index dbb82fb..44c1adb 100644 (file)
@@ -48,7 +48,7 @@ public:
     RefPtr<SharedBuffer> startStreaming();
 
     void start(ScriptExecutionContext&, const FetchRequest&);
-    void start(ScriptExecutionContext&, Blob&);
+    void start(ScriptExecutionContext&, const Blob&);
     void stop();
 
     bool isStarted() const { return m_isStarted; }