From 196aab1690ded4cb60d607f99d1598b95a55ef2b Mon Sep 17 00:00:00 2001 From: Corey Johnson Date: Wed, 6 Jun 2012 15:20:36 -0700 Subject: [PATCH] $native is now created once and registered as a CEF Extension. --- Atom/src/Atom.mm | 4 +--- Atom/src/AtomController.mm | 4 ---- Atom/src/native_handler.mm | 15 +++++++++------ 3 files changed, 10 insertions(+), 13 deletions(-) diff --git a/Atom/src/Atom.mm b/Atom/src/Atom.mm index 7b563e796..d78856627 100755 --- a/Atom/src/Atom.mm +++ b/Atom/src/Atom.mm @@ -93,6 +93,7 @@ } - (void)applicationWillFinishLaunching:(NSNotification *)notification { + CefRefPtr nativeHandler = new NativeHandler(); [self createAtomContext]; } @@ -107,9 +108,6 @@ CefRefPtr bootstrapScript = CefV8Value::CreateString("atom-bootstrap"); global->SetValue("$bootstrapScript", bootstrapScript, V8_PROPERTY_ATTRIBUTE_NONE); - CefRefPtr nativeHandler = new NativeHandler(); - global->SetValue("$native", nativeHandler->m_object, V8_PROPERTY_ATTRIBUTE_NONE); - CefRefPtr atom = CefV8Value::CreateObject(NULL, NULL); global->SetValue("atom", atom, V8_PROPERTY_ATTRIBUTE_NONE); diff --git a/Atom/src/AtomController.mm b/Atom/src/AtomController.mm index 1984ecb31..16ca86946 100644 --- a/Atom/src/AtomController.mm +++ b/Atom/src/AtomController.mm @@ -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 bootstrapScript = CefV8Value::CreateString([_bootstrapScript UTF8String]); global->SetValue("$bootstrapScript", bootstrapScript, V8_PROPERTY_ATTRIBUTE_NONE); - CefRefPtr nativeHandler = new NativeHandler(); - global->SetValue("$native", nativeHandler->m_object, V8_PROPERTY_ATTRIBUTE_NONE); - CefRefPtr pathToOpen = _pathToOpen ? CefV8Value::CreateString([_pathToOpen UTF8String]) : CefV8Value::CreateNull(); global->SetValue("$pathToOpen", pathToOpen, V8_PROPERTY_ATTRIBUTE_NONE); diff --git a/Atom/src/native_handler.mm b/Atom/src/native_handler.mm index 31038e8ee..5cbf75027 100644 --- a/Atom/src/native_handler.mm +++ b/Atom/src/native_handler.mm @@ -14,18 +14,21 @@ NSString *stringFromCefV8Value(const CefRefPtr& 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 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 object, const CefV8ValueList& arguments,