| 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 | |
| 26 | #pragma once |
| 27 | |
| 28 | #include "APIObject.h" |
| 29 | #include "WebCoreArgumentCoders.h" |
| 30 | #include <wtf/Forward.h> |
| 31 | #include <wtf/URL.h> |
| 32 | #include <wtf/text/WTFString.h> |
| 33 | |
| 34 | namespace API { |
| 35 | |
| 36 | class URL : public ObjectImpl<Object::Type::URL> { |
| 37 | public: |
| 38 | static Ref<API::URL> create(const WTF::String& string) |
| 39 | { |
| 40 | return adoptRef(*new URL(string)); |
| 41 | } |
| 42 | |
| 43 | static Ref<API::URL> create(const API::URL* baseURL, const WTF::String& relativeURL) |
| 44 | { |
| 45 | ASSERT(baseURL); |
| 46 | baseURL->parseURLIfNecessary(); |
| 47 | auto absoluteURL = std::make_unique<WTF::URL>(*baseURL->m_parsedURL.get(), relativeURL); |
| 48 | const WTF::String& absoluteURLString = absoluteURL->string(); |
| 49 | |
| 50 | return adoptRef(*new API::URL(WTFMove(absoluteURL), absoluteURLString)); |
| 51 | } |
| 52 | |
| 53 | bool isNull() const { return m_string.isNull(); } |
| 54 | bool isEmpty() const { return m_string.isEmpty(); } |
| 55 | |
| 56 | const WTF::String& string() const { return m_string; } |
| 57 | |
| 58 | static bool equals(const API::URL& a, const API::URL& b) |
| 59 | { |
| 60 | return a.url() == b.url(); |
| 61 | } |
| 62 | |
| 63 | WTF::String host() const |
| 64 | { |
| 65 | parseURLIfNecessary(); |
| 66 | return m_parsedURL->isValid() ? m_parsedURL->host().toString() : WTF::String(); |
| 67 | } |
| 68 | |
| 69 | WTF::String protocol() const |
| 70 | { |
| 71 | parseURLIfNecessary(); |
| 72 | return m_parsedURL->isValid() ? m_parsedURL->protocol().toString() : WTF::String(); |
| 73 | } |
| 74 | |
| 75 | WTF::String path() const |
| 76 | { |
| 77 | parseURLIfNecessary(); |
| 78 | return m_parsedURL->isValid() ? m_parsedURL->path() : WTF::String(); |
| 79 | } |
| 80 | |
| 81 | WTF::String lastPathComponent() const |
| 82 | { |
| 83 | parseURLIfNecessary(); |
| 84 | return m_parsedURL->isValid() ? m_parsedURL->lastPathComponent() : WTF::String(); |
| 85 | } |
| 86 | |
| 87 | void encode(IPC::Encoder& encoder) const |
| 88 | { |
| 89 | encoder << m_string; |
| 90 | } |
| 91 | |
| 92 | static bool decode(IPC::Decoder& decoder, RefPtr<Object>& result) |
| 93 | { |
| 94 | WTF::String string; |
| 95 | if (!decoder.decode(string)) |
| 96 | return false; |
| 97 | |
| 98 | result = create(string); |
| 99 | return true; |
| 100 | } |
| 101 | |
| 102 | private: |
| 103 | URL(const WTF::String& string) |
| 104 | : m_string(string) |
| 105 | { |
| 106 | } |
| 107 | |
| 108 | URL(std::unique_ptr<WTF::URL> parsedURL, const WTF::String& string) |
| 109 | : m_string(string) |
| 110 | , m_parsedURL(WTFMove(parsedURL)) |
| 111 | { |
| 112 | } |
| 113 | |
| 114 | const WTF::URL& url() const |
| 115 | { |
| 116 | parseURLIfNecessary(); |
| 117 | return *m_parsedURL; |
| 118 | } |
| 119 | |
| 120 | void parseURLIfNecessary() const |
| 121 | { |
| 122 | if (m_parsedURL) |
| 123 | return; |
| 124 | m_parsedURL = std::make_unique<WTF::URL>(WTF::URL(), m_string); |
| 125 | } |
| 126 | |
| 127 | WTF::String m_string; |
| 128 | mutable std::unique_ptr<WTF::URL> m_parsedURL; |
| 129 | }; |
| 130 | |
| 131 | } // namespace WebKit |
| 132 | |