Cleanup platform Cookie
authordbates@webkit.org <dbates@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 16 May 2018 18:54:39 +0000 (18:54 +0000)
committerdbates@webkit.org <dbates@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 16 May 2018 18:54:39 +0000 (18:54 +0000)
https://bugs.webkit.org/show_bug.cgi?id=185654

Reviewed by Per Arne Vollan.

Remove unnecessary constructor and use =default for default constructor.
Simplify Cookie::decode() by having it decode directly into a stack-allocated
cookie. Remove some unnecessary local variables. Fix style nits.

* platform/Cookie.h:
(WebCore::Cookie::Cookie):
(WebCore::Cookie::isNull const):
(WebCore::Cookie::encode const):
(WebCore::Cookie::decode):
* platform/network/cf/CookieJarCFNet.cpp:
(WebCore::getRawCookies):
* platform/network/cocoa/CookieCocoa.mm:
(WebCore::cookieCreated):
(WebCore::Cookie::Cookie):
(WebCore::Cookie::operator== const):
(WebCore::Cookie::hash const):
* platform/network/soup/CookieJarSoup.cpp:
(WebCore::getRawCookies):

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

Source/WebCore/ChangeLog
Source/WebCore/platform/Cookie.h
Source/WebCore/platform/network/cf/CookieJarCFNet.cpp
Source/WebCore/platform/network/cocoa/CookieCocoa.mm
Source/WebCore/platform/network/soup/CookieJarSoup.cpp

index 051b6bc..9b5dc69 100644 (file)
@@ -1,3 +1,29 @@
+2018-05-16  Daniel Bates  <dabates@apple.com>
+
+        Cleanup platform Cookie
+        https://bugs.webkit.org/show_bug.cgi?id=185654
+
+        Reviewed by Per Arne Vollan.
+
+        Remove unnecessary constructor and use =default for default constructor.
+        Simplify Cookie::decode() by having it decode directly into a stack-allocated
+        cookie. Remove some unnecessary local variables. Fix style nits.
+
+        * platform/Cookie.h:
+        (WebCore::Cookie::Cookie):
+        (WebCore::Cookie::isNull const):
+        (WebCore::Cookie::encode const):
+        (WebCore::Cookie::decode):
+        * platform/network/cf/CookieJarCFNet.cpp:
+        (WebCore::getRawCookies):
+        * platform/network/cocoa/CookieCocoa.mm:
+        (WebCore::cookieCreated):
+        (WebCore::Cookie::Cookie):
+        (WebCore::Cookie::operator== const):
+        (WebCore::Cookie::hash const):
+        * platform/network/soup/CookieJarSoup.cpp:
+        (WebCore::getRawCookies):
+
 2018-05-14  Yusuke Suzuki  <utatane.tea@gmail.com>
 
         [Win] Use C++17 in MSVC
