Add atom extension, which implements atom.open

We send a message from the render process to the browser process indicating which path to open. Also start introducing a `v8_extensions::` namespace to contain the extensions.
This commit is contained in:
Nathan Sobo
2012-08-28 11:08:50 -05:00
parent 8cd4042fe9
commit 76b8f42bb7
9 changed files with 101 additions and 13 deletions

View File

@@ -1,33 +1,32 @@
#include "atom_cef_app.h"
#import "native/v8_extensions/atom.h"
#import "native/v8_extensions/native.h"
#import "native/v8_extensions/onig_reg_exp.h"
#include <iostream>
void AtomCefApp::OnWebKitInitialized() {
new v8_extensions::Atom();
new NativeHandler();
new OnigRegexpExtension();
new OnigRegexpExtension();
}
void AtomCefApp::OnContextCreated(CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame> frame,
CefRefPtr<CefV8Context> context) {
CefRefPtr<CefV8Value> global = context->GetGlobal();
CefRefPtr<CefV8Value> atom = CefV8Value::CreateObject(NULL);
CefRefPtr<CefV8Context> context) {
#ifdef RESOURCE_PATH
CefRefPtr<CefV8Value> resourcePath = CefV8Value::CreateString(RESOURCE_PATH);
#else
CefRefPtr<CefV8Value> resourcePath = CefV8Value::CreateString([[[NSBundle mainBundle] resourcePath] UTF8String]);
#endif
atom->SetValue("resourcePath", resourcePath, V8_PROPERTY_ATTRIBUTE_NONE);
global->SetValue("atom", atom, V8_PROPERTY_ATTRIBUTE_NONE);
CefRefPtr<CefV8Value> global = context->GetGlobal();
CefRefPtr<CefV8Value> atom = global->GetValue("atom");
atom->SetValue("resourcePath", resourcePath, V8_PROPERTY_ATTRIBUTE_NONE);
}
bool AtomCefApp::OnProcessMessageReceived(CefRefPtr<CefBrowser> browser,
CefProcessId source_process,
CefRefPtr<CefProcessMessage> message) {
if (message->GetName().ToString() == "reload") {
Reload(browser);
}

View File

@@ -17,6 +17,20 @@ AtomCefClient::AtomCefClient(){
AtomCefClient::~AtomCefClient() {
}
bool AtomCefClient::OnProcessMessageReceived(CefRefPtr<CefBrowser> browser,
CefProcessId source_process,
CefRefPtr<CefProcessMessage> message) {
std::string name = message->GetName().ToString();
if (name == "open") {
Open(message->GetArgumentList()->GetString(0));
return true;
}
return false;
}
void AtomCefClient::OnBeforeContextMenu(
CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame> frame,

View File

@@ -49,7 +49,12 @@ class AtomCefClient : public CefClient,
virtual CefRefPtr<CefRequestHandler> GetRequestHandler() OVERRIDE {
return this;
}
virtual bool OnProcessMessageReceived(CefRefPtr<CefBrowser> browser,
CefProcessId source_process,
CefRefPtr<CefProcessMessage> message) OVERRIDE;
// CefContextMenuHandler methods
virtual void OnBeforeContextMenu(CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame> frame,
@@ -99,7 +104,9 @@ class AtomCefClient : public CefClient,
CefRefPtr<CefBrowser> m_Browser;
void ShowDevTools(CefRefPtr<CefBrowser> browser);
void Open(std::string path);
IMPLEMENT_REFCOUNTING(AtomCefClient);
IMPLEMENT_LOCKING(AtomCefClient);
};

View File

@@ -2,5 +2,9 @@
#import "include/cef_browser.h"
#import "include/cef_frame.h"
#import "native/atom_cef_client.h"
#import "atom_application.h"
void AtomCefClient::Open(std::string path) {
NSString *pathString = [NSString stringWithCString:path.c_str() encoding:NSUTF8StringEncoding];
[(AtomApplication *)[AtomApplication sharedApplication] open:pathString];
}

View File

@@ -0,0 +1,19 @@
#import <Cocoa/Cocoa.h>
#import "include/cef_base.h"
#import "include/cef_v8.h"
namespace v8_extensions {
class Atom : public CefV8Handler {
public:
Atom();
virtual bool Execute(const CefString& name,
CefRefPtr<CefV8Value> object,
const CefV8ValueList& arguments,
CefRefPtr<CefV8Value>& retval,
CefString& exception) OVERRIDE;
// Provide the reference counting implementation for this class.
IMPLEMENT_REFCOUNTING(Atom);
};
}

View File

@@ -0,0 +1,9 @@
(function () {
native function open(path);
this.atom = {
open: open
};
})();

View File

@@ -0,0 +1,32 @@
#import <Cocoa/Cocoa.h>
#import <dispatch/dispatch.h>
#import "atom.h"
#import "atom_application.h"
#import "util.h"
namespace v8_extensions {
v8_extensions::Atom::Atom() : CefV8Handler() {
NSString *filePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"v8_extensions/atom.js"];
NSString *extensionCode = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
CefRegisterExtension("v8/atom", [extensionCode UTF8String], this);
}
bool v8_extensions::Atom::Execute(const CefString& name,
CefRefPtr<CefV8Value> object,
const CefV8ValueList& arguments,
CefRefPtr<CefV8Value>& retval,
CefString& exception) {
CefRefPtr<CefBrowser> browser = CefV8Context::GetCurrentContext()->GetBrowser();
if (name == "open") {
CefRefPtr<CefProcessMessage> message = CefProcessMessage::Create("open");
CefRefPtr<CefListValue> messageArgs = message->GetArgumentList();
messageArgs->SetSize(1);
messageArgs->SetString(0, arguments[0]->GetStringValue());
browser->SendProcessMessage(PID_BROWSER, message);
}
};
}