2012-04-03 03:34:16 +02:00
|
|
|
// Copyright (c) 2009 The Chromium Embedded Framework Authors. All rights
|
|
|
|
// reserved. Use of this source code is governed by a BSD-style license that
|
|
|
|
// can be found in the LICENSE file.
|
|
|
|
|
2017-02-09 23:07:43 +01:00
|
|
|
#ifndef CEF_LIBCEF_DLL_CTOCPP_CTOCPP_REF_COUNTED_H_
|
|
|
|
#define CEF_LIBCEF_DLL_CTOCPP_CTOCPP_REF_COUNTED_H_
|
2012-04-03 03:34:16 +02:00
|
|
|
#pragma once
|
|
|
|
|
2014-07-11 22:10:05 +02:00
|
|
|
#include "include/base/cef_logging.h"
|
2012-04-03 03:34:16 +02:00
|
|
|
#include "include/capi/cef_base_capi.h"
|
2017-05-17 11:29:28 +02:00
|
|
|
#include "include/cef_base.h"
|
2015-04-26 20:40:01 +02:00
|
|
|
#include "libcef_dll/wrapper_types.h"
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2015-04-26 20:40:01 +02:00
|
|
|
// Wrap a C structure with a C++ class. This is used when the implementation
|
2012-04-03 03:34:16 +02:00
|
|
|
// exists on the other side of the DLL boundary but will have methods called on
|
|
|
|
// this side of the DLL boundary.
|
|
|
|
template <class ClassName, class BaseName, class StructName>
|
2017-02-09 23:07:43 +01:00
|
|
|
class CefCToCppRefCounted : public BaseName {
|
2012-04-03 03:34:16 +02:00
|
|
|
public:
|
2021-12-06 22:59:14 +01:00
|
|
|
CefCToCppRefCounted(const CefCToCppRefCounted&) = delete;
|
|
|
|
CefCToCppRefCounted& operator=(const CefCToCppRefCounted&) = delete;
|
|
|
|
|
2015-04-26 20:40:01 +02:00
|
|
|
// Create a new wrapper instance for a structure reference received from the
|
|
|
|
// other side.
|
|
|
|
static CefRefPtr<BaseName> Wrap(StructName* s);
|
|
|
|
|
|
|
|
// Retrieve the underlying structure reference from a wrapper instance for
|
|
|
|
// return back to the other side.
|
|
|
|
static StructName* Unwrap(CefRefPtr<BaseName> c);
|
|
|
|
|
2017-02-09 23:07:43 +01:00
|
|
|
// CefBaseRefCounted methods increment/decrement reference counts on both this
|
|
|
|
// object and the underlying wrapped structure.
|
2015-04-26 20:40:01 +02:00
|
|
|
void AddRef() const {
|
|
|
|
UnderlyingAddRef();
|
|
|
|
ref_count_.AddRef();
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
2015-04-26 20:40:01 +02:00
|
|
|
bool Release() const;
|
|
|
|
bool HasOneRef() const { return UnderlyingHasOneRef(); }
|
2018-08-09 22:18:24 +02:00
|
|
|
bool HasAtLeastOneRef() const { return UnderlyingHasAtLeastOneRef(); }
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2015-04-26 20:40:01 +02:00
|
|
|
protected:
|
2021-12-06 22:59:14 +01:00
|
|
|
CefCToCppRefCounted() = default;
|
|
|
|
virtual ~CefCToCppRefCounted() = default;
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2017-02-07 22:25:11 +01:00
|
|
|
// If returning the structure across the DLL boundary use Unwrap() instead.
|
|
|
|
StructName* GetStruct() const {
|
|
|
|
WrapperStruct* wrapperStruct = GetWrapperStruct(this);
|
|
|
|
// Verify that the wrapper offset was calculated correctly.
|
|
|
|
DCHECK_EQ(kWrapperType, wrapperStruct->type_);
|
|
|
|
return wrapperStruct->struct_;
|
|
|
|
}
|
|
|
|
|
2015-04-26 20:40:01 +02:00
|
|
|
private:
|
|
|
|
// Used to associate this wrapper object and the structure reference received
|
|
|
|
// from the other side.
|
|
|
|
struct WrapperStruct;
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2015-04-26 20:40:01 +02:00
|
|
|
static WrapperStruct* GetWrapperStruct(const BaseName* obj);
|
|
|
|
|
|
|
|
// Unwrap as the derived type.
|
|
|
|
static StructName* UnwrapDerived(CefWrapperType type, BaseName* c);
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
// Increment/decrement reference counts on only the underlying class.
|
2018-07-12 19:55:56 +02:00
|
|
|
NO_SANITIZE("cfi-icall")
|
2014-07-15 00:18:51 +02:00
|
|
|
void UnderlyingAddRef() const {
|
2017-02-09 23:07:43 +01:00
|
|
|
cef_base_ref_counted_t* base =
|
|
|
|
reinterpret_cast<cef_base_ref_counted_t*>(GetStruct());
|
2015-04-26 20:40:01 +02:00
|
|
|
if (base->add_ref)
|
|
|
|
base->add_ref(base);
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
2015-04-26 20:40:01 +02:00
|
|
|
|
2018-07-12 19:55:56 +02:00
|
|
|
NO_SANITIZE("cfi-icall")
|
2014-07-15 00:18:51 +02:00
|
|
|
bool UnderlyingRelease() const {
|
2017-02-09 23:07:43 +01:00
|
|
|
cef_base_ref_counted_t* base =
|
|
|
|
reinterpret_cast<cef_base_ref_counted_t*>(GetStruct());
|
2015-04-26 20:40:01 +02:00
|
|
|
if (!base->release)
|
2014-07-15 00:18:51 +02:00
|
|
|
return false;
|
2015-04-26 20:40:01 +02:00
|
|
|
return base->release(base) ? true : false;
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
2015-04-26 20:40:01 +02:00
|
|
|
|
2018-07-12 19:55:56 +02:00
|
|
|
NO_SANITIZE("cfi-icall")
|
2014-07-15 00:18:51 +02:00
|
|
|
bool UnderlyingHasOneRef() const {
|
2017-02-09 23:07:43 +01:00
|
|
|
cef_base_ref_counted_t* base =
|
|
|
|
reinterpret_cast<cef_base_ref_counted_t*>(GetStruct());
|
2015-04-26 20:40:01 +02:00
|
|
|
if (!base->has_one_ref)
|
2014-07-15 00:18:51 +02:00
|
|
|
return false;
|
2015-04-26 20:40:01 +02:00
|
|
|
return base->has_one_ref(base) ? true : false;
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
2018-08-09 22:18:24 +02:00
|
|
|
NO_SANITIZE("cfi-icall")
|
|
|
|
bool UnderlyingHasAtLeastOneRef() const {
|
|
|
|
cef_base_ref_counted_t* base =
|
|
|
|
reinterpret_cast<cef_base_ref_counted_t*>(GetStruct());
|
2021-11-22 16:28:06 +01:00
|
|
|
if (!base->has_at_least_one_ref)
|
2018-08-09 22:18:24 +02:00
|
|
|
return false;
|
|
|
|
return base->has_at_least_one_ref(base) ? true : false;
|
|
|
|
}
|
|
|
|
|
2014-07-15 00:18:51 +02:00
|
|
|
CefRefCount ref_count_;
|
|
|
|
|
2015-04-26 20:40:01 +02:00
|
|
|
static CefWrapperType kWrapperType;
|
2012-04-03 03:34:16 +02:00
|
|
|
};
|
|
|
|
|
2015-04-26 20:40:01 +02:00
|
|
|
template <class ClassName, class BaseName, class StructName>
|
2017-05-17 11:29:28 +02:00
|
|
|
struct CefCToCppRefCounted<ClassName, BaseName, StructName>::WrapperStruct {
|
2015-04-26 20:40:01 +02:00
|
|
|
CefWrapperType type_;
|
|
|
|
StructName* struct_;
|
|
|
|
ClassName wrapper_;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class ClassName, class BaseName, class StructName>
|
2017-05-17 11:29:28 +02:00
|
|
|
CefRefPtr<BaseName> CefCToCppRefCounted<ClassName, BaseName, StructName>::Wrap(
|
|
|
|
StructName* s) {
|
2015-04-26 20:40:01 +02:00
|
|
|
if (!s)
|
2020-01-15 14:36:24 +01:00
|
|
|
return nullptr;
|
2015-04-26 20:40:01 +02:00
|
|
|
|
2017-02-09 23:07:43 +01:00
|
|
|
// Wrap their structure with the CefCToCppRefCounted object.
|
2015-04-26 20:40:01 +02:00
|
|
|
WrapperStruct* wrapperStruct = new WrapperStruct;
|
|
|
|
wrapperStruct->type_ = kWrapperType;
|
|
|
|
wrapperStruct->struct_ = s;
|
|
|
|
|
|
|
|
// Put the wrapper object in a smart pointer.
|
|
|
|
CefRefPtr<BaseName> wrapperPtr(&wrapperStruct->wrapper_);
|
|
|
|
// Release the reference that was added to the CefCppToC wrapper object on
|
|
|
|
// the other side before their structure was passed to us.
|
|
|
|
wrapperStruct->wrapper_.UnderlyingRelease();
|
|
|
|
// Return the smart pointer.
|
|
|
|
return wrapperPtr;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class ClassName, class BaseName, class StructName>
|
2017-02-09 23:07:43 +01:00
|
|
|
StructName* CefCToCppRefCounted<ClassName, BaseName, StructName>::Unwrap(
|
|
|
|
CefRefPtr<BaseName> c) {
|
2015-04-26 20:40:01 +02:00
|
|
|
if (!c.get())
|
2020-01-15 14:36:24 +01:00
|
|
|
return nullptr;
|
2015-04-26 20:40:01 +02:00
|
|
|
|
|
|
|
WrapperStruct* wrapperStruct = GetWrapperStruct(c.get());
|
|
|
|
|
|
|
|
// If the type does not match this object then we need to unwrap as the
|
|
|
|
// derived type.
|
|
|
|
if (wrapperStruct->type_ != kWrapperType)
|
|
|
|
return UnwrapDerived(wrapperStruct->type_, c.get());
|
|
|
|
|
|
|
|
// Add a reference to the CefCppToC wrapper object on the other side that
|
|
|
|
// will be released once the structure is received.
|
|
|
|
wrapperStruct->wrapper_.UnderlyingAddRef();
|
|
|
|
// Return their original structure.
|
|
|
|
return wrapperStruct->struct_;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class ClassName, class BaseName, class StructName>
|
2017-02-09 23:07:43 +01:00
|
|
|
bool CefCToCppRefCounted<ClassName, BaseName, StructName>::Release() const {
|
2015-04-26 20:40:01 +02:00
|
|
|
UnderlyingRelease();
|
|
|
|
if (ref_count_.Release()) {
|
|
|
|
WrapperStruct* wrapperStruct = GetWrapperStruct(this);
|
|
|
|
// Verify that the wrapper offset was calculated correctly.
|
|
|
|
DCHECK_EQ(kWrapperType, wrapperStruct->type_);
|
|
|
|
delete wrapperStruct;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class ClassName, class BaseName, class StructName>
|
2017-02-09 23:07:43 +01:00
|
|
|
typename CefCToCppRefCounted<ClassName, BaseName, StructName>::WrapperStruct*
|
2017-05-17 11:29:28 +02:00
|
|
|
CefCToCppRefCounted<ClassName, BaseName, StructName>::GetWrapperStruct(
|
|
|
|
const BaseName* obj) {
|
2015-04-26 20:40:01 +02:00
|
|
|
// Offset using the WrapperStruct size instead of individual member sizes to
|
|
|
|
// avoid problems due to platform/compiler differences in structure padding.
|
|
|
|
return reinterpret_cast<WrapperStruct*>(
|
|
|
|
reinterpret_cast<char*>(const_cast<BaseName*>(obj)) -
|
|
|
|
(sizeof(WrapperStruct) - sizeof(ClassName)));
|
|
|
|
}
|
|
|
|
|
2017-02-09 23:07:43 +01:00
|
|
|
#endif // CEF_LIBCEF_DLL_CTOCPP_CTOCPP_REF_COUNTED_H_
|