netwerk/system/mac/nsNetworkLinkService.mm

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 #include "nsNetworkLinkService.h"
michael@0 7 #include "nsCOMPtr.h"
michael@0 8 #include "nsIObserverService.h"
michael@0 9 #include "nsServiceManagerUtils.h"
michael@0 10 #include "nsString.h"
michael@0 11 #include "nsCRT.h"
michael@0 12
michael@0 13 #import <Cocoa/Cocoa.h>
michael@0 14 #import <netinet/in.h>
michael@0 15
michael@0 16 NS_IMPL_ISUPPORTS(nsNetworkLinkService,
michael@0 17 nsINetworkLinkService,
michael@0 18 nsIObserver)
michael@0 19
michael@0 20 nsNetworkLinkService::nsNetworkLinkService()
michael@0 21 : mLinkUp(true)
michael@0 22 , mStatusKnown(false)
michael@0 23 , mReachability(NULL)
michael@0 24 , mCFRunLoop(NULL)
michael@0 25 {
michael@0 26 }
michael@0 27
michael@0 28 nsNetworkLinkService::~nsNetworkLinkService()
michael@0 29 {
michael@0 30 }
michael@0 31
michael@0 32 NS_IMETHODIMP
michael@0 33 nsNetworkLinkService::GetIsLinkUp(bool *aIsUp)
michael@0 34 {
michael@0 35 *aIsUp = mLinkUp;
michael@0 36 return NS_OK;
michael@0 37 }
michael@0 38
michael@0 39 NS_IMETHODIMP
michael@0 40 nsNetworkLinkService::GetLinkStatusKnown(bool *aIsUp)
michael@0 41 {
michael@0 42 *aIsUp = mStatusKnown;
michael@0 43 return NS_OK;
michael@0 44 }
michael@0 45
michael@0 46 NS_IMETHODIMP
michael@0 47 nsNetworkLinkService::GetLinkType(uint32_t *aLinkType)
michael@0 48 {
michael@0 49 NS_ENSURE_ARG_POINTER(aLinkType);
michael@0 50
michael@0 51 // XXX This function has not yet been implemented for this platform
michael@0 52 *aLinkType = nsINetworkLinkService::LINK_TYPE_UNKNOWN;
michael@0 53 return NS_OK;
michael@0 54 }
michael@0 55
michael@0 56 NS_IMETHODIMP
michael@0 57 nsNetworkLinkService::Observe(nsISupports *subject,
michael@0 58 const char *topic,
michael@0 59 const char16_t *data)
michael@0 60 {
michael@0 61 if (!strcmp(topic, "xpcom-shutdown")) {
michael@0 62 Shutdown();
michael@0 63 }
michael@0 64
michael@0 65 return NS_OK;
michael@0 66 }
michael@0 67
michael@0 68 nsresult
michael@0 69 nsNetworkLinkService::Init(void)
michael@0 70 {
michael@0 71 nsresult rv;
michael@0 72
michael@0 73 nsCOMPtr<nsIObserverService> observerService =
michael@0 74 do_GetService("@mozilla.org/observer-service;1", &rv);
michael@0 75 NS_ENSURE_SUCCESS(rv, rv);
michael@0 76
michael@0 77 rv = observerService->AddObserver(this, "xpcom-shutdown", false);
michael@0 78 NS_ENSURE_SUCCESS(rv, rv);
michael@0 79
michael@0 80 // If the network reachability API can reach 0.0.0.0 without
michael@0 81 // requiring a connection, there is a network interface available.
michael@0 82 struct sockaddr_in addr;
michael@0 83 bzero(&addr, sizeof(addr));
michael@0 84 addr.sin_len = sizeof(addr);
michael@0 85 addr.sin_family = AF_INET;
michael@0 86 mReachability =
michael@0 87 ::SCNetworkReachabilityCreateWithAddress(NULL,
michael@0 88 (struct sockaddr *)&addr);
michael@0 89 if (!mReachability) {
michael@0 90 return NS_ERROR_NOT_AVAILABLE;
michael@0 91 }
michael@0 92
michael@0 93 SCNetworkReachabilityContext context = {0, this, NULL, NULL, NULL};
michael@0 94 if (!::SCNetworkReachabilitySetCallback(mReachability,
michael@0 95 ReachabilityChanged,
michael@0 96 &context)) {
michael@0 97 NS_WARNING("SCNetworkReachabilitySetCallback failed.");
michael@0 98 ::CFRelease(mReachability);
michael@0 99 mReachability = NULL;
michael@0 100 return NS_ERROR_NOT_AVAILABLE;
michael@0 101 }
michael@0 102
michael@0 103 // Get the current run loop. This service is initialized at startup,
michael@0 104 // so we shouldn't run in to any problems with modal dialog run loops.
michael@0 105 mCFRunLoop = [[NSRunLoop currentRunLoop] getCFRunLoop];
michael@0 106 if (!mCFRunLoop) {
michael@0 107 NS_WARNING("Could not get current run loop.");
michael@0 108 ::CFRelease(mReachability);
michael@0 109 mReachability = NULL;
michael@0 110 return NS_ERROR_NOT_AVAILABLE;
michael@0 111 }
michael@0 112 ::CFRetain(mCFRunLoop);
michael@0 113
michael@0 114 if (!::SCNetworkReachabilityScheduleWithRunLoop(mReachability, mCFRunLoop,
michael@0 115 kCFRunLoopDefaultMode)) {
michael@0 116 NS_WARNING("SCNetworkReachabilityScheduleWIthRunLoop failed.");
michael@0 117 ::CFRelease(mReachability);
michael@0 118 mReachability = NULL;
michael@0 119 ::CFRelease(mCFRunLoop);
michael@0 120 mCFRunLoop = NULL;
michael@0 121 return NS_ERROR_NOT_AVAILABLE;
michael@0 122 }
michael@0 123
michael@0 124 UpdateReachability();
michael@0 125
michael@0 126 return NS_OK;
michael@0 127 }
michael@0 128
michael@0 129 nsresult
michael@0 130 nsNetworkLinkService::Shutdown()
michael@0 131 {
michael@0 132 if (!::SCNetworkReachabilityUnscheduleFromRunLoop(mReachability,
michael@0 133 mCFRunLoop,
michael@0 134 kCFRunLoopDefaultMode)) {
michael@0 135 NS_WARNING("SCNetworkReachabilityUnscheduleFromRunLoop failed.");
michael@0 136 }
michael@0 137
michael@0 138 ::CFRelease(mReachability);
michael@0 139 mReachability = NULL;
michael@0 140
michael@0 141 ::CFRelease(mCFRunLoop);
michael@0 142 mCFRunLoop = NULL;
michael@0 143
michael@0 144 return NS_OK;
michael@0 145 }
michael@0 146
michael@0 147 void
michael@0 148 nsNetworkLinkService::UpdateReachability()
michael@0 149 {
michael@0 150 if (!mReachability) {
michael@0 151 return;
michael@0 152 }
michael@0 153
michael@0 154 SCNetworkConnectionFlags flags;
michael@0 155 if (!::SCNetworkReachabilityGetFlags(mReachability, &flags)) {
michael@0 156 mStatusKnown = false;
michael@0 157 return;
michael@0 158 }
michael@0 159
michael@0 160 bool reachable = (flags & kSCNetworkFlagsReachable) != 0;
michael@0 161 bool needsConnection = (flags & kSCNetworkFlagsConnectionRequired) != 0;
michael@0 162
michael@0 163 mLinkUp = (reachable && !needsConnection);
michael@0 164 mStatusKnown = true;
michael@0 165 }
michael@0 166
michael@0 167 void
michael@0 168 nsNetworkLinkService::SendEvent()
michael@0 169 {
michael@0 170 nsCOMPtr<nsIObserverService> observerService =
michael@0 171 do_GetService("@mozilla.org/observer-service;1");
michael@0 172 if (!observerService)
michael@0 173 return;
michael@0 174
michael@0 175 const char *event;
michael@0 176 if (!mStatusKnown)
michael@0 177 event = NS_NETWORK_LINK_DATA_UNKNOWN;
michael@0 178 else
michael@0 179 event = mLinkUp ? NS_NETWORK_LINK_DATA_UP
michael@0 180 : NS_NETWORK_LINK_DATA_DOWN;
michael@0 181
michael@0 182 observerService->NotifyObservers(static_cast<nsINetworkLinkService*>(this),
michael@0 183 NS_NETWORK_LINK_TOPIC,
michael@0 184 NS_ConvertASCIItoUTF16(event).get());
michael@0 185 }
michael@0 186
michael@0 187 /* static */
michael@0 188 void
michael@0 189 nsNetworkLinkService::ReachabilityChanged(SCNetworkReachabilityRef target,
michael@0 190 SCNetworkConnectionFlags flags,
michael@0 191 void *info)
michael@0 192 {
michael@0 193 nsNetworkLinkService *service =
michael@0 194 static_cast<nsNetworkLinkService*>(info);
michael@0 195
michael@0 196 service->UpdateReachability();
michael@0 197 service->SendEvent();
michael@0 198 }

mercurial