KURLGooglePrivate.h revision 2fc2651226baac27029e38c9d6ef883fa32084db
1/*
2 * Copyright (c) 2008, 2009, 2011 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 KURLGooglePrivate_h
32#define KURLGooglePrivate_h
33
34#include <wtf/text/CString.h>
35
36#include <googleurl/src/url_parse.h>
37#include <googleurl/src/url_canon.h>
38
39namespace WebCore {
40
41    class KURL;
42    class TextEncoding;
43
44    // Wraps the internals related to using Google-URL as the backend for KURL.
45    // This maintains the state and has auxiliary functions so that we don't need
46    // to uglify KURL.h while allowing Google-URL to be evaluated.
47    class KURLGooglePrivate {
48    public:
49        KURLGooglePrivate();
50        KURLGooglePrivate(const url_parse::Parsed&, bool isValid);
51
52        // Initializes the object. This will call through the backend initializer
53        // below.
54        void init(const KURL& base, const String& relative,
55                  const TextEncoding* queryEncoding);
56
57        // Backend initializer. The query encoding parameters are optional and can
58        // be 0 (this implies UTF-8). This initializer requires that the object
59        // has just been created and the strings are null. Do not call on an
60        // already-constructed object.
61        template <typename CHAR>
62        void init(const KURL& base, const CHAR* rel, int relLength,
63                  const TextEncoding* queryEncoding);
64
65        // Does a deep copy to the given output object.
66        void copyTo(KURLGooglePrivate* dest) const;
67
68        // Returns the substring of the input identified by the given component.
69        String componentString(const url_parse::Component&) const;
70
71        // Replaces the given components, modifying the current URL. The current
72        // URL must be valid.
73        typedef url_canon::Replacements<url_parse::UTF16Char> Replacements;
74        void replaceComponents(const Replacements&);
75
76        // Setters for the data. Using the ASCII version when you know the
77        // data is ASCII will be slightly more efficient. The UTF-8 version
78        // will always be correct if the caller is unsure.
79        void setUtf8(const CString&);
80        void setAscii(const CString&);
81
82        // TODO(brettw) we can support an additional optimization.  Make this
83        // buffer support both optinal Strings and UTF-8 data. This way, we can use
84        // the optimization from the original KURL which uses = with the original
85        // string when canonicalization did not change it. This allows the strings
86        // to share a buffer internally, and saves a malloc.
87
88        // Getters for the data.
89        const CString& utf8String() const { return m_utf8; }
90        const String& string() const;
91
92        bool m_isValid;
93        bool m_protocolInHTTPFamily;
94        url_parse::Parsed m_parsed; // Indexes into the UTF-8 version of the string.
95
96    private:
97        void initProtocolInHTTPFamily();
98
99        CString m_utf8;
100
101        // Set to true when the caller set us using the ASCII setter. We can
102        // be more efficient when we know there is no UTF-8 to worry about.
103        // This flag is currently always correct, but should be changed to be a
104        // hint (see setUtf8).
105        bool m_utf8IsASCII;
106
107        mutable bool m_stringIsValid;
108        mutable String m_string;
109    };
110
111} // namespace WebCore
112
113#endif
114