@@ -16,17 +16,17 @@ let listeningPortRange: Range<UInt> = 12000...13000
16
16
/// we revert to the last known good version
17
17
let startupTimeoutInterval = 10.0
18
18
19
- @objc ( METWebAppCordova )
20
- final public class WebAppCordova : CDVPlugin , AssetBundleManagerDelegate {
19
+ @objc ( METWebApp )
20
+ final public class WebApp : CDVPlugin , AssetBundleManagerDelegate {
21
21
/// The local web server responsible for serving assets to the web app
22
22
private( set) var localServer : GCDWebServer !
23
-
23
+
24
24
/// The listening port of the local web server
25
25
private var localServerPort : UInt = 0
26
26
27
27
/// The www directory in the app bundle
28
28
private( set) var wwwDirectoryURL : NSURL !
29
-
29
+
30
30
/// Persistent configuration settings for the webapp
31
31
private var configuration : WebAppConfiguration !
32
32
@@ -98,10 +98,10 @@ final public class WebAppCordova: CDVPlugin, AssetBundleManagerDelegate {
98
98
NSLog ( " Could not remove versions directory: \( error) " )
99
99
return
100
100
}
101
-
101
+
102
102
configuration. reset ( )
103
103
}
104
-
104
+
105
105
// We keep track of the last seen initial version (see above)
106
106
configuration. lastSeenInitialVersion = initialAssetBundle. version
107
107
@@ -151,9 +151,9 @@ final public class WebAppCordova: CDVPlugin, AssetBundleManagerDelegate {
151
151
self ? . revertToLastKnownGoodVersion ( )
152
152
}
153
153
}
154
-
154
+
155
155
NSNotificationCenter . defaultCenter ( ) . addObserver ( self , selector: " applicationDidEnterBackground " , name: UIApplicationDidEnterBackgroundNotification, object: nil )
156
-
156
+
157
157
NSNotificationCenter . defaultCenter ( ) . addObserver ( self , selector: " pageDidLoad " , name: CDVPageDidLoadNotification, object: webView)
158
158
}
159
159
@@ -166,7 +166,7 @@ final public class WebAppCordova: CDVPlugin, AssetBundleManagerDelegate {
166
166
currentAssetBundle = pendingAssetBundle
167
167
self . pendingAssetBundle = nil
168
168
}
169
-
169
+
170
170
startupTimer? . startWithTimeInterval ( startupTimeoutInterval)
171
171
}
172
172
@@ -241,7 +241,7 @@ final public class WebAppCordova: CDVPlugin, AssetBundleManagerDelegate {
241
241
242
242
private func notifyDownloadFailure( error: ErrorType ) {
243
243
NSLog ( " Failure: \( error) " )
244
-
244
+
245
245
let errorMessage = String ( error)
246
246
let result = CDVPluginResult ( status: CDVCommandStatus_OK, messageAsString: errorMessage)
247
247
commandDelegate? . sendPluginResult ( result, callbackId: downloadFailureCallbackId)
@@ -252,7 +252,7 @@ final public class WebAppCordova: CDVPlugin, AssetBundleManagerDelegate {
252
252
func revertToLastKnownGoodVersion( ) {
253
253
// Blacklist the current version, so we don't update to it again rightaway
254
254
configuration. addBlacklistedVersion ( currentAssetBundle. version)
255
-
255
+
256
256
if let lastKnownGoodVersion = configuration. lastKnownGoodVersion,
257
257
let lastKnownGoodAssetBundle = assetBundleManager. downloadedAssetBundleWithVersion ( lastKnownGoodVersion) {
258
258
pendingAssetBundle = lastKnownGoodAssetBundle
@@ -287,31 +287,31 @@ final public class WebAppCordova: CDVPlugin, AssetBundleManagerDelegate {
287
287
288
288
func assetBundleManager( assetBundleManager: AssetBundleManager , didFinishDownloadingBundle assetBundle: AssetBundle ) {
289
289
NSLog ( " Finished downloading new asset bundle version: \( assetBundle. version) " )
290
-
290
+
291
291
do {
292
292
try verifyDownloadedAssetBundle ( assetBundle)
293
-
293
+
294
294
configuration. lastDownloadedVersion = assetBundle. version
295
295
pendingAssetBundle = assetBundle
296
296
notifyNewVersionDownloaded ( assetBundle. version)
297
297
} catch {
298
298
notifyDownloadFailure ( error)
299
299
}
300
300
}
301
-
301
+
302
302
private func verifyDownloadedAssetBundle( assetBundle: AssetBundle ) throws {
303
303
guard let appId = assetBundle. appId else {
304
304
throw WebAppError . UnsuitableAssetBundle ( reason: " Could not find appId in downloaded asset bundle " , underlyingError: nil )
305
305
}
306
-
306
+
307
307
if appId != configuration. appId {
308
308
throw WebAppError . UnsuitableAssetBundle ( reason: " appId in downloaded asset bundle does not match current appId " , underlyingError: nil )
309
309
}
310
-
310
+
311
311
guard let rootURL = assetBundle. rootURL else {
312
312
throw WebAppError . UnsuitableAssetBundle ( reason: " Could not find ROOT_URL in downloaded asset bundle " , underlyingError: nil )
313
313
}
314
-
314
+
315
315
if configuration. rootURL? . host != " localhost " && rootURL. host == " localhost " {
316
316
throw WebAppError . UnsuitableAssetBundle ( reason: " ROOT_URL in downloaded asset bundle would change current ROOT_URL to localhost. Make sure ROOT_URL has been configured correctly on the server. " , underlyingError: nil )
317
317
}
@@ -389,11 +389,11 @@ final public class WebAppCordova: CDVPlugin, AssetBundleManagerDelegate {
389
389
private func addIndexFileHandler( ) {
390
390
localServer. addHandlerWithMatchBlock ( { [ weak self] ( requestMethod, requestURL, requestHeaders, URLPath, URLQuery) -> GCDWebServerRequest ! in
391
391
if requestMethod != " GET " { return nil }
392
-
392
+
393
393
if URLPath == " /favicon.ico " { return nil }
394
394
395
395
guard let indexFile = self ? . currentAssetBundle? . indexFile else { return nil }
396
-
396
+
397
397
let request = GCDWebServerRequest ( method: requestMethod, url: requestURL, headers: requestHeaders, path: URLPath, query: URLQuery)
398
398
request. setAttribute ( Box ( indexFile) , forKey: GCDWebServerRequestAttribute_Asset)
399
399
return request
@@ -402,7 +402,7 @@ final public class WebAppCordova: CDVPlugin, AssetBundleManagerDelegate {
402
402
return self . responseForAsset ( request, asset: asset)
403
403
}
404
404
}
405
-
405
+
406
406
private func addNotFoundHandler( ) {
407
407
localServer. addDefaultHandlerForMethod ( " GET " , requestClass: GCDWebServerRequest . self) { ( request) -> GCDWebServerResponse ! in
408
408
return GCDWebServerResponse ( statusCode: GCDWebServerClientErrorHTTPStatusCode . HTTPStatusCode_NotFound. rawValue)
0 commit comments