status.sys.mjs (3223B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 import { 6 CLIENT_NOT_CONFIGURED, 7 ENGINE_SUCCEEDED, 8 LOGIN_FAILED, 9 LOGIN_FAILED_NO_PASSPHRASE, 10 LOGIN_FAILED_NO_USERNAME, 11 LOGIN_SUCCEEDED, 12 STATUS_OK, 13 SYNC_FAILED, 14 SYNC_FAILED_PARTIAL, 15 SYNC_SUCCEEDED, 16 } from "resource://services-sync/constants.sys.mjs"; 17 18 import { Log } from "resource://gre/modules/Log.sys.mjs"; 19 20 import { SyncAuthManager } from "resource://services-sync/sync_auth.sys.mjs"; 21 22 export var Status = { 23 _log: Log.repository.getLogger("Sync.Status"), 24 __authManager: null, 25 ready: false, 26 27 get _authManager() { 28 if (this.__authManager) { 29 return this.__authManager; 30 } 31 this.__authManager = new SyncAuthManager(); 32 return this.__authManager; 33 }, 34 35 get service() { 36 return this._service; 37 }, 38 39 set service(code) { 40 this._log.debug( 41 "Status.service: " + (this._service || undefined) + " => " + code 42 ); 43 this._service = code; 44 }, 45 46 get login() { 47 return this._login; 48 }, 49 50 set login(code) { 51 this._log.debug("Status.login: " + this._login + " => " + code); 52 this._login = code; 53 54 if ( 55 code == LOGIN_FAILED_NO_USERNAME || 56 code == LOGIN_FAILED_NO_PASSPHRASE 57 ) { 58 this.service = CLIENT_NOT_CONFIGURED; 59 } else if (code != LOGIN_SUCCEEDED) { 60 this.service = LOGIN_FAILED; 61 } else { 62 this.service = STATUS_OK; 63 } 64 }, 65 66 get sync() { 67 return this._sync; 68 }, 69 70 set sync(code) { 71 this._log.debug("Status.sync: " + this._sync + " => " + code); 72 this._sync = code; 73 this.service = code == SYNC_SUCCEEDED ? STATUS_OK : SYNC_FAILED; 74 }, 75 76 get engines() { 77 return this._engines; 78 }, 79 80 set engines([name, code]) { 81 this._log.debug("Status for engine " + name + ": " + code); 82 this._engines[name] = code; 83 84 if (code != ENGINE_SUCCEEDED) { 85 this.service = SYNC_FAILED_PARTIAL; 86 } 87 }, 88 89 // Implement toString because adding a logger introduces a cyclic object 90 // value, so we can't trivially debug-print Status as JSON. 91 toString: function toString() { 92 return ( 93 "<Status" + 94 ": login: " + 95 Status.login + 96 ", service: " + 97 Status.service + 98 ", sync: " + 99 Status.sync + 100 ">" 101 ); 102 }, 103 104 checkSetup: function checkSetup() { 105 if (!this._authManager.username) { 106 Status.login = LOGIN_FAILED_NO_USERNAME; 107 Status.service = CLIENT_NOT_CONFIGURED; 108 } else if (Status.login == STATUS_OK) { 109 Status.service = STATUS_OK; 110 } 111 return Status.service; 112 }, 113 114 resetBackoff: function resetBackoff() { 115 this.enforceBackoff = false; 116 this.backoffInterval = 0; 117 this.minimumNextSync = 0; 118 }, 119 120 resetSync: function resetSync() { 121 // Logger setup. 122 this._log.manageLevelFromPref("services.sync.log.logger.status"); 123 124 this._log.info("Resetting Status."); 125 this.service = STATUS_OK; 126 this._login = LOGIN_SUCCEEDED; 127 this._sync = SYNC_SUCCEEDED; 128 this._engines = {}; 129 this.partial = false; 130 }, 131 }; 132 133 // Initialize various status values. 134 Status.resetBackoff(); 135 Status.resetSync();