summaryrefslogtreecommitdiffstats
path: root/chrome/browser/net/url_fixer_upper.h
blob: f5b5327526dadbfb10bd40c561962c1366c0643f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_NET_URL_FIXER_UPPER_H_
#define CHROME_BROWSER_NET_URL_FIXER_UPPER_H_
#pragma once

#include <string>

#include "base/string16.h"
#include "googleurl/src/gurl.h"

namespace url_parse {
  struct Component;
  struct Parsed;
}

class FilePath;

// This object is designed to convert various types of input into URLs that we
// know are valid. For example, user typing in the URL bar or command line
// options. This is NOT the place for converting between different types of
// URLs or parsing them, see net_util.h for that.
namespace URLFixerUpper {

  // Segments the given text string into parts of a URL.  This is most useful
  // for schemes such as http, https, and ftp where |SegmentURL| will find many
  // segments.  Currently does not segment "file" schemes.
  // Returns the canonicalized scheme, or the empty string when |text| is only
  // whitespace.
  std::string SegmentURL(const std::string& text, url_parse::Parsed* parts);
  string16 SegmentURL(const string16& text, url_parse::Parsed* parts);

  // Converts |text| to a fixed-up URL and returns it. Attempts to make
  // some "smart" adjustments to obviously-invalid input where possible.
  // |text| may be an absolute path to a file, which will get converted to a
  // "file:" URL.
  //
  // The result will be a "more" valid URL than the input. It may still not
  // be valid, so check the return value's validity or use
  // possibly_invalid_spec().
  //
  // Schemes "about" and "chrome" are normalized to "chrome://", with slashes.
  // "about:blank" is unaltered, as Webkit allows frames to access about:blank.
  // Additionally, if a chrome URL does not have a valid host, as in "about:",
  // the returned URL will have the host "version", as in "chrome://version".
  //
  // If |desired_tld| is non-empty, it represents the TLD the user wishes to
  // append in the case of an incomplete domain.  We check that this is not a
  // file path and there does not appear to be a valid TLD already, then append
  // |desired_tld| to the domain and prepend "www." (unless it, or a scheme,
  // are already present.)  This TLD should not have a leading '.' (use "com"
  // instead of ".com").
  GURL FixupURL(const std::string& text, const std::string& desired_tld);

  // Converts |text| to a fixed-up URL, allowing it to be a relative path on
  // the local filesystem.  Begin searching in |base_dir|; if empty, use the
  // current working directory.  If this resolves to a file on disk, convert it
  // to a "file:" URL in |fixed_up_url|; otherwise, fall back to the behavior
  // of FixupURL().
  //
  // For "regular" input, even if it is possibly a file with a full path, you
  // should use FixupURL() directly.  This function should only be used when
  // relative path handling is desired, as for command line processing.
  GURL FixupRelativeFile(const FilePath& base_dir, const FilePath& text);

  // Offsets the beginning index of |part| by |offset|, which is allowed to be
  // negative.  In some cases, the desired component does not exist at the given
  // offset.  For example, when converting from "http://foo" to "foo", the
  // scheme component no longer exists.  In such a case, the beginning index is
  // set to 0.
  // Does nothing if |part| is invalid.
  void OffsetComponent(int offset, url_parse::Component* part);

  // For paths like ~, we use $HOME for the current user's home
  // directory.  For tests, we allow our idea of $HOME to be overriden
  // by this variable.
  extern const char* home_directory_override;
};

#endif  // CHROME_BROWSER_NET_URL_FIXER_UPPER_H_