$native is now created once and registered as a CEF Extension.

This commit is contained in:
Corey Johnson
2012-06-06 15:20:36 -07:00
parent 4885af4fa8
commit 196aab1690
3 changed files with 10 additions and 13 deletions

View File

@@ -93,6 +93,7 @@
}
- (void)applicationWillFinishLaunching:(NSNotification *)notification {
CefRefPtr<CefV8Handler> nativeHandler = new NativeHandler();
[self createAtomContext];
}
@@ -107,9 +108,6 @@
CefRefPtr<CefV8Value> bootstrapScript = CefV8Value::CreateString("atom-bootstrap");
global->SetValue("$bootstrapScript", bootstrapScript, V8_PROPERTY_ATTRIBUTE_NONE);
CefRefPtr<NativeHandler> nativeHandler = new NativeHandler();
global->SetValue("$native", nativeHandler->m_object, V8_PROPERTY_ATTRIBUTE_NONE);
CefRefPtr<CefV8Value> atom = CefV8Value::CreateObject(NULL, NULL);
global->SetValue("atom", atom, V8_PROPERTY_ATTRIBUTE_NONE);

View File

@@ -2,7 +2,6 @@
#import "include/cef_base.h"
#import "client_handler.h"
#import "native_handler.h"
#import "PathWatcher.h"
@implementation AtomController
@@ -76,9 +75,6 @@
CefRefPtr<CefV8Value> bootstrapScript = CefV8Value::CreateString([_bootstrapScript UTF8String]);
global->SetValue("$bootstrapScript", bootstrapScript, V8_PROPERTY_ATTRIBUTE_NONE);
CefRefPtr<NativeHandler> nativeHandler = new NativeHandler();
global->SetValue("$native", nativeHandler->m_object, V8_PROPERTY_ATTRIBUTE_NONE);
CefRefPtr<CefV8Value> pathToOpen = _pathToOpen ? CefV8Value::CreateString([_pathToOpen UTF8String]) : CefV8Value::CreateNull();
global->SetValue("$pathToOpen", pathToOpen, V8_PROPERTY_ATTRIBUTE_NONE);

View File

@@ -14,18 +14,21 @@ NSString *stringFromCefV8Value(const CefRefPtr<CefV8Value>& value) {
}
NativeHandler::NativeHandler() : CefV8Handler() {
m_object = CefV8Value::CreateObject(NULL, NULL);
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", "newWindow", "saveDialog", "exit", "watchPath", "unwatchPath", "makeDirectory", "move", "moveToTrash"};
const char *functionNames[] = {"exists", "alert", "read", "write", "absolute", "list", "isFile", "isDirectory", "remove", "asyncList", "open", "openDialog", "quit", "writeToPasteboard", "readFromPasteboard", "showDevTools", "newWindow", "saveDialog", "exit", "watchPath", "unwatchPath", "makeDirectory", "move", "moveToTrash"};
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);
std::string functionName = std::string(functionNames[i]);
extensionCode += "native function " + functionName + "(); $native." + functionName + " = " + functionName + ";";
}
extensionCode += "})();";
// Register the extension.
CefRegisterExtension("v8/test", extensionCode, this);
}
bool NativeHandler::Execute(const CefString& name,
CefRefPtr<CefV8Value> object,
const CefV8ValueList& arguments,