Move all sources under atom/.

This commit is contained in:
Cheng Zhao 2014-03-16 08:30:26 +08:00
parent 26ddbbb0ee
commit 516d46444d
217 changed files with 519 additions and 519 deletions

View 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 "atom/common/api/api_messages.h"
// Generate constructors.
#include "ipc/struct_constructor_macros.h"
#include "atom/common/api/api_messages.h"
// Generate destructors.
#include "ipc/struct_destructor_macros.h"
#include "atom/common/api/api_messages.h"
// Generate param traits write methods.
#include "ipc/param_traits_write_macros.h"
namespace IPC {
#include "atom/common/api/api_messages.h"
} // namespace IPC
// Generate param traits read methods.
#include "ipc/param_traits_read_macros.h"
namespace IPC {
#include "atom/common/api/api_messages.h"
} // namespace IPC
// Generate param traits log methods.
#include "ipc/param_traits_log_macros.h"
namespace IPC {
#include "atom/common/api/api_messages.h"
} // namespace IPC

View file

@ -0,0 +1,38 @@
// 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 "base/strings/string16.h"
#include "base/values.h"
#include "atom/common/draggable_region.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_STRUCT_TRAITS_BEGIN(atom::DraggableRegion)
IPC_STRUCT_TRAITS_MEMBER(draggable)
IPC_STRUCT_TRAITS_MEMBER(bounds)
IPC_STRUCT_TRAITS_END()
IPC_MESSAGE_ROUTED2(AtomViewHostMsg_Message,
string16 /* channel */,
ListValue /* arguments */)
IPC_SYNC_MESSAGE_ROUTED2_1(AtomViewHostMsg_Message_Sync,
string16 /* channel */,
ListValue /* arguments */,
string16 /* result (in JSON) */)
IPC_MESSAGE_ROUTED2(AtomViewMsg_Message,
string16 /* channel */,
ListValue /* arguments */)
// Sent by the renderer when the draggable regions are updated.
IPC_MESSAGE_ROUTED1(AtomViewHostMsg_UpdateDraggableRegions,
std::vector<atom::DraggableRegion> /* regions */)

View file

@ -0,0 +1,88 @@
// 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 "atom/common/api/atom_api_clipboard.h"
#include <string>
#include "atom/common/v8/native_type_conversions.h"
#include "ui/base/clipboard/clipboard.h"
#include "atom/common/v8/node_common.h"
namespace atom {
namespace api {
// static
void Clipboard::Has(const v8::FunctionCallbackInfo<v8::Value>& args) {
std::string format_string;
if (!FromV8Arguments(args, &format_string))
return node::ThrowTypeError("Bad argument");
ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread();
ui::Clipboard::FormatType format(ui::Clipboard::GetFormatType(format_string));
args.GetReturnValue().Set(
clipboard->IsFormatAvailable(format, ui::Clipboard::BUFFER_STANDARD));
}
// static
void Clipboard::Read(const v8::FunctionCallbackInfo<v8::Value>& args) {
std::string format_string;
if (!FromV8Arguments(args, &format_string))
return node::ThrowTypeError("Bad argument");
ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread();
ui::Clipboard::FormatType format(ui::Clipboard::GetFormatType(format_string));
std::string data;
clipboard->ReadData(format, &data);
args.GetReturnValue().Set(ToV8Value(data));
}
// static
void Clipboard::ReadText(const v8::FunctionCallbackInfo<v8::Value>& args) {
ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread();
std::string data;
clipboard->ReadAsciiText(ui::Clipboard::BUFFER_STANDARD, &data);
args.GetReturnValue().Set(ToV8Value(data));
}
// static
void Clipboard::WriteText(const v8::FunctionCallbackInfo<v8::Value>& args) {
std::string text;
if (!FromV8Arguments(args, &text))
return node::ThrowTypeError("Bad argument");
ui::Clipboard::ObjectMap object_map;
object_map[ui::Clipboard::CBF_TEXT].push_back(
std::vector<char>(text.begin(), text.end()));
ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread();
clipboard->WriteObjects(ui::Clipboard::BUFFER_STANDARD, object_map);
}
// static
void Clipboard::Clear(const v8::FunctionCallbackInfo<v8::Value>& args) {
ui::Clipboard::GetForCurrentThread()->Clear(ui::Clipboard::BUFFER_STANDARD);
}
// static
void Clipboard::Initialize(v8::Handle<v8::Object> target) {
NODE_SET_METHOD(target, "has", Has);
NODE_SET_METHOD(target, "read", Read);
NODE_SET_METHOD(target, "readText", ReadText);
NODE_SET_METHOD(target, "writeText", WriteText);
NODE_SET_METHOD(target, "clear", Clear);
}
} // namespace api
} // namespace atom
NODE_MODULE(atom_common_clipboard, atom::api::Clipboard::Initialize)

