Title: [220863] trunk/Source/WebCore
Revision
220863
Author
[email protected]
Date
2017-08-17 11:07:26 -0700 (Thu, 17 Aug 2017)

Log Message

Add a DOMPromiseDeferred method to handle ExceptionOr<> results
https://bugs.webkit.org/show_bug.cgi?id=175603

Patch by Youenn Fablet <[email protected]> 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):

Modified Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (220862 => 220863)


--- trunk/Source/WebCore/ChangeLog	2017-08-17 16:40:56 UTC (rev 220862)
+++ trunk/Source/WebCore/ChangeLog	2017-08-17 18:07:26 UTC (rev 220863)
@@ -1,3 +1,35 @@
+2017-08-17  Youenn Fablet  <[email protected]>
+
+        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  <[email protected]>
 
         [GStreamer] AppendPipeline: support dispatch of decryption-specific GstStructure into the pipeline

Modified: trunk/Source/WebCore/Modules/cache/Cache.cpp (220862 => 220863)


--- trunk/Source/WebCore/Modules/cache/Cache.cpp	2017-08-17 16:40:56 UTC (rev 220862)
+++ trunk/Source/WebCore/Modules/cache/Cache.cpp	2017-08-17 18:07:26 UTC (rev 220863)
@@ -172,12 +172,8 @@
         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 @@
         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 @@
     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 @@
     };
 }
 
-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 @@
     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);
     });

Modified: trunk/Source/WebCore/Modules/cache/Cache.h (220862 => 220863)


--- trunk/Source/WebCore/Modules/cache/Cache.h	2017-08-17 16:40:56 UTC (rev 220862)
+++ trunk/Source/WebCore/Modules/cache/Cache.h	2017-08-17 18:07:26 UTC (rev 220863)
@@ -68,8 +68,8 @@
     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>&&);
 

Modified: trunk/Source/WebCore/Modules/cache/CacheStorage.cpp (220862 => 220863)


--- trunk/Source/WebCore/Modules/cache/CacheStorage.cpp	2017-08-17 16:40:56 UTC (rev 220862)
+++ trunk/Source/WebCore/Modules/cache/CacheStorage.cpp	2017-08-17 18:07:26 UTC (rev 220863)
@@ -102,8 +102,9 @@
         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 @@
         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);

Modified: trunk/Source/WebCore/Modules/cache/CacheStorageConnection.cpp (220862 => 220863)


--- trunk/Source/WebCore/Modules/cache/CacheStorageConnection.cpp	2017-08-17 16:40:56 UTC (rev 220862)
+++ trunk/Source/WebCore/Modules/cache/CacheStorageConnection.cpp	2017-08-17 18:07:26 UTC (rev 220863)
@@ -33,16 +33,12 @@
 
 namespace WebCore {
 
-Exception CacheStorageConnection::exceptionFromError(Error error)
+ExceptionOr<void> CacheStorageConnection::errorToException(Error error)
 {
-    ASSERT(error != Error::None);
+    if (error == Error::NotImplemented)
+        return Exception { NotSupportedError, ASCIILiteral("Not implemented") };
 
-    switch (error) {
-    case 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)

Modified: trunk/Source/WebCore/Modules/cache/CacheStorageConnection.h (220862 => 220863)


--- trunk/Source/WebCore/Modules/cache/CacheStorageConnection.h	2017-08-17 16:40:56 UTC (rev 220862)
+++ trunk/Source/WebCore/Modules/cache/CacheStorageConnection.h	2017-08-17 18:07:26 UTC (rev 220863)
@@ -47,10 +47,17 @@
         NotImplemented,
     };
 
+    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);
+    }
+
     WEBCORE_EXPORT static bool queryCacheMatch(const ResourceRequest& request, const ResourceRequest& cachedRequest, const ResourceResponse&, const CacheQueryOptions&);
 
-    static Exception exceptionFromError(Error);
-
     struct Record {
         uint64_t identifier;
 

Modified: trunk/Source/WebCore/bindings/js/JSDOMPromiseDeferred.h (220862 => 220863)


--- trunk/Source/WebCore/bindings/js/JSDOMPromiseDeferred.h	2017-08-17 16:40:56 UTC (rev 220862)
+++ trunk/Source/WebCore/bindings/js/JSDOMPromiseDeferred.h	2017-08-17 18:07:26 UTC (rev 220863)
@@ -25,6 +25,7 @@
 
 #pragma once
 
+#include "ExceptionOr.h"
 #include "JSDOMConvert.h"
 #include "JSDOMGuardedObject.h"
 #include <runtime/CatchScope.h>
@@ -211,6 +212,15 @@
     { 
         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 @@
     { 
         m_promiseDeferred->resolve();
     }
+
+    void settle(ExceptionOr<void>&& result)
+    {
+        if (result.hasException()) {
+            reject(result.releaseException());
+            return;
+        }
+        resolve();
+    }
 };
 
 
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to