Adapt to API changes of Chromium and node.

This commit is contained in:
Cheng Zhao 2014-06-28 22:33:00 +08:00
parent 58ccb27792
commit cd4c5d976b
55 changed files with 281 additions and 402 deletions

View file

@ -8,24 +8,25 @@
#include "atom/common/native_mate_converters/string16_converter.h"
#include "native_mate/dictionary.h"
#include "ui/base/clipboard/clipboard.h"
#include "ui/base/clipboard/scoped_clipboard_writer.h"
#include "atom/common/node_includes.h"
namespace mate {
template<>
struct Converter<ui::Clipboard::Buffer> {
struct Converter<ui::ClipboardType> {
static bool FromV8(v8::Isolate* isolate,
v8::Handle<v8::Value> val,
ui::Clipboard::Buffer* out) {
ui::ClipboardType* out) {
std::string type;
if (!Converter<std::string>::FromV8(isolate, val, &type))
return false;
if (type == "selection")
*out = ui::Clipboard::BUFFER_SELECTION;
*out = ui::CLIPBOARD_TYPE_SELECTION;
else
*out = ui::Clipboard::BUFFER_STANDARD;
*out = ui::CLIPBOARD_TYPE_COPY_PASTE;
return true;
}
};
@ -34,14 +35,14 @@ struct Converter<ui::Clipboard::Buffer> {
namespace {
bool Has(const std::string& format_string, ui::Clipboard::Buffer buffer) {
bool Has(const std::string& format_string, ui::ClipboardType type) {
ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread();
ui::Clipboard::FormatType format(ui::Clipboard::GetFormatType(format_string));
return clipboard->IsFormatAvailable(format, buffer);
return clipboard->IsFormatAvailable(format, type);
}
std::string Read(const std::string& format_string,
ui::Clipboard::Buffer buffer) {
ui::ClipboardType type) {
ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread();
ui::Clipboard::FormatType format(ui::Clipboard::GetFormatType(format_string));
@ -50,23 +51,20 @@ std::string Read(const std::string& format_string,
return data;
}
string16 ReadText(ui::Clipboard::Buffer buffer) {
string16 data;
ui::Clipboard::GetForCurrentThread()->ReadText(buffer, &data);
base::string16 ReadText(ui::ClipboardType type) {
base::string16 data;
ui::Clipboard::GetForCurrentThread()->ReadText(type, &data);
return data;
}
void WriteText(const std::string text, ui::Clipboard::Buffer buffer) {
ui::Clipboard::ObjectMap object_map;
object_map[ui::Clipboard::CBF_TEXT].push_back(
std::vector<char>(text.begin(), text.end()));
void WriteText(const base::string16& text, ui::ClipboardType type) {
ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread();
clipboard->WriteObjects(buffer, object_map);
ui::ScopedClipboardWriter writer(clipboard, type);
writer.WriteText(text);
}
void Clear(ui::Clipboard::Buffer buffer) {
ui::Clipboard::GetForCurrentThread()->Clear(buffer);
void Clear(ui::ClipboardType type) {
ui::Clipboard::GetForCurrentThread()->Clear(type);
}
void Initialize(v8::Handle<v8::Object> exports) {
@ -80,4 +78,4 @@ void Initialize(v8::Handle<v8::Object> exports) {
} // namespace
NODE_MODULE(atom_common_clipboard, Initialize)
NODE_MODULE_X(atom_common_clipboard, Initialize, NULL, NM_F_BUILTIN)

View file

@ -45,4 +45,4 @@ void Initialize(v8::Handle<v8::Object> exports) {
} // namespace
NODE_MODULE(atom_common_crash_reporter, Initialize)
NODE_MODULE_X(atom_common_crash_reporter, Initialize, NULL, NM_F_BUILTIN)

View file

@ -29,14 +29,14 @@ int32_t IDWeakMap::Add(v8::Isolate* isolate, v8::Handle<v8::Object> object) {
mate::Converter<int32_t>::ToV8(isolate, key));
map_[key] = new mate::RefCountedPersistent<v8::Object>(object);
map_[key]->MakeWeak(this, WeakCallback);
map_[key]->SetWeak(this, WeakCallback);
return key;
}
v8::Handle<v8::Value> IDWeakMap::Get(int32_t key) {
v8::Handle<v8::Value> IDWeakMap::Get(v8::Isolate* isolate, int32_t key) {
if (!Has(key)) {
node::ThrowError("Invalid key");
return v8::Undefined();
return v8::Undefined(isolate);
}
return map_[key]->NewHandle();
@ -77,14 +77,11 @@ void IDWeakMap::BuildPrototype(v8::Isolate* isolate,
}
// static
void IDWeakMap::WeakCallback(v8::Isolate* isolate,
v8::Persistent<v8::Object>* value,
IDWeakMap* self) {
v8::HandleScope handle_scope(isolate);
v8::Local<v8::Object> object = v8::Local<v8::Object>::New(isolate, *value);
int32_t key = object->GetHiddenValue(
mate::StringToV8(isolate, "IDWeakMapKey"))->Int32Value();
self->Remove(key);
void IDWeakMap::WeakCallback(
const v8::WeakCallbackData<v8::Object, IDWeakMap>& data) {
int32_t key = data.GetValue()->GetHiddenValue(
mate::StringToV8(data.GetIsolate(), "IDWeakMapKey"))->Int32Value();
data.GetParameter()->Remove(key);
}
} // namespace api
@ -107,4 +104,4 @@ void Initialize(v8::Handle<v8::Object> exports) {
} // namespace
NODE_MODULE(atom_common_id_weak_map, Initialize)
NODE_MODULE_X(atom_common_id_weak_map, Initialize, NULL, NM_F_BUILTIN)

View file

@ -29,15 +29,14 @@ class IDWeakMap : public mate::Wrappable {
virtual ~IDWeakMap();
int32_t Add(v8::Isolate* isolate, v8::Handle<v8::Object> object);
v8::Handle<v8::Value> Get(int32_t key);
v8::Handle<v8::Value> Get(v8::Isolate* isolate, int32_t key);
bool Has(int32_t key) const;
std::vector<int32_t> Keys() const;
void Remove(int32_t key);
int GetNextID();
static void WeakCallback(v8::Isolate* isolate,
v8::Persistent<v8::Object>* value,
IDWeakMap* self);
static void WeakCallback(
const v8::WeakCallbackData<v8::Object, IDWeakMap>& data);
int32_t next_id_;

View file

@ -103,4 +103,4 @@ void Initialize(v8::Handle<v8::Object> exports) {
} // namespace
NODE_MODULE(atom_common_screen, Initialize)
NODE_MODULE_X(atom_common_screen, Initialize, NULL, NM_F_BUILTIN)

View file

@ -24,4 +24,4 @@ void Initialize(v8::Handle<v8::Object> exports) {
} // namespace
NODE_MODULE(atom_common_shell, Initialize)
NODE_MODULE_X(atom_common_shell, Initialize, NULL, NM_F_BUILTIN)

View file

@ -10,8 +10,9 @@
namespace {
v8::Handle<v8::Object> CreateObjectWithName(v8::Handle<v8::String> name) {
v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New();
v8::Handle<v8::Object> CreateObjectWithName(v8::Isolate* isolate,
v8::Handle<v8::String> name) {
v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New(isolate);
t->SetClassName(name);
return t->GetFunction()->NewInstance();
}
@ -31,14 +32,15 @@ int32_t GetObjectHash(v8::Handle<v8::Object> object) {
return object->GetIdentityHash();
}
void SetDestructor(v8::Handle<v8::Object> object,
void SetDestructor(v8::Isolate* isolate,
v8::Handle<v8::Object> object,
v8::Handle<v8::Function> callback) {
atom::ObjectLifeMonitor::BindTo(object, callback);
atom::ObjectLifeMonitor::BindTo(isolate, object, callback);
}
void TakeHeapSnapshot() {
node::node_isolate->GetHeapProfiler()->TakeHeapSnapshot(
v8::String::New("test"));
void TakeHeapSnapshot(v8::Isolate* isolate) {
isolate->GetHeapProfiler()->TakeHeapSnapshot(
mate::StringToV8(isolate, "test"));
}
void Initialize(v8::Handle<v8::Object> exports) {
@ -53,4 +55,4 @@ void Initialize(v8::Handle<v8::Object> exports) {
} // namespace
NODE_MODULE(atom_common_v8_util, Initialize)
NODE_MODULE_X(atom_common_v8_util, Initialize, NULL, NM_F_BUILTIN)

View file

@ -17,9 +17,6 @@
namespace atom {
// Defined in atom_extensions.cc.
node::node_module_struct* GetBuiltinModule(const char *name, bool is_browser);
namespace {
// Async handle to wake up uv loop.
@ -35,8 +32,9 @@ base::Closure g_v8_callback;
struct DummyClass { bool crash; };
// Async handler to call next process.nextTick callbacks.
void UvCallNextTick(uv_async_t* handle, int status) {
node::Environment* env = node::Environment::GetCurrent(node_isolate);
void UvCallNextTick(uv_async_t* handle) {
v8::Isolate* isolate = v8::Isolate::GetCurrent();
node::Environment* env = node::Environment::GetCurrent(isolate);
node::Environment::TickInfo* tick_info = env->tick_info();
if (tick_info->in_tick())
@ -53,7 +51,7 @@ void UvCallNextTick(uv_async_t* handle, int status) {
}
// Async handler to execute the stored v8 callback.
void UvOnCallback(uv_async_t* handle, int status) {
void UvOnCallback(uv_async_t* handle) {
g_v8_callback.Run();
}
@ -72,45 +70,6 @@ v8::Handle<v8::Value> DumpStackFrame(v8::Isolate* isolate,
return mate::ConvertToV8(isolate, frame_dict);;
}
v8::Handle<v8::Value> Binding(v8::Handle<v8::String> module) {
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.type == 'browser'.
bool is_browser = std::string("browser") == *v8::String::Utf8Value(
process->Get(v8::String::New("type")));
// Cached in process.__atom_binding_cache.
v8::Local<v8::Object> binding_cache;
v8::Local<v8::String> bc_name = v8::String::New("__atomBindingCache");
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);
}
if (binding_cache->Has(module))
return binding_cache->Get(module)->ToObject();
if ((modp = GetBuiltinModule(*module_v, is_browser)) != NULL) {
v8::Local<v8::Object> 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 exports;
}
node::ThrowError("No such module");
return v8::Undefined();
}
void Crash() {
static_cast<DummyClass*>(NULL)->crash = true;
}
@ -119,17 +78,17 @@ void ActivateUVLoop() {
uv_async_send(&g_next_tick_uv_handle);
}
void Log(const string16& message) {
void Log(const base::string16& message) {
logging::LogMessage("CONSOLE", 0, 0).stream() << message;
}
v8::Handle<v8::Value> GetCurrentStackTrace(v8::Isolate* isolate,
int stack_limit) {
v8::Local<v8::StackTrace> stack_trace = v8::StackTrace::CurrentStackTrace(
stack_limit, v8::StackTrace::kDetailed);
isolate, stack_limit, v8::StackTrace::kDetailed);
int frame_count = stack_trace->GetFrameCount();
v8::Local<v8::Array> result = v8::Array::New(frame_count);
v8::Local<v8::Array> result = v8::Array::New(isolate, frame_count);
for (int i = 0; i < frame_count; ++i)
result->Set(i, DumpStackFrame(isolate, stack_trace->GetFrame(i)));
@ -153,10 +112,9 @@ AtomBindings::AtomBindings() {
AtomBindings::~AtomBindings() {
}
void AtomBindings::BindTo(v8::Handle<v8::Object> process) {
v8::Isolate* isolate = v8::Isolate::GetCurrent();
void AtomBindings::BindTo(v8::Isolate* isolate,
v8::Handle<v8::Object> process) {
mate::Dictionary dict(isolate, process);
dict.SetMethod("atomBinding", &Binding);
dict.SetMethod("crash", &Crash);
dict.SetMethod("activateUvLoop", &ActivateUVLoop);
dict.SetMethod("log", &Log);

View file

@ -17,9 +17,9 @@ class AtomBindings {
AtomBindings();
virtual ~AtomBindings();
// Add process.atom_binding function, which behaves like process.binding but
// Add process.atom_binding function, which behaves like process.atomBinding but
// load native code from atom-shell instead.
virtual void BindTo(v8::Handle<v8::Object> process);
virtual void BindTo(v8::Isolate* isolate, v8::Handle<v8::Object> process);
private:
DISALLOW_COPY_AND_ASSIGN(AtomBindings);

View file

@ -1,56 +0,0 @@
// Copyright (c) 2013 GitHub, Inc. All rights reserved.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#include <string.h>
#include <stdio.h>
#include "base/strings/string_util.h"
#include "vendor/node/src/node_version.h"
#include "atom/common/node_includes.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 "atom/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 "atom/common/api/atom_extensions.h" // NOLINT
node::node_module_struct* GetBuiltinModule(const char *name, bool is_browser) {
char common[128];
char spec[128];
node::node_module_struct *cur = NULL;
base::snprintf(common, sizeof(common), "atom_common_%s", name);
base::snprintf(spec, sizeof(spec),
(is_browser ? "atom_browser_%s": "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, common) == 0 || strcmp(cur->modname, spec) == 0) {
return cur;
}
}
return NULL;
}
} // namespace atom

View file

@ -1,35 +0,0 @@
// Copyright (c) 2013 GitHub, Inc. All rights reserved.
// Use of this source code is governed by the MIT 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
// Module names start with `atom_browser_` can only be used by browser process.
NODE_EXT_LIST_ITEM(atom_browser_app)
NODE_EXT_LIST_ITEM(atom_browser_auto_updater)
NODE_EXT_LIST_ITEM(atom_browser_dialog)
NODE_EXT_LIST_ITEM(atom_browser_menu)
NODE_EXT_LIST_ITEM(atom_browser_power_monitor)
NODE_EXT_LIST_ITEM(atom_browser_protocol)
NODE_EXT_LIST_ITEM(atom_browser_tray)
NODE_EXT_LIST_ITEM(atom_browser_window)
// Module names start with `atom_renderer_` can only be used by renderer
// process.
NODE_EXT_LIST_ITEM(atom_renderer_ipc)
NODE_EXT_LIST_ITEM(atom_renderer_web_view)
// Module names start with `atom_common_` can be used by both browser and
// renderer processes.
NODE_EXT_LIST_ITEM(atom_common_clipboard)
NODE_EXT_LIST_ITEM(atom_common_crash_reporter)
NODE_EXT_LIST_ITEM(atom_common_id_weak_map)
NODE_EXT_LIST_ITEM(atom_common_screen)
NODE_EXT_LIST_ITEM(atom_common_shell)
NODE_EXT_LIST_ITEM(atom_common_v8_util)
NODE_EXT_LIST_END

View file

@ -5,34 +5,32 @@
#include "atom/common/api/object_life_monitor.h"
#include "native_mate/compat.h"
namespace atom {
// static
void ObjectLifeMonitor::BindTo(v8::Handle<v8::Object> target,
void ObjectLifeMonitor::BindTo(v8::Isolate* isolate,
v8::Handle<v8::Object> target,
v8::Handle<v8::Value> destructor) {
target->SetHiddenValue(v8::String::New("destructor"), destructor);
target->SetHiddenValue(MATE_STRING_NEW(isolate, "destructor"), destructor);
ObjectLifeMonitor* olm = new ObjectLifeMonitor();
olm->handle_.reset(target);
olm->handle_.MakeWeak(olm, WeakCallback);
olm->handle_.SetWeak(olm, WeakCallback);
}
ObjectLifeMonitor::ObjectLifeMonitor() {
}
// static
void ObjectLifeMonitor::WeakCallback(v8::Isolate* isolate,
v8::Persistent<v8::Object>* value,
ObjectLifeMonitor* self) {
void ObjectLifeMonitor::WeakCallback(
const v8::WeakCallbackData<v8::Object, ObjectLifeMonitor>& data) {
// destructor.call(object, object);
{
v8::HandleScope handle_scope(isolate);
v8::Local<v8::Object> obj = self->handle_.NewHandle();
v8::Local<v8::Function>::Cast(obj->GetHiddenValue(
v8::String::New("destructor")))->Call(obj, 0, NULL);
}
delete self;
v8::Local<v8::Object> obj = data.GetValue();
v8::Local<v8::Function>::Cast(obj->GetHiddenValue(
MATE_STRING_NEW(data.GetIsolate(), "destructor")))->Call(obj, 0, NULL);
delete data.GetParameter();
}
} // namespace atom

View file

@ -12,15 +12,15 @@ namespace atom {
class ObjectLifeMonitor {
public:
static void BindTo(v8::Handle<v8::Object> target,
static void BindTo(v8::Isolate* isolate,
v8::Handle<v8::Object> target,
v8::Handle<v8::Value> destructor);
private:
ObjectLifeMonitor();
static void WeakCallback(v8::Isolate* isolate,
v8::Persistent<v8::Object>* value,
ObjectLifeMonitor* self);
static void WeakCallback(
const v8::WeakCallbackData<v8::Object, ObjectLifeMonitor>& data);
mate::ScopedPersistent<v8::Object> handle_;