View 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.
#ifndef ATOM_COMMON_API_ATOM_API_CLIPBOARD_H_
#define ATOM_COMMON_API_ATOM_API_CLIPBOARD_H_
#include "base/basictypes.h"
#include "v8/include/v8.h"
namespace atom {
namespace api {
class Clipboard {
public:
static void Initialize(v8::Handle<v8::Object> target);
private:
static void Has(const v8::FunctionCallbackInfo<v8::Value>& args);
static void Read(const v8::FunctionCallbackInfo<v8::Value>& args);
static void ReadText(const v8::FunctionCallbackInfo<v8::Value>& args);
static void WriteText(const v8::FunctionCallbackInfo<v8::Value>& args);
static void Clear(const v8::FunctionCallbackInfo<v8::Value>& args);
DISALLOW_IMPLICIT_CONSTRUCTORS(Clipboard);
};
} // namespace api
} // namespace atom
#endif // ATOM_COMMON_API_ATOM_API_CLIPBOARD_H_

View file

@ -0,0 +1,38 @@
// 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 "atom/common/api/atom_api_crash_reporter.h"
#include "atom/common/crash_reporter/crash_reporter.h"
#include "atom/common/v8/native_type_conversions.h"
#include "atom/common/v8/node_common.h"
namespace atom {
namespace api {
// static
void CrashReporter::Start(const v8::FunctionCallbackInfo<v8::Value>& args) {
std::string product_name, company_name, submit_url;
bool auto_submit, skip_system;
std::map<std::string, std::string> dict;
if (!FromV8Arguments(args, &product_name, &company_name, &submit_url,
&auto_submit, &skip_system, &dict))
return node::ThrowTypeError("Bad argument");
crash_reporter::CrashReporter::GetInstance()->Start(
product_name, company_name, submit_url, auto_submit, skip_system, dict);
}
// static
void CrashReporter::Initialize(v8::Handle<v8::Object> target) {
NODE_SET_METHOD(target, "start", Start);
}
} // namespace api
} // namespace atom
NODE_MODULE(atom_common_crash_reporter, atom::api::CrashReporter::Initialize)

View file

@ -0,0 +1,29 @@
// 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_API_CRASH_REPORTER_H_
#define ATOM_COMMON_API_ATOM_API_CRASH_REPORTER_H_
#include "base/basictypes.h"
#include "v8/include/v8.h"
namespace atom {
namespace api {
class CrashReporter {
public:
static void Initialize(v8::Handle<v8::Object> target);
private:
static void Start(const v8::FunctionCallbackInfo<v8::Value>& args);
DISALLOW_IMPLICIT_CONSTRUCTORS(CrashReporter);
};
} // namespace api
} // namespace atom
#endif // ATOM_COMMON_API_ATOM_API_CRASH_REPORTER_H_

View file

@ -0,0 +1,81 @@
// 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 "atom/common/api/atom_api_event_emitter.h"
#include <vector>
#include "base/logging.h"
#include "atom/browser/api/atom_api_event.h"
#include "atom/common/v8/native_type_conversions.h"
#include "atom/common/v8/node_common.h"
namespace atom {
namespace api {
EventEmitter::EventEmitter(v8::Handle<v8::Object> wrapper) {
Wrap(wrapper);
}
EventEmitter::~EventEmitter() {
// Use Locker in browser process.
scoped_ptr<v8::Locker> locker;
if (node::g_standalone_mode)
locker.reset(new v8::Locker(node_isolate));
// Clear the aligned pointer, it should have been done by ObjectWrap but
// somehow node v0.11.x changed this behaviour.
v8::HandleScope handle_scope(node_isolate);
handle()->SetAlignedPointerInInternalField(0, NULL);
}
bool EventEmitter::Emit(const std::string& name) {
base::ListValue args;
return Emit(name, &args);
}
bool EventEmitter::Emit(const std::string& name, base::ListValue* args) {
// Use Locker in browser process.
scoped_ptr<v8::Locker> locker;
if (node::g_standalone_mode)
locker.reset(new v8::Locker(node_isolate));
v8::HandleScope handle_scope(node_isolate);
v8::Handle<v8::Context> context = v8::Context::GetCurrent();
scoped_ptr<V8ValueConverter> converter(new V8ValueConverter);
v8::Handle<v8::Object> v8_event = Event::CreateV8Object();
Event* event = Event::Unwrap<Event>(v8_event);
// Generate arguments for calling handle.emit.
std::vector<v8::Handle<v8::Value>> v8_args;
v8_args.reserve(args->GetSize() + 2);
v8_args.push_back(ToV8Value(name));
v8_args.push_back(v8_event);
for (size_t i = 0; i < args->GetSize(); i++) {
base::Value* value = NULL;
if (args->Get(i, &value)) {
DCHECK(value);
v8_args.push_back(converter->ToV8Value(value, context));
} else {
NOTREACHED() << "Wrong offset " << i << " for " << *args;
}
}
node::MakeCallback(handle(), "emit", v8_args.size(), &v8_args[0]);
bool prevent_default = event->prevent_default();
// Don't wait for V8 GC, delete it immediately.
delete event;
return prevent_default;
}
} // namespace api
} // namespace atom

