mirror of
https://bitbucket.org/chromiumembedded/cef
synced 2025-06-05 21:39:12 +02:00
Add new CefV8StackTrace and CefV8StackFrame interfaces to support retrieval of the JavaScript stack trace for the currently active V8 context (issue #682).
git-svn-id: https://chromiumembedded.googlecode.com/svn/trunk@727 5089003a-bbd8-11dd-ad1f-f1f9622dbc98
This commit is contained in:
@@ -155,16 +155,19 @@ v8::Handle<v8::String> GetV8String(const CefString& str) {
|
||||
|
||||
#if defined(CEF_STRING_TYPE_UTF16)
|
||||
void v8impl_string_dtor(char16* str) {
|
||||
delete [] str;
|
||||
delete [] str;
|
||||
}
|
||||
#elif defined(CEF_STRING_TYPE_UTF8)
|
||||
void v8impl_string_dtor(char* str) {
|
||||
delete [] str;
|
||||
delete [] str;
|
||||
}
|
||||
#endif
|
||||
|
||||
// Convert a v8::String to CefString.
|
||||
void GetCefString(v8::Handle<v8::String> str, CefString& out) {
|
||||
if (str.IsEmpty())
|
||||
return;
|
||||
|
||||
#if defined(CEF_STRING_TYPE_WIDE)
|
||||
// Allocate enough space for a worst-case conversion.
|
||||
int len = str->Utf8Length();
|
||||
@@ -392,7 +395,7 @@ bool CefRegisterExtension(const CefString& extension_name,
|
||||
}
|
||||
|
||||
|
||||
// CefV8Context implementation.
|
||||
// CefV8Context
|
||||
|
||||
// static
|
||||
CefRefPtr<CefV8Context> CefV8Context::GetCurrentContext() {
|
||||
@@ -423,7 +426,7 @@ bool CefV8Context::InContext() {
|
||||
}
|
||||
|
||||
|
||||
// CefV8ContextImpl implementation.
|
||||
// CefV8ContextImpl
|
||||
|
||||
#define CEF_V8_REQUIRE_OBJECT_RETURN(ret) \
|
||||
if (!GetHandle()->IsObject()) { \
|
||||
@@ -444,11 +447,11 @@ bool CefV8Context::InContext() {
|
||||
}
|
||||
|
||||
CefV8ContextImpl::CefV8ContextImpl(v8::Handle<v8::Context> context)
|
||||
: handle_(new Handle(context))
|
||||
#ifndef NDEBUG
|
||||
: enter_count_(0)
|
||||
, enter_count_(0)
|
||||
#endif
|
||||
{ // NOLINT(whitespace/braces)
|
||||
v8_context_ = new CefV8ContextHandle(context);
|
||||
}
|
||||
|
||||
CefV8ContextImpl::~CefV8ContextImpl() {
|
||||
@@ -484,13 +487,13 @@ CefRefPtr<CefV8Value> CefV8ContextImpl::GetGlobal() {
|
||||
CEF_REQUIRE_RT_RETURN(NULL);
|
||||
|
||||
v8::HandleScope handle_scope;
|
||||
v8::Context::Scope context_scope(v8_context_->GetHandle());
|
||||
return new CefV8ValueImpl(v8_context_->GetHandle()->Global());
|
||||
v8::Context::Scope context_scope(GetHandle());
|
||||
return new CefV8ValueImpl(GetHandle()->Global());
|
||||
}
|
||||
|
||||
bool CefV8ContextImpl::Enter() {
|
||||
CEF_REQUIRE_RT_RETURN(false);
|
||||
v8_context_->GetHandle()->Enter();
|
||||
GetHandle()->Enter();
|
||||
#ifndef NDEBUG
|
||||
++enter_count_;
|
||||
#endif
|
||||
@@ -500,7 +503,7 @@ bool CefV8ContextImpl::Enter() {
|
||||
bool CefV8ContextImpl::Exit() {
|
||||
CEF_REQUIRE_RT_RETURN(false);
|
||||
DLOG_ASSERT(enter_count_ > 0);
|
||||
v8_context_->GetHandle()->Exit();
|
||||
GetHandle()->Exit();
|
||||
#ifndef NDEBUG
|
||||
--enter_count_;
|
||||
#endif
|
||||
@@ -533,8 +536,8 @@ bool CefV8ContextImpl::Eval(const CefString& code,
|
||||
}
|
||||
|
||||
v8::HandleScope handle_scope;
|
||||
v8::Context::Scope context_scope(v8_context_->GetHandle());
|
||||
v8::Local<v8::Object> obj = v8_context_->GetHandle()->Global();
|
||||
v8::Context::Scope context_scope(GetHandle());
|
||||
v8::Local<v8::Object> obj = GetHandle()->Global();
|
||||
|
||||
// Retrieve the eval function.
|
||||
v8::Local<v8::Value> val = obj->Get(v8::String::New("eval"));
|
||||
@@ -568,28 +571,26 @@ bool CefV8ContextImpl::Eval(const CefString& code,
|
||||
}
|
||||
|
||||
v8::Local<v8::Context> CefV8ContextImpl::GetContext() {
|
||||
return v8::Local<v8::Context>::New(v8_context_->GetHandle());
|
||||
return v8::Local<v8::Context>::New(GetHandle());
|
||||
}
|
||||
|
||||
WebKit::WebFrame* CefV8ContextImpl::GetWebFrame() {
|
||||
v8::HandleScope handle_scope;
|
||||
v8::Context::Scope context_scope(v8_context_->GetHandle());
|
||||
v8::Context::Scope context_scope(GetHandle());
|
||||
WebKit::WebFrame* frame = WebKit::WebFrame::frameForCurrentContext();
|
||||
return frame;
|
||||
}
|
||||
|
||||
|
||||
// CefV8ValueHandle
|
||||
// CefV8ValueImpl::Handle
|
||||
|
||||
// Custom destructor for a v8 value handle which gets called only on the UI
|
||||
// thread.
|
||||
CefV8ValueHandle::~CefV8ValueHandle() {
|
||||
CefV8ValueImpl::Handle::~Handle() {
|
||||
if (tracker_) {
|
||||
TrackAdd(tracker_);
|
||||
v8_handle_.MakeWeak(tracker_, TrackDestructor);
|
||||
handle_.MakeWeak(tracker_, TrackDestructor);
|
||||
} else {
|
||||
v8_handle_.Dispose();
|
||||
v8_handle_.Clear();
|
||||
handle_.Dispose();
|
||||
handle_.Clear();
|
||||
}
|
||||
tracker_ = NULL;
|
||||
}
|
||||
@@ -760,8 +761,8 @@ CefRefPtr<CefV8Value> CefV8Value::CreateFunction(
|
||||
|
||||
CefV8ValueImpl::CefV8ValueImpl(v8::Handle<v8::Value> value,
|
||||
CefTrackNode* tracker)
|
||||
: rethrow_exceptions_(false) {
|
||||
v8_value_ = new CefV8ValueHandle(value, tracker);
|
||||
: handle_(new Handle(value, tracker)),
|
||||
rethrow_exceptions_(false) {
|
||||
}
|
||||
|
||||
CefV8ValueImpl::~CefV8ValueImpl() {
|
||||
@@ -1316,3 +1317,104 @@ bool CefV8ValueImpl::HasCaught(v8::TryCatch& try_catch) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// CefV8StackTrace
|
||||
|
||||
// static
|
||||
CefRefPtr<CefV8StackTrace> CefV8StackTrace::GetCurrent(int frame_limit) {
|
||||
CEF_REQUIRE_RT_RETURN(NULL);
|
||||
|
||||
v8::Handle<v8::StackTrace> stackTrace =
|
||||
v8::StackTrace::CurrentStackTrace(
|
||||
frame_limit, v8::StackTrace::kDetailed);
|
||||
if (stackTrace.IsEmpty())
|
||||
return NULL;
|
||||
return new CefV8StackTraceImpl(stackTrace);
|
||||
}
|
||||
|
||||
|
||||
// CefV8StackTraceImpl
|
||||
|
||||
CefV8StackTraceImpl::CefV8StackTraceImpl(v8::Handle<v8::StackTrace> handle)
|
||||
: handle_(new Handle(handle)) {
|
||||
}
|
||||
|
||||
CefV8StackTraceImpl::~CefV8StackTraceImpl() {
|
||||
}
|
||||
|
||||
int CefV8StackTraceImpl::GetFrameCount() {
|
||||
CEF_REQUIRE_RT_RETURN(0);
|
||||
v8::HandleScope handle_scope;
|
||||
return GetHandle()->GetFrameCount();
|
||||
}
|
||||
|
||||
CefRefPtr<CefV8StackFrame> CefV8StackTraceImpl::GetFrame(int index) {
|
||||
CEF_REQUIRE_RT_RETURN(NULL);
|
||||
v8::HandleScope handle_scope;
|
||||
v8::Handle<v8::StackFrame> stackFrame = GetHandle()->GetFrame(index);
|
||||
if (stackFrame.IsEmpty())
|
||||
return NULL;
|
||||
return new CefV8StackFrameImpl(stackFrame);
|
||||
}
|
||||
|
||||
|
||||
// CefV8StackFrameImpl
|
||||
|
||||
CefV8StackFrameImpl::CefV8StackFrameImpl(v8::Handle<v8::StackFrame> handle)
|
||||
: handle_(new Handle(handle)) {
|
||||
}
|
||||
|
||||
CefV8StackFrameImpl::~CefV8StackFrameImpl() {
|
||||
}
|
||||
|
||||
CefString CefV8StackFrameImpl::GetScriptName() {
|
||||
CefString rv;
|
||||
CEF_REQUIRE_RT_RETURN(rv);
|
||||
v8::HandleScope handle_scope;
|
||||
GetCefString(v8::Handle<v8::String>::Cast(GetHandle()->GetScriptName()), rv);
|
||||
return rv;
|
||||
}
|
||||
|
||||
CefString CefV8StackFrameImpl::GetScriptNameOrSourceURL() {
|
||||
CefString rv;
|
||||
CEF_REQUIRE_RT_RETURN(rv);
|
||||
v8::HandleScope handle_scope;
|
||||
GetCefString(
|
||||
v8::Handle<v8::String>::Cast(GetHandle()->GetScriptNameOrSourceURL()),
|
||||
rv);
|
||||
return rv;
|
||||
}
|
||||
|
||||
CefString CefV8StackFrameImpl::GetFunctionName() {
|
||||
CefString rv;
|
||||
CEF_REQUIRE_RT_RETURN(rv);
|
||||
v8::HandleScope handle_scope;
|
||||
GetCefString(
|
||||
v8::Handle<v8::String>::Cast(GetHandle()->GetFunctionName()), rv);
|
||||
return rv;
|
||||
}
|
||||
|
||||
int CefV8StackFrameImpl::GetLineNumber() {
|
||||
CEF_REQUIRE_RT_RETURN(0);
|
||||
v8::HandleScope handle_scope;
|
||||
return GetHandle()->GetLineNumber();
|
||||
}
|
||||
|
||||
int CefV8StackFrameImpl::GetColumn() {
|
||||
CEF_REQUIRE_RT_RETURN(0);
|
||||
v8::HandleScope handle_scope;
|
||||
return GetHandle()->GetColumn();
|
||||
}
|
||||
|
||||
bool CefV8StackFrameImpl::IsEval() {
|
||||
CEF_REQUIRE_RT_RETURN(false);
|
||||
v8::HandleScope handle_scope;
|
||||
return GetHandle()->IsEval();
|
||||
}
|
||||
|
||||
bool CefV8StackFrameImpl::IsConstructor() {
|
||||
CEF_REQUIRE_RT_RETURN(false);
|
||||
v8::HandleScope handle_scope;
|
||||
return GetHandle()->IsConstructor();
|
||||
}
|
||||
|
@@ -21,44 +21,38 @@ class WebFrame;
|
||||
};
|
||||
|
||||
// Template for V8 Handle types. This class is used to ensure that V8 objects
|
||||
// are only released on the UI thread.
|
||||
template <class v8class>
|
||||
class CefReleaseV8HandleOnUIThread
|
||||
: public base::RefCountedThreadSafe<CefReleaseV8HandleOnUIThread<v8class>,
|
||||
CefDeleteOnRenderThread> {
|
||||
// are only released on the render thread.
|
||||
template <typename v8class>
|
||||
class CefV8Handle :
|
||||
public base::RefCountedThreadSafe<CefV8Handle<v8class>,
|
||||
CefDeleteOnRenderThread> {
|
||||
public:
|
||||
typedef v8::Handle<v8class> handleType;
|
||||
typedef v8::Persistent<v8class> persistentType;
|
||||
typedef CefReleaseV8HandleOnUIThread<v8class> superType;
|
||||
|
||||
explicit CefReleaseV8HandleOnUIThread(handleType v) {
|
||||
v8_handle_ = persistentType::New(v);
|
||||
CefV8Handle(handleType v)
|
||||
: handle_(persistentType::New(v)) {
|
||||
}
|
||||
virtual ~CefReleaseV8HandleOnUIThread() {
|
||||
~CefV8Handle() {
|
||||
handle_.Dispose();
|
||||
handle_.Clear();
|
||||
}
|
||||
|
||||
handleType GetHandle() {
|
||||
return v8_handle_;
|
||||
}
|
||||
handleType GetHandle() { return handle_; }
|
||||
|
||||
persistentType v8_handle_;
|
||||
protected:
|
||||
persistentType handle_;
|
||||
|
||||
DISALLOW_COPY_AND_ASSIGN(CefV8Handle);
|
||||
};
|
||||
|
||||
// Special class for a v8::Context to ensure that it is deleted from the UI
|
||||
// thread.
|
||||
class CefV8ContextHandle : public CefReleaseV8HandleOnUIThread<v8::Context> {
|
||||
public:
|
||||
explicit CefV8ContextHandle(handleType context)
|
||||
: superType(context) {
|
||||
}
|
||||
|
||||
// Context handles are disposed rather than makeweak.
|
||||
~CefV8ContextHandle() {
|
||||
v8_handle_.Dispose();
|
||||
v8_handle_.Clear();
|
||||
}
|
||||
// Specialization for v8::Value with empty implementation to avoid incorrect
|
||||
// usage.
|
||||
template <>
|
||||
class CefV8Handle<v8::Value> {
|
||||
};
|
||||
|
||||
|
||||
class CefV8ContextImpl : public CefV8Context {
|
||||
public:
|
||||
explicit CefV8ContextImpl(v8::Handle<v8::Context> context);
|
||||
@@ -77,8 +71,11 @@ class CefV8ContextImpl : public CefV8Context {
|
||||
v8::Local<v8::Context> GetContext();
|
||||
WebKit::WebFrame* GetWebFrame();
|
||||
|
||||
v8::Handle<v8::Context> GetHandle() { return handle_->GetHandle(); }
|
||||
|
||||
protected:
|
||||
scoped_refptr<CefV8ContextHandle> v8_context_;
|
||||
typedef CefV8Handle<v8::Context> Handle;
|
||||
scoped_refptr<Handle> handle_;
|
||||
|
||||
#ifndef NDEBUG
|
||||
// Used in debug builds to catch missing Exits in destructor.
|
||||
@@ -86,29 +83,7 @@ class CefV8ContextImpl : public CefV8Context {
|
||||
#endif
|
||||
|
||||
IMPLEMENT_REFCOUNTING(CefV8ContextImpl);
|
||||
};
|
||||
|
||||
// Special class for a v8::Value to ensure that it is deleted from the UI
|
||||
// thread.
|
||||
class CefV8ValueHandle: public CefReleaseV8HandleOnUIThread<v8::Value> {
|
||||
public:
|
||||
CefV8ValueHandle(handleType value, CefTrackNode* tracker)
|
||||
: superType(value),
|
||||
tracker_(tracker) {
|
||||
}
|
||||
// Destructor implementation is provided in v8_impl.cc.
|
||||
~CefV8ValueHandle();
|
||||
|
||||
CefTrackNode* GetTracker() {
|
||||
return tracker_;
|
||||
}
|
||||
|
||||
private:
|
||||
// For Object and Function types, we need to hold on to a reference to their
|
||||
// internal data or function handler objects that are reference counted.
|
||||
CefTrackNode* tracker_;
|
||||
|
||||
DISALLOW_COPY_AND_ASSIGN(CefV8ValueHandle);
|
||||
DISALLOW_COPY_AND_ASSIGN(CefV8ContextImpl);
|
||||
};
|
||||
|
||||
class CefV8ValueImpl : public CefV8Value {
|
||||
@@ -167,16 +142,37 @@ class CefV8ValueImpl : public CefV8Value {
|
||||
CefRefPtr<CefV8Value> object,
|
||||
const CefV8ValueList& arguments) OVERRIDE;
|
||||
|
||||
inline v8::Handle<v8::Value> GetHandle() {
|
||||
DCHECK(v8_value_.get());
|
||||
return v8_value_->GetHandle();
|
||||
}
|
||||
v8::Handle<v8::Value> GetHandle() { return handle_->GetHandle(); }
|
||||
|
||||
protected:
|
||||
// Test for and record any exception.
|
||||
bool HasCaught(v8::TryCatch& try_catch);
|
||||
|
||||
scoped_refptr<CefV8ValueHandle> v8_value_;
|
||||
class Handle :
|
||||
public base::RefCountedThreadSafe<Handle, CefDeleteOnRenderThread> {
|
||||
public:
|
||||
typedef v8::Handle<v8::Value> handleType;
|
||||
typedef v8::Persistent<v8::Value> persistentType;
|
||||
|
||||
Handle(handleType v, CefTrackNode* tracker)
|
||||
: handle_(persistentType::New(v)),
|
||||
tracker_(tracker) {
|
||||
}
|
||||
~Handle();
|
||||
|
||||
handleType GetHandle() { return handle_; }
|
||||
|
||||
private:
|
||||
persistentType handle_;
|
||||
|
||||
// For Object and Function types, we need to hold on to a reference to their
|
||||
// internal data or function handler objects that are reference counted.
|
||||
CefTrackNode* tracker_;
|
||||
|
||||
DISALLOW_COPY_AND_ASSIGN(Handle);
|
||||
};
|
||||
scoped_refptr<Handle> handle_;
|
||||
|
||||
CefRefPtr<CefV8Exception> last_exception_;
|
||||
bool rethrow_exceptions_;
|
||||
|
||||
@@ -184,4 +180,45 @@ class CefV8ValueImpl : public CefV8Value {
|
||||
DISALLOW_COPY_AND_ASSIGN(CefV8ValueImpl);
|
||||
};
|
||||
|
||||
class CefV8StackTraceImpl : public CefV8StackTrace {
|
||||
public:
|
||||
explicit CefV8StackTraceImpl(v8::Handle<v8::StackTrace> handle);
|
||||
virtual ~CefV8StackTraceImpl();
|
||||
|
||||
virtual int GetFrameCount() OVERRIDE;
|
||||
virtual CefRefPtr<CefV8StackFrame> GetFrame(int index) OVERRIDE;
|
||||
|
||||
v8::Handle<v8::StackTrace> GetHandle() { return handle_->GetHandle(); }
|
||||
|
||||
protected:
|
||||
typedef CefV8Handle<v8::StackTrace> Handle;
|
||||
scoped_refptr<Handle> handle_;
|
||||
|
||||
IMPLEMENT_REFCOUNTING(CefV8StackTraceImpl);
|
||||
DISALLOW_COPY_AND_ASSIGN(CefV8StackTraceImpl);
|
||||
};
|
||||
|
||||
class CefV8StackFrameImpl : public CefV8StackFrame {
|
||||
public:
|
||||
explicit CefV8StackFrameImpl(v8::Handle<v8::StackFrame> handle);
|
||||
virtual ~CefV8StackFrameImpl();
|
||||
|
||||
virtual CefString GetScriptName() OVERRIDE;
|
||||
virtual CefString GetScriptNameOrSourceURL() OVERRIDE;
|
||||
virtual CefString GetFunctionName() OVERRIDE;
|
||||
virtual int GetLineNumber() OVERRIDE;
|
||||
virtual int GetColumn() OVERRIDE;
|
||||
virtual bool IsEval() OVERRIDE;
|
||||
virtual bool IsConstructor() OVERRIDE;
|
||||
|
||||
v8::Handle<v8::StackFrame> GetHandle() { return handle_->GetHandle(); }
|
||||
|
||||
protected:
|
||||
typedef CefV8Handle<v8::StackFrame> Handle;
|
||||
scoped_refptr<Handle> handle_;
|
||||
|
||||
IMPLEMENT_REFCOUNTING(CefV8StackFrameImpl);
|
||||
DISALLOW_COPY_AND_ASSIGN(CefV8StackFrameImpl);
|
||||
};
|
||||
|
||||
#endif // CEF_LIBCEF_RENDERER_V8_IMPL_H_
|
||||
|
Reference in New Issue
Block a user