Skip to content

Commit

Permalink
[ios] bugfixes and comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
mmocny committed Oct 22, 2012
1 parent a39921b commit 1dca15c
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 16 deletions.
38 changes: 24 additions & 14 deletions CordovaLib/Classes/CDVLocalStorage.m
Expand Up @@ -38,7 +38,7 @@ - (CDVPlugin*)initWithWebView:(UIWebView*)theWebView settings:(NSDictionary*)cla
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onResignActive)
name:UIApplicationWillResignActiveNotification object:nil];

self.backupInfo = [[self class] createBackupInfoWithCloudBackup:((NSString*)[classSettings objectForKey:@"backupType"] == @"cloud")];
self.backupInfo = [[self class] createBackupInfoWithCloudBackup:[(NSString*)[classSettings objectForKey:@"backupType"] isEqualToString:@"cloud"]];

// over-ride current webview delegate (for restore reasons)
self.webviewDelegate = theWebView.delegate;
Expand All @@ -53,6 +53,16 @@ - (CDVPlugin*)initWithWebView:(UIWebView*)theWebView settings:(NSDictionary*)cla

+ (NSMutableArray*)createBackupInfoWithTargetDir:(NSString*)targetDir backupDir:(NSString*)backupDir targetDirNests:(BOOL)targetDirNests backupDirNests:(BOOL)backupDirNests rename:(BOOL)rename
{
/*
This "helper" does so much work and has so many options it would probably be clearer to refactor the whole thing.
Basically, there are three database locations:
1. "Normal" dir -- LIB/<nested dires WebKit/LocalStorage etc>/<normal filenames>
2. "Caches" dir -- LIB/Caches/<normal filenames>
3. "Backup" dir -- DOC/Backups/<renamed filenames>
And between these three, there are various migration paths, most of which only consider 2 of the 3, which is why this helper is based on 2 locations and has a notion of "direction".
*/
NSMutableArray* backupInfo = [NSMutableArray arrayWithCapacity:3];

NSString* original;
Expand All @@ -63,7 +73,7 @@ + (NSMutableArray*)createBackupInfoWithTargetDir:(NSString*)targetDir backupDir:

original = [targetDir stringByAppendingPathComponent:targetDirNests ? @"WebKit/LocalStorage/file__0.localstorage":@"file__0.localstorage"];
backup = [backupDir stringByAppendingPathComponent:(backupDirNests ? @"WebKit/LocalStorage":@"")];
backup = [backupDir stringByAppendingPathComponent:(rename ? @"localstorage.appdata.db":@"file__0.localstorage")];
backup = [backup stringByAppendingPathComponent:(rename ? @"localstorage.appdata.db":@"file__0.localstorage")];

backupItem = [[CDVBackupInfo alloc] init];
backupItem.backup = backup;
Expand All @@ -76,7 +86,7 @@ + (NSMutableArray*)createBackupInfoWithTargetDir:(NSString*)targetDir backupDir:

original = [targetDir stringByAppendingPathComponent:targetDirNests ? @"WebKit/Databases/Databases.db":@"Databases.db"];
backup = [backupDir stringByAppendingPathComponent:(backupDirNests ? @"WebKit/Databases":@"")];
backup = [backupDir stringByAppendingPathComponent:(rename ? @"websqlmain.appdata.db":@"Databases.db")];
backup = [backup stringByAppendingPathComponent:(rename ? @"websqlmain.appdata.db":@"Databases.db")];

backupItem = [[CDVBackupInfo alloc] init];
backupItem.backup = backup;
Expand All @@ -89,7 +99,7 @@ + (NSMutableArray*)createBackupInfoWithTargetDir:(NSString*)targetDir backupDir:

original = [targetDir stringByAppendingPathComponent:targetDirNests ? @"WebKit/Databases/file__0":@"file__0"];
backup = [backupDir stringByAppendingPathComponent:(backupDirNests ? @"WebKit/Databases":@"")];
backup = [backupDir stringByAppendingPathComponent:(rename ? @"websqldbs.appdata.db":@"file__0")];
backup = [backup stringByAppendingPathComponent:(rename ? @"websqldbs.appdata.db":@"file__0")];

backupItem = [[CDVBackupInfo alloc] init];
backupItem.backup = backup;
Expand All @@ -111,15 +121,15 @@ + (NSMutableArray*)createBackupInfoWithCloudBackup:(BOOL)cloudBackup

// create the backups folder, if needed
[[NSFileManager defaultManager] createDirectoryAtPath:backupsFolder withIntermediateDirectories:YES attributes:nil error:nil];
[self addSkipBackupAttributeToItemAtURL:[NSURL fileURLWithPath:backupsFolder] skip:cloudBackup];

[self addSkipBackupAttributeToItemAtURL:[NSURL fileURLWithPath:backupsFolder] skip:!cloudBackup];

return [self createBackupInfoWithTargetDir:cacheFolder backupDir:backupsFolder targetDirNests:NO backupDirNests:NO rename:YES];
}

