diff --git a/native/atom_cef_render_process_handler.mm b/native/atom_cef_render_process_handler.mm index a968dbd7c..1db151518 100644 --- a/native/atom_cef_render_process_handler.mm +++ b/native/atom_cef_render_process_handler.mm @@ -11,7 +11,6 @@ void AtomCefRenderProcessHandler::OnWebKitInitialized() { new v8_extensions::Atom(); - new v8_extensions::Native(); new v8_extensions::OnigRegExp(); new v8_extensions::OnigScanner(); new v8_extensions::Git(); @@ -19,8 +18,9 @@ void AtomCefRenderProcessHandler::OnWebKitInitialized() { } void AtomCefRenderProcessHandler::OnContextCreated(CefRefPtr browser, - CefRefPtr frame, - CefRefPtr context) { + CefRefPtr frame, + CefRefPtr context) { + v8_extensions::Native::CreateContextBinding(context); } void AtomCefRenderProcessHandler::OnContextReleased(CefRefPtr browser, diff --git a/native/v8_extensions/native.h b/native/v8_extensions/native.h index e7a200493..629f2803a 100644 --- a/native/v8_extensions/native.h +++ b/native/v8_extensions/native.h @@ -6,8 +6,8 @@ namespace v8_extensions { class Native : public CefV8Handler { public: - Native(); + static void CreateContextBinding(CefRefPtr context); virtual bool Execute(const CefString& name, CefRefPtr object, const CefV8ValueList& arguments, @@ -18,6 +18,10 @@ public: IMPLEMENT_REFCOUNTING(Native); private: + Native(); + Native(Native const&); + static CefRefPtr GetInstance(); + void operator=(Native const&); std::string windowState; }; diff --git a/native/v8_extensions/native.js b/native/v8_extensions/native.js deleted file mode 100644 index 9124d89fc..000000000 --- a/native/v8_extensions/native.js +++ /dev/null @@ -1,85 +0,0 @@ -var $native = {}; -(function() { - - native function exists(path); - $native.exists = exists; - - native function read(path); - $native.read = read; - - native function write(path, content); - $native.write = write; - - native function absolute(path); - $native.absolute = absolute; - - native function traverseTree(path, onFile, onDirectory); - $native.traverseTree = traverseTree; - - native function getAllFilePathsAsync(path, callback); - $native.getAllFilePathsAsync = getAllFilePathsAsync; - - native function isFile(path); - $native.isFile = isFile; - - native function isDirectory(path); - $native.isDirectory = isDirectory; - - native function remove(path); - $native.remove = remove; - - native function open(path); - $native.open = open; - - native function quit(); - $native.quit = quit; - - native function writeToPasteboard(text); - $native.writeToPasteboard = writeToPasteboard; - - native function readFromPasteboard(); - $native.readFromPasteboard = readFromPasteboard; - - native function watchPath(path); - $native.watchPath = watchPath; - - native function unwatchPath(path, callbackId); - $native.unwatchPath = unwatchPath; - - native function getWatchedPaths(); - $native.getWatchedPaths = getWatchedPaths; - - native function unwatchAllPaths(); - $native.unwatchAllPaths = unwatchAllPaths; - - 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; - - native function setWindowState(state); - $native.setWindowState = setWindowState; - - native function getWindowState(); - $native.getWindowState = getWindowState; - -})(); diff --git a/native/v8_extensions/native.mm b/native/v8_extensions/native.mm index 0a66591ca..82609b380 100644 --- a/native/v8_extensions/native.mm +++ b/native/v8_extensions/native.mm @@ -13,26 +13,41 @@ namespace v8_extensions { -NSString *stringFromCefV8Value(const CefRefPtr& value) { - std::string cc_value = value->GetStringValue().ToString(); - return [NSString stringWithUTF8String:cc_value.c_str()]; -} +using namespace std; -void throwException(const CefRefPtr& global, CefRefPtr exception, NSString *message) { - CefV8ValueList arguments; - - message = [message stringByAppendingFormat:@"\n%s", exception->GetMessage().ToString().c_str()]; - arguments.push_back(CefV8Value::CreateString(std::string([message UTF8String], [message lengthOfBytesUsingEncoding:NSUTF8StringEncoding]))); - - CefRefPtr console = global->GetValue("console"); - console->GetValue("error")->ExecuteFunction(console, arguments); -} +NSString *stringFromCefV8Value(const CefRefPtr& value); +void throwException(const CefRefPtr& global, CefRefPtr exception, NSString *message); Native::Native() : CefV8Handler() { NSString *filePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"v8_extensions/native.js"]; NSString *extensionCode = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil]; windowState = "{}"; - CefRegisterExtension("v8/native", [extensionCode UTF8String], this); +} + +void Native::CreateContextBinding(CefRefPtr context) { + const char* methodNames[] = { + "exists", "read", "write", "absolute", "getAllFilePathsAsync", "traverseTree", "isDirectory", + "isFile", "remove", "writeToPasteboard", "readFromPasteboard", "quit", "watchPath", "unwatchPath", + "getWatchedPaths", "unwatchAllPaths", "makeDirectory", "move", "moveToTrash", "reload", "lastModified", + "md5ForPath", "exec", "getPlatform", "setWindowState", "getWindowState" + }; + + CefRefPtr nativeObject = CefV8Value::CreateObject(NULL); + int arrayLength = sizeof(methodNames) / sizeof(const char *); + for (int i = 0; i < arrayLength; i++) { + const char *functionName = methodNames[i]; + CefRefPtr function = CefV8Value::CreateFunction(functionName, GetInstance()); + nativeObject->SetValue(functionName, function, V8_PROPERTY_ATTRIBUTE_NONE); + } + + CefRefPtr global = context->GetGlobal(); + global->SetValue("$native", nativeObject, V8_PROPERTY_ATTRIBUTE_NONE); +} + +CefRefPtr Native::GetInstance() { + static Native instance; + static CefRefPtr instancePtr = CefRefPtr(&instance); + return instancePtr; } bool Native::Execute(const CefString& name, @@ -500,4 +515,19 @@ bool Native::Execute(const CefString& name, return false; }; +NSString *stringFromCefV8Value(const CefRefPtr& value) { + std::string cc_value = value->GetStringValue().ToString(); + return [NSString stringWithUTF8String:cc_value.c_str()]; +} + +void throwException(const CefRefPtr& global, CefRefPtr exception, NSString *message) { + CefV8ValueList arguments; + + message = [message stringByAppendingFormat:@"\n%s", exception->GetMessage().ToString().c_str()]; + arguments.push_back(CefV8Value::CreateString(std::string([message UTF8String], [message lengthOfBytesUsingEncoding:NSUTF8StringEncoding]))); + + CefRefPtr console = global->GetValue("console"); + console->GetValue("error")->ExecuteFunction(console, arguments); +} + } // namespace v8_extensions