1/*
2 * Copyright (C) 2009 Google 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 are
6 * met:
7 *
8 *     * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *     * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 *     * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31#ifndef WebURL_h
32#define WebURL_h
33
34#include "WebCString.h"
35#include "WebString.h"
36#include <url/third_party/mozilla/url_parse.h>
37
38#if !INSIDE_BLINK
39#include <url/gurl.h>
40#endif
41
42namespace blink {
43
44class KURL;
45
46class WebURL {
47public:
48    ~WebURL()
49    {
50    }
51
52    WebURL()
53        : m_isValid(false)
54    {
55    }
56
57    WebURL(const WebURL& url)
58        : m_string(url.m_string)
59        , m_parsed(url.m_parsed)
60        , m_isValid(url.m_isValid)
61    {
62    }
63
64    WebURL& operator=(const WebURL& url)
65    {
66        m_string = url.m_string;
67        m_parsed = url.m_parsed;
68        m_isValid = url.m_isValid;
69        return *this;
70    }
71
72    // FIXME: Remove this API.
73    WebCString spec() const
74    {
75        std::string spec = m_string.utf8();
76        return WebCString(spec.data(), spec.length());
77    }
78
79    const WebString& string() const
80    {
81        return m_string;
82    }
83
84    const url::Parsed& parsed() const
85    {
86        return m_parsed;
87    }
88
89    bool isValid() const
90    {
91        return m_isValid;
92    }
93
94    bool isEmpty() const
95    {
96        return m_string.isEmpty();
97    }
98
99    bool isNull() const
100    {
101        return m_string.isEmpty();
102    }
103
104#if INSIDE_BLINK
105    BLINK_PLATFORM_EXPORT WebURL(const KURL&);
106    BLINK_PLATFORM_EXPORT WebURL& operator=(const KURL&);
107    BLINK_PLATFORM_EXPORT operator KURL() const;
108#else
109    WebURL(const GURL& url)
110        : m_string(WebString::fromUTF8(url.possibly_invalid_spec()))
111        , m_parsed(url.parsed_for_possibly_invalid_spec())
112        , m_isValid(url.is_valid())
113    {
114    }
115
116    WebURL& operator=(const GURL& url)
117    {
118        m_string = WebString::fromUTF8(url.possibly_invalid_spec());
119        m_parsed = url.parsed_for_possibly_invalid_spec();
120        m_isValid = url.is_valid();
121        return *this;
122    }
123
124    operator GURL() const
125    {
126        return isNull() ? GURL() : GURL(m_string.utf8(), m_parsed, m_isValid);
127    }
128#endif
129
130private:
131    WebString m_string;
132    url::Parsed m_parsed;
133    bool m_isValid;
134};
135
136inline bool operator==(const WebURL& a, const WebURL& b)
137{
138    return a.string().equals(b.string());
139}
140
141inline bool operator!=(const WebURL& a, const WebURL& b)
142{
143    return !(a == b);
144}
145
146} // namespace blink
147
148#endif
149