Global background page is working.

This commit is contained in:
Corey Johnson
2012-02-28 10:58:25 -08:00
parent 19be5fed04
commit cd3dfa8ad2
6 changed files with 65 additions and 32 deletions

View File

@@ -1,10 +1,14 @@
#import "include/cef.h"
#import "include/cef_application_mac.h"
class ClientHandler;
@class AtomController;
@interface Atom : NSApplication<CefAppProtocol> {
NSView *_hiddenGlobalView;
BOOL handlingSendEvent_;
CefRefPtr<ClientHandler> _globalHandler;
}
- (void)open:(NSString *)path;

View File

@@ -2,6 +2,8 @@
#import "include/cef.h"
#import "AtomController.h"
#import "client_handler.h"
// Provide the CefAppProtocol implementation required by CEF.
@implementation Atom
@@ -19,6 +21,11 @@
return [super sharedApplication];
}
- (void)dealloc {
[_hiddenGlobalView release];
[self dealloc];
}
- (BOOL)isHandlingSendEvent {
return handlingSendEvent_;
}
@@ -32,19 +39,32 @@
[super sendEvent:event];
}
- (void)createGlobalContext {
_globalHandler = new ClientHandler(self);
CefWindowInfo window_info;
_hiddenGlobalView = [[NSView alloc] init];
window_info.SetAsChild(_hiddenGlobalView, 0, 0, 0, 0);
CefBrowserSettings settings;
CefBrowser::CreateBrowser(window_info, _globalHandler.get(), "", settings);
}
- (void)open:(NSString *)path {
}
- (IBAction)runSpecs:(id)sender {
[[AtomController alloc] initForSpecs];
CefRefPtr<CefV8Context> appContext = _globalHandler->GetBrowser()->GetMainFrame()->GetV8Context();
[[AtomController alloc] initSpecsWithAppContext:appContext];
}
- (void)applicationWillFinishLaunching:(NSNotification *)notification {
[self createGlobalContext];
}
// Sent by the default notification center immediately before the application terminates.
- (void)applicationWillTerminate:(NSNotification *)aNotification {
CefShutdown();
[self release];
}
@end

View File

@@ -1,13 +1,20 @@
#import <Cocoa/Cocoa.h>
#import "include/cef.h"
class ClientHandler;
@interface AtomController : NSWindowController <NSWindowDelegate> {
NSView *_webView;
NSString *_bootstrapScript;
CefRefPtr<CefV8Context> _appContext;
CefRefPtr<ClientHandler> _handler;
}
- (id)initWithBootstrapScript:(NSString *)bootstrapScript;
- (id)initForSpecs;
- (id)initWithBootstrapScript:(NSString *)bootstrapScript appContext:(CefRefPtr<CefV8Context>) context;
- (id)initSpecsWithAppContext:(CefRefPtr<CefV8Context>)appContext;
- (void)createBrowser;
- (void)afterCreated:(CefRefPtr<CefBrowser>) browser;

View File

