Add UMA stats for ExtensionCache
[chromium-blink-merge.git] / url / url_util.h
blobeff386cf46a9df30f26c04cbf20174c80d061b2f
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef URL_URL_UTIL_H_
6 #define URL_URL_UTIL_H_
8 #include <string>
10 #include "base/strings/string16.h"
11 #include "url/url_canon.h"
12 #include "url/url_export.h"
13 #include "url/url_parse.h"
15 namespace url_util {
17 // Init ------------------------------------------------------------------------
19 // Initialization is NOT required, it will be implicitly initialized when first
20 // used. However, this implicit initialization is NOT threadsafe. If you are
21 // using this library in a threaded environment and don't have a consistent
22 // "first call" (an example might be calling "AddStandardScheme" with your
23 // special application-specific schemes) then you will want to call initialize
24 // before spawning any threads.
26 // It is OK to call this function more than once, subsequent calls will simply
27 // "noop", unless Shutdown() was called in the mean time. This will also be a
28 // "noop" if other calls to the library have forced an initialization
29 // beforehand.
30 URL_EXPORT void Initialize();
32 // Cleanup is not required, except some strings may leak. For most user
33 // applications, this is fine. If you're using it in a library that may get
34 // loaded and unloaded, you'll want to unload to properly clean up your
35 // library.
36 URL_EXPORT void Shutdown();
38 // Schemes --------------------------------------------------------------------
40 // Adds an application-defined scheme to the internal list of "standard" URL
41 // schemes. This function is not threadsafe and can not be called concurrently
42 // with any other url_util function. It will assert if the list of standard
43 // schemes has been locked (see LockStandardSchemes).
44 URL_EXPORT void AddStandardScheme(const char* new_scheme);
46 // Sets a flag to prevent future calls to AddStandardScheme from succeeding.
48 // This is designed to help prevent errors for multithreaded applications.
49 // Normal usage would be to call AddStandardScheme for your custom schemes at
50 // the beginning of program initialization, and then LockStandardSchemes. This
51 // prevents future callers from mistakenly calling AddStandardScheme when the
52 // program is running with multiple threads, where such usage would be
53 // dangerous.
55 // We could have had AddStandardScheme use a lock instead, but that would add
56 // some platform-specific dependencies we don't otherwise have now, and is
57 // overkill considering the normal usage is so simple.
58 URL_EXPORT void LockStandardSchemes();
60 // Locates the scheme in the given string and places it into |found_scheme|,
61 // which may be NULL to indicate the caller does not care about the range.
63 // Returns whether the given |compare| scheme matches the scheme found in the
64 // input (if any). The |compare| scheme must be a valid canonical scheme or
65 // the result of the comparison is undefined.
66 URL_EXPORT bool FindAndCompareScheme(const char* str,
67 int str_len,
68 const char* compare,
69 url_parse::Component* found_scheme);
70 URL_EXPORT bool FindAndCompareScheme(const base::char16* str,
71 int str_len,
72 const char* compare,
73 url_parse::Component* found_scheme);
74 inline bool FindAndCompareScheme(const std::string& str,
75 const char* compare,
76 url_parse::Component* found_scheme) {
77 return FindAndCompareScheme(str.data(), static_cast<int>(str.size()),
78 compare, found_scheme);
80 inline bool FindAndCompareScheme(const base::string16& str,
81 const char* compare,
82 url_parse::Component* found_scheme) {
83 return FindAndCompareScheme(str.data(), static_cast<int>(str.size()),
84 compare, found_scheme);
87 // Returns true if the given string represents a standard URL. This means that
88 // either the scheme is in the list of known standard schemes.
89 URL_EXPORT bool IsStandard(const char* spec,
90 const url_parse::Component& scheme);
91 URL_EXPORT bool IsStandard(const base::char16* spec,
92 const url_parse::Component& scheme);
94 // TODO(brettw) remove this. This is a temporary compatibility hack to avoid
95 // breaking the WebKit build when this version is synced via Chrome.
96 inline bool IsStandard(const char* spec, int spec_len,
97 const url_parse::Component& scheme) {
98 return IsStandard(spec, scheme);
101 // URL library wrappers -------------------------------------------------------
103 // Parses the given spec according to the extracted scheme type. Normal users
104 // should use the URL object, although this may be useful if performance is
105 // critical and you don't want to do the heap allocation for the std::string.
107 // As with the url_canon::Canonicalize* functions, the charset converter can
108 // be NULL to use UTF-8 (it will be faster in this case).
110 // Returns true if a valid URL was produced, false if not. On failure, the
111 // output and parsed structures will still be filled and will be consistent,
112 // but they will not represent a loadable URL.
113 URL_EXPORT bool Canonicalize(const char* spec,
114 int spec_len,
115 bool trim_path_end,
116 url_canon::CharsetConverter* charset_converter,
117 url_canon::CanonOutput* output,
118 url_parse::Parsed* output_parsed);
119 URL_EXPORT bool Canonicalize(const base::char16* spec,
120 int spec_len,
121 bool trim_path_end,
122 url_canon::CharsetConverter* charset_converter,
123 url_canon::CanonOutput* output,
124 url_parse::Parsed* output_parsed);
126 // Resolves a potentially relative URL relative to the given parsed base URL.
127 // The base MUST be valid. The resulting canonical URL and parsed information
128 // will be placed in to the given out variables.
130 // The relative need not be relative. If we discover that it's absolute, this
131 // will produce a canonical version of that URL. See Canonicalize() for more
132 // about the charset_converter.
134 // Returns true if the output is valid, false if the input could not produce
135 // a valid URL.
136 URL_EXPORT bool ResolveRelative(const char* base_spec,
137 int base_spec_len,
138 const url_parse::Parsed& base_parsed,
139 const char* relative,
140 int relative_length,
141 url_canon::CharsetConverter* charset_converter,
142 url_canon::CanonOutput* output,
143 url_parse::Parsed* output_parsed);
144 URL_EXPORT bool ResolveRelative(const char* base_spec,
145 int base_spec_len,
146 const url_parse::Parsed& base_parsed,
147 const base::char16* relative,
148 int relative_length,
149 url_canon::CharsetConverter* charset_converter,
150 url_canon::CanonOutput* output,
151 url_parse::Parsed* output_parsed);
153 // Replaces components in the given VALID input url. The new canonical URL info
154 // is written to output and out_parsed.
156 // Returns true if the resulting URL is valid.
157 URL_EXPORT bool ReplaceComponents(
158 const char* spec,
159 int spec_len,
160 const url_parse::Parsed& parsed,
161 const url_canon::Replacements<char>& replacements,
162 url_canon::CharsetConverter* charset_converter,
163 url_canon::CanonOutput* output,
164 url_parse::Parsed* out_parsed);
165 URL_EXPORT bool ReplaceComponents(
166 const char* spec,
167 int spec_len,
168 const url_parse::Parsed& parsed,
169 const url_canon::Replacements<base::char16>& replacements,
170 url_canon::CharsetConverter* charset_converter,
171 url_canon::CanonOutput* output,
172 url_parse::Parsed* out_parsed);
174 // String helper functions ----------------------------------------------------
176 // Compare the lower-case form of the given string against the given ASCII
177 // string. This is useful for doing checking if an input string matches some
178 // token, and it is optimized to avoid intermediate string copies.
180 // The versions of this function that don't take a b_end assume that the b
181 // string is NULL terminated.
182 URL_EXPORT bool LowerCaseEqualsASCII(const char* a_begin,
183 const char* a_end,
184 const char* b);
185 URL_EXPORT bool LowerCaseEqualsASCII(const char* a_begin,
186 const char* a_end,
187 const char* b_begin,
188 const char* b_end);
189 URL_EXPORT bool LowerCaseEqualsASCII(const base::char16* a_begin,
190 const base::char16* a_end,
191 const char* b);
193 // Unescapes the given string using URL escaping rules.
194 URL_EXPORT void DecodeURLEscapeSequences(const char* input, int length,
195 url_canon::CanonOutputW* output);
197 // Escapes the given string as defined by the JS method encodeURIComponent. See
198 // https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/encodeURIComponent
199 URL_EXPORT void EncodeURIComponent(const char* input, int length,
200 url_canon::CanonOutput* output);
203 } // namespace url_util
205 #endif // URL_URL_UTIL_H_