Title: [109671] trunk/Source
Revision
109671
Author
[email protected]
Date
2012-03-03 19:49:19 -0800 (Sat, 03 Mar 2012)

Log Message

Implement the basis of KURLWTFURL
https://bugs.webkit.org/show_bug.cgi?id=79600

Reviewed by Adam Barth.

Source/_javascript_Core: 

Add an API to know if a ParsedURL is valid.

* wtf/url/api/ParsedURL.cpp:
(WTF::ParsedURL::ParsedURL):
(WTF):
(WTF::ParsedURL::isolatedCopy): This is needed by APIs moving URL objects between thread
and by KURL's detach() on write.
(WTF::ParsedURL::baseAsString):
(WTF::ParsedURL::segment):
Add a stronger constraint on accessors: the client of this API should never ask for the segments
on an invalid URL.
* wtf/url/api/ParsedURL.h:
(WTF):
(WTF::ParsedURL::ParsedURL):
(ParsedURL):
(WTF::ParsedURL::isValid):

Source/WebCore: 

Add a simple, non-optimized, implementation for the main methods of KURL based
on ParsedURL.

* platform/KURLWTFURL.cpp:
(WebCore):
(WebCore::detach):
(WebCore::KURL::KURL):
(WebCore::KURL::copy):
(WebCore::KURL::isEmpty):
(WebCore::KURL::isValid):
(WebCore::KURL::string):
(WebCore::KURL::protocol):
(WebCore::KURL::host):
(WebCore::KURL::hasPort):
(WebCore::KURL::port):
(WebCore::KURL::user):
(WebCore::KURL::pass):
(WebCore::KURL::hasPath):
(WebCore::KURL::path):
(WebCore::KURL::lastPathComponent):
(WebCore::KURL::query):
(WebCore::KURL::hasFragmentIdentifier):
(WebCore::KURL::fragmentIdentifier):
(WebCore::KURL::baseAsString):
(WebCore::KURL::fileSystemPath):
(WebCore::KURL::protocolIs):
(WebCore::KURL::protocolIsInHTTPFamily):
(WebCore::KURL::setProtocol):
(WebCore::KURL::setHost):
(WebCore::KURL::removePort):
(WebCore::KURL::setPort):
(WebCore::KURL::setHostAndPort):
(WebCore::KURL::setUser):
(WebCore::KURL::setPass):
(WebCore::KURL::setPath):
(WebCore::KURL::setQuery):
(WebCore::KURL::setFragmentIdentifier):
(WebCore::KURL::removeFragmentIdentifier):
(WebCore::protocolHostAndPortAreEqual):
* platform/KURLWTFURLImpl.h:
(KURLWTFURLImpl):
(WebCore::KURLWTFURLImpl::copy):
(WebCore):

Modified Paths

Diff

Modified: trunk/Source/_javascript_Core/ChangeLog (109670 => 109671)


--- trunk/Source/_javascript_Core/ChangeLog	2012-03-04 03:26:27 UTC (rev 109670)
+++ trunk/Source/_javascript_Core/ChangeLog	2012-03-04 03:49:19 UTC (rev 109671)
@@ -1,3 +1,27 @@
+2012-03-03  Benjamin Poulain  <[email protected]>
+
+        Implement the basis of KURLWTFURL
+        https://bugs.webkit.org/show_bug.cgi?id=79600
+
+        Reviewed by Adam Barth.
+
+        Add an API to know if a ParsedURL is valid.
+
+        * wtf/url/api/ParsedURL.cpp:
+        (WTF::ParsedURL::ParsedURL):
+        (WTF):
+        (WTF::ParsedURL::isolatedCopy): This is needed by APIs moving URL objects between thread
+        and by KURL's detach() on write.
+        (WTF::ParsedURL::baseAsString):
+        (WTF::ParsedURL::segment):
+        Add a stronger constraint on accessors: the client of this API should never ask for the segments
+        on an invalid URL.
+        * wtf/url/api/ParsedURL.h:
+        (WTF):
+        (WTF::ParsedURL::ParsedURL):
+        (ParsedURL):
+        (WTF::ParsedURL::isValid):
+
 2012-03-03  Hans Wennborg  <[email protected]>
 
         Implement Speech _javascript_ API

Modified: trunk/Source/_javascript_Core/wtf/url/api/ParsedURL.cpp (109670 => 109671)


