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

Side by Side Diff: chromeos/network/shill_property_util.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 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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 "chromeos/network/shill_property_util.h" 5 #include "chromeos/network/shill_property_util.h"
6 6
7 #include "base/i18n/icu_encoding_detection.h" 7 #include "base/i18n/icu_encoding_detection.h"
8 #include "base/i18n/icu_string_conversions.h" 8 #include "base/i18n/icu_string_conversions.h"
9 #include "base/json/json_writer.h" 9 #include "base/json/json_writer.h"
10 #include "base/strings/string_number_conversions.h" 10 #include "base/strings/string_number_conversions.h"
(...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after
229 NOTREACHED() << "Unsupported network type " << type; 229 NOTREACHED() << "Unsupported network type " << type;
230 success = false; 230 success = false;
231 } 231 }
232 if (!success) 232 if (!success)
233 NET_LOG_ERROR("CopyIdentifyingProperties", "Missing required properties"); 233 NET_LOG_ERROR("CopyIdentifyingProperties", "Missing required properties");
234 return success; 234 return success;
235 } 235 }
236 236
237 } // namespace shill_property_util 237 } // namespace shill_property_util
238 238
239 namespace {
240
241 const char kPatternDefault[] = "PatternDefault";
242 const char kPatternEthernet[] = "PatternEthernet";
243 const char kPatternWireless[] = "PatternWireless";
244 const char kPatternMobile[] = "PatternMobile";
245 const char kPatternNonVirtual[] = "PatternNonVirtual";
246
247 enum NetworkTypeBitFlag {
248 kNetworkTypeNone = 0,
249 kNetworkTypeEthernet = 1 << 0,
250 kNetworkTypeWifi = 1 << 1,
251 kNetworkTypeWimax = 1 << 2,
252 kNetworkTypeCellular = 1 << 3,
253 kNetworkTypeVPN = 1 << 4,
254 kNetworkTypeEthernetEap = 1 << 5
255 };
256
257 struct ShillToBitFlagEntry {
258 const char* shill_network_type;
259 NetworkTypeBitFlag bit_flag;
260 } shill_type_to_flag[] = {
261 { flimflam::kTypeEthernet, kNetworkTypeEthernet },
262 { shill::kTypeEthernetEap, kNetworkTypeEthernetEap },
263 { flimflam::kTypeWifi, kNetworkTypeWifi },
264 { flimflam::kTypeWimax, kNetworkTypeWimax },
265 { flimflam::kTypeCellular, kNetworkTypeCellular },
266 { flimflam::kTypeVPN, kNetworkTypeVPN }
267 };
268
269 NetworkTypeBitFlag ShillNetworkTypeToFlag(const std::string& shill_type) {
270 for (size_t i = 0; i < arraysize(shill_type_to_flag); ++i) {
271 if (shill_type_to_flag[i].shill_network_type == shill_type)
272 return shill_type_to_flag[i].bit_flag;
273 }
274 NET_LOG_ERROR("ShillNetworkTypeToFlag", "Unknown type: " + shill_type);
275 return kNetworkTypeNone;
276 }
277
278 } // namespace
279
280 // static
281 NetworkTypePattern NetworkTypePattern::Default() {
282 return NetworkTypePattern(~0);
283 }
284
285 // static
286 NetworkTypePattern NetworkTypePattern::Wireless() {
287 return NetworkTypePattern(kNetworkTypeWifi | kNetworkTypeWimax |
288 kNetworkTypeCellular);
289 }
290
291 // static
292 NetworkTypePattern NetworkTypePattern::Mobile() {
293 return NetworkTypePattern(kNetworkTypeCellular | kNetworkTypeWimax);
294 }
295
296 // static
297 NetworkTypePattern NetworkTypePattern::NonVirtual() {
298 return NetworkTypePattern(~kNetworkTypeVPN);
299 }
300
301 // static
302 NetworkTypePattern NetworkTypePattern::Ethernet() {
303 return NetworkTypePattern(kNetworkTypeEthernet);
304 }
305
306 // static
307 NetworkTypePattern NetworkTypePattern::WiFi() {
308 return NetworkTypePattern(kNetworkTypeWifi);
309 }
310
311 // static
312 NetworkTypePattern NetworkTypePattern::Cellular() {
313 return NetworkTypePattern(kNetworkTypeCellular);
314 }
315
316 // static
317 NetworkTypePattern NetworkTypePattern::VPN() {
318 return NetworkTypePattern(kNetworkTypeVPN);
319 }
320
321 // static
322 NetworkTypePattern NetworkTypePattern::Wimax() {
323 return NetworkTypePattern(kNetworkTypeWimax);
324 }
325
326 // static
327 NetworkTypePattern NetworkTypePattern::Primitive(
328 const std::string& shill_network_type) {
329 return NetworkTypePattern(ShillNetworkTypeToFlag(shill_network_type));
330 }
331
332 bool NetworkTypePattern::Equals(const NetworkTypePattern& other) const {
333 return pattern_ == other.pattern_;
334 }
335
336 bool NetworkTypePattern::MatchesType(
337 const std::string& shill_network_type) const {
338 return MatchesPattern(Primitive(shill_network_type));
339 }
340
341 bool NetworkTypePattern::MatchesPattern(
342 const NetworkTypePattern& other_pattern) const {
343 if (Equals(other_pattern))
344 return true;
345
346 return pattern_ & other_pattern.pattern_;
347 }
348
349 std::string NetworkTypePattern::ToDebugString() const {
350 if (Equals(Default()))
351 return kPatternDefault;
352 if (Equals(Ethernet()))
353 return kPatternEthernet;
354 if (Equals(Wireless()))
355 return kPatternWireless;
356 if (Equals(Mobile()))
357 return kPatternMobile;
358 if (Equals(NonVirtual()))
359 return kPatternNonVirtual;
360
361 std::string str;
362 for (size_t i = 0; i < arraysize(shill_type_to_flag); ++i) {
363 if (!(pattern_ & shill_type_to_flag[i].bit_flag))
364 continue;
365 if (!str.empty())
366 str += "|";
367 str += shill_type_to_flag[i].shill_network_type;
368 }
369 return str;
370 }
371
372 NetworkTypePattern::NetworkTypePattern(int pattern) : pattern_(pattern) {}
373
239 } // namespace chromeos 374 } // namespace chromeos
OLDNEW
« no previous file with comments | « chromeos/network/shill_property_util.h ('k') | chromeos/network/shill_property_util_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698