View file

@ -0,0 +1,43 @@
// 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_API_EVENT_EMITTER_H_
#define ATOM_COMMON_API_ATOM_API_EVENT_EMITTER_H_
#include <string>
#include "base/basictypes.h"
#include "vendor/node/src/node_object_wrap.h"
namespace base {
class ListValue;
}
namespace atom {
namespace api {
// Class interiting EventEmitter should assume it's a javascript object which
// interits require('events').EventEmitter, this class provides many helper
// methods to do event processing in C++.
class EventEmitter : public node::ObjectWrap {
public:
virtual ~EventEmitter();
// Emit an event and returns whether the handler has called preventDefault().
bool Emit(const std::string& name);
bool Emit(const std::string& name, base::ListValue* args);
protected:
explicit EventEmitter(v8::Handle<v8::Object> wrapper);
private:
DISALLOW_COPY_AND_ASSIGN(EventEmitter);
};
} // namespace api
} // namespace atom
#endif // ATOM_COMMON_API_ATOM_API_EVENT_EMITTER_H_

View file

@ -0,0 +1,142 @@
// Copyright (c) 2013 GitHub, Inc. All rights reserved.
// Copyright (c) 2012 Intel Corp. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "atom/common/api/atom_api_id_weak_map.h"
#include <algorithm>
#include "base/logging.h"
#include "atom/common/v8/native_type_conversions.h"
#include "atom/common/v8/node_common.h"
namespace atom {
namespace api {
IDWeakMap::IDWeakMap()
: next_id_(0) {
}
IDWeakMap::~IDWeakMap() {
}
bool IDWeakMap::Has(int key) const {
return map_.find(key) != map_.end();
}
void IDWeakMap::Erase(int key) {
if (Has(key))
map_.erase(key);
else
LOG(WARNING) << "Object with key " << key << " is being GCed for twice.";
}
int IDWeakMap::GetNextID() {
return ++next_id_;
}
// static
void IDWeakMap::WeakCallback(v8::Isolate* isolate,
v8::Persistent<v8::Object>* value,
IDWeakMap* self) {
v8::HandleScope handle_scope(isolate);
v8::Local<v8::Object> local = v8::Local<v8::Object>::New(isolate, *value);
self->Erase(
FromV8Value(local->GetHiddenValue(v8::String::New("IDWeakMapKey"))));
}
// static
void IDWeakMap::New(const v8::FunctionCallbackInfo<v8::Value>& args) {
(new IDWeakMap)->Wrap(args.This());
}
// static
void IDWeakMap::Add(const v8::FunctionCallbackInfo<v8::Value>& args) {
if (!args[0]->IsObject())
return node::ThrowTypeError("Bad argument");
IDWeakMap* self = Unwrap<IDWeakMap>(args.This());
int key = self->GetNextID();
v8::Local<v8::Object> v8_value = args[0]->ToObject();
v8_value->SetHiddenValue(v8::String::New("IDWeakMapKey"), ToV8Value(key));
self->map_[key] = new RefCountedPersistent<v8::Object>(v8_value);
self->map_[key]->MakeWeak(self, WeakCallback);
args.GetReturnValue().Set(key);
}
// static
void IDWeakMap::Get(const v8::FunctionCallbackInfo<v8::Value>& args) {
int key;
if (!FromV8Arguments(args, &key))
return node::ThrowTypeError("Bad argument");
IDWeakMap* self = Unwrap<IDWeakMap>(args.This());
if (!self->Has(key))
return node::ThrowError("Invalid key");
args.GetReturnValue().Set(self->map_[key]->NewHandle());
}
// static
void IDWeakMap::Has(const v8::FunctionCallbackInfo<v8::Value>& args) {
int key;
if (!FromV8Arguments(args, &key))
return node::ThrowTypeError("Bad argument");
IDWeakMap* self = Unwrap<IDWeakMap>(args.This());
args.GetReturnValue().Set(self->Has(key));
}
// static
void IDWeakMap::Keys(const v8::FunctionCallbackInfo<v8::Value>& args) {
IDWeakMap* self = Unwrap<IDWeakMap>(args.This());
v8::Local<v8::Array> keys = v8::Array::New(self->map_.size());
int i = 0;
for (auto el = self->map_.begin(); el != self->map_.end(); ++el) {
keys->Set(i, ToV8Value(el->first));
++i;
}
args.GetReturnValue().Set(keys);
}
// static
void IDWeakMap::Remove(const v8::FunctionCallbackInfo<v8::Value>& args) {
int key;
if (!FromV8Arguments(args, &key))
return node::ThrowTypeError("Bad argument");
IDWeakMap* self = Unwrap<IDWeakMap>(args.This());
if (!self->Has(key))
return node::ThrowError("Invalid key");
self->Erase(key);
}
// static
void IDWeakMap::Initialize(v8::Handle<v8::Object> target) {
v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New(New);
t->InstanceTemplate()->SetInternalFieldCount(1);
t->SetClassName(v8::String::NewSymbol("IDWeakMap"));
NODE_SET_PROTOTYPE_METHOD(t, "add", Add);
NODE_SET_PROTOTYPE_METHOD(t, "get", Get);
NODE_SET_PROTOTYPE_METHOD(t, "has", Has);
NODE_SET_PROTOTYPE_METHOD(t, "keys", Keys);
NODE_SET_PROTOTYPE_METHOD(t, "remove", Remove);
target->Set(v8::String::NewSymbol("IDWeakMap"), t->GetFunction());
}
} // namespace api
} // namespace atom
NODE_MODULE(atom_common_id_weak_map, atom::api::IDWeakMap::Initialize)

