Restore backups if they exist, then remove them (for upgrade from 5.1)
Project: http://git-wip-us.apache.org/repos/asf/incubator-cordova-ios/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-cordova-ios/commit/09883f80 Tree: http://git-wip-us.apache.org/repos/asf/incubator-cordova-ios/tree/09883f80 Diff: http://git-wip-us.apache.org/repos/asf/incubator-cordova-ios/diff/09883f80 Branch: refs/heads/master Commit: 09883f80dbd92d04b56642fe22960aab5a3ae1d0 Parents: b7741dc Author: Michal Mocny <mmo...@gmail.com> Authored: Wed Aug 22 10:42:20 2012 -0400 Committer: Michal Mocny <mmo...@gmail.com> Committed: Wed Aug 22 10:42:20 2012 -0400 ---------------------------------------------------------------------- CordovaLib/Classes/CDVLocalStorage.h | 2 +- CordovaLib/Classes/CDVLocalStorage.m | 146 +++++++++++++++------------ CordovaLib/Classes/CDVViewController.m | 12 ++- 3 files changed, 89 insertions(+), 71 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-cordova-ios/blob/09883f80/CordovaLib/Classes/CDVLocalStorage.h ---------------------------------------------------------------------- diff --git a/CordovaLib/Classes/CDVLocalStorage.h b/CordovaLib/Classes/CDVLocalStorage.h index 9abb9a2..02ae2c5 100644 --- a/CordovaLib/Classes/CDVLocalStorage.h +++ b/CordovaLib/Classes/CDVLocalStorage.h @@ -30,13 +30,13 @@ - (BOOL) shouldRestore; - (void) backup:(CDVInvokedUrlCommand*)command; - (void) restore:(CDVInvokedUrlCommand*)command; -- (void) verifyAndFixDatabaseLocations:(CDVInvokedUrlCommand*)command; + (void) __verifyAndFixDatabaseLocations; // Visible for testing. + (BOOL) __verifyAndFixDatabaseLocationsWithAppPlistDict:(NSMutableDictionary*)appPlistDict bundlePath:(NSString*)bundlePath fileManager:(NSFileManager*)fileManager; ++ (void) __restoreThenRemoveBackupLocations; @end @interface CDVBackupInfo : NSObject http://git-wip-us.apache.org/repos/asf/incubator-cordova-ios/blob/09883f80/CordovaLib/Classes/CDVLocalStorage.m ---------------------------------------------------------------------- diff --git a/CordovaLib/Classes/CDVLocalStorage.m b/CordovaLib/Classes/CDVLocalStorage.m index 37352ca..6e74270 100644 --- a/CordovaLib/Classes/CDVLocalStorage.m +++ b/CordovaLib/Classes/CDVLocalStorage.m @@ -41,79 +41,83 @@ [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onResignActive) name:UIApplicationWillResignActiveNotification object:nil]; - NSString *original, *backup; - self.backupInfo = [NSMutableArray arrayWithCapacity:3]; - - // set up common folders - NSString* appLibraryFolder = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES)objectAtIndex:0]; - NSString* appDocumentsFolder = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; - NSString* backupsFolder = [appDocumentsFolder stringByAppendingPathComponent:@"Backups"]; - - // create the backups folder - [[NSFileManager defaultManager] createDirectoryAtPath:backupsFolder withIntermediateDirectories:YES attributes:nil error:nil]; - - //////////// LOCALSTORAGE - - original = [[appLibraryFolder stringByAppendingPathComponent: - (IsAtLeastiOSVersion(@"5.1")) ? @"Caches" : @"WebKit/LocalStorage"] - stringByAppendingPathComponent:@"file__0.localstorage"]; - - backup = [backupsFolder stringByAppendingPathComponent:@"localstorage.appdata.db"]; - - CDVBackupInfo* backupItem = [[CDVBackupInfo alloc] init]; - backupItem.backup = backup; - backupItem.original = original; - backupItem.label = @"localStorage database"; - - [self.backupInfo addObject:backupItem]; - - //////////// WEBSQL MAIN DB - - original = [[appLibraryFolder stringByAppendingPathComponent: - (IsAtLeastiOSVersion(@"5.1")) ? @"Caches" : @"WebKit/Databases"] - stringByAppendingPathComponent:@"Databases.db"]; - - backup = [backupsFolder stringByAppendingPathComponent:@"websqlmain.appdata.db"]; - - backupItem = [[CDVBackupInfo alloc] init]; - backupItem.backup = backup; - backupItem.original = original; - backupItem.label = @"websql main database"; - - [self.backupInfo addObject:backupItem]; - - //////////// WEBSQL DATABASES - - original = [[appLibraryFolder stringByAppendingPathComponent: - (IsAtLeastiOSVersion(@"5.1")) ? @"Caches" : @"WebKit/Databases"] - stringByAppendingPathComponent:@"file__0"]; - - backup = [backupsFolder stringByAppendingPathComponent:@"websqldbs.appdata.db"]; - - backupItem = [[CDVBackupInfo alloc] init]; - backupItem.backup = backup; - backupItem.original = original; - backupItem.label = @"websql databases"; - - [self.backupInfo addObject:backupItem]; - - //////////// + self.backupInfo = [[self class] createBackupInfo]; // over-ride current webview delegate (for restore reasons) self.webviewDelegate = theWebView.delegate; theWebView.delegate = self; // verify the and fix the iOS 5.1 database locations once - [self verifyAndFixDatabaseLocations:nil]; + [[self class] __verifyAndFixDatabaseLocations]; } return self; } ++ (NSMutableArray*) createBackupInfo +{ + NSMutableArray* backupInfo = [NSMutableArray arrayWithCapacity:3]; + + // set up common folders + NSString* appLibraryFolder = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES)objectAtIndex:0]; + NSString* appDocumentsFolder = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; + NSString* backupsFolder = [appDocumentsFolder stringByAppendingPathComponent:@"Backups"]; + + // create the backups folder + [[NSFileManager defaultManager] createDirectoryAtPath:backupsFolder withIntermediateDirectories:YES attributes:nil error:nil]; + + //////////// LOCALSTORAGE + + NSString *original = [[appLibraryFolder stringByAppendingPathComponent: + (IsAtLeastiOSVersion(@"5.1")) ? @"Caches" : @"WebKit/LocalStorage"] + stringByAppendingPathComponent:@"file__0.localstorage"]; + + NSString *backup = [backupsFolder stringByAppendingPathComponent:@"localstorage.appdata.db"]; + + CDVBackupInfo* backupItem = [[CDVBackupInfo alloc] init]; + backupItem.backup = backup; + backupItem.original = original; + backupItem.label = @"localStorage database"; + + [backupInfo addObject:backupItem]; + + //////////// WEBSQL MAIN DB + + original = [[appLibraryFolder stringByAppendingPathComponent: + (IsAtLeastiOSVersion(@"5.1")) ? @"Caches" : @"WebKit/Databases"] + stringByAppendingPathComponent:@"Databases.db"]; + + backup = [backupsFolder stringByAppendingPathComponent:@"websqlmain.appdata.db"]; + + backupItem = [[CDVBackupInfo alloc] init]; + backupItem.backup = backup; + backupItem.original = original; + backupItem.label = @"websql main database"; + + [backupInfo addObject:backupItem]; + + //////////// WEBSQL DATABASES + + original = [[appLibraryFolder stringByAppendingPathComponent: + (IsAtLeastiOSVersion(@"5.1")) ? @"Caches" : @"WebKit/Databases"] + stringByAppendingPathComponent:@"file__0"]; + + backup = [backupsFolder stringByAppendingPathComponent:@"websqldbs.appdata.db"]; + + backupItem = [[CDVBackupInfo alloc] init]; + backupItem.backup = backup; + backupItem.original = original; + backupItem.label = @"websql databases"; + + [backupInfo addObject:backupItem]; + + return backupInfo; +} + #pragma mark - #pragma mark Plugin interface methods -- (BOOL) copyFrom:(NSString*)src to:(NSString*)dest error:(NSError* __autoreleasing*)error ++ (BOOL) copyFrom:(NSString*)src to:(NSString*)dest error:(NSError* __autoreleasing*)error { NSFileManager* fileManager = [NSFileManager defaultManager]; @@ -200,7 +204,7 @@ { if ([info shouldBackup]) { - [self copyFrom:info.original to:info.backup error:&error]; + [[self class] copyFrom:info.original to:info.backup error:&error]; if (callbackId) { if (error == nil) { @@ -235,7 +239,7 @@ { if ([info shouldRestore]) { - [self copyFrom:info.backup to:info.original error:&error]; + [[self class] copyFrom:info.backup to:info.original error:&error]; if (error == nil) { message = [NSString stringWithFormat:@"Restored: %@", info.label]; @@ -255,11 +259,6 @@ } } -- (void) verifyAndFixDatabaseLocations:(CDVInvokedUrlCommand*)command -{ - [[self class] __verifyAndFixDatabaseLocations]; -} - + (void) __verifyAndFixDatabaseLocations { NSBundle* mainBundle = [NSBundle mainBundle]; @@ -312,6 +311,21 @@ return dirty; } ++ (void) __restoreThenRemoveBackupLocations +{ + NSMutableArray* backupInfo = [CDVLocalStorage createBackupInfo]; + NSFileManager* manager = [NSFileManager defaultManager]; + + for (CDVBackupInfo* info in backupInfo) + { + if ([manager fileExistsAtPath:info.backup]) { + [self copyFrom:info.backup to:info.original error:nil]; + [manager removeItemAtPath:info.backup error:nil]; + NSLog(@"Removing old webstorage backup locations: %@", info.backup); + } + } +} + #pragma mark - #pragma mark Notification handlers http://git-wip-us.apache.org/repos/asf/incubator-cordova-ios/blob/09883f80/CordovaLib/Classes/CDVViewController.m ---------------------------------------------------------------------- diff --git a/CordovaLib/Classes/CDVViewController.m b/CordovaLib/Classes/CDVViewController.m index 9ce1d45..fae4b76 100644 --- a/CordovaLib/Classes/CDVViewController.m +++ b/CordovaLib/Classes/CDVViewController.m @@ -222,11 +222,15 @@ /* * Fire up CDVLocalStorage on iOS 5.1 to work-around WebKit storage limitations, or adjust set user defaults on iOS 6.0+ */ - if (backupWebStorage) { - if (IsAtLeastiOSVersion(@"6.0")) { - [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"WebKitStoreWebDataForBackup"]; - } else { + if (IsAtLeastiOSVersion(@"6.0")) { + [[NSUserDefaults standardUserDefaults] setBool:backupWebStorage forKey:@"WebKitStoreWebDataForBackup"]; + // We don't manually back anything up in 6.0 and so we should remove any old backups. + [CDVLocalStorage __restoreThenRemoveBackupLocations]; + } else { + if (backupWebStorage) { [self.commandDelegate registerPlugin:[[CDVLocalStorage alloc] initWithWebView:self.webView] withClassName:NSStringFromClass([CDVLocalStorage class])]; + } else { + [CDVLocalStorage __restoreThenRemoveBackupLocations]; } }