Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / net / base / host_port_pair.h
blob9b1df5697b02186f34e3d1532814f73e33947ccb
1 // Copyright (c) 2012 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 #ifndef NET_BASE_HOST_PORT_PAIR_H_
6 #define NET_BASE_HOST_PORT_PAIR_H_
8 #include <string>
9 #include "base/basictypes.h"
10 #include "net/base/net_export.h"
12 class GURL;
14 namespace net {
16 class IPEndPoint;
18 class NET_EXPORT HostPortPair {
19 public:
20 HostPortPair();
21 // If |in_host| represents an IPv6 address, it should not bracket the address.
22 HostPortPair(const std::string& in_host, uint16_t in_port);
24 // Creates a HostPortPair for the origin of |url|.
25 static HostPortPair FromURL(const GURL& url);
27 // Creates a HostPortPair from an IPEndPoint.
28 static HostPortPair FromIPEndPoint(const IPEndPoint& ipe);
30 // Creates a HostPortPair from a string formatted in same manner as
31 // ToString().
32 static HostPortPair FromString(const std::string& str);
34 // TODO(willchan): Define a functor instead.
35 // Comparator function so this can be placed in a std::map.
36 bool operator<(const HostPortPair& other) const {
37 if (port_ != other.port_)
38 return port_ < other.port_;
39 return host_ < other.host_;
42 // Equality test of contents. (Probably another violation of style guide).
43 bool Equals(const HostPortPair& other) const {
44 return host_ == other.host_ && port_ == other.port_;
47 bool IsEmpty() const {
48 return host_.empty() && port_ == 0;
51 const std::string& host() const {
52 return host_;
55 uint16_t port() const { return port_; }
57 void set_host(const std::string& in_host) {
58 host_ = in_host;
61 void set_port(uint16_t in_port) { port_ = in_port; }
63 // ToString() will convert the HostPortPair to "host:port". If |host_| is an
64 // IPv6 literal, it will add brackets around |host_|.
65 std::string ToString() const;
67 // Returns |host_|, adding IPv6 brackets if needed.
68 std::string HostForURL() const;
70 private:
71 // If |host_| represents an IPv6 address, this string will not contain
72 // brackets around the address.
73 std::string host_;
74 uint16_t port_;
77 } // namespace net
79 #endif // NET_BASE_HOST_PORT_PAIR_H_