View file

@ -0,0 +1,52 @@
// Copyright (c) 2013 GitHub, Inc. All rights reserved.
// Copyright (c) 2012 Intel Corp. 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_API_ID_WEAK_MAP_H_
#define ATOM_COMMON_API_ATOM_API_ID_WEAK_MAP_H_
#include <map>
#include "base/basictypes.h"
#include "atom/common/v8/scoped_persistent.h"
#include "vendor/node/src/node_object_wrap.h"
namespace atom {
namespace api {
class IDWeakMap : public node::ObjectWrap {
public:
static void Initialize(v8::Handle<v8::Object> target);
private:
IDWeakMap();
virtual ~IDWeakMap();
bool Has(int key) const;
void Erase(int key);
int GetNextID();
static void WeakCallback(v8::Isolate* isolate,
v8::Persistent<v8::Object>* value,
IDWeakMap* self);
static void New(const v8::FunctionCallbackInfo<v8::Value>& args);
static void Add(const v8::FunctionCallbackInfo<v8::Value>& args);
static void Get(const v8::FunctionCallbackInfo<v8::Value>& args);
static void Has(const v8::FunctionCallbackInfo<v8::Value>& args);
static void Keys(const v8::FunctionCallbackInfo<v8::Value>& args);
static void Remove(const v8::FunctionCallbackInfo<v8::Value>& args);
int next_id_;
std::map<int, RefCountedV8Object> map_;
DISALLOW_COPY_AND_ASSIGN(IDWeakMap);
};
} // namespace api
} // namespace atom
#endif // ATOM_COMMON_API_ATOM_API_ID_WEAK_MAP_H_

View file

