mirror of
https://github.com/pulsar-edit/pulsar.git
synced 2024-09-21 07:58:04 +03:00
Use window binding instead of v8 extension for $native
object
This paves the way to inject `$native` into the context of a web worker
This commit is contained in:
parent
d9cfc491e0
commit
87829043d4
@ -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();
|
||||
@ -21,6 +20,7 @@ void AtomCefRenderProcessHandler::OnWebKitInitialized() {
|
||||
void AtomCefRenderProcessHandler::OnContextCreated(CefRefPtr<CefBrowser> browser,
|
||||
CefRefPtr<CefFrame> frame,
|
||||
CefRefPtr<CefV8Context> context) {
|
||||
v8_extensions::Native::CreateContextBinding(context);
|
||||
}
|
||||
|
||||
void AtomCefRenderProcessHandler::OnContextReleased(CefRefPtr<CefBrowser> browser,
|
||||
|
@ -6,8 +6,8 @@ namespace v8_extensions {
|
||||
|
||||
class Native : public CefV8Handler {
|
||||
public:
|
||||
Native();
|
||||
|
||||
static void CreateContextBinding(CefRefPtr<CefV8Context> context);
|
||||
virtual bool Execute(const CefString& name,
|
||||
CefRefPtr<CefV8Value> object,
|
||||
const CefV8ValueList& arguments,
|
||||
@ -18,6 +18,10 @@ public:
|
||||
IMPLEMENT_REFCOUNTING(Native);
|
||||
|
||||
private:
|
||||
Native();
|
||||
Native(Native const&);
|
||||
static CefRefPtr<CefV8Handler> GetInstance();
|
||||
void operator=(Native const&);
|
||||
std::string windowState;
|
||||
};
|
||||
|
||||
|
@ -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;
|
||||
|
||||
})();
|
@ -13,26 +13,41 @@
|
||||
|
||||
namespace v8_extensions {
|
||||
|
||||
NSString *stringFromCefV8Value(const CefRefPtr<CefV8Value>& value) {
|
||||
std::string cc_value = value->GetStringValue().ToString();
|
||||
return [NSString stringWithUTF8String:cc_value.c_str()];
|
||||
}
|
||||
using namespace std;
|
||||
|
||||
void throwException(const CefRefPtr<CefV8Value>& global, CefRefPtr<CefV8Exception> 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<CefV8Value> console = global->GetValue("console");
|
||||
console->GetValue("error")->ExecuteFunction(console, arguments);
|
||||
}
|
||||
NSString *stringFromCefV8Value(const CefRefPtr<CefV8Value>& value);
|
||||
void throwException(const CefRefPtr<CefV8Value>& global, CefRefPtr<CefV8Exception> 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<CefV8Context> 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<CefV8Value> nativeObject = CefV8Value::CreateObject(NULL);
|
||||
int arrayLength = sizeof(methodNames) / sizeof(const char *);
|
||||
for (int i = 0; i < arrayLength; i++) {
|
||||
const char *functionName = methodNames[i];
|
||||
CefRefPtr<CefV8Value> function = CefV8Value::CreateFunction(functionName, GetInstance());
|
||||
nativeObject->SetValue(functionName, function, V8_PROPERTY_ATTRIBUTE_NONE);
|
||||
}
|
||||
|
||||
CefRefPtr<CefV8Value> global = context->GetGlobal();
|
||||
global->SetValue("$native", nativeObject, V8_PROPERTY_ATTRIBUTE_NONE);
|
||||
}
|
||||
|
||||
CefRefPtr<CefV8Handler> Native::GetInstance() {
|
||||
static Native instance;
|
||||
static CefRefPtr<CefV8Handler> instancePtr = CefRefPtr<CefV8Handler>(&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<CefV8Value>& value) {
|
||||
std::string cc_value = value->GetStringValue().ToString();
|
||||
return [NSString stringWithUTF8String:cc_value.c_str()];
|
||||
}
|
||||
|
||||
void throwException(const CefRefPtr<CefV8Value>& global, CefRefPtr<CefV8Exception> 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<CefV8Value> console = global->GetValue("console");
|
||||
console->GetValue("error")->ExecuteFunction(console, arguments);
|
||||
}
|
||||
|
||||
} // namespace v8_extensions
|
||||
|
Loading…
Reference in New Issue
Block a user