Repository navigation
Expand file tree
/
Copy pathappsmanager.js
More file actions
181 lines (173 loc) · 8.23 KB
/
Copy pathappsmanager.js
File metadata and controls
181 lines (173 loc) · 8.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// KaiOS 3 AppsManager client. Its generated URL encoder differs by firmware:
// some builds accept a string and others a URL object. Inspect the local
// encoder before sending an action, then retry only a proven argument-type
// error. A service failure may mean the action has already started.
const ROOT = 'http://127.0.0.1/api/v1/'
const SCRIPT_PATHS = ['shared/core.js', 'shared/session.js', 'apps/service.js']
const REQUEST_FIELDS = {
installPackage: ['InstallPackageRequest', 'updateUrl'],
checkForUpdate: ['CheckForUpdateRequest', 'updateUrl'],
update: ['UpdateRequest', 'manifestUrl'],
installPwa: ['InstallPwaRequest', 'manifestUrl'],
uninstall: ['UninstallRequest', 'manifestUrl'],
}
let managerPromise = null
const scriptPromises = new Map()
let lastDownloadFailure = null
// AppsManager can reject with an AppsServiceError value rather than an Error.
// Match the JScalated website helper so the phone displays the service code
// instead of alerting an undefined .message.
function describeError(error) {
const value = error?.value
const code = [error, value, value?.error, error?.code, error?.error]
.find(candidate => typeof candidate === 'number')
const names = globalThis.lib_apps?.AppsServiceError
const name = code === undefined ? '' : Object.keys(names || {})
.find(key => names[key] === code) || `code ${code}`
const details = [name, error?.reason, error?.message,
typeof value === 'string' ? value : '']
.filter(part => part !== undefined && part !== null && part !== '')
.map(String)
if (details.length) return [...new Set(details)].join(': ')
if (typeof error === 'string' && error) return error
// if (error && typeof error === 'object') {
try {
const json = JSON.stringify(error)
if (json && json !== '{}') return json
} catch (_) { /* Use the fallback below for cyclic service objects. */ }
return 'AppsManager returned no error details'
}
function errorText(error, includeDownloadFailure = false) {
const detail = describeError(error)
if (!includeDownloadFailure || lastDownloadFailure === null) return detail
const downloadDetail = describeError(lastDownloadFailure)
return downloadDetail === detail ? detail : `${detail}; download failed: ${downloadDetail}`
}
function watchDownloadFailures(manager) {
if (typeof manager?.addEventListener !== 'function' ||
manager.APP_DOWNLOAD_FAILED_EVENT === undefined) return
try {
manager.addEventListener(manager.APP_DOWNLOAD_FAILED_EVENT, event => {
lastDownloadFailure = event?.reason ?? event?.value ?? event
console.warn('[ostore] AppsManager download failed:', event)
})
} catch (error) {
console.warn('[ostore] AppsManager download event unavailable:', error)
}
}
function loadScript(path) {
if (scriptPromises.has(path)) return scriptPromises.get(path)
const promise = new Promise((resolve, reject) => {
const script = document.createElement('script')
const timer = setTimeout(() => reject(new Error(`Timed out loading ${path}`)), 12000)
script.onload = () => { clearTimeout(timer); resolve() }
script.onerror = () => { clearTimeout(timer); reject(new Error(`Could not load ${path}`)) }
script.src = ROOT + path
document.head.appendChild(script)
}).catch(error => {
scriptPromises.delete(path)
throw error
})
scriptPromises.set(path, promise)
return promise
}
async function connect() {
if (managerPromise) return managerPromise
managerPromise = (async () => {
for (const path of SCRIPT_PATHS) await loadScript(path)
if (!globalThis.lib_session?.Session || !globalThis.lib_apps?.AppsManager) {
throw new Error('AppsManager client scripts are unavailable')
}
const token = typeof navigator.b2g?.externalapi?.getToken === 'function'
? await navigator.b2g.externalapi.getToken() : 'secrettoken'
return new Promise((resolve, reject) => {
const session = new globalThis.lib_session.Session()
let settled = false
const timer = setTimeout(() => finish(new Error('AppsManager connection timed out')), 12000)
function finish(error, manager) {
if (settled) return
settled = true
clearTimeout(timer)
if (error) reject(error)
else {
watchDownloadFailures(manager)
resolve(manager)
}
}
try {
session.open('websocket', 'localhost', token, {
onsessionconnected() {
globalThis.lib_apps.AppsManager.get(session).then(
manager => finish(null, manager), finish
)
},
onsessiondisconnected() {
managerPromise = null
finish(new Error('AppsManager session disconnected'))
},
onsessionconnectionerror() {
finish(new Error('AppsManager session connection error'))
},
}, true)
} catch (error) { finish(error) }
})
})().catch(error => {
managerPromise = null
throw error
})
return managerPromise
}
function callUrlMethod(manager, method, rawUrl, rest = []) {
if (typeof manager?.[method] !== 'function') {
return Promise.reject(new Error(`AppsManager.${method} is unavailable`))
}
const address = new URL(String(rawUrl)).href
if (!/^https?:$/.test(new URL(address).protocol)) {
return Promise.reject(new Error('AppsManager needs an HTTP(S) URL'))
}
const request = REQUEST_FIELDS[method]
const codec = request && manager.wrapper_payload_message?.[request[0]]
const extra = method === 'checkForUpdate' ? { appsOption: rest[0] } : {}
const accepts = value => {
if (typeof codec?.encode !== 'function') return false
try {
codec.encode({ [request[1]]: value, ...extra })
return true
} catch (_) { return false }
}
const asObject = new URL(address)
const first = !accepts(address) && accepts(asObject) ? asObject : address
const call = value => Promise.resolve().then(() => manager[method](value, ...rest))
return call(first).catch(error => {
const message = String(error?.message || error || '')
if (typeof first === 'string' &&
/Expected\s+URL\s*,\s*(?:bug\s+|but\s+)?got\s+string/i.test(message)) {
return call(asObject)
}
if (typeof first !== 'string' &&
/Expected\s+string\s*,\s*(?:bug\s+|but\s+)?got\s+(?:object|URL)/i.test(message)) {
return call(address)
}
throw error
})
}
async function probe() { return connect() }
async function getAll() { return (await connect()).getAll() }
async function installPackage(updateUrl) {
lastDownloadFailure = null
return callUrlMethod(await connect(), 'installPackage', updateUrl)
}
async function installPWA(manifestUrl) {
return callUrlMethod(await connect(), 'installPwa', manifestUrl)
}
async function uninstall(manifestUrl) {
return callUrlMethod(await connect(), 'uninstall', manifestUrl)
}
async function updatePackage(updateUrl, manifestUrl) {
lastDownloadFailure = null
const manager = await connect()
const available = await callUrlMethod(manager, 'checkForUpdate', updateUrl, [{ autoInstall: false }])
if (!available) throw new Error('No update is available at this app’s installed update URL')
return callUrlMethod(manager, 'update', manifestUrl)
}
window.AppsManagement = {installPackage}