193ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)/*
293ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles) * Copyright (C) 2009 Google Inc. All rights reserved.
393ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles) *
493ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles) * Redistribution and use in source and binary forms, with or without
593ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles) * modification, are permitted provided that the following conditions are
693ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles) * met:
793ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles) *
893ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles) *     * Redistributions of source code must retain the above copyright
993ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles) * notice, this list of conditions and the following disclaimer.
1093ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles) *     * Redistributions in binary form must reproduce the above
1193ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles) * copyright notice, this list of conditions and the following disclaimer
1293ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles) * in the documentation and/or other materials provided with the
1393ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles) * distribution.
1493ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles) *     * Neither the name of Google Inc. nor the names of its
1593ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles) * contributors may be used to endorse or promote products derived from
1693ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles) * this software without specific prior written permission.
1793ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles) *
1893ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles) * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1993ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles) * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2093ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles) * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
2193ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles) * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2293ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles) * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2393ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles) * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2493ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles) * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2593ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles) * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2693ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles) * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2793ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles) * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2893ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles) * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2993ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles) */
3093ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)
3193ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)#ifndef WebURL_h
3293ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)#define WebURL_h
3393ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)
3493ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)#include "WebCString.h"
35fff8884795cb540f87cf6e6d67b629519b00eb8bBen Murdoch#include "WebString.h"
36e1f1df5f01594c0e62e751e4b46e779b85c2faa5Torne (Richard Coles)#include <url/third_party/mozilla/url_parse.h>
3793ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)
38e38fbeeb576b5094e34e038ab88d9d6a5c5c2214Torne (Richard Coles)#if !INSIDE_BLINK
39521d96ec04ace82590870fb04353ec4f82bb150fTorne (Richard Coles)#include <url/gurl.h>
4093ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)#endif
4193ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)
4251b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)namespace blink {
4393ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)
44e38fbeeb576b5094e34e038ab88d9d6a5c5c2214Torne (Richard Coles)class KURL;
45e38fbeeb576b5094e34e038ab88d9d6a5c5c2214Torne (Richard Coles)
4693ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)class WebURL {
4793ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)public:
4893ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)    ~WebURL()
4993ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)    {
5093ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)    }
5193ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)
52fff8884795cb540f87cf6e6d67b629519b00eb8bBen Murdoch    WebURL()
53fff8884795cb540f87cf6e6d67b629519b00eb8bBen Murdoch        : m_isValid(false)
5493ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)    {
5593ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)    }
5693ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)
57fff8884795cb540f87cf6e6d67b629519b00eb8bBen Murdoch    WebURL(const WebURL& url)
58fff8884795cb540f87cf6e6d67b629519b00eb8bBen Murdoch        : m_string(url.m_string)
59fff8884795cb540f87cf6e6d67b629519b00eb8bBen Murdoch        , m_parsed(url.m_parsed)
60fff8884795cb540f87cf6e6d67b629519b00eb8bBen Murdoch        , m_isValid(url.m_isValid)
6193ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)    {
6293ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)    }
6393ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)
64fff8884795cb540f87cf6e6d67b629519b00eb8bBen Murdoch    WebURL& operator=(const WebURL& url)
6593ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)    {
66fff8884795cb540f87cf6e6d67b629519b00eb8bBen Murdoch        m_string = url.m_string;
67fff8884795cb540f87cf6e6d67b629519b00eb8bBen Murdoch        m_parsed = url.m_parsed;
68fff8884795cb540f87cf6e6d67b629519b00eb8bBen Murdoch        m_isValid = url.m_isValid;
6993ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)        return *this;
7093ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)    }
7193ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)
72fff8884795cb540f87cf6e6d67b629519b00eb8bBen Murdoch    // FIXME: Remove this API.
73fff8884795cb540f87cf6e6d67b629519b00eb8bBen Murdoch    WebCString spec() const
7493ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)    {
75fff8884795cb540f87cf6e6d67b629519b00eb8bBen Murdoch        std::string spec = m_string.utf8();
76fff8884795cb540f87cf6e6d67b629519b00eb8bBen Murdoch        return WebCString(spec.data(), spec.length());
7793ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)    }
7893ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)
79fff8884795cb540f87cf6e6d67b629519b00eb8bBen Murdoch    const WebString& string() const
8093ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)    {
81fff8884795cb540f87cf6e6d67b629519b00eb8bBen Murdoch        return m_string;
8293ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)    }
8393ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)
8410f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch    const url::Parsed& parsed() const
8593ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)    {
8693ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)        return m_parsed;
8793ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)    }
8893ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)
8993ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)    bool isValid() const
9093ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)    {
9193ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)        return m_isValid;
9293ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)    }
9393ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)
9493ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)    bool isEmpty() const
9593ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)    {
96fff8884795cb540f87cf6e6d67b629519b00eb8bBen Murdoch        return m_string.isEmpty();
9793ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)    }
9893ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)
9993ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)    bool isNull() const
10093ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)    {
101fff8884795cb540f87cf6e6d67b629519b00eb8bBen Murdoch        return m_string.isEmpty();
10293ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)    }
10393ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)
1041e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)#if INSIDE_BLINK
105e38fbeeb576b5094e34e038ab88d9d6a5c5c2214Torne (Richard Coles)    BLINK_PLATFORM_EXPORT WebURL(const KURL&);
106e38fbeeb576b5094e34e038ab88d9d6a5c5c2214Torne (Richard Coles)    BLINK_PLATFORM_EXPORT WebURL& operator=(const KURL&);
107e38fbeeb576b5094e34e038ab88d9d6a5c5c2214Torne (Richard Coles)    BLINK_PLATFORM_EXPORT operator KURL() const;
10893ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)#else
109fff8884795cb540f87cf6e6d67b629519b00eb8bBen Murdoch    WebURL(const GURL& url)
110fff8884795cb540f87cf6e6d67b629519b00eb8bBen Murdoch        : m_string(WebString::fromUTF8(url.possibly_invalid_spec()))
111fff8884795cb540f87cf6e6d67b629519b00eb8bBen Murdoch        , m_parsed(url.parsed_for_possibly_invalid_spec())
112fff8884795cb540f87cf6e6d67b629519b00eb8bBen Murdoch        , m_isValid(url.is_valid())
11393ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)    {
11493ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)    }
11593ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)
116fff8884795cb540f87cf6e6d67b629519b00eb8bBen Murdoch    WebURL& operator=(const GURL& url)
11793ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)    {
118fff8884795cb540f87cf6e6d67b629519b00eb8bBen Murdoch        m_string = WebString::fromUTF8(url.possibly_invalid_spec());
119fff8884795cb540f87cf6e6d67b629519b00eb8bBen Murdoch        m_parsed = url.parsed_for_possibly_invalid_spec();
120fff8884795cb540f87cf6e6d67b629519b00eb8bBen Murdoch        m_isValid = url.is_valid();
12193ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)        return *this;
12293ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)    }
12393ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)
12493ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)    operator GURL() const
12593ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)    {
126fff8884795cb540f87cf6e6d67b629519b00eb8bBen Murdoch        return isNull() ? GURL() : GURL(m_string.utf8(), m_parsed, m_isValid);
12793ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)    }
12893ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)#endif
12993ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)
13093ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)private:
131fff8884795cb540f87cf6e6d67b629519b00eb8bBen Murdoch    WebString m_string;
13210f88d5669dbd969c059d61ba09fa37dd72ac559Ben Murdoch    url::Parsed m_parsed;
13393ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)    bool m_isValid;
13493ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)};
13593ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)
13693ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)inline bool operator==(const WebURL& a, const WebURL& b)
13793ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles){
138fff8884795cb540f87cf6e6d67b629519b00eb8bBen Murdoch    return a.string().equals(b.string());
13993ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)}
14093ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)
14193ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)inline bool operator!=(const WebURL& a, const WebURL& b)
14293ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles){
14393ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)    return !(a == b);
14493ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)}
14593ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)
14651b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)} // namespace blink
14793ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)
14893ac45cfc74041c8ae536ce58a9534d46db2024eTorne (Richard Coles)#endif
149