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_CPPTOC_CPPTOC_REF_COUNTED_H_
|
|
|
|
#define CEF_LIBCEF_DLL_CPPTOC_CPPTOC_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"
|
|
|
|
#include "include/base/cef_macros.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
|
|
|
|
|
|
|
// Wrap a C++ class with a C structure. This is used when the class
|
|
|
|
// implementation exists on this side of the DLL boundary but will have methods
|
|
|
|
// called from the other side of the DLL boundary.
|
|
|
|
template <class ClassName, class BaseName, class StructName>
|
2017-02-09 23:07:43 +01:00
|
|
|
class CefCppToCRefCounted : public CefBaseRefCounted {
|
2012-04-03 03:34:16 +02:00
|
|
|
public:
|
2015-04-26 20:40:01 +02:00
|
|
|
// Create a new wrapper instance and associated structure reference for
|
|
|
|
// passing an object instance the other side.
|
2012-04-03 03:34:16 +02:00
|
|
|
static StructName* Wrap(CefRefPtr<BaseName> c) {
|
|
|
|
if (!c.get())
|
2020-01-15 14:36:24 +01:00
|
|
|
return nullptr;
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2017-02-09 23:07:43 +01:00
|
|
|
// Wrap our object with the CefCppToCRefCounted class.
|
2015-04-26 20:40:01 +02:00
|
|
|
ClassName* wrapper = new ClassName();
|
|
|
|
wrapper->wrapper_struct_.object_ = c.get();
|
2012-04-03 03:34:16 +02:00
|
|
|
// Add a reference to our wrapper object that will be released once our
|
|
|
|
// structure arrives on the other side.
|
|
|
|
wrapper->AddRef();
|
|
|
|
// Return the structure pointer that can now be passed to the other side.
|
|
|
|
return wrapper->GetStruct();
|
|
|
|
}
|
|
|
|
|
2015-04-26 20:40:01 +02:00
|
|
|
// Retrieve the underlying object instance for a structure reference passed
|
|
|
|
// back from the other side.
|
2012-04-03 03:34:16 +02:00
|
|
|
static CefRefPtr<BaseName> Unwrap(StructName* s) {
|
|
|
|
if (!s)
|
2020-01-15 14:36:24 +01:00
|
|
|
return nullptr;
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
// Cast our structure to the wrapper structure type.
|
2015-04-26 20:40:01 +02:00
|
|
|
WrapperStruct* wrapperStruct = GetWrapperStruct(s);
|
|
|
|
|
|
|
|
// 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_, s);
|
|
|
|
|
2012-04-03 03:34:16 +02:00
|
|
|
// Add the underlying object instance to a smart pointer.
|
2015-04-26 20:40:01 +02:00
|
|
|
CefRefPtr<BaseName> objectPtr(wrapperStruct->object_);
|
2012-04-03 03:34:16 +02:00
|
|
|
// Release the reference to our wrapper object that was added before the
|
|
|
|
// structure was passed back to us.
|
2015-04-26 20:40:01 +02:00
|
|
|
wrapperStruct->wrapper_->Release();
|
2012-04-03 03:34:16 +02:00
|
|
|
// Return the underlying object instance.
|
|
|
|
return objectPtr;
|
|
|
|
}
|
|
|
|
|
2015-04-26 20:40:01 +02:00
|
|
|
// Retrieve the underlying object instance from our own structure reference
|
|
|
|
// when the reference is passed as the required first parameter of a C API
|
|
|
|
// function call. No explicit reference counting is done in this case.
|
|
|
|
static CefRefPtr<BaseName> Get(StructName* s) {
|
|
|
|
DCHECK(s);
|
|
|
|
WrapperStruct* wrapperStruct = GetWrapperStruct(s);
|
|
|
|
// Verify that the wrapper offset was calculated correctly.
|
|
|
|
DCHECK_EQ(kWrapperType, wrapperStruct->type_);
|
|
|
|
return wrapperStruct->object_;
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// If returning the structure across the DLL boundary you should call
|
2017-02-09 23:07:43 +01:00
|
|
|
// AddRef() on this CefCppToCRefCounted object. On the other side of the DLL
|
|
|
|
// boundary, call UnderlyingRelease() on the wrapping CefCToCpp object.
|
2015-04-26 20:40:01 +02:00
|
|
|
StructName* GetStruct() { return &wrapper_struct_.struct_; }
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2017-02-09 23:07:43 +01:00
|
|
|
// CefBaseRefCounted methods increment/decrement reference counts on both this
|
|
|
|
// object and the underlying wrapper class.
|
2014-07-15 00:18:51 +02:00
|
|
|
void AddRef() const {
|
2012-04-03 03:34:16 +02:00
|
|
|
UnderlyingAddRef();
|
2014-07-15 00:18:51 +02:00
|
|
|
ref_count_.AddRef();
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
2014-07-15 00:18:51 +02:00
|
|
|
bool Release() const {
|
2012-04-03 03:34:16 +02:00
|
|
|
UnderlyingRelease();
|
2014-07-15 00:18:51 +02:00
|
|
|
if (ref_count_.Release()) {
|
2012-04-03 03:34:16 +02:00
|
|
|
delete this;
|
2014-07-15 00:18:51 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
2015-04-26 20:40:01 +02:00
|
|
|
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
|
|
|
|
2014-07-11 22:10:05 +02:00
|
|
|
protected:
|
2017-02-09 23:07:43 +01:00
|
|
|
CefCppToCRefCounted() {
|
2015-04-26 20:40:01 +02:00
|
|
|
wrapper_struct_.type_ = kWrapperType;
|
|
|
|
wrapper_struct_.wrapper_ = this;
|
|
|
|
memset(GetStruct(), 0, sizeof(StructName));
|
|
|
|
|
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
|
|
|
base->size = sizeof(StructName);
|
|
|
|
base->add_ref = struct_add_ref;
|
|
|
|
base->release = struct_release;
|
|
|
|
base->has_one_ref = struct_has_one_ref;
|
2018-08-09 22:18:24 +02:00
|
|
|
base->has_at_least_one_ref = struct_has_at_least_one_ref;
|
2015-04-26 20:40:01 +02:00
|
|
|
}
|
|
|
|
|
2019-01-23 17:10:23 +01:00
|
|
|
virtual ~CefCppToCRefCounted() {}
|
2014-07-11 22:10:05 +02:00
|
|
|
|
2012-04-03 03:34:16 +02:00
|
|
|
private:
|
2015-04-26 20:40:01 +02:00
|
|
|
// Used to associate this wrapper object, the underlying object instance and
|
|
|
|
// the structure that will be passed to the other side.
|
|
|
|
struct WrapperStruct {
|
|
|
|
CefWrapperType type_;
|
|
|
|
BaseName* object_;
|
2017-02-09 23:07:43 +01:00
|
|
|
CefCppToCRefCounted<ClassName, BaseName, StructName>* wrapper_;
|
2015-04-26 20:40:01 +02:00
|
|
|
StructName struct_;
|
|
|
|
};
|
|
|
|
|
|
|
|
static WrapperStruct* GetWrapperStruct(StructName* s) {
|
|
|
|
// 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*>(s) -
|
|
|
|
(sizeof(WrapperStruct) - sizeof(StructName)));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unwrap as the derived type.
|
|
|
|
static CefRefPtr<BaseName> UnwrapDerived(CefWrapperType type, StructName* s);
|
|
|
|
|
|
|
|
// Increment/decrement reference counts on only the underlying class.
|
2017-05-17 11:29:28 +02:00
|
|
|
void UnderlyingAddRef() const { wrapper_struct_.object_->AddRef(); }
|
2017-11-28 16:57:10 +01:00
|
|
|
void UnderlyingRelease() const { wrapper_struct_.object_->Release(); }
|
2015-04-26 20:40:01 +02:00
|
|
|
bool UnderlyingHasOneRef() const {
|
|
|
|
return wrapper_struct_.object_->HasOneRef();
|
|
|
|
}
|
2018-08-09 22:18:24 +02:00
|
|
|
bool UnderlyingHasAtLeastOneRef() const {
|
|
|
|
return wrapper_struct_.object_->HasAtLeastOneRef();
|
|
|
|
}
|
2015-04-26 20:40:01 +02:00
|
|
|
|
2017-02-09 23:07:43 +01:00
|
|
|
static void CEF_CALLBACK struct_add_ref(cef_base_ref_counted_t* base) {
|
2012-04-03 03:34:16 +02:00
|
|
|
DCHECK(base);
|
|
|
|
if (!base)
|
2014-07-15 00:18:51 +02:00
|
|
|
return;
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2015-04-26 20:40:01 +02:00
|
|
|
WrapperStruct* wrapperStruct =
|
|
|
|
GetWrapperStruct(reinterpret_cast<StructName*>(base));
|
|
|
|
// Verify that the wrapper offset was calculated correctly.
|
|
|
|
DCHECK_EQ(kWrapperType, wrapperStruct->type_);
|
|
|
|
|
|
|
|
wrapperStruct->wrapper_->AddRef();
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
2017-02-09 23:07:43 +01:00
|
|
|
static int CEF_CALLBACK struct_release(cef_base_ref_counted_t* base) {
|
2012-04-03 03:34:16 +02:00
|
|
|
DCHECK(base);
|
|
|
|
if (!base)
|
|
|
|
return 0;
|
|
|
|
|
2015-04-26 20:40:01 +02:00
|
|
|
WrapperStruct* wrapperStruct =
|
|
|
|
GetWrapperStruct(reinterpret_cast<StructName*>(base));
|
|
|
|
// Verify that the wrapper offset was calculated correctly.
|
|
|
|
DCHECK_EQ(kWrapperType, wrapperStruct->type_);
|
|
|
|
|
|
|
|
return wrapperStruct->wrapper_->Release();
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
2017-02-09 23:07:43 +01:00
|
|
|
static int CEF_CALLBACK struct_has_one_ref(cef_base_ref_counted_t* base) {
|
2012-04-03 03:34:16 +02:00
|
|
|
DCHECK(base);
|
|
|
|
if (!base)
|
|
|
|
return 0;
|
|
|
|
|
2015-04-26 20:40:01 +02:00
|
|
|
WrapperStruct* wrapperStruct =
|
|
|
|
GetWrapperStruct(reinterpret_cast<StructName*>(base));
|
|
|
|
// Verify that the wrapper offset was calculated correctly.
|
|
|
|
DCHECK_EQ(kWrapperType, wrapperStruct->type_);
|
|
|
|
|
|
|
|
return wrapperStruct->wrapper_->HasOneRef();
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
2018-08-09 22:18:24 +02:00
|
|
|
static int CEF_CALLBACK
|
|
|
|
struct_has_at_least_one_ref(cef_base_ref_counted_t* base) {
|
|
|
|
DCHECK(base);
|
|
|
|
if (!base)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
WrapperStruct* wrapperStruct =
|
|
|
|
GetWrapperStruct(reinterpret_cast<StructName*>(base));
|
|
|
|
// Verify that the wrapper offset was calculated correctly.
|
|
|
|
DCHECK_EQ(kWrapperType, wrapperStruct->type_);
|
|
|
|
|
|
|
|
return wrapperStruct->wrapper_->HasAtLeastOneRef();
|
|
|
|
}
|
|
|
|
|
2015-04-26 20:40:01 +02:00
|
|
|
WrapperStruct wrapper_struct_;
|
2014-07-15 00:18:51 +02:00
|
|
|
CefRefCount ref_count_;
|
|
|
|
|
2015-04-26 20:40:01 +02:00
|
|
|
static CefWrapperType kWrapperType;
|
|
|
|
|
2017-02-09 23:07:43 +01:00
|
|
|
DISALLOW_COPY_AND_ASSIGN(CefCppToCRefCounted);
|
2012-04-03 03:34:16 +02:00
|
|
|
};
|
|
|
|
|
2017-02-09 23:07:43 +01:00
|
|
|
#endif // CEF_LIBCEF_DLL_CPPTOC_CPPTOC_REF_COUNTED_H_
|