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

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: Fixed unit test in Debug. 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()->ConnectedNetworkByType(type);
124 IPAddressNumber bind_ip_address;
125 if (!state ||
126 !net::ParseIPLiteralToNumber(state->ip_address(), &bind_ip_address)) {
127 return IPAddressNumber();
128 }
129 if (bind_ip_address.size() != net::kIPv4AddressSize) {
130 LOG(ERROR) << "Default network is not using IPv4.";
131 return IPAddressNumber();
132 }
133
134 DVLOG(1) << "Found " << state->type() << ", " << state->name() << ":"
135 << state->ip_address();
136 return bind_ip_address;
137 }
138
139 // Returns the IP address of the preferred interface to bind the socket. This
140 // ChromeOS version can prioritize wifi and ethernet interfaces.
141 IPAddressNumber GetBestBindAddressChromeOS() {
142 IPAddressNumber bind_ip_address =
143 GetBestBindAddressByType(chromeos::NetworkTypePattern::Ethernet());
144 if (bind_ip_address.empty()) {
145 bind_ip_address =
146 GetBestBindAddressByType(chromeos::NetworkTypePattern::WiFi());
147 }
148 return bind_ip_address;
149 }
150 #endif
151
118 } // namespace 152 } // namespace
119 153
120 DialServiceImpl::DialServiceImpl(net::NetLog* net_log) 154 DialServiceImpl::DialServiceImpl(net::NetLog* net_log)
121 : is_writing_(false), 155 : is_writing_(false),
122 is_reading_(false), 156 is_reading_(false),
123 discovery_active_(false), 157 discovery_active_(false),
124 num_requests_sent_(0), 158 num_requests_sent_(0),
125 max_requests_(kDialMaxRequests), 159 max_requests_(kDialMaxRequests),
126 finish_delay_(TimeDelta::FromMilliseconds((kDialMaxRequests - 1) * 160 finish_delay_(TimeDelta::FromMilliseconds((kDialMaxRequests - 1) *
127 kDialRequestIntervalMillis) + 161 kDialRequestIntervalMillis) +
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
180 // information such as interface types (i.e. wifi vs cellular). 214 // information such as interface types (i.e. wifi vs cellular).
181 BindSocketAndSendRequest(GetBestBindAddressChromeOS()); 215 BindSocketAndSendRequest(GetBestBindAddressChromeOS());
182 #else 216 #else
183 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, base::Bind( 217 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, base::Bind(
184 &GetNetworkListOnFileThread, 218 &GetNetworkListOnFileThread,
185 base::MessageLoopProxy::current(), base::Bind( 219 base::MessageLoopProxy::current(), base::Bind(
186 &DialServiceImpl::SendNetworkList, AsWeakPtr()))); 220 &DialServiceImpl::SendNetworkList, AsWeakPtr())));
187 #endif 221 #endif
188 } 222 }
189 223
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( 224 bool DialServiceImpl::BindSocketAndSendRequest(
212 const IPAddressNumber& bind_ip_address) { 225 const IPAddressNumber& bind_ip_address) {
213 DCHECK(thread_checker_.CalledOnValidThread()); 226 DCHECK(thread_checker_.CalledOnValidThread());
214 DCHECK(!socket_.get()); 227 DCHECK(!socket_.get());
215 228
216 if (bind_ip_address.size() == 0) { 229 if (bind_ip_address.size() == 0) {
217 DVLOG(1) << "Could not find a valid interface to bind."; 230 DVLOG(1) << "Could not find a valid interface to bind.";
218 FinishDiscovery(); 231 FinishDiscovery();
219 return false; 232 return false;
220 } 233 }
(...skipping 256 matching lines...) Expand 10 before | Expand all | Expand 10 after
477 DVLOG(0) << "dial socket error: " << error_str; 490 DVLOG(0) << "dial socket error: " << error_str;
478 // TODO(justinlin): More granular socket errors. 491 // TODO(justinlin): More granular socket errors.
479 FOR_EACH_OBSERVER( 492 FOR_EACH_OBSERVER(
480 Observer, observer_list_, OnError(this, DIAL_SERVICE_SOCKET_ERROR)); 493 Observer, observer_list_, OnError(this, DIAL_SERVICE_SOCKET_ERROR));
481 return false; 494 return false;
482 } 495 }
483 return true; 496 return true;
484 } 497 }
485 498
486 } // namespace extensions 499 } // namespace extensions
OLDNEW
« no previous file with comments | « chrome/browser/extensions/api/dial/dial_service.h ('k') | chrome/browser/ui/webui/chromeos/choose_mobile_network_ui.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698