mirror of
https://github.com/atom/atom.git
synced 2026-01-24 14:28:14 -05:00
85 lines
2.7 KiB
Plaintext
85 lines
2.7 KiB
Plaintext
#import "native_handler.h"
|
|
#import "include/cef.h"
|
|
|
|
NSString *stringFromCefV8Value(const CefRefPtr<CefV8Value>& value) {
|
|
std::string cc_value = value->GetStringValue().ToString();
|
|
return [NSString stringWithUTF8String:cc_value.c_str()];
|
|
}
|
|
|
|
NativeHandler::NativeHandler() : CefV8Handler() {
|
|
m_object = CefV8Value::CreateObject(NULL);
|
|
|
|
const char *functionNames[] = {"exists", "read", "absolute", "list"};
|
|
NSUInteger arrayLength = sizeof(functionNames) / sizeof(const char *);
|
|
for (NSUInteger i = 0; i < arrayLength; i++) {
|
|
const char *functionName = functionNames[i];
|
|
CefRefPtr<CefV8Value> function = CefV8Value::CreateFunction(functionName, this);
|
|
m_object->SetValue(functionName, function, V8_PROPERTY_ATTRIBUTE_NONE);
|
|
}
|
|
}
|
|
|
|
|
|
bool NativeHandler::Execute(const CefString& name,
|
|
CefRefPtr<CefV8Value> object,
|
|
const CefV8ValueList& arguments,
|
|
CefRefPtr<CefV8Value>& retval,
|
|
CefString& exception)
|
|
{
|
|
if (name == "exists") {
|
|
NSString *path = stringFromCefV8Value(arguments[0]);
|
|
bool exists = [[NSFileManager defaultManager] fileExistsAtPath:path isDirectory:nil];
|
|
retval = CefV8Value::CreateBool(exists);
|
|
|
|
return true;
|
|
}
|
|
else if (name == "read") {
|
|
NSString *path = stringFromCefV8Value(arguments[0]);
|
|
|
|
NSError *error = nil;
|
|
NSString *contents = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error];
|
|
|
|
if (error) {
|
|
exception = [[error localizedDescription] UTF8String];
|
|
}
|
|
else {
|
|
retval = CefV8Value::CreateString([contents UTF8String]);
|
|
}
|
|
|
|
return true;
|
|
}
|
|
else if (name == "absolute") {
|
|
NSString *path = stringFromCefV8Value(arguments[0]);
|
|
|
|
path = [path stringByStandardizingPath];
|
|
if ([path characterAtIndex:0] == '/') {
|
|
retval = CefV8Value::CreateString([path UTF8String]);
|
|
}
|
|
|
|
return true;
|
|
}
|
|
else if (name == "list") {
|
|
NSString *path = stringFromCefV8Value(arguments[0]);
|
|
|
|
NSFileManager *fm = [NSFileManager defaultManager];
|
|
NSArray *relativePaths = [NSArray array];
|
|
NSError *error = nil;
|
|
relativePaths = [fm contentsOfDirectoryAtPath:path error:&error];
|
|
|
|
if (error) {
|
|
exception = [[error localizedDescription] UTF8String];
|
|
}
|
|
else {
|
|
retval = CefV8Value::CreateArray();
|
|
for (NSUInteger i = 0; i < relativePaths.count; i++) {
|
|
NSString *relativePath = [relativePaths objectAtIndex:i];
|
|
NSString *fullPath = [path stringByAppendingString:relativePath];
|
|
retval->SetValue(i, CefV8Value::CreateString([fullPath UTF8String]));
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
};
|