fs = require 'fs'
path = require 'path'
+# Restore the proces.argv.
+process.__atom_type = 'browser'
+process.resourcesPath = path.resolve process.argv[1], '..', '..', '..'
+process.argv.splice 1, 1
+
if process.platform is 'win32'
# Redirect node's console to use our own implementations, since node can not
# handle output when running as GUI program.
# And also common/api/lib
globalPaths.push path.join process.resourcesPath, 'common', 'api', 'lib'
-# Don't quit on fatal error.
-process.on 'uncaughtException', (error) ->
- # Show error in GUI.
- message = error.stack ? "#{error.name}: #{error.message}"
- require('dialog').showMessageBox
- type: 'warning'
- title: 'An javascript error occured in the browser'
- message: 'uncaughtException'
- detail: message
- buttons: ['OK']
+# Do loading in next tick since we still need some initialize work before
+# native bindings can work.
+setImmediate ->
+ # Don't quit on fatal error.
+ process.on 'uncaughtException', (error) ->
+ # Show error in GUI.
+ message = error.stack ? "#{error.name}: #{error.message}"
+ require('dialog').showMessageBox
+ type: 'warning'
+ title: 'An javascript error occured in the browser'
+ message: 'uncaughtException'
+ detail: message
+ buttons: ['OK']
-# Load the RPC server.
-require './rpc-server.js'
+ # Load the RPC server.
+ require './rpc-server.js'
-# Now we try to load app's package.json.
-packageJson = null
+ # Now we try to load app's package.json.
+ packageJson = null
-packagePath = path.join process.resourcesPath, 'app'
-try
- # First we try to load process.resourcesPath/app
- packageJson = JSON.parse(fs.readFileSync(path.join(packagePath, 'package.json')))
-catch error
- # If not found then we load browser/default_app
- packagePath = path.join process.resourcesPath, 'browser', 'default_app'
- packageJson = JSON.parse(fs.readFileSync(path.join(packagePath, 'package.json')))
+ packagePath = path.join process.resourcesPath, 'app'
+ try
+ # First we try to load process.resourcesPath/app
+ packageJson = JSON.parse(fs.readFileSync(path.join(packagePath, 'package.json')))
+ catch error
+ # If not found then we load browser/default_app
+ packagePath = path.join process.resourcesPath, 'browser', 'default_app'
+ packageJson = JSON.parse(fs.readFileSync(path.join(packagePath, 'package.json')))
-# Set application's version.
-app = require 'app'
-app.setVersion packageJson.version if packageJson.version?
+ # Set application's version.
+ app = require 'app'
+ app.setVersion packageJson.version if packageJson.version?
-# Set application's name.
-if packageJson.productName?
- app.setName packageJson.productName
-else if packageJson.name?
- app.setName packageJson.name
+ # Set application's name.
+ if packageJson.productName?
+ app.setName packageJson.productName
+ else if packageJson.name?
+ app.setName packageJson.name
-# Finally load app's main.js and transfer control to C++.
-require path.join(packagePath, packageJson.main)
+ # Finally load app's main.js and transfer control to C++.
+ require path.join(packagePath, packageJson.main)
#include "common/node_bindings.h"
+#include <limits.h> // PATH_MAX
+
#include "base/command_line.h"
#include "base/logging.h"
#include "base/message_loop/message_loop.h"
+#include "base/base_paths.h"
+#include "base/path_service.h"
#include "common/v8/native_type_conversions.h"
#include "content/public/browser/browser_thread.h"
#include "net/base/escape.h"
using content::BrowserThread;
+// Forward declaration of internal node functions.
namespace node {
void Init(int* argc,
const char** argv,
#endif
}
- // Init idle GC.
- uv_timer_init(uv_default_loop(), &idle_timer_);
- uv_timer_start(&idle_timer_, IdleCallback, 5000, 5000);
+ // Feed node the path to initialization script.
+ base::FilePath exec_path(str_argv[0]);
+ PathService::Get(base::FILE_EXE, &exec_path);
+ base::FilePath resources_path =
+#if defined(OS_MACOSX)
+ is_browser_ ? exec_path.DirName().DirName().Append("Resources") :
+ exec_path.DirName().DirName().DirName().DirName().DirName()
+ .Append("Resources");
+#else
+ exec_path.DirName().Append("resources");
+#endif
+ base::FilePath script_path =
+ resources_path.AppendASCII("browser")
+ .AppendASCII("atom")
+ .AppendASCII(is_browser_ ? "atom.js" : "atom-renderer.js");
+ std::string script_path_str = script_path.AsUTF8Unsafe();
+ args.insert(args.begin() + 1, script_path_str.c_str());
+
+ // Init idle GC for browser.
+ if (is_browser_) {
+ uv_timer_init(uv_default_loop(), &idle_timer_);
+ uv_timer_start(&idle_timer_, IdleCallback, 5000, 5000);
+ }
// Open node's error reporting system for browser process.
node::g_standalone_mode = is_browser_;
// Create environment (setup process object and load node.js).
global_env = node::CreateEnvironment(node_isolate, argc, argv, argc, argv);
-
- // Set the process.__atom_type.
- {
- v8::Context::Scope context_scope(global_env->context());
- v8::HandleScope handle_scope(global_env->isolate());
-
- // Tell node.js we are in browser or renderer.
- v8::Handle<v8::String> type =
- is_browser_ ? v8::String::New("browser") : v8::String::New("renderer");
- global_env->process_object()->Set(v8::String::New("__atom_type"), type);
- }
}
void NodeBindings::BindTo(WebKit::WebFrame* frame) {