[email protected] | 89f550b | 2011-06-08 18:34:03 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
rsleevi | 24f64dc2 | 2015-08-07 21:39:21 | [diff] [blame] | 5 | #ifndef COMPONENTS_URL_FORMATTER_URL_FIXER_H_ |
| 6 | #define COMPONENTS_URL_FORMATTER_URL_FIXER_H_ |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 7 | |
| 8 | #include <string> |
| 9 | |
[email protected] | f9b29436 | 2013-06-10 20:22:31 | [diff] [blame] | 10 | #include "base/strings/string16.h" |
[email protected] | 761fa470 | 2013-07-02 15:25:15 | [diff] [blame] | 11 | #include "url/gurl.h" |
[email protected] | 76e7da2 | 2010-06-18 22:44:49 | [diff] [blame] | 12 | |
[email protected] | a3ef483 | 2013-02-02 05:12:33 | [diff] [blame] | 13 | namespace base { |
| 14 | class FilePath; |
[email protected] | 71be2a6 | 2009-03-12 00:23:07 | [diff] [blame] | 15 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 16 | |
[email protected] | 0318f92 | 2014-04-22 00:09:23 | [diff] [blame] | 17 | namespace url { |
[email protected] | a3ef483 | 2013-02-02 05:12:33 | [diff] [blame] | 18 | struct Component; |
| 19 | struct Parsed; |
| 20 | } |
[email protected] | b1c33f8 | 2009-01-23 01:51:23 | [diff] [blame] | 21 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 22 | // This object is designed to convert various types of input into URLs that we |
| 23 | // know are valid. For example, user typing in the URL bar or command line |
[email protected] | 8f6e532 | 2014-08-11 08:06:08 | [diff] [blame] | 24 | // options. This is NOT the place for converting between different types of URLs |
| 25 | // or parsing them, see net_util.h for that. |
rsleevi | 24f64dc2 | 2015-08-07 21:39:21 | [diff] [blame] | 26 | namespace url_formatter { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 27 | |
[email protected] | 8f6e532 | 2014-08-11 08:06:08 | [diff] [blame] | 28 | // Segments the given text string into parts of a URL. This is most useful for |
| 29 | // schemes such as http, https, and ftp where |SegmentURL| will find many |
| 30 | // segments. Currently does not segment "file" schemes. |
| 31 | // Returns the canonicalized scheme, or the empty string when |text| is only |
| 32 | // whitespace. |
[email protected] | b4533450 | 2014-04-30 19:44:05 | [diff] [blame] | 33 | std::string SegmentURL(const std::string& text, url::Parsed* parts); |
| 34 | base::string16 SegmentURL(const base::string16& text, url::Parsed* parts); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 35 | |
[email protected] | 8f6e532 | 2014-08-11 08:06:08 | [diff] [blame] | 36 | // Converts |text| to a fixed-up URL and returns it. Attempts to make some |
| 37 | // "smart" adjustments to obviously-invalid input where possible. |
| 38 | // |text| may be an absolute path to a file, which will get converted to a |
| 39 | // "file:" URL. |
| 40 | // |
| 41 | // The result will be a "more" valid URL than the input. It may still not be |
| 42 | // valid, so check the return value's validity or use possibly_invalid_spec(). |
| 43 | // |
| 44 | // Schemes "about" and "chrome" are normalized to "chrome://", with slashes. |
| 45 | // "about:blank" is unaltered, as Webkit allows frames to access about:blank. |
| 46 | // Additionally, if a chrome URL does not have a valid host, as in "about:", the |
| 47 | // returned URL will have the host "version", as in "chrome://version". |
| 48 | // |
| 49 | // If |desired_tld| is non-empty, it represents the TLD the user wishes to |
| 50 | // append in the case of an incomplete domain. We check that this is not a file |
| 51 | // path and there does not appear to be a valid TLD already, then append |
| 52 | // |desired_tld| to the domain and prepend "www." (unless it, or a scheme, are |
| 53 | // already present.) This TLD should not have a leading '.' (use "com" instead |
| 54 | // of ".com"). |
| 55 | GURL FixupURL(const std::string& text, const std::string& desired_tld); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 56 | |
[email protected] | 8f6e532 | 2014-08-11 08:06:08 | [diff] [blame] | 57 | // Converts |text| to a fixed-up URL, allowing it to be a relative path on the |
| 58 | // local filesystem. Begin searching in |base_dir|; if empty, use the current |
| 59 | // working directory. If this resolves to a file on disk, convert it to a |
| 60 | // "file:" URL in |fixed_up_url|; otherwise, fall back to the behavior of |
| 61 | // FixupURL(). |
| 62 | // |
| 63 | // For "regular" input, even if it is possibly a file with a full path, you |
| 64 | // should use FixupURL() directly. This function should only be used when |
| 65 | // relative path handling is desired, as for command line processing. |
| 66 | GURL FixupRelativeFile(const base::FilePath& base_dir, |
| 67 | const base::FilePath& text); |
[email protected] | b1c33f8 | 2009-01-23 01:51:23 | [diff] [blame] | 68 | |
[email protected] | 8f6e532 | 2014-08-11 08:06:08 | [diff] [blame] | 69 | // Offsets the beginning index of |part| by |offset|, which is allowed to be |
| 70 | // negative. In some cases, the desired component does not exist at the given |
| 71 | // offset. For example, when converting from "http://foo" to "foo", the scheme |
| 72 | // component no longer exists. In such a case, the beginning index is set to 0. |
| 73 | // Does nothing if |part| is invalid. |
| 74 | void OffsetComponent(int offset, url::Component* part); |
[email protected] | d1e83b3 | 2010-12-22 00:34:35 | [diff] [blame] | 75 | |
[email protected] | 8f6e532 | 2014-08-11 08:06:08 | [diff] [blame] | 76 | // Returns true if |scheme1| is equivalent to |scheme2|. |
| 77 | // Generally this is true if the two schemes are actually identical, but it's |
| 78 | // also true when one scheme is "about" and the other "chrome". |
| 79 | bool IsEquivalentScheme(const std::string& scheme1, const std::string& scheme2); |
| 80 | |
| 81 | // For paths like ~, we use $HOME for the current user's home directory. |
| 82 | // For tests, we allow our idea of $HOME to be overriden by this variable. |
| 83 | extern const char* home_directory_override; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 84 | |
rsleevi | 24f64dc2 | 2015-08-07 21:39:21 | [diff] [blame] | 85 | } // namespace url_formatter |
[email protected] | 4f3b446 | 2013-07-27 19:20:18 | [diff] [blame] | 86 | |
rsleevi | 24f64dc2 | 2015-08-07 21:39:21 | [diff] [blame] | 87 | #endif // COMPONENTS_URL_FORMATTER_URL_FIXER_H_ |