Add a DOMPromiseDeferred method to handle ExceptionOr<> results
authorcommit-queue@webkit.org <commit-queue@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 17 Aug 2017 18:07:26 +0000 (18:07 +0000)
committercommit-queue@webkit.org <commit-queue@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 17 Aug 2017 18:07:26 +0000 (18:07 +0000)
https://bugs.webkit.org/show_bug.cgi?id=175603

Patch by Youenn Fablet <youenn@apple.com> on 2017-08-17
Reviewed by Darin Adler.

No change of behavior.

Introduce DOMPromiseDeferred::settle to reject/resolve a promise with an ExceptionOr<>.
Making batchPutOperation/batchDeleteOperation take a Function with an ExceptionOr<>.
Using DOMPromiseDeferred::settle in Cache put/remove.
Updated CacheStorageConnection to create ExceptionOr<> from CacheStorageConnection::Error.

* Modules/cache/Cache.cpp:
(WebCore::Cache::put):
(WebCore::Cache::remove):
(WebCore::Cache::batchDeleteOperation):
(WebCore::Cache::batchPutOperation):
* Modules/cache/Cache.h:
* Modules/cache/CacheStorage.cpp:
(WebCore::CacheStorage::open):
(WebCore::CacheStorage::remove):
* Modules/cache/CacheStorageConnection.cpp:
(WebCore::CacheStorageConnection::errorToException):
* Modules/cache/CacheStorageConnection.h:
(WebCore::CacheStorageConnection::errorToException):
(WebCore::CacheStorageConnection::exceptionOrResult):
* bindings/js/JSDOMPromiseDeferred.h:
(WebCore::DOMPromiseDeferred::settle):
(WebCore::DOMPromiseDeferred<void>::settle):

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

Source/WebCore/ChangeLog
Source/WebCore/Modules/cache/Cache.cpp
Source/WebCore/Modules/cache/Cache.h
Source/WebCore/Modules/cache/CacheStorage.cpp
Source/WebCore/Modules/cache/CacheStorageConnection.cpp
Source/WebCore/Modules/cache/CacheStorageConnection.h
Source/WebCore/bindings/js/JSDOMPromiseDeferred.h

index 99d28ff6b3e557479d7f67dd30968c241afc2bc6..39f28a56fc457c9ffa4a43aa03f7a5ff3e51b6b8 100644 (file)
@@ -1,3 +1,35 @@
+2017-08-17  Youenn Fablet  <youenn@apple.com>
+
+        Add a DOMPromiseDeferred method to handle ExceptionOr<> results
+        https://bugs.webkit.org/show_bug.cgi?id=175603
+
+        Reviewed by Darin Adler.
+
+        No change of behavior.
+
+        Introduce DOMPromiseDeferred::settle to reject/resolve a promise with an ExceptionOr<>.
+        Making batchPutOperation/batchDeleteOperation take a Function with an ExceptionOr<>.
+        Using DOMPromiseDeferred::settle in Cache put/remove.
+        Updated CacheStorageConnection to create ExceptionOr<> from CacheStorageConnection::Error.
+
+        * Modules/cache/Cache.cpp:
+        (WebCore::Cache::put):
+        (WebCore::Cache::remove):
+        (WebCore::Cache::batchDeleteOperation):
+        (WebCore::Cache::batchPutOperation):
+        * Modules/cache/Cache.h:
+        * Modules/cache/CacheStorage.cpp:
+        (WebCore::CacheStorage::open):
+        (WebCore::CacheStorage::remove):
+        * Modules/cache/CacheStorageConnection.cpp:
+        (WebCore::CacheStorageConnection::errorToException):
+        * Modules/cache/CacheStorageConnection.h:
+        (WebCore::CacheStorageConnection::errorToException):
+        (WebCore::CacheStorageConnection::exceptionOrResult):
+        * bindings/js/JSDOMPromiseDeferred.h:
+        (WebCore::DOMPromiseDeferred::settle):
+        (WebCore::DOMPromiseDeferred<void>::settle):
+
 2017-08-17  Zan Dobersek  <zdobersek@igalia.com>
 
         [GStreamer] AppendPipeline: support dispatch of decryption-specific GstStructure into the pipeline
index 37ee858f37d385c27db5d5d49b349bd5ddacbdda..dc55137087f4e96f130fd9c3a9d5a4193b163841 100644 (file)
@@ -172,12 +172,8 @@ void Cache::put(RequestInfo&& info, Ref<FetchResponse>&& response, DOMPromiseDef
         return;
     }
 
