Use node's Environment feature to add "require" in renderer.
This commit is contained in:
parent
2413eebd7b
commit
886ebdb002
16 changed files with 180 additions and 166 deletions
|
@ -4,9 +4,6 @@
|
|||
|
||||
#include "renderer/atom_render_view_observer.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <vector>
|
||||
|
||||
#include "common/api/api_messages.h"
|
||||
#include "common/node_bindings.h"
|
||||
#include "ipc/ipc_message_macros.h"
|
||||
|
@ -15,69 +12,47 @@
|
|||
#include "third_party/WebKit/public/web/WebDocument.h"
|
||||
#include "third_party/WebKit/public/web/WebDraggableRegion.h"
|
||||
#include "third_party/WebKit/public/web/WebFrame.h"
|
||||
#include "v8/include/v8.h"
|
||||
|
||||
#include "common/v8/node_common.h"
|
||||
|
||||
using WebKit::WebFrame;
|
||||
|
||||
namespace webkit {
|
||||
extern void SetGetFirstWindowContext(v8::Handle<v8::Context> (*)());
|
||||
extern void SetIsValidWindowContext(bool (*)(v8::Handle<v8::Context>));
|
||||
}
|
||||
|
||||
namespace atom {
|
||||
|
||||
namespace {
|
||||
|
||||
std::vector<WebFrame*>& web_frames() {
|
||||
CR_DEFINE_STATIC_LOCAL(std::vector<WebFrame*>, frames, ());
|
||||
return frames;
|
||||
}
|
||||
|
||||
v8::Handle<v8::Context> GetFirstWindowContext() {
|
||||
if (web_frames().size() == 0)
|
||||
return v8::Handle<v8::Context>();
|
||||
|
||||
return web_frames()[0]->mainWorldScriptContext();
|
||||
}
|
||||
|
||||
bool IsValidWindowContext(v8::Handle<v8::Context> context) {
|
||||
size_t size = web_frames().size();
|
||||
for (size_t i = 0; i < size; ++i)
|
||||
if (web_frames()[i]->mainWorldScriptContext() == context)
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
AtomRenderViewObserver::AtomRenderViewObserver(
|
||||
content::RenderView* render_view,
|
||||
AtomRendererClient* renderer_client)
|
||||
: content::RenderViewObserver(render_view),
|
||||
atom_bindings_(new AtomRendererBindings(render_view)),
|
||||
renderer_client_(renderer_client) {
|
||||
// Interact with dirty workarounds of extra node context in WebKit.
|
||||
webkit::SetGetFirstWindowContext(GetFirstWindowContext);
|
||||
webkit::SetIsValidWindowContext(IsValidWindowContext);
|
||||
}
|
||||
|
||||
AtomRenderViewObserver::~AtomRenderViewObserver() {
|
||||
}
|
||||
|
||||
void AtomRenderViewObserver::DidClearWindowObject(WebFrame* frame) {
|
||||
// Remember the web frame.
|
||||
web_frames().push_back(frame);
|
||||
// Get the context.
|
||||
v8::HandleScope handle_scope(node_isolate);
|
||||
v8::Handle<v8::Context> context = frame->mainWorldScriptContext();
|
||||
if (context.IsEmpty())
|
||||
return;
|
||||
|
||||
renderer_client_->node_bindings()->BindTo(frame);
|
||||
v8::Context::Scope scope(context);
|
||||
|
||||
// Check the existance of process object to prevent duplicate initialization.
|
||||
if (context->Global()->Has(v8::String::New("process")))
|
||||
return;
|
||||
|
||||
// Give the node loop a run to make sure everything is ready.
|
||||
renderer_client_->node_bindings()->RunMessageLoop();
|
||||
|
||||
// Setup node environment for each window.
|
||||
renderer_client_->node_bindings()->CreateEnvironment(context);
|
||||
|
||||
// Add atom-shell extended APIs.
|
||||
atom_bindings()->BindToFrame(frame);
|
||||
}
|
||||
|
||||
void AtomRenderViewObserver::FrameWillClose(WebFrame* frame) {
|
||||
std::vector<WebFrame*>& vec = web_frames();
|
||||
vec.erase(std::remove(vec.begin(), vec.end(), frame), vec.end());
|
||||
}
|
||||
|
||||
void AtomRenderViewObserver::DraggableRegionsChanged(WebKit::WebFrame* frame) {
|
||||
WebKit::WebVector<WebKit::WebDraggableRegion> webregions =
|
||||
frame->document().draggableRegions();
|
||||
|
|
|
@ -28,7 +28,6 @@ class AtomRenderViewObserver : content::RenderViewObserver {
|
|||
virtual ~AtomRenderViewObserver();
|
||||
|
||||
virtual void DidClearWindowObject(WebKit::WebFrame*) OVERRIDE;
|
||||
virtual void FrameWillClose(WebKit::WebFrame*) OVERRIDE;
|
||||
|
||||
private:
|
||||
// content::RenderViewObserver implementation.
|
||||
|
|
|
@ -9,20 +9,8 @@
|
|||
|
||||
#include "common/v8/node_common.h"
|
||||
|
||||
namespace webkit {
|
||||
extern void SetGetNodeContext(v8::Handle<v8::Context> (*)());
|
||||
}
|
||||
|
||||
namespace atom {
|
||||
|
||||
namespace {
|
||||
|
||||
v8::Handle<v8::Context> GetNodeContext() {
|
||||
return global_env->context();
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
AtomRendererClient::AtomRendererClient()
|
||||
: node_bindings_(NodeBindings::Create(false)) {
|
||||
}
|
||||
|
@ -32,12 +20,15 @@ AtomRendererClient::~AtomRendererClient() {
|
|||
|
||||
void AtomRendererClient::RenderThreadStarted() {
|
||||
node_bindings_->Initialize();
|
||||
|
||||
// Interact with dirty workarounds of extra node context in WebKit.
|
||||
webkit::SetGetNodeContext(GetNodeContext);
|
||||
|
||||
node_bindings_->PrepareMessageLoop();
|
||||
node_bindings_->RunMessageLoop();
|
||||
|
||||
DCHECK(!global_env);
|
||||
|
||||
// Create a default empty environment which would be used when we need to
|
||||
// run V8 code out of a window context (like running a uv callback).
|
||||
v8::HandleScope handle_scope(node_isolate);
|
||||
v8::Local<v8::Context> context = v8::Context::New(node_isolate);
|
||||
global_env = node::Environment::New(context);
|
||||
}
|
||||
|
||||
void AtomRendererClient::RenderViewCreated(content::RenderView* render_view) {
|
||||
|
|
23
renderer/lib/init.coffee
Normal file
23
renderer/lib/init.coffee
Normal file
|
@ -0,0 +1,23 @@
|
|||
path = require 'path'
|
||||
|
||||
# Expose information of current process.
|
||||
process.__atom_type = 'renderer'
|
||||
process.resourcesPath = path.resolve process.argv[1], '..', '..', '..'
|
||||
|
||||
# We modified the original process.argv to let node.js load the
|
||||
# atom-renderer.js, we need to restore it here.
|
||||
process.argv.splice 1, 1
|
||||
|
||||
# Add renderer/api/lib to require's search paths, which contains javascript part
|
||||
# of Atom's built-in libraries.
|
||||
globalPaths = require('module').globalPaths
|
||||
globalPaths.push path.join process.resourcesPath, 'renderer', 'api', 'lib'
|
||||
|
||||
# And also common/api/lib
|
||||
globalPaths.push path.join process.resourcesPath, 'common', 'api', 'lib'
|
||||
|
||||
# Expose global variables.
|
||||
global.require = require
|
||||
global.module = module
|
||||
global.__filename = __filename
|
||||
global.__dirname = __dirname
|
Loading…
Add table
Add a link
Reference in a new issue