Merge pull request #36 from github/native-handler.js

Move native function declarations to js file
This commit is contained in:
Corey Johnson
2012-08-23 10:47:51 -07:00
3 changed files with 105 additions and 13 deletions

View File

@@ -67,6 +67,14 @@ NativeHandler::NativeHandler() :
object->SetValue(functionName, function, V8_PROPERTY_ATTRIBUTE_NONE);
}
string nativePath = io_utils_real_app_path(
"/../src/stdlib/native-handler.js");
if (!nativePath.empty()) {
string extensionCode;
if (io_utils_read(nativePath, &extensionCode) > 0)
CefRegisterExtension("v8/native-handler", extensionCode, this);
}
notifyFd = inotify_init();
if (notifyFd != -1)
g_thread_create_full(NotifyWatchersCallback, this, 0, true, false,

View File

@@ -31,19 +31,9 @@ void throwException(const CefRefPtr<CefV8Value>& global, CefRefPtr<CefV8Exceptio
}
NativeHandler::NativeHandler() : CefV8Handler() {
std::string extensionCode = "var $native = {}; (function() {";
const char *functionNames[] = {"exists", "alert", "read", "write", "absolute", "list", "isFile", "isDirectory", "remove", "asyncList", "open", "openDialog", "quit", "writeToPasteboard", "readFromPasteboard", "showDevTools", "toggleDevTools", "newWindow", "saveDialog", "exit", "watchPath", "unwatchPath", "makeDirectory", "move", "moveToTrash", "reload", "lastModified", "md5ForPath", "exec", "getPlatform"};
NSUInteger arrayLength = sizeof(functionNames) / sizeof(const char *);
for (NSUInteger i = 0; i < arrayLength; i++) {
std::string functionName = std::string(functionNames[i]);
extensionCode += "native function " + functionName + "(); $native." + functionName + " = " + functionName + ";";
}
extensionCode += "})();";
// Register the extension.
CefRegisterExtension("v8/test", extensionCode, this);
NSString *filePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"src/stdlib/native-handler.js"];
NSString *extensionCode = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
CefRegisterExtension("v8/native-handler", [extensionCode UTF8String], this);
}
bool NativeHandler::Execute(const CefString& name,

View File

@@ -0,0 +1,94 @@
var $native = {};
(function() {
native function exists(path);
$native.exists = exists;
native function alert(message, detailedMessage, buttonNamesAndCallbacks);
$native.alert = alert;
native function read(path);
$native.read = read;
native function write(path, content);
$native.write = write;
native function absolute(path);
$native.absolute = absolute;
native function list(path, recursive);
$native.list = list;
native function isFile(path);
$native.isFile = isFile;
native function isDirectory(path);
$native.isDirectory = isDirectory;
native function remove(path);
$native.remove = remove;
native function asyncList(path, recursive, callback);
$native.asyncList = asyncList;
native function open(path);
$native.open = open;
native function openDialog();
$native.openDialog = openDialog;
native function quit();
$native.quit = quit;
native function writeToPasteboard(text);
$native.writeToPasteboard = writeToPasteboard;
native function readFromPasteboard();
$native.readFromPasteboard = readFromPasteboard;
native function showDevTools();
$native.showDevTools = showDevTools;
native function toggleDevTools();
$native.toggleDevTools = toggleDevTools;
native function newWindow();
$native.newWindow = newWindow;
native function saveDialog();
$native.saveDialog = saveDialog;
native function exit(status);
$native.exit = exit;
native function watchPath(path);
$native.watchPath = watchPath;
native function unwatchPath(path, callbackId);
$native.unwatchPath = unwatchPath;
native function makeDirectory(path);
$native.makeDirectory = makeDirectory;
native function move(sourcePath, targetPath);
$native.move = move;
native function moveToTrash(path);
$native.moveToTrash = moveToTrash;
native function reload();
$native.reload = reload;
native function lastModified(path);
$native.lastModified = lastModified;
native function md5ForPath(path);
$native.md5ForPath = md5ForPath;
native function exec(command, options, callback);
$native.exec = exec;
native function getPlatform();
$native.getPlatform = getPlatform;
})();