2015-06-24 14:36:05 +08: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.
|
|
|
|
|
2019-10-25 22:03:28 +09:00
|
|
|
#include "shell/common/gin_helper/trackable_object.h"
|
2015-06-24 14:36:05 +08:00
|
|
|
|
2018-09-12 19:25:56 -05:00
|
|
|
#include <memory>
|
|
|
|
|
2015-06-24 17:58:12 +08:00
|
|
|
#include "base/bind.h"
|
2015-06-24 14:49:08 +08:00
|
|
|
#include "base/supports_user_data.h"
|
2019-06-19 13:46:59 -07:00
|
|
|
#include "shell/browser/electron_browser_main_parts.h"
|
2019-10-31 16:56:00 +09:00
|
|
|
#include "shell/common/gin_helper/locker.h"
|
2015-06-24 14:49:08 +08:00
|
|
|
|
2019-10-25 22:03:28 +09:00
|
|
|
namespace gin_helper {
|
2015-06-24 14:36:05 +08:00
|
|
|
|
2015-06-24 14:49:08 +08:00
|
|
|
namespace {
|
|
|
|
|
2021-07-01 17:51:52 -07:00
|
|
|
const char kTrackedObjectKey[] = "TrackedObjectKey";
|
2015-06-24 14:49:08 +08:00
|
|
|
|
|
|
|
class IDUserData : public base::SupportsUserData::Data {
|
|
|
|
public:
|
|
|
|
explicit IDUserData(int32_t id) : id_(id) {}
|
|
|
|
|
|
|
|
operator int32_t() const { return id_; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
int32_t id_;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2021-01-26 19:16:21 +01:00
|
|
|
TrackableObjectBase::TrackableObjectBase() {
|
2019-10-25 22:03:28 +09:00
|
|
|
// TODO(zcbenz): Make TrackedObject work in renderer process.
|
2019-10-31 16:56:00 +09:00
|
|
|
DCHECK(gin_helper::Locker::IsBrowserProcess())
|
2019-10-25 22:03:28 +09:00
|
|
|
<< "This class only works for browser process";
|
2015-06-24 14:36:05 +08:00
|
|
|
}
|
|
|
|
|
2019-09-16 18:12:00 -04:00
|
|
|
TrackableObjectBase::~TrackableObjectBase() = default;
|
2015-06-24 14:36:05 +08:00
|
|
|
|
2018-03-30 18:54:55 +05:30
|
|
|
base::OnceClosure TrackableObjectBase::GetDestroyClosure() {
|
|
|
|
return base::BindOnce(&TrackableObjectBase::Destroy,
|
|
|
|
weak_factory_.GetWeakPtr());
|
2015-12-03 15:38:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void TrackableObjectBase::Destroy() {
|
|
|
|
delete this;
|
|
|
|
}
|
|
|
|
|
2015-06-24 17:58:12 +08:00
|
|
|
void TrackableObjectBase::AttachAsUserData(base::SupportsUserData* wrapped) {
|
2017-10-03 12:42:35 +08:00
|
|
|
wrapped->SetUserData(kTrackedObjectKey,
|
2018-04-17 21:55:30 -04:00
|
|
|
std::make_unique<IDUserData>(weak_map_id_));
|
2015-06-24 14:49:08 +08:00
|
|
|
}
|
|
|
|
|
2015-06-24 17:58:12 +08:00
|
|
|
// static
|
2017-10-03 12:42:35 +08:00
|
|
|
int32_t TrackableObjectBase::GetIDFromWrappedClass(
|
|
|
|
base::SupportsUserData* wrapped) {
|
|
|
|
if (wrapped) {
|
2018-05-22 00:18:38 +02:00
|
|
|
auto* id =
|
|
|
|
static_cast<IDUserData*>(wrapped->GetUserData(kTrackedObjectKey));
|
2017-10-03 12:42:35 +08:00
|
|
|
if (id)
|
|
|
|
return *id;
|
|
|
|
}
|
|
|
|
return 0;
|
2015-06-24 17:58:12 +08:00
|
|
|
}
|
|
|
|
|
2019-10-25 22:03:28 +09:00
|
|
|
} // namespace gin_helper
|