2008-09-30 15:27:06 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2012-05-21 04:12:37 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2008-09-30 15:27:06 -07:00
|
|
|
|
|
|
|
#include "nsNetworkLinkService.h"
|
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsIObserverService.h"
|
|
|
|
#include "nsServiceManagerUtils.h"
|
|
|
|
#include "nsString.h"
|
2011-07-10 15:24:05 -07:00
|
|
|
#include "nsCRT.h"
|
2008-09-30 15:27:06 -07:00
|
|
|
|
|
|
|
#import <Cocoa/Cocoa.h>
|
|
|
|
#import <netinet/in.h>
|
|
|
|
|
2014-04-27 00:06:00 -07:00
|
|
|
NS_IMPL_ISUPPORTS(nsNetworkLinkService,
|
|
|
|
nsINetworkLinkService,
|
|
|
|
nsIObserver)
|
2008-09-30 15:27:06 -07:00
|
|
|
|
|
|
|
nsNetworkLinkService::nsNetworkLinkService()
|
2011-10-17 07:59:28 -07:00
|
|
|
: mLinkUp(true)
|
|
|
|
, mStatusKnown(false)
|
2008-09-30 15:27:06 -07:00
|
|
|
, mReachability(NULL)
|
|
|
|
, mCFRunLoop(NULL)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsNetworkLinkService::~nsNetworkLinkService()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-28 23:19:26 -07:00
|
|
|
nsNetworkLinkService::GetIsLinkUp(bool *aIsUp)
|
2008-09-30 15:27:06 -07:00
|
|
|
{
|
|
|
|
*aIsUp = mLinkUp;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-28 23:19:26 -07:00
|
|
|
nsNetworkLinkService::GetLinkStatusKnown(bool *aIsUp)
|
2008-09-30 15:27:06 -07:00
|
|
|
{
|
|
|
|
*aIsUp = mStatusKnown;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-07-10 15:24:05 -07:00
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
nsNetworkLinkService::GetLinkType(uint32_t *aLinkType)
|
2011-07-10 15:24:05 -07:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aLinkType);
|
|
|
|
|
|
|
|
// XXX This function has not yet been implemented for this platform
|
|
|
|
*aLinkType = nsINetworkLinkService::LINK_TYPE_UNKNOWN;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2008-09-30 15:27:06 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsNetworkLinkService::Observe(nsISupports *subject,
|
|
|
|
const char *topic,
|
2014-01-04 07:02:17 -08:00
|
|
|
const char16_t *data)
|
2008-09-30 15:27:06 -07:00
|
|
|
{
|
|
|
|
if (!strcmp(topic, "xpcom-shutdown")) {
|
|
|
|
Shutdown();
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsNetworkLinkService::Init(void)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIObserverService> observerService =
|
|
|
|
do_GetService("@mozilla.org/observer-service;1", &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
rv = observerService->AddObserver(this, "xpcom-shutdown", false);
|
2008-09-30 15:27:06 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// If the network reachability API can reach 0.0.0.0 without
|
|
|
|
// requiring a connection, there is a network interface available.
|
|
|
|
struct sockaddr_in addr;
|
|
|
|
bzero(&addr, sizeof(addr));
|
|
|
|
addr.sin_len = sizeof(addr);
|
|
|
|
addr.sin_family = AF_INET;
|
|
|
|
mReachability =
|
|
|
|
::SCNetworkReachabilityCreateWithAddress(NULL,
|
|
|
|
(struct sockaddr *)&addr);
|
|
|
|
if (!mReachability) {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
SCNetworkReachabilityContext context = {0, this, NULL, NULL, NULL};
|
|
|
|
if (!::SCNetworkReachabilitySetCallback(mReachability,
|
|
|
|
ReachabilityChanged,
|
|
|
|
&context)) {
|
|
|
|
NS_WARNING("SCNetworkReachabilitySetCallback failed.");
|
|
|
|
::CFRelease(mReachability);
|
|
|
|
mReachability = NULL;
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the current run loop. This service is initialized at startup,
|
|
|
|
// so we shouldn't run in to any problems with modal dialog run loops.
|
|
|
|
mCFRunLoop = [[NSRunLoop currentRunLoop] getCFRunLoop];
|
|
|
|
if (!mCFRunLoop) {
|
|
|
|
NS_WARNING("Could not get current run loop.");
|
|
|
|
::CFRelease(mReachability);
|
|
|
|
mReachability = NULL;
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
::CFRetain(mCFRunLoop);
|
|
|
|
|
|
|
|
if (!::SCNetworkReachabilityScheduleWithRunLoop(mReachability, mCFRunLoop,
|
|
|
|
kCFRunLoopDefaultMode)) {
|
|
|
|
NS_WARNING("SCNetworkReachabilityScheduleWIthRunLoop failed.");
|
|
|
|
::CFRelease(mReachability);
|
|
|
|
mReachability = NULL;
|
|
|
|
::CFRelease(mCFRunLoop);
|
|
|
|
mCFRunLoop = NULL;
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
UpdateReachability();
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsNetworkLinkService::Shutdown()
|
|
|
|
{
|
|
|
|
if (!::SCNetworkReachabilityUnscheduleFromRunLoop(mReachability,
|
|
|
|
mCFRunLoop,
|
|
|
|
kCFRunLoopDefaultMode)) {
|
|
|
|
NS_WARNING("SCNetworkReachabilityUnscheduleFromRunLoop failed.");
|
|
|
|
}
|
|
|
|
|
|
|
|
::CFRelease(mReachability);
|
|
|
|
mReachability = NULL;
|
|
|
|
|
|
|
|
::CFRelease(mCFRunLoop);
|
|
|
|
mCFRunLoop = NULL;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsNetworkLinkService::UpdateReachability()
|
|
|
|
{
|
|
|
|
if (!mReachability) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
SCNetworkConnectionFlags flags;
|
2009-02-28 01:53:45 -08:00
|
|
|
if (!::SCNetworkReachabilityGetFlags(mReachability, &flags)) {
|
2011-10-17 07:59:28 -07:00
|
|
|
mStatusKnown = false;
|
2008-09-30 15:27:06 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool reachable = (flags & kSCNetworkFlagsReachable) != 0;
|
|
|
|
bool needsConnection = (flags & kSCNetworkFlagsConnectionRequired) != 0;
|
2008-09-30 15:27:06 -07:00
|
|
|
|
|
|
|
mLinkUp = (reachable && !needsConnection);
|
2011-10-17 07:59:28 -07:00
|
|
|
mStatusKnown = true;
|
2008-09-30 15:27:06 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsNetworkLinkService::SendEvent()
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIObserverService> observerService =
|
|
|
|
do_GetService("@mozilla.org/observer-service;1");
|
|
|
|
if (!observerService)
|
|
|
|
return;
|
|
|
|
|
|
|
|
const char *event;
|
|
|
|
if (!mStatusKnown)
|
|
|
|
event = NS_NETWORK_LINK_DATA_UNKNOWN;
|
|
|
|
else
|
|
|
|
event = mLinkUp ? NS_NETWORK_LINK_DATA_UP
|
|
|
|
: NS_NETWORK_LINK_DATA_DOWN;
|
|
|
|
|
|
|
|
observerService->NotifyObservers(static_cast<nsINetworkLinkService*>(this),
|
|
|
|
NS_NETWORK_LINK_TOPIC,
|
|
|
|
NS_ConvertASCIItoUTF16(event).get());
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */
|
|
|
|
void
|
|
|
|
nsNetworkLinkService::ReachabilityChanged(SCNetworkReachabilityRef target,
|
|
|
|
SCNetworkConnectionFlags flags,
|
|
|
|
void *info)
|
|
|
|
{
|
|
|
|
nsNetworkLinkService *service =
|
|
|
|
static_cast<nsNetworkLinkService*>(info);
|
|
|
|
|
|
|
|
service->UpdateReachability();
|
|
|
|
service->SendEvent();
|
|
|
|
}
|