electron/native_mate/scoped_persistent.h

112 lines
2.7 KiB
C
Raw Normal View History

2014-04-15 03:04:36 +00:00
// Copyright 2014 Cheng Zhao. All rights reserved.
// Use of this source code is governed by MIT license that can be found in the
// LICENSE file.
#ifndef NATIVE_MATE_SCOPED_PERSISTENT_H_
#define NATIVE_MATE_SCOPED_PERSISTENT_H_
#include "base/memory/ref_counted.h"
2014-07-21 05:07:56 +00:00
#include "native_mate/converter.h"
2014-04-15 03:04:36 +00:00
#include "v8/include/v8.h"
namespace mate {
// A v8::Persistent handle to a V8 value which destroys and clears the
// underlying handle on destruction.
template <typename T>
class ScopedPersistent {
public:
ScopedPersistent() : isolate_(v8::Isolate::GetCurrent()) {}
2014-04-15 03:04:36 +00:00
2015-05-22 11:11:02 +00:00
ScopedPersistent(v8::Isolate* isolate, v8::Local<v8::Value> handle)
2014-07-21 05:07:56 +00:00
: isolate_(isolate) {
2015-05-22 11:11:02 +00:00
reset(isolate, v8::Local<T>::Cast(handle));
2014-04-15 03:04:36 +00:00
}
~ScopedPersistent() {
reset();
}
2015-05-22 11:11:02 +00:00
void reset(v8::Isolate* isolate, v8::Local<T> handle) {
2014-07-21 05:07:56 +00:00
if (!handle.IsEmpty()) {
isolate_ = isolate;
MATE_PERSISTENT_ASSIGN(T, isolate, handle_, handle);
2014-07-21 05:07:56 +00:00
} else {
2014-04-15 03:04:36 +00:00
reset();
2014-07-21 05:07:56 +00:00
}
2014-04-15 03:04:36 +00:00
}
void reset() {
MATE_PERSISTENT_RESET(handle_);
2014-04-15 03:04:36 +00:00
}
bool IsEmpty() const {
return handle_.IsEmpty();
}
2015-05-22 11:11:02 +00:00
v8::Local<T> NewHandle() const {
return NewHandle(isolate_);
2014-04-15 03:04:36 +00:00
}
2015-05-22 11:11:02 +00:00
v8::Local<T> NewHandle(v8::Isolate* isolate) const {
2014-04-15 03:04:36 +00:00
if (handle_.IsEmpty())
return v8::Local<T>();
2014-08-10 11:05:25 +00:00
return MATE_PERSISTENT_TO_LOCAL(T, isolate, handle_);
2014-04-15 03:04:36 +00:00
}
template<typename P, typename C>
void SetWeak(P* parameter, C callback) {
MATE_PERSISTENT_SET_WEAK(handle_, parameter, callback);
2014-06-28 13:26:34 +00:00
}
2014-07-21 05:07:56 +00:00
v8::Isolate* isolate() const { return isolate_; }
2014-04-15 03:04:36 +00:00
private:
2014-07-21 05:07:56 +00:00
v8::Isolate* isolate_;
2014-04-15 03:04:36 +00:00
v8::Persistent<T> handle_;
DISALLOW_COPY_AND_ASSIGN(ScopedPersistent);
};
template <typename T>
class RefCountedPersistent : public ScopedPersistent<T>,
public base::RefCounted<RefCountedPersistent<T>> {
public:
RefCountedPersistent() {}
2015-05-22 11:11:02 +00:00
RefCountedPersistent(v8::Isolate* isolate, v8::Local<v8::Value> handle)
: ScopedPersistent<T>(isolate, handle) {
2014-04-15 03:04:36 +00:00
}
protected:
friend class base::RefCounted<RefCountedPersistent<T>>;
~RefCountedPersistent() {}
private:
DISALLOW_COPY_AND_ASSIGN(RefCountedPersistent);
};
2014-07-21 05:07:56 +00:00
template<typename T>
struct Converter<ScopedPersistent<T> > {
2015-05-22 11:11:02 +00:00
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
2014-07-21 05:07:56 +00:00
const ScopedPersistent<T>& val) {
return val.NewHandle(isolate);
}
static bool FromV8(v8::Isolate* isolate,
2015-05-22 11:11:02 +00:00
v8::Local<v8::Value> val,
2014-07-21 05:07:56 +00:00
ScopedPersistent<T>* out) {
2015-05-22 11:11:02 +00:00
v8::Local<T> converted;
if (!Converter<v8::Local<T> >::FromV8(isolate, val, &converted))
2014-07-21 05:07:56 +00:00
return false;
out->reset(isolate, converted);
return true;
}
};
2014-04-15 03:04:36 +00:00
} // namespace mate
#endif // NATIVE_MATE_SCOPED_PERSISTENT_H_