2015-06-24 06:36:05 +00:00
|
|
|
// Copyright (c) 2015 GitHub, Inc.
|
|
|
|
// Use of this source code is governed by the MIT license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
|
|
|
#include "atom/browser/api/trackable_object.h"
|
|
|
|
|
2015-06-24 06:49:08 +00:00
|
|
|
#include "base/supports_user_data.h"
|
|
|
|
|
2015-06-24 06:36:05 +00:00
|
|
|
namespace mate {
|
|
|
|
|
2015-06-24 06:49:08 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
const char* kTrackedObjectKey = "TrackedObjectKey";
|
|
|
|
|
|
|
|
class IDUserData : public base::SupportsUserData::Data {
|
|
|
|
public:
|
|
|
|
explicit IDUserData(int32_t id) : id_(id) {}
|
|
|
|
|
|
|
|
operator int32_t() const { return id_; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
int32_t id_;
|
|
|
|
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(IDUserData);
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2015-06-24 06:36:05 +00:00
|
|
|
atom::IDWeakMap TrackableObject::weak_map_;
|
|
|
|
|
|
|
|
// static
|
|
|
|
TrackableObject* TrackableObject::FromWeakMapID(v8::Isolate* isolate,
|
|
|
|
int32_t id) {
|
|
|
|
v8::MaybeLocal<v8::Object> object = weak_map_.Get(isolate, id);
|
|
|
|
if (object.IsEmpty())
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
TrackableObject* self = nullptr;
|
|
|
|
mate::ConvertFromV8(isolate, object.ToLocalChecked(), &self);
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2015-06-24 06:49:08 +00:00
|
|
|
// static
|
|
|
|
TrackableObject* TrackableObject::FromWrappedClass(
|
|
|
|
v8::Isolate* isolate, base::SupportsUserData* wrapped) {
|
|
|
|
auto id = static_cast<IDUserData*>(wrapped->GetUserData(kTrackedObjectKey));
|
|
|
|
if (!id)
|
|
|
|
return nullptr;
|
|
|
|
return FromWeakMapID(isolate, *id);
|
|
|
|
}
|
|
|
|
|
2015-06-24 06:36:05 +00:00
|
|
|
// static
|
|
|
|
void TrackableObject::ReleaseAllWeakReferences() {
|
|
|
|
weak_map_.Clear();
|
|
|
|
}
|
|
|
|
|
2015-06-24 07:37:04 +00:00
|
|
|
TrackableObject::TrackableObject() : weak_map_id_(0), wrapped_(nullptr) {
|
2015-06-24 06:36:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TrackableObject::~TrackableObject() {
|
|
|
|
weak_map_.Remove(weak_map_id_);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TrackableObject::AfterInit(v8::Isolate* isolate) {
|
|
|
|
weak_map_id_ = weak_map_.Add(isolate, GetWrapper(isolate));
|
2015-06-24 07:37:04 +00:00
|
|
|
if (wrapped_)
|
|
|
|
AttachAsUserData(wrapped_);
|
2015-06-24 06:36:05 +00:00
|
|
|
}
|
|
|
|
|
2015-06-24 07:09:50 +00:00
|
|
|
void TrackableObject::AttachAsUserData(base::SupportsUserData* wrapped) {
|
2015-06-24 07:37:04 +00:00
|
|
|
if (weak_map_id_ != 0) {
|
|
|
|
wrapped->SetUserData(kTrackedObjectKey, new IDUserData(weak_map_id_));
|
|
|
|
wrapped_ = nullptr;
|
|
|
|
} else {
|
|
|
|
// If the TrackableObject is not ready yet then delay SetUserData until
|
|
|
|
// AfterInit is called.
|
|
|
|
wrapped_ = wrapped;
|
|
|
|
}
|
2015-06-24 06:49:08 +00:00
|
|
|
}
|
|
|
|
|
2015-06-24 06:36:05 +00:00
|
|
|
} // namespace mate
|