From 8f2b9987183eaff3e73b5dcc3ce5cc4c6f6b636d Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Thu, 17 Apr 2014 17:26:21 +0800 Subject: [PATCH] Simpilfy auto_updater api. --- atom/browser/api/atom_api_auto_updater.cc | 77 ++++++++++------------- atom/browser/api/atom_api_auto_updater.h | 25 ++++---- atom/browser/api/lib/auto-updater.coffee | 5 +- 3 files changed, 45 insertions(+), 62 deletions(-) diff --git a/atom/browser/api/atom_api_auto_updater.cc b/atom/browser/api/atom_api_auto_updater.cc index 54dbde83b6ce..2aded03f5958 100644 --- a/atom/browser/api/atom_api_auto_updater.cc +++ b/atom/browser/api/atom_api_auto_updater.cc @@ -7,7 +7,8 @@ #include "base/time/time.h" #include "base/values.h" #include "atom/browser/auto_updater.h" -#include "atom/common/v8/native_type_conversions.h" +#include "native_mate/dictionary.h" +#include "native_mate/object_template_builder.h" #include "atom/common/node_includes.h" @@ -15,8 +16,7 @@ namespace atom { namespace api { -AutoUpdater::AutoUpdater(v8::Handle wrapper) - : EventEmitter(wrapper) { +AutoUpdater::AutoUpdater() { auto_updater::AutoUpdater::SetDelegate(this); } @@ -27,7 +27,7 @@ AutoUpdater::~AutoUpdater() { void AutoUpdater::OnError(const std::string& error) { base::ListValue args; args.AppendString(error); - Emit("error", &args); + Emit("error", args); } void AutoUpdater::OnCheckingForUpdate() { @@ -54,53 +54,40 @@ void AutoUpdater::OnUpdateDownloaded(const std::string& release_notes, args.AppendString(release_name); args.AppendDouble(release_date.ToJsTime()); args.AppendString(update_url); - Emit("update-downloaded-raw", &args); + Emit("update-downloaded-raw", args); +} + +mate::ObjectTemplateBuilder AutoUpdater::GetObjectTemplateBuilder( + v8::Isolate* isolate) { + return mate::ObjectTemplateBuilder(isolate) + .SetMethod("setFeedUrl", &auto_updater::AutoUpdater::SetFeedURL) + .SetMethod("checkForUpdates", &auto_updater::AutoUpdater::CheckForUpdates) + .SetMethod("quitAndInstall", &AutoUpdater::QuitAndInstall); +} + +void AutoUpdater::QuitAndInstall() { + if (!quit_and_install_.is_null()) + quit_and_install_.Run(); } // static -void AutoUpdater::New(const v8::FunctionCallbackInfo& args) { - if (!args.IsConstructCall()) - return node::ThrowError("Require constructor call"); - - new AutoUpdater(args.This()); -} - -// static -void AutoUpdater::SetFeedURL(const v8::FunctionCallbackInfo& args) { - auto_updater::AutoUpdater::SetFeedURL(FromV8Value(args[0])); -} - -// static -void AutoUpdater::CheckForUpdates( - const v8::FunctionCallbackInfo& args) { - auto_updater::AutoUpdater::CheckForUpdates(); -} - -// static -void AutoUpdater::QuitAndInstall( - const v8::FunctionCallbackInfo& args) { - AutoUpdater* self = AutoUpdater::Unwrap(args.This()); - - if (!self->quit_and_install_.is_null()) - self->quit_and_install_.Run(); -} - -// static -void AutoUpdater::Initialize(v8::Handle target) { - v8::Local t( - v8::FunctionTemplate::New(AutoUpdater::New)); - t->InstanceTemplate()->SetInternalFieldCount(1); - t->SetClassName(v8::String::NewSymbol("AutoUpdater")); - - NODE_SET_PROTOTYPE_METHOD(t, "setFeedUrl", SetFeedURL); - NODE_SET_PROTOTYPE_METHOD(t, "checkForUpdates", CheckForUpdates); - NODE_SET_PROTOTYPE_METHOD(t, "quitAndInstall", QuitAndInstall); - - target->Set(v8::String::NewSymbol("AutoUpdater"), t->GetFunction()); +mate::Handle AutoUpdater::Create(v8::Isolate* isolate) { + return CreateHandle(isolate, new AutoUpdater); } } // namespace api } // namespace atom -NODE_MODULE(atom_browser_auto_updater, atom::api::AutoUpdater::Initialize) + +namespace { + +void Initialize(v8::Handle exports) { + v8::Isolate* isolate = v8::Isolate::GetCurrent(); + mate::Dictionary dict(isolate, exports); + dict.Set("autoUpdater", atom::api::AutoUpdater::Create(isolate)); +} + +} // namespace + +NODE_MODULE(atom_browser_auto_updater, Initialize) diff --git a/atom/browser/api/atom_api_auto_updater.h b/atom/browser/api/atom_api_auto_updater.h index 60586af15c0f..e832f9e107ff 100644 --- a/atom/browser/api/atom_api_auto_updater.h +++ b/atom/browser/api/atom_api_auto_updater.h @@ -8,23 +8,22 @@ #include #include "base/callback.h" -#include "base/memory/scoped_ptr.h" +#include "atom/browser/api/event_emitter.h" #include "atom/browser/auto_updater_delegate.h" -#include "atom/common/api/atom_api_event_emitter.h" +#include "native_mate/handle.h" namespace atom { namespace api { -class AutoUpdater : public EventEmitter, +class AutoUpdater : public mate::EventEmitter, public auto_updater::AutoUpdaterDelegate { public: - virtual ~AutoUpdater(); - - static void Initialize(v8::Handle target); + static mate::Handle Create(v8::Isolate* isolate); protected: - explicit AutoUpdater(v8::Handle wrapper); + AutoUpdater(); + virtual ~AutoUpdater(); // AutoUpdaterDelegate implementations. virtual void OnError(const std::string& error) OVERRIDE; @@ -38,14 +37,12 @@ class AutoUpdater : public EventEmitter, const std::string& update_url, const base::Closure& quit_and_install) OVERRIDE; + // mate::Wrappable implementations: + virtual mate::ObjectTemplateBuilder GetObjectTemplateBuilder( + v8::Isolate* isolate); + private: - static void New(const v8::FunctionCallbackInfo& args); - - static void SetFeedURL(const v8::FunctionCallbackInfo& args); - static void CheckForUpdates(const v8::FunctionCallbackInfo& args); - - static void ContinueUpdate(const v8::FunctionCallbackInfo& args); - static void QuitAndInstall(const v8::FunctionCallbackInfo& args); + void QuitAndInstall(); base::Closure quit_and_install_; diff --git a/atom/browser/api/lib/auto-updater.coffee b/atom/browser/api/lib/auto-updater.coffee index 4cd8dc9cb7e9..ffa0e70af0bf 100644 --- a/atom/browser/api/lib/auto-updater.coffee +++ b/atom/browser/api/lib/auto-updater.coffee @@ -1,9 +1,8 @@ -AutoUpdater = process.atomBinding('auto_updater').AutoUpdater +autoUpdater = process.atomBinding('auto_updater').autoUpdater EventEmitter = require('events').EventEmitter -AutoUpdater::__proto__ = EventEmitter.prototype +autoUpdater.__proto__ = EventEmitter.prototype -autoUpdater = new AutoUpdater autoUpdater.on 'update-downloaded-raw', (args...) -> args[3] = new Date(args[3]) # releaseDate @emit 'update-downloaded', args..., => @quitAndInstall()