Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(36)

Side by Side Diff: chrome/browser/extensions/api/dial/dial_service.cc

Issue 23712002: Cleanup network type matching. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Added unit tests and rebased. Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/extensions/api/dial/dial_service.h" 5 #include "chrome/browser/extensions/api/dial/dial_service.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 8
9 #include "base/basictypes.h" 9 #include "base/basictypes.h"
10 #include "base/callback.h" 10 #include "base/callback.h"
11 #include "base/logging.h" 11 #include "base/logging.h"
12 #include "base/rand_util.h" 12 #include "base/rand_util.h"
13 #include "base/strings/string_number_conversions.h" 13 #include "base/strings/string_number_conversions.h"
14 #include "base/strings/stringprintf.h" 14 #include "base/strings/stringprintf.h"
15 #include "base/time/time.h" 15 #include "base/time/time.h"
16 #include "chrome/browser/extensions/api/dial/dial_device_data.h" 16 #include "chrome/browser/extensions/api/dial/dial_device_data.h"
17 #include "chrome/common/chrome_version_info.h" 17 #include "chrome/common/chrome_version_info.h"
18 #include "content/public/browser/browser_thread.h" 18 #include "content/public/browser/browser_thread.h"
19 #include "net/base/completion_callback.h" 19 #include "net/base/completion_callback.h"
20 #include "net/base/io_buffer.h" 20 #include "net/base/io_buffer.h"
21 #include "net/base/ip_endpoint.h" 21 #include "net/base/ip_endpoint.h"
22 #include "net/base/net_errors.h" 22 #include "net/base/net_errors.h"
23 #include "net/base/net_util.h" 23 #include "net/base/net_util.h"
24 #include "net/http/http_response_headers.h" 24 #include "net/http/http_response_headers.h"
25 #include "net/http/http_util.h" 25 #include "net/http/http_util.h"
26 #include "url/gurl.h" 26 #include "url/gurl.h"
27 #if defined(OS_CHROMEOS) 27 #if defined(OS_CHROMEOS)
28 #include "chromeos/network/network_state.h" 28 #include "chromeos/network/network_state.h"
29 #include "chromeos/network/network_state_handler.h" 29 #include "chromeos/network/network_state_handler.h"
30 #include "chromeos/network/shill_property_util.h"
30 #include "third_party/cros_system_api/dbus/service_constants.h" 31 #include "third_party/cros_system_api/dbus/service_constants.h"
31 #endif 32 #endif
32 33
33 using base::Time; 34 using base::Time;
34 using base::TimeDelta; 35 using base::TimeDelta;
35 using content::BrowserThread; 36 using content::BrowserThread;
36 using net::HttpResponseHeaders; 37 using net::HttpResponseHeaders;
37 using net::HttpUtil; 38 using net::HttpUtil;
38 using net::IOBufferWithSize; 39 using net::IOBufferWithSize;
39 using net::IPAddressNumber; 40 using net::IPAddressNumber;
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
108 const scoped_refptr<base::MessageLoopProxy>& loop, 109 const scoped_refptr<base::MessageLoopProxy>& loop,
109 const base::Callback<void(const NetworkInterfaceList& networks)>& cb) { 110 const base::Callback<void(const NetworkInterfaceList& networks)>& cb) {
110 NetworkInterfaceList list; 111 NetworkInterfaceList list;
111 bool success = net::GetNetworkList(&list); 112 bool success = net::GetNetworkList(&list);
112 if (!success) 113 if (!success)
113 DVLOG(1) << "Could not retrieve network list!"; 114 DVLOG(1) << "Could not retrieve network list!";
114 115
115 loop->PostTask(FROM_HERE, base::Bind(cb, list)); 116 loop->PostTask(FROM_HERE, base::Bind(cb, list));
116 } 117 }
117 118
119 #if defined(OS_CHROMEOS)
120 IPAddressNumber GetBestBindAddressByType(
121 const chromeos::NetworkTypePattern& type) {
122 const chromeos::NetworkState* state = chromeos::NetworkHandler::Get()
123 ->network_state_handler()
124 ->ConnectedNetworkByType(type);
stevenjb 2013/09/04 20:40:39 Is this from clang-format? It's kind of weird look
pneubeck (no reviews) 2013/09/05 08:28:34 yes. I changed it to two lines.
125 IPAddressNumber bind_ip_address;
126 if (!state ||
127 !net::ParseIPLiteralToNumber(state->ip_address(), &bind_ip_address)) {
128 return IPAddressNumber();
129 }
130 if (bind_ip_address.size() != net::kIPv4AddressSize) {
131 LOG(ERROR) << "Default network is not using IPv4.";
132 return IPAddressNumber();
133 }
134
135 DVLOG(1) << "Found " << state->type() << ", " << state->name() << ":"
136 << state->ip_address();
137 return bind_ip_address;
138 }
139
140 // Returns the IP address of the preferred interface to bind the socket. This
141 // ChromeOS version can prioritize wifi and ethernet interfaces.
142 IPAddressNumber GetBestBindAddressChromeOS() {
143 IPAddressNumber bind_ip_address =
144 GetBestBindAddressByType(chromeos::NetworkTypePattern::Ethernet());
145 if (bind_ip_address.empty()) {
146 bind_ip_address =
147 GetBestBindAddressByType(chromeos::NetworkTypePattern::WiFi());
148 }
149 return bind_ip_address;
150 }
151 #endif
152
118 } // namespace 153 } // namespace
119 154
120 DialServiceImpl::DialServiceImpl(net::NetLog* net_log) 155 DialServiceImpl::DialServiceImpl(net::NetLog* net_log)
121 : is_writing_(false), 156 : is_writing_(false),
122 is_reading_(false), 157 is_reading_(false),
123 discovery_active_(false), 158 discovery_active_(false),
124 num_requests_sent_(0), 159 num_requests_sent_(0),
125 max_requests_(kDialMaxRequests), 160 max_requests_(kDialMaxRequests),
126 finish_delay_(TimeDelta::FromMilliseconds((kDialMaxRequests - 1) * 161 finish_delay_(TimeDelta::FromMilliseconds((kDialMaxRequests - 1) *
127 kDialRequestIntervalMillis) + 162 kDialRequestIntervalMillis) +
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
180 // information such as interface types (i.e. wifi vs cellular). 215 // information such as interface types (i.e. wifi vs cellular).
181 BindSocketAndSendRequest(GetBestBindAddressChromeOS()); 216 BindSocketAndSendRequest(GetBestBindAddressChromeOS());
182 #else 217 #else
183 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, base::Bind( 218 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, base::Bind(
184 &GetNetworkListOnFileThread, 219 &GetNetworkListOnFileThread,
185 base::MessageLoopProxy::current(), base::Bind( 220 base::MessageLoopProxy::current(), base::Bind(
186 &DialServiceImpl::SendNetworkList, AsWeakPtr()))); 221 &DialServiceImpl::SendNetworkList, AsWeakPtr())));
187 #endif 222 #endif
188 } 223 }
189 224
190 #if defined(OS_CHROMEOS)
191 IPAddressNumber DialServiceImpl::GetBestBindAddressChromeOS() {
192 std::string connection_types[] =
193 {flimflam::kTypeWifi, flimflam::kTypeEthernet};
194 for (uint i = 0; i < arraysize(connection_types); ++i) {
195 IPAddressNumber bind_ip_address;
196 const chromeos::NetworkState* state =
197 chromeos::NetworkHandler::Get()->network_state_handler()->
198 ConnectedNetworkByType(connection_types[i]);
199 if (state &&
200 net::ParseIPLiteralToNumber(state->ip_address(), &bind_ip_address)) {
201 DCHECK(bind_ip_address.size() == net::kIPv4AddressSize);
202 DVLOG(1) << "Found " << state->type() << ", " << state->name() << ":"
203 << state->ip_address();
204 return bind_ip_address;
205 }
206 }
207 return IPAddressNumber();
208 }
209 #endif
210
211 bool DialServiceImpl::BindSocketAndSendRequest( 225 bool DialServiceImpl::BindSocketAndSendRequest(
212 const IPAddressNumber& bind_ip_address) { 226 const IPAddressNumber& bind_ip_address) {
213 DCHECK(thread_checker_.CalledOnValidThread()); 227 DCHECK(thread_checker_.CalledOnValidThread());
214 DCHECK(!socket_.get()); 228 DCHECK(!socket_.get());
215 229
216 if (bind_ip_address.size() == 0) { 230 if (bind_ip_address.size() == 0) {
217 DVLOG(1) << "Could not find a valid interface to bind."; 231 DVLOG(1) << "Could not find a valid interface to bind.";
218 FinishDiscovery(); 232 FinishDiscovery();
219 return false; 233 return false;
220 } 234 }
(...skipping 256 matching lines...) Expand 10 before | Expand all | Expand 10 after
477 DVLOG(0) << "dial socket error: " << error_str; 491 DVLOG(0) << "dial socket error: " << error_str;
478 // TODO(justinlin): More granular socket errors. 492 // TODO(justinlin): More granular socket errors.
479 FOR_EACH_OBSERVER( 493 FOR_EACH_OBSERVER(
480 Observer, observer_list_, OnError(this, DIAL_SERVICE_SOCKET_ERROR)); 494 Observer, observer_list_, OnError(this, DIAL_SERVICE_SOCKET_ERROR));
481 return false; 495 return false;
482 } 496 }
483 return true; 497 return true;
484 } 498 }
485 499
486 } // namespace extensions 500 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698