@ -0,0 +1,93 @@
// 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 "atom/common/api/atom_api_screen.h"
#include "atom/common/v8/native_type_conversions.h"
#include "ui/gfx/screen.h"
#include "atom/common/v8/node_common.h"
#if defined(TOOLKIT_GTK)
#include "base/command_line.h"
#include "ui/gfx/gtk_util.h"
#endif
#define UNWRAP_SCREEN_AND_CHECK \
Screen* self = ObjectWrap::Unwrap<Screen>(args.This()); \
if (self == NULL) \
return node::ThrowError("Screen is already destroyed")
namespace atom {
namespace api {
namespace {
v8::Handle<v8::Object> DisplayToV8Value(const gfx::Display& display) {
v8::Handle<v8::Object> obj = v8::Object::New();
obj->Set(ToV8Value("bounds"), ToV8Value(display.bounds()));
obj->Set(ToV8Value("workArea"), ToV8Value(display.work_area()));
obj->Set(ToV8Value("size"), ToV8Value(display.size()));
obj->Set(ToV8Value("workAreaSize"), ToV8Value(display.work_area_size()));
obj->Set(ToV8Value("scaleFactor"), ToV8Value(display.device_scale_factor()));
return obj;
}
} // namespace
Screen::Screen(v8::Handle<v8::Object> wrapper)
: EventEmitter(wrapper),
screen_(gfx::Screen::GetNativeScreen()) {
}
Screen::~Screen() {
}
// static
void Screen::New(const v8::FunctionCallbackInfo<v8::Value>& args) {
v8::HandleScope scope(args.GetIsolate());
if (!args.IsConstructCall())
return node::ThrowError("Require constructor call");
new Screen(args.This());
}
// static
void Screen::GetCursorScreenPoint(
const v8::FunctionCallbackInfo<v8::Value>& args) {
UNWRAP_SCREEN_AND_CHECK;
args.GetReturnValue().Set(ToV8Value(self->screen_->GetCursorScreenPoint()));
}
// static
void Screen::GetPrimaryDisplay(
const v8::FunctionCallbackInfo<v8::Value>& args) {
UNWRAP_SCREEN_AND_CHECK;
gfx::Display display = self->screen_->GetPrimaryDisplay();
args.GetReturnValue().Set(DisplayToV8Value(display));
}
// static
void Screen::Initialize(v8::Handle<v8::Object> target) {
#if defined(TOOLKIT_GTK)
gfx::GdkInitFromCommandLine(*CommandLine::ForCurrentProcess());
#endif
v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New(New);
t->InstanceTemplate()->SetInternalFieldCount(1);
t->SetClassName(v8::String::NewSymbol("Screen"));
NODE_SET_PROTOTYPE_METHOD(t, "getCursorScreenPoint", GetCursorScreenPoint);
NODE_SET_PROTOTYPE_METHOD(t, "getPrimaryDisplay", GetPrimaryDisplay);
target->Set(v8::String::NewSymbol("Screen"), t->GetFunction());
}
} // namespace api
} // namespace atom
NODE_MODULE(atom_common_screen, atom::api::Screen::Initialize)

View file

@ -0,0 +1,44 @@
// 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_API_SCREEN_H_
#define ATOM_COMMON_API_ATOM_API_SCREEN_H_
#include "atom/common/api/atom_api_event_emitter.h"
namespace gfx {
class Screen;
}
namespace atom {
namespace api {
class Screen : public EventEmitter {
public:
virtual ~Screen();
static void Initialize(v8::Handle<v8::Object> target);
protected:
explicit Screen(v8::Handle<v8::Object> wrapper);
private:
static void New(const v8::FunctionCallbackInfo<v8::Value>& args);
static void GetCursorScreenPoint(
const v8::FunctionCallbackInfo<v8::Value>& args);
static void GetPrimaryDisplay(
const v8::FunctionCallbackInfo<v8::Value>& args);
gfx::Screen* screen_;
DISALLOW_COPY_AND_ASSIGN(Screen);
};
} // namespace api
} // namespace atom
#endif // ATOM_COMMON_API_ATOM_API_SCREEN_H_

View file

@ -0,0 +1,74 @@
// 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 "atom/common/api/atom_api_shell.h"
#include <string>
#include "base/files/file_path.h"
#include "atom/common/platform_util.h"
#include "atom/common/v8/native_type_conversions.h"
#include "url/gurl.h"
#include "atom/common/v8/node_common.h"
namespace atom {
namespace api {
// static
void Shell::ShowItemInFolder(const v8::FunctionCallbackInfo<v8::Value>& args) {
base::FilePath file_path;
if (!FromV8Arguments(args, &file_path))
return node::ThrowTypeError("Bad argument");
platform_util::ShowItemInFolder(file_path);
}
// static
void Shell::OpenItem(const v8::FunctionCallbackInfo<v8::Value>& args) {
base::FilePath file_path;
if (!FromV8Arguments(args, &file_path))
return node::ThrowTypeError("Bad argument");
platform_util::OpenItem(file_path);
}
// static
void Shell::OpenExternal(const v8::FunctionCallbackInfo<v8::Value>& args) {
GURL url;
if (!FromV8Arguments(args, &url))
return node::ThrowTypeError("Bad argument");
platform_util::OpenExternal(url);
}
// static
void Shell::MoveItemToTrash(const v8::FunctionCallbackInfo<v8::Value>& args) {
base::FilePath file_path;
if (!FromV8Arguments(args, &file_path))
return node::ThrowTypeError("Bad argument");
platform_util::MoveItemToTrash(file_path);
}
// static
void Shell::Beep(const v8::FunctionCallbackInfo<v8::Value>& args) {
platform_util::Beep();
}
// static
void Shell::Initialize(v8::Handle<v8::Object> target) {
NODE_SET_METHOD(target, "showItemInFolder", ShowItemInFolder);
NODE_SET_METHOD(target, "openItem", OpenItem);
NODE_SET_METHOD(target, "openExternal", OpenExternal);
NODE_SET_METHOD(target, "moveItemToTrash", MoveItemToTrash);
NODE_SET_METHOD(target, "beep", Beep);
}
} // namespace api
} // namespace atom
NODE_MODULE(atom_common_shell, atom::api::Shell::Initialize)

