Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2014 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. | |
| 4 | |
| 5 #include "chromeos/network/host_resolver_impl_chromeos.h" | |
| 6 | |
| 7 #include "base/message_loop/message_loop_proxy.h" | |
| 8 #include "base/values.h" | |
| 9 #include "chromeos/network/device_state.h" | |
| 10 #include "chromeos/network/network_handler.h" | |
| 11 #include "chromeos/network/network_state.h" | |
| 12 #include "chromeos/network/network_state_handler.h" | |
| 13 #include "chromeos/network/network_state_handler_observer.h" | |
| 14 #include "net/base/address_list.h" | |
| 15 #include "net/base/net_errors.h" | |
| 16 #include "net/base/net_util.h" | |
| 17 #include "third_party/cros_system_api/dbus/service_constants.h" | |
| 18 | |
| 19 namespace chromeos { | |
| 20 | |
| 21 // HostResolverImplChromeOS::NetworkStateHandlerObserver | |
| 22 // | |
| 23 // An instance of this class is created on the NetworkHandler (UI) thread and | |
| 24 // manages its own lifetime, destroying itself when NetworkStateHandlerObserver | |
| 25 // ::IsShuttingDown() gets called. | |
| 26 | |
| 27 class HostResolverImplChromeOS::NetworkObserver | |
| 28 : public chromeos::NetworkStateHandlerObserver { | |
| 29 public: | |
| 30 static void Create(const base::WeakPtr<HostResolverImplChromeOS>& owner, | |
| 31 scoped_refptr<base::MessageLoopProxy> owner_message_loop, | |
| 32 NetworkStateHandler* network_state_handler) { | |
| 33 new NetworkObserver(owner, owner_message_loop, network_state_handler); | |
| 34 } | |
| 35 | |
| 36 NetworkObserver(const base::WeakPtr<HostResolverImplChromeOS>& owner, | |
| 37 scoped_refptr<base::MessageLoopProxy> owner_message_loop, | |
| 38 NetworkStateHandler* network_state_handler) | |
| 39 : owner_(owner), | |
|
eroman
2014/06/16 22:37:48
nit: "owner" and "owner_message_loop" aren't the b
stevenjb
2014/06/16 23:03:53
Done. (owner -> resolver)
| |
| 40 owner_message_loop_(owner_message_loop), | |
| 41 network_state_handler_(network_state_handler), | |
| 42 weak_ptr_factory_owner_thread_(this) { | |
| 43 network_state_handler_->AddObserver(this, FROM_HERE); | |
| 44 DefaultNetworkChanged(network_state_handler_->DefaultNetwork()); | |
| 45 } | |
| 46 | |
| 47 private: | |
| 48 virtual ~NetworkObserver() { | |
| 49 network_state_handler_->RemoveObserver(this, FROM_HERE); | |
| 50 } | |
| 51 | |
| 52 // NetworkStateHandlerObserver | |
| 53 virtual void DefaultNetworkChanged(const NetworkState* network) OVERRIDE { | |
| 54 if (!network) { | |
| 55 DVLOG(2) << "DefaultNetworkChanged: No Network."; | |
| 56 CallOwnerSetIpAddress("", ""); | |
| 57 return; | |
| 58 } | |
| 59 std::string ipv4_address, ipv6_address; | |
| 60 const DeviceState* device_state = | |
| 61 network_state_handler_->GetDeviceState(network->device_path()); | |
| 62 if (!device_state) { | |
| 63 LOG(ERROR) << "DefaultNetworkChanged: Network missing device: " | |
| 64 << network->path(); | |
| 65 CallOwnerSetIpAddress("", ""); | |
| 66 return; | |
| 67 } | |
| 68 for (base::DictionaryValue::Iterator iter(device_state->ip_configs()); | |
| 69 !iter.IsAtEnd(); iter.Advance()) { | |
| 70 const base::DictionaryValue* ip_config; | |
| 71 if (!iter.value().GetAsDictionary(&ip_config)) { | |
| 72 LOG(ERROR) << "Badly formatted IPConfigs: " << network->path(); | |
| 73 continue; | |
| 74 } | |
| 75 std::string method, address; | |
| 76 if (ip_config->GetString(shill::kMethodProperty, &method) && | |
| 77 ip_config->GetString(shill::kAddressProperty, &address)) { | |
| 78 if (method == shill::kTypeIPv4 || method == shill::kTypeDHCP) | |
| 79 ipv4_address = address; | |
| 80 else if (method == shill::kTypeIPv6 || method == shill::kTypeDHCP6) | |
| 81 ipv6_address = address; | |
| 82 } else { | |
| 83 LOG(ERROR) << "DefaultNetworkChanged: IPConfigs missing properties: " | |
| 84 << network->path(); | |
| 85 } | |
| 86 } | |
| 87 DVLOG(2) << "DefaultNetworkChanged: " << network->name() | |
| 88 << " IPv4: " << ipv4_address << " IPv6: " << ipv6_address; | |
| 89 CallOwnerSetIpAddress(ipv4_address, ipv6_address); | |
| 90 } | |
| 91 | |
| 92 virtual void IsShuttingDown() OVERRIDE { | |
| 93 delete this; | |
| 94 } | |
| 95 | |
| 96 void CallOwnerSetIpAddress(const std::string& ipv4_address, | |
| 97 const std::string& ipv6_address) { | |
| 98 owner_message_loop_->PostTask( | |
| 99 FROM_HERE, | |
| 100 base::Bind(&NetworkObserver::SetIpAddressOnOwnerThread, | |
| 101 weak_ptr_factory_owner_thread_.GetWeakPtr(), | |
| 102 ipv4_address, ipv6_address)); | |
| 103 } | |
| 104 | |
| 105 void SetIpAddressOnOwnerThread(const std::string& ipv4_address, | |
| 106 const std::string& ipv6_address) { | |
| 107 if (owner_) | |
| 108 owner_->SetIPAddresses(ipv4_address, ipv6_address); | |
| 109 } | |
| 110 | |
| 111 base::WeakPtr<HostResolverImplChromeOS> owner_; | |
| 112 scoped_refptr<base::MessageLoopProxy> owner_message_loop_; | |
| 113 NetworkStateHandler* network_state_handler_; | |
| 114 base::WeakPtrFactory<NetworkObserver> weak_ptr_factory_owner_thread_; | |
| 115 | |
| 116 DISALLOW_COPY_AND_ASSIGN(NetworkObserver); | |
| 117 }; | |
| 118 | |
| 119 // HostResolverImplChromeOS | |
| 120 | |
| 121 HostResolverImplChromeOS::HostResolverImplChromeOS( | |
| 122 scoped_refptr<base::MessageLoopProxy> network_handler_message_loop, | |
| 123 NetworkStateHandler* network_state_handler, | |
| 124 const Options& options, | |
| 125 net::NetLog* net_log) | |
| 126 : HostResolverImpl(options, net_log), | |
| 127 network_handler_message_loop_(network_handler_message_loop), | |
| 128 weak_ptr_factory_(this) { | |
| 129 network_handler_message_loop->PostTask( | |
| 130 FROM_HERE, | |
| 131 base::Bind(&NetworkObserver::Create, | |
| 132 weak_ptr_factory_.GetWeakPtr(), | |
| 133 base::MessageLoopProxy::current(), | |
| 134 network_state_handler)); | |
| 135 } | |
| 136 | |
| 137 HostResolverImplChromeOS::~HostResolverImplChromeOS() { | |
| 138 } | |
| 139 | |
| 140 int HostResolverImplChromeOS::Resolve(const RequestInfo& info, | |
| 141 net::RequestPriority priority, | |
| 142 net::AddressList* addresses, | |
| 143 const net::CompletionCallback& callback, | |
| 144 RequestHandle* out_req, | |
| 145 const net::BoundNetLog& source_net_log) { | |
| 146 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 147 if (ResolveLocalIPAddress(info, addresses)) | |
| 148 return net::OK; | |
| 149 return net::HostResolverImpl::Resolve( | |
| 150 info, priority, addresses, callback, out_req, source_net_log); | |
| 151 } | |
| 152 | |
| 153 void HostResolverImplChromeOS::SetIPAddresses(const std::string& ipv4_address, | |
| 154 const std::string& ipv6_address) { | |
| 155 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 156 ipv4_address_ = ipv4_address; | |
| 157 ipv6_address_ = ipv6_address; | |
| 158 } | |
| 159 | |
| 160 bool HostResolverImplChromeOS::ResolveLocalIPAddress( | |
| 161 const RequestInfo& info, | |
| 162 net::AddressList* addresses) { | |
| 163 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 164 if (info.hostname() != net::GetHostName() || ipv4_address_.empty()) | |
| 165 return false; | |
| 166 | |
| 167 // Use IPConfig data for localhost address lookup. | |
| 168 addresses->clear(); | |
| 169 | |
| 170 if (info.address_family() != net::ADDRESS_FAMILY_IPV4 && | |
| 171 !ipv6_address_.empty()) { | |
| 172 net::IPAddressNumber ipv6; | |
| 173 if (net::ParseIPLiteralToNumber(ipv6_address_, &ipv6)) | |
| 174 addresses->push_back(net::IPEndPoint(ipv6, 0)); | |
| 175 } | |
| 176 | |
| 177 net::IPAddressNumber ipv4; | |
| 178 if (net::ParseIPLiteralToNumber(ipv4_address_, &ipv4)) | |
| 179 addresses->push_back(net::IPEndPoint(ipv4, 0)); | |
| 180 | |
| 181 DVLOG(2) << "ResolveLocalIPAddress(" | |
| 182 << static_cast<int>(info.address_family()) << "): " | |
| 183 << addresses->size() | |
| 184 << " IPv4: " << ipv4_address_ << " IPv6: " << ipv6_address_; | |
| 185 addresses->SetDefaultCanonicalName(); | |
| 186 return true; | |
| 187 } | |
| 188 | |
| 189 // static | |
| 190 scoped_ptr<net::HostResolver> HostResolverImplChromeOS::CreateSystemResolver( | |
| 191 const Options& options, | |
| 192 net::NetLog* net_log) { | |
| 193 return scoped_ptr<net::HostResolver>(new HostResolverImplChromeOS( | |
| 194 NetworkHandler::Get()->message_loop(), | |
| 195 NetworkHandler::Get()->network_state_handler(), | |
| 196 options, | |
| 197 net_log)); | |
| 198 } | |
| 199 | |
| 200 // static | |
| 201 scoped_ptr<net::HostResolver> | |
| 202 HostResolverImplChromeOS::CreateHostResolverForTest( | |
| 203 scoped_refptr<base::MessageLoopProxy> network_handler_message_loop, | |
| 204 NetworkStateHandler* network_state_handler) { | |
| 205 Options options; | |
| 206 return scoped_ptr<net::HostResolver>(new HostResolverImplChromeOS( | |
| 207 network_handler_message_loop, | |
| 208 network_state_handler, | |
| 209 options, | |
| 210 NULL)); | |
| 211 } | |
| 212 | |
| 213 } // namespace chromeos | |
| OLD | NEW |