1d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved. 2d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 3d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)// found in the LICENSE file. 4d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) 5d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)#ifndef CHROME_BROWSER_HISTORY_URL_UTILS_H_ 6d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)#define CHROME_BROWSER_HISTORY_URL_UTILS_H_ 7d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) 8d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)#include <string> 9d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) 10d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)#include "chrome/browser/history/history_types.h" 11d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) 12d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)namespace history { 13d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) 14d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)// CanonicalURLStringCompare performs lexicographical comparison of two strings 15d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)// that represent valid URLs, so that if the pre-path (scheme, host, and port) 16d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)// parts are equal, then the path parts are compared by treating path components 17d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)// (delimited by "/") as separate tokens that form units of comparison. 18d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)// For example, let us compare |s1| and |s2|, with 19d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)// |s1| = "http://www.google.com:80/base/test/ab/cd?query/stuff" 20d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)// |s2| = "http://www.google.com:80/base/test-case/yz#ref/stuff" 21d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)// The pre-path parts "http://www.google.com:80/" match. We treat the paths as 22d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)// |s1| => ["base", "test", "ab", "cd"] 23d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)// |s2| => ["base", "test-case", "yz"] 24d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)// Components 1 "base" are identical. Components 2 yield "test" < "test-case", 25d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)// so we consider |s1| < |s2|, and return true. Note that naive string 26d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)// comparison would yield the opposite (|s1| > |s2|), since '/' > '-' in ASCII. 27d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)// Note that path can be terminated by "?query" or "#ref". The post-path parts 28d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)// are compared in an arbitrary (but consistent) way. 29d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)bool CanonicalURLStringCompare(const std::string& s1, const std::string& s2); 30d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) 314e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)// Returns whether |url1| and |url2| have the same scheme, host, and port. 324e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)bool HaveSameSchemeHostAndPort(const GURL&url1, const GURL& url2); 334e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles) 344e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)// Treats |path1| and |path2| as lists of path components (e.g., ["a", "bb"] 354e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)// for "/a/bb"). Returns whether |path1|'s list is a prefix of |path2|'s list. 364e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)// This is used to define "URL prefix". Note that "test" does not count as a 374e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)// prefix of "testing", even though "test" is a (string) prefix of "testing". 384e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)bool IsPathPrefix(const std::string& p1, const std::string& p2); 39d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) 401e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)// Converts |url| from HTTP to HTTPS, and vice versa, then returns the result. 411e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)// If |url| is neither HTTP nor HTTPS, returns an empty URL. 421e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)GURL ToggleHTTPAndHTTPS(const GURL& url); 431e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) 44d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)} // namespace history 45d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) 46d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)#endif // CHROME_BROWSER_HISTORY_URL_UTILS_H_ 47