View 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.
#ifndef ATOM_COMMON_API_ATOM_API_SHELL_H_
#define ATOM_COMMON_API_ATOM_API_SHELL_H_
#include "base/basictypes.h"
#include "v8/include/v8.h"
namespace atom {
namespace api {
class Shell {
public:
static void Initialize(v8::Handle<v8::Object> target);
private:
static void ShowItemInFolder(const v8::FunctionCallbackInfo<v8::Value>& args);
static void OpenItem(const v8::FunctionCallbackInfo<v8::Value>& args);
static void OpenExternal(const v8::FunctionCallbackInfo<v8::Value>& args);
static void MoveItemToTrash(const v8::FunctionCallbackInfo<v8::Value>& args);
static void Beep(const v8::FunctionCallbackInfo<v8::Value>& args);
DISALLOW_IMPLICIT_CONSTRUCTORS(Shell);
};
} // namespace api
} // namespace atom
#endif // ATOM_COMMON_API_ATOM_API_SHELL_H_

View file

@ -0,0 +1,61 @@
// 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 "atom/common/api/object_life_monitor.h"
#include "atom/common/v8/native_type_conversions.h"
#include "v8/include/v8-profiler.h"
#include "atom/common/v8/node_common.h"
namespace atom {
namespace api {
namespace {
void CreateObjectWithName(const v8::FunctionCallbackInfo<v8::Value>& args) {
v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New();
t->SetClassName(args[0]->ToString());
args.GetReturnValue().Set(t->GetFunction()->NewInstance());
}
void GetHiddenValue(const v8::FunctionCallbackInfo<v8::Value>& args) {
args.GetReturnValue().Set(
args[0]->ToObject()->GetHiddenValue(args[1]->ToString()));
}
void SetHiddenValue(const v8::FunctionCallbackInfo<v8::Value>& args) {
args[0]->ToObject()->SetHiddenValue(args[1]->ToString(), args[2]);
}
void GetObjectHash(const v8::FunctionCallbackInfo<v8::Value>& args) {
args.GetReturnValue().Set(args[0]->ToObject()->GetIdentityHash());
}
void SetDestructor(const v8::FunctionCallbackInfo<v8::Value>& args) {
ObjectLifeMonitor::BindTo(args[0]->ToObject(), args[1]);
}
void TakeHeapSnapshot(const v8::FunctionCallbackInfo<v8::Value>& args) {
node::node_isolate->GetHeapProfiler()->TakeHeapSnapshot(
v8::String::New("test"));
}
} // namespace
void InitializeV8Util(v8::Handle<v8::Object> target) {
NODE_SET_METHOD(target, "createObjectWithName", CreateObjectWithName);
NODE_SET_METHOD(target, "getHiddenValue", GetHiddenValue);
NODE_SET_METHOD(target, "setHiddenValue", SetHiddenValue);
NODE_SET_METHOD(target, "getObjectHash", GetObjectHash);
NODE_SET_METHOD(target, "setDestructor", SetDestructor);
NODE_SET_METHOD(target, "takeHeapSnapshot", TakeHeapSnapshot);
}
} // namespace api
} // namespace atom
NODE_MODULE(atom_common_v8_util, atom::api::InitializeV8Util)

View file

