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       (Constants.SDK_VERSIONS_LIST + listOf(RNObject.UNVERSIONED)).forEach {
144         add(it)
145         add("exposdk:$it")
146       }
147     }
148     val selectionPolicy = SelectionPolicy(
149       LauncherSelectionPolicyFilterAware(sdkVersionsList),
150       LoaderSelectionPolicyFilterAware(),
151       ReaperSelectionPolicyDevelopmentClient()
152     )
153     val directory: File = try {
154       UpdatesUtils.getOrCreateUpdatesDirectory(context)
155     } catch (e: Exception) {
156       callback.onError(e)
157       return
158     }
159     startLoaderTask(configuration, directory, selectionPolicy, context)
160   }
161 
162   private fun startLoaderTask(
163     configuration: UpdatesConfiguration,
164     directory: File,
165     selectionPolicy: SelectionPolicy,
166     context: Context
167   ) {
168     updatesConfiguration = configuration
169     updatesDirectory = directory
170     this.selectionPolicy = selectionPolicy
171     if (!configuration.isEnabled) {
172       launchWithNoDatabase(context, null)
173       return
174     }
175     LoaderTask(
176       configuration,
177       databaseHolder,
178       directory,
179       fileDownloader,
180       selectionPolicy,
181       object : LoaderTaskCallback {
182         private var didAbort = false
183         override fun onFailure(e: Exception) {
184           if (Constants.isStandaloneApp()) {
185             isEmergencyLaunch = true
186             launchWithNoDatabase(context, e)
187           } else {
188             if (didAbort) {
189               return
190             }
191             var exception = e
192             try {
193               val errorJson = JSONObject(e.message!!)
194               exception = ManifestException(e, manifestUrl, errorJson)
195             } catch (ex: Exception) {
196               // do nothing, expected if the error payload does not come from a conformant server
197             }
198             callback.onError(exception)
199           }
200         }
201 
202         override fun onCachedUpdateLoaded(update: UpdateEntity): Boolean {
203           val manifest = Manifest.fromManifestJson(update.manifest)
204           setShouldShowAppLoaderStatus(manifest)
205           if (manifest.isUsingDeveloperTool()) {
206             return false
207           } else {
208             try {
209               val experienceKey = ExperienceKey.fromManifest(manifest)
210               // if previous run of this app failed due to a loading error, we want to make sure to check for remote updates
211               val experienceMetadata = exponentSharedPreferences.getExperienceMetadata(experienceKey)
212               if (experienceMetadata != null && experienceMetadata.optBoolean(
213                   ExponentSharedPreferences.EXPERIENCE_METADATA_LOADING_ERROR
214                 )
215               ) {
216                 return false
217               }
218             } catch (e: Exception) {
219               return true
220             }
221           }
222           return true
223         }
224 
225         override fun onRemoteUpdateManifestLoaded(updateManifest: UpdateManifest) {
226           // expo-cli does not always respect our SDK version headers and respond with a compatible update or an error
227           // so we need to check the compatibility here
228           val sdkVersion = updateManifest.manifest.getSDKVersion()
229           if (!isValidSdkVersion(sdkVersion)) {
230             callback.onError(formatExceptionForIncompatibleSdk(sdkVersion ?: "null"))
231             didAbort = true
232             return
233           }
234           setShouldShowAppLoaderStatus(updateManifest.manifest)
235           callback.onOptimisticManifest(updateManifest.manifest)
236           updateStatus(AppLoaderStatus.DOWNLOADING_NEW_UPDATE)
237         }
238 
239         override fun onSuccess(launcher: Launcher, isUpToDate: Boolean) {
240           if (didAbort) {
241             return
242           }
243           this@ExpoUpdatesAppLoader.launcher = launcher
244           this@ExpoUpdatesAppLoader.isUpToDate = isUpToDate
245           try {
246             val manifestJson = processManifestJson(launcher.launchedUpdate!!.manifest)
247             val manifest = Manifest.fromManifestJson(manifestJson)
248             callback.onManifestCompleted(manifest)
249 
250             // ReactAndroid will load the bundle on its own in development mode
251             if (!manifest.isDevelopmentMode()) {
252               callback.onBundleCompleted(launcher.launchAssetFile)
253             }
254           } catch (e: Exception) {
255             callback.onError(e)
256           }
257         }
258 
259         override fun onBackgroundUpdateFinished(
260           status: BackgroundUpdateStatus,
261           update: UpdateEntity?,
262           exception: Exception?
263         ) {
264           if (didAbort) {
265             return
266           }
267           try {
268             val jsonParams = JSONObject()
269             when (status) {
270               BackgroundUpdateStatus.ERROR -> {
271                 if (exception == null) {
272                   throw AssertionError("Background update with error status must have a nonnull exception object")
273                 }
274                 jsonParams.put("type", UPDATE_ERROR_EVENT)
275                 jsonParams.put("message", exception.message)
276               }
277               BackgroundUpdateStatus.UPDATE_AVAILABLE -> {
278                 if (update == null) {
279                   throw AssertionError("Background update with error status must have a nonnull update object")
280                 }
281                 jsonParams.put("type", UPDATE_AVAILABLE_EVENT)
282                 jsonParams.put("manifestString", update.manifest.toString())
283               }
284               BackgroundUpdateStatus.NO_UPDATE_AVAILABLE -> {
285                 jsonParams.put("type", UPDATE_NO_UPDATE_AVAILABLE_EVENT)
286               }
287             }
288             callback.emitEvent(jsonParams)
289           } catch (e: Exception) {
290             Log.e(TAG, "Failed to emit event to JS", e)
291           }
292         }
293       }
294     ).start(context)
295   }
296 
297   private fun launchWithNoDatabase(context: Context, e: Exception?) {
298     this.launcher = NoDatabaseLauncher(context, updatesConfiguration, e)
299     var manifestJson = EmbeddedLoader.readEmbeddedManifest(context, updatesConfiguration)!!.manifest.getRawJson()
300     try {
301       manifestJson = processManifestJson(manifestJson)
302     } catch (ex: Exception) {
303       Log.e(
304         TAG,
305         "Failed to process manifest; attempting to launch with raw manifest. This may cause errors or unexpected behavior.",
306         e
307       )
308     }
309     callback.onManifestCompleted(Manifest.fromManifestJson(manifestJson))
310     var launchAssetFile = launcher.launchAssetFile
311     if (launchAssetFile == null) {
312       // ReactInstanceManagerBuilder accepts embedded assets as strings with "assets://" prefixed
313       launchAssetFile = "assets://" + launcher.bundleAssetName
314     }
315     callback.onBundleCompleted(launchAssetFile)
316   }
317 
318   @Throws(JSONException::class)
319   private fun processManifestJson(manifestJson: JSONObject): JSONObject {
320     val parsedManifestUrl = Uri.parse(manifestUrl)
321     if (!manifestJson.optBoolean(ExponentManifest.MANIFEST_IS_VERIFIED_KEY, false) &&
322       isThirdPartyHosted(parsedManifestUrl) &&
323       !Constants.isStandaloneApp()
324     ) {
325       // Sandbox third party apps and consider them verified
326       // for https urls, sandboxed id is of form quinlanj.github.io/myProj-myApp
327       // for http urls, sandboxed id is of form UNVERIFIED-quinlanj.github.io/myProj-myApp
328       val protocol = parsedManifestUrl.scheme
329       val securityPrefix = if (protocol == "https" || protocol == "exps") "" else "UNVERIFIED-"
330       val path = if (parsedManifestUrl.path != null) parsedManifestUrl.path else ""
331       val slug = manifestJson.getNullable<String>(ExponentManifest.MANIFEST_SLUG) ?: ""
332       val sandboxedId = securityPrefix + parsedManifestUrl.host + path + "-" + slug
333       manifestJson.put(ExponentManifest.MANIFEST_ID_KEY, sandboxedId)
334       manifestJson.put(ExponentManifest.MANIFEST_IS_VERIFIED_KEY, true)
335     }
336     if (Constants.isStandaloneApp()) {
337       manifestJson.put(ExponentManifest.MANIFEST_IS_VERIFIED_KEY, true)
338     }
339     if (!manifestJson.has(ExponentManifest.MANIFEST_IS_VERIFIED_KEY)) {
340       manifestJson.put(ExponentManifest.MANIFEST_IS_VERIFIED_KEY, false)
341     }
342     if (!manifestJson.optBoolean(ExponentManifest.MANIFEST_IS_VERIFIED_KEY, false) &&
343       exponentManifest.isAnonymousExperience(Manifest.fromManifestJson(manifestJson))
344     ) {
345       // automatically verified
346       manifestJson.put(ExponentManifest.MANIFEST_IS_VERIFIED_KEY, true)
347     }
348     return manifestJson
349   }
350 
351   private fun isThirdPartyHosted(uri: Uri): Boolean {
352     val host = uri.host
353     return !(
354       host == "exp.host" || host == "expo.io" || host == "exp.direct" || host == "expo.test" ||
355         host!!.endsWith(".exp.host") || host.endsWith(".expo.io") || host.endsWith(".exp.direct") || host.endsWith(
356         ".expo.test"
357       )
358       )
359   }
360 
361   private fun setShouldShowAppLoaderStatus(manifest: Manifest) {
362     // we don't want to show the cached experience alert when Updates.reloadAsync() is called
363     if (useCacheOnly) {
364       shouldShowAppLoaderStatus = false
365       return
366     }
367     shouldShowAppLoaderStatus = !manifest.isDevelopmentSilentLaunch()
368   }
369 
370   // XDL expects the full "exponent-" header names
371   private val requestHeaders: Map<String, String?>
372     get() {
373       val headers = mutableMapOf<String, String>()
374       headers["Expo-Updates-Environment"] = clientEnvironment
375       headers["Expo-Client-Environment"] = clientEnvironment
376       val versionName = ExpoViewKernel.instance.versionName
377       if (versionName != null) {
378         headers["Exponent-Version"] = versionName
379       }
380       val sessionSecret = exponentSharedPreferences.sessionSecret
381       if (sessionSecret != null) {
382         headers["Expo-Session"] = sessionSecret
383       }
384 
385       // XDL expects the full "exponent-" header names
386       headers["Exponent-Accept-Signature"] = "true"
387       headers["Exponent-Platform"] = "android"
388       if (KernelConfig.FORCE_UNVERSIONED_PUBLISHED_EXPERIENCES) {
389         headers["Exponent-SDK-Version"] = "UNVERSIONED"
390       } else {
391         headers["Exponent-SDK-Version"] = Constants.SDK_VERSIONS
392       }
393       return headers
394     }
395 
396   private val clientEnvironment: String
397     get() = if (Constants.isStandaloneApp()) {
398       "STANDALONE"
399     } else if (Build.FINGERPRINT.contains("vbox") || Build.FINGERPRINT.contains("generic")) {
400       "EXPO_SIMULATOR"
401     } else {
402       "EXPO_DEVICE"
403     }
404 
405   private fun isValidSdkVersion(sdkVersion: String?): Boolean {
406     if (sdkVersion == null) {
407       return false
408     }
409     if (RNObject.UNVERSIONED == sdkVersion) {
410       return true
411     }
412     for (version in Constants.SDK_VERSIONS_LIST) {
413       if (version == sdkVersion) {
414         return true
415       }
416     }
417     return false
418   }
419 
420   private fun formatExceptionForIncompatibleSdk(sdkVersion: String): ManifestException {
421     val errorJson = JSONObject()
422     try {
423       errorJson.put("message", "Invalid SDK version")
424       if (ABIVersion.toNumber(sdkVersion) > ABIVersion.toNumber(Constants.SDK_VERSIONS_LIST[0])) {
425         errorJson.put("errorCode", "EXPERIENCE_SDK_VERSION_TOO_NEW")
426       } else {
427         errorJson.put("errorCode", "EXPERIENCE_SDK_VERSION_OUTDATED")
428         errorJson.put(
429           "metadata",
430           JSONObject().put(
431             "availableSDKVersions",
432             JSONArray().put(sdkVersion)
433           )
434         )
435       }
436     } catch (e: Exception) {
437       Log.e(TAG, "Failed to format error message for incompatible SDK version", e)
438     }
439     return ManifestException(Exception("Incompatible SDK version"), manifestUrl, errorJson)
440   }
441 
442   companion object {
443     private val TAG = ExpoUpdatesAppLoader::class.java.simpleName
444     const val UPDATES_EVENT_NAME = "Expo.nativeUpdatesEvent"
445   }
446 
447   init {
448     NativeModuleDepsProvider.instance.inject(ExpoUpdatesAppLoader::class.java, this)
449   }
450 }
451