Adds a reset button to the zoom bubble on GTK.
[chromium-blink-merge.git] / net / base / net_util_posix.cc
blob8ff720f3e43d0e47e4707eecde5a5a36a25892ea
1 // Copyright (c) 2011 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 #include "net/base/net_util.h"
7 #include <sys/types.h>
9 #include "base/eintr_wrapper.h"
10 #include "base/file_path.h"
11 #include "base/logging.h"
12 #include "base/string_util.h"
13 #include "base/threading/thread_restrictions.h"
14 #include "googleurl/src/gurl.h"
15 #include "net/base/escape.h"
16 #include "net/base/ip_endpoint.h"
17 #include "net/base/net_errors.h"
19 #if !defined(OS_ANDROID)
20 #include <ifaddrs.h>
21 #endif
22 #include <net/if.h>
23 #include <netinet/in.h>
25 namespace net {
27 bool FileURLToFilePath(const GURL& url, FilePath* path) {
28 *path = FilePath();
29 std::string& file_path_str = const_cast<std::string&>(path->value());
30 file_path_str.clear();
32 if (!url.is_valid())
33 return false;
35 // Firefox seems to ignore the "host" of a file url if there is one. That is,
36 // file://foo/bar.txt maps to /bar.txt.
37 // TODO(dhg): This should probably take into account UNCs which could
38 // include a hostname other than localhost or blank
39 std::string old_path = url.path();
41 if (old_path.empty())
42 return false;
44 // GURL stores strings as percent-encoded 8-bit, this will undo if possible.
45 old_path = UnescapeURLComponent(old_path,
46 UnescapeRule::SPACES | UnescapeRule::URL_SPECIAL_CHARS);
48 // Collapse multiple path slashes into a single path slash.
49 std::string new_path;
50 do {
51 new_path = old_path;
52 ReplaceSubstringsAfterOffset(&new_path, 0, "//", "/");
53 old_path.swap(new_path);
54 } while (new_path != old_path);
56 file_path_str.assign(old_path);
58 return !file_path_str.empty();
61 bool GetNetworkList(NetworkInterfaceList* networks) {
62 #if defined(OS_ANDROID)
63 // TODO: Android API doesn't support ifaddrs. This method was only used by
64 // P2PMessage. Consider to implement it until really needed. The possible
65 // approach is implementing the similar feature by
66 // java.net.NetworkInterface through JNI.
67 NOTIMPLEMENTED();
68 return false;
69 #else
70 // getifaddrs() may require IO operations.
71 base::ThreadRestrictions::AssertIOAllowed();
73 ifaddrs *interfaces;
74 if (getifaddrs(&interfaces) < 0) {
75 PLOG(ERROR) << "getifaddrs";
76 return false;
79 // Enumerate the addresses assigned to network interfaces which are up.
80 for (ifaddrs *interface = interfaces;
81 interface != NULL;
82 interface = interface->ifa_next) {
83 // Skip loopback interfaces, and ones which are down.
84 if (!(IFF_UP & interface->ifa_flags))
85 continue;
86 if (IFF_LOOPBACK & interface->ifa_flags)
87 continue;
88 // Skip interfaces with no address configured.
89 struct sockaddr* addr = interface->ifa_addr;
90 if (!addr)
91 continue;
92 // Skip loopback addresses configured on non-loopback interfaces.
93 int addr_size = 0;
94 if (addr->sa_family == AF_INET6) {
95 struct sockaddr_in6* addr_in6 =
96 reinterpret_cast<struct sockaddr_in6*>(addr);
97 struct in6_addr* sin6_addr = &addr_in6->sin6_addr;
98 addr_size = sizeof(*addr_in6);
99 if (IN6_IS_ADDR_LOOPBACK(sin6_addr))
100 continue;
101 } else if (addr->sa_family == AF_INET) {
102 struct sockaddr_in* addr_in =
103 reinterpret_cast<struct sockaddr_in*>(addr);
104 addr_size = sizeof(*addr_in);
105 if (addr_in->sin_addr.s_addr == INADDR_LOOPBACK)
106 continue;
107 } else {
108 // Skip non-IP addresses.
109 continue;
111 IPEndPoint address;
112 std::string name = interface->ifa_name;
113 if (address.FromSockAddr(addr, addr_size)) {
114 networks->push_back(NetworkInterface(name, address.address()));
118 freeifaddrs(interfaces);
120 return true;
121 #endif
124 } // namespace net