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.updates.UpdatesConfiguration
9 import expo.modules.updates.UpdatesUtils
10 import expo.modules.updates.db.DatabaseHolder
11 import expo.modules.updates.db.entity.UpdateEntity
12 import expo.modules.updates.launcher.Launcher
13 import expo.modules.updates.launcher.NoDatabaseLauncher
14 import expo.modules.updates.loader.EmbeddedLoader
15 import expo.modules.updates.loader.FileDownloader
16 import expo.modules.updates.loader.LoaderTask
17 import expo.modules.updates.loader.LoaderTask.BackgroundUpdateStatus
18 import expo.modules.updates.loader.LoaderTask.LoaderTaskCallback
19 import expo.modules.updates.manifest.UpdateManifest
20 import expo.modules.updates.manifest.ManifestFactory
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 = ManifestFactory.getManifestFromManifestJson(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.getSDKVersionNullable()
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 = ManifestFactory.getManifestFromManifestJson(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(ManifestFactory.getManifestFromManifestJson(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 = if (manifestJson.has(ExponentManifest.MANIFEST_SLUG)) manifestJson.getString(
333         ExponentManifest.MANIFEST_SLUG
334       ) else ""
335       val sandboxedId = securityPrefix + parsedManifestUrl.host + path + "-" + slug
336       manifestJson.put(ExponentManifest.MANIFEST_ID_KEY, sandboxedId)
337       manifestJson.put(ExponentManifest.MANIFEST_IS_VERIFIED_KEY, true)
338     }
339     if (Constants.isStandaloneApp()) {
340       manifestJson.put(ExponentManifest.MANIFEST_IS_VERIFIED_KEY, true)
341     }
342     if (!manifestJson.has(ExponentManifest.MANIFEST_IS_VERIFIED_KEY)) {
343       manifestJson.put(ExponentManifest.MANIFEST_IS_VERIFIED_KEY, false)
344     }
345     if (!manifestJson.optBoolean(ExponentManifest.MANIFEST_IS_VERIFIED_KEY, false) &&
346       exponentManifest.isAnonymousExperience(ManifestFactory.getManifestFromManifestJson(manifestJson))
347     ) {
348       // automatically verified
349       manifestJson.put(ExponentManifest.MANIFEST_IS_VERIFIED_KEY, true)
350     }
351     return manifestJson
352   }
353 
354   private fun isThirdPartyHosted(uri: Uri): Boolean {
355     val host = uri.host
356     return !(
357       host == "exp.host" || host == "expo.io" || host == "exp.direct" || host == "expo.test" ||
358         host!!.endsWith(".exp.host") || host.endsWith(".expo.io") || host.endsWith(".exp.direct") || host.endsWith(
359         ".expo.test"
360       )
361       )
362   }
363 
364   private fun setShouldShowAppLoaderStatus(manifest: Manifest) {
365     // we don't want to show the cached experience alert when Updates.reloadAsync() is called
366     if (useCacheOnly) {
367       shouldShowAppLoaderStatus = false
368       return
369     }
370     shouldShowAppLoaderStatus = !manifest.isDevelopmentSilentLaunch()
371   }
372 
373   // XDL expects the full "exponent-" header names
374   private val requestHeaders: Map<String, String?>
375     get() {
376       val headers = mutableMapOf<String, String>()
377       headers["Expo-Updates-Environment"] = clientEnvironment
378       headers["Expo-Client-Environment"] = clientEnvironment
379       val versionName = ExpoViewKernel.instance.versionName
380       if (versionName != null) {
381         headers["Exponent-Version"] = versionName
382       }
383       val sessionSecret = exponentSharedPreferences.sessionSecret
384       if (sessionSecret != null) {
385         headers["Expo-Session"] = sessionSecret
386       }
387 
388       // XDL expects the full "exponent-" header names
389       headers["Exponent-Accept-Signature"] = "true"
390       headers["Exponent-Platform"] = "android"
391       if (KernelConfig.FORCE_UNVERSIONED_PUBLISHED_EXPERIENCES) {
392         headers["Exponent-SDK-Version"] = "UNVERSIONED"
393       } else {
394         headers["Exponent-SDK-Version"] = Constants.SDK_VERSIONS
395       }
396       return headers
397     }
398 
399   private val clientEnvironment: String
400     get() = if (Constants.isStandaloneApp()) {
401       "STANDALONE"
402     } else if (Build.FINGERPRINT.contains("vbox") || Build.FINGERPRINT.contains("generic")) {
403       "EXPO_SIMULATOR"
404     } else {
405       "EXPO_DEVICE"
406     }
407 
408   private fun isValidSdkVersion(sdkVersion: String?): Boolean {
409     if (sdkVersion == null) {
410       return false
411     }
412     if (RNObject.UNVERSIONED == sdkVersion) {
413       return true
414     }
415     for (version in Constants.SDK_VERSIONS_LIST) {
416       if (version == sdkVersion) {
417         return true
418       }
419     }
420     return false
421   }
422 
423   private fun formatExceptionForIncompatibleSdk(sdkVersion: String): ManifestException {
424     val errorJson = JSONObject()
425     try {
426       errorJson.put("message", "Invalid SDK version")
427       if (ABIVersion.toNumber(sdkVersion) > ABIVersion.toNumber(Constants.SDK_VERSIONS_LIST[0])) {
428         errorJson.put("errorCode", "EXPERIENCE_SDK_VERSION_TOO_NEW")
429       } else {
430         errorJson.put("errorCode", "EXPERIENCE_SDK_VERSION_OUTDATED")
431         errorJson.put(
432           "metadata",
433           JSONObject().put(
434             "availableSDKVersions",
435             JSONArray().put(sdkVersion)
436           )
437         )
438       }
439     } catch (e: Exception) {
440       Log.e(TAG, "Failed to format error message for incompatible SDK version", e)
441     }
442     return ManifestException(Exception("Incompatible SDK version"), manifestUrl, errorJson)
443   }
444 
445   companion object {
446     private val TAG = ExpoUpdatesAppLoader::class.java.simpleName
447     const val UPDATES_EVENT_NAME = "Expo.nativeUpdatesEvent"
448   }
449 
450   init {
451     NativeModuleDepsProvider.instance.inject(ExpoUpdatesAppLoader::class.java, this)
452   }
453 }
454