Share same native bindings code on both renderer and browser.
This commit is contained in:
parent
993cf1cc61
commit
5948bff23f
16 changed files with 206 additions and 122 deletions
33
common/api/api_messages.cc
Normal file
33
common/api/api_messages.cc
Normal file
|
@ -0,0 +1,33 @@
|
|||
// Copyright (c) 2013 GitHub, Inc. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
// Get basic type definitions.
|
||||
#define IPC_MESSAGE_IMPL
|
||||
#include "common/api/api_messages.h"
|
||||
|
||||
// Generate constructors.
|
||||
#include "ipc/struct_constructor_macros.h"
|
||||
#include "common/api/api_messages.h"
|
||||
|
||||
// Generate destructors.
|
||||
#include "ipc/struct_destructor_macros.h"
|
||||
#include "common/api/api_messages.h"
|
||||
|
||||
// Generate param traits write methods.
|
||||
#include "ipc/param_traits_write_macros.h"
|
||||
namespace IPC {
|
||||
#include "common/api/api_messages.h"
|
||||
} // namespace IPC
|
||||
|
||||
// Generate param traits read methods.
|
||||
#include "ipc/param_traits_read_macros.h"
|
||||
namespace IPC {
|
||||
#include "common/api/api_messages.h"
|
||||
} // namespace IPC
|
||||
|
||||
// Generate param traits log methods.
|
||||
#include "ipc/param_traits_log_macros.h"
|
||||
namespace IPC {
|
||||
#include "common/api/api_messages.h"
|
||||
} // namespace IPC
|
35
common/api/api_messages.h
Normal file
35
common/api/api_messages.h
Normal file
|
@ -0,0 +1,35 @@
|
|||
// Copyright (c) 2013 GitHub, Inc. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
// Multiply-included file, no traditional include guard.
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "base/values.h"
|
||||
#include "content/public/common/common_param_traits.h"
|
||||
#include "ipc/ipc_message_macros.h"
|
||||
|
||||
// The message starter should be declared in ipc/ipc_message_start.h. Since
|
||||
// we don't wan't to patch Chromium, we just pretend to be Content Shell.
|
||||
|
||||
#define IPC_MESSAGE_START ShellMsgStart
|
||||
|
||||
IPC_SYNC_MESSAGE_CONTROL2_1(AtomViewHostMsg_Allocate_Object,
|
||||
std::string /* type name */,
|
||||
DictionaryValue /* options */,
|
||||
int /* object id */)
|
||||
|
||||
IPC_SYNC_MESSAGE_CONTROL1_0(AtomViewHostMsg_Deallocate_Object,
|
||||
int /* object id */)
|
||||
|
||||
IPC_SYNC_MESSAGE_CONTROL3_1(AtomViewHostMsg_Call_Object_Method,
|
||||
int /* object id */,
|
||||
std::string /* method name */,
|
||||
ListValue /* arguments */,
|
||||
DictionaryValue /* return value */)
|
||||
|
||||
IPC_SYNC_MESSAGE_CONTROL2_1(AtomViewMsg_Callback,
|
||||
int /* callback id */,
|
||||
ListValue /* arguments */,
|
||||
DictionaryValue /* return value */)
|
91
common/api/atom_bindings.cc
Normal file
91
common/api/atom_bindings.cc
Normal file
|
@ -0,0 +1,91 @@
|
|||
// Copyright (c) 2013 GitHub, Inc. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#include "common/api/atom_bindings.h"
|
||||
|
||||
#include "base/logging.h"
|
||||
#include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h"
|
||||
#include "vendor/node/src/node.h"
|
||||
|
||||
namespace atom {
|
||||
|
||||
// Defined in atom_extensions.cc.
|
||||
node::node_module_struct* GetBuiltinModule(const char *name, bool is_browser);
|
||||
|
||||
AtomBindings::AtomBindings() {
|
||||
}
|
||||
|
||||
AtomBindings::~AtomBindings() {
|
||||
}
|
||||
|
||||
void AtomBindings::BindTo(v8::Handle<v8::Object> process) {
|
||||
v8::HandleScope scope;
|
||||
|
||||
node::SetMethod(process, "atom_binding", Binding);
|
||||
}
|
||||
|
||||
// static
|
||||
v8::Handle<v8::Value> AtomBindings::Binding(const v8::Arguments& args) {
|
||||
v8::HandleScope scope;
|
||||
|
||||
v8::Local<v8::String> module = args[0]->ToString();
|
||||
v8::String::Utf8Value module_v(module);
|
||||
node::node_module_struct* modp;
|
||||
|
||||
v8::Local<v8::Object> process = v8::Context::GetCurrent()->Global()->
|
||||
Get(v8::String::New("process"))->ToObject();
|
||||
DCHECK(!process.IsEmpty());
|
||||
|
||||
// is_browser = process.__atom_type == 'browser'.
|
||||
bool is_browser = std::string("browser") == *v8::String::Utf8Value(
|
||||
process->Get(v8::String::New("__atom_type")));
|
||||
|
||||
// Cached in process.__atom_binding_cache.
|
||||
v8::Local<v8::Object> binding_cache;
|
||||
v8::Local<v8::String> bc_name = v8::String::New("__atom_binding_cache");
|
||||
if (process->Has(bc_name)) {
|
||||
binding_cache = process->Get(bc_name)->ToObject();
|
||||
DCHECK(!binding_cache.IsEmpty());
|
||||
} else {
|
||||
binding_cache = v8::Object::New();
|
||||
process->Set(bc_name, binding_cache);
|
||||
}
|
||||
|
||||
v8::Local<v8::Object> exports;
|
||||
|
||||
if (binding_cache->Has(module)) {
|
||||
exports = binding_cache->Get(module)->ToObject();
|
||||
return scope.Close(exports);
|
||||
}
|
||||
|
||||
if ((modp = GetBuiltinModule(*module_v, is_browser)) != NULL) {
|
||||
exports = v8::Object::New();
|
||||
// Internal bindings don't have a "module" object,
|
||||
// only exports.
|
||||
modp->register_func(exports, v8::Undefined());
|
||||
binding_cache->Set(module, exports);
|
||||
return scope.Close(exports);
|
||||
}
|
||||
|
||||
return v8::ThrowException(v8::Exception::Error(
|
||||
v8::String::New("No such module")));
|
||||
}
|
||||
|
||||
void AtomBindings::BindToFrame(WebKit::WebFrame* frame) {
|
||||
v8::HandleScope handle_scope;
|
||||
|
||||
v8::Handle<v8::Context> context = frame->mainWorldScriptContext();
|
||||
if (context.IsEmpty())
|
||||
return;
|
||||
|
||||
v8::Context::Scope scope(context);
|
||||
|
||||
v8::Handle<v8::Object> process =
|
||||
context->Global()->Get(v8::String::New("process"))->ToObject();
|
||||
DCHECK(!process.IsEmpty());
|
||||
|
||||
AtomBindings::BindTo(process);
|
||||
}
|
||||
|
||||
} // namespace atom
|
37
common/api/atom_bindings.h
Normal file
37
common/api/atom_bindings.h
Normal file
|
@ -0,0 +1,37 @@
|
|||
// Copyright (c) 2013 GitHub, Inc. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#ifndef ATOM_COMMON_API_ATOM_BINDINGS_
|
||||
#define ATOM_COMMON_API_ATOM_BINDINGS_
|
||||
|
||||
#include "base/basictypes.h"
|
||||
#include "v8/include/v8.h"
|
||||
|
||||
namespace WebKit {
|
||||
class WebFrame;
|
||||
}
|
||||
|
||||
namespace atom {
|
||||
|
||||
class AtomBindings {
|
||||
public:
|
||||
AtomBindings();
|
||||
virtual ~AtomBindings();
|
||||
|
||||
// Add process.atom_binding function, which behaves like process.binding but
|
||||
// load native code from atom-shell instead.
|
||||
virtual void BindTo(v8::Handle<v8::Object> process);
|
||||
|
||||
// Call BindTo for process object of the frame.
|
||||
void BindToFrame(WebKit::WebFrame* frame);
|
||||
|
||||
private:
|
||||
static v8::Handle<v8::Value> Binding(const v8::Arguments& args);
|
||||
|
||||
DISALLOW_COPY_AND_ASSIGN(AtomBindings);
|
||||
};
|
||||
|
||||
} // namespace atom
|
||||
|
||||
#endif // ATOM_COMMON_API_ATOM_BINDINGS_
|
53
common/api/atom_extensions.cc
Normal file
53
common/api/atom_extensions.cc
Normal file
|
@ -0,0 +1,53 @@
|
|||
// Copyright (c) 2013 GitHub, Inc. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "vendor/node/src/node.h"
|
||||
#include "vendor/node/src/node_version.h"
|
||||
|
||||
namespace atom {
|
||||
|
||||
#undef NODE_EXT_LIST_START
|
||||
#undef NODE_EXT_LIST_ITEM
|
||||
#undef NODE_EXT_LIST_END
|
||||
|
||||
#define NODE_EXT_LIST_START
|
||||
#define NODE_EXT_LIST_ITEM NODE_MODULE_DECL
|
||||
#define NODE_EXT_LIST_END
|
||||
|
||||
#include "common/api/atom_extensions.h"
|
||||
|
||||
#undef NODE_EXT_LIST_START
|
||||
#undef NODE_EXT_LIST_ITEM
|
||||
#undef NODE_EXT_LIST_END
|
||||
|
||||
#define NODE_EXT_STRING(x) &x ## _module,
|
||||
#define NODE_EXT_LIST_START node::node_module_struct *node_module_list[] = {
|
||||
#define NODE_EXT_LIST_ITEM NODE_EXT_STRING
|
||||
#define NODE_EXT_LIST_END NULL};
|
||||
|
||||
#include "common/api/atom_extensions.h" // NOLINT
|
||||
|
||||
node::node_module_struct* GetBuiltinModule(const char *name, bool is_browser) {
|
||||
char buf[128];
|
||||
node::node_module_struct *cur = NULL;
|
||||
if (is_browser)
|
||||
snprintf(buf, sizeof(buf), "atom_browser_%s", name);
|
||||
else
|
||||
snprintf(buf, sizeof(buf), "atom_renderer_%s", name);
|
||||
/* TODO: you could look these up in a hash, but there are only
|
||||
* a few, and once loaded they are cached. */
|
||||
for (int i = 0; node_module_list[i] != NULL; i++) {
|
||||
cur = node_module_list[i];
|
||||
if (strcmp(cur->modname, buf) == 0) {
|
||||
return cur;
|
||||
}
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
} // namespace atom
|
13
common/api/atom_extensions.h
Normal file
13
common/api/atom_extensions.h
Normal file
|
@ -0,0 +1,13 @@
|
|||
// Copyright (c) 2013 GitHub, Inc. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
// Multiply-included file, no traditional include guard.
|
||||
|
||||
// Used by atom_extensions.cc to declare a list of built-in modules of Atom.
|
||||
|
||||
NODE_EXT_LIST_START
|
||||
|
||||
NODE_EXT_LIST_ITEM(atom_browser_window)
|
||||
|
||||
NODE_EXT_LIST_END
|
Loading…
Add table
Add a link
Reference in a new issue