WebURL.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * Copyright (C) 2010 Apple Inc. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. * 1. Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
  14. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  15. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  16. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
  17. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  18. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  19. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  20. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  21. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  22. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  23. * THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #ifndef WebURL_h
  26. #define WebURL_h
  27. #include "APIObject.h"
  28. #include <WebCore/KURL.h>
  29. #include <wtf/OwnPtr.h>
  30. #include <wtf/PassOwnPtr.h>
  31. #include <wtf/PassRefPtr.h>
  32. #include <wtf/text/WTFString.h>
  33. namespace WebKit {
  34. // WebURL - A URL type suitable for vending to an API.
  35. class WebURL : public TypedAPIObject<APIObject::TypeURL> {
  36. public:
  37. static PassRefPtr<WebURL> create(const String& string)
  38. {
  39. return adoptRef(new WebURL(string));
  40. }
  41. static PassRefPtr<WebURL> create(const WebURL* baseURL, const String& relativeURL)
  42. {
  43. using WebCore::KURL;
  44. ASSERT(baseURL);
  45. baseURL->parseURLIfNecessary();
  46. KURL* absoluteURL = new KURL(*baseURL->m_parsedURL.get(), relativeURL);
  47. return adoptRef(new WebURL(adoptPtr(absoluteURL), absoluteURL->string()));
  48. }
  49. bool isNull() const { return m_string.isNull(); }
  50. bool isEmpty() const { return m_string.isEmpty(); }
  51. const String& string() const { return m_string; }
  52. String host() const
  53. {
  54. parseURLIfNecessary();
  55. return m_parsedURL->isValid() ? m_parsedURL->host() : String();
  56. }
  57. String protocol() const
  58. {
  59. parseURLIfNecessary();
  60. return m_parsedURL->isValid() ? m_parsedURL->protocol() : String();
  61. }
  62. String path() const
  63. {
  64. parseURLIfNecessary();
  65. return m_parsedURL->isValid() ? m_parsedURL->path() : String();
  66. }
  67. String lastPathComponent() const
  68. {
  69. parseURLIfNecessary();
  70. return m_parsedURL->isValid() ? m_parsedURL->lastPathComponent() : String();
  71. }
  72. private:
  73. WebURL(const String& string)
  74. : m_string(string)
  75. {
  76. }
  77. WebURL(PassOwnPtr<WebCore::KURL> parsedURL, const String& string)
  78. : m_string(string)
  79. , m_parsedURL(parsedURL)
  80. {
  81. }
  82. void parseURLIfNecessary() const
  83. {
  84. if (m_parsedURL)
  85. return;
  86. m_parsedURL = WTF::adoptPtr(new WebCore::KURL(WebCore::KURL(), m_string));
  87. }
  88. String m_string;
  89. mutable OwnPtr<WebCore::KURL> m_parsedURL;
  90. };
  91. } // namespace WebKit
  92. #endif // WebURL_h