Inject OmniboxClient instance into Omnibox core code
[chromium-blink-merge.git] / url / scheme_host_port.cc
blobcb2d5cc5b5a379d9161769d0dd8de052ad49c1af
1 // Copyright 2015 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.
5 #include "url/scheme_host_port.h"
7 #include <string.h>
9 #include "base/logging.h"
10 #include "base/strings/string_number_conversions.h"
11 #include "url/gurl.h"
12 #include "url/url_canon.h"
13 #include "url/url_canon_stdstring.h"
14 #include "url/url_constants.h"
15 #include "url/url_util.h"
17 namespace url {
19 SchemeHostPort::SchemeHostPort() : port_(0) {
22 SchemeHostPort::SchemeHostPort(base::StringPiece scheme,
23 base::StringPiece host,
24 uint16 port)
25 : scheme_(scheme.data(), scheme.length()),
26 host_(host.data(), host.length()),
27 port_(port) {
28 #if DCHECK_IS_ON()
29 DCHECK(url::IsStandard(scheme.data(),
30 url::Component(0, static_cast<int>(scheme.length()))));
32 // Try to canonicalize the host (copy/pasted from net/base. :( ).
33 const url::Component raw_host_component(0, static_cast<int>(host.length()));
34 std::string canon_host;
35 url::StdStringCanonOutput canon_host_output(&canon_host);
36 url::CanonHostInfo host_info;
37 url::CanonicalizeHostVerbose(host.data(), raw_host_component,
38 &canon_host_output, &host_info);
40 if (host_info.out_host.is_nonempty() &&
41 host_info.family != url::CanonHostInfo::BROKEN) {
42 // Success! Assert that there's no extra garbage.
43 canon_host_output.Complete();
44 DCHECK_EQ(host_info.out_host.len, static_cast<int>(canon_host.length()));
45 } else {
46 // Empty host, or canonicalization failed.
47 canon_host.clear();
49 DCHECK_EQ(host, canon_host);
51 DCHECK(scheme == kFileScheme ? port == 0 : port != 0);
52 DCHECK(!host.empty() || port == 0);
53 #endif
56 SchemeHostPort::SchemeHostPort(const GURL& url) : port_(0) {
57 if (!url.is_valid() || !url.IsStandard())
58 return;
60 // These schemes do not follow the generic URL syntax, so we treat them as
61 // invalid (scheme, host, port) tuples (even though such URLs' _Origin_ might
62 // have a (scheme, host, port) tuple, they themselves do not).
63 if (url.SchemeIsBlob() || url.SchemeIsFileSystem())
64 return;
66 scheme_ = url.scheme();
67 host_ = url.host();
68 port_ = url.EffectiveIntPort() == url::PORT_UNSPECIFIED
69 ? 0
70 : url.EffectiveIntPort();
73 SchemeHostPort::~SchemeHostPort() {
76 bool SchemeHostPort::IsInvalid() const {
77 return scheme_.empty() && host_.empty() && !port_;
80 std::string SchemeHostPort::Serialize() const {
81 std::string result;
82 if (IsInvalid())
83 return result;
85 bool is_default_port =
86 port_ == url::DefaultPortForScheme(scheme_.data(),
87 static_cast<int>(scheme_.length()));
89 result.append(scheme_);
90 result.append(kStandardSchemeSeparator);
91 result.append(host_);
93 if (scheme_ != kFileScheme && !is_default_port) {
94 result.push_back(':');
95 result.append(base::IntToString(port_));
98 return result;
101 bool SchemeHostPort::Equals(const SchemeHostPort& other) const {
102 return port_ == other.port() && scheme_ == other.scheme() &&
103 host_ == other.host();
106 bool SchemeHostPort::operator<(const SchemeHostPort& other) const {
107 if (port_ != other.port_)
108 return port_ < other.port_;
109 if (scheme_ != other.scheme_)
110 return scheme_ < other.scheme_;
111 if (host_ != other.host_)
112 return host_ < other.host_;
113 return false;
116 } // namespace url