1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/netwerk/system/mac/nsNetworkLinkService.mm Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,198 @@ 1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#include "nsNetworkLinkService.h" 1.10 +#include "nsCOMPtr.h" 1.11 +#include "nsIObserverService.h" 1.12 +#include "nsServiceManagerUtils.h" 1.13 +#include "nsString.h" 1.14 +#include "nsCRT.h" 1.15 + 1.16 +#import <Cocoa/Cocoa.h> 1.17 +#import <netinet/in.h> 1.18 + 1.19 +NS_IMPL_ISUPPORTS(nsNetworkLinkService, 1.20 + nsINetworkLinkService, 1.21 + nsIObserver) 1.22 + 1.23 +nsNetworkLinkService::nsNetworkLinkService() 1.24 + : mLinkUp(true) 1.25 + , mStatusKnown(false) 1.26 + , mReachability(NULL) 1.27 + , mCFRunLoop(NULL) 1.28 +{ 1.29 +} 1.30 + 1.31 +nsNetworkLinkService::~nsNetworkLinkService() 1.32 +{ 1.33 +} 1.34 + 1.35 +NS_IMETHODIMP 1.36 +nsNetworkLinkService::GetIsLinkUp(bool *aIsUp) 1.37 +{ 1.38 + *aIsUp = mLinkUp; 1.39 + return NS_OK; 1.40 +} 1.41 + 1.42 +NS_IMETHODIMP 1.43 +nsNetworkLinkService::GetLinkStatusKnown(bool *aIsUp) 1.44 +{ 1.45 + *aIsUp = mStatusKnown; 1.46 + return NS_OK; 1.47 +} 1.48 + 1.49 +NS_IMETHODIMP 1.50 +nsNetworkLinkService::GetLinkType(uint32_t *aLinkType) 1.51 +{ 1.52 + NS_ENSURE_ARG_POINTER(aLinkType); 1.53 + 1.54 + // XXX This function has not yet been implemented for this platform 1.55 + *aLinkType = nsINetworkLinkService::LINK_TYPE_UNKNOWN; 1.56 + return NS_OK; 1.57 +} 1.58 + 1.59 +NS_IMETHODIMP 1.60 +nsNetworkLinkService::Observe(nsISupports *subject, 1.61 + const char *topic, 1.62 + const char16_t *data) 1.63 +{ 1.64 + if (!strcmp(topic, "xpcom-shutdown")) { 1.65 + Shutdown(); 1.66 + } 1.67 + 1.68 + return NS_OK; 1.69 +} 1.70 + 1.71 +nsresult 1.72 +nsNetworkLinkService::Init(void) 1.73 +{ 1.74 + nsresult rv; 1.75 + 1.76 + nsCOMPtr<nsIObserverService> observerService = 1.77 + do_GetService("@mozilla.org/observer-service;1", &rv); 1.78 + NS_ENSURE_SUCCESS(rv, rv); 1.79 + 1.80 + rv = observerService->AddObserver(this, "xpcom-shutdown", false); 1.81 + NS_ENSURE_SUCCESS(rv, rv); 1.82 + 1.83 + // If the network reachability API can reach 0.0.0.0 without 1.84 + // requiring a connection, there is a network interface available. 1.85 + struct sockaddr_in addr; 1.86 + bzero(&addr, sizeof(addr)); 1.87 + addr.sin_len = sizeof(addr); 1.88 + addr.sin_family = AF_INET; 1.89 + mReachability = 1.90 + ::SCNetworkReachabilityCreateWithAddress(NULL, 1.91 + (struct sockaddr *)&addr); 1.92 + if (!mReachability) { 1.93 + return NS_ERROR_NOT_AVAILABLE; 1.94 + } 1.95 + 1.96 + SCNetworkReachabilityContext context = {0, this, NULL, NULL, NULL}; 1.97 + if (!::SCNetworkReachabilitySetCallback(mReachability, 1.98 + ReachabilityChanged, 1.99 + &context)) { 1.100 + NS_WARNING("SCNetworkReachabilitySetCallback failed."); 1.101 + ::CFRelease(mReachability); 1.102 + mReachability = NULL; 1.103 + return NS_ERROR_NOT_AVAILABLE; 1.104 + } 1.105 + 1.106 + // Get the current run loop. This service is initialized at startup, 1.107 + // so we shouldn't run in to any problems with modal dialog run loops. 1.108 + mCFRunLoop = [[NSRunLoop currentRunLoop] getCFRunLoop]; 1.109 + if (!mCFRunLoop) { 1.110 + NS_WARNING("Could not get current run loop."); 1.111 + ::CFRelease(mReachability); 1.112 + mReachability = NULL; 1.113 + return NS_ERROR_NOT_AVAILABLE; 1.114 + } 1.115 + ::CFRetain(mCFRunLoop); 1.116 + 1.117 + if (!::SCNetworkReachabilityScheduleWithRunLoop(mReachability, mCFRunLoop, 1.118 + kCFRunLoopDefaultMode)) { 1.119 + NS_WARNING("SCNetworkReachabilityScheduleWIthRunLoop failed."); 1.120 + ::CFRelease(mReachability); 1.121 + mReachability = NULL; 1.122 + ::CFRelease(mCFRunLoop); 1.123 + mCFRunLoop = NULL; 1.124 + return NS_ERROR_NOT_AVAILABLE; 1.125 + } 1.126 + 1.127 + UpdateReachability(); 1.128 + 1.129 + return NS_OK; 1.130 +} 1.131 + 1.132 +nsresult 1.133 +nsNetworkLinkService::Shutdown() 1.134 +{ 1.135 + if (!::SCNetworkReachabilityUnscheduleFromRunLoop(mReachability, 1.136 + mCFRunLoop, 1.137 + kCFRunLoopDefaultMode)) { 1.138 + NS_WARNING("SCNetworkReachabilityUnscheduleFromRunLoop failed."); 1.139 + } 1.140 + 1.141 + ::CFRelease(mReachability); 1.142 + mReachability = NULL; 1.143 + 1.144 + ::CFRelease(mCFRunLoop); 1.145 + mCFRunLoop = NULL; 1.146 + 1.147 + return NS_OK; 1.148 +} 1.149 + 1.150 +void 1.151 +nsNetworkLinkService::UpdateReachability() 1.152 +{ 1.153 + if (!mReachability) { 1.154 + return; 1.155 + } 1.156 + 1.157 + SCNetworkConnectionFlags flags; 1.158 + if (!::SCNetworkReachabilityGetFlags(mReachability, &flags)) { 1.159 + mStatusKnown = false; 1.160 + return; 1.161 + } 1.162 + 1.163 + bool reachable = (flags & kSCNetworkFlagsReachable) != 0; 1.164 + bool needsConnection = (flags & kSCNetworkFlagsConnectionRequired) != 0; 1.165 + 1.166 + mLinkUp = (reachable && !needsConnection); 1.167 + mStatusKnown = true; 1.168 +} 1.169 + 1.170 +void 1.171 +nsNetworkLinkService::SendEvent() 1.172 +{ 1.173 + nsCOMPtr<nsIObserverService> observerService = 1.174 + do_GetService("@mozilla.org/observer-service;1"); 1.175 + if (!observerService) 1.176 + return; 1.177 + 1.178 + const char *event; 1.179 + if (!mStatusKnown) 1.180 + event = NS_NETWORK_LINK_DATA_UNKNOWN; 1.181 + else 1.182 + event = mLinkUp ? NS_NETWORK_LINK_DATA_UP 1.183 + : NS_NETWORK_LINK_DATA_DOWN; 1.184 + 1.185 + observerService->NotifyObservers(static_cast<nsINetworkLinkService*>(this), 1.186 + NS_NETWORK_LINK_TOPIC, 1.187 + NS_ConvertASCIItoUTF16(event).get()); 1.188 +} 1.189 + 1.190 +/* static */ 1.191 +void 1.192 +nsNetworkLinkService::ReachabilityChanged(SCNetworkReachabilityRef target, 1.193 + SCNetworkConnectionFlags flags, 1.194 + void *info) 1.195 +{ 1.196 + nsNetworkLinkService *service = 1.197 + static_cast<nsNetworkLinkService*>(info); 1.198 + 1.199 + service->UpdateReachability(); 1.200 + service->SendEvent(); 1.201 +}