1// Copyright (c) 2010 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 CHROME_BROWSER_NET_URL_FIXER_UPPER_H_
6#define CHROME_BROWSER_NET_URL_FIXER_UPPER_H_
7#pragma once
8
9#include <string>
10
11#include "base/string16.h"
12#include "googleurl/src/gurl.h"
13
14namespace url_parse {
15  struct Component;
16  struct Parsed;
17}
18
19class FilePath;
20
21// This object is designed to convert various types of input into URLs that we
22// know are valid. For example, user typing in the URL bar or command line
23// options. This is NOT the place for converting between different types of
24// URLs or parsing them, see net_util.h for that.
25namespace URLFixerUpper {
26
27  // Segments the given text string into parts of a URL.  This is most useful
28  // for schemes such as http, https, and ftp where |SegmentURL| will find many
29  // segments.  Currently does not segment "file" schemes.
30  // Returns the canonicalized scheme, or the empty string when |text| is only
31  // whitespace.
32  std::string SegmentURL(const std::string& text, url_parse::Parsed* parts);
33  string16 SegmentURL(const string16& text, url_parse::Parsed* parts);
34
35  // Converts |text| to a fixed-up URL and returns it. Attempts to make
36  // some "smart" adjustments to obviously-invalid input where possible.
37  // |text| may be an absolute path to a file, which will get converted to a
38  // "file:" URL.
39  //
40  // The result will be a "more" valid URL than the input. It may still not
41  // be valid, so check the return value's validity or use
42  // possibly_invalid_spec().
43  //
44  // If |desired_tld| is non-empty, it represents the TLD the user wishes to
45  // append in the case of an incomplete domain.  We check that this is not a
46  // file path and there does not appear to be a valid TLD already, then append
47  // |desired_tld| to the domain and prepend "www." (unless it, or a scheme,
48  // are already present.)  This TLD should not have a leading '.' (use "com"
49  // instead of ".com").
50  GURL FixupURL(const std::string& text, const std::string& desired_tld);
51
52  // Converts |text| to a fixed-up URL, allowing it to be a relative path on
53  // the local filesystem.  Begin searching in |base_dir|; if empty, use the
54  // current working directory.  If this resolves to a file on disk, convert it
55  // to a "file:" URL in |fixed_up_url|; otherwise, fall back to the behavior
56  // of FixupURL().
57  //
58  // For "regular" input, even if it is possibly a file with a full path, you
59  // should use FixupURL() directly.  This function should only be used when
60  // relative path handling is desired, as for command line processing.
61  GURL FixupRelativeFile(const FilePath& base_dir, const FilePath& text);
62
63  // Offsets the beginning index of |part| by |offset|, which is allowed to be
64  // negative.  In some cases, the desired component does not exist at the given
65  // offset.  For example, when converting from "http://foo" to "foo", the
66  // scheme component no longer exists.  In such a case, the beginning index is
67  // set to 0.
68  // Does nothing if |part| is invalid.
69  void OffsetComponent(int offset, url_parse::Component* part);
70
71  // For paths like ~, we use $HOME for the current user's home
72  // directory.  For tests, we allow our idea of $HOME to be overriden
73  // by this variable.
74  extern const char* home_directory_override;
75};
76
77#endif  // CHROME_BROWSER_NET_URL_FIXER_UPPER_H_
78