From aad345a985b987289d75a0e59f91d39f071dcba3 Mon Sep 17 00:00:00 2001 From: sbosse Date: Mon, 21 Jul 2025 23:34:30 +0200 Subject: [PATCH] Mon 21 Jul 22:43:21 CEST 2025 --- .../helpers.js | 65 +++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 js/ui/cordova/plugins/cordova-plugin-chrome-apps-common/helpers.js diff --git a/js/ui/cordova/plugins/cordova-plugin-chrome-apps-common/helpers.js b/js/ui/cordova/plugins/cordova-plugin-chrome-apps-common/helpers.js new file mode 100644 index 0000000..edc1a47 --- /dev/null +++ b/js/ui/cordova/plugins/cordova-plugin-chrome-apps-common/helpers.js @@ -0,0 +1,65 @@ +// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Distinguish between having the plugin loaded vs. being run using chromeapp.html. +var isChromeApp = /chromeapp.html$/.exec(location.href) ? true : false; +var channel = require('cordova/channel'); + +function getChromeAppFlag() { + return isChromeApp; +} + +// TODO: Delete this method if favour of delayDeviceReadyUntil. +exports.runAtStartUp = function(func) { + if (isChromeApp) { + // TODO: Implement done() call back for chrome app case. + var mobile = require('cordova-plugin-chrome-apps-bootstrap.mobile.impl'); + mobile.onBackgroundPageLoaded.subscribe(func); + } else { + channel.onCordovaReady.subscribe(func); + } +}; + +// TODO: Add this to cordova-js. +// Executes func after onCordovaReady fires, and delays deviceready until done() is called. +exports.delayDeviceReadyUntil = function(func) { + channel.onCordovaReady.subscribe(function() { + var newChannel = channel.createSticky('cca-delay-device-ready'); + channel.deviceReadyChannelsArray.push(newChannel); + func(function done() { + newChannel.fire(); + }); + }); +}; + +// For Chrome Apps: +// - Call func after background page is loaded. +// - Prevents onLaunched from being fired +// - func() called immeditately if start-up sequence has already completed. +// For vanilla Cordova: +// - Call func after deviceready has been fired. +// - func() called immeditately if deviceready has already fired. +exports.queueLifeCycleEvent = function(func) { + if (isChromeApp) { + var mobile = require('cordova-plugin-chrome-apps-bootstrap.mobile.impl'); + if (mobile.lifeCycleEventFuncs) { + mobile.lifeCycleEventFuncs.push(func); + } else { + // Initial events have already occurred. + func(); + } + } else { + if (channel.onDeviceReady.state == 1) { + // Fire after deviceready, and within a setTimeout to allow + // user code to register listeners. + channel.onDeviceReady.subscribe(function() { + setTimeout(func, 0); + }); + } else { + func(); + } + } +}; + +exports.__defineGetter__("isChromeApp", getChromeAppFlag);