2012-01-19 19:52:59 +01:00
|
|
|
// Copyright (c) 2012 The Chromium Embedded Framework Authors. All rights
|
2010-10-03 23:04:50 +02:00
|
|
|
// reserved. Use of this source code is governed by a BSD-style license that
|
|
|
|
// can be found in the LICENSE file.
|
|
|
|
|
2012-01-19 19:52:59 +01:00
|
|
|
#include <string>
|
2012-10-29 22:54:57 +01:00
|
|
|
#include <vector>
|
2012-01-19 19:52:59 +01:00
|
|
|
|
2012-05-23 21:01:04 +02:00
|
|
|
#include "base/compiler_specific.h"
|
|
|
|
|
|
|
|
#include "third_party/WebKit/Source/WebCore/config.h"
|
|
|
|
MSVC_PUSH_WARNING_LEVEL(0);
|
2012-08-29 22:11:26 +02:00
|
|
|
#include "Frame.h" // NOLINT(build/include)
|
|
|
|
#include "ScriptController.h" // NOLINT(build/include)
|
|
|
|
#include "ScriptControllerBase.h" // NOLINT(build/include)
|
|
|
|
#include "V8Binding.h" // NOLINT(build/include)
|
2012-05-23 21:01:04 +02:00
|
|
|
#include "V8RecursionScope.h" // NOLINT(build/include)
|
|
|
|
MSVC_POP_WARNING();
|
|
|
|
#undef LOG
|
|
|
|
|
|
|
|
#include "libcef/v8_impl.h"
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
#include "libcef/browser_impl.h"
|
|
|
|
#include "libcef/cef_context.h"
|
|
|
|
#include "libcef/tracker.h"
|
2012-02-17 15:51:20 +01:00
|
|
|
|
|
|
|
#include "base/bind.h"
|
2010-10-03 23:04:50 +02:00
|
|
|
#include "base/lazy_instance.h"
|
2011-02-15 19:07:24 +01:00
|
|
|
#include "third_party/WebKit/Source/WebKit/chromium/public/WebKit.h"
|
2011-02-21 23:44:06 +01:00
|
|
|
#include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h"
|
2011-02-15 19:07:24 +01:00
|
|
|
#include "third_party/WebKit/Source/WebKit/chromium/public/WebScriptController.h"
|
2010-10-03 23:04:50 +02:00
|
|
|
|
2011-02-21 23:44:06 +01:00
|
|
|
#define CEF_REQUIRE_UI_THREAD(var) \
|
|
|
|
if (!CefThread::CurrentlyOn(CefThread::UI)) { \
|
2011-11-07 22:39:57 +01:00
|
|
|
NOTREACHED() << "called on invalid thread"; \
|
2011-02-21 23:44:06 +01:00
|
|
|
return var; \
|
|
|
|
}
|
|
|
|
|
|
|
|
#define CEF_REQUIRE_VALID_CONTEXT(var) \
|
|
|
|
if (!CONTEXT_STATE_VALID()) { \
|
2011-11-07 22:39:57 +01:00
|
|
|
NOTREACHED() << "context not valid"; \
|
2011-02-21 23:44:06 +01:00
|
|
|
return var; \
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2012-05-23 21:01:04 +02:00
|
|
|
static const char kCefTrackObject[] = "Cef::TrackObject";
|
2012-10-29 22:54:57 +01:00
|
|
|
static const char kCefContextState[] = "Cef::ContextState";
|
2011-11-04 20:34:14 +01:00
|
|
|
|
2010-10-03 23:04:50 +02:00
|
|
|
// Memory manager.
|
|
|
|
|
2012-10-30 21:50:58 +01:00
|
|
|
class CefV8TrackManager {
|
2012-10-29 22:54:57 +01:00
|
|
|
public:
|
|
|
|
CefV8TrackManager() {
|
|
|
|
const CefSettings& settings = _Context->settings();
|
|
|
|
if (settings.context_safety_implementation < 0)
|
|
|
|
context_safety_impl_ = IMPL_DISABLED;
|
|
|
|
else if (settings.context_safety_implementation == 1)
|
|
|
|
context_safety_impl_ = IMPL_VALUE;
|
|
|
|
else
|
|
|
|
context_safety_impl_ = IMPL_HASH;
|
|
|
|
}
|
|
|
|
|
|
|
|
scoped_refptr<CefV8ContextState> GetContextState(
|
|
|
|
v8::Handle<v8::Context> context) {
|
|
|
|
if (context_safety_impl_ == IMPL_DISABLED)
|
|
|
|
return scoped_refptr<CefV8ContextState>();
|
|
|
|
|
|
|
|
if (context.IsEmpty()) {
|
|
|
|
if (v8::Context::InContext())
|
|
|
|
context = v8::Context::GetCurrent();
|
|
|
|
else
|
|
|
|
return scoped_refptr<CefV8ContextState>();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (context_safety_impl_ == IMPL_HASH) {
|
|
|
|
int hash = context->Global()->GetIdentityHash();
|
|
|
|
ContextMap::const_iterator it = context_map_.find(hash);
|
|
|
|
if (it != context_map_.end())
|
|
|
|
return it->second;
|
|
|
|
|
|
|
|
scoped_refptr<CefV8ContextState> state = new CefV8ContextState();
|
|
|
|
context_map_.insert(std::make_pair(hash, state));
|
|
|
|
|
|
|
|
return state;
|
|
|
|
} else {
|
|
|
|
if (context_state_key_.IsEmpty()) {
|
|
|
|
context_state_key_ =
|
|
|
|
v8::Persistent<v8::String>::New(v8::String::New(kCefContextState));
|
|
|
|
}
|
|
|
|
|
|
|
|
v8::Handle<v8::Object> object = context->Global();
|
|
|
|
v8::Handle<v8::Value> value = object->GetHiddenValue(context_state_key_);
|
2012-11-30 19:30:34 +01:00
|
|
|
if (!value.IsEmpty()) {
|
|
|
|
return static_cast<CefV8ContextState*>(
|
|
|
|
v8::External::Cast(*value)->Value());
|
|
|
|
}
|
2012-10-29 22:54:57 +01:00
|
|
|
|
|
|
|
scoped_refptr<CefV8ContextState> state = new CefV8ContextState();
|
|
|
|
object->SetHiddenValue(context_state_key_,
|
|
|
|
v8::External::New(state.get()));
|
|
|
|
|
|
|
|
// Reference will be released in ReleaseContext.
|
|
|
|
state->AddRef();
|
|
|
|
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ReleaseContext(v8::Handle<v8::Context> context) {
|
|
|
|
if (context_safety_impl_ == IMPL_DISABLED)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (context_safety_impl_ == IMPL_HASH) {
|
|
|
|
int hash = context->Global()->GetIdentityHash();
|
|
|
|
ContextMap::iterator it = context_map_.find(hash);
|
|
|
|
if (it != context_map_.end()) {
|
|
|
|
it->second->Detach();
|
|
|
|
context_map_.erase(it);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (context_state_key_.IsEmpty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
v8::Handle<v8::Object> object = context->Global();
|
|
|
|
v8::Handle<v8::Value> value = object->GetHiddenValue(context_state_key_);
|
2012-11-05 23:32:57 +01:00
|
|
|
if (value.IsEmpty())
|
|
|
|
return;
|
|
|
|
|
2012-10-29 22:54:57 +01:00
|
|
|
scoped_refptr<CefV8ContextState> state =
|
2012-11-30 19:30:34 +01:00
|
|
|
static_cast<CefV8ContextState*>(v8::External::Cast(*value)->Value());
|
2012-10-29 22:54:57 +01:00
|
|
|
state->Detach();
|
|
|
|
object->DeleteHiddenValue(context_state_key_);
|
|
|
|
|
|
|
|
// Match the AddRef in GetContextState.
|
|
|
|
state->Release();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-30 21:50:58 +01:00
|
|
|
void AddGlobalTrackObject(CefTrackNode* object) {
|
|
|
|
global_manager_.Add(object);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DeleteGlobalTrackObject(CefTrackNode* object) {
|
|
|
|
global_manager_.Delete(object);
|
|
|
|
}
|
|
|
|
|
2012-10-29 22:54:57 +01:00
|
|
|
private:
|
|
|
|
enum ContextSafetyImpl {
|
|
|
|
IMPL_DISABLED,
|
|
|
|
IMPL_HASH,
|
|
|
|
IMPL_VALUE,
|
|
|
|
};
|
|
|
|
ContextSafetyImpl context_safety_impl_;
|
|
|
|
|
|
|
|
// Used with IMPL_HASH.
|
|
|
|
typedef std::map<int, scoped_refptr<CefV8ContextState> > ContextMap;
|
|
|
|
ContextMap context_map_;
|
|
|
|
|
|
|
|
// Used with IMPL_VALUE.
|
|
|
|
v8::Persistent<v8::String> context_state_key_;
|
2012-10-30 21:50:58 +01:00
|
|
|
|
|
|
|
// Used for globally tracked objects that are not associated with a particular
|
|
|
|
// context.
|
|
|
|
CefTrackManager global_manager_;
|
2012-10-29 22:54:57 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
base::LazyInstance<CefV8TrackManager> g_v8_tracker = LAZY_INSTANCE_INITIALIZER;
|
2010-10-03 23:04:50 +02:00
|
|
|
|
2012-01-19 19:52:59 +01:00
|
|
|
class V8TrackObject : public CefTrackNode {
|
2012-01-10 00:46:23 +01:00
|
|
|
public:
|
2012-05-23 21:01:04 +02:00
|
|
|
V8TrackObject()
|
|
|
|
: external_memory_(0) {
|
2012-10-30 21:50:58 +01:00
|
|
|
v8::V8::AdjustAmountOfExternalAllocatedMemory(
|
|
|
|
static_cast<int>(sizeof(V8TrackObject)));
|
2012-05-23 21:01:04 +02:00
|
|
|
}
|
|
|
|
~V8TrackObject() {
|
2012-10-30 21:50:58 +01:00
|
|
|
v8::V8::AdjustAmountOfExternalAllocatedMemory(
|
|
|
|
-static_cast<int>(sizeof(V8TrackObject)) - external_memory_);
|
2012-05-23 21:01:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
inline int GetExternallyAllocatedMemory() {
|
|
|
|
return external_memory_;
|
|
|
|
}
|
|
|
|
|
|
|
|
int AdjustExternallyAllocatedMemory(int change_in_bytes) {
|
|
|
|
int new_value = external_memory_ + change_in_bytes;
|
|
|
|
if (new_value < 0) {
|
|
|
|
NOTREACHED() << "External memory usage cannot be less than 0 bytes";
|
|
|
|
change_in_bytes = -(external_memory_);
|
|
|
|
new_value = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (change_in_bytes != 0)
|
|
|
|
v8::V8::AdjustAmountOfExternalAllocatedMemory(change_in_bytes);
|
|
|
|
external_memory_ = new_value;
|
|
|
|
|
|
|
|
return new_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void SetAccessor(CefRefPtr<CefV8Accessor> accessor) {
|
|
|
|
accessor_ = accessor;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline CefRefPtr<CefV8Accessor> GetAccessor() {
|
|
|
|
return accessor_;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void SetHandler(CefRefPtr<CefV8Handler> handler) {
|
|
|
|
handler_ = handler;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline CefRefPtr<CefV8Handler> GetHandler() {
|
|
|
|
return handler_;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void SetUserData(CefRefPtr<CefBase> user_data) {
|
|
|
|
user_data_ = user_data;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline CefRefPtr<CefBase> GetUserData() {
|
|
|
|
return user_data_;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Attach this track object to the specified V8 object.
|
|
|
|
void AttachTo(v8::Handle<v8::Object> object) {
|
|
|
|
object->SetHiddenValue(v8::String::New(kCefTrackObject),
|
2012-11-30 19:30:34 +01:00
|
|
|
v8::External::New(this));
|
2012-01-19 19:52:59 +01:00
|
|
|
}
|
2010-10-03 23:04:50 +02:00
|
|
|
|
2012-05-23 21:01:04 +02:00
|
|
|
// Retrieve the track object for the specified V8 object.
|
|
|
|
static V8TrackObject* Unwrap(v8::Handle<v8::Object> object) {
|
|
|
|
v8::Local<v8::Value> value =
|
|
|
|
object->GetHiddenValue(v8::String::New(kCefTrackObject));
|
|
|
|
if (!value.IsEmpty())
|
2012-11-30 19:30:34 +01:00
|
|
|
return static_cast<V8TrackObject*>(v8::External::Cast(*value)->Value());
|
2012-05-23 21:01:04 +02:00
|
|
|
|
|
|
|
return NULL;
|
2011-04-07 03:58:49 +02:00
|
|
|
}
|
|
|
|
|
2012-01-19 19:52:59 +01:00
|
|
|
private:
|
2012-05-23 21:01:04 +02:00
|
|
|
CefRefPtr<CefV8Accessor> accessor_;
|
|
|
|
CefRefPtr<CefV8Handler> handler_;
|
2012-01-19 19:52:59 +01:00
|
|
|
CefRefPtr<CefBase> user_data_;
|
2012-05-23 21:01:04 +02:00
|
|
|
int external_memory_;
|
2011-04-07 03:58:49 +02:00
|
|
|
};
|
|
|
|
|
2012-01-19 19:52:59 +01:00
|
|
|
class V8TrackString : public CefTrackNode {
|
2012-01-10 00:46:23 +01:00
|
|
|
public:
|
2012-01-19 19:52:59 +01:00
|
|
|
explicit V8TrackString(const std::string& str) : string_(str) {}
|
2010-10-03 23:04:50 +02:00
|
|
|
const char* GetString() { return string_.c_str(); }
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
private:
|
2010-10-03 23:04:50 +02:00
|
|
|
std::string string_;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2012-10-30 21:50:58 +01:00
|
|
|
// Manages the life span of a CefTrackNode associated with a persistent Object
|
|
|
|
// or Function.
|
|
|
|
class CefV8MakeWeakParam {
|
|
|
|
public:
|
|
|
|
CefV8MakeWeakParam(scoped_refptr<CefV8ContextState> context_state,
|
|
|
|
CefTrackNode* object)
|
|
|
|
: context_state_(context_state),
|
|
|
|
object_(object) {
|
|
|
|
DCHECK(object_);
|
|
|
|
|
|
|
|
v8::V8::AdjustAmountOfExternalAllocatedMemory(
|
|
|
|
static_cast<int>(sizeof(CefV8MakeWeakParam)));
|
|
|
|
|
|
|
|
if (context_state_.get()) {
|
|
|
|
// |object_| will be deleted when:
|
|
|
|
// A. The associated context is released, or
|
|
|
|
// B. TrackDestructor is called for the weak handle.
|
|
|
|
DCHECK(context_state_->IsValid());
|
|
|
|
context_state_->AddTrackObject(object_);
|
|
|
|
} else {
|
|
|
|
// |object_| will be deleted when:
|
|
|
|
// A. The process shuts down, or
|
|
|
|
// B. TrackDestructor is called for the weak handle.
|
|
|
|
g_v8_tracker.Pointer()->AddGlobalTrackObject(object_);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
~CefV8MakeWeakParam() {
|
|
|
|
if (context_state_.get()) {
|
|
|
|
// If the associated context is still valid then delete |object_|.
|
|
|
|
// Otherwise, |object_| will already have been deleted.
|
|
|
|
if (context_state_->IsValid())
|
|
|
|
context_state_->DeleteTrackObject(object_);
|
|
|
|
} else {
|
|
|
|
g_v8_tracker.Pointer()->DeleteGlobalTrackObject(object_);
|
|
|
|
}
|
|
|
|
|
|
|
|
v8::V8::AdjustAmountOfExternalAllocatedMemory(
|
|
|
|
-static_cast<int>(sizeof(CefV8MakeWeakParam)));
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
scoped_refptr<CefV8ContextState> context_state_;
|
|
|
|
CefTrackNode* object_;
|
|
|
|
};
|
2010-10-03 23:04:50 +02:00
|
|
|
|
|
|
|
// Callback for weak persistent reference destruction.
|
2012-01-10 00:46:23 +01:00
|
|
|
void TrackDestructor(v8::Persistent<v8::Value> object, void* parameter) {
|
2012-05-23 21:01:04 +02:00
|
|
|
if (parameter)
|
2012-10-30 21:50:58 +01:00
|
|
|
delete static_cast<CefV8MakeWeakParam*>(parameter);
|
2012-05-23 21:01:04 +02:00
|
|
|
|
2010-10-03 23:04:50 +02:00
|
|
|
object.Dispose();
|
|
|
|
object.Clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-02-21 23:44:06 +01:00
|
|
|
// Return the browser associated with the specified WebFrame.
|
2012-01-10 00:46:23 +01:00
|
|
|
CefRefPtr<CefBrowserImpl> FindBrowserForFrame(WebKit::WebFrame *frame) {
|
2011-05-20 16:42:25 +02:00
|
|
|
CefContext::AutoLock lock_scope(_Context);
|
2012-01-10 00:46:23 +01:00
|
|
|
|
2011-05-20 16:42:25 +02:00
|
|
|
CefContext::BrowserList *list = _Context->GetBrowserList();
|
2011-02-21 23:44:06 +01:00
|
|
|
CefContext::BrowserList::const_iterator i;
|
|
|
|
i = list->begin();
|
|
|
|
for (; i != list->end(); ++i) {
|
|
|
|
WebKit::WebFrame* thisframe = i->get()->UIT_GetMainWebFrame();
|
2011-05-20 16:42:25 +02:00
|
|
|
if (thisframe == frame)
|
|
|
|
return i->get();
|
2011-02-21 23:44:06 +01:00
|
|
|
}
|
2011-05-20 16:42:25 +02:00
|
|
|
|
|
|
|
return NULL;
|
2011-02-21 23:44:06 +01:00
|
|
|
}
|
|
|
|
|
2011-09-20 22:41:54 +02:00
|
|
|
// Convert a CefString to a V8::String.
|
2012-01-10 00:46:23 +01:00
|
|
|
v8::Handle<v8::String> GetV8String(const CefString& str) {
|
2011-09-20 22:41:54 +02:00
|
|
|
#if defined(CEF_STRING_TYPE_UTF16)
|
|
|
|
// Already a UTF16 string.
|
|
|
|
return v8::String::New(
|
|
|
|
reinterpret_cast<uint16_t*>(
|
|
|
|
const_cast<CefString::char_type*>(str.c_str())),
|
|
|
|
str.length());
|
|
|
|
#elif defined(CEF_STRING_TYPE_UTF8)
|
|
|
|
// Already a UTF8 string.
|
|
|
|
return v8::String::New(const_cast<char*>(str.c_str()), str.length());
|
|
|
|
#else
|
|
|
|
// Convert the string to UTF8.
|
2010-11-22 18:49:46 +01:00
|
|
|
std::string tmpStr = str;
|
2010-11-15 16:39:56 +01:00
|
|
|
return v8::String::New(tmpStr.c_str(), tmpStr.length());
|
2011-09-20 22:41:54 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(CEF_STRING_TYPE_UTF16)
|
2012-01-10 00:46:23 +01:00
|
|
|
void v8impl_string_dtor(char16* str) {
|
2012-07-25 11:45:49 +02:00
|
|
|
delete [] str;
|
2010-10-03 23:04:50 +02:00
|
|
|
}
|
2011-09-20 22:41:54 +02:00
|
|
|
#elif defined(CEF_STRING_TYPE_UTF8)
|
2012-01-10 00:46:23 +01:00
|
|
|
void v8impl_string_dtor(char* str) {
|
2012-07-25 11:45:49 +02:00
|
|
|
delete [] str;
|
2011-09-20 22:41:54 +02:00
|
|
|
}
|
|
|
|
#endif
|
2010-10-03 23:04:50 +02:00
|
|
|
|
2011-09-20 22:41:54 +02:00
|
|
|
// Convert a v8::String to CefString.
|
2012-01-10 00:46:23 +01:00
|
|
|
void GetCefString(v8::Handle<v8::String> str, CefString& out) {
|
2012-07-25 11:45:49 +02:00
|
|
|
if (str.IsEmpty())
|
|
|
|
return;
|
|
|
|
|
2011-09-20 22:41:54 +02:00
|
|
|
#if defined(CEF_STRING_TYPE_WIDE)
|
2010-11-15 16:39:56 +01:00
|
|
|
// Allocate enough space for a worst-case conversion.
|
2010-11-22 18:49:46 +01:00
|
|
|
int len = str->Utf8Length();
|
2012-04-25 22:00:28 +02:00
|
|
|
if (len == 0)
|
|
|
|
return;
|
2011-09-20 22:41:54 +02:00
|
|
|
char* buf = new char[len + 1];
|
|
|
|
str->WriteUtf8(buf, len + 1);
|
2012-01-10 00:46:23 +01:00
|
|
|
|
2011-09-20 22:41:54 +02:00
|
|
|
// Perform conversion to the wide type.
|
|
|
|
cef_string_t* retws = out.GetWritableStruct();
|
|
|
|
cef_string_utf8_to_wide(buf, len, retws);
|
2012-01-10 00:46:23 +01:00
|
|
|
|
2010-10-03 23:04:50 +02:00
|
|
|
delete [] buf;
|
2012-01-10 00:46:23 +01:00
|
|
|
#else // !defined(CEF_STRING_TYPE_WIDE)
|
2011-09-20 22:41:54 +02:00
|
|
|
#if defined(CEF_STRING_TYPE_UTF16)
|
|
|
|
int len = str->Length();
|
2012-04-25 22:00:28 +02:00
|
|
|
if (len == 0)
|
|
|
|
return;
|
2011-09-20 22:41:54 +02:00
|
|
|
char16* buf = new char16[len + 1];
|
|
|
|
str->Write(reinterpret_cast<uint16_t*>(buf), 0, len + 1);
|
|
|
|
#else
|
|
|
|
// Allocate enough space for a worst-case conversion.
|
|
|
|
int len = str->Utf8Length();
|
2012-04-25 22:00:28 +02:00
|
|
|
if (len == 0)
|
|
|
|
return;
|
2011-09-20 22:41:54 +02:00
|
|
|
char* buf = new char[len + 1];
|
|
|
|
str->WriteUtf8(buf, len + 1);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Don't perform an extra string copy.
|
|
|
|
out.clear();
|
|
|
|
cef_string_t* retws = out.GetWritableStruct();
|
|
|
|
retws->str = buf;
|
|
|
|
retws->length = len;
|
|
|
|
retws->dtor = v8impl_string_dtor;
|
2012-01-10 00:46:23 +01:00
|
|
|
#endif // !defined(CEF_STRING_TYPE_WIDE)
|
2010-10-03 23:04:50 +02:00
|
|
|
}
|
|
|
|
|
2011-02-21 23:44:06 +01:00
|
|
|
// V8 function callback.
|
2012-01-10 00:46:23 +01:00
|
|
|
v8::Handle<v8::Value> FunctionCallbackImpl(const v8::Arguments& args) {
|
2010-10-03 23:04:50 +02:00
|
|
|
v8::HandleScope handle_scope;
|
2011-09-20 22:41:54 +02:00
|
|
|
|
2010-10-03 23:04:50 +02:00
|
|
|
CefV8Handler* handler =
|
2012-11-30 19:30:34 +01:00
|
|
|
static_cast<CefV8Handler*>(v8::External::Cast(*args.Data())->Value());
|
2011-09-20 22:41:54 +02:00
|
|
|
|
2010-10-03 23:04:50 +02:00
|
|
|
CefV8ValueList params;
|
2012-01-10 00:46:23 +01:00
|
|
|
for (int i = 0; i < args.Length(); i++)
|
2010-10-03 23:04:50 +02:00
|
|
|
params.push_back(new CefV8ValueImpl(args[i]));
|
|
|
|
|
2011-09-20 22:41:54 +02:00
|
|
|
CefString func_name;
|
|
|
|
GetCefString(v8::Handle<v8::String>::Cast(args.Callee()->GetName()),
|
|
|
|
func_name);
|
2010-10-03 23:04:50 +02:00
|
|
|
CefRefPtr<CefV8Value> object = new CefV8ValueImpl(args.This());
|
|
|
|
CefRefPtr<CefV8Value> retval;
|
2010-11-22 18:49:46 +01:00
|
|
|
CefString exception;
|
2010-10-03 23:04:50 +02:00
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
if (handler->Execute(func_name, object, params, retval, exception)) {
|
|
|
|
if (!exception.empty()) {
|
2011-09-20 22:41:54 +02:00
|
|
|
return v8::ThrowException(v8::Exception::Error(GetV8String(exception)));
|
2012-01-10 00:46:23 +01:00
|
|
|
} else {
|
2010-10-03 23:04:50 +02:00
|
|
|
CefV8ValueImpl* rv = static_cast<CefV8ValueImpl*>(retval.get());
|
2011-09-20 22:41:54 +02:00
|
|
|
if (rv)
|
2012-10-30 21:50:58 +01:00
|
|
|
return rv->GetHandle(true);
|
2010-10-03 23:04:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-20 22:41:54 +02:00
|
|
|
return v8::Undefined();
|
2010-10-03 23:04:50 +02:00
|
|
|
}
|
|
|
|
|
2011-04-07 03:58:49 +02:00
|
|
|
// V8 Accessor callbacks
|
|
|
|
v8::Handle<v8::Value> AccessorGetterCallbackImpl(v8::Local<v8::String> property,
|
2012-01-10 00:46:23 +01:00
|
|
|
const v8::AccessorInfo& info) {
|
2011-04-07 03:58:49 +02:00
|
|
|
v8::HandleScope handle_scope;
|
|
|
|
|
|
|
|
v8::Handle<v8::Object> obj = info.This();
|
|
|
|
|
2012-05-23 21:01:04 +02:00
|
|
|
CefRefPtr<CefV8Accessor> accessorPtr;
|
|
|
|
|
|
|
|
V8TrackObject* tracker = V8TrackObject::Unwrap(obj);
|
|
|
|
if (tracker)
|
|
|
|
accessorPtr = tracker->GetAccessor();
|
|
|
|
|
|
|
|
if (accessorPtr.get()) {
|
2011-04-07 03:58:49 +02:00
|
|
|
CefRefPtr<CefV8Value> retval;
|
|
|
|
CefRefPtr<CefV8Value> object = new CefV8ValueImpl(obj);
|
2011-09-20 22:41:54 +02:00
|
|
|
CefString name, exception;
|
|
|
|
GetCefString(property, name);
|
|
|
|
if (accessorPtr->Get(name, object, retval, exception)) {
|
|
|
|
if (!exception.empty()) {
|
|
|
|
return v8::ThrowException(
|
|
|
|
v8::Exception::Error(GetV8String(exception)));
|
|
|
|
} else {
|
|
|
|
CefV8ValueImpl* rv = static_cast<CefV8ValueImpl*>(retval.get());
|
|
|
|
if (rv)
|
2012-10-30 21:50:58 +01:00
|
|
|
return rv->GetHandle(true);
|
2011-09-20 22:41:54 +02:00
|
|
|
}
|
2011-04-07 03:58:49 +02:00
|
|
|
}
|
|
|
|
}
|
2011-09-20 22:41:54 +02:00
|
|
|
|
|
|
|
return v8::Undefined();
|
2011-04-07 03:58:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void AccessorSetterCallbackImpl(v8::Local<v8::String> property,
|
|
|
|
v8::Local<v8::Value> value,
|
2012-01-10 00:46:23 +01:00
|
|
|
const v8::AccessorInfo& info) {
|
2011-04-07 03:58:49 +02:00
|
|
|
v8::HandleScope handle_scope;
|
|
|
|
|
|
|
|
v8::Handle<v8::Object> obj = info.This();
|
2011-09-20 22:41:54 +02:00
|
|
|
|
2012-05-23 21:01:04 +02:00
|
|
|
CefRefPtr<CefV8Accessor> accessorPtr;
|
|
|
|
|
|
|
|
V8TrackObject* tracker = V8TrackObject::Unwrap(obj);
|
|
|
|
if (tracker)
|
|
|
|
accessorPtr = tracker->GetAccessor();
|
|
|
|
|
|
|
|
if (accessorPtr.get()) {
|
2011-04-07 03:58:49 +02:00
|
|
|
CefRefPtr<CefV8Value> object = new CefV8ValueImpl(obj);
|
|
|
|
CefRefPtr<CefV8Value> cefValue = new CefV8ValueImpl(value);
|
2011-09-20 22:41:54 +02:00
|
|
|
CefString name, exception;
|
|
|
|
GetCefString(property, name);
|
|
|
|
accessorPtr->Set(name, object, cefValue, exception);
|
|
|
|
if (!exception.empty()) {
|
|
|
|
v8::ThrowException(v8::Exception::Error(GetV8String(exception)));
|
|
|
|
return;
|
|
|
|
}
|
2011-04-07 03:58:49 +02:00
|
|
|
}
|
|
|
|
}
|
2010-10-03 23:04:50 +02:00
|
|
|
|
|
|
|
// V8 extension registration.
|
|
|
|
|
|
|
|
class ExtensionWrapper : public v8::Extension {
|
2012-01-10 00:46:23 +01:00
|
|
|
public:
|
2010-10-03 23:04:50 +02:00
|
|
|
ExtensionWrapper(const char* extension_name,
|
|
|
|
const char* javascript_code,
|
|
|
|
CefV8Handler* handler)
|
2012-01-10 00:46:23 +01:00
|
|
|
: v8::Extension(extension_name, javascript_code), handler_(handler) {
|
2011-11-21 20:01:22 +01:00
|
|
|
if (handler) {
|
2012-05-23 21:01:04 +02:00
|
|
|
// The reference will be released when the process exits.
|
|
|
|
V8TrackObject* object = new V8TrackObject;
|
|
|
|
object->SetHandler(handler);
|
2012-10-30 21:50:58 +01:00
|
|
|
g_v8_tracker.Pointer()->AddGlobalTrackObject(object);
|
2011-11-21 20:01:22 +01:00
|
|
|
}
|
2010-10-03 23:04:50 +02:00
|
|
|
}
|
2012-01-10 00:46:23 +01:00
|
|
|
|
2010-10-03 23:04:50 +02:00
|
|
|
virtual v8::Handle<v8::FunctionTemplate> GetNativeFunction(
|
2012-01-10 00:46:23 +01:00
|
|
|
v8::Handle<v8::String> name) {
|
2011-11-21 20:01:22 +01:00
|
|
|
if (!handler_)
|
|
|
|
return v8::Handle<v8::FunctionTemplate>();
|
|
|
|
|
2010-10-03 23:04:50 +02:00
|
|
|
return v8::FunctionTemplate::New(FunctionCallbackImpl,
|
2012-11-30 19:30:34 +01:00
|
|
|
v8::External::New(handler_));
|
2010-10-03 23:04:50 +02:00
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
void UIT_RegisterExtension() {
|
2010-10-03 23:04:50 +02:00
|
|
|
WebKit::WebScriptController::registerExtension(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AddRef() {}
|
|
|
|
void Release() {}
|
|
|
|
|
|
|
|
static bool ImplementsThreadSafeReferenceCounting() { return true; }
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
private:
|
2010-10-03 23:04:50 +02:00
|
|
|
CefV8Handler* handler_;
|
|
|
|
};
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
class CefV8ExceptionImpl : public CefV8Exception {
|
|
|
|
public:
|
2011-11-10 23:57:23 +01:00
|
|
|
explicit CefV8ExceptionImpl(v8::Handle<v8::Message> message)
|
|
|
|
: line_number_(0),
|
|
|
|
start_position_(0),
|
|
|
|
end_position_(0),
|
|
|
|
start_column_(0),
|
2012-01-10 00:46:23 +01:00
|
|
|
end_column_(0) {
|
2011-11-10 23:57:23 +01:00
|
|
|
if (message.IsEmpty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
GetCefString(message->Get(), message_);
|
|
|
|
GetCefString(message->GetSourceLine(), source_line_);
|
|
|
|
|
|
|
|
if (!message->GetScriptResourceName().IsEmpty())
|
|
|
|
GetCefString(message->GetScriptResourceName()->ToString(), script_);
|
|
|
|
|
|
|
|
line_number_ = message->GetLineNumber();
|
|
|
|
start_position_ = message->GetStartPosition();
|
|
|
|
end_position_ = message->GetEndPosition();
|
|
|
|
start_column_ = message->GetStartColumn();
|
|
|
|
end_column_ = message->GetEndColumn();
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual CefString GetMessage() OVERRIDE { return message_; }
|
|
|
|
virtual CefString GetSourceLine() OVERRIDE { return source_line_; }
|
|
|
|
virtual CefString GetScriptResourceName() OVERRIDE { return script_; }
|
|
|
|
virtual int GetLineNumber() OVERRIDE { return line_number_; }
|
|
|
|
virtual int GetStartPosition() OVERRIDE { return start_position_; }
|
|
|
|
virtual int GetEndPosition() OVERRIDE { return end_position_; }
|
|
|
|
virtual int GetStartColumn() OVERRIDE { return start_column_; }
|
|
|
|
virtual int GetEndColumn() OVERRIDE { return end_column_; }
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
protected:
|
2011-11-10 23:57:23 +01:00
|
|
|
CefString message_;
|
|
|
|
CefString source_line_;
|
|
|
|
CefString script_;
|
|
|
|
int line_number_;
|
|
|
|
int start_position_;
|
|
|
|
int end_position_;
|
|
|
|
int start_column_;
|
|
|
|
int end_column_;
|
|
|
|
|
|
|
|
IMPLEMENT_REFCOUNTING(CefV8ExceptionImpl);
|
|
|
|
};
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
} // namespace
|
2011-02-21 23:44:06 +01:00
|
|
|
|
2012-05-23 21:01:04 +02:00
|
|
|
|
|
|
|
// Global functions.
|
|
|
|
|
2012-10-29 22:54:57 +01:00
|
|
|
void CefV8ReleaseContext(v8::Handle<v8::Context> context) {
|
|
|
|
g_v8_tracker.Pointer()->ReleaseContext(context);
|
|
|
|
}
|
|
|
|
|
2010-11-22 18:49:46 +01:00
|
|
|
bool CefRegisterExtension(const CefString& extension_name,
|
|
|
|
const CefString& javascript_code,
|
2012-01-10 00:46:23 +01:00
|
|
|
CefRefPtr<CefV8Handler> handler) {
|
2011-01-07 02:06:10 +01:00
|
|
|
// Verify that the context is in a valid state.
|
2011-02-21 23:44:06 +01:00
|
|
|
CEF_REQUIRE_VALID_CONTEXT(false);
|
2011-01-07 02:06:10 +01:00
|
|
|
|
2012-01-19 19:52:59 +01:00
|
|
|
V8TrackString* name = new V8TrackString(extension_name);
|
2012-10-30 21:50:58 +01:00
|
|
|
g_v8_tracker.Pointer()->AddGlobalTrackObject(name);
|
2012-01-19 19:52:59 +01:00
|
|
|
V8TrackString* code = new V8TrackString(javascript_code);
|
2012-10-30 21:50:58 +01:00
|
|
|
g_v8_tracker.Pointer()->AddGlobalTrackObject(code);
|
2012-01-10 00:46:23 +01:00
|
|
|
|
2010-10-03 23:04:50 +02:00
|
|
|
ExtensionWrapper* wrapper = new ExtensionWrapper(name->GetString(),
|
|
|
|
code->GetString(), handler.get());
|
2012-01-10 00:46:23 +01:00
|
|
|
|
2012-02-17 15:51:20 +01:00
|
|
|
CefThread::PostTask(CefThread::UI, FROM_HERE,
|
|
|
|
base::Bind(&ExtensionWrapper::UIT_RegisterExtension, wrapper));
|
2010-10-03 23:04:50 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-10-29 22:54:57 +01:00
|
|
|
// CefV8HandleBase
|
|
|
|
|
|
|
|
CefV8HandleBase::CefV8HandleBase(v8::Handle<v8::Context> context) {
|
|
|
|
context_state_ = g_v8_tracker.Pointer()->GetContextState(context);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-25 11:45:49 +02:00
|
|
|
// CefV8Context
|
2011-02-21 23:44:06 +01:00
|
|
|
|
|
|
|
// static
|
2012-01-10 00:46:23 +01:00
|
|
|
CefRefPtr<CefV8Context> CefV8Context::GetCurrentContext() {
|
2011-02-21 23:44:06 +01:00
|
|
|
CefRefPtr<CefV8Context> context;
|
|
|
|
CEF_REQUIRE_VALID_CONTEXT(context);
|
|
|
|
CEF_REQUIRE_UI_THREAD(context);
|
2011-11-21 20:01:22 +01:00
|
|
|
if (v8::Context::InContext()) {
|
|
|
|
v8::HandleScope handle_scope;
|
|
|
|
context = new CefV8ContextImpl(v8::Context::GetCurrent());
|
|
|
|
}
|
2011-02-21 23:44:06 +01:00
|
|
|
return context;
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
2012-01-10 00:46:23 +01:00
|
|
|
CefRefPtr<CefV8Context> CefV8Context::GetEnteredContext() {
|
2011-02-21 23:44:06 +01:00
|
|
|
CefRefPtr<CefV8Context> context;
|
|
|
|
CEF_REQUIRE_VALID_CONTEXT(context);
|
|
|
|
CEF_REQUIRE_UI_THREAD(context);
|
2011-11-21 20:01:22 +01:00
|
|
|
if (v8::Context::InContext()) {
|
|
|
|
v8::HandleScope handle_scope;
|
|
|
|
context = new CefV8ContextImpl(v8::Context::GetEntered());
|
|
|
|
}
|
2011-02-21 23:44:06 +01:00
|
|
|
return context;
|
|
|
|
}
|
|
|
|
|
2011-11-21 20:01:22 +01:00
|
|
|
// static
|
2012-01-10 00:46:23 +01:00
|
|
|
bool CefV8Context::InContext() {
|
2011-11-21 20:01:22 +01:00
|
|
|
CEF_REQUIRE_VALID_CONTEXT(false);
|
|
|
|
CEF_REQUIRE_UI_THREAD(false);
|
|
|
|
return v8::Context::InContext();
|
|
|
|
}
|
|
|
|
|
2011-02-21 23:44:06 +01:00
|
|
|
|
2012-07-25 11:45:49 +02:00
|
|
|
// CefV8ContextImpl
|
2012-05-23 21:01:04 +02:00
|
|
|
|
2012-10-29 22:54:57 +01:00
|
|
|
#define CEF_V8_REQUIRE_VALID_RETURN(ret) \
|
|
|
|
if (!handle_->IsValid()) { \
|
|
|
|
NOTREACHED() << "V8 handle is not valid"; \
|
|
|
|
return ret; \
|
|
|
|
}
|
|
|
|
|
2012-05-23 21:01:04 +02:00
|
|
|
#define CEF_V8_REQUIRE_OBJECT_RETURN(ret) \
|
2012-10-29 22:54:57 +01:00
|
|
|
CEF_V8_REQUIRE_VALID_RETURN(ret); \
|
2012-10-30 21:50:58 +01:00
|
|
|
if (!GetHandle(false)->IsObject()) { \
|
2012-05-23 21:01:04 +02:00
|
|
|
NOTREACHED() << "V8 value is not an object"; \
|
|
|
|
return ret; \
|
|
|
|
}
|
|
|
|
|
|
|
|
#define CEF_V8_REQUIRE_ARRAY_RETURN(ret) \
|
2012-10-29 22:54:57 +01:00
|
|
|
CEF_V8_REQUIRE_VALID_RETURN(ret); \
|
2012-10-30 21:50:58 +01:00
|
|
|
if (!GetHandle(false)->IsArray()) { \
|
2012-05-23 21:01:04 +02:00
|
|
|
NOTREACHED() << "V8 value is not an array"; \
|
|
|
|
return ret; \
|
|
|
|
}
|
|
|
|
|
|
|
|
#define CEF_V8_REQUIRE_FUNCTION_RETURN(ret) \
|
2012-10-29 22:54:57 +01:00
|
|
|
CEF_V8_REQUIRE_VALID_RETURN(ret); \
|
2012-10-30 21:50:58 +01:00
|
|
|
if (!GetHandle(false)->IsFunction()) { \
|
2012-05-23 21:01:04 +02:00
|
|
|
NOTREACHED() << "V8 value is not a function"; \
|
|
|
|
return ret; \
|
|
|
|
}
|
2011-02-21 23:44:06 +01:00
|
|
|
|
|
|
|
CefV8ContextImpl::CefV8ContextImpl(v8::Handle<v8::Context> context)
|
2012-10-29 22:54:57 +01:00
|
|
|
: handle_(new Handle(context, context))
|
2011-05-20 16:42:25 +02:00
|
|
|
#ifndef NDEBUG
|
2012-07-25 11:45:49 +02:00
|
|
|
, enter_count_(0)
|
2011-04-07 03:58:49 +02:00
|
|
|
#endif
|
2012-01-10 00:46:23 +01:00
|
|
|
{ // NOLINT(whitespace/braces)
|
2011-02-21 23:44:06 +01:00
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
CefV8ContextImpl::~CefV8ContextImpl() {
|
2011-04-07 03:58:49 +02:00
|
|
|
DLOG_ASSERT(0 == enter_count_);
|
2011-02-21 23:44:06 +01:00
|
|
|
}
|
|
|
|
|
2012-10-29 22:54:57 +01:00
|
|
|
bool CefV8ContextImpl::IsValid() {
|
|
|
|
CEF_REQUIRE_UI_THREAD(false);
|
|
|
|
return handle_->IsValid();
|
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
CefRefPtr<CefBrowser> CefV8ContextImpl::GetBrowser() {
|
2011-02-21 23:44:06 +01:00
|
|
|
CefRefPtr<CefBrowser> browser;
|
|
|
|
CEF_REQUIRE_UI_THREAD(browser);
|
2012-10-29 22:54:57 +01:00
|
|
|
CEF_V8_REQUIRE_VALID_RETURN(browser);
|
2011-02-21 23:44:06 +01:00
|
|
|
|
|
|
|
WebKit::WebFrame* webframe = GetWebFrame();
|
|
|
|
if (webframe)
|
|
|
|
browser = FindBrowserForFrame(webframe->top());
|
|
|
|
|
|
|
|
return browser;
|
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
CefRefPtr<CefFrame> CefV8ContextImpl::GetFrame() {
|
2011-02-21 23:44:06 +01:00
|
|
|
CefRefPtr<CefFrame> frame;
|
|
|
|
CEF_REQUIRE_UI_THREAD(frame);
|
2012-10-29 22:54:57 +01:00
|
|
|
CEF_V8_REQUIRE_VALID_RETURN(frame);
|
2011-02-21 23:44:06 +01:00
|
|
|
|
|
|
|
WebKit::WebFrame* webframe = GetWebFrame();
|
|
|
|
if (webframe) {
|
|
|
|
CefRefPtr<CefBrowserImpl> browser;
|
|
|
|
browser = FindBrowserForFrame(webframe->top());
|
|
|
|
if (browser.get())
|
|
|
|
frame = browser->UIT_GetCefFrame(webframe);
|
|
|
|
}
|
|
|
|
|
|
|
|
return frame;
|
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
CefRefPtr<CefV8Value> CefV8ContextImpl::GetGlobal() {
|
2011-02-21 23:44:06 +01:00
|
|
|
CEF_REQUIRE_UI_THREAD(NULL);
|
2012-10-29 22:54:57 +01:00
|
|
|
CEF_V8_REQUIRE_VALID_RETURN(NULL);
|
2011-02-21 23:44:06 +01:00
|
|
|
|
|
|
|
v8::HandleScope handle_scope;
|
2012-07-25 11:45:49 +02:00
|
|
|
v8::Context::Scope context_scope(GetHandle());
|
|
|
|
return new CefV8ValueImpl(GetHandle()->Global());
|
2011-02-21 23:44:06 +01:00
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
bool CefV8ContextImpl::Enter() {
|
2011-04-07 03:58:49 +02:00
|
|
|
CEF_REQUIRE_UI_THREAD(false);
|
2012-10-29 22:54:57 +01:00
|
|
|
CEF_V8_REQUIRE_VALID_RETURN(false);
|
|
|
|
|
2012-07-25 11:45:49 +02:00
|
|
|
GetHandle()->Enter();
|
2011-05-20 16:42:25 +02:00
|
|
|
#ifndef NDEBUG
|
2011-04-07 03:58:49 +02:00
|
|
|
++enter_count_;
|
|
|
|
#endif
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
bool CefV8ContextImpl::Exit() {
|
2011-04-07 03:58:49 +02:00
|
|
|
CEF_REQUIRE_UI_THREAD(false);
|
2012-10-29 22:54:57 +01:00
|
|
|
CEF_V8_REQUIRE_VALID_RETURN(false);
|
|
|
|
|
2011-04-07 03:58:49 +02:00
|
|
|
DLOG_ASSERT(enter_count_ > 0);
|
2012-07-25 11:45:49 +02:00
|
|
|
GetHandle()->Exit();
|
2011-05-20 16:42:25 +02:00
|
|
|
#ifndef NDEBUG
|
2011-04-07 03:58:49 +02:00
|
|
|
--enter_count_;
|
|
|
|
#endif
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
bool CefV8ContextImpl::IsSame(CefRefPtr<CefV8Context> that) {
|
2011-11-21 22:21:55 +01:00
|
|
|
CEF_REQUIRE_UI_THREAD(false);
|
2012-10-29 22:54:57 +01:00
|
|
|
CEF_V8_REQUIRE_VALID_RETURN(false);
|
2011-11-21 22:21:55 +01:00
|
|
|
|
|
|
|
v8::HandleScope handle_scope;
|
|
|
|
|
|
|
|
v8::Local<v8::Context> thatHandle;
|
|
|
|
v8::Local<v8::Context> thisHandle = GetContext();
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
CefV8ContextImpl* impl = static_cast<CefV8ContextImpl*>(that.get());
|
2011-11-21 22:21:55 +01:00
|
|
|
if (impl)
|
|
|
|
thatHandle = impl->GetContext();
|
|
|
|
|
|
|
|
return (thisHandle == thatHandle);
|
|
|
|
}
|
|
|
|
|
2012-05-23 21:01:04 +02:00
|
|
|
bool CefV8ContextImpl::Eval(const CefString& code,
|
|
|
|
CefRefPtr<CefV8Value>& retval,
|
|
|
|
CefRefPtr<CefV8Exception>& exception) {
|
2012-08-07 15:58:35 +02:00
|
|
|
CEF_REQUIRE_UI_THREAD(false);
|
2012-10-29 22:54:57 +01:00
|
|
|
CEF_V8_REQUIRE_VALID_RETURN(false);
|
2012-05-23 21:01:04 +02:00
|
|
|
|
|
|
|
if (code.empty()) {
|
|
|
|
NOTREACHED() << "invalid input parameter";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
v8::HandleScope handle_scope;
|
2012-07-25 11:45:49 +02:00
|
|
|
v8::Context::Scope context_scope(GetHandle());
|
|
|
|
v8::Local<v8::Object> obj = GetHandle()->Global();
|
2012-05-23 21:01:04 +02:00
|
|
|
|
|
|
|
// Retrieve the eval function.
|
|
|
|
v8::Local<v8::Value> val = obj->Get(v8::String::New("eval"));
|
|
|
|
if (val.IsEmpty() || !val->IsFunction())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
v8::Local<v8::Function> func = v8::Local<v8::Function>::Cast(val);
|
|
|
|
v8::Handle<v8::Value> code_val = GetV8String(code);
|
|
|
|
|
|
|
|
v8::TryCatch try_catch;
|
|
|
|
try_catch.SetVerbose(true);
|
|
|
|
v8::Local<v8::Value> func_rv;
|
|
|
|
|
|
|
|
retval = NULL;
|
|
|
|
exception = NULL;
|
|
|
|
|
2012-08-29 22:11:26 +02:00
|
|
|
// Execute the function call using the ScriptController so that inspector
|
2012-05-23 21:01:04 +02:00
|
|
|
// instrumentation works.
|
2012-08-29 22:11:26 +02:00
|
|
|
RefPtr<WebCore::Frame> frame = WebCore::toFrameIfNotDetached(GetHandle());
|
|
|
|
DCHECK(frame);
|
|
|
|
if (frame &&
|
|
|
|
frame->script()->canExecuteScripts(WebCore::AboutToExecuteScript)) {
|
|
|
|
func_rv = frame->script()->callFunction(func, obj, 1, &code_val);
|
|
|
|
}
|
2012-05-23 21:01:04 +02:00
|
|
|
|
|
|
|
if (try_catch.HasCaught()) {
|
|
|
|
exception = new CefV8ExceptionImpl(try_catch.Message());
|
|
|
|
return false;
|
|
|
|
} else if (!func_rv.IsEmpty()) {
|
|
|
|
retval = new CefV8ValueImpl(func_rv);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
v8::Local<v8::Context> CefV8ContextImpl::GetContext() {
|
2012-07-25 11:45:49 +02:00
|
|
|
return v8::Local<v8::Context>::New(GetHandle());
|
2011-02-21 23:44:06 +01:00
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
WebKit::WebFrame* CefV8ContextImpl::GetWebFrame() {
|
2011-02-21 23:44:06 +01:00
|
|
|
v8::HandleScope handle_scope;
|
2012-07-25 11:45:49 +02:00
|
|
|
v8::Context::Scope context_scope(GetHandle());
|
2011-02-21 23:44:06 +01:00
|
|
|
WebKit::WebFrame* frame = WebKit::WebFrame::frameForCurrentContext();
|
|
|
|
return frame;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-25 11:45:49 +02:00
|
|
|
// CefV8ValueImpl::Handle
|
2011-02-21 23:44:06 +01:00
|
|
|
|
2012-07-25 11:45:49 +02:00
|
|
|
CefV8ValueImpl::Handle::~Handle() {
|
2012-10-30 21:50:58 +01:00
|
|
|
// Persist the |tracker_| object (call MakeWeak) if:
|
|
|
|
// A. The value represents an Object or Function, and
|
|
|
|
// B. The handle has been passed into a V8 function or used as a return value
|
|
|
|
// from a V8 callback, and
|
|
|
|
// C. The associated context, if any, is still valid.
|
|
|
|
if (tracker_ && tracker_should_persist_
|
|
|
|
&& (!context_state_.get() || context_state_->IsValid())) {
|
|
|
|
handle_.MakeWeak(new CefV8MakeWeakParam(context_state_, tracker_),
|
|
|
|
TrackDestructor);
|
2011-09-20 22:41:54 +02:00
|
|
|
} else {
|
2012-07-25 11:45:49 +02:00
|
|
|
handle_.Dispose();
|
|
|
|
handle_.Clear();
|
2012-10-30 21:50:58 +01:00
|
|
|
|
|
|
|
if (tracker_)
|
|
|
|
delete tracker_;
|
2011-09-20 22:41:54 +02:00
|
|
|
}
|
2011-02-21 23:44:06 +01:00
|
|
|
tracker_ = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-10-03 23:04:50 +02:00
|
|
|
// CefV8Value
|
|
|
|
|
|
|
|
// static
|
2012-01-10 00:46:23 +01:00
|
|
|
CefRefPtr<CefV8Value> CefV8Value::CreateUndefined() {
|
2011-02-21 23:44:06 +01:00
|
|
|
CEF_REQUIRE_VALID_CONTEXT(NULL);
|
|
|
|
CEF_REQUIRE_UI_THREAD(NULL);
|
2010-10-03 23:04:50 +02:00
|
|
|
v8::HandleScope handle_scope;
|
|
|
|
return new CefV8ValueImpl(v8::Undefined());
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
2012-01-10 00:46:23 +01:00
|
|
|
CefRefPtr<CefV8Value> CefV8Value::CreateNull() {
|
2011-02-21 23:44:06 +01:00
|
|
|
CEF_REQUIRE_VALID_CONTEXT(NULL);
|
|
|
|
CEF_REQUIRE_UI_THREAD(NULL);
|
2010-10-03 23:04:50 +02:00
|
|
|
v8::HandleScope handle_scope;
|
|
|
|
return new CefV8ValueImpl(v8::Null());
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
2012-01-10 00:46:23 +01:00
|
|
|
CefRefPtr<CefV8Value> CefV8Value::CreateBool(bool value) {
|
2011-02-21 23:44:06 +01:00
|
|
|
CEF_REQUIRE_VALID_CONTEXT(NULL);
|
|
|
|
CEF_REQUIRE_UI_THREAD(NULL);
|
2010-10-03 23:04:50 +02:00
|
|
|
v8::HandleScope handle_scope;
|
|
|
|
return new CefV8ValueImpl(v8::Boolean::New(value));
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
2012-05-23 21:01:04 +02:00
|
|
|
CefRefPtr<CefV8Value> CefV8Value::CreateInt(int32 value) {
|
2011-02-21 23:44:06 +01:00
|
|
|
CEF_REQUIRE_VALID_CONTEXT(NULL);
|
|
|
|
CEF_REQUIRE_UI_THREAD(NULL);
|
2010-10-03 23:04:50 +02:00
|
|
|
v8::HandleScope handle_scope;
|
|
|
|
return new CefV8ValueImpl(v8::Int32::New(value));
|
|
|
|
}
|
|
|
|
|
2012-05-23 21:01:04 +02:00
|
|
|
// static
|
|
|
|
CefRefPtr<CefV8Value> CefV8Value::CreateUInt(uint32 value) {
|
|
|
|
CEF_REQUIRE_VALID_CONTEXT(NULL);
|
|
|
|
CEF_REQUIRE_UI_THREAD(NULL);
|
|
|
|
v8::HandleScope handle_scope;
|
|
|
|
return new CefV8ValueImpl(v8::Int32::NewFromUnsigned(value));
|
|
|
|
}
|
|
|
|
|
2010-10-03 23:04:50 +02:00
|
|
|
// static
|
2012-01-10 00:46:23 +01:00
|
|
|
CefRefPtr<CefV8Value> CefV8Value::CreateDouble(double value) {
|
2011-02-21 23:44:06 +01:00
|
|
|
CEF_REQUIRE_VALID_CONTEXT(NULL);
|
|
|
|
CEF_REQUIRE_UI_THREAD(NULL);
|
2010-10-03 23:04:50 +02:00
|
|
|
v8::HandleScope handle_scope;
|
|
|
|
return new CefV8ValueImpl(v8::Number::New(value));
|
|
|
|
}
|
|
|
|
|
2011-05-23 19:43:53 +02:00
|
|
|
// static
|
2012-01-10 00:46:23 +01:00
|
|
|
CefRefPtr<CefV8Value> CefV8Value::CreateDate(const CefTime& date) {
|
2011-05-23 19:43:53 +02:00
|
|
|
CEF_REQUIRE_VALID_CONTEXT(NULL);
|
|
|
|
CEF_REQUIRE_UI_THREAD(NULL);
|
|
|
|
v8::HandleScope handle_scope;
|
|
|
|
// Convert from seconds to milliseconds.
|
|
|
|
return new CefV8ValueImpl(v8::Date::New(date.GetDoubleT() * 1000));
|
|
|
|
}
|
|
|
|
|
2010-10-03 23:04:50 +02:00
|
|
|
// static
|
2012-01-10 00:46:23 +01:00
|
|
|
CefRefPtr<CefV8Value> CefV8Value::CreateString(const CefString& value) {
|
2011-02-21 23:44:06 +01:00
|
|
|
CEF_REQUIRE_VALID_CONTEXT(NULL);
|
|
|
|
CEF_REQUIRE_UI_THREAD(NULL);
|
2010-10-03 23:04:50 +02:00
|
|
|
v8::HandleScope handle_scope;
|
|
|
|
return new CefV8ValueImpl(GetV8String(value));
|
|
|
|
}
|
|
|
|
|
2011-04-07 03:58:49 +02:00
|
|
|
// static
|
|
|
|
CefRefPtr<CefV8Value> CefV8Value::CreateObject(
|
2012-05-23 21:01:04 +02:00
|
|
|
CefRefPtr<CefV8Accessor> accessor) {
|
2011-02-21 23:44:06 +01:00
|
|
|
CEF_REQUIRE_VALID_CONTEXT(NULL);
|
|
|
|
CEF_REQUIRE_UI_THREAD(NULL);
|
|
|
|
|
2010-10-03 23:04:50 +02:00
|
|
|
v8::HandleScope handle_scope;
|
|
|
|
|
2011-11-21 20:01:22 +01:00
|
|
|
v8::Local<v8::Context> context = v8::Context::GetCurrent();
|
|
|
|
if (context.IsEmpty()) {
|
|
|
|
NOTREACHED() << "not currently in a V8 context";
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2010-10-03 23:04:50 +02:00
|
|
|
// Create the new V8 object.
|
|
|
|
v8::Local<v8::Object> obj = v8::Object::New();
|
|
|
|
|
2012-05-23 21:01:04 +02:00
|
|
|
// Create a tracker object that will cause the user data and/or accessor
|
2011-04-07 03:58:49 +02:00
|
|
|
// reference to be released when the V8 object is destroyed.
|
2012-05-23 21:01:04 +02:00
|
|
|
V8TrackObject* tracker = new V8TrackObject;
|
|
|
|
tracker->SetAccessor(accessor);
|
2012-01-19 19:52:59 +01:00
|
|
|
|
2012-05-23 21:01:04 +02:00
|
|
|
// Attach the tracker object.
|
|
|
|
tracker->AttachTo(obj);
|
2010-10-03 23:04:50 +02:00
|
|
|
|
2011-02-21 23:44:06 +01:00
|
|
|
return new CefV8ValueImpl(obj, tracker);
|
2010-10-03 23:04:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
2012-05-23 21:01:04 +02:00
|
|
|
CefRefPtr<CefV8Value> CefV8Value::CreateArray(int length) {
|
2011-02-21 23:44:06 +01:00
|
|
|
CEF_REQUIRE_VALID_CONTEXT(NULL);
|
|
|
|
CEF_REQUIRE_UI_THREAD(NULL);
|
|
|
|
|
2010-10-03 23:04:50 +02:00
|
|
|
v8::HandleScope handle_scope;
|
2011-11-21 20:01:22 +01:00
|
|
|
|
|
|
|
v8::Local<v8::Context> context = v8::Context::GetCurrent();
|
|
|
|
if (context.IsEmpty()) {
|
|
|
|
NOTREACHED() << "not currently in a V8 context";
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-05-23 21:01:04 +02:00
|
|
|
// Create a tracker object that will cause the user data reference to be
|
|
|
|
// released when the V8 object is destroyed.
|
|
|
|
V8TrackObject* tracker = new V8TrackObject;
|
|
|
|
|
|
|
|
// Create the new V8 array.
|
|
|
|
v8::Local<v8::Array> arr = v8::Array::New(length);
|
|
|
|
|
|
|
|
// Attach the tracker object.
|
|
|
|
tracker->AttachTo(arr);
|
|
|
|
|
|
|
|
return new CefV8ValueImpl(arr, tracker);
|
2010-10-03 23:04:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
2012-01-10 00:46:23 +01:00
|
|
|
CefRefPtr<CefV8Value> CefV8Value::CreateFunction(
|
2012-05-23 21:01:04 +02:00
|
|
|
const CefString& name,
|
|
|
|
CefRefPtr<CefV8Handler> handler) {
|
2011-02-21 23:44:06 +01:00
|
|
|
CEF_REQUIRE_VALID_CONTEXT(NULL);
|
|
|
|
CEF_REQUIRE_UI_THREAD(NULL);
|
|
|
|
|
2011-11-07 22:39:57 +01:00
|
|
|
if (!handler.get()) {
|
|
|
|
NOTREACHED() << "invalid parameter";
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2010-10-03 23:04:50 +02:00
|
|
|
v8::HandleScope handle_scope;
|
2011-11-21 20:01:22 +01:00
|
|
|
|
|
|
|
v8::Local<v8::Context> context = v8::Context::GetCurrent();
|
|
|
|
if (context.IsEmpty()) {
|
|
|
|
NOTREACHED() << "not currently in a V8 context";
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2012-05-23 21:01:04 +02:00
|
|
|
// Create a new V8 function template.
|
2010-10-03 23:04:50 +02:00
|
|
|
v8::Local<v8::FunctionTemplate> tmpl = v8::FunctionTemplate::New();
|
2011-11-07 22:39:57 +01:00
|
|
|
|
2012-11-30 19:30:34 +01:00
|
|
|
v8::Local<v8::Value> data = v8::External::New(handler.get());
|
2010-10-03 23:04:50 +02:00
|
|
|
|
|
|
|
// Set the function handler callback.
|
|
|
|
tmpl->SetCallHandler(FunctionCallbackImpl, data);
|
|
|
|
|
|
|
|
// Retrieve the function object and set the name.
|
|
|
|
v8::Local<v8::Function> func = tmpl->GetFunction();
|
2011-11-18 18:43:12 +01:00
|
|
|
if (func.IsEmpty()) {
|
2011-11-21 20:01:22 +01:00
|
|
|
NOTREACHED() << "failed to create V8 function";
|
2011-11-18 18:43:12 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2010-10-03 23:04:50 +02:00
|
|
|
func->SetName(GetV8String(name));
|
|
|
|
|
2012-05-23 21:01:04 +02:00
|
|
|
// Create a tracker object that will cause the user data and/or handler
|
|
|
|
// reference to be released when the V8 object is destroyed.
|
|
|
|
V8TrackObject* tracker = new V8TrackObject;
|
|
|
|
tracker->SetHandler(handler);
|
2012-01-19 19:52:59 +01:00
|
|
|
|
2012-05-23 21:01:04 +02:00
|
|
|
// Attach the tracker object.
|
|
|
|
tracker->AttachTo(func);
|
2010-10-03 23:04:50 +02:00
|
|
|
|
2011-02-21 23:44:06 +01:00
|
|
|
// Create the CefV8ValueImpl and provide a tracker object that will cause
|
2010-10-03 23:04:50 +02:00
|
|
|
// the handler reference to be released when the V8 object is destroyed.
|
2012-01-19 19:52:59 +01:00
|
|
|
return new CefV8ValueImpl(func, tracker);
|
2010-10-03 23:04:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// CefV8ValueImpl
|
|
|
|
|
|
|
|
CefV8ValueImpl::CefV8ValueImpl(v8::Handle<v8::Value> value,
|
2012-05-23 21:01:04 +02:00
|
|
|
CefTrackNode* tracker)
|
2012-10-29 22:54:57 +01:00
|
|
|
: handle_(new Handle(v8::Handle<v8::Context>(), value, tracker)),
|
2012-07-25 11:45:49 +02:00
|
|
|
rethrow_exceptions_(false) {
|
2010-10-03 23:04:50 +02:00
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
CefV8ValueImpl::~CefV8ValueImpl() {
|
2010-10-03 23:04:50 +02:00
|
|
|
}
|
|
|
|
|
2012-10-29 22:54:57 +01:00
|
|
|
|
|
|
|
bool CefV8ValueImpl::IsValid() {
|
|
|
|
CEF_REQUIRE_UI_THREAD(false);
|
|
|
|
return handle_->IsValid();
|
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
bool CefV8ValueImpl::IsUndefined() {
|
2011-02-21 23:44:06 +01:00
|
|
|
CEF_REQUIRE_UI_THREAD(false);
|
2012-10-29 22:54:57 +01:00
|
|
|
CEF_V8_REQUIRE_VALID_RETURN(false);
|
2012-10-30 21:50:58 +01:00
|
|
|
return GetHandle(false)->IsUndefined();
|
2010-10-03 23:04:50 +02:00
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
bool CefV8ValueImpl::IsNull() {
|
2011-02-21 23:44:06 +01:00
|
|
|
CEF_REQUIRE_UI_THREAD(false);
|
2012-10-29 22:54:57 +01:00
|
|
|
CEF_V8_REQUIRE_VALID_RETURN(false);
|
2012-10-30 21:50:58 +01:00
|
|
|
return GetHandle(false)->IsNull();
|
2010-10-03 23:04:50 +02:00
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
bool CefV8ValueImpl::IsBool() {
|
2011-02-21 23:44:06 +01:00
|
|
|
CEF_REQUIRE_UI_THREAD(false);
|
2012-10-29 22:54:57 +01:00
|
|
|
CEF_V8_REQUIRE_VALID_RETURN(false);
|
2012-10-30 21:50:58 +01:00
|
|
|
return (GetHandle(false)->IsBoolean() || GetHandle(false)->IsTrue() ||
|
|
|
|
GetHandle(false)->IsFalse());
|
2010-10-03 23:04:50 +02:00
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
bool CefV8ValueImpl::IsInt() {
|
2011-02-21 23:44:06 +01:00
|
|
|
CEF_REQUIRE_UI_THREAD(false);
|
2012-10-29 22:54:57 +01:00
|
|
|
CEF_V8_REQUIRE_VALID_RETURN(false);
|
2012-10-30 21:50:58 +01:00
|
|
|
return GetHandle(false)->IsInt32();
|
2010-10-03 23:04:50 +02:00
|
|
|
}
|
|
|
|
|
2012-05-23 21:01:04 +02:00
|
|
|
bool CefV8ValueImpl::IsUInt() {
|
|
|
|
CEF_REQUIRE_UI_THREAD(false);
|
2012-10-29 22:54:57 +01:00
|
|
|
CEF_V8_REQUIRE_VALID_RETURN(false);
|
2012-10-30 21:50:58 +01:00
|
|
|
return GetHandle(false)->IsUint32();
|
2012-05-23 21:01:04 +02:00
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
bool CefV8ValueImpl::IsDouble() {
|
2011-02-21 23:44:06 +01:00
|
|
|
CEF_REQUIRE_UI_THREAD(false);
|
2012-10-29 22:54:57 +01:00
|
|
|
CEF_V8_REQUIRE_VALID_RETURN(false);
|
2012-10-30 21:50:58 +01:00
|
|
|
return GetHandle(false)->IsNumber();
|
2011-05-23 19:43:53 +02:00
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
bool CefV8ValueImpl::IsDate() {
|
2011-05-23 19:43:53 +02:00
|
|
|
CEF_REQUIRE_UI_THREAD(false);
|
2012-10-29 22:54:57 +01:00
|
|
|
CEF_V8_REQUIRE_VALID_RETURN(false);
|
2012-10-30 21:50:58 +01:00
|
|
|
return GetHandle(false)->IsDate();
|
2010-10-03 23:04:50 +02:00
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
bool CefV8ValueImpl::IsString() {
|
2011-02-21 23:44:06 +01:00
|
|
|
CEF_REQUIRE_UI_THREAD(false);
|
2012-10-29 22:54:57 +01:00
|
|
|
CEF_V8_REQUIRE_VALID_RETURN(false);
|
2012-10-30 21:50:58 +01:00
|
|
|
return GetHandle(false)->IsString();
|
2010-10-03 23:04:50 +02:00
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
bool CefV8ValueImpl::IsObject() {
|
2011-02-21 23:44:06 +01:00
|
|
|
CEF_REQUIRE_UI_THREAD(false);
|
2012-10-29 22:54:57 +01:00
|
|
|
CEF_V8_REQUIRE_VALID_RETURN(false);
|
2012-10-30 21:50:58 +01:00
|
|
|
return GetHandle(false)->IsObject();
|
2010-10-03 23:04:50 +02:00
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
bool CefV8ValueImpl::IsArray() {
|
2011-02-21 23:44:06 +01:00
|
|
|
CEF_REQUIRE_UI_THREAD(false);
|
2012-10-29 22:54:57 +01:00
|
|
|
CEF_V8_REQUIRE_VALID_RETURN(false);
|
2012-10-30 21:50:58 +01:00
|
|
|
return GetHandle(false)->IsArray();
|
2010-10-03 23:04:50 +02:00
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
bool CefV8ValueImpl::IsFunction() {
|
2011-02-21 23:44:06 +01:00
|
|
|
CEF_REQUIRE_UI_THREAD(false);
|
2012-10-29 22:54:57 +01:00
|
|
|
CEF_V8_REQUIRE_VALID_RETURN(false);
|
2012-10-30 21:50:58 +01:00
|
|
|
return GetHandle(false)->IsFunction();
|
2010-10-03 23:04:50 +02:00
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
bool CefV8ValueImpl::IsSame(CefRefPtr<CefV8Value> that) {
|
2011-04-07 03:58:49 +02:00
|
|
|
CEF_REQUIRE_UI_THREAD(false);
|
2012-10-29 22:54:57 +01:00
|
|
|
CEF_V8_REQUIRE_VALID_RETURN(false);
|
2011-04-07 03:58:49 +02:00
|
|
|
|
|
|
|
v8::HandleScope handle_scope;
|
|
|
|
|
|
|
|
v8::Handle<v8::Value> thatHandle;
|
2012-10-30 21:50:58 +01:00
|
|
|
v8::Handle<v8::Value> thisHandle = GetHandle(false);
|
2011-04-07 03:58:49 +02:00
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
CefV8ValueImpl* impl = static_cast<CefV8ValueImpl*>(that.get());
|
2011-04-07 03:58:49 +02:00
|
|
|
if (impl)
|
2012-10-30 21:50:58 +01:00
|
|
|
thatHandle = impl->GetHandle(false);
|
2011-04-07 03:58:49 +02:00
|
|
|
|
|
|
|
return (thisHandle == thatHandle);
|
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
bool CefV8ValueImpl::GetBoolValue() {
|
2011-02-21 23:44:06 +01:00
|
|
|
CEF_REQUIRE_UI_THREAD(false);
|
2012-10-29 22:54:57 +01:00
|
|
|
CEF_V8_REQUIRE_VALID_RETURN(false);
|
2012-10-30 21:50:58 +01:00
|
|
|
if (GetHandle(false)->IsTrue()) {
|
2011-02-21 23:44:06 +01:00
|
|
|
return true;
|
2012-10-30 21:50:58 +01:00
|
|
|
} else if (GetHandle(false)->IsFalse()) {
|
2011-02-21 23:44:06 +01:00
|
|
|
return false;
|
|
|
|
} else {
|
2010-10-03 23:04:50 +02:00
|
|
|
v8::HandleScope handle_scope;
|
2012-10-30 21:50:58 +01:00
|
|
|
v8::Local<v8::Boolean> val = GetHandle(false)->ToBoolean();
|
2011-02-21 23:44:06 +01:00
|
|
|
return val->Value();
|
2010-10-03 23:04:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-23 21:01:04 +02:00
|
|
|
int32 CefV8ValueImpl::GetIntValue() {
|
2011-02-21 23:44:06 +01:00
|
|
|
CEF_REQUIRE_UI_THREAD(0);
|
2012-10-29 22:54:57 +01:00
|
|
|
CEF_V8_REQUIRE_VALID_RETURN(0);
|
2010-10-03 23:04:50 +02:00
|
|
|
v8::HandleScope handle_scope;
|
2012-10-30 21:50:58 +01:00
|
|
|
v8::Local<v8::Int32> val = GetHandle(false)->ToInt32();
|
2011-02-21 23:44:06 +01:00
|
|
|
return val->Value();
|
2010-10-03 23:04:50 +02:00
|
|
|
}
|
|
|
|
|
2012-05-23 21:01:04 +02:00
|
|
|
uint32 CefV8ValueImpl::GetUIntValue() {
|
|
|
|
CEF_REQUIRE_UI_THREAD(0);
|
2012-10-29 22:54:57 +01:00
|
|
|
CEF_V8_REQUIRE_VALID_RETURN(0);
|
2012-05-23 21:01:04 +02:00
|
|
|
v8::HandleScope handle_scope;
|
2012-10-30 21:50:58 +01:00
|
|
|
v8::Local<v8::Uint32> val = GetHandle(false)->ToUint32();
|
2012-05-23 21:01:04 +02:00
|
|
|
return val->Value();
|
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
double CefV8ValueImpl::GetDoubleValue() {
|
2011-02-21 23:44:06 +01:00
|
|
|
CEF_REQUIRE_UI_THREAD(0.);
|
2012-10-29 22:54:57 +01:00
|
|
|
CEF_V8_REQUIRE_VALID_RETURN(0.);
|
2010-10-03 23:04:50 +02:00
|
|
|
v8::HandleScope handle_scope;
|
2012-10-30 21:50:58 +01:00
|
|
|
v8::Local<v8::Number> val = GetHandle(false)->ToNumber();
|
2011-02-21 23:44:06 +01:00
|
|
|
return val->Value();
|
2010-10-03 23:04:50 +02:00
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
CefTime CefV8ValueImpl::GetDateValue() {
|
|
|
|
CEF_REQUIRE_UI_THREAD(CefTime(0.));
|
2012-10-29 22:54:57 +01:00
|
|
|
CEF_V8_REQUIRE_VALID_RETURN(CefTime(0.));
|
2011-05-23 19:43:53 +02:00
|
|
|
v8::HandleScope handle_scope;
|
2012-10-30 21:50:58 +01:00
|
|
|
v8::Local<v8::Number> val = GetHandle(false)->ToNumber();
|
2011-05-23 19:43:53 +02:00
|
|
|
// Convert from milliseconds to seconds.
|
|
|
|
return CefTime(val->Value() / 1000);
|
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
CefString CefV8ValueImpl::GetStringValue() {
|
2010-11-22 18:49:46 +01:00
|
|
|
CefString rv;
|
2011-02-21 23:44:06 +01:00
|
|
|
CEF_REQUIRE_UI_THREAD(rv);
|
2012-10-29 22:54:57 +01:00
|
|
|
CEF_V8_REQUIRE_VALID_RETURN(rv);
|
2010-10-03 23:04:50 +02:00
|
|
|
v8::HandleScope handle_scope;
|
2012-10-30 21:50:58 +01:00
|
|
|
GetCefString(GetHandle(false)->ToString(), rv);
|
2010-10-03 23:04:50 +02:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2012-05-23 21:01:04 +02:00
|
|
|
bool CefV8ValueImpl::IsUserCreated() {
|
|
|
|
CEF_REQUIRE_UI_THREAD(false);
|
|
|
|
CEF_V8_REQUIRE_OBJECT_RETURN(false);
|
|
|
|
|
|
|
|
v8::HandleScope handle_scope;
|
2012-10-30 21:50:58 +01:00
|
|
|
v8::Local<v8::Object> obj = GetHandle(false)->ToObject();
|
2012-05-23 21:01:04 +02:00
|
|
|
|
|
|
|
V8TrackObject* tracker = V8TrackObject::Unwrap(obj);
|
|
|
|
return (tracker != NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CefV8ValueImpl::HasException() {
|
|
|
|
CEF_REQUIRE_UI_THREAD(false);
|
|
|
|
CEF_V8_REQUIRE_OBJECT_RETURN(false);
|
|
|
|
|
|
|
|
return (last_exception_.get() != NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
CefRefPtr<CefV8Exception> CefV8ValueImpl::GetException() {
|
|
|
|
CEF_REQUIRE_UI_THREAD(NULL);
|
|
|
|
CEF_V8_REQUIRE_OBJECT_RETURN(NULL);
|
|
|
|
|
|
|
|
return last_exception_;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CefV8ValueImpl::ClearException() {
|
2012-08-07 15:58:35 +02:00
|
|
|
CEF_REQUIRE_UI_THREAD(false);
|
|
|
|
CEF_V8_REQUIRE_OBJECT_RETURN(false);
|
2012-05-23 21:01:04 +02:00
|
|
|
|
|
|
|
last_exception_ = NULL;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CefV8ValueImpl::WillRethrowExceptions() {
|
|
|
|
CEF_REQUIRE_UI_THREAD(false);
|
|
|
|
CEF_V8_REQUIRE_OBJECT_RETURN(false);
|
|
|
|
|
|
|
|
return rethrow_exceptions_;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CefV8ValueImpl::SetRethrowExceptions(bool rethrow) {
|
|
|
|
CEF_REQUIRE_UI_THREAD(false);
|
|
|
|
CEF_V8_REQUIRE_OBJECT_RETURN(false);
|
|
|
|
|
|
|
|
rethrow_exceptions_ = rethrow;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
bool CefV8ValueImpl::HasValue(const CefString& key) {
|
2011-02-21 23:44:06 +01:00
|
|
|
CEF_REQUIRE_UI_THREAD(false);
|
2012-05-23 21:01:04 +02:00
|
|
|
CEF_V8_REQUIRE_OBJECT_RETURN(false);
|
2011-11-16 23:12:54 +01:00
|
|
|
|
2011-02-21 23:44:06 +01:00
|
|
|
v8::HandleScope handle_scope;
|
2012-10-30 21:50:58 +01:00
|
|
|
v8::Local<v8::Object> obj = GetHandle(false)->ToObject();
|
2011-02-21 23:44:06 +01:00
|
|
|
return obj->Has(GetV8String(key));
|
2010-10-03 23:04:50 +02:00
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
bool CefV8ValueImpl::HasValue(int index) {
|
2011-02-21 23:44:06 +01:00
|
|
|
CEF_REQUIRE_UI_THREAD(false);
|
2012-05-23 21:01:04 +02:00
|
|
|
CEF_V8_REQUIRE_OBJECT_RETURN(false);
|
|
|
|
|
2011-12-08 11:22:15 +01:00
|
|
|
if (index < 0) {
|
|
|
|
NOTREACHED() << "invalid input parameter";
|
|
|
|
return false;
|
|
|
|
}
|
2011-11-16 23:12:54 +01:00
|
|
|
|
2011-02-21 23:44:06 +01:00
|
|
|
v8::HandleScope handle_scope;
|
2012-10-30 21:50:58 +01:00
|
|
|
v8::Local<v8::Object> obj = GetHandle(false)->ToObject();
|
2011-02-21 23:44:06 +01:00
|
|
|
return obj->Has(index);
|
2010-10-03 23:04:50 +02:00
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
bool CefV8ValueImpl::DeleteValue(const CefString& key) {
|
2011-02-21 23:44:06 +01:00
|
|
|
CEF_REQUIRE_UI_THREAD(false);
|
2012-05-23 21:01:04 +02:00
|
|
|
CEF_V8_REQUIRE_OBJECT_RETURN(false);
|
2011-11-16 23:12:54 +01:00
|
|
|
|
2011-02-21 23:44:06 +01:00
|
|
|
v8::HandleScope handle_scope;
|
2012-10-30 21:50:58 +01:00
|
|
|
v8::Local<v8::Object> obj = GetHandle(false)->ToObject();
|
2012-05-23 21:01:04 +02:00
|
|
|
|
|
|
|
v8::TryCatch try_catch;
|
|
|
|
try_catch.SetVerbose(true);
|
|
|
|
bool del = obj->Delete(GetV8String(key));
|
|
|
|
return (!HasCaught(try_catch) && del);
|
2010-10-03 23:04:50 +02:00
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
bool CefV8ValueImpl::DeleteValue(int index) {
|
2011-02-21 23:44:06 +01:00
|
|
|
CEF_REQUIRE_UI_THREAD(false);
|
2012-05-23 21:01:04 +02:00
|
|
|
CEF_V8_REQUIRE_OBJECT_RETURN(false);
|
|
|
|
|
2011-12-08 11:22:15 +01:00
|
|
|
if (index < 0) {
|
|
|
|
NOTREACHED() << "invalid input parameter";
|
|
|
|
return false;
|
|
|
|
}
|
2011-11-16 23:12:54 +01:00
|
|
|
|
2011-02-21 23:44:06 +01:00
|
|
|
v8::HandleScope handle_scope;
|
2012-10-30 21:50:58 +01:00
|
|
|
v8::Local<v8::Object> obj = GetHandle(false)->ToObject();
|
2012-05-23 21:01:04 +02:00
|
|
|
|
|
|
|
v8::TryCatch try_catch;
|
|
|
|
try_catch.SetVerbose(true);
|
|
|
|
bool del = obj->Delete(index);
|
|
|
|
return (!HasCaught(try_catch) && del);
|
2010-10-03 23:04:50 +02:00
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
CefRefPtr<CefV8Value> CefV8ValueImpl::GetValue(const CefString& key) {
|
2011-02-21 23:44:06 +01:00
|
|
|
CEF_REQUIRE_UI_THREAD(NULL);
|
2012-05-23 21:01:04 +02:00
|
|
|
CEF_V8_REQUIRE_OBJECT_RETURN(NULL);
|
2011-11-16 23:12:54 +01:00
|
|
|
|
2011-02-21 23:44:06 +01:00
|
|
|
v8::HandleScope handle_scope;
|
2012-10-30 21:50:58 +01:00
|
|
|
v8::Local<v8::Object> obj = GetHandle(false)->ToObject();
|
2012-05-23 21:01:04 +02:00
|
|
|
|
|
|
|
v8::TryCatch try_catch;
|
|
|
|
try_catch.SetVerbose(true);
|
|
|
|
v8::Local<v8::Value> value = obj->Get(GetV8String(key));
|
|
|
|
if (!HasCaught(try_catch) && !value.IsEmpty())
|
|
|
|
return new CefV8ValueImpl(value);
|
|
|
|
return NULL;
|
2010-10-03 23:04:50 +02:00
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
CefRefPtr<CefV8Value> CefV8ValueImpl::GetValue(int index) {
|
2011-02-21 23:44:06 +01:00
|
|
|
CEF_REQUIRE_UI_THREAD(NULL);
|
2012-05-23 21:01:04 +02:00
|
|
|
CEF_V8_REQUIRE_OBJECT_RETURN(NULL);
|
|
|
|
|
2011-12-08 11:22:15 +01:00
|
|
|
if (index < 0) {
|
|
|
|
NOTREACHED() << "invalid input parameter";
|
2011-12-08 14:15:55 +01:00
|
|
|
return NULL;
|
2011-12-08 11:22:15 +01:00
|
|
|
}
|
2011-02-21 23:44:06 +01:00
|
|
|
|
|
|
|
v8::HandleScope handle_scope;
|
2012-10-30 21:50:58 +01:00
|
|
|
v8::Local<v8::Object> obj = GetHandle(false)->ToObject();
|
2012-05-23 21:01:04 +02:00
|
|
|
|
|
|
|
v8::TryCatch try_catch;
|
|
|
|
try_catch.SetVerbose(true);
|
|
|
|
v8::Local<v8::Value> value = obj->Get(v8::Number::New(index));
|
|
|
|
if (!HasCaught(try_catch) && !value.IsEmpty())
|
|
|
|
return new CefV8ValueImpl(value);
|
|
|
|
return NULL;
|
2010-10-03 23:04:50 +02:00
|
|
|
}
|
|
|
|
|
2010-11-22 18:49:46 +01:00
|
|
|
bool CefV8ValueImpl::SetValue(const CefString& key,
|
2011-11-04 20:34:14 +01:00
|
|
|
CefRefPtr<CefV8Value> value,
|
2012-01-10 00:46:23 +01:00
|
|
|
PropertyAttribute attribute) {
|
2011-02-21 23:44:06 +01:00
|
|
|
CEF_REQUIRE_UI_THREAD(false);
|
2012-05-23 21:01:04 +02:00
|
|
|
CEF_V8_REQUIRE_OBJECT_RETURN(false);
|
2010-10-03 23:04:50 +02:00
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
CefV8ValueImpl* impl = static_cast<CefV8ValueImpl*>(value.get());
|
2012-09-26 23:39:21 +02:00
|
|
|
if (impl) {
|
2011-02-21 23:44:06 +01:00
|
|
|
v8::HandleScope handle_scope;
|
2012-10-30 21:50:58 +01:00
|
|
|
v8::Local<v8::Object> obj = GetHandle(false)->ToObject();
|
2012-05-23 21:01:04 +02:00
|
|
|
|
|
|
|
v8::TryCatch try_catch;
|
|
|
|
try_catch.SetVerbose(true);
|
2012-10-30 21:50:58 +01:00
|
|
|
bool set = obj->Set(GetV8String(key), impl->GetHandle(true),
|
2012-05-23 21:01:04 +02:00
|
|
|
static_cast<v8::PropertyAttribute>(attribute));
|
|
|
|
return (!HasCaught(try_catch) && set);
|
2011-02-21 23:44:06 +01:00
|
|
|
} else {
|
2011-11-16 23:12:54 +01:00
|
|
|
NOTREACHED() << "invalid input parameter";
|
2011-02-21 23:44:06 +01:00
|
|
|
return false;
|
2010-10-03 23:04:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
bool CefV8ValueImpl::SetValue(int index, CefRefPtr<CefV8Value> value) {
|
2011-02-21 23:44:06 +01:00
|
|
|
CEF_REQUIRE_UI_THREAD(false);
|
2012-05-23 21:01:04 +02:00
|
|
|
CEF_V8_REQUIRE_OBJECT_RETURN(false);
|
2011-09-20 22:41:54 +02:00
|
|
|
|
2011-12-08 11:22:15 +01:00
|
|
|
if (index < 0) {
|
|
|
|
NOTREACHED() << "invalid input parameter";
|
|
|
|
return false;
|
|
|
|
}
|
2011-09-20 22:41:54 +02:00
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
CefV8ValueImpl* impl = static_cast<CefV8ValueImpl*>(value.get());
|
|
|
|
if (impl) {
|
2011-02-21 23:44:06 +01:00
|
|
|
v8::HandleScope handle_scope;
|
2012-10-30 21:50:58 +01:00
|
|
|
v8::Local<v8::Object> obj = GetHandle(false)->ToObject();
|
2012-05-23 21:01:04 +02:00
|
|
|
|
|
|
|
v8::TryCatch try_catch;
|
|
|
|
try_catch.SetVerbose(true);
|
2012-10-30 21:50:58 +01:00
|
|
|
bool set = obj->Set(index, impl->GetHandle(true));
|
2012-05-23 21:01:04 +02:00
|
|
|
return (!HasCaught(try_catch) && set);
|
2011-02-21 23:44:06 +01:00
|
|
|
} else {
|
2011-11-16 23:12:54 +01:00
|
|
|
NOTREACHED() << "invalid input parameter";
|
2011-02-21 23:44:06 +01:00
|
|
|
return false;
|
2010-10-03 23:04:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
bool CefV8ValueImpl::SetValue(const CefString& key, AccessControl settings,
|
|
|
|
PropertyAttribute attribute) {
|
2011-04-07 03:58:49 +02:00
|
|
|
CEF_REQUIRE_UI_THREAD(false);
|
2012-05-23 21:01:04 +02:00
|
|
|
CEF_V8_REQUIRE_OBJECT_RETURN(false);
|
2011-11-16 23:12:54 +01:00
|
|
|
|
2011-04-07 03:58:49 +02:00
|
|
|
v8::HandleScope handle_scope;
|
2012-10-30 21:50:58 +01:00
|
|
|
v8::Local<v8::Object> obj = GetHandle(false)->ToObject();
|
2011-04-07 03:58:49 +02:00
|
|
|
|
2012-05-23 21:01:04 +02:00
|
|
|
CefRefPtr<CefV8Accessor> accessorPtr;
|
|
|
|
|
|
|
|
V8TrackObject* tracker = V8TrackObject::Unwrap(obj);
|
|
|
|
if (tracker)
|
|
|
|
accessorPtr = tracker->GetAccessor();
|
|
|
|
|
2011-11-07 22:39:57 +01:00
|
|
|
// Verify that an accessor exists for this object.
|
2012-05-23 21:01:04 +02:00
|
|
|
if (!accessorPtr.get())
|
2011-11-07 22:39:57 +01:00
|
|
|
return false;
|
|
|
|
|
2011-04-07 03:58:49 +02:00
|
|
|
v8::AccessorGetter getter = AccessorGetterCallbackImpl;
|
|
|
|
v8::AccessorSetter setter = (attribute & V8_PROPERTY_ATTRIBUTE_READONLY) ?
|
|
|
|
NULL : AccessorSetterCallbackImpl;
|
|
|
|
|
2012-05-23 21:01:04 +02:00
|
|
|
v8::TryCatch try_catch;
|
|
|
|
try_catch.SetVerbose(true);
|
|
|
|
bool set = obj->SetAccessor(GetV8String(key), getter, setter, obj,
|
|
|
|
static_cast<v8::AccessControl>(settings),
|
|
|
|
static_cast<v8::PropertyAttribute>(attribute));
|
|
|
|
return (!HasCaught(try_catch) && set);
|
2011-04-07 03:58:49 +02:00
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
bool CefV8ValueImpl::GetKeys(std::vector<CefString>& keys) {
|
2011-02-21 23:44:06 +01:00
|
|
|
CEF_REQUIRE_UI_THREAD(false);
|
2012-05-23 21:01:04 +02:00
|
|
|
CEF_V8_REQUIRE_OBJECT_RETURN(false);
|
2012-01-10 00:46:23 +01:00
|
|
|
|
2011-02-21 23:44:06 +01:00
|
|
|
v8::HandleScope handle_scope;
|
2012-10-30 21:50:58 +01:00
|
|
|
v8::Local<v8::Object> obj = GetHandle(false)->ToObject();
|
2011-02-21 23:44:06 +01:00
|
|
|
v8::Local<v8::Array> arr_keys = obj->GetPropertyNames();
|
|
|
|
uint32_t len = arr_keys->Length();
|
2012-01-10 00:46:23 +01:00
|
|
|
for (uint32_t i = 0; i < len; ++i) {
|
2011-02-21 23:44:06 +01:00
|
|
|
v8::Local<v8::Value> value = arr_keys->Get(v8::Integer::New(i));
|
2011-09-20 22:41:54 +02:00
|
|
|
CefString str;
|
|
|
|
GetCefString(value->ToString(), str);
|
2011-11-07 22:39:57 +01:00
|
|
|
keys.push_back(str);
|
2011-02-21 23:44:06 +01:00
|
|
|
}
|
|
|
|
return true;
|
2010-10-03 23:04:50 +02:00
|
|
|
}
|
|
|
|
|
2012-05-23 21:01:04 +02:00
|
|
|
bool CefV8ValueImpl::SetUserData(CefRefPtr<CefBase> user_data) {
|
|
|
|
CEF_REQUIRE_UI_THREAD(false);
|
|
|
|
CEF_V8_REQUIRE_OBJECT_RETURN(false);
|
|
|
|
|
|
|
|
v8::HandleScope handle_scope;
|
2012-10-30 21:50:58 +01:00
|
|
|
v8::Local<v8::Object> obj = GetHandle(false)->ToObject();
|
2012-05-23 21:01:04 +02:00
|
|
|
|
|
|
|
V8TrackObject* tracker = V8TrackObject::Unwrap(obj);
|
|
|
|
if (tracker) {
|
|
|
|
tracker->SetUserData(user_data);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
CefRefPtr<CefBase> CefV8ValueImpl::GetUserData() {
|
2011-02-21 23:44:06 +01:00
|
|
|
CEF_REQUIRE_UI_THREAD(NULL);
|
2012-05-23 21:01:04 +02:00
|
|
|
CEF_V8_REQUIRE_OBJECT_RETURN(NULL);
|
2012-01-10 00:46:23 +01:00
|
|
|
|
2011-02-21 23:44:06 +01:00
|
|
|
v8::HandleScope handle_scope;
|
2012-10-30 21:50:58 +01:00
|
|
|
v8::Local<v8::Object> obj = GetHandle(false)->ToObject();
|
2011-11-07 22:39:57 +01:00
|
|
|
|
2012-05-23 21:01:04 +02:00
|
|
|
V8TrackObject* tracker = V8TrackObject::Unwrap(obj);
|
|
|
|
if (tracker)
|
|
|
|
return tracker->GetUserData();
|
2011-11-07 22:39:57 +01:00
|
|
|
|
2011-02-21 23:44:06 +01:00
|
|
|
return NULL;
|
2010-10-03 23:04:50 +02:00
|
|
|
}
|
|
|
|
|
2012-01-19 19:52:59 +01:00
|
|
|
int CefV8ValueImpl::GetExternallyAllocatedMemory() {
|
|
|
|
CEF_REQUIRE_UI_THREAD(0);
|
2012-05-23 21:01:04 +02:00
|
|
|
CEF_V8_REQUIRE_OBJECT_RETURN(0);
|
2012-01-19 19:52:59 +01:00
|
|
|
|
2012-05-23 21:01:04 +02:00
|
|
|
v8::HandleScope handle_scope;
|
2012-10-30 21:50:58 +01:00
|
|
|
v8::Local<v8::Object> obj = GetHandle(false)->ToObject();
|
2012-05-23 21:01:04 +02:00
|
|
|
|
|
|
|
V8TrackObject* tracker = V8TrackObject::Unwrap(obj);
|
|
|
|
if (tracker)
|
|
|
|
return tracker->GetExternallyAllocatedMemory();
|
|
|
|
|
|
|
|
return 0;
|
2012-01-19 19:52:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int CefV8ValueImpl::AdjustExternallyAllocatedMemory(int change_in_bytes) {
|
|
|
|
CEF_REQUIRE_UI_THREAD(0);
|
2012-05-23 21:01:04 +02:00
|
|
|
CEF_V8_REQUIRE_OBJECT_RETURN(0);
|
2012-01-19 19:52:59 +01:00
|
|
|
|
2012-05-23 21:01:04 +02:00
|
|
|
v8::HandleScope handle_scope;
|
2012-10-30 21:50:58 +01:00
|
|
|
v8::Local<v8::Object> obj = GetHandle(false)->ToObject();
|
2012-01-19 19:52:59 +01:00
|
|
|
|
2012-05-23 21:01:04 +02:00
|
|
|
V8TrackObject* tracker = V8TrackObject::Unwrap(obj);
|
|
|
|
if (tracker)
|
|
|
|
return tracker->AdjustExternallyAllocatedMemory(change_in_bytes);
|
2012-01-19 19:52:59 +01:00
|
|
|
|
2012-05-23 21:01:04 +02:00
|
|
|
return 0;
|
2012-01-19 19:52:59 +01:00
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
int CefV8ValueImpl::GetArrayLength() {
|
2011-02-21 23:44:06 +01:00
|
|
|
CEF_REQUIRE_UI_THREAD(0);
|
2012-05-23 21:01:04 +02:00
|
|
|
CEF_V8_REQUIRE_ARRAY_RETURN(0);
|
2011-02-21 23:44:06 +01:00
|
|
|
|
|
|
|
v8::HandleScope handle_scope;
|
2012-10-30 21:50:58 +01:00
|
|
|
v8::Local<v8::Object> obj = GetHandle(false)->ToObject();
|
2011-02-21 23:44:06 +01:00
|
|
|
v8::Local<v8::Array> arr = v8::Local<v8::Array>::Cast(obj);
|
|
|
|
return arr->Length();
|
2010-10-03 23:04:50 +02:00
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
CefString CefV8ValueImpl::GetFunctionName() {
|
2010-11-22 18:49:46 +01:00
|
|
|
CefString rv;
|
2011-02-21 23:44:06 +01:00
|
|
|
CEF_REQUIRE_UI_THREAD(rv);
|
2012-05-23 21:01:04 +02:00
|
|
|
CEF_V8_REQUIRE_FUNCTION_RETURN(rv);
|
2012-01-10 00:46:23 +01:00
|
|
|
|
2011-02-21 23:44:06 +01:00
|
|
|
v8::HandleScope handle_scope;
|
2012-10-30 21:50:58 +01:00
|
|
|
v8::Local<v8::Object> obj = GetHandle(false)->ToObject();
|
2011-02-21 23:44:06 +01:00
|
|
|
v8::Local<v8::Function> func = v8::Local<v8::Function>::Cast(obj);
|
2011-09-20 22:41:54 +02:00
|
|
|
GetCefString(v8::Handle<v8::String>::Cast(func->GetName()), rv);
|
2010-10-03 23:04:50 +02:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2012-01-10 00:46:23 +01:00
|
|
|
CefRefPtr<CefV8Handler> CefV8ValueImpl::GetFunctionHandler() {
|
2011-02-21 23:44:06 +01:00
|
|
|
CEF_REQUIRE_UI_THREAD(NULL);
|
2012-05-23 21:01:04 +02:00
|
|
|
CEF_V8_REQUIRE_FUNCTION_RETURN(NULL);
|
2011-02-21 23:44:06 +01:00
|
|
|
|
|
|
|
v8::HandleScope handle_scope;
|
2012-10-30 21:50:58 +01:00
|
|
|
v8::Local<v8::Object> obj = GetHandle(false)->ToObject();
|
2011-11-07 22:39:57 +01:00
|
|
|
|
2012-05-23 21:01:04 +02:00
|
|
|
V8TrackObject* tracker = V8TrackObject::Unwrap(obj);
|
|
|
|
if (tracker)
|
|
|
|
return tracker->GetHandler();
|
2011-11-07 22:39:57 +01:00
|
|
|
|
2011-02-21 23:44:06 +01:00
|
|
|
return NULL;
|
2010-10-03 23:04:50 +02:00
|
|
|
}
|
|
|
|
|
2012-05-23 21:01:04 +02:00
|
|
|
CefRefPtr<CefV8Value> CefV8ValueImpl::ExecuteFunction(
|
|
|
|
CefRefPtr<CefV8Value> object,
|
|
|
|
const CefV8ValueList& arguments) {
|
2011-02-21 23:44:06 +01:00
|
|
|
// An empty context value defaults to the current context.
|
|
|
|
CefRefPtr<CefV8Context> context;
|
2012-05-23 21:01:04 +02:00
|
|
|
return ExecuteFunctionWithContext(context, object, arguments);
|
2011-02-21 23:44:06 +01:00
|
|
|
}
|
2010-10-03 23:04:50 +02:00
|
|
|
|
2012-05-23 21:01:04 +02:00
|
|
|
CefRefPtr<CefV8Value> CefV8ValueImpl::ExecuteFunctionWithContext(
|
|
|
|
CefRefPtr<CefV8Context> context,
|
|
|
|
CefRefPtr<CefV8Value> object,
|
|
|
|
const CefV8ValueList& arguments) {
|
|
|
|
CEF_REQUIRE_UI_THREAD(NULL);
|
|
|
|
CEF_V8_REQUIRE_FUNCTION_RETURN(NULL);
|
2010-10-03 23:04:50 +02:00
|
|
|
|
2011-02-21 23:44:06 +01:00
|
|
|
v8::HandleScope handle_scope;
|
2010-10-03 23:04:50 +02:00
|
|
|
|
2011-02-21 23:44:06 +01:00
|
|
|
v8::Local<v8::Context> context_local;
|
|
|
|
if (context.get()) {
|
|
|
|
CefV8ContextImpl* context_impl =
|
|
|
|
static_cast<CefV8ContextImpl*>(context.get());
|
|
|
|
context_local = context_impl->GetContext();
|
|
|
|
} else {
|
|
|
|
context_local = v8::Context::GetCurrent();
|
2010-10-03 23:04:50 +02:00
|
|
|
}
|
2011-02-21 23:44:06 +01:00
|
|
|
|
|
|
|
v8::Context::Scope context_scope(context_local);
|
|
|
|
|
2012-10-30 21:50:58 +01:00
|
|
|
v8::Local<v8::Object> obj = GetHandle(false)->ToObject();
|
2011-02-21 23:44:06 +01:00
|
|
|
v8::Local<v8::Function> func = v8::Local<v8::Function>::Cast(obj);
|
|
|
|
v8::Handle<v8::Object> recv;
|
|
|
|
|
|
|
|
// Default to the global object if no object or a non-object was provided.
|
|
|
|
if (object.get() && object->IsObject()) {
|
|
|
|
CefV8ValueImpl* recv_impl = static_cast<CefV8ValueImpl*>(object.get());
|
2012-10-30 21:50:58 +01:00
|
|
|
recv = v8::Handle<v8::Object>::Cast(recv_impl->GetHandle(true));
|
2011-02-21 23:44:06 +01:00
|
|
|
} else {
|
|
|
|
recv = context_local->Global();
|
|
|
|
}
|
|
|
|
|
|
|
|
int argc = arguments.size();
|
|
|
|
v8::Handle<v8::Value> *argv = NULL;
|
|
|
|
if (argc > 0) {
|
|
|
|
argv = new v8::Handle<v8::Value>[argc];
|
2012-10-30 21:50:58 +01:00
|
|
|
for (int i = 0; i < argc; ++i) {
|
|
|
|
argv[i] =
|
|
|
|
static_cast<CefV8ValueImpl*>(arguments[i].get())->GetHandle(true);
|
|
|
|
}
|
2011-02-21 23:44:06 +01:00
|
|
|
}
|
|
|
|
|
2012-05-23 21:01:04 +02:00
|
|
|
CefRefPtr<CefV8Value> retval;
|
|
|
|
|
|
|
|
{
|
|
|
|
v8::TryCatch try_catch;
|
|
|
|
try_catch.SetVerbose(true);
|
|
|
|
v8::Local<v8::Value> func_rv;
|
|
|
|
|
2012-08-29 22:11:26 +02:00
|
|
|
// Execute the function call using the ScriptController so that inspector
|
2012-05-23 21:01:04 +02:00
|
|
|
// instrumentation works.
|
2012-08-29 22:11:26 +02:00
|
|
|
RefPtr<WebCore::Frame> frame = WebCore::toFrameIfNotDetached(context_local);
|
|
|
|
DCHECK(frame);
|
|
|
|
if (frame &&
|
|
|
|
frame->script()->canExecuteScripts(WebCore::AboutToExecuteScript)) {
|
|
|
|
func_rv = frame->script()->callFunction(func, recv, argc, argv);
|
|
|
|
}
|
2012-05-23 21:01:04 +02:00
|
|
|
|
|
|
|
if (!HasCaught(try_catch) && !func_rv.IsEmpty())
|
|
|
|
retval = new CefV8ValueImpl(func_rv);
|
2011-10-21 16:25:14 +02:00
|
|
|
}
|
2011-02-21 23:44:06 +01:00
|
|
|
|
2012-02-15 17:19:59 +01:00
|
|
|
if (argv)
|
|
|
|
delete [] argv;
|
|
|
|
|
2012-05-23 21:01:04 +02:00
|
|
|
return retval;
|
2011-02-21 23:44:06 +01:00
|
|
|
}
|
|
|
|
|
2012-05-23 21:01:04 +02:00
|
|
|
bool CefV8ValueImpl::HasCaught(v8::TryCatch& try_catch) {
|
|
|
|
if (try_catch.HasCaught()) {
|
|
|
|
last_exception_ = new CefV8ExceptionImpl(try_catch.Message());
|
|
|
|
if (rethrow_exceptions_)
|
|
|
|
try_catch.ReThrow();
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
if (last_exception_.get())
|
|
|
|
last_exception_ = NULL;
|
|
|
|
return false;
|
|
|
|
}
|
2012-01-19 19:52:59 +01:00
|
|
|
}
|
2012-07-25 11:45:49 +02:00
|
|
|
|
|
|
|
|
|
|
|
// CefV8StackTrace
|
|
|
|
|
|
|
|
// static
|
|
|
|
CefRefPtr<CefV8StackTrace> CefV8StackTrace::GetCurrent(int frame_limit) {
|
|
|
|
CEF_REQUIRE_VALID_CONTEXT(NULL);
|
|
|
|
CEF_REQUIRE_UI_THREAD(NULL);
|
|
|
|
|
2012-10-29 22:54:57 +01:00
|
|
|
v8::HandleScope handle_scope;
|
2012-07-25 11:45:49 +02:00
|
|
|
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)
|
2012-10-29 22:54:57 +01:00
|
|
|
: handle_(new Handle(v8::Handle<v8::Context>(), handle)) {
|
2012-07-25 11:45:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
CefV8StackTraceImpl::~CefV8StackTraceImpl() {
|
|
|
|
}
|
|
|
|
|
2012-10-29 22:54:57 +01:00
|
|
|
bool CefV8StackTraceImpl::IsValid() {
|
|
|
|
CEF_REQUIRE_UI_THREAD(false);
|
|
|
|
return handle_->IsValid();
|
|
|
|
}
|
|
|
|
|
2012-07-25 11:45:49 +02:00
|
|
|
int CefV8StackTraceImpl::GetFrameCount() {
|
|
|
|
CEF_REQUIRE_UI_THREAD(0);
|
2012-10-29 22:54:57 +01:00
|
|
|
CEF_V8_REQUIRE_VALID_RETURN(0);
|
2012-07-25 11:45:49 +02:00
|
|
|
v8::HandleScope handle_scope;
|
|
|
|
return GetHandle()->GetFrameCount();
|
|
|
|
}
|
|
|
|
|
|
|
|
CefRefPtr<CefV8StackFrame> CefV8StackTraceImpl::GetFrame(int index) {
|
|
|
|
CEF_REQUIRE_UI_THREAD(NULL);
|
2012-10-29 22:54:57 +01:00
|
|
|
CEF_V8_REQUIRE_VALID_RETURN(NULL);
|
2012-07-25 11:45:49 +02:00
|
|
|
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)
|
2012-10-29 22:54:57 +01:00
|
|
|
: handle_(new Handle(v8::Handle<v8::Context>(), handle)) {
|
2012-07-25 11:45:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
CefV8StackFrameImpl::~CefV8StackFrameImpl() {
|
|
|
|
}
|
|
|
|
|
2012-10-29 22:54:57 +01:00
|
|
|
bool CefV8StackFrameImpl::IsValid() {
|
|
|
|
CEF_REQUIRE_UI_THREAD(false);
|
|
|
|
return handle_->IsValid();
|
|
|
|
}
|
|
|
|
|
2012-07-25 11:45:49 +02:00
|
|
|
CefString CefV8StackFrameImpl::GetScriptName() {
|
|
|
|
CefString rv;
|
|
|
|
CEF_REQUIRE_UI_THREAD(rv);
|
2012-10-29 22:54:57 +01:00
|
|
|
CEF_V8_REQUIRE_VALID_RETURN(rv);
|
2012-07-25 11:45:49 +02:00
|
|
|
v8::HandleScope handle_scope;
|
|
|
|
GetCefString(v8::Handle<v8::String>::Cast(GetHandle()->GetScriptName()), rv);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
CefString CefV8StackFrameImpl::GetScriptNameOrSourceURL() {
|
|
|
|
CefString rv;
|
|
|
|
CEF_REQUIRE_UI_THREAD(rv);
|
2012-10-29 22:54:57 +01:00
|
|
|
CEF_V8_REQUIRE_VALID_RETURN(rv);
|
2012-07-25 11:45:49 +02:00
|
|
|
v8::HandleScope handle_scope;
|
|
|
|
GetCefString(
|
|
|
|
v8::Handle<v8::String>::Cast(GetHandle()->GetScriptNameOrSourceURL()),
|
|
|
|
rv);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
CefString CefV8StackFrameImpl::GetFunctionName() {
|
|
|
|
CefString rv;
|
|
|
|
CEF_REQUIRE_UI_THREAD(rv);
|
2012-10-29 22:54:57 +01:00
|
|
|
CEF_V8_REQUIRE_VALID_RETURN(rv);
|
2012-07-25 11:45:49 +02:00
|
|
|
v8::HandleScope handle_scope;
|
|
|
|
GetCefString(
|
|
|
|
v8::Handle<v8::String>::Cast(GetHandle()->GetFunctionName()), rv);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
int CefV8StackFrameImpl::GetLineNumber() {
|
|
|
|
CEF_REQUIRE_UI_THREAD(0);
|
2012-10-29 22:54:57 +01:00
|
|
|
CEF_V8_REQUIRE_VALID_RETURN(0);
|
2012-07-25 11:45:49 +02:00
|
|
|
v8::HandleScope handle_scope;
|
|
|
|
return GetHandle()->GetLineNumber();
|
|
|
|
}
|
|
|
|
|
|
|
|
int CefV8StackFrameImpl::GetColumn() {
|
|
|
|
CEF_REQUIRE_UI_THREAD(0);
|
2012-10-29 22:54:57 +01:00
|
|
|
CEF_V8_REQUIRE_VALID_RETURN(0);
|
2012-07-25 11:45:49 +02:00
|
|
|
v8::HandleScope handle_scope;
|
|
|
|
return GetHandle()->GetColumn();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CefV8StackFrameImpl::IsEval() {
|
|
|
|
CEF_REQUIRE_UI_THREAD(false);
|
2012-10-29 22:54:57 +01:00
|
|
|
CEF_V8_REQUIRE_VALID_RETURN(false);
|
2012-07-25 11:45:49 +02:00
|
|
|
v8::HandleScope handle_scope;
|
|
|
|
return GetHandle()->IsEval();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CefV8StackFrameImpl::IsConstructor() {
|
|
|
|
CEF_REQUIRE_UI_THREAD(false);
|
2012-10-29 22:54:57 +01:00
|
|
|
CEF_V8_REQUIRE_VALID_RETURN(false);
|
2012-07-25 11:45:49 +02:00
|
|
|
v8::HandleScope handle_scope;
|
|
|
|
return GetHandle()->IsConstructor();
|
|
|
|
}
|
2012-10-12 18:38:24 +02:00
|
|
|
|
|
|
|
void CefV8MessageHandler(v8::Handle<v8::Message> message,
|
|
|
|
v8::Handle<v8::Value> data) {
|
|
|
|
CEF_REQUIRE_VALID_CONTEXT(void());
|
|
|
|
CEF_REQUIRE_UI_THREAD(void());
|
|
|
|
|
|
|
|
CefRefPtr<CefV8Context> context = CefV8Context::GetCurrentContext();
|
|
|
|
CefRefPtr<CefBrowser> browser = context->GetBrowser();
|
|
|
|
CefRefPtr<CefFrame> frame = context->GetFrame();
|
|
|
|
|
|
|
|
v8::Handle<v8::StackTrace> v8Stack = message->GetStackTrace();
|
|
|
|
DCHECK(!v8Stack.IsEmpty());
|
|
|
|
CefRefPtr<CefV8StackTrace> stackTrace = new CefV8StackTraceImpl(v8Stack);
|
|
|
|
|
|
|
|
CefRefPtr<CefClient> client = browser->GetClient();
|
|
|
|
if (!client.get())
|
|
|
|
return;
|
|
|
|
|
|
|
|
CefRefPtr<CefV8ContextHandler> handler = client->GetV8ContextHandler();
|
|
|
|
if (!handler.get())
|
|
|
|
return;
|
|
|
|
|
|
|
|
CefRefPtr<CefV8Exception> exception = new CefV8ExceptionImpl(message);
|
|
|
|
handler->OnUncaughtException(browser, frame, context, exception, stackTrace);
|
|
|
|
}
|