--- trunk/Source/_javascript_Core/wtf/url/api/ParsedURL.cpp	2012-03-04 03:26:27 UTC (rev 109670)
+++ trunk/Source/_javascript_Core/wtf/url/api/ParsedURL.cpp	2012-03-04 03:49:19 UTC (rev 109671)
@@ -33,15 +33,23 @@
 
 namespace WTF {
 
-ParsedURL::ParsedURL(const URLString& spec)
-    : m_spec(spec)
+ParsedURL::ParsedURL(const String& urlString)
+    : m_spec(urlString)
 {
     // FIXME: Handle non-standard URLs.
-    if (spec.string().isEmpty())
+    if (urlString.isEmpty())
         return;
-    URLParser<UChar>::parseStandardURL(spec.string().characters(), spec.string().length(), m_segments);
+    URLParser<UChar>::parseStandardURL(urlString.characters(), urlString.length(), m_segments);
 }
 
+ParsedURL ParsedURL::isolatedCopy() const
+{
+    ParsedURL copy;
+    copy.m_segments = this->m_segments;
+    copy.m_spec = URLString(this->m_spec.string().isolatedCopy());
+    return copy;
+}
+
 String ParsedURL::scheme() const
 {
     return segment(m_segments.scheme);
@@ -82,11 +90,22 @@
     return segment(m_segments.fragment);
 }
 
+String ParsedURL::baseAsString() const
+{
+    // FIXME: Add WTFURL Implementation.
+    return String();
+}
+
 String ParsedURL::segment(const URLComponent& component) const
 {
+    ASSERT(isValid());
+
     if (!component.isValid())
         return String();
-    return m_spec.string().substring(component.begin(), component.length());
+
+    String segment = m_spec.string().substring(component.begin(), component.length());
+    ASSERT_WITH_MESSAGE(!segment.isEmpty(), "A valid URL component should not be empty.");
+    return segment;
 }
 
 }

Modified: trunk/Source/_javascript_Core/wtf/url/api/ParsedURL.h (109670 => 109671)


--- trunk/Source/_javascript_Core/wtf/url/api/ParsedURL.h	2012-03-04 03:26:27 UTC (rev 109670)
+++ trunk/Source/_javascript_Core/wtf/url/api/ParsedURL.h	2012-03-04 03:49:19 UTC (rev 109671)
@@ -35,21 +35,29 @@
 
 class URLComponent;
 
