diff --git a/chrome-cli/App.h b/chrome-cli/App.h index 4a41c78..26b756e 100644 --- a/chrome-cli/App.h +++ b/chrome-cli/App.h @@ -57,5 +57,5 @@ typedef enum { - (void)printSourceFromTab:(Arguments *)args; - (void)printChromeVersion:(Arguments *)args; - (void)printVersion:(Arguments *)args; - +- (void)listProfiles:(Arguments *)args; @end diff --git a/chrome-cli/App.m b/chrome-cli/App.m index 0ee78c4..ec25df0 100644 --- a/chrome-cli/App.m +++ b/chrome-cli/App.m @@ -28,6 +28,33 @@ - (id)initWithBundleIdentifier:(NSString *)bundleIdentifier outputFormat:(Output return self; } +- (NSArray *)getProfiles { + NSString *homeDir = NSHomeDirectory(); + NSString *profilesPath = [homeDir stringByAppendingPathComponent:@"Library/Application Support/Google/Chrome"]; + + NSError *error = nil; + NSFileManager *fileManager = [NSFileManager defaultManager]; + + NSArray *contents = [fileManager contentsOfDirectoryAtPath:profilesPath error:&error]; + if (error) { + return @[]; + } + + NSMutableArray *profiles = [NSMutableArray array]; + + for (NSString *item in contents) { + if ([item isEqualToString:@"Default"] || [item hasPrefix:@"Profile"]) { + NSString *prefsPath = [profilesPath stringByAppendingPathComponent: + [item stringByAppendingPathComponent:@"Preferences"]]; + + if ([fileManager fileExistsAtPath:prefsPath]) { + [profiles addObject:item]; + } + } + } + + return profiles; +} - (chromeApplication *)chrome { chromeApplication *chrome = [SBApplication applicationWithBundleIdentifier:self->bundleIdentifier]; @@ -762,4 +789,16 @@ - (NSJSONWritingOptions)jsonWritingOptions { return NSJSONWritingPrettyPrinted; } +- (void)listProfiles:(Arguments *)args { + NSArray *profiles = [self getProfiles]; + + if (self->outputFormat == kOutputFormatJSON) { + [self printJSON:@{@"profiles": profiles}]; + } else { + for (NSString *profile in profiles) { + printf("Profile Directory: %s\n", profile.UTF8String); + } + } +} + @end diff --git a/chrome-cli/main.m b/chrome-cli/main.m index 02e1ce5..c70ccad 100644 --- a/chrome-cli/main.m +++ b/chrome-cli/main.m @@ -84,6 +84,8 @@ int main(int argc, const char * argv[]) [argonaut add:@"execute " target:app action:@selector(executeJavascriptInActiveTab:) description:@"Execute javascript in active tab"]; [argonaut add:@"execute -t " target:app action:@selector(executeJavascriptInTab:) description:@"Execute javascript in specific tab"]; + + [argonaut add:@"list profiles" target:app action:@selector(listProfiles:) description:@"List Chrome profiles"]; [argonaut add:@"chrome version" target:app action:@selector(printChromeVersion:) description:@"Print Chrome version"]; [argonaut add:@"version" target:app action:@selector(printVersion:) description:@"Print application version"];