1 // Copyright (c) 2011 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.
4 
5 #ifndef COMPONENTS_URL_FORMATTER_URL_FIXER_H_
6 #define COMPONENTS_URL_FORMATTER_URL_FIXER_H_
7 
8 #include <string>
9 
10 #include "base/strings/string16.h"
11 #include "url/gurl.h"
12 
13 namespace base {
14 class FilePath;
15 }
16 
17 namespace url {
18 struct Component;
19 struct Parsed;
20 }
21 
22 // These methods process user typed input that is meant to be a URL - like user
23 // typing in the URL bar or command line switches. The output is NOT guaranteed
24 // to be a valid URL.
25 //
26 // This is NOT the place for converting between different types of URLs or
27 // parsing them, see net_util.h for that. These methods should only be used on
28 // user typed input, NOT untrusted strings sourced from the web or elsewhere.
29 namespace url_formatter {
30 
31 // Segments the given text string into parts of a URL. This is most useful for
32 // schemes such as http, https, and ftp where |SegmentURL| will find many
33 // segments. Currently does not segment "file" schemes.
34 // Returns the canonicalized scheme, or the empty string when |text| is only
35 // whitespace.
36 std::string SegmentURL(const std::string& text, url::Parsed* parts);
37 base::string16 SegmentURL(const base::string16& text, url::Parsed* parts);
38 
39 // Attempts to fix common problems in user-typed text, making some "smart"
40 // adjustments to obviously-invalid input where possible.
41 //
42 // The result can still be invalid, so check the return value's validity or
43 // use possibly_invalid_spec(). DO NOT USE this method on untrusted strings
44 // from the web or elsewhere. Only use this for user-typed input.
45 //
46 // If |text| may be an absolute path to a file, it will get converted to a
47 // "file:" URL.
48 //
49 // Schemes "about" and "chrome" are normalized to "chrome://", with slashes.
50 // "about:blank" is unaltered, as Webkit allows frames to access about:blank.
51 // Additionally, if a chrome URL does not have a valid host, as in "about:", the
52 // returned URL will have the host "version", as in "chrome://version".
53 //
54 // If |desired_tld| is non-empty, it represents the TLD the user wishes to
55 // append in the case of an incomplete domain. We check that this is not a file
56 // path and there does not appear to be a valid TLD already, then append
57 // |desired_tld| to the domain and prepend "www." (unless it, or a scheme, are
58 // already present.)  This TLD should not have a leading '.' (use "com" instead
59 // of ".com").
60 GURL FixupURL(const std::string& text, const std::string& desired_tld);
61 
62 // Converts |text| to a fixed-up URL, allowing it to be a relative path on the
63 // local filesystem. Begin searching in |base_dir|; if empty, use the current
64 // working directory. If this resolves to a file on disk, convert it to a
65 // "file:" URL in |fixed_up_url|; otherwise, fall back to the behavior of
66 // FixupURL().
67 //
68 // For "regular" input, even if it is possibly a file with a full path, you
69 // should use FixupURL() directly. This function should only be used when
70 // relative path handling is desired, as for command line processing.
71 GURL FixupRelativeFile(const base::FilePath& base_dir,
72                        const base::FilePath& text);
73 
74 // Offsets the beginning index of |part| by |offset|, which is allowed to be
75 // negative. In some cases, the desired component does not exist at the given
76 // offset. For example, when converting from "http://foo" to "foo", the scheme
77 // component no longer exists. In such a case, the beginning index is set to 0.
78 // Does nothing if |part| is invalid.
79 void OffsetComponent(int offset, url::Component* part);
80 
81 // Returns true if |scheme1| is equivalent to |scheme2|.
82 // Generally this is true if the two schemes are actually identical, but it's
83 // also true when one scheme is "about" and the other "chrome".
84 bool IsEquivalentScheme(const std::string& scheme1, const std::string& scheme2);
85 
86 // For paths like ~, we use $HOME for the current user's home directory.
87 // For tests, we allow our idea of $HOME to be overriden by this variable.
88 extern const char* home_directory_override;
89 
90 }  // namespace url_formatter
91 
92 #endif  // COMPONENTS_URL_FORMATTER_URL_FIXER_H_
93