Title: [163545] trunk/Source/WebCore
Revision
163545
Author
[email protected]
Date
2014-02-06 10:08:38 -0800 (Thu, 06 Feb 2014)

Log Message

Modernize CrossOriginPreflightResultCache
https://bugs.webkit.org/show_bug.cgi?id=128309

Reviewed by Antti Koivisto.

Use std::chrono::steady_clock instead of currentTime() for determining when
cache items expire, Use std::unique_ptr instead of OwnPtr, use NeverDestroyed,
get rid of unnecessary container typedefs now that we have auto. Finally,
de-indent the entire class declaration.

* loader/CrossOriginPreflightResultCache.cpp:
(WebCore::CrossOriginPreflightResultCache::CrossOriginPreflightResultCache):
(WebCore::parseAccessControlMaxAge):
(WebCore::CrossOriginPreflightResultCacheItem::parse):
(WebCore::CrossOriginPreflightResultCacheItem::allowsRequest):
(WebCore::CrossOriginPreflightResultCache::shared):
(WebCore::CrossOriginPreflightResultCache::appendEntry):
(WebCore::CrossOriginPreflightResultCache::canSkipPreflight):
* loader/CrossOriginPreflightResultCache.h:
(WebCore::CrossOriginPreflightResultCacheItem::CrossOriginPreflightResultCacheItem):
* loader/DocumentThreadableLoader.cpp:
(WebCore::DocumentThreadableLoader::didReceiveResponse):

Modified Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (163544 => 163545)


--- trunk/Source/WebCore/ChangeLog	2014-02-06 18:08:36 UTC (rev 163544)
+++ trunk/Source/WebCore/ChangeLog	2014-02-06 18:08:38 UTC (rev 163545)
@@ -1,3 +1,28 @@
+2014-02-06  Anders Carlsson  <[email protected]>
+
+        Modernize CrossOriginPreflightResultCache
+        https://bugs.webkit.org/show_bug.cgi?id=128309
+
+        Reviewed by Antti Koivisto.
+
+        Use std::chrono::steady_clock instead of currentTime() for determining when
+        cache items expire, Use std::unique_ptr instead of OwnPtr, use NeverDestroyed,
+        get rid of unnecessary container typedefs now that we have auto. Finally,
+        de-indent the entire class declaration.
+
+        * loader/CrossOriginPreflightResultCache.cpp:
+        (WebCore::CrossOriginPreflightResultCache::CrossOriginPreflightResultCache):
+        (WebCore::parseAccessControlMaxAge):
+        (WebCore::CrossOriginPreflightResultCacheItem::parse):
+        (WebCore::CrossOriginPreflightResultCacheItem::allowsRequest):
+        (WebCore::CrossOriginPreflightResultCache::shared):
+        (WebCore::CrossOriginPreflightResultCache::appendEntry):
+        (WebCore::CrossOriginPreflightResultCache::canSkipPreflight):
+        * loader/CrossOriginPreflightResultCache.h:
+        (WebCore::CrossOriginPreflightResultCacheItem::CrossOriginPreflightResultCacheItem):
+        * loader/DocumentThreadableLoader.cpp:
+        (WebCore::DocumentThreadableLoader::didReceiveResponse):
+
 2014-02-06  Gurpreet Kaur  <[email protected]>
 
         Menclose with no notation attribute does not display anything.

Modified: trunk/Source/WebCore/loader/CrossOriginPreflightResultCache.cpp (163544 => 163545)


--- trunk/Source/WebCore/loader/CrossOriginPreflightResultCache.cpp	2014-02-06 18:08:36 UTC (rev 163544)
+++ trunk/Source/WebCore/loader/CrossOriginPreflightResultCache.cpp	2014-02-06 18:08:38 UTC (rev 163545)
@@ -29,21 +29,25 @@
 
 #include "CrossOriginAccessControl.h"
 #include "ResourceResponse.h"
-#include <wtf/CurrentTime.h>
 #include <wtf/MainThread.h>
+#include <wtf/NeverDestroyed.h>
 #include <wtf/StdLibExtras.h>
 
 namespace WebCore {
 
 // These values are at the discretion of the user agent.
-static const unsigned defaultPreflightCacheTimeoutSeconds = 5;
-static const unsigned maxPreflightCacheTimeoutSeconds = 600; // Should be short enough to minimize the risk of using a poisoned cache after switching to a secure network.
+static const auto defaultPreflightCacheTimeout = std::chrono::seconds(5);
+static const auto maxPreflightCacheTimeout = std::chrono::seconds(600); // Should be short enough to minimize the risk of using a poisoned cache after switching to a secure network.
 
-static bool parseAccessControlMaxAge(const String& string, unsigned& expiryDelta)
+CrossOriginPreflightResultCache::CrossOriginPreflightResultCache()
 {
+}
+
+static bool parseAccessControlMaxAge(const String& string, std::chrono::seconds& expiryDelta)
+{
     // FIXME: this will not do the correct thing for a number starting with a '+'
     bool ok = false;
-    expiryDelta = string.toUIntStrict(&ok);
+    expiryDelta = std::chrono::seconds(string.toUIntStrict(&ok));
     return ok;
 }
 
@@ -99,14 +103,14 @@
         return false;
     }
 