+ (BOOL)addSkipBackupAttributeToItemAtURL:(NSURL*)URL skip:(BOOL)skip
{
assert(IsAtLeastiOSVersion(@"5.1"));
assert([[NSFileManager defaultManager] fileExistsAtPath:[URL path]]);
NSAssert(IsAtLeastiOSVersion(@"5.1"), @"Cannot mark files for NSURLIsExcludedFromBackupKey on iOS less than 5.1");

NSError* error = nil;
BOOL success = [URL setResourceValue:[NSNumber numberWithBool:skip] forKey:NSURLIsExcludedFromBackupKey error:&error];
Expand Down Expand Up @@ -335,31 +345,31 @@ + (void)__restoreLegacyDatabaseLocationsWithBackupType:(NSString*)backupType
NSString* appLibraryFolder = [NSSearchPathForDirectoriesInDomains (NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString* appDocumentsFolder = [NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

NSMutableArray* backupInfo = nil;
NSMutableArray* backupInfo = [NSMutableArray arrayWithCapacity:0];

if ([backupType isEqualToString:@"cloud"]) {
// We would like to restore old backups/caches databases to the new destination (nested in lib folder)
[backupInfo addObjectsFromArray:[self createBackupInfoWithTargetDir:appLibraryFolder backupDir:[appDocumentsFolder stringByAppendingPathComponent:@"Backups"] targetDirNests:YES backupDirNests:NO rename:YES]];
[backupInfo addObjectsFromArray:[self createBackupInfoWithTargetDir:appLibraryFolder backupDir:[appLibraryFolder stringByAppendingPathComponent:@"Caches"] targetDirNests:YES backupDirNests:NO rename:NO]];
} else {
assert([backupType isEqualToString:@"local"]);
// For ios6 local backups we also want to restore from Backups dir -- but we don't need to do that here, since the plugin will do that itself.
[backupInfo addObjectsFromArray:[self createBackupInfoWithTargetDir:[appLibraryFolder stringByAppendingPathComponent:@"Caches"] backupDir:appLibraryFolder targetDirNests:NO backupDirNests:YES rename:NO]];
}

NSFileManager* manager = [NSFileManager defaultManager];

for (CDVBackupInfo* info in backupInfo) {
if ([info shouldRestore]) {
NSLog(@"Restoring old webstorage backup. From: '%@' To: '%@'.", info.backup, info.original);
[self copyFrom:info.backup to:info.original error:nil];
}
if ([manager fileExistsAtPath:info.backup]) {
if ([info shouldRestore]) {
NSLog(@"Restoring old webstorage backup. From: '%@' To: '%@'.", info.backup, info.original);
[self copyFrom:info.backup to:info.original error:nil];
}
NSLog(@"Removing old webstorage backup: '%@'.", info.backup);
[manager removeItemAtPath:info.backup error:nil];
}
}

[[NSUserDefaults standardUserDefaults] setBool:(backupType == @"cloud") forKey:@"WebKitStoreWebDataForBackup"];
[[NSUserDefaults standardUserDefaults] setBool:[backupType isEqualToString:@"cloud"] forKey:@"WebKitStoreWebDataForBackup"];
}

#pragma mark -
Expand Down
1 change: 0 additions & 1 deletion CordovaLib/Classes/CDVViewController.m
Expand Up @@ -183,7 +183,6 @@ - (void)viewDidLoad
} else if ([backupWebStorage isKindOfClass:[NSNumber class]]) {
backupWebStorageType = [(NSNumber*) backupWebStorage boolValue] ? @"cloud" : @"none";
}
NSLog(@"BackupType: %@", backupWebStorageType);

if (IsAtLeastiOSVersion(@"5.1")) {
[CDVLocalStorage __fixupDatabaseLocationsWithBackupType:backupWebStorageType];
Expand Down
2 changes: 1 addition & 1 deletion bin/templates/project/__TESTING__/Cordova.plist
Expand Up @@ -45,7 +45,7 @@
<key>OpenAllWhitelistURLsInWebView</key>
<false/>
<key>BackupWebStorage</key>
<string>gray</string>
<string>cloud</string>
<key>ExternalHosts</key>
<array/>
<key>Plugins</key>
Expand Down

0 comments on commit 1dca15c

Please sign in to comment.