@@ -4,37 +4,36 @@
#import "client_handler.h"
#import "native_handler.h"
CefRefPtr<ClientHandler> g_handler;
@implementation AtomController
@synthesize webView=_webView;
- (void)dealloc {
[_bootstrapScript release];
[super dealloc];
}
- (id)initWithBootstrapScript:(NSString *)bootstrapScript {
- (id)initWithBootstrapScript:(NSString *)bootstrapScript appContext:(CefRefPtr<CefV8Context>)appContext {
self = [super initWithWindowNibName:@"ClientWindow"];
_bootstrapScript = [bootstrapScript retain];
_appContext = appContext;
[self.window makeKeyWindow];
[self createBrowser];
[self.window makeKeyAndOrderFront:nil];
return self;
}
- (id)initForSpecs {
return [self initWithBootstrapScript:@"spec-bootstrap"];
- (id)initSpecsWithAppContext:(CefRefPtr<CefV8Context>)appContext {
return [self initWithBootstrapScript:@"spec-bootstrap" appContext:appContext];
}
- (void)windowDidLoad {
[super windowDidLoad];
- (void)createBrowser {
[self.window setDelegate:self];
[self.window setReleasedWhenClosed:NO];
g_handler = new ClientHandler(self);
_handler = new ClientHandler(self);
CefWindowInfo window_info;
CefBrowserSettings settings;
@@ -45,19 +44,19 @@ CefRefPtr<ClientHandler> g_handler;
NSURL *resourceDirURL = [[NSBundle mainBundle] resourceURL];
NSString *indexURLString = [[resourceDirURL URLByAppendingPathComponent:@"index.html"] absoluteString];
CefBrowser::CreateBrowser(window_info, g_handler.get(), [indexURLString UTF8String], settings);
[self.window makeKeyAndOrderFront:nil];
CefBrowser::CreateBrowser(window_info, _handler.get(), [indexURLString UTF8String], settings);
}
- (void)afterCreated:(CefRefPtr<CefBrowser>) browser {
browser->ShowDevTools();
CefRefPtr<CefFrame> frame = browser->GetMainFrame();
CefRefPtr<CefV8Context> jsContext = frame->GetV8Context();
CefRefPtr<CefV8Value> global = jsContext->GetGlobal();
CefRefPtr<CefV8Context> context = frame->GetV8Context();
CefRefPtr<CefV8Value> global = context->GetGlobal();
jsContext->Enter();
context->Enter();
global->SetValue("$app", _appContext->GetGlobal(), V8_PROPERTY_ATTRIBUTE_NONE);
CefRefPtr<CefV8Value> bootstrapScript = CefV8Value::CreateString([_bootstrapScript UTF8String]);
global->SetValue("$bootstrapScript", bootstrapScript, V8_PROPERTY_ATTRIBUTE_NONE);
@@ -75,7 +74,7 @@ CefRefPtr<ClientHandler> g_handler;
CefRefPtr<NativeHandler> nativeHandler = new NativeHandler();
global->SetValue("$native", nativeHandler->m_object, V8_PROPERTY_ATTRIBUTE_NONE);
jsContext->Exit();
context->Exit();
}
#pragma mark NSWindowDelegate
@@ -87,7 +86,8 @@ CefRefPtr<ClientHandler> g_handler;
// sequence by getting rid of the window. By returning YES, we allow the window
// to be removed from the screen.
- (BOOL)windowShouldClose:(id)window {
g_handler = NULL;
_appContext = NULL;
_handler = NULL;
// Clean ourselves up after clearing the stack of anything that might have the window on it.
[self autorelease];
@@ -113,9 +113,9 @@ void AppGetBrowserSettings(CefBrowserSettings& settings) {
settings.javascript_access_clipboard_disallowed = false;
settings.dom_paste_disabled = false;
settings.caret_browsing_enabled = false;
settings.java_disabled = false;
settings.plugins_disabled = false;
settings.universal_access_from_file_urls_allowed = false;
settings.java_disabled = true;
settings.plugins_disabled = true;
settings.universal_access_from_file_urls_allowed = true;
settings.file_access_from_file_urls_allowed = false;
settings.web_security_disabled = false;
settings.xss_auditor_enabled = false;

View File

@@ -22,7 +22,7 @@ class ClientHandler : public CefClient,
public CefDragHandler
{
public:
ClientHandler(AtomController *clientController);
ClientHandler(id delegate);
virtual ~ClientHandler();
// CefClient methods
@@ -117,7 +117,7 @@ protected:
// The child browser window handle
CefWindowHandle m_BrowserHwnd;
AtomController *m_clientController;
id m_delegate;
// Include the default reference counting implementation.
IMPLEMENT_REFCOUNTING(ClientHandler);

View File

@@ -20,11 +20,11 @@
#define REQUIRE_IO_THREAD() ASSERT(CefCurrentlyOn(TID_IO));
#define REQUIRE_FILE_THREAD() ASSERT(CefCurrentlyOn(TID_FILE));
ClientHandler::ClientHandler(AtomController *clientController)
ClientHandler::ClientHandler(id delegate)
: m_MainHwnd(NULL),
m_BrowserHwnd(NULL)
{
m_clientController = clientController;
m_delegate = delegate;
}
ClientHandler::~ClientHandler()
@@ -43,7 +43,9 @@ void ClientHandler::OnAfterCreated(CefRefPtr<CefBrowser> browser)
m_Browser = browser;
m_BrowserHwnd = browser->GetWindowHandle();
[m_clientController afterCreated:browser];
if ([m_delegate respondsToSelector:@selector(afterCreated:)]) {
[m_delegate afterCreated:browser];
}
}
}