2010-10-03 23:04:50 +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.
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
#ifndef CEF_LIBCEF_DLL_CPPTOC_BASE_CPPTOC_H_
|
|
|
|
#define CEF_LIBCEF_DLL_CPPTOC_BASE_CPPTOC_H_
|
|
|
|
#pragma once
|
2010-10-03 23:04:50 +02:00
|
|
|
|
2011-12-23 18:36:30 +01:00
|
|
|
#include "include/cef_base.h"
|
|
|
|
#include "include/capi/cef_base_capi.h"
|
2010-10-03 23:04:50 +02:00
|
|
|
#include "libcef_dll/cef_logging.h"
|
|
|
|
|
|
|
|
|
|
|
|
// CefCppToC implementation for CefBase.
|
2012-01-10 00:46:23 +01:00
|
|
|
class CefBaseCppToC : public CefBase {
|
|
|
|
public:
|
2010-10-03 23:04:50 +02:00
|
|
|
// Use this method to retrieve the underlying class instance from our
|
|
|
|
// own structure when the structure is passed as the required first
|
|
|
|
// parameter of a C API function call. No explicit reference counting
|
|
|
|
// is done in this case.
|
2012-01-10 00:46:23 +01:00
|
|
|
static CefRefPtr<CefBase> Get(cef_base_t* s) {
|
2011-12-08 02:38:30 +01:00
|
|
|
DCHECK(s);
|
|
|
|
|
2010-10-03 23:04:50 +02:00
|
|
|
// Cast our structure to the wrapper structure type.
|
|
|
|
CefBaseCppToC::Struct* wrapperStruct =
|
|
|
|
reinterpret_cast<CefBaseCppToC::Struct*>(s);
|
|
|
|
// Return the underlying object instance.
|
|
|
|
return wrapperStruct->class_->GetClass();
|
|
|
|
}
|
2012-01-10 00:46:23 +01:00
|
|
|
|
2010-10-03 23:04:50 +02:00
|
|
|
// Use this method to create a wrapper structure for passing our class
|
|
|
|
// instance to the other side.
|
2012-01-10 00:46:23 +01:00
|
|
|
static cef_base_t* Wrap(CefRefPtr<CefBase> c) {
|
2011-12-08 02:38:30 +01:00
|
|
|
if (!c.get())
|
|
|
|
return NULL;
|
|
|
|
|
2010-10-03 23:04:50 +02:00
|
|
|
// Wrap our object with the CefCppToC class.
|
|
|
|
CefBaseCppToC* wrapper = new CefBaseCppToC(c);
|
|
|
|
// 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();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use this method to retrieve the underlying class instance when receiving
|
|
|
|
// our wrapper structure back from the other side.
|
2012-01-10 00:46:23 +01:00
|
|
|
static CefRefPtr<CefBase> Unwrap(cef_base_t* s) {
|
2011-12-08 02:38:30 +01:00
|
|
|
if (!s)
|
|
|
|
return NULL;
|
|
|
|
|
2010-10-03 23:04:50 +02:00
|
|
|
// Cast our structure to the wrapper structure type.
|
|
|
|
CefBaseCppToC::Struct* wrapperStruct =
|
|
|
|
reinterpret_cast<CefBaseCppToC::Struct*>(s);
|
|
|
|
// Add the underlying object instance to a smart pointer.
|
|
|
|
CefRefPtr<CefBase> objectPtr(wrapperStruct->class_->GetClass());
|
|
|
|
// Release the reference to our wrapper object that was added before the
|
|
|
|
// structure was passed back to us.
|
|
|
|
wrapperStruct->class_->Release();
|
|
|
|
// Return the underlying object instance.
|
|
|
|
return objectPtr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Structure representation with pointer to the C++ class.
|
2012-01-10 00:46:23 +01:00
|
|
|
struct Struct {
|
2010-10-03 23:04:50 +02:00
|
|
|
cef_base_t struct_;
|
|
|
|
CefBaseCppToC* class_;
|
|
|
|
};
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
explicit CefBaseCppToC(CefBase* cls)
|
|
|
|
: class_(cls) {
|
2010-10-03 23:04:50 +02:00
|
|
|
DCHECK(cls);
|
|
|
|
|
|
|
|
struct_.class_ = this;
|
2012-01-10 00:46:23 +01:00
|
|
|
|
2010-10-03 23:04:50 +02:00
|
|
|
// zero the underlying structure and set base members
|
|
|
|
memset(&struct_.struct_, 0, sizeof(cef_base_t));
|
|
|
|
struct_.struct_.size = sizeof(cef_base_t);
|
|
|
|
struct_.struct_.add_ref = struct_add_ref;
|
|
|
|
struct_.struct_.release = struct_release;
|
|
|
|
struct_.struct_.get_refct = struct_get_refct;
|
|
|
|
}
|
|
|
|
virtual ~CefBaseCppToC() {}
|
|
|
|
|
|
|
|
CefBase* GetClass() { return class_; }
|
|
|
|
|
|
|
|
// If returning the structure across the DLL boundary you should call
|
|
|
|
// AddRef() on this CefCppToC object. On the other side of the DLL boundary,
|
|
|
|
// call UnderlyingRelease() on the wrapping CefCToCpp object.
|
|
|
|
cef_base_t* GetStruct() { return &struct_.struct_; }
|
|
|
|
|
|
|
|
// CefBase methods increment/decrement reference counts on both this object
|
|
|
|
// and the underlying wrapper class.
|
2012-01-10 00:46:23 +01:00
|
|
|
int AddRef() {
|
2010-10-03 23:04:50 +02:00
|
|
|
UnderlyingAddRef();
|
2011-05-20 16:42:25 +02:00
|
|
|
return refct_.AddRef();
|
2010-10-03 23:04:50 +02:00
|
|
|
}
|
2012-01-10 00:46:23 +01:00
|
|
|
int Release() {
|
2010-10-03 23:04:50 +02:00
|
|
|
UnderlyingRelease();
|
2011-05-20 16:42:25 +02:00
|
|
|
int retval = refct_.Release();
|
|
|
|
if (retval == 0)
|
|
|
|
delete this;
|
|
|
|
return retval;
|
2010-10-03 23:04:50 +02:00
|
|
|
}
|
2011-05-20 16:42:25 +02:00
|
|
|
int GetRefCt() { return refct_.GetRefCt(); }
|
2010-10-03 23:04:50 +02:00
|
|
|
|
|
|
|
// Increment/decrement reference counts on only the underlying class.
|
|
|
|
int UnderlyingAddRef() { return class_->AddRef(); }
|
|
|
|
int UnderlyingRelease() { return class_->Release(); }
|
|
|
|
int UnderlyingGetRefCt() { return class_->GetRefCt(); }
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
private:
|
|
|
|
static int CEF_CALLBACK struct_add_ref(struct _cef_base_t* base) {
|
2010-10-03 23:04:50 +02:00
|
|
|
DCHECK(base);
|
2012-01-10 00:46:23 +01:00
|
|
|
if (!base)
|
2010-10-03 23:04:50 +02:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
Struct* impl = reinterpret_cast<Struct*>(base);
|
|
|
|
return impl->class_->AddRef();
|
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
static int CEF_CALLBACK struct_release(struct _cef_base_t* base) {
|
2010-10-03 23:04:50 +02:00
|
|
|
DCHECK(base);
|
2012-01-10 00:46:23 +01:00
|
|
|
if (!base)
|
2010-10-03 23:04:50 +02:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
Struct* impl = reinterpret_cast<Struct*>(base);
|
|
|
|
return impl->class_->Release();
|
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
static int CEF_CALLBACK struct_get_refct(struct _cef_base_t* base) {
|
2010-10-03 23:04:50 +02:00
|
|
|
DCHECK(base);
|
2012-01-10 00:46:23 +01:00
|
|
|
if (!base)
|
2010-10-03 23:04:50 +02:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
Struct* impl = reinterpret_cast<Struct*>(base);
|
|
|
|
return impl->class_->GetRefCt();
|
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
protected:
|
2011-05-20 16:42:25 +02:00
|
|
|
CefRefCount refct_;
|
2010-10-03 23:04:50 +02:00
|
|
|
Struct struct_;
|
|
|
|
CefBase* class_;
|
|
|
|
};
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
#endif // CEF_LIBCEF_DLL_CPPTOC_BASE_CPPTOC_H_
|