From cd3dfa8ad2187491f7b43df9c4b14faf561ccca1 Mon Sep 17 00:00:00 2001 From: Corey Johnson Date: Tue, 28 Feb 2012 10:58:25 -0800 Subject: [PATCH] Global background page is working. --- Atom/Classes/Atom.h | 4 ++++ Atom/Classes/Atom.mm | 28 +++++++++++++++++++---- Atom/Classes/AtomController.h | 11 +++++++-- Atom/Classes/AtomController.mm | 42 +++++++++++++++++----------------- Atom/Classes/client_handler.h | 4 ++-- Atom/Classes/client_handler.mm | 8 ++++--- 6 files changed, 65 insertions(+), 32 deletions(-) diff --git a/Atom/Classes/Atom.h b/Atom/Classes/Atom.h index 439c972ca..d448ad84f 100755 --- a/Atom/Classes/Atom.h +++ b/Atom/Classes/Atom.h @@ -1,10 +1,14 @@ #import "include/cef.h" #import "include/cef_application_mac.h" +class ClientHandler; + @class AtomController; @interface Atom : NSApplication { + NSView *_hiddenGlobalView; BOOL handlingSendEvent_; + CefRefPtr _globalHandler; } - (void)open:(NSString *)path; diff --git a/Atom/Classes/Atom.mm b/Atom/Classes/Atom.mm index 5ddf92d79..17637737e 100755 --- a/Atom/Classes/Atom.mm +++ b/Atom/Classes/Atom.mm @@ -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 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 diff --git a/Atom/Classes/AtomController.h b/Atom/Classes/AtomController.h index 1d4c9ba72..f99870ca7 100644 --- a/Atom/Classes/AtomController.h +++ b/Atom/Classes/AtomController.h @@ -1,13 +1,20 @@ #import #import "include/cef.h" +class ClientHandler; + @interface AtomController : NSWindowController { NSView *_webView; NSString *_bootstrapScript; + + CefRefPtr _appContext; + CefRefPtr _handler; } -- (id)initWithBootstrapScript:(NSString *)bootstrapScript; -- (id)initForSpecs; +- (id)initWithBootstrapScript:(NSString *)bootstrapScript appContext:(CefRefPtr) context; +- (id)initSpecsWithAppContext:(CefRefPtr)appContext; + +- (void)createBrowser; - (void)afterCreated:(CefRefPtr) browser; diff --git a/Atom/Classes/AtomController.mm b/Atom/Classes/AtomController.mm index 5a88c882b..1ebb3131c 100644 --- a/Atom/Classes/AtomController.mm +++ b/Atom/Classes/AtomController.mm @@ -4,37 +4,36 @@ #import "client_handler.h" #import "native_handler.h" -CefRefPtr g_handler; - @implementation AtomController @synthesize webView=_webView; - (void)dealloc { [_bootstrapScript release]; + [super dealloc]; } -- (id)initWithBootstrapScript:(NSString *)bootstrapScript { +- (id)initWithBootstrapScript:(NSString *)bootstrapScript appContext:(CefRefPtr)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)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 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) browser { browser->ShowDevTools(); CefRefPtr frame = browser->GetMainFrame(); - CefRefPtr jsContext = frame->GetV8Context(); - CefRefPtr global = jsContext->GetGlobal(); + CefRefPtr context = frame->GetV8Context(); + CefRefPtr global = context->GetGlobal(); - jsContext->Enter(); + context->Enter(); + + global->SetValue("$app", _appContext->GetGlobal(), V8_PROPERTY_ATTRIBUTE_NONE); CefRefPtr bootstrapScript = CefV8Value::CreateString([_bootstrapScript UTF8String]); global->SetValue("$bootstrapScript", bootstrapScript, V8_PROPERTY_ATTRIBUTE_NONE); @@ -75,7 +74,7 @@ CefRefPtr g_handler; CefRefPtr 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 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; diff --git a/Atom/Classes/client_handler.h b/Atom/Classes/client_handler.h index 949375512..c03ec83ee 100755 --- a/Atom/Classes/client_handler.h +++ b/Atom/Classes/client_handler.h @@ -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); diff --git a/Atom/Classes/client_handler.mm b/Atom/Classes/client_handler.mm index a563137c7..4d13bed3d 100755 --- a/Atom/Classes/client_handler.mm +++ b/Atom/Classes/client_handler.mm @@ -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 browser) m_Browser = browser; m_BrowserHwnd = browser->GetWindowHandle(); - [m_clientController afterCreated:browser]; + if ([m_delegate respondsToSelector:@selector(afterCreated:)]) { + [m_delegate afterCreated:browser]; + } } }