#import #import "native/v8_extensions/atom.h" #import "native/v8_extensions/native.h" #import "native/v8_extensions/onig_reg_exp.h" #import "native/v8_extensions/onig_scanner.h" #import "native/v8_extensions/git.h" #import "native/v8_extensions/tags.h" #import "native/message_translation.h" #import "path_watcher.h" #import "atom_cef_render_process_handler.h" void AtomCefRenderProcessHandler::OnWebKitInitialized() { } void AtomCefRenderProcessHandler::OnContextCreated(CefRefPtr browser, CefRefPtr frame, CefRefPtr context) { InjectExtensionsIntoV8Context(context); } void AtomCefRenderProcessHandler::OnContextReleased(CefRefPtr browser, CefRefPtr frame, CefRefPtr context) { [PathWatcher removePathWatcherForContext:context]; } void AtomCefRenderProcessHandler::OnWorkerContextCreated(int worker_id, const CefString& url, CefRefPtr context) { InjectExtensionsIntoV8Context(context); } void AtomCefRenderProcessHandler::OnWorkerContextReleased(int worker_id, const CefString& url, CefRefPtr context) { } void AtomCefRenderProcessHandler::OnWorkerUncaughtException(int worker_id, const CefString& url, CefRefPtr context, CefRefPtr exception, CefRefPtr stackTrace) { std::string message = exception->GetMessage().ToString(); NSLog(@"Exception throw in worker thread %s", message.c_str()); } bool AtomCefRenderProcessHandler::OnProcessMessageReceived(CefRefPtr browser, CefProcessId source_process, CefRefPtr message) { std::string name = message->GetName().ToString(); if (name == "reload") { Reload(browser); return true; } else if (name == "shutdown") { Shutdown(browser); return true; } else { return CallMessageReceivedHandler(browser->GetMainFrame()->GetV8Context(), message); } } void AtomCefRenderProcessHandler::Reload(CefRefPtr browser) { CefRefPtr context = browser->GetMainFrame()->GetV8Context(); CefRefPtr global = context->GetGlobal(); context->Enter(); CefV8ValueList arguments; CefRefPtr reloadFunction = global->GetValue("reload"); reloadFunction->ExecuteFunction(global, arguments); if (!reloadFunction->IsFunction() || reloadFunction->HasException()) { browser->ReloadIgnoreCache(); } context->Exit(); } void AtomCefRenderProcessHandler::Shutdown(CefRefPtr browser) { CefRefPtr context = browser->GetMainFrame()->GetV8Context(); CefRefPtr global = context->GetGlobal(); context->Enter(); CefV8ValueList arguments; CefRefPtr shutdownFunction = global->GetValue("shutdown"); shutdownFunction->ExecuteFunction(global, arguments); context->Exit(); } bool AtomCefRenderProcessHandler::CallMessageReceivedHandler(CefRefPtr context, CefRefPtr message) { context->Enter(); CefRefPtr atom = context->GetGlobal()->GetValue("atom"); CefRefPtr receiveFn = atom->GetValue("receiveMessageFromBrowserProcess"); CefV8ValueList arguments; arguments.push_back(CefV8Value::CreateString(message->GetName().ToString())); CefRefPtr messageArguments = message->GetArgumentList(); if (messageArguments->GetSize() > 0) { CefRefPtr data = CefV8Value::CreateArray(messageArguments->GetSize()); TranslateList(messageArguments, data); arguments.push_back(data); } receiveFn->ExecuteFunction(atom, arguments); context->Exit(); if (receiveFn->HasException()) { std::cout << "ERROR: Exception in JS receiving message " << message->GetName().ToString() << "\n"; return false; } else { return true; } } void AtomCefRenderProcessHandler::InjectExtensionsIntoV8Context(CefRefPtr context) { // these objects are deleted when the context removes all references to them (new v8_extensions::Atom())->CreateContextBinding(context); (new v8_extensions::Native())->CreateContextBinding(context); (new v8_extensions::Git())->CreateContextBinding(context); (new v8_extensions::OnigRegExp())->CreateContextBinding(context); (new v8_extensions::OnigScanner())->CreateContextBinding(context); (new v8_extensions::Tags())->CreateContextBinding(context); }