@ -0,0 +1,190 @@
// 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 "atom/common/api/atom_bindings.h"
#include "base/logging.h"
#include "atom/common/atom_version.h"
#include "atom/common/v8/native_type_conversions.h"
#include "atom/common/v8/node_common.h"
namespace atom {
namespace {
static int kMaxCallStackSize = 200; // Same with WebKit.
// Async handle to wake up uv loop.
static uv_async_t g_next_tick_uv_handle;
// Async handle to execute the stored v8 callback.
static uv_async_t g_callback_uv_handle;
// Stored v8 callback, to be called by the async handler.
RefCountedV8Function g_v8_callback;
// Dummy class type that used for crashing the program.
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);
node::Environment::TickInfo* tick_info = env->tick_info();
if (tick_info->in_tick())
return;
if (tick_info->length() == 0) {
tick_info->set_index(0);
return;
}
tick_info->set_in_tick(true);
env->tick_callback_function()->Call(env->process_object(), 0, NULL);
tick_info->set_in_tick(false);
}
// Async handler to execute the stored v8 callback.
void UvOnCallback(uv_async_t* handle, int status) {
v8::HandleScope handle_scope(node_isolate);
v8::Handle<v8::Object> global = v8::Context::GetCurrent()->Global();
g_v8_callback->NewHandle()->Call(global, 0, NULL);
}
// Called when there is a fatal error in V8, we just crash the process here so
// we can get the stack trace.
void FatalErrorCallback(const char* location, const char* message) {
LOG(ERROR) << "Fatal error in V8: " << location << " " << message;
static_cast<DummyClass*>(NULL)->crash = true;
}
v8::Handle<v8::Object> DumpStackFrame(v8::Handle<v8::StackFrame> stack_frame) {
v8::Local<v8::Object> result = v8::Object::New();
result->Set(ToV8Value("line"), ToV8Value(stack_frame->GetLineNumber()));
result->Set(ToV8Value("column"), ToV8Value(stack_frame->GetColumn()));
v8::Handle<v8::String> script = stack_frame->GetScriptName();
if (!script.IsEmpty())
result->Set(ToV8Value("script"), script);
v8::Handle<v8::String> function = stack_frame->GetScriptNameOrSourceURL();
if (!function.IsEmpty())
result->Set(ToV8Value("function"), function);
return result;
}
} // namespace
// Defined in atom_extensions.cc.
node::node_module_struct* GetBuiltinModule(const char *name, bool is_browser);
AtomBindings::AtomBindings() {
uv_async_init(uv_default_loop(), &g_next_tick_uv_handle, UvCallNextTick);
uv_async_init(uv_default_loop(), &g_callback_uv_handle, UvOnCallback);
v8::V8::SetFatalErrorHandler(FatalErrorCallback);
}
AtomBindings::~AtomBindings() {
}
void AtomBindings::BindTo(v8::Handle<v8::Object> process) {
NODE_SET_METHOD(process, "atomBinding", Binding);
NODE_SET_METHOD(process, "crash", Crash);
NODE_SET_METHOD(process, "activateUvLoop", ActivateUVLoop);
NODE_SET_METHOD(process, "log", Log);
NODE_SET_METHOD(process, "getCurrentStackTrace", GetCurrentStackTrace);
NODE_SET_METHOD(process, "scheduleCallback", ScheduleCallback);
process->Get(v8::String::New("versions"))->ToObject()->
Set(v8::String::New("atom-shell"), v8::String::New(ATOM_VERSION_STRING));
}
// static
void AtomBindings::Binding(const v8::FunctionCallbackInfo<v8::Value>& args) {
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("__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);
}
v8::Local<v8::Object> exports;
if (binding_cache->Has(module)) {
exports = binding_cache->Get(module)->ToObject();
return args.GetReturnValue().Set(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 args.GetReturnValue().Set(exports);
}
return node::ThrowError("No such module");
}
// static
void AtomBindings::Crash(const v8::FunctionCallbackInfo<v8::Value>& args) {
static_cast<DummyClass*>(NULL)->crash = true;
}
// static
void AtomBindings::ActivateUVLoop(
const v8::FunctionCallbackInfo<v8::Value>& args) {
uv_async_send(&g_next_tick_uv_handle);
}
// static
void AtomBindings::Log(const v8::FunctionCallbackInfo<v8::Value>& args) {
v8::String::Utf8Value str(args[0]);
logging::LogMessage("CONSOLE", 0, 0).stream() << *str;
}
// static
void AtomBindings::GetCurrentStackTrace(
const v8::FunctionCallbackInfo<v8::Value>& args) {
int stack_limit = kMaxCallStackSize;
FromV8Arguments(args, &stack_limit);
v8::Local<v8::StackTrace> stack_trace = v8::StackTrace::CurrentStackTrace(
stack_limit, v8::StackTrace::kDetailed);
int frame_count = stack_trace->GetFrameCount();
v8::Local<v8::Array> result = v8::Array::New(frame_count);
for (int i = 0; i < frame_count; ++i)
result->Set(i, DumpStackFrame(stack_trace->GetFrame(i)));
args.GetReturnValue().Set(result);
}
// static
void AtomBindings::ScheduleCallback(
const v8::FunctionCallbackInfo<v8::Value>& args) {
if (!FromV8Arguments(args, &g_v8_callback))
return node::ThrowTypeError("Bad arguments");
uv_async_send(&g_callback_uv_handle);
}
} // namespace atom

View file