-    unsigned expiryDelta;
+    std::chrono::seconds expiryDelta;
     if (parseAccessControlMaxAge(response.httpHeaderField("Access-Control-Max-Age"), expiryDelta)) {
-        if (expiryDelta > maxPreflightCacheTimeoutSeconds)
-            expiryDelta = maxPreflightCacheTimeoutSeconds;
+        if (expiryDelta > maxPreflightCacheTimeout)
+            expiryDelta = maxPreflightCacheTimeout;
     } else
-        expiryDelta = defaultPreflightCacheTimeoutSeconds;
+        expiryDelta = defaultPreflightCacheTimeout;
 
-    m_absoluteExpiryTime = monotonicallyIncreasingTime() + expiryDelta;
+    m_absoluteExpiryTime = std::chrono::steady_clock::now() + expiryDelta;
     return true;
 }
 
@@ -133,7 +137,7 @@
 bool CrossOriginPreflightResultCacheItem::allowsRequest(StoredCredentials includeCredentials, const String& method, const HTTPHeaderMap& requestHeaders) const
 {
     String ignoredExplanation;
-    if (m_absoluteExpiryTime < monotonicallyIncreasingTime())
+    if (m_absoluteExpiryTime < std::chrono::steady_clock::now())
         return false;
     if (includeCredentials == AllowStoredCredentials && m_credentials == DoNotAllowStoredCredentials)
         return false;
@@ -146,28 +150,29 @@
 
 CrossOriginPreflightResultCache& CrossOriginPreflightResultCache::shared()
 {
-    DEFINE_STATIC_LOCAL(CrossOriginPreflightResultCache, cache, ());
     ASSERT(isMainThread());
+
+    static NeverDestroyed<CrossOriginPreflightResultCache> cache;
     return cache;
 }
 
-void CrossOriginPreflightResultCache::appendEntry(const String& origin, const URL& url, PassOwnPtr<CrossOriginPreflightResultCacheItem> preflightResult)
+void CrossOriginPreflightResultCache::appendEntry(const String& origin, const URL& url, std::unique_ptr<CrossOriginPreflightResultCacheItem> preflightResult)
 {
     ASSERT(isMainThread());
-    m_preflightHashMap.set(std::make_pair(origin, url), preflightResult);
+    m_preflightHashMap.set(std::make_pair(origin, url), std::move(preflightResult));
 }
 
 bool CrossOriginPreflightResultCache::canSkipPreflight(const String& origin, const URL& url, StoredCredentials includeCredentials, const String& method, const HTTPHeaderMap& requestHeaders)
 {
     ASSERT(isMainThread());
-    CrossOriginPreflightResultHashMap::iterator cacheIt = m_preflightHashMap.find(std::make_pair(origin, url));
-    if (cacheIt == m_preflightHashMap.end())
+    auto it = m_preflightHashMap.find(std::make_pair(origin, url));
+    if (it == m_preflightHashMap.end())
         return false;
 
-    if (cacheIt->value->allowsRequest(includeCredentials, method, requestHeaders))
+    if (it->value->allowsRequest(includeCredentials, method, requestHeaders))
         return true;
 
-    m_preflightHashMap.remove(cacheIt);
+    m_preflightHashMap.remove(it);
     return false;
 }
 

Modified: trunk/Source/WebCore/loader/CrossOriginPreflightResultCache.h (163544 => 163545)


--- trunk/Source/WebCore/loader/CrossOriginPreflightResultCache.h	2014-02-06 18:08:36 UTC (rev 163544)
+++ trunk/Source/WebCore/loader/CrossOriginPreflightResultCache.h	2014-02-06 18:08:38 UTC (rev 163545)
@@ -29,60 +29,57 @@
 
 #include "URLHash.h"
 #include "ResourceHandleTypes.h"
+#include <chrono>
 #include <wtf/HashMap.h>
 #include <wtf/HashSet.h>
-#include <wtf/PassOwnPtr.h>
 #include <wtf/text/StringHash.h>
 
 namespace WebCore {
 
-    class HTTPHeaderMap;
-    class ResourceResponse;
+class HTTPHeaderMap;
+class ResourceResponse;
 
-    class CrossOriginPreflightResultCacheItem {
-        WTF_MAKE_NONCOPYABLE(CrossOriginPreflightResultCacheItem); WTF_MAKE_FAST_ALLOCATED;
-    public:
-        CrossOriginPreflightResultCacheItem(StoredCredentials credentials)
-            : m_absoluteExpiryTime(0)
-            , m_credentials(credentials)
-        {
-        }
+class CrossOriginPreflightResultCacheItem {
+    WTF_MAKE_NONCOPYABLE(CrossOriginPreflightResultCacheItem); WTF_MAKE_FAST_ALLOCATED;
+public:
+    explicit CrossOriginPreflightResultCacheItem(StoredCredentials credentials)
+        : m_credentials(credentials)
+    {
+    }
 
-        bool parse(const ResourceResponse&, String& errorDescription);
-        bool allowsCrossOriginMethod(const String&, String& errorDescription) const;
-        bool allowsCrossOriginHeaders(const HTTPHeaderMap&, String& errorDescription) const;
-        bool allowsRequest(StoredCredentials, const String& method, const HTTPHeaderMap& requestHeaders) const;
+    bool parse(const ResourceResponse&, String& errorDescription);
+    bool allowsCrossOriginMethod(const String&, String& errorDescription) const;
+    bool allowsCrossOriginHeaders(const HTTPHeaderMap&, String& errorDescription) const;
+    bool allowsRequest(StoredCredentials, const String& method, const HTTPHeaderMap& requestHeaders) const;
 
-    private:
-        typedef HashSet<String, CaseFoldingHash> HeadersSet;
+private:
+    // FIXME: A better solution to holding onto the absolute expiration time might be
+    // to start a timer for the expiration delta that removes this from the cache when
+    // it fires.
+    std::chrono::steady_clock::time_point m_absoluteExpiryTime;
+    StoredCredentials m_credentials;
+    HashSet<String> m_methods;
+    HashSet<String, CaseFoldingHash> m_headers;
+};
 
-        // FIXME: A better solution to holding onto the absolute expiration time might be
-        // to start a timer for the expiration delta that removes this from the cache when
-        // it fires.
-        double m_absoluteExpiryTime;
-        StoredCredentials m_credentials;
-        HashSet<String> m_methods;
-        HeadersSet m_headers;
-    };
+class CrossOriginPreflightResultCache {
+    WTF_MAKE_NONCOPYABLE(CrossOriginPreflightResultCache); WTF_MAKE_FAST_ALLOCATED;
 
-    class CrossOriginPreflightResultCache {
-        WTF_MAKE_NONCOPYABLE(CrossOriginPreflightResultCache); WTF_MAKE_FAST_ALLOCATED;
-    public:
-        static CrossOriginPreflightResultCache& shared();
+public:
+    static CrossOriginPreflightResultCache& shared();
 
-        void appendEntry(const String& origin, const URL&, PassOwnPtr<CrossOriginPreflightResultCacheItem>);
-        bool canSkipPreflight(const String& origin, const URL&, StoredCredentials, const String& method, const HTTPHeaderMap& requestHeaders);
+    void appendEntry(const String& origin, const URL&, std::unique_ptr<CrossOriginPreflightResultCacheItem>);
+    bool canSkipPreflight(const String& origin, const URL&, StoredCredentials, const String& method, const HTTPHeaderMap& requestHeaders);
 
-        void empty();
+    void empty();
 
-    private:
-        CrossOriginPreflightResultCache() { }
+private:
+    friend NeverDestroyed<CrossOriginPreflightResultCache>;
+    CrossOriginPreflightResultCache();
 
-        typedef HashMap<std::pair<String, URL>, OwnPtr<CrossOriginPreflightResultCacheItem>> CrossOriginPreflightResultHashMap;
+    HashMap<std::pair<String, URL>, std::unique_ptr<CrossOriginPreflightResultCacheItem>> m_preflightHashMap;
+};
 
-        CrossOriginPreflightResultHashMap m_preflightHashMap;
-    };
-
 } // namespace WebCore
 
 #endif

Modified: trunk/Source/WebCore/loader/DocumentThreadableLoader.cpp (163544 => 163545)


--- trunk/Source/WebCore/loader/DocumentThreadableLoader.cpp	2014-02-06 18:08:36 UTC (rev 163544)
+++ trunk/Source/WebCore/loader/DocumentThreadableLoader.cpp	2014-02-06 18:08:38 UTC (rev 163545)
@@ -258,7 +258,7 @@
             return;
         }
 
-        OwnPtr<CrossOriginPreflightResultCacheItem> preflightResult = adoptPtr(new CrossOriginPreflightResultCacheItem(m_options.allowCredentials));
+        auto preflightResult = std::make_unique<CrossOriginPreflightResultCacheItem>(static_cast<StoredCredentials>(m_options.allowCredentials));
         if (!preflightResult->parse(response, accessControlErrorDescription)
             || !preflightResult->allowsCrossOriginMethod(m_actualRequest->httpMethod(), accessControlErrorDescription)
             || !preflightResult->allowsCrossOriginHeaders(m_actualRequest->httpHeaderFields(), accessControlErrorDescription)) {
@@ -266,7 +266,7 @@
             return;
         }
 
-        CrossOriginPreflightResultCache::shared().appendEntry(securityOrigin()->toString(), m_actualRequest->url(), preflightResult.release());
+        CrossOriginPreflightResultCache::shared().appendEntry(securityOrigin()->toString(), m_actualRequest->url(), std::move(preflightResult));
     } else {
         if (!m_sameOriginRequest && m_options.crossOriginRequestPolicy == UseAccessControl) {
             if (!passesAccessControlCheck(response, m_options.allowCredentials, securityOrigin(), accessControlErrorDescription)) {
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to