Bug 1018320 - RequestSync API - patch 2 - Wifi Only, r=ehsan

This commit is contained in:
Andrea Marchesini 2015-01-13 09:53:16 +00:00
parent 4585c23b29
commit 9265f36379
5 changed files with 204 additions and 24 deletions

View File

@ -4,10 +4,6 @@
'use strict'
/* TODO:
- wifi
*/
const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components;
function debug(s) {
@ -58,6 +54,8 @@ this.RequestSyncService = {
_registrations: {},
_wifi: false,
// Initialization of the RequestSyncService.
init: function() {
debug("init");
@ -68,6 +66,7 @@ this.RequestSyncService = {
Services.obs.addObserver(this, 'xpcom-shutdown', false);
Services.obs.addObserver(this, 'webapps-clear-data', false);
Services.obs.addObserver(this, 'wifi-state-changed', false);
this.initDBHelper("requestSync", RSYNCDB_VERSION, [RSYNCDB_NAME]);
@ -103,15 +102,16 @@ this.RequestSyncService = {
Services.obs.removeObserver(this, 'xpcom-shutdown');
Services.obs.removeObserver(this, 'webapps-clear-data');
Services.obs.removeObserver(this, 'wifi-state-changed');
this.close();
// Removing all the registrations will delete the pending timers.
for (let key in this._registrations) {
for (let task in this._registrations[key]) {
this.removeRegistrationInternal(task, key);
}
}
let self = this;
this.forEachRegistration(function(aObj) {
let key = self.principalToKey(aObj.principal);
self.removeRegistrationInternal(aObj.data.task, key);
});
},
observe: function(aSubject, aTopic, aData) {
@ -123,8 +123,12 @@ this.RequestSyncService = {
break;
case 'webapps-clear-data':
this.clearData(aSubject);
break;
this.clearData(aSubject);
break;
case 'wifi-state-changed':
this.wifiStateChanged(aSubject == 'enabled');
break;
default:
debug("Wrong observer topic: " + aTopic);
@ -444,12 +448,10 @@ this.RequestSyncService = {
debug("managerRegistrations");
let results = [];
for (var key in this._registrations) {
for (var task in this._registrations[key]) {
results.push(
this.createFullTaskObject(this._registrations[key][task].data));
}
}
let self = this;
this.forEachRegistration(function(aObj) {
results.push(self.createFullTaskObject(aObj.data));
});
aTarget.sendAsyncMessage("RequestSyncManager:Registrations:Return",
{ requestID: aData.requestID,
@ -488,14 +490,21 @@ this.RequestSyncService = {
debug("scheduleTimer");
// A registration can be already inactive if it was 1 shot.
if (aObj.active) {
aObj.timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
let self = this;
aObj.timer.initWithCallback(function() { self.timeout(aObj); },
aObj.data.minInterval * 1000,
Ci.nsITimer.TYPE_ONE_SHOT);
if (!aObj.active) {
return;
}
// WifiOnly check.
if (aObj.data.wifiOnly && !this._wifi) {
return;
}
aObj.timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
let self = this;
aObj.timer.initWithCallback(function() { self.timeout(aObj); },
aObj.data.minInterval * 1000,
Ci.nsITimer.TYPE_ONE_SHOT);
},
timeout: function(aObj) {
@ -617,6 +626,49 @@ this.RequestSyncService = {
self.pendingOperationDone();
aErrorCb();
});
},
forEachRegistration: function(aCb) {
// This method is used also to remove registations from the map, so we have
// to make a new list and let _registations free to be used.
let list = [];
for (var key in this._registrations) {
for (var task in this._registrations[key]) {
list.push(this._registrations[key][task]);
}
}
for (var i = 0; i < list.length; ++i) {
aCb(list[i]);
}
},
wifiStateChanged: function(aEnabled) {
debug("onWifiStateChanged");
this._wifi = aEnabled;
if (!this._wifi) {
// Disable all the wifiOnly tasks.
this.forEachRegistration(function(aObj) {
if (aObj.data.wifiOnly && aObj.timer) {
aObj.timer.cancel();
aObj.timer = null;
}
});
return;
}
// Enable all the tasks.
let self = this;
this.forEachRegistration(function(aObj) {
if (aObj.active && !aObj.timer) {
if (!aObj.data.wifiOnly) {
dump("ERROR - Found a disabled task that is not wifiOnly.");
}
self.scheduleTimer(aObj);
}
});
}
}

View File

@ -0,0 +1,67 @@
/* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */
/* vim: set ts=2 et sw=2 tw=80: */
/* 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/. */
#include "RequestSyncWifiService.h"
#include "mozilla/ClearOnShutdown.h"
#include "mozilla/Services.h"
#include "mozilla/StaticPtr.h"
#include "nsIObserverService.h"
namespace mozilla {
namespace dom {
using namespace hal;
NS_IMPL_ISUPPORTS0(RequestSyncWifiService)
namespace {
StaticRefPtr<RequestSyncWifiService> sService;
} // anonymous namespace
/* static */ void
RequestSyncWifiService::Init()
{
nsRefPtr<RequestSyncWifiService> service = GetInstance();
if (!service) {
NS_WARNING("Failed to initialize RequestSyncWifiService.");
}
}
/* static */ already_AddRefed<RequestSyncWifiService>
RequestSyncWifiService::GetInstance()
{
if (!sService) {
sService = new RequestSyncWifiService();
hal::RegisterNetworkObserver(sService);
ClearOnShutdown(&sService);
}
nsRefPtr<RequestSyncWifiService> service = sService.get();
return service.forget();
}
void
RequestSyncWifiService::Notify(const hal::NetworkInformation& aNetworkInfo)
{
bool isWifi = aNetworkInfo.isWifi();
if (isWifi == mIsWifi) {
return;
}
mIsWifi = isWifi;
nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService();
if (obs) {
obs->NotifyObservers(nullptr, "wifi-state-changed",
mIsWifi ? MOZ_UTF16("enabled") :
MOZ_UTF16("disabled"));
}
}
} // dom namespace
} // mozilla namespace

View File

@ -0,0 +1,43 @@
/* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */
/* vim: set ts=2 et sw=2 tw=80: */
/* 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/. */
#ifndef mozilla_dom_RequestSyncWifiService_h
#define mozilla_dom_RequestSyncWifiService_h
#include "mozilla/dom/network/Types.h"
#include "mozilla/Hal.h"
#include "nsIObserver.h"
namespace mozilla {
namespace dom {
class RequestSyncWifiService MOZ_FINAL : public nsISupports
, public NetworkObserver
{
public:
NS_DECL_ISUPPORTS
static void Init();
static already_AddRefed<RequestSyncWifiService> GetInstance();
void Notify(const hal::NetworkInformation& aNetworkInfo);
private:
RequestSyncWifiService()
: mIsWifi(false)
{}
~RequestSyncWifiService()
{}
bool mIsWifi;
};
} // dom namespace
} // mozilla namespace
#endif // mozilla_dom_RequestSyncWifiService_h

View File

@ -6,6 +6,10 @@
MOCHITEST_MANIFESTS += ['tests/mochitest.ini']
EXPORTS.mozilla.dom += [
'RequestSyncWifiService.h',
]
EXTRA_COMPONENTS += [
'RequestSync.manifest',
'RequestSyncManager.js',
@ -15,3 +19,12 @@ EXTRA_COMPONENTS += [
EXTRA_JS_MODULES += [
'RequestSyncService.jsm',
]
SOURCES += [
'RequestSyncWifiService.cpp',
]
include('/ipc/chromium/chromium-config.mozbuild')
FAIL_ON_WARNINGS = True
FINAL_LIBRARY = 'xul'

View File

@ -64,6 +64,7 @@
#include "ActiveLayerTracker.h"
#include "CounterStyleManager.h"
#include "FrameLayerBuilder.h"
#include "mozilla/dom/RequestSyncWifiService.h"
#include "AudioChannelService.h"
#include "mozilla/dom/DataStoreService.h"
@ -300,6 +301,10 @@ nsLayoutStatics::Initialize()
IMEStateManager::Init();
#ifdef MOZ_B2G
RequestSyncWifiService::Init();
#endif
return NS_OK;
}