mirror of
https://github.com/bitwarden/browser
synced 2025-12-21 18:53:29 +00:00
adapt chromium_importer to match conventions established by autofill:
• eliminate extern c and unsafe rust from chromium_importer logic (still called in prior napi/lib.rs) • use CommandInput / CommandResult JSON pattern to call objc methods • remove usage of libc to cleanup memory and remove this dependency
This commit is contained in:
@@ -1,22 +0,0 @@
|
||||
#ifndef BROWSER_ACCESS_H
|
||||
#define BROWSER_ACCESS_H
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
// Request user permission to access browser directory
|
||||
// Returns base64-encoded bookmark data, or NULL if declined
|
||||
// Caller must free returned string
|
||||
char* requestBrowserAccess(const char* browserName, const char* relativePath);
|
||||
|
||||
// Check if we have stored bookmark (doesn't verify validity)
|
||||
bool hasStoredBrowserAccess(const char* browserName);
|
||||
|
||||
// Start accessing browser using stored bookmark
|
||||
// Returns resolved path, or NULL if bookmark invalid
|
||||
// Caller must free returned string and call stopBrowserAccess when done
|
||||
char* startBrowserAccess(const char* browserName);
|
||||
|
||||
// Stop accessing browser (MUST be called after startBrowserAccess)
|
||||
void stopBrowserAccess(const char* browserName);
|
||||
|
||||
#endif
|
||||
@@ -1,56 +0,0 @@
|
||||
#import <Foundation/Foundation.h>
|
||||
#import "browser_access.h"
|
||||
#import "../utils.h"
|
||||
|
||||
#import "browser_access_manager.h"
|
||||
|
||||
static BrowserAccessManager* sharedManager = nil;
|
||||
|
||||
static BrowserAccessManager* getManager() {
|
||||
static dispatch_once_t onceToken;
|
||||
dispatch_once(&onceToken, ^{
|
||||
sharedManager = [[BrowserAccessManager alloc] init];
|
||||
});
|
||||
return sharedManager;
|
||||
}
|
||||
|
||||
char* requestBrowserAccess(const char* browserName, const char* relativePath) {
|
||||
@autoreleasepool {
|
||||
NSString* name = [NSString stringWithUTF8String:browserName];
|
||||
NSString* path = [NSString stringWithUTF8String:relativePath];
|
||||
NSString* result = [getManager() requestAccessToBrowserDir:name relativePath:path];
|
||||
|
||||
if (result == nil) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return strdup([result UTF8String]);
|
||||
}
|
||||
}
|
||||
|
||||
bool hasStoredBrowserAccess(const char* browserName) {
|
||||
@autoreleasepool {
|
||||
NSString* name = [NSString stringWithUTF8String:browserName];
|
||||
return [getManager() hasStoredAccess:name];
|
||||
}
|
||||
}
|
||||
|
||||
char* startBrowserAccess(const char* browserName) {
|
||||
@autoreleasepool {
|
||||
NSString* name = [NSString stringWithUTF8String:browserName];
|
||||
NSString* result = [getManager() startAccessingBrowser:name];
|
||||
|
||||
if (result == nil) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return strdup([result UTF8String]);
|
||||
}
|
||||
}
|
||||
|
||||
void stopBrowserAccess(const char* browserName) {
|
||||
@autoreleasepool {
|
||||
NSString* name = [NSString stringWithUTF8String:browserName];
|
||||
[getManager() stopAccessingBrowser:name];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
#ifndef HAS_STORED_ACCESS_H
|
||||
#define HAS_STORED_ACCESS_H
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
void hasStoredAccessCommand(void *context, NSDictionary *params);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,17 @@
|
||||
#import <Foundation/Foundation.h>
|
||||
#import "../../interop.h"
|
||||
#import "../browser_access_manager.h"
|
||||
#import "has_stored_access.h"
|
||||
|
||||
void hasStoredAccessCommand(void* context, NSDictionary *params) {
|
||||
NSString *browserName = params[@"browserName"];
|
||||
|
||||
if (!browserName) {
|
||||
return _return(context, _error(@"Missing required parameter: browserName"));
|
||||
}
|
||||
|
||||
BrowserAccessManager *manager = [[BrowserAccessManager alloc] init];
|
||||
BOOL hasAccess = [manager hasStoredAccess:browserName];
|
||||
|
||||
_return(context, _success(@{@"hasAccess": @(hasAccess)}));
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
#ifndef REQUEST_ACCESS_H
|
||||
#define REQUEST_ACCESS_H
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
void requestAccessCommand(void *context, NSDictionary *params);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,22 @@
|
||||
#import <Foundation/Foundation.h>
|
||||
#import "../../interop.h"
|
||||
#import "../browser_access_manager.h"
|
||||
#import "request_access.h"
|
||||
|
||||
void requestAccessCommand(void* context, NSDictionary *params) {
|
||||
NSString *browserName = params[@"browserName"];
|
||||
NSString *relativePath = params[@"relativePath"];
|
||||
|
||||
if (!browserName || !relativePath) {
|
||||
return _return(context, _error(@"Missing required parameters: browserName and relativePath"));
|
||||
}
|
||||
|
||||
BrowserAccessManager *manager = [[BrowserAccessManager alloc] init];
|
||||
NSString *bookmarkData = [manager requestAccessToBrowserDir:browserName relativePath:relativePath];
|
||||
|
||||
if (bookmarkData == nil) {
|
||||
return _return(context, _error(@"User denied access or selected an invalid browser directory"));
|
||||
}
|
||||
|
||||
_return(context, _success(@{@"bookmark": bookmarkData}));
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
#ifndef START_ACCESS_H
|
||||
#define START_ACCESS_H
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
void startAccessCommand(void *context, NSDictionary *params);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,21 @@
|
||||
#import <Foundation/Foundation.h>
|
||||
#import "../../interop.h"
|
||||
#import "../browser_access_manager.h"
|
||||
#import "start_access.h"
|
||||
|
||||
void startAccessCommand(void* context, NSDictionary *params) {
|
||||
NSString *browserName = params[@"browserName"];
|
||||
|
||||
if (!browserName) {
|
||||
return _return(context, _error(@"Missing required parameter: browserName"));
|
||||
}
|
||||
|
||||
BrowserAccessManager *manager = [[BrowserAccessManager alloc] init];
|
||||
NSString *resolvedPath = [manager startAccessingBrowser:browserName];
|
||||
|
||||
if (resolvedPath == nil) {
|
||||
return _return(context, _error(@"Failed to start accessing browser. Bookmark may be invalid or revoked"));
|
||||
}
|
||||
|
||||
_return(context, _success(@{@"path": resolvedPath}));
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
#ifndef STOP_ACCESS_H
|
||||
#define STOP_ACCESS_H
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
void stopAccessCommand(void *context, NSDictionary *params);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,17 @@
|
||||
#import <Foundation/Foundation.h>
|
||||
#import "../../interop.h"
|
||||
#import "../browser_access_manager.h"
|
||||
#import "stop_access.h"
|
||||
|
||||
void stopAccessCommand(void* context, NSDictionary *params) {
|
||||
NSString *browserName = params[@"browserName"];
|
||||
|
||||
if (!browserName) {
|
||||
return _return(context, _error(@"Missing required parameter: browserName"));
|
||||
}
|
||||
|
||||
BrowserAccessManager *manager = [[BrowserAccessManager alloc] init];
|
||||
[manager stopAccessingBrowser:browserName];
|
||||
|
||||
_return(context, _success(@{}));
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
#ifndef RUN_CHROMIUM_COMMAND_H
|
||||
#define RUN_CHROMIUM_COMMAND_H
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
void runChromiumCommand(void* context, NSDictionary *input);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,25 @@
|
||||
#import <Foundation/Foundation.h>
|
||||
#import "commands/request_access.h"
|
||||
#import "commands/has_stored_access.h"
|
||||
#import "commands/start_access.h"
|
||||
#import "commands/stop_access.h"
|
||||
#import "../interop.h"
|
||||
#import "../utils.h"
|
||||
#import "run_chromium_command.h"
|
||||
|
||||
void runChromiumCommand(void* context, NSDictionary *input) {
|
||||
NSString *command = input[@"command"];
|
||||
NSDictionary *params = input[@"params"];
|
||||
|
||||
if ([command isEqual:@"request_access"]) {
|
||||
return requestAccessCommand(context, params);
|
||||
} else if ([command isEqual:@"has_stored_access"]) {
|
||||
return hasStoredAccessCommand(context, params);
|
||||
} else if ([command isEqual:@"start_access"]) {
|
||||
return startAccessCommand(context, params);
|
||||
} else if ([command isEqual:@"stop_access"]) {
|
||||
return stopAccessCommand(context, params);
|
||||
}
|
||||
|
||||
_return(context, _error([NSString stringWithFormat:@"Unknown command: %@", command]));
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
#import <Foundation/Foundation.h>
|
||||
#import "autofill/run_autofill_command.h"
|
||||
#import "chromium_importer/run_chromium_command.h"
|
||||
#import "interop.h"
|
||||
#import "utils.h"
|
||||
|
||||
@@ -8,6 +9,8 @@ void pickAndRunCommand(void* context, NSDictionary *input) {
|
||||
|
||||
if ([namespace isEqual:@"autofill"]) {
|
||||
return runAutofillCommand(context, input);
|
||||
} else if ([namespace isEqual:@"chromium_importer"]) {
|
||||
return runChromiumCommand(context, input);
|
||||
}
|
||||
|
||||
_return(context, _error([NSString stringWithFormat:@"Unknown namespace: %@", namespace]));
|
||||
|
||||
Reference in New Issue
Block a user