-    batchPutOperation(*request, response.get(), [promise = WTFMove(promise)](CacheStorageConnection::Error error) mutable {
-        if (error != CacheStorageConnection::Error::None) {
-            promise.reject(CacheStorageConnection::exceptionFromError(error));
-            return;
-        }
-        promise.resolve();
+    batchPutOperation(*request, response.get(), [promise = WTFMove(promise)](ExceptionOr<void>&& result) mutable {
+        promise.settle(WTFMove(result));
     });
 }
 
@@ -196,12 +192,8 @@ void Cache::remove(RequestInfo&& info, CacheQueryOptions&& options, DOMPromiseDe
         request = FetchRequest::create(*scriptExecutionContext(), WTFMove(info), { }).releaseReturnValue();
     }
 
-    batchDeleteOperation(*request, WTFMove(options), [promise = WTFMove(promise)](bool didDelete, CacheStorageConnection::Error error) mutable {
-        if (error !=  CacheStorageConnection::Error::None) {
-            promise.reject(CacheStorageConnection::exceptionFromError(error));
-            return;
-        }
-        promise.resolve(didDelete);
+    batchDeleteOperation(*request, WTFMove(options), [promise = WTFMove(promise)](ExceptionOr<bool>&& result) mutable {
+        promise.settle(WTFMove(result));
     });
 }
 
@@ -280,16 +272,13 @@ Vector<CacheStorageRecord> Cache::queryCacheWithTargetStorage(const FetchRequest
     return records;
 }
 
-void Cache::batchDeleteOperation(const FetchRequest& request, CacheQueryOptions&& options, WTF::Function<void(bool didRemoval, CacheStorageConnection::Error error)>&& callback)
+void Cache::batchDeleteOperation(const FetchRequest& request, CacheQueryOptions&& options, WTF::Function<void(ExceptionOr<bool>&&)>&& callback)
 {
     setPendingActivity(this);
     m_connection->batchDeleteOperation(m_identifier, request.internalRequest(), WTFMove(options), [this, callback = WTFMove(callback)](Vector<uint64_t>&& records, CacheStorageConnection::Error error) {
-        if (!m_isStopped) {
-            if (error == CacheStorageConnection::Error::None)
-                m_records.removeAllMatching([&](const auto& item) { return records.contains(item.identifier); });
+        if (!m_isStopped)
+            callback(CacheStorageConnection::exceptionOrResult(!records.isEmpty(), error));
 
-            callback(!records.isEmpty(), error);
-        }
         unsetPendingActivity(this);
     });
 }
@@ -310,7 +299,7 @@ static inline CacheStorageConnection::Record toConnectionRecord(const FetchReque
     };
 }
 
-void Cache::batchPutOperation(const FetchRequest& request, FetchResponse& response, WTF::Function<void(CacheStorageConnection::Error)>&& callback)
+void Cache::batchPutOperation(const FetchRequest& request, FetchResponse& response, WTF::Function<void(ExceptionOr<void>&&)>&& callback)
 {
     Vector<CacheStorageConnection::Record> records;
     records.append(toConnectionRecord(request, response));
@@ -318,7 +307,7 @@ void Cache::batchPutOperation(const FetchRequest& request, FetchResponse& respon
     setPendingActivity(this);
     m_connection->batchPutOperation(m_identifier, WTFMove(records), [this, callback = WTFMove(callback)](Vector<uint64_t>&&, CacheStorageConnection::Error error) {
         if (!m_isStopped)
-            callback(error);
+            callback(CacheStorageConnection::errorToException(error));
 
         unsetPendingActivity(this);
     });
index 2109733800783bdfd3b2661c602474440caa88f9..03436d9d05d746ea5e4e5efcd4be8f16be83e228 100644 (file)
@@ -68,8 +68,8 @@ private:
     void retrieveRecords(WTF::Function<void()>&&);
     Vector<CacheStorageRecord> queryCacheWithTargetStorage(const FetchRequest&, const CacheQueryOptions&, const Vector<CacheStorageRecord>&);
     void queryCache(Ref<FetchRequest>&&, CacheQueryOptions&&, WTF::Function<void(const Vector<CacheStorageRecord>&)>&&);
-    void batchDeleteOperation(const FetchRequest&, CacheQueryOptions&&, WTF::Function<void(bool didRemoval, CacheStorageConnection::Error)>&&);
-    void batchPutOperation(const FetchRequest&, FetchResponse&, WTF::Function<void(CacheStorageConnection::Error)>&&);
+    void batchDeleteOperation(const FetchRequest&, CacheQueryOptions&&, WTF::Function<void(ExceptionOr<bool>&&)>&&);
+    void batchPutOperation(const FetchRequest&, FetchResponse&, WTF::Function<void(ExceptionOr<void>&&)>&&);
 
     void updateRecords(Vector<CacheStorageConnection::Record>&&);
 
index a0c29de9bd423be5a244d6bdc18714e18e6fc97c..094801573d5128e3d4ced5abd3290dfac923d51f 100644 (file)
@@ -102,8 +102,9 @@ void CacheStorage::open(const String& name, DOMPromiseDeferred<IDLInterface<Cach
         setPendingActivity(this);
         m_connection->open(origin, name, [this, name, promise = WTFMove(promise)](uint64_t cacheIdentifier, CacheStorageConnection::Error error) mutable {
             if (!m_isStopped) {
-                if (error != CacheStorageConnection::Error::None)
-                    promise.reject(CacheStorageConnection::exceptionFromError(error));
+                auto result = CacheStorageConnection::errorToException(error);
+                if (result.hasException())
+                    promise.reject(result.releaseException());
                 else {
                     auto cache = Cache::create(*scriptExecutionContext(), String { name }, cacheIdentifier, m_connection.copyRef());
                     promise.resolve(cache);
@@ -130,12 +131,9 @@ void CacheStorage::remove(const String& name, DOMPromiseDeferred<IDLBoolean>&& p
         setPendingActivity(this);
         m_connection->remove(m_caches[position]->identifier(), [this, name, promise = WTFMove(promise)](uint64_t cacheIdentifier, CacheStorageConnection::Error error) mutable {
             UNUSED_PARAM(cacheIdentifier);
-            if (!m_isStopped) {
-                if (error != CacheStorageConnection::Error::None)
-                    promise.reject(CacheStorageConnection::exceptionFromError(error));
-                else
-                    promise.resolve(true);
-            }
+            if (!m_isStopped)
+                promise.settle(CacheStorageConnection::exceptionOrResult(true, error));
+
             unsetPendingActivity(this);
         });
         m_caches.remove(position);
index 53f0b6571300fed8fb0d5272c472f9eecf843195..3562724d4bae743fdbb456c4ae5e88440124bcd9 100644 (file)
 
 namespace WebCore {
 
-Exception CacheStorageConnection::exceptionFromError(Error error)
+ExceptionOr<void> CacheStorageConnection::errorToException(Error error)
 {
-    ASSERT(error != Error::None);
-
-    switch (error) {
-    case Error::NotImplemented:
+    if (error == Error::NotImplemented)
         return Exception { NotSupportedError, ASCIILiteral("Not implemented") };
-    default:
-        return Exception { TypeError, ASCIILiteral("Unknown error") };
-    };
+
+    return { };
 }
 
 bool CacheStorageConnection::queryCacheMatch(const ResourceRequest& request, const ResourceRequest& cachedRequest, const ResourceResponse& cachedResponse, const CacheQueryOptions& options)
index 2bf69e5ac5d22ba2d0a49ba88d540d37f37ea29a..c37e67a8bff98ce7346015af6c47d7f8f8b77a17 100644 (file)
@@ -47,9 +47,16 @@ public:
         NotImplemented,
     };
 
-    WEBCORE_EXPORT static bool queryCacheMatch(const ResourceRequest& request, const ResourceRequest& cachedRequest, const ResourceResponse&, const CacheQueryOptions&);
+    static ExceptionOr<void> errorToException(Error);
+    template<typename T> static ExceptionOr<T> exceptionOrResult(T&& value, Error error)
+    {
+        auto result = errorToException(error);
+        if (result.hasException())
+            return result.releaseException();
+        return std::forward<T>(value);
+    }
 
-    static Exception exceptionFromError(Error);
+    WEBCORE_EXPORT static bool queryCacheMatch(const ResourceRequest& request, const ResourceRequest& cachedRequest, const ResourceResponse&, const CacheQueryOptions&);
 
     struct Record {
         uint64_t identifier;
index 36996cd705c03ed87ecf527c44e2bc263817b5eb..fb6cdf538b80bbf7f47d4cea24c95bbfc4fe8a3f 100644 (file)
@@ -25,6 +25,7 @@
 
 #pragma once
 
+#include "ExceptionOr.h"
 #include "JSDOMConvert.h"
 #include "JSDOMGuardedObject.h"
 #include <runtime/CatchScope.h>
@@ -211,6 +212,15 @@ public:
     { 
         m_promiseDeferred->resolve<IDLType>(std::forward<typename IDLType::ParameterType>(value));
     }
+
+    void settle(ExceptionOr<typename IDLType::ParameterType>&& result)
+    {
+        if (result.hasException()) {
+            reject(result.releaseException());
+            return;
+        }
+        resolve(result.releaseReturnValue());
+    }
 };
 
 template<> class DOMPromiseDeferred<void> : public DOMPromiseDeferredBase {
@@ -224,6 +234,15 @@ public:
     { 
         m_promiseDeferred->resolve();
     }
+
+    void settle(ExceptionOr<void>&& result)
+    {
+        if (result.hasException()) {
+            reject(result.releaseException());
+            return;
+        }
+        resolve();
+    }
 };