1 // Copyright 2015-present 650 Industries. All rights reserved.
2 package host.exp.exponent
3 
4 import android.content.Context
5 import android.net.Uri
6 import android.os.Build
7 import android.util.Log
8 import expo.modules.jsonutils.getNullable
9 import expo.modules.updates.UpdatesConfiguration
10 import expo.modules.updates.UpdatesUtils
11 import expo.modules.updates.db.DatabaseHolder
12 import expo.modules.updates.db.entity.UpdateEntity
13 import expo.modules.updates.launcher.Launcher
14 import expo.modules.updates.launcher.NoDatabaseLauncher
15 import expo.modules.updates.loader.EmbeddedLoader
16 import expo.modules.updates.loader.FileDownloader
17 import expo.modules.updates.loader.LoaderTask
18 import expo.modules.updates.loader.LoaderTask.BackgroundUpdateStatus
19 import expo.modules.updates.loader.LoaderTask.LoaderTaskCallback
20 import expo.modules.updates.manifest.UpdateManifest
21 import expo.modules.manifests.core.Manifest
22 import expo.modules.updates.selectionpolicy.LauncherSelectionPolicyFilterAware
23 import expo.modules.updates.selectionpolicy.LoaderSelectionPolicyFilterAware
24 import expo.modules.updates.selectionpolicy.ReaperSelectionPolicyDevelopmentClient
25 import expo.modules.updates.selectionpolicy.SelectionPolicy
26 import host.exp.exponent.di.NativeModuleDepsProvider
27 import host.exp.exponent.exceptions.ManifestException
28 import host.exp.exponent.kernel.ExperienceKey
29 import host.exp.exponent.kernel.ExpoViewKernel
30 import host.exp.exponent.kernel.Kernel
31 import host.exp.exponent.kernel.KernelConfig
32 import host.exp.exponent.storage.ExponentSharedPreferences
33 import org.json.JSONArray
34 import org.json.JSONException
35 import org.json.JSONObject
36 import java.io.File
37 import java.util.*
38 import javax.inject.Inject
39 
40 private const val UPDATE_AVAILABLE_EVENT = "updateAvailable"
41 private const val UPDATE_NO_UPDATE_AVAILABLE_EVENT = "noUpdateAvailable"
42 private const val UPDATE_ERROR_EVENT = "error"
43 
44 class ExpoUpdatesAppLoader @JvmOverloads constructor(
45   private val manifestUrl: String,
46   private val callback: AppLoaderCallback,
47   private val useCacheOnly: Boolean = false
48 ) {
49   @Inject
50   lateinit var exponentManifest: ExponentManifest
51 
52   @Inject
53   lateinit var exponentSharedPreferences: ExponentSharedPreferences
54 
55   @Inject
56   lateinit var databaseHolder: DatabaseHolder
57 
58   @Inject
59   lateinit var kernel: Kernel
60 
61   enum class AppLoaderStatus {
62     CHECKING_FOR_UPDATE, DOWNLOADING_NEW_UPDATE
63   }
64 
65   var isEmergencyLaunch = false
66     private set
67   var isUpToDate = true
68     private set
69   var status: AppLoaderStatus? = null
70     private set
71   var shouldShowAppLoaderStatus = true
72     private set
73   private var isStarted = false
74 
75   interface AppLoaderCallback {
76     fun onOptimisticManifest(optimisticManifest: Manifest)
77     fun onManifestCompleted(manifest: Manifest)
78     fun onBundleCompleted(localBundlePath: String?)
79     fun emitEvent(params: JSONObject)
80     fun updateStatus(status: AppLoaderStatus)
81     fun onError(e: Exception)
82   }
83 
84   lateinit var updatesConfiguration: UpdatesConfiguration
85     private set
86 
87   lateinit var updatesDirectory: File
88     private set
89 
90   lateinit var selectionPolicy: SelectionPolicy
91     private set
92 
93   lateinit var fileDownloader: FileDownloader
94     private set
95 
96   lateinit var launcher: Launcher
97     private set
98 
99   private fun updateStatus(status: AppLoaderStatus) {
100     this.status = status
101     callback.updateStatus(status)
102   }
103 
104   fun start(context: Context) {
105     check(!isStarted) { "AppLoader for $manifestUrl was started twice. AppLoader.start() may only be called once per instance." }
106     isStarted = true
107     status = AppLoaderStatus.CHECKING_FOR_UPDATE
108     fileDownloader = FileDownloader(context)
109     kernel.addAppLoaderForManifestUrl(manifestUrl, this)
110     val httpManifestUrl = exponentManifest.httpManifestUrl(manifestUrl)
111     var releaseChannel = Constants.RELEASE_CHANNEL
112     if (!Constants.isStandaloneApp()) {
113       // in Expo Go, the release channel can change at runtime depending on the URL we load
114       val releaseChannelQueryParam =
115         httpManifestUrl.getQueryParameter(ExponentManifest.QUERY_PARAM_KEY_RELEASE_CHANNEL)
116       if (releaseChannelQueryParam != null) {
117         releaseChannel = releaseChannelQueryParam
118       }
119     }
120     val configMap = mutableMapOf<String, Any>()
121     configMap[UpdatesConfiguration.UPDATES_CONFIGURATION_UPDATE_URL_KEY] = httpManifestUrl
122     configMap[UpdatesConfiguration.UPDATES_CONFIGURATION_SCOPE_KEY_KEY] = httpManifestUrl.toString()
123     configMap[UpdatesConfiguration.UPDATES_CONFIGURATION_SDK_VERSION_KEY] = Constants.SDK_VERSIONS
124     configMap[UpdatesConfiguration.UPDATES_CONFIGURATION_RELEASE_CHANNEL_KEY] = releaseChannel
125     configMap[UpdatesConfiguration.UPDATES_CONFIGURATION_HAS_EMBEDDED_UPDATE_KEY] = Constants.isStandaloneApp()
126     configMap[UpdatesConfiguration.UPDATES_CONFIGURATION_ENABLED_KEY] = Constants.ARE_REMOTE_UPDATES_ENABLED
127     if (useCacheOnly) {
128       configMap[UpdatesConfiguration.UPDATES_CONFIGURATION_CHECK_ON_LAUNCH_KEY] = "NEVER"
129       configMap[UpdatesConfiguration.UPDATES_CONFIGURATION_LAUNCH_WAIT_MS_KEY] = 0
130     } else {
131       if (Constants.isStandaloneApp()) {
132         configMap[UpdatesConfiguration.UPDATES_CONFIGURATION_CHECK_ON_LAUNCH_KEY] = if (Constants.UPDATES_CHECK_AUTOMATICALLY) "ALWAYS" else "NEVER"
133         configMap[UpdatesConfiguration.UPDATES_CONFIGURATION_LAUNCH_WAIT_MS_KEY] = Constants.UPDATES_FALLBACK_TO_CACHE_TIMEOUT
134       } else {
135         configMap[UpdatesConfiguration.UPDATES_CONFIGURATION_CHECK_ON_LAUNCH_KEY] = "ALWAYS"
136         configMap[UpdatesConfiguration.UPDATES_CONFIGURATION_LAUNCH_WAIT_MS_KEY] = 60000
137       }
138     }
139     configMap[UpdatesConfiguration.UPDATES_CONFIGURATION_REQUEST_HEADERS_KEY] = requestHeaders
140     configMap["expectsSignedManifest"] = true
141     val configuration = UpdatesConfiguration().loadValuesFromMap(configMap)
142     val sdkVersionsList = mutableListOf<String>().apply {
143       addAll(Constants.SDK_VERSIONS_LIST)
144       add(RNObject.UNVERSIONED)
145       for (sdkVersion in Constants.SDK_VERSIONS_LIST) {
146         add("exposdk:$sdkVersion")
147       }
148     }
149     val selectionPolicy = SelectionPolicy(
150       LauncherSelectionPolicyFilterAware(sdkVersionsList),
151       LoaderSelectionPolicyFilterAware(),
152       ReaperSelectionPolicyDevelopmentClient()
153     )
154     val directory: File = try {
155       UpdatesUtils.getOrCreateUpdatesDirectory(context)
156     } catch (e: Exception) {
157       callback.onError(e)
158       return
159     }
160     startLoaderTask(configuration, directory, selectionPolicy, context)
161   }
162 
163   private fun startLoaderTask(
164     configuration: UpdatesConfiguration,
165     directory: File,
166     selectionPolicy: SelectionPolicy,
167     context: Context
168   ) {
169     updatesConfiguration = configuration
170     updatesDirectory = directory
171     this.selectionPolicy = selectionPolicy
172     if (!configuration.isEnabled) {
173       launchWithNoDatabase(context, null)
174       return
175     }
176     LoaderTask(
177       configuration,
178       databaseHolder,
179       directory,
180       fileDownloader,
181       selectionPolicy,
182       object : LoaderTaskCallback {
183         private var didAbort = false
184         override fun onFailure(e: Exception) {
185           if (Constants.isStandaloneApp()) {
186             isEmergencyLaunch = true
187             launchWithNoDatabase(context, e)
188           } else {
189             if (didAbort) {
190               return
191             }
192             var exception = e
193             try {
194               val errorJson = JSONObject(e.message!!)
195               exception = ManifestException(e, manifestUrl, errorJson)
196             } catch (ex: Exception) {
197               // do nothing, expected if the error payload does not come from a conformant server
198             }
199             callback.onError(exception)
200           }
201         }
202 
203         override fun onCachedUpdateLoaded(update: UpdateEntity): Boolean {
204           val manifest = Manifest.fromManifestJson(update.manifest)
205           setShouldShowAppLoaderStatus(manifest)
206           if (manifest.isUsingDeveloperTool()) {
207             return false
208           } else {
209             try {
210               val experienceKey = ExperienceKey.fromManifest(manifest)
211               // if previous run of this app failed due to a loading error, we want to make sure to check for remote updates
212               val experienceMetadata = exponentSharedPreferences.getExperienceMetadata(experienceKey)
213               if (experienceMetadata != null && experienceMetadata.optBoolean(
214                   ExponentSharedPreferences.EXPERIENCE_METADATA_LOADING_ERROR
215                 )
216               ) {
217                 return false
218               }
219             } catch (e: Exception) {
220               return true
221             }
222           }
223           return true
224         }
225 
226         override fun onRemoteUpdateManifestLoaded(updateManifest: UpdateManifest) {
227           // expo-cli does not always respect our SDK version headers and respond with a compatible update or an error
228           // so we need to check the compatibility here
229           val sdkVersion = updateManifest.manifest.getSDKVersion()
230           if (!isValidSdkVersion(sdkVersion)) {
231             callback.onError(formatExceptionForIncompatibleSdk(sdkVersion ?: "null"))
232             didAbort = true
233             return
234           }
235           setShouldShowAppLoaderStatus(updateManifest.manifest)
236           callback.onOptimisticManifest(updateManifest.manifest)
237           updateStatus(AppLoaderStatus.DOWNLOADING_NEW_UPDATE)
238         }
239 
240         override fun onSuccess(launcher: Launcher, isUpToDate: Boolean) {
241           if (didAbort) {
242             return
243           }
244           this@ExpoUpdatesAppLoader.launcher = launcher
245           this@ExpoUpdatesAppLoader.isUpToDate = isUpToDate
246           try {
247             val manifestJson = processManifestJson(launcher.launchedUpdate!!.manifest)
248             val manifest = Manifest.fromManifestJson(manifestJson)
249             callback.onManifestCompleted(manifest)
250 
251             // ReactAndroid will load the bundle on its own in development mode
252             if (!manifest.isDevelopmentMode()) {
253               callback.onBundleCompleted(launcher.launchAssetFile)
254             }
255           } catch (e: Exception) {
256             callback.onError(e)
257           }
258         }
259 
260         override fun onBackgroundUpdateFinished(
261           status: BackgroundUpdateStatus,
262           update: UpdateEntity?,
263           exception: Exception?
264         ) {
265           if (didAbort) {
266             return
267           }
268           try {
269             val jsonParams = JSONObject()
270             when (status) {
271               BackgroundUpdateStatus.ERROR -> {
272                 if (exception == null) {
273                   throw AssertionError("Background update with error status must have a nonnull exception object")
274                 }
275                 jsonParams.put("type", UPDATE_ERROR_EVENT)
276                 jsonParams.put("message", exception.message)
277               }
278               BackgroundUpdateStatus.UPDATE_AVAILABLE -> {
279                 if (update == null) {
280                   throw AssertionError("Background update with error status must have a nonnull update object")
281                 }
282                 jsonParams.put("type", UPDATE_AVAILABLE_EVENT)
283                 jsonParams.put("manifestString", update.manifest.toString())
284               }
285               BackgroundUpdateStatus.NO_UPDATE_AVAILABLE -> {
286                 jsonParams.put("type", UPDATE_NO_UPDATE_AVAILABLE_EVENT)
287               }
288             }
289             callback.emitEvent(jsonParams)
290           } catch (e: Exception) {
291             Log.e(TAG, "Failed to emit event to JS", e)
292           }
293         }
294       }
295     ).start(context)
296   }
297 
298   private fun launchWithNoDatabase(context: Context, e: Exception?) {
299     this.launcher = NoDatabaseLauncher(context, updatesConfiguration, e)
300     var manifestJson = EmbeddedLoader.readEmbeddedManifest(context, updatesConfiguration)!!.manifest.getRawJson()
301     try {
302       manifestJson = processManifestJson(manifestJson)
303     } catch (ex: Exception) {
304       Log.e(
305         TAG,
306         "Failed to process manifest; attempting to launch with raw manifest. This may cause errors or unexpected behavior.",
307         e
308       )
309     }
310     callback.onManifestCompleted(Manifest.fromManifestJson(manifestJson))
311     var launchAssetFile = launcher.launchAssetFile
312     if (launchAssetFile == null) {
313       // ReactInstanceManagerBuilder accepts embedded assets as strings with "assets://" prefixed
314       launchAssetFile = "assets://" + launcher.bundleAssetName
315     }
316     callback.onBundleCompleted(launchAssetFile)
317   }
318 
319   @Throws(JSONException::class)
320   private fun processManifestJson(manifestJson: JSONObject): JSONObject {
321     val parsedManifestUrl = Uri.parse(manifestUrl)
322     if (!manifestJson.optBoolean(ExponentManifest.MANIFEST_IS_VERIFIED_KEY, false) &&
323       isThirdPartyHosted(parsedManifestUrl) &&
324       !Constants.isStandaloneApp()
325     ) {
326       // Sandbox third party apps and consider them verified
327       // for https urls, sandboxed id is of form quinlanj.github.io/myProj-myApp
328       // for http urls, sandboxed id is of form UNVERIFIED-quinlanj.github.io/myProj-myApp
329       val protocol = parsedManifestUrl.scheme
330       val securityPrefix = if (protocol == "https" || protocol == "exps") "" else "UNVERIFIED-"
331       val path = if (parsedManifestUrl.path != null) parsedManifestUrl.path else ""
332       val slug = manifestJson.getNullable<String>(ExponentManifest.MANIFEST_SLUG) ?: ""
333       val sandboxedId = securityPrefix + parsedManifestUrl.host + path + "-" + slug
334       manifestJson.put(ExponentManifest.MANIFEST_ID_KEY, sandboxedId)
335       manifestJson.put(ExponentManifest.MANIFEST_IS_VERIFIED_KEY, true)
336     }
337     if (Constants.isStandaloneApp()) {
338       manifestJson.put(ExponentManifest.MANIFEST_IS_VERIFIED_KEY, true)
339     }
340     if (!manifestJson.has(ExponentManifest.MANIFEST_IS_VERIFIED_KEY)) {
341       manifestJson.put(ExponentManifest.MANIFEST_IS_VERIFIED_KEY, false)
342     }
343     if (!manifestJson.optBoolean(ExponentManifest.MANIFEST_IS_VERIFIED_KEY, false) &&
344       exponentManifest.isAnonymousExperience(Manifest.fromManifestJson(manifestJson))
345     ) {
346       // automatically verified
347       manifestJson.put(ExponentManifest.MANIFEST_IS_VERIFIED_KEY, true)
348     }
349     return manifestJson
350   }
351 
352   private fun isThirdPartyHosted(uri: Uri): Boolean {
353     val host = uri.host
354     return !(
355       host == "exp.host" || host == "expo.io" || host == "exp.direct" || host == "expo.test" ||
356         host!!.endsWith(".exp.host") || host.endsWith(".expo.io") || host.endsWith(".exp.direct") || host.endsWith(
357         ".expo.test"
358       )
359       )
360   }
361 
362   private fun setShouldShowAppLoaderStatus(manifest: Manifest) {
363     // we don't want to show the cached experience alert when Updates.reloadAsync() is called
364     if (useCacheOnly) {
365       shouldShowAppLoaderStatus = false
366       return
367     }
368     shouldShowAppLoaderStatus = !manifest.isDevelopmentSilentLaunch()
369   }
370 
371   // XDL expects the full "exponent-" header names
372   private val requestHeaders: Map<String, String?>
373     get() {
374       val headers = mutableMapOf<String, String>()
375       headers["Expo-Updates-Environment"] = clientEnvironment
376       headers["Expo-Client-Environment"] = clientEnvironment
377       val versionName = ExpoViewKernel.instance.versionName
378       if (versionName != null) {
379         headers["Exponent-Version"] = versionName
380       }
381       val sessionSecret = exponentSharedPreferences.sessionSecret
382       if (sessionSecret != null) {
383         headers["Expo-Session"] = sessionSecret
384       }
385 
386       // XDL expects the full "exponent-" header names
387       headers["Exponent-Accept-Signature"] = "true"
388       headers["Exponent-Platform"] = "android"
389       if (KernelConfig.FORCE_UNVERSIONED_PUBLISHED_EXPERIENCES) {
390         headers["Exponent-SDK-Version"] = "UNVERSIONED"
391       } else {
392         headers["Exponent-SDK-Version"] = Constants.SDK_VERSIONS
393       }
394       return headers
395     }
396 
397   private val clientEnvironment: String
398     get() = if (Constants.isStandaloneApp()) {
399       "STANDALONE"
400     } else if (Build.FINGERPRINT.contains("vbox") || Build.FINGERPRINT.contains("generic")) {
401       "EXPO_SIMULATOR"
402     } else {
403       "EXPO_DEVICE"
404     }
405 
406   private fun isValidSdkVersion(sdkVersion: String?): Boolean {
407     if (sdkVersion == null) {
408       return false
409     }
410     if (RNObject.UNVERSIONED == sdkVersion) {
411       return true
412     }
413     for (version in Constants.SDK_VERSIONS_LIST) {
414       if (version == sdkVersion) {
415         return true
416       }
417     }
418     return false
419   }
420 
421   private fun formatExceptionForIncompatibleSdk(sdkVersion: String): ManifestException {
422     val errorJson = JSONObject()
423     try {
424       errorJson.put("message", "Invalid SDK version")
425       if (ABIVersion.toNumber(sdkVersion) > ABIVersion.toNumber(Constants.SDK_VERSIONS_LIST[0])) {
426         errorJson.put("errorCode", "EXPERIENCE_SDK_VERSION_TOO_NEW")
427       } else {
428         errorJson.put("errorCode", "EXPERIENCE_SDK_VERSION_OUTDATED")
429         errorJson.put(
430           "metadata",
431           JSONObject().put(
432             "availableSDKVersions",
433             JSONArray().put(sdkVersion)
434           )
435         )
436       }
437     } catch (e: Exception) {
438       Log.e(TAG, "Failed to format error message for incompatible SDK version", e)
439     }
440     return ManifestException(Exception("Incompatible SDK version"), manifestUrl, errorJson)
441   }
442 
443   companion object {
444     private val TAG = ExpoUpdatesAppLoader::class.java.simpleName
445     const val UPDATES_EVENT_NAME = "Expo.nativeUpdatesEvent"
446   }
447 
448   init {
449     NativeModuleDepsProvider.instance.inject(ExpoUpdatesAppLoader::class.java, this)
450   }
451 }
452