index 1aa664f..67d227c 100644 (file)
 namespace WebCore {
 
 struct Cookie {
-    Cookie() { }
-    
+    Cookie() = default;
     Cookie(WTF::HashTableDeletedValueType)
         : name(WTF::HashTableDeletedValue)
-    { }
-
-    Cookie(const String& name, const String& value, const String& domain, const String& path, double created, double expires, bool httpOnly, bool secure, bool session, const String& comment, const URL& commentURL, const Vector<uint16_t> ports)
-        : name(name)
-        , value(value)
-        , domain(domain)
-        , path(path)
-        , created(created)
-        , expires(expires)
-        , httpOnly(httpOnly)
-        , secure(secure)
-        , session(session)
-        , comment(comment)
-        , commentURL(commentURL)
-        , ports(ports)
     {
     }
 
@@ -76,16 +60,16 @@ struct Cookie {
     bool isNull() const
     {
         return name.isNull()
-        && value.isNull()
-        && domain.isNull()
-        && path.isNull()
-        && created == 0
-        && expires == 0
-        && !httpOnly
-        && !secure
-        && !session
-        && comment.isNull()
-        && commentURL.isNull();
+            && value.isNull()
+            && domain.isNull()
+            && path.isNull()
+            && !created
+            && !expires
+            && !httpOnly
+            && !secure
+            && !session
+            && comment.isNull()
+            && commentURL.isNull();
     }
     
     String name;
@@ -119,72 +103,49 @@ struct CookieHash {
 template<class Encoder>
 void Cookie::encode(Encoder& encoder) const
 {
-    encoder << name << value << domain << path << created << expires << httpOnly << secure << session << comment << commentURL << ports;
+    encoder << name;
+    encoder << value;
+    encoder << domain;
+    encoder << path;
+    encoder << created;
+    encoder << expires;
+    encoder << httpOnly;
+    encoder << secure;
+    encoder << session;
+    encoder << comment;
+    encoder << commentURL;
+    encoder << ports;
 }
 
 template<class Decoder>
 std::optional<Cookie> Cookie::decode(Decoder& decoder)
 {
-    std::optional<String> name;
-    decoder >> name;
-    if (!name)
+    Cookie cookie;
+    if (!decoder.decode(cookie.name))
         return std::nullopt;
-    
-    std::optional<String> value;
-    decoder >> value;
-    if (!value)
+    if (!decoder.decode(cookie.value))
         return std::nullopt;
-
-    std::optional<String> domain;
-    decoder >> domain;
-    if (!domain)
+    if (!decoder.decode(cookie.domain))
         return std::nullopt;
-
-    std::optional<String> path;
-    decoder >> path;
-    if (!path)
+    if (!decoder.decode(cookie.path))
         return std::nullopt;
-
-    std::optional<double> created;
-    decoder >> created;
-    if (!created)
+    if (!decoder.decode(cookie.created))
         return std::nullopt;
-
-    std::optional<double> expires;
-    decoder >> expires;
-    if (!expires)
+    if (!decoder.decode(cookie.expires))
         return std::nullopt;
-
-    std::optional<bool> httpOnly;
-    decoder >> httpOnly;
-    if (!httpOnly)
+    if (!decoder.decode(cookie.httpOnly))
         return std::nullopt;
-
-    std::optional<bool> secure;
-    decoder >> secure;
-    if (!secure)
+    if (!decoder.decode(cookie.secure))
         return std::nullopt;
-
-    std::optional<bool> session;
-    decoder >> session;
-    if (!session)
+    if (!decoder.decode(cookie.session))
         return std::nullopt;
-
-    std::optional<String> comment;
-    decoder >> comment;
-    if (!comment)
+    if (!decoder.decode(cookie.comment))
         return std::nullopt;
-
-    URL commentURL;
-    if (!decoder.decode(commentURL))
+    if (!decoder.decode(cookie.commentURL))
         return std::nullopt;
-
-    std::optional<Vector<uint16_t>> ports;
-    decoder >> ports;
-    if (!ports)
+    if (!decoder.decode(cookie.ports))
         return std::nullopt;
-
-    return {{ WTFMove(*name), WTFMove(*value), WTFMove(*domain), WTFMove(*path), WTFMove(*created), WTFMove(*expires), WTFMove(*httpOnly), WTFMove(*secure), WTFMove(*session), WTFMove(*comment), WTFMove(commentURL), WTFMove(*ports) }};
+    return cookie;
 }
 
 }
index 00bf0bd..1e0c74a 100644 (file)
@@ -257,24 +257,18 @@ bool getRawCookies(const NetworkStorageSession& session, const URL& firstParty,
     rawCookies.reserveCapacity(count);
 
     for (CFIndex i = 0; i < count; i++) {
-        CFHTTPCookieRef cookie = checked_cf_cast<CFHTTPCookieRef>(CFArrayGetValueAtIndex(cookiesCF.get(), i));
-        String name = cookieName(cookie).get();
-        String value = cookieValue(cookie).get();
-        String domain = cookieDomain(cookie).get();
-        String path = cookiePath(cookie).get();
-
-        double created = cookieCreatedTime(cookie);
-        double expires = cookieExpirationTime(cookie);
-
-        bool httpOnly = CFHTTPCookieIsHTTPOnly(cookie);
-        bool secure = CFHTTPCookieIsSecure(cookie);
-        bool session = false; // FIXME: Need API for if a cookie is a session cookie.
-
-        String comment;
-        URL commentURL;
-        Vector<uint16_t> ports;
-
-        rawCookies.uncheckedAppend(Cookie(name, value, domain, path, created, expires, httpOnly, secure, session, comment, commentURL, ports));
+        CFHTTPCookieRef cfCookie = checked_cf_cast<CFHTTPCookieRef>(CFArrayGetValueAtIndex(cookiesCF.get(), i));
+        Cookie cookie;
+        cookie.name = cookieName(cfCookie).get();
+        cookie.value = cookieValue(cfCookie).get();
+        cookie.domain = cookieDomain(cfCookie).get();
+        cookie.path = cookiePath(cfCookie).get();
+        cookie.created = cookieCreatedTime(cfCookie);
+        cookie.expires = cookieExpirationTime(cfCookie);
+        cookie.httpOnly = CFHTTPCookieIsHTTPOnly(cfCookie);
+        cookie.secure = CFHTTPCookieIsSecure(cfCookie);
+        cookie.session = false; // FIXME: Need API for if a cookie is a session cookie.
+        rawCookies.uncheckedAppend(WTFMove(cookie));
     }
 
     return true;
index 575579d..9bc2cab 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2015 Apple, Inc.  All rights reserved.
+ * Copyright (C) 2015-2018 Apple, Inc.  All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
@@ -68,12 +68,22 @@ static double cookieCreated(NSHTTPCookie *cookie)
     if ([value isKindOfClass:[NSString class]])
         return toCanonicalFormat(((NSString *)value).doubleValue);
 
-    return 0.0;
+    return 0;
 }
 
 Cookie::Cookie(NSHTTPCookie *cookie)
-    : Cookie(cookie.name, cookie.value, cookie.domain, cookie.path, cookieCreated(cookie), [cookie.expiresDate timeIntervalSince1970] * 1000.0,
-    cookie.HTTPOnly, cookie.secure, cookie.sessionOnly, cookie.comment, cookie.commentURL, portVectorFromList(cookie.portList))
+    : name { cookie.name }
+    , value { cookie.value }
+    , domain { cookie.domain }
+    , path { cookie.path }
+    , created { cookieCreated(cookie) }
+    , expires { [cookie.expiresDate timeIntervalSince1970] * 1000.0 }
+    , httpOnly { static_cast<bool>(cookie.HTTPOnly) }
+    , secure { static_cast<bool>(cookie.secure) }
+    , session { static_cast<bool>(cookie.sessionOnly) }
+    , comment { cookie.comment }
+    , commentURL { cookie.commentURL }
+    , ports { portVectorFromList(cookie.portList) }
 {
 }
 
@@ -132,17 +142,14 @@ bool Cookie::operator==(const Cookie& other) const
     bool otherNull = other.isNull();
     if (thisNull || otherNull)
         return thisNull == otherNull;
-    
-    NSHTTPCookie *nsCookie(*this);
-    return [nsCookie isEqual:other];
+    return [static_cast<NSHTTPCookie *>(*this) isEqual:other];
 }
     
 unsigned Cookie::hash() const
 {
     ASSERT(!name.isHashTableDeletedValue());
     ASSERT(!isNull());
-    NSHTTPCookie *nsCookie(*this);
-    return nsCookie.hash;
+    return static_cast<NSHTTPCookie *>(*this).hash;
 }
 
 } // namespace WebCore
index deadfa4..9c0912e 100644 (file)
@@ -159,11 +159,19 @@ bool getRawCookies(const NetworkStorageSession& session, const URL& firstParty,
         return false;
 
     for (GSList* iter = cookies.get(); iter; iter = g_slist_next(iter)) {
-        SoupCookie* cookie = static_cast<SoupCookie*>(iter->data);
-        rawCookies.append(Cookie(String::fromUTF8(cookie->name), String::fromUTF8(cookie->value), String::fromUTF8(cookie->domain),
-            String::fromUTF8(cookie->path), 0, cookie->expires ? static_cast<double>(soup_date_to_time_t(cookie->expires)) * 1000 : 0,
-            cookie->http_only, cookie->secure, !cookie->expires, String(), URL(), Vector<uint16_t>{ }));
-        soup_cookie_free(cookie);
+        SoupCookie* soupCookie = static_cast<SoupCookie*>(iter->data);
+        Cookie cookie;
+        cookie.name = String::fromUTF8(soupCookie->name);
+        cookie.value = String::fromUTF8(soupCookie->value);
+        cookie.domain = String::fromUTF8(soupCookie->domain);
+        cookie.path = String::fromUTF8(soupCookie->path);
+        cookie.created = 0;
+        cookie.expires = soupCookie->expires ? static_cast<double>(soup_date_to_time_t(soupCookie->expires)) * 1000 : 0;
+        cookie.httpOnly = soupCookie->http_only;
+        cookie.secure = soupCookie->secure;
+        cookie.session = !soupCookie->expires;
+        rawCookies.append(WTFMove(cookie));
+        soup_cookie_free(soupCookie);
     }
 
     return true;