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