+// ParsedURL represents a valid URL decomposed by components.
 class ParsedURL {
 public:
-    explicit ParsedURL(const URLString&);
-
     // FIXME: Add a method for parsing non-canonicalized URLs.
+    ParsedURL() { };
+    WTF_EXPORT_PRIVATE explicit ParsedURL(const String&);
 
-    String scheme() const;
-    String username() const;
-    String password() const;
-    String host() const;
-    String port() const;
-    String path() const;
-    String query() const;
-    String fragment() const;
+    WTF_EXPORT_PRIVATE ParsedURL isolatedCopy() const;
 
+    bool isValid() const { return !m_spec.string().isEmpty(); }
+
+    // Return a URL component or a null String if the component is undefined for the URL.
+    WTF_EXPORT_PRIVATE String scheme() const;
+    WTF_EXPORT_PRIVATE String username() const;
+    WTF_EXPORT_PRIVATE String password() const;
+    WTF_EXPORT_PRIVATE String host() const;
+    WTF_EXPORT_PRIVATE String port() const;
+    WTF_EXPORT_PRIVATE String path() const;
+    WTF_EXPORT_PRIVATE String query() const;
+    WTF_EXPORT_PRIVATE String fragment() const;
+
+    WTF_EXPORT_PRIVATE String baseAsString() const;
+
     URLString spec() { return m_spec; }
 
 private:

Modified: trunk/Source/WebCore/ChangeLog (109670 => 109671)


--- trunk/Source/WebCore/ChangeLog	2012-03-04 03:26:27 UTC (rev 109670)
+++ trunk/Source/WebCore/ChangeLog	2012-03-04 03:49:19 UTC (rev 109671)
@@ -1,5 +1,56 @@
 2012-03-03  Benjamin Poulain  <[email protected]>
 
+        Implement the basis of KURLWTFURL
+        https://bugs.webkit.org/show_bug.cgi?id=79600
+
+        Reviewed by Adam Barth.
+
+        Add a simple, non-optimized, implementation for the main methods of KURL based
+        on ParsedURL.
+
+        * platform/KURLWTFURL.cpp:
+        (WebCore):
+        (WebCore::detach):
+        (WebCore::KURL::KURL):
+        (WebCore::KURL::copy):
+        (WebCore::KURL::isEmpty):
+        (WebCore::KURL::isValid):
+        (WebCore::KURL::string):
+        (WebCore::KURL::protocol):
+        (WebCore::KURL::host):
+        (WebCore::KURL::hasPort):
+        (WebCore::KURL::port):
+        (WebCore::KURL::user):
+        (WebCore::KURL::pass):
+        (WebCore::KURL::hasPath):
+        (WebCore::KURL::path):
+        (WebCore::KURL::lastPathComponent):
+        (WebCore::KURL::query):
+        (WebCore::KURL::hasFragmentIdentifier):
+        (WebCore::KURL::fragmentIdentifier):
+        (WebCore::KURL::baseAsString):
+        (WebCore::KURL::fileSystemPath):
+        (WebCore::KURL::protocolIs):
+        (WebCore::KURL::protocolIsInHTTPFamily):
+        (WebCore::KURL::setProtocol):
+        (WebCore::KURL::setHost):
+        (WebCore::KURL::removePort):
+        (WebCore::KURL::setPort):
+        (WebCore::KURL::setHostAndPort):
+        (WebCore::KURL::setUser):
+        (WebCore::KURL::setPass):
+        (WebCore::KURL::setPath):
+        (WebCore::KURL::setQuery):
+        (WebCore::KURL::setFragmentIdentifier):
+        (WebCore::KURL::removeFragmentIdentifier):
+        (WebCore::protocolHostAndPortAreEqual):
+        * platform/KURLWTFURLImpl.h:
+        (KURLWTFURLImpl):
+        (WebCore::KURLWTFURLImpl::copy):
+        (WebCore):
+
+2012-03-03  Benjamin Poulain  <[email protected]>
+
         Remove the redundant method KURL::protocolInHTTPFamily()
         https://bugs.webkit.org/show_bug.cgi?id=80216
 

Modified: trunk/Source/WebCore/platform/KURLWTFURL.cpp (109670 => 109671)


--- trunk/Source/WebCore/platform/KURLWTFURL.cpp	2012-03-04 03:26:27 UTC (rev 109670)
+++ trunk/Source/WebCore/platform/KURLWTFURL.cpp	2012-03-04 03:49:19 UTC (rev 109671)
@@ -28,13 +28,31 @@
 
 #if USE(WTFURL)
 
+using namespace WTF;
+
 namespace WebCore {
 
-KURL::KURL(ParsedURLStringTag, const String&)
+static const unsigned maximumValidPortNumber = 0xFFFE;
+static const unsigned invalidPortNumber = 0xFFFF;
+
+static inline void detach(RefPtr<KURLWTFURLImpl>& urlImpl)
 {
-    // FIXME: Add WTFURL Implementation.
+    if (!urlImpl)
+        return;
+
+    if (urlImpl->hasOneRef())
+        return;
+
+    urlImpl = urlImpl->copy();
 }
 
+KURL::KURL(ParsedURLStringTag, const String& urlString)
+    : m_urlImpl(adoptRef(new KURLWTFURLImpl()))
+{
+    m_urlImpl->m_parsedURL = ParsedURL(urlString);
+    ASSERT(m_urlImpl->m_parsedURL.isValid());
+}
+
 KURL::KURL(const KURL&, const String&)
 {
     // FIXME: Add WTFURL Implementation.
@@ -47,8 +65,10 @@
 
 KURL KURL::copy() const
 {
-    // FIXME: Add WTFURL Implementation.
-    return KURL();
+    KURL other;
+    if (!isNull())
+        other.m_urlImpl = m_urlImpl->copy();
+    return other;
 }
 
 bool KURL::isNull() const
@@ -56,174 +76,218 @@
     return !m_urlImpl;
 }
 
+// FIXME: Can we get rid of the concept of EmptyURL? Can an null URL be enough?
+// If we cannot get rid of the concept, we should make a shared empty URL.
 bool KURL::isEmpty() const
 {
-    // FIXME: Add WTFURL Implementation.
-    return true;
+    return !m_urlImpl
+           || (!m_urlImpl->m_parsedURL.isValid() && m_urlImpl->m_invalidUrlString.isEmpty());
 }
 
 bool KURL::isValid() const
 {
-    // FIXME: Add WTFURL Implementation.
-    return false;
-}
+    if (!m_urlImpl)
+        return false;
 
-bool KURL::hasPath() const
-{
-    // FIXME: Add WTFURL Implementation.
-    return false;
+    bool isParsedURLValid = m_urlImpl->m_parsedURL.isValid();
+#ifndef NDEBUG
+    if (isParsedURLValid)
+        ASSERT_WITH_MESSAGE(m_urlImpl->m_invalidUrlString.isNull(), "A valid URL should never have a valid invalidUrlString.");
+#endif
+    return isParsedURLValid;
 }
 
 const String &KURL::string() const
 {
-    // FIXME: Add WTFURL Implementation.
-    ASSERT(m_urlImpl);
-    return m_urlImpl->m_invalidUrlString;
+    if (isNull()) {
+        DEFINE_STATIC_LOCAL(const String, nullString, ());
+        return nullString;
+    }
+
+    if (!m_urlImpl->m_invalidUrlString.isNull()) {
+        ASSERT(!isValid());
+        return m_urlImpl->m_invalidUrlString;
+    }
+
+    ASSERT(isValid());
+    return m_urlImpl->m_parsedURL.spec().string();
 }
 
 String KURL::protocol() const
 {
-    // FIXME: Add WTFURL Implementation.
-    return String();
+    ASSERT(isValid());
+    return m_urlImpl->m_parsedURL.scheme();
 }
 
 String KURL::host() const
 {
-    // FIXME: Add WTFURL Implementation.
-    return String();
+    ASSERT(isValid());
+    return m_urlImpl->m_parsedURL.host();
 }
 
-unsigned short KURL::port() const
+bool KURL::hasPort() const
 {
-    // FIXME: Add WTFURL Implementation.
-    return 0;
+    ASSERT(isValid());
+    return !m_urlImpl->m_parsedURL.port().isNull();
 }
 
-bool KURL::hasPort() const
+unsigned short KURL::port() const
 {
-    // FIXME: Add WTFURL Implementation.
-    return false;
+    ASSERT(isValid());
+
+    String portString = m_urlImpl->m_parsedURL.port();
+    bool ok = false;
+    unsigned portValue = portString.toUIntStrict(&ok);
+
+    if (!ok || portValue > maximumValidPortNumber)
+        return invalidPortNumber;
+
+    return static_cast<unsigned short>(portValue);
 }
 
 String KURL::user() const
 {
-    // FIXME: Add WTFURL Implementation.
-    return String();
+    ASSERT(isValid());
+    return m_urlImpl->m_parsedURL.username();
 }
 
 String KURL::pass() const
 {
-    // FIXME: Add WTFURL Implementation.
-    return String();
+    ASSERT(isValid());
+    return m_urlImpl->m_parsedURL.password();
 }
 
+bool KURL::hasPath() const
+{
+    ASSERT(isValid());
+    return !path().isEmpty();
+}
+
 String KURL::path() const
 {
-    // FIXME: Add WTFURL Implementation.
-    return String();
+    ASSERT(isValid());
+    return m_urlImpl->m_parsedURL.path();
 }
 
 String KURL::lastPathComponent() const
 {
-    // FIXME: Add WTFURL Implementation.
-    return String();
+    ASSERT(isValid());
+
+    String pathString = path();
+    size_t index = pathString.reverseFind('/');
+
+    if (index == notFound)
+        return pathString;
+
+    return pathString.substring(index + 1);
 }
 
 String KURL::query() const
 {
-    // FIXME: Add WTFURL Implementation.
-    return String();
+    ASSERT(isValid());
+    return m_urlImpl->m_parsedURL.query();
 }
 
-String KURL::fragmentIdentifier() const
+bool KURL::hasFragmentIdentifier() const
 {
-    // FIXME: Add WTFURL Implementation.
-    return String();
+    ASSERT(isValid());
+    return !fragmentIdentifier().isNull();
 }
 
-bool KURL::hasFragmentIdentifier() const
+String KURL::fragmentIdentifier() const
 {
-    // FIXME: Add WTFURL Implementation.
-    return false;
+    ASSERT(isValid());
+    return m_urlImpl->m_parsedURL.fragment();
 }
 
+// FIXME: track an fix the bad use of this method.
 String KURL::baseAsString() const
 {
-    // FIXME: Add WTFURL Implementation.
-    return String();
+    ASSERT(isValid());
+    return m_urlImpl->m_parsedURL.baseAsString();
 }
 
+// FIXME: Get rid of this function from KURL.
 String KURL::fileSystemPath() const
 {
-    // FIXME: Add WTFURL Implementation.
     return String();
 }
 
-bool KURL::protocolIs(const char*) const
+bool KURL::protocolIs(const char* testProtocol) const
 {
-    // FIXME: Add WTFURL Implementation.
-    return false;
+    ASSERT(isValid());
+    return WebCore::protocolIs(protocol(), testProtocol);
 }
 
 bool KURL::protocolIsInHTTPFamily() const
 {
-    // FIXME: Add WTFURL Implementation.
-    return false;
+    return protocolIs("http") || protocolIs("https");
 }
 
 bool KURL::setProtocol(const String&)
 {
+    detach(m_urlImpl);
     // FIXME: Add WTFURL Implementation.
     return false;
 }
 
 void KURL::setHost(const String&)
 {
+    detach(m_urlImpl);
     // FIXME: Add WTFURL Implementation.
 }
 
 void KURL::removePort()
 {
+    detach(m_urlImpl);
     // FIXME: Add WTFURL Implementation.
 }
 
 void KURL::setPort(unsigned short)
 {
+    detach(m_urlImpl);
     // FIXME: Add WTFURL Implementation.
 }
 
 void KURL::setHostAndPort(const String&)
 {
+    detach(m_urlImpl);
     // FIXME: Add WTFURL Implementation.
 }
 
 void KURL::setUser(const String&)
 {
+    detach(m_urlImpl);
     // FIXME: Add WTFURL Implementation.
 }
 
 void KURL::setPass(const String&)
 {
+    detach(m_urlImpl);
     // FIXME: Add WTFURL Implementation.
 }
 
 void KURL::setPath(const String&)
 {
+    detach(m_urlImpl);
     // FIXME: Add WTFURL Implementation.
 }
 
 void KURL::setQuery(const String&)
 {
+    detach(m_urlImpl);
     // FIXME: Add WTFURL Implementation.
 }
 
 void KURL::setFragmentIdentifier(const String&)
 {
+    detach(m_urlImpl);
     // FIXME: Add WTFURL Implementation.
 }
 
 void KURL::removeFragmentIdentifier()
 {
+    detach(m_urlImpl);
     // FIXME: Add WTFURL Implementation.
 }
 
@@ -280,10 +344,14 @@
     return false;
 }
 
-bool protocolHostAndPortAreEqual(const KURL&, const KURL&)
+bool protocolHostAndPortAreEqual(const KURL& a, const KURL& b)
 {
-    // FIXME: Add WTFURL Implementation.
-    return false;
+    if (!a.isValid() || !b.isValid())
+        return false;
+
+    return a.protocol() == b.protocol()
+           && a.host() == b.host()
+           && a.port() == b.port();
 }
 
 String encodeWithURLEscapeSequences(const String&)

Modified: trunk/Source/WebCore/platform/KURLWTFURLImpl.h (109670 => 109671)


--- trunk/Source/WebCore/platform/KURLWTFURLImpl.h	2012-03-04 03:26:27 UTC (rev 109670)
+++ trunk/Source/WebCore/platform/KURLWTFURLImpl.h	2012-03-04 03:49:19 UTC (rev 109671)
@@ -28,7 +28,9 @@
 
 #if USE(WTFURL)
 
+#include <wtf/PassRefPtr.h>
 #include <wtf/RefCounted.h>
+#include <wtf/RefPtr.h>
 #include <wtf/text/WTFString.h>
 #include <wtf/url/ParsedURL.h>
 
@@ -38,8 +40,18 @@
 public:
     WTF::ParsedURL m_parsedURL;
     String m_invalidUrlString;
+
+    PassRefPtr<KURLWTFURLImpl> copy() const;
 };
 
+inline PassRefPtr<KURLWTFURLImpl> KURLWTFURLImpl::copy() const
+{
+    RefPtr<KURLWTFURLImpl> clone = adoptRef(new KURLWTFURLImpl);
+    clone->m_parsedURL = m_parsedURL.isolatedCopy();
+    clone->m_invalidUrlString = m_invalidUrlString.isolatedCopy();
+    return clone.release();
+}
+
 } // namespace WebCore
 
 #endif // USE(WTFURL)
_______________________________________________
webkit-changes mailing list
[email protected]
http://lists.webkit.org/mailman/listinfo.cgi/webkit-changes

Reply via email to