@ -0,0 +1,36 @@
// 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 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);
private:
static void Binding(const v8::FunctionCallbackInfo<v8::Value>& args);
static void Crash(const v8::FunctionCallbackInfo<v8::Value>& args);
static void ActivateUVLoop(const v8::FunctionCallbackInfo<v8::Value>& args);
static void Log(const v8::FunctionCallbackInfo<v8::Value>& args);
static void GetCurrentStackTrace(
const v8::FunctionCallbackInfo<v8::Value>& args);
static void ScheduleCallback(const v8::FunctionCallbackInfo<v8::Value>& args);
DISALLOW_COPY_AND_ASSIGN(AtomBindings);
};
} // namespace atom
#endif // ATOM_COMMON_API_ATOM_BINDINGS_

View file

@ -0,0 +1,56 @@
// 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 "base/strings/string_util.h"
#include "vendor/node/src/node_version.h"
#include "atom/common/v8/node_common.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

@ -0,0 +1,34 @@
// 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
// 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_ipc)
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_window)
// Module names start with `atom_renderer_` can only be used by renderer
// process.
NODE_EXT_LIST_ITEM(atom_renderer_ipc)
// 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

@ -0,0 +1,25 @@
module.exports =
class CallbacksRegistry
constructor: ->
@emptyFunc = -> throw new Error "Browser trying to call a non-exist callback
in renderer, this usually happens when renderer code forgot to release
a callback installed on objects in browser when renderer was going to be
unloaded or released."
@callbacks = {}
add: (callback) ->
id = Math.random().toString()
@callbacks[id] = callback
id
get: (id) ->
@callbacks[id] ? ->
call: (id, args...) ->
@get(id).call global, args...
apply: (id, args...) ->
@get(id).apply global, args...
remove: (id) ->
delete @callbacks[id]

View file

@ -0,0 +1 @@
module.exports = process.atomBinding 'clipboard'

View file

@ -0,0 +1,38 @@
{spawn} = require 'child_process'
binding = process.atomBinding 'crash_reporter'
class CrashReporter
start: (options={}) ->
{productName, companyName, submitUrl, autoSubmit, ignoreSystemCrashHandler, extra} = options
productName ?= 'Atom-Shell'
companyName ?= 'GitHub, Inc'
submitUrl ?= 'http://54.249.141.255:1127/post'
autoSubmit ?= true
ignoreSystemCrashHandler ?= false
extra ?= {}
extra._productName ?= productName
extra._companyName ?= companyName
extra._version ?=
if process.__atom_type is 'browser'
require('app').getVersion()
else
require('remote').require('app').getVersion()
start = -> binding.start productName, companyName, submitUrl, autoSubmit, ignoreSystemCrashHandler, extra
if process.platform is 'win32'
args = [
"--reporter-url=#{submitUrl}"
"--application-name=#{productName}"
"--v=1"
]
env = ATOM_SHELL_INTERNAL_CRASH_SERVICE: 1
spawn process.execPath, args, {env, detached: true}
start()
else
start()
module.exports = new CrashReporter

View file

@ -0,0 +1,3 @@
IDWeakMap = process.atomBinding('id_weak_map').IDWeakMap
module.exports = IDWeakMap

View file

@ -0,0 +1,3 @@
{Screen} = process.atomBinding 'screen'
module.exports = new Screen

View file

@ -0,0 +1 @@
module.exports = process.atomBinding 'shell'

View file

@ -0,0 +1,38 @@
// Copyright (c) 2013 GitHub, Inc. All rights reserved.
// Copyright (c) 2012 Intel Corp. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "atom/common/api/object_life_monitor.h"
namespace atom {
// static
void ObjectLifeMonitor::BindTo(v8::Handle<v8::Object> target,
v8::Handle<v8::Value> destructor) {
target->SetHiddenValue(v8::String::New("destructor"), destructor);
ObjectLifeMonitor* olm = new ObjectLifeMonitor();
olm->handle_.reset(target);
olm->handle_.MakeWeak(olm, WeakCallback);
}
ObjectLifeMonitor::ObjectLifeMonitor() {
}
// static
void ObjectLifeMonitor::WeakCallback(v8::Isolate* isolate,
v8::Persistent<v8::Object>* value,
ObjectLifeMonitor* self) {
// 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;
}
} // namespace atom

View file

@ -0,0 +1,33 @@
// Copyright (c) 2013 GitHub, Inc. All rights reserved.
// Copyright (c) 2012 Intel Corp. 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_OBJECT_LIFE_MONITOR_H_
#define ATOM_COMMON_API_OBJECT_LIFE_MONITOR_H_
#include "base/basictypes.h"
#include "atom/common/v8/scoped_persistent.h"
namespace atom {
class ObjectLifeMonitor {
public:
static void BindTo(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);
ScopedPersistent<v8::Object> handle_;
DISALLOW_COPY_AND_ASSIGN(ObjectLifeMonitor);
};
} // namespace atom
#endif // ATOM_COMMON_API_OBJECT_LIFE_MONITOR_H_