From b37b45b4b1d65993c64e21f4fa6acffe6cd79bbd Mon Sep 17 00:00:00 2001 From: Corey Johnson & Nathan Sobo Date: Tue, 28 Feb 2012 13:14:35 -0800 Subject: [PATCH] atom variable holds all global state and is shared across contexts. --- Atom/Classes/Atom.mm | 31 ++++++++++++++++++++++++++++--- Atom/Classes/AtomController.mm | 17 +++-------------- index.html | 2 +- spec/atom/app-spec.coffee | 2 +- spec/spec-helper.coffee | 2 -- spec/spec-suite.coffee | 2 +- src/atom-bootstrap.coffee | 2 ++ src/atom/app.coffee | 13 +++++++------ src/bootstrap.coffee | 3 --- src/stdlib/fs.coffee | 22 +++++++++++----------- src/stdlib/native.coffee | 9 ++++++--- src/stdlib/require.coffee | 18 +++++++++--------- static/atom-bootstrap.coffee | 0 13 files changed, 69 insertions(+), 54 deletions(-) create mode 100644 src/atom-bootstrap.coffee create mode 100644 static/atom-bootstrap.coffee diff --git a/Atom/Classes/Atom.mm b/Atom/Classes/Atom.mm index 17637737e..2e3fdb4db 100755 --- a/Atom/Classes/Atom.mm +++ b/Atom/Classes/Atom.mm @@ -2,6 +2,7 @@ #import "include/cef.h" #import "AtomController.h" +#import "native_handler.h" #import "client_handler.h" // Provide the CefAppProtocol implementation required by CEF. @@ -39,7 +40,7 @@ [super sendEvent:event]; } -- (void)createGlobalContext { +- (void)createSharedContext { _globalHandler = new ClientHandler(self); CefWindowInfo window_info; @@ -47,7 +48,9 @@ window_info.SetAsChild(_hiddenGlobalView, 0, 0, 0, 0); CefBrowserSettings settings; - CefBrowser::CreateBrowser(window_info, _globalHandler.get(), "", settings); + NSURL *resourceDirURL = [[NSBundle mainBundle] resourceURL]; + NSString *indexURLString = [[resourceDirURL URLByAppendingPathComponent:@"index.html"] absoluteString]; + CefBrowser::CreateBrowser(window_info, _globalHandler.get(), [indexURLString UTF8String], settings); } - (void)open:(NSString *)path { @@ -60,13 +63,35 @@ } - (void)applicationWillFinishLaunching:(NSNotification *)notification { - [self createGlobalContext]; + [self createSharedContext]; } - (void)applicationWillTerminate:(NSNotification *)aNotification { CefShutdown(); } +- (void)loadStart:(CefRefPtr) browser { + CefRefPtr frame = browser->GetMainFrame(); + CefRefPtr context = frame->GetV8Context(); + CefRefPtr global = context->GetGlobal(); + + context->Enter(); + + CefRefPtr bootstrapScript = CefV8Value::CreateString("atom-bootstrap"); + global->SetValue("$bootstrapScript", bootstrapScript, V8_PROPERTY_ATTRIBUTE_NONE); + + CefRefPtr atom = CefV8Value::CreateObject(NULL); + global->SetValue("atom", atom, V8_PROPERTY_ATTRIBUTE_NONE); + + CefRefPtr nativeHandler = new NativeHandler(); + atom->SetValue("native", nativeHandler->m_object, V8_PROPERTY_ATTRIBUTE_NONE); + + CefRefPtr loadPath = CefV8Value::CreateString(PROJECT_DIR); + atom->SetValue("loadPath", loadPath, V8_PROPERTY_ATTRIBUTE_NONE); + + context->Exit(); +} + @end // Returns the application settings based on command line arguments. diff --git a/Atom/Classes/AtomController.mm b/Atom/Classes/AtomController.mm index dfd6f2df5..28f49d0d2 100644 --- a/Atom/Classes/AtomController.mm +++ b/Atom/Classes/AtomController.mm @@ -2,7 +2,6 @@ #import "include/cef.h" #import "client_handler.h" -#import "native_handler.h" @implementation AtomController @@ -63,18 +62,8 @@ CefRefPtr pathToOpen = CefV8Value::CreateString("~/"); global->SetValue("$pathToOpen", pathToOpen, V8_PROPERTY_ATTRIBUTE_NONE); - - // $atom - global->SetValue("$app", _appContext->GetGlobal(), V8_PROPERTY_ATTRIBUTE_NONE); - - CefRefPtr atom = CefV8Value::CreateObject(NULL); - CefRefPtr loadPath = CefV8Value::CreateString(PROJECT_DIR); - atom->SetValue("loadPath", loadPath, V8_PROPERTY_ATTRIBUTE_NONE); - global->SetValue("$atom", atom, V8_PROPERTY_ATTRIBUTE_NONE); - - // $native - CefRefPtr nativeHandler = new NativeHandler(); - global->SetValue("$native", nativeHandler->m_object, V8_PROPERTY_ATTRIBUTE_NONE); + + global->SetValue("atom", _appContext->GetGlobal()->GetValue("atom"), V8_PROPERTY_ATTRIBUTE_NONE); context->Exit(); } @@ -118,7 +107,7 @@ void AppGetBrowserSettings(CefBrowserSettings& settings) { 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.web_security_disabled = true; settings.xss_auditor_enabled = false; settings.image_load_disabled = false; settings.shrink_standalone_images_to_fit = false; diff --git a/index.html b/index.html index 0b3c415b3..ba41ea34a 100644 --- a/index.html +++ b/index.html @@ -25,7 +25,7 @@ } window.onload = function() { - require($bootstrapScript); + if ($bootstrapScript) require($bootstrapScript); } diff --git a/spec/atom/app-spec.coffee b/spec/atom/app-spec.coffee index ed4692671..61d7d01c7 100644 --- a/spec/atom/app-spec.coffee +++ b/spec/atom/app-spec.coffee @@ -1,7 +1,7 @@ App = require 'app' fs = require 'fs' -xdescribe "App", -> +describe "App", -> app = null beforeEach -> diff --git a/spec/spec-helper.coffee b/spec/spec-helper.coffee index bce36195c..5bfaf2dcf 100644 --- a/spec/spec-helper.coffee +++ b/spec/spec-helper.coffee @@ -15,8 +15,6 @@ afterEach -> atom.globalKeymap.reset() $('#jasmine-content').empty() -window.atom = new (require 'app') - # Use underscore's definition of equality for toEqual assertions jasmine.Env.prototype.equals_ = _.isEqual diff --git a/spec/spec-suite.coffee b/spec/spec-suite.coffee index 4479e1c12..90079cd6a 100644 --- a/spec/spec-suite.coffee +++ b/spec/spec-suite.coffee @@ -1,4 +1,4 @@ fs = require 'fs' require 'spec-helper' -require path for path in fs.listTree($atom.loadPath + "/spec") when /-spec\.coffee$/.test path +require path for path in fs.listTree(atom.loadPath + "/spec") when /-spec\.coffee$/.test path diff --git a/src/atom-bootstrap.coffee b/src/atom-bootstrap.coffee new file mode 100644 index 000000000..70cb10d85 --- /dev/null +++ b/src/atom-bootstrap.coffee @@ -0,0 +1,2 @@ +App = require 'app' +window.atom = new App(atom.loadPath, atom.native) diff --git a/src/atom/app.coffee b/src/atom/app.coffee index 8ad42b7c6..b76346688 100644 --- a/src/atom/app.coffee +++ b/src/atom/app.coffee @@ -1,14 +1,15 @@ Native = require 'native' GlobalKeymap = require 'global-keymap' $ = require 'jquery' +_ = require 'underscore' module.exports = class App globalKeymap: null native: null - constructor: -> - @native = new Native + constructor: (@loadPath, nativeMethods)-> + @native = new Native(nativeMethods) @globalKeymap = new GlobalKeymap $(document).on 'keydown', (e) => @globalKeymap.handleKeyEvent(e) @@ -19,11 +20,11 @@ class App @globalKeymap.bindKey(selector, pattern, eventName) open: (url) -> - $native.open url + @native.open url quit: -> - $native.terminate null + @native.terminate null windows: -> - # controller.jsWindow for controller in OSX.NSApp.controllers - [] + # controller.jsWindow for controller in OSX.NSApp.controllers + [] diff --git a/src/bootstrap.coffee b/src/bootstrap.coffee index 325d8efac..cd8207efe 100644 --- a/src/bootstrap.coffee +++ b/src/bootstrap.coffee @@ -1,7 +1,4 @@ # Like sands through the hourglass, so are the days of our lives. -App = require 'app' -window.atom = new App - require 'window' window.startup $pathToOpen diff --git a/src/stdlib/fs.coffee b/src/stdlib/fs.coffee index f888b0c91..97ece8dcd 100644 --- a/src/stdlib/fs.coffee +++ b/src/stdlib/fs.coffee @@ -9,7 +9,7 @@ module.exports = # Make the given path absolute by resolving it against the # current working directory. absolute: (path) -> - $native.absolute(path) + atom.native.absolute(path) # Return the basename of the given path. That is the path with # any leading directory components removed. If specified, also @@ -28,7 +28,7 @@ module.exports = # Returns true if the file specified by path exists exists: (path) -> - $native.exists path + atom.native.exists path join: (paths...) -> return paths[0] if paths.length == 1 @@ -38,44 +38,44 @@ module.exports = # Returns true if the file specified by path exists and is a # directory. isDirectory: (path) -> - $native.isDirectory path + atom.native.isDirectory path # Returns true if the file specified by path exists and is a # regular file. isFile: (path) -> - not $native.isDirectory path + not atom.native.isDirectory path # Returns an array with all the names of files contained # in the directory path. list: (path) -> - $native.list(path, false) + atom.native.list(path, false) listTree: (path) -> - $native.list(path, true) + atom.native.list(path, true) # Remove a file at the given path. Throws an error if path is not a # file or a symbolic link to a file. remove: (path) -> - $native.remove path + atom.native.remove path # Open, read, and close a file, returning the file's contents. read: (path) -> - $native.read(path) + atom.native.read(path) # Open, write, flush, and close a file, writing the given content. write: (path, content) -> - $native.write(path, content) + atom.native.write(path, content) async: list: (path) -> deferred = $.Deferred() - $native.asyncList path, false, (subpaths) -> + atom.native.asyncList path, false, (subpaths) -> deferred.resolve subpaths deferred listTree: (path) -> deferred = $.Deferred() - $native.asyncList path, true, (subpaths) -> + atom.native.asyncList path, true, (subpaths) -> deferred.resolve subpaths deferred diff --git a/src/stdlib/native.coffee b/src/stdlib/native.coffee index 283f47fa1..f14d3c42e 100644 --- a/src/stdlib/native.coffee +++ b/src/stdlib/native.coffee @@ -2,8 +2,11 @@ _ = require 'underscore' module.exports = class Native + constructor: (nativeMethods)-> + _.extend(this, nativeMethods) + alert: (message, detailedMessage, buttons) -> - $native.alert(message, detailedMessage, buttons) + atom.native.alert(message, detailedMessage, buttons) # path - Optional. The String path to the file to base it on. newWindow: (path) -> @@ -29,10 +32,10 @@ class Native panel.filenames.lastObject.valueOf() writeToPasteboard: (text) -> - $native.writeToPasteboard text + atom.native.writeToPasteboard text readFromPasteboard: -> - $native.readFromPasteboard() + atom.native.readFromPasteboard() resetMainMenu: (menu) -> # OSX.NSApp.resetMainMenu diff --git a/src/stdlib/require.coffee b/src/stdlib/require.coffee index 83f8a22f4..15c267114 100644 --- a/src/stdlib/require.coffee +++ b/src/stdlib/require.coffee @@ -1,11 +1,11 @@ paths = [ - "#{$atom.loadPath}/spec" - "#{$atom.loadPath}/src/stdlib" - "#{$atom.loadPath}/src/atom" - "#{$atom.loadPath}/src" - "#{$atom.loadPath}/extensions" - "#{$atom.loadPath}/vendor" - "#{$atom.loadPath}/static" + "#{atom.loadPath}/spec" + "#{atom.loadPath}/src/stdlib" + "#{atom.loadPath}/src/atom" + "#{atom.loadPath}/src" + "#{atom.loadPath}/extensions" + "#{atom.loadPath}/vendor" + "#{atom.loadPath}/static" ] window.__filename = null @@ -100,7 +100,7 @@ __expand = (path) -> return null __exists = (path) -> - $native.exists path + atom.native.exists path __coffeeCache = (filePath) -> {CoffeeScript} = require 'coffee-script' @@ -108,7 +108,7 @@ __coffeeCache = (filePath) -> __read = (path) -> try - $native.read(path) + atom.native.read(path) catch e throw "require: can't read #{path}" diff --git a/static/atom-bootstrap.coffee b/static/atom-bootstrap.coffee new file mode 100644 index 000000000..e69de29bb