dom/wifi/StateMachine.jsm

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */
michael@0 3 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
michael@0 5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7 "use strict";
michael@0 8
michael@0 9 const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components;
michael@0 10
michael@0 11 Cu.import("resource://gre/modules/Services.jsm");
michael@0 12
michael@0 13 this.EXPORTED_SYMBOLS = ["StateMachine"];
michael@0 14
michael@0 15 const DEBUG = false;
michael@0 16
michael@0 17 this.StateMachine = function(aDebugTag) {
michael@0 18 function debug(aMsg) {
michael@0 19 dump('-------------- StateMachine:' + aDebugTag + ': ' + aMsg);
michael@0 20 }
michael@0 21
michael@0 22 var sm = {};
michael@0 23
michael@0 24 var _initialState;
michael@0 25 var _curState;
michael@0 26 var _prevState;
michael@0 27 var _paused;
michael@0 28 var _eventQueue = [];
michael@0 29 var _deferredEventQueue = [];
michael@0 30 var _defaultEventHandler;
michael@0 31
michael@0 32 // Public interfaces.
michael@0 33
michael@0 34 sm.setDefaultEventHandler = function(aDefaultEventHandler) {
michael@0 35 _defaultEventHandler = aDefaultEventHandler;
michael@0 36 };
michael@0 37
michael@0 38 sm.start = function(aInitialState) {
michael@0 39 _initialState = aInitialState;
michael@0 40 sm.gotoState(_initialState);
michael@0 41 };
michael@0 42
michael@0 43 sm.sendEvent = function (aEvent) {
michael@0 44 if (!_initialState) {
michael@0 45 if (DEBUG) {
michael@0 46 debug('StateMachine is not running. Call StateMachine.start() first.');
michael@0 47 }
michael@0 48 return;
michael@0 49 }
michael@0 50 _eventQueue.push(aEvent);
michael@0 51 asyncCall(handleFirstEvent);
michael@0 52 };
michael@0 53
michael@0 54 sm.getPreviousState = function() {
michael@0 55 return _prevState;
michael@0 56 };
michael@0 57
michael@0 58 sm.getCurrentState = function() {
michael@0 59 return _curState;
michael@0 60 };
michael@0 61
michael@0 62 // State object maker.
michael@0 63 // @param aName string for this state's name.
michael@0 64 // @param aDelegate object:
michael@0 65 // .handleEvent: required.
michael@0 66 // .enter: called before entering this state (optional).
michael@0 67 // .exit: called before exiting this state (optional).
michael@0 68 sm.makeState = function (aName, aDelegate) {
michael@0 69 if (!aDelegate.handleEvent) {
michael@0 70 throw "handleEvent is a required delegate function.";
michael@0 71 }
michael@0 72 var nop = function() {};
michael@0 73 return {
michael@0 74 name: aName,
michael@0 75 enter: (aDelegate.enter || nop),
michael@0 76 exit: (aDelegate.exit || nop),
michael@0 77 handleEvent: aDelegate.handleEvent
michael@0 78 };
michael@0 79 };
michael@0 80
michael@0 81 sm.deferEvent = function (aEvent) {
michael@0 82 // The definition of a 'deferred event' is:
michael@0 83 // We are not able to handle this event now but after receiving
michael@0 84 // certain event or entering a new state, we might be able to handle
michael@0 85 // it. For example, we couldn't handle CONNECT_EVENT in the
michael@0 86 // diconnecting state. But once we finish doing "disconnecting", we
michael@0 87 // could then handle CONNECT_EVENT!
michael@0 88 //
michael@0 89 // So, the deferred event may be handled in the following cases:
michael@0 90 // 1. Once we entered a new state.
michael@0 91 // 2. Once we handled a regular event.
michael@0 92 if (DEBUG) {
michael@0 93 debug('Deferring event: ' + JSON.stringify(aEvent));
michael@0 94 }
michael@0 95 _deferredEventQueue.push(aEvent);
michael@0 96 };
michael@0 97
michael@0 98 // Goto the new state. If the current state is null, the exit
michael@0 99 // function won't be called.
michael@0 100 sm.gotoState = function (aNewState) {
michael@0 101 if (_curState) {
michael@0 102 if (DEBUG) {
michael@0 103 debug("exiting state: " + _curState.name);
michael@0 104 }
michael@0 105 _curState.exit();
michael@0 106 }
michael@0 107
michael@0 108 _prevState = _curState;
michael@0 109 _curState = aNewState;
michael@0 110
michael@0 111 if (DEBUG) {
michael@0 112 debug("entering state: " + _curState.name);
michael@0 113 }
michael@0 114 _curState.enter();
michael@0 115
michael@0 116 // We are in the new state now. We got a chance to handle the
michael@0 117 // deferred events.
michael@0 118 handleDeferredEvents();
michael@0 119
michael@0 120 sm.resume();
michael@0 121 };
michael@0 122
michael@0 123 // No incoming event will be handled after you call pause().
michael@0 124 // (But they will be queued.)
michael@0 125 sm.pause = function() {
michael@0 126 _paused = true;
michael@0 127 };
michael@0 128
michael@0 129 // Continue to handle incoming events.
michael@0 130 sm.resume = function() {
michael@0 131 _paused = false;
michael@0 132 asyncCall(handleFirstEvent);
michael@0 133 };
michael@0 134
michael@0 135 //----------------------------------------------------------
michael@0 136 // Private stuff
michael@0 137 //----------------------------------------------------------
michael@0 138
michael@0 139 function asyncCall(f) {
michael@0 140 Services.tm.currentThread.dispatch(f, Ci.nsIThread.DISPATCH_NORMAL);
michael@0 141 }
michael@0 142
michael@0 143 function handleFirstEvent() {
michael@0 144 var hadDeferredEvents;
michael@0 145
michael@0 146 if (0 === _eventQueue.length) {
michael@0 147 return;
michael@0 148 }
michael@0 149
michael@0 150 if (_paused) {
michael@0 151 return; // The state machine is paused now.
michael@0 152 }
michael@0 153
michael@0 154 hadDeferredEvents = _deferredEventQueue.length > 0;
michael@0 155
michael@0 156 handleOneEvent(_eventQueue.shift()); // The handler may defer this event.
michael@0 157
michael@0 158 // We've handled one event. If we had deferred events before, now is
michael@0 159 // a good chance to handle them.
michael@0 160 if (hadDeferredEvents) {
michael@0 161 handleDeferredEvents();
michael@0 162 }
michael@0 163
michael@0 164 // Continue to handle the next regular event.
michael@0 165 handleFirstEvent();
michael@0 166 }
michael@0 167
michael@0 168 function handleDeferredEvents() {
michael@0 169 if (_deferredEventQueue.length && DEBUG) {
michael@0 170 debug('Handle deferred events: ' + _deferredEventQueue.length);
michael@0 171 }
michael@0 172 for (let i = 0; i < _deferredEventQueue.length; i++) {
michael@0 173 handleOneEvent(_deferredEventQueue.shift());
michael@0 174 }
michael@0 175 }
michael@0 176
michael@0 177 function handleOneEvent(aEvent)
michael@0 178 {
michael@0 179 if (DEBUG) {
michael@0 180 debug('Handling event: ' + JSON.stringify(aEvent));
michael@0 181 }
michael@0 182
michael@0 183 var handled = _curState.handleEvent(aEvent);
michael@0 184
michael@0 185 if (undefined === handled) {
michael@0 186 throw "handleEvent returns undefined: " + _curState.name;
michael@0 187 }
michael@0 188 if (!handled) {
michael@0 189 // Event is not handled in the current state. Try handleEventCommon().
michael@0 190 handled = (_defaultEventHandler ? _defaultEventHandler(aEvent) : handled);
michael@0 191 }
michael@0 192 if (undefined === handled) {
michael@0 193 throw "handleEventCommon returns undefined: " + _curState.name;
michael@0 194 }
michael@0 195 if (!handled) {
michael@0 196 if (DEBUG) {
michael@0 197 debug('!!!!!!!!! FIXME !!!!!!!!! Event not handled: ' + JSON.stringify(aEvent));
michael@0 198 }
michael@0 199 }
michael@0 200
michael@0 201 return handled;
michael@0 202 }
michael@0 203
michael@0 204 return sm;
michael@0 205 };

mercurial