2013-01-03 18:24:24 +01:00
|
|
|
// Copyright (c) 2013 The Chromium Embedded Framework Authors. All rights
|
2012-04-03 03:34:16 +02:00
|
|
|
// reserved. Use of this source code is governed by a BSD-style license that
|
|
|
|
// can be found in the LICENSE file.
|
|
|
|
|
2013-10-16 02:25:38 +02:00
|
|
|
// MSVC++ requires this to be set before any other includes to get M_PI.
|
|
|
|
// Otherwise there will be compile errors in wtf/MathExtras.h.
|
|
|
|
#define _USE_MATH_DEFINES
|
|
|
|
|
2012-10-29 22:46:02 +01:00
|
|
|
#include <map>
|
2012-04-03 03:34:16 +02:00
|
|
|
#include <string>
|
|
|
|
|
2012-10-29 22:46:02 +01:00
|
|
|
#include "base/command_line.h"
|
2012-04-24 20:01:48 +02:00
|
|
|
#include "base/compiler_specific.h"
|
|
|
|
|
2013-05-07 23:48:34 +02:00
|
|
|
#include "config.h"
|
2012-04-24 20:01:48 +02:00
|
|
|
MSVC_PUSH_WARNING_LEVEL(0);
|
2013-10-17 01:09:07 +02:00
|
|
|
#include "core/frame/Frame.h"
|
2013-07-24 22:15:18 +02:00
|
|
|
#include "core/workers/WorkerGlobalScope.h"
|
2013-05-07 23:48:34 +02:00
|
|
|
#include "bindings/v8/ScriptController.h"
|
|
|
|
#include "bindings/v8/V8Binding.h"
|
|
|
|
#include "bindings/v8/V8RecursionScope.h"
|
|
|
|
#include "bindings/v8/WorkerScriptController.h"
|
2012-04-24 20:01:48 +02:00
|
|
|
MSVC_POP_WARNING();
|
|
|
|
#undef LOG
|
|
|
|
|
|
|
|
#include "libcef/renderer/v8_impl.h"
|
|
|
|
|
2012-10-29 22:46:02 +01:00
|
|
|
#include "libcef/common/cef_switches.h"
|
2012-11-02 19:16:28 +01:00
|
|
|
#include "libcef/common/content_client.h"
|
2013-01-03 18:24:24 +01:00
|
|
|
#include "libcef/common/task_runner_impl.h"
|
2012-04-03 03:34:16 +02:00
|
|
|
#include "libcef/common/tracker.h"
|
|
|
|
#include "libcef/renderer/browser_impl.h"
|
|
|
|
#include "libcef/renderer/thread_util.h"
|
|
|
|
|
|
|
|
#include "base/bind.h"
|
|
|
|
#include "base/lazy_instance.h"
|
2013-06-22 04:06:32 +02:00
|
|
|
#include "base/strings/string_number_conversions.h"
|
2013-01-03 18:24:24 +01:00
|
|
|
#include "base/threading/thread_local.h"
|
2013-06-22 04:06:32 +02:00
|
|
|
#include "third_party/WebKit/public/web/WebKit.h"
|
|
|
|
#include "third_party/WebKit/public/web/WebFrame.h"
|
|
|
|
#include "third_party/WebKit/public/web/WebScriptController.h"
|
2013-07-24 22:15:18 +02:00
|
|
|
#include "url/gurl.h"
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
static const char kCefTrackObject[] = "Cef::TrackObject";
|
2012-10-29 22:46:02 +01:00
|
|
|
static const char kCefContextState[] = "Cef::ContextState";
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2013-01-03 18:24:24 +01:00
|
|
|
void MessageListenerCallbackImpl(v8::Handle<v8::Message> message,
|
|
|
|
v8::Handle<v8::Value> data);
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2013-01-03 18:24:24 +01:00
|
|
|
// Manages memory and state information associated with a single Isolate.
|
|
|
|
class CefV8IsolateManager {
|
2012-10-29 22:46:02 +01:00
|
|
|
public:
|
2013-01-03 18:24:24 +01:00
|
|
|
CefV8IsolateManager()
|
|
|
|
: isolate_(v8::Isolate::GetCurrent()),
|
|
|
|
task_runner_(CefContentRendererClient::Get()->GetCurrentTaskRunner()),
|
|
|
|
context_safety_impl_(IMPL_HASH),
|
|
|
|
message_listener_registered_(false),
|
|
|
|
worker_id_(0) {
|
|
|
|
DCHECK(isolate_);
|
|
|
|
DCHECK(task_runner_.get());
|
|
|
|
|
2012-10-29 22:46:02 +01:00
|
|
|
const CommandLine& command_line = *CommandLine::ForCurrentProcess();
|
|
|
|
if (command_line.HasSwitch(switches::kContextSafetyImplementation)) {
|
|
|
|
std::string value = command_line.GetSwitchValueASCII(
|
|
|
|
switches::kContextSafetyImplementation);
|
|
|
|
int mode;
|
|
|
|
if (base::StringToInt(value, &mode)) {
|
|
|
|
if (mode < 0)
|
|
|
|
context_safety_impl_ = IMPL_DISABLED;
|
|
|
|
else if (mode == 1)
|
|
|
|
context_safety_impl_ = IMPL_VALUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-01-03 18:24:24 +01:00
|
|
|
~CefV8IsolateManager() {
|
|
|
|
DCHECK_EQ(isolate_, v8::Isolate::GetCurrent());
|
|
|
|
DCHECK(context_map_.empty());
|
|
|
|
}
|
2012-10-29 22:46:02 +01:00
|
|
|
|
|
|
|
scoped_refptr<CefV8ContextState> GetContextState(
|
|
|
|
v8::Handle<v8::Context> context) {
|
2013-01-03 18:24:24 +01:00
|
|
|
DCHECK_EQ(isolate_, v8::Isolate::GetCurrent());
|
2013-03-12 21:23:24 +01:00
|
|
|
DCHECK(context.IsEmpty() || isolate_ == context->GetIsolate());
|
2013-01-03 18:24:24 +01:00
|
|
|
|
2012-10-29 22:46:02 +01:00
|
|
|
if (context_safety_impl_ == IMPL_DISABLED)
|
|
|
|
return scoped_refptr<CefV8ContextState>();
|
|
|
|
|
|
|
|
if (context.IsEmpty()) {
|
2013-12-17 23:04:35 +01:00
|
|
|
if (isolate_->InContext())
|
|
|
|
context = isolate_->GetCurrentContext();
|
2012-10-29 22:46:02 +01:00
|
|
|
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 {
|
2013-12-17 23:04:35 +01:00
|
|
|
v8::Handle<v8::String> key =
|
|
|
|
v8::String::NewFromUtf8(isolate_, kCefContextState);
|
2012-10-29 22:46:02 +01:00
|
|
|
|
|
|
|
v8::Handle<v8::Object> object = context->Global();
|
2013-06-27 00:33:44 +02:00
|
|
|
v8::Handle<v8::Value> value = object->GetHiddenValue(key);
|
2012-11-30 20:08:20 +01:00
|
|
|
if (!value.IsEmpty()) {
|
|
|
|
return static_cast<CefV8ContextState*>(
|
|
|
|
v8::External::Cast(*value)->Value());
|
|
|
|
}
|
2012-10-29 22:46:02 +01:00
|
|
|
|
|
|
|
scoped_refptr<CefV8ContextState> state = new CefV8ContextState();
|
2013-12-17 23:04:35 +01:00
|
|
|
object->SetHiddenValue(key, v8::External::New(isolate_, state.get()));
|
2012-10-29 22:46:02 +01:00
|
|
|
|
|
|
|
// Reference will be released in ReleaseContext.
|
|
|
|
state->AddRef();
|
|
|
|
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ReleaseContext(v8::Handle<v8::Context> context) {
|
2013-01-03 18:24:24 +01:00
|
|
|
DCHECK_EQ(isolate_, v8::Isolate::GetCurrent());
|
2013-12-17 23:04:35 +01:00
|
|
|
DCHECK_EQ(isolate_, context->GetIsolate());
|
2013-01-03 18:24:24 +01:00
|
|
|
|
2012-10-29 22:46:02 +01:00
|
|
|
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 {
|
2013-12-17 23:04:35 +01:00
|
|
|
v8::Handle<v8::String> key =
|
|
|
|
v8::String::NewFromUtf8(isolate_, kCefContextState);
|
2012-10-29 22:46:02 +01:00
|
|
|
v8::Handle<v8::Object> object = context->Global();
|
2013-06-27 00:33:44 +02:00
|
|
|
v8::Handle<v8::Value> value = object->GetHiddenValue(key);
|
2012-11-05 23:32:37 +01:00
|
|
|
if (value.IsEmpty())
|
|
|
|
return;
|
|
|
|
|
2012-10-29 22:46:02 +01:00
|
|
|
scoped_refptr<CefV8ContextState> state =
|
2012-11-30 20:08:20 +01:00
|
|
|
static_cast<CefV8ContextState*>(v8::External::Cast(*value)->Value());
|
2012-10-29 22:46:02 +01:00
|
|
|
state->Detach();
|
2013-06-27 00:33:44 +02:00
|
|
|
object->DeleteHiddenValue(key);
|
2012-10-29 22:46:02 +01:00
|
|
|
|
|
|
|
// Match the AddRef in GetContextState.
|
|
|
|
state->Release();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-30 21:49:43 +01:00
|
|
|
void AddGlobalTrackObject(CefTrackNode* object) {
|
2013-01-03 18:24:24 +01:00
|
|
|
DCHECK_EQ(isolate_, v8::Isolate::GetCurrent());
|
2012-10-30 21:49:43 +01:00
|
|
|
global_manager_.Add(object);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DeleteGlobalTrackObject(CefTrackNode* object) {
|
2013-01-03 18:24:24 +01:00
|
|
|
DCHECK_EQ(isolate_, v8::Isolate::GetCurrent());
|
2012-10-30 21:49:43 +01:00
|
|
|
global_manager_.Delete(object);
|
|
|
|
}
|
|
|
|
|
2013-01-03 18:24:24 +01:00
|
|
|
void SetUncaughtExceptionStackSize(int stack_size) {
|
|
|
|
if (stack_size <= 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!message_listener_registered_) {
|
|
|
|
v8::V8::AddMessageListener(&MessageListenerCallbackImpl);
|
|
|
|
message_listener_registered_ = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
v8::V8::SetCaptureStackTraceForUncaughtExceptions(true,
|
|
|
|
stack_size, v8::StackTrace::kDetailed);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetWorkerAttributes(int worker_id, const GURL& worker_url) {
|
|
|
|
worker_id_ = worker_id;
|
|
|
|
worker_url_ = worker_url;
|
|
|
|
}
|
|
|
|
|
2013-03-12 21:23:24 +01:00
|
|
|
v8::Isolate* isolate() const { return isolate_; }
|
2013-01-03 18:24:24 +01:00
|
|
|
scoped_refptr<base::SequencedTaskRunner> task_runner() const {
|
|
|
|
return task_runner_;
|
|
|
|
}
|
|
|
|
|
|
|
|
int worker_id() const {
|
|
|
|
return worker_id_;
|
|
|
|
}
|
|
|
|
|
|
|
|
const GURL& worker_url() const {
|
|
|
|
return worker_url_;
|
|
|
|
}
|
|
|
|
|
2012-10-29 22:46:02 +01:00
|
|
|
private:
|
2013-01-03 18:24:24 +01:00
|
|
|
v8::Isolate* isolate_;
|
|
|
|
scoped_refptr<base::SequencedTaskRunner> task_runner_;
|
|
|
|
|
2012-10-29 22:46:02 +01:00
|
|
|
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_;
|
|
|
|
|
2012-10-30 21:49:43 +01:00
|
|
|
// Used for globally tracked objects that are not associated with a particular
|
|
|
|
// context.
|
|
|
|
CefTrackManager global_manager_;
|
2013-01-03 18:24:24 +01:00
|
|
|
|
|
|
|
// True if the message listener has been registered.
|
|
|
|
bool message_listener_registered_;
|
|
|
|
|
|
|
|
// Attributes associated with WebWorker threads.
|
|
|
|
int worker_id_;
|
|
|
|
GURL worker_url_;
|
2012-10-29 22:46:02 +01:00
|
|
|
};
|
|
|
|
|
2013-01-03 18:24:24 +01:00
|
|
|
// Chromium uses the default Isolate for the main render process thread and a
|
|
|
|
// new Isolate for each WebWorker thread. Continue this pattern by tracking
|
|
|
|
// Isolate information on a per-thread basis. This implementation will need to
|
|
|
|
// be re-worked (perhaps using a map keyed on v8::Isolate::GetCurrent()) if
|
|
|
|
// in the future Chromium begins using the same Isolate across multiple threads.
|
|
|
|
class CefV8StateManager {
|
|
|
|
public:
|
|
|
|
CefV8StateManager() {
|
|
|
|
}
|
|
|
|
|
|
|
|
void CreateIsolateManager() {
|
|
|
|
DCHECK(!current_tls_.Get());
|
|
|
|
current_tls_.Set(new CefV8IsolateManager());
|
|
|
|
}
|
|
|
|
|
|
|
|
void DestroyIsolateManager() {
|
|
|
|
DCHECK(current_tls_.Get());
|
|
|
|
delete current_tls_.Get();
|
|
|
|
current_tls_.Set(NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
CefV8IsolateManager* GetIsolateManager() {
|
|
|
|
CefV8IsolateManager* manager = current_tls_.Get();
|
|
|
|
DCHECK(manager);
|
|
|
|
return manager;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
base::ThreadLocalPointer<CefV8IsolateManager> current_tls_;
|
|
|
|
};
|
|
|
|
|
|
|
|
base::LazyInstance<CefV8StateManager> g_v8_state = LAZY_INSTANCE_INITIALIZER;
|
|
|
|
|
|
|
|
CefV8IsolateManager* GetIsolateManager() {
|
|
|
|
return g_v8_state.Pointer()->GetIsolateManager();
|
|
|
|
}
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
class V8TrackObject : public CefTrackNode {
|
|
|
|
public:
|
2013-12-17 23:04:35 +01:00
|
|
|
explicit V8TrackObject(v8::Isolate* isolate)
|
|
|
|
: isolate_(isolate),
|
|
|
|
external_memory_(0) {
|
|
|
|
DCHECK(isolate_);
|
|
|
|
isolate_->AdjustAmountOfExternalAllocatedMemory(
|
2012-10-30 21:49:43 +01:00
|
|
|
static_cast<int>(sizeof(V8TrackObject)));
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
~V8TrackObject() {
|
2013-12-17 23:04:35 +01:00
|
|
|
isolate_->AdjustAmountOfExternalAllocatedMemory(
|
2012-10-30 21:49:43 +01:00
|
|
|
-static_cast<int>(sizeof(V8TrackObject)) - external_memory_);
|
2012-04-03 03:34:16 +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)
|
2013-12-17 23:04:35 +01:00
|
|
|
isolate_->AdjustAmountOfExternalAllocatedMemory(change_in_bytes);
|
2012-04-03 03:34:16 +02:00
|
|
|
external_memory_ = new_value;
|
|
|
|
|
|
|
|
return new_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void SetAccessor(CefRefPtr<CefV8Accessor> accessor) {
|
|
|
|
accessor_ = accessor;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline CefRefPtr<CefV8Accessor> GetAccessor() {
|
|
|
|
return accessor_;
|
|
|
|
}
|
2012-04-24 20:01:48 +02:00
|
|
|
|
2012-04-03 03:34:16 +02:00
|
|
|
inline void SetHandler(CefRefPtr<CefV8Handler> handler) {
|
|
|
|
handler_ = handler;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline CefRefPtr<CefV8Handler> GetHandler() {
|
|
|
|
return handler_;
|
|
|
|
}
|
2012-04-24 20:01:48 +02:00
|
|
|
|
2012-04-03 03:34:16 +02:00
|
|
|
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) {
|
2013-12-17 23:04:35 +01:00
|
|
|
object->SetHiddenValue(v8::String::NewFromUtf8(isolate_, kCefTrackObject),
|
|
|
|
v8::External::New(isolate_, this));
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Retrieve the track object for the specified V8 object.
|
2013-12-17 23:04:35 +01:00
|
|
|
static V8TrackObject* Unwrap(v8::Isolate* isolate,
|
|
|
|
v8::Handle<v8::Object> object) {
|
|
|
|
DCHECK(isolate);
|
2012-04-03 03:34:16 +02:00
|
|
|
v8::Local<v8::Value> value =
|
2013-12-17 23:04:35 +01:00
|
|
|
object->GetHiddenValue(
|
|
|
|
v8::String::NewFromUtf8(isolate, kCefTrackObject));
|
2012-04-03 03:34:16 +02:00
|
|
|
if (!value.IsEmpty())
|
2012-11-30 20:08:20 +01:00
|
|
|
return static_cast<V8TrackObject*>(v8::External::Cast(*value)->Value());
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2013-12-17 23:04:35 +01:00
|
|
|
v8::Isolate* isolate_;
|
2012-04-03 03:34:16 +02:00
|
|
|
CefRefPtr<CefV8Accessor> accessor_;
|
|
|
|
CefRefPtr<CefV8Handler> handler_;
|
|
|
|
CefRefPtr<CefBase> user_data_;
|
|
|
|
int external_memory_;
|
|
|
|
};
|
|
|
|
|
|
|
|
class V8TrackString : public CefTrackNode {
|
|
|
|
public:
|
|
|
|
explicit V8TrackString(const std::string& str) : string_(str) {}
|
|
|
|
const char* GetString() { return string_.c_str(); }
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::string string_;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2012-10-30 21:49:43 +01:00
|
|
|
// Manages the life span of a CefTrackNode associated with a persistent Object
|
|
|
|
// or Function.
|
|
|
|
class CefV8MakeWeakParam {
|
|
|
|
public:
|
2013-12-17 23:04:35 +01:00
|
|
|
CefV8MakeWeakParam(v8::Isolate* isolate,
|
|
|
|
scoped_refptr<CefV8ContextState> context_state,
|
2012-10-30 21:49:43 +01:00
|
|
|
CefTrackNode* object)
|
2013-12-17 23:04:35 +01:00
|
|
|
: isolate_(isolate),
|
|
|
|
context_state_(context_state),
|
2012-10-30 21:49:43 +01:00
|
|
|
object_(object) {
|
2013-12-17 23:04:35 +01:00
|
|
|
DCHECK(isolate_);
|
2012-10-30 21:49:43 +01:00
|
|
|
DCHECK(object_);
|
|
|
|
|
2013-12-17 23:04:35 +01:00
|
|
|
isolate_->AdjustAmountOfExternalAllocatedMemory(
|
2012-10-30 21:49:43 +01:00
|
|
|
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.
|
2013-01-03 18:24:24 +01:00
|
|
|
GetIsolateManager()->AddGlobalTrackObject(object_);
|
2012-10-30 21:49:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
~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 {
|
2013-01-03 18:24:24 +01:00
|
|
|
GetIsolateManager()->DeleteGlobalTrackObject(object_);
|
2012-10-30 21:49:43 +01:00
|
|
|
}
|
|
|
|
|
2013-12-17 23:04:35 +01:00
|
|
|
isolate_->AdjustAmountOfExternalAllocatedMemory(
|
2012-10-30 21:49:43 +01:00
|
|
|
-static_cast<int>(sizeof(CefV8MakeWeakParam)));
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2013-12-17 23:04:35 +01:00
|
|
|
v8::Isolate* isolate_;
|
2012-10-30 21:49:43 +01:00
|
|
|
scoped_refptr<CefV8ContextState> context_state_;
|
|
|
|
CefTrackNode* object_;
|
|
|
|
};
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
// Callback for weak persistent reference destruction.
|
2013-12-17 23:04:35 +01:00
|
|
|
void TrackDestructor(
|
|
|
|
const v8::WeakCallbackData<v8::Value, CefV8MakeWeakParam>& data) {
|
|
|
|
if (data.GetParameter())
|
|
|
|
delete data.GetParameter();
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
2012-10-29 22:46:02 +01:00
|
|
|
|
2012-04-03 03:34:16 +02:00
|
|
|
// Convert a CefString to a V8::String.
|
2013-12-17 23:04:35 +01:00
|
|
|
v8::Handle<v8::String> GetV8String(v8::Isolate* isolate,
|
|
|
|
const CefString& str) {
|
2012-04-03 03:34:16 +02:00
|
|
|
#if defined(CEF_STRING_TYPE_UTF16)
|
|
|
|
// Already a UTF16 string.
|
2013-12-17 23:04:35 +01:00
|
|
|
return v8::String::NewFromTwoByte(
|
|
|
|
isolate,
|
2012-04-03 03:34:16 +02:00
|
|
|
reinterpret_cast<uint16_t*>(
|
|
|
|
const_cast<CefString::char_type*>(str.c_str())),
|
2013-12-17 23:04:35 +01:00
|
|
|
v8::String::kNormalString,
|
2012-04-03 03:34:16 +02:00
|
|
|
str.length());
|
|
|
|
#elif defined(CEF_STRING_TYPE_UTF8)
|
|
|
|
// Already a UTF8 string.
|
2013-12-17 23:04:35 +01:00
|
|
|
return v8::String::NewFromUtf8(isolate,
|
|
|
|
const_cast<char*>(str.c_str()),
|
|
|
|
v8::String::kNormalString,
|
|
|
|
str.length());
|
2012-04-03 03:34:16 +02:00
|
|
|
#else
|
|
|
|
// Convert the string to UTF8.
|
|
|
|
std::string tmpStr = str;
|
2013-12-17 23:04:35 +01:00
|
|
|
return v8::String::NewFromUtf8(isolate,
|
|
|
|
tmpStr.c_str(),
|
|
|
|
v8::String::kNormalString,
|
|
|
|
tmpStr.length());
|
2012-04-03 03:34:16 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(CEF_STRING_TYPE_UTF16)
|
|
|
|
void v8impl_string_dtor(char16* str) {
|
2012-07-25 13:50:35 +02:00
|
|
|
delete [] str;
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
#elif defined(CEF_STRING_TYPE_UTF8)
|
|
|
|
void v8impl_string_dtor(char* str) {
|
2012-07-25 13:50:35 +02:00
|
|
|
delete [] str;
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Convert a v8::String to CefString.
|
|
|
|
void GetCefString(v8::Handle<v8::String> str, CefString& out) {
|
2012-07-25 13:50:35 +02:00
|
|
|
if (str.IsEmpty())
|
|
|
|
return;
|
|
|
|
|
2012-04-03 03:34:16 +02:00
|
|
|
#if defined(CEF_STRING_TYPE_WIDE)
|
|
|
|
// Allocate enough space for a worst-case conversion.
|
|
|
|
int len = str->Utf8Length();
|
2012-04-25 22:01:10 +02:00
|
|
|
if (len == 0)
|
|
|
|
return;
|
2012-04-03 03:34:16 +02:00
|
|
|
char* buf = new char[len + 1];
|
|
|
|
str->WriteUtf8(buf, len + 1);
|
|
|
|
|
|
|
|
// Perform conversion to the wide type.
|
|
|
|
cef_string_t* retws = out.GetWritableStruct();
|
|
|
|
cef_string_utf8_to_wide(buf, len, retws);
|
|
|
|
|
|
|
|
delete [] buf;
|
|
|
|
#else // !defined(CEF_STRING_TYPE_WIDE)
|
|
|
|
#if defined(CEF_STRING_TYPE_UTF16)
|
|
|
|
int len = str->Length();
|
2012-04-25 22:01:10 +02:00
|
|
|
if (len == 0)
|
|
|
|
return;
|
2012-04-03 03:34:16 +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:01:10 +02:00
|
|
|
if (len == 0)
|
|
|
|
return;
|
2012-04-03 03:34:16 +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;
|
|
|
|
#endif // !defined(CEF_STRING_TYPE_WIDE)
|
|
|
|
}
|
|
|
|
|
|
|
|
// V8 function callback.
|
2013-07-24 22:15:18 +02:00
|
|
|
void FunctionCallbackImpl(const v8::FunctionCallbackInfo<v8::Value>& info) {
|
2013-12-17 23:04:35 +01:00
|
|
|
v8::Isolate* isolate = info.GetIsolate();
|
2012-09-04 17:18:04 +02:00
|
|
|
WebCore::V8RecursionScope recursion_scope(
|
2013-12-17 23:04:35 +01:00
|
|
|
WebCore::toExecutionContext(isolate->GetCurrentContext()));
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
CefV8Handler* handler =
|
2013-07-24 22:15:18 +02:00
|
|
|
static_cast<CefV8Handler*>(v8::External::Cast(*info.Data())->Value());
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
CefV8ValueList params;
|
2013-07-24 22:15:18 +02:00
|
|
|
for (int i = 0; i < info.Length(); i++)
|
2013-12-17 23:04:35 +01:00
|
|
|
params.push_back(new CefV8ValueImpl(isolate, info[i]));
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
CefString func_name;
|
2013-07-24 22:15:18 +02:00
|
|
|
GetCefString(v8::Handle<v8::String>::Cast(info.Callee()->GetName()),
|
2012-04-03 03:34:16 +02:00
|
|
|
func_name);
|
2013-12-17 23:04:35 +01:00
|
|
|
CefRefPtr<CefV8Value> object = new CefV8ValueImpl(isolate, info.This());
|
2012-04-03 03:34:16 +02:00
|
|
|
CefRefPtr<CefV8Value> retval;
|
|
|
|
CefString exception;
|
|
|
|
|
|
|
|
if (handler->Execute(func_name, object, params, retval, exception)) {
|
|
|
|
if (!exception.empty()) {
|
2013-07-24 22:15:18 +02:00
|
|
|
info.GetReturnValue().Set(
|
2013-12-17 23:04:35 +01:00
|
|
|
isolate->ThrowException(
|
|
|
|
v8::Exception::Error(GetV8String(isolate, exception))));
|
2013-07-24 22:15:18 +02:00
|
|
|
return;
|
2012-04-03 03:34:16 +02:00
|
|
|
} else {
|
|
|
|
CefV8ValueImpl* rv = static_cast<CefV8ValueImpl*>(retval.get());
|
2013-07-24 22:15:18 +02:00
|
|
|
if (rv && rv->IsValid()) {
|
|
|
|
info.GetReturnValue().Set(rv->GetV8Value(true));
|
|
|
|
return;
|
|
|
|
}
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-24 22:15:18 +02:00
|
|
|
info.GetReturnValue().SetUndefined();
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// V8 Accessor callbacks
|
2013-07-24 22:15:18 +02:00
|
|
|
void AccessorGetterCallbackImpl(
|
|
|
|
v8::Local<v8::String> property,
|
|
|
|
const v8::PropertyCallbackInfo<v8::Value>& info) {
|
2013-12-17 23:04:35 +01:00
|
|
|
v8::Isolate* isolate = info.GetIsolate();
|
2012-09-04 17:18:04 +02:00
|
|
|
WebCore::V8RecursionScope recursion_scope(
|
2013-12-17 23:04:35 +01:00
|
|
|
WebCore::toExecutionContext(isolate->GetCurrentContext()));
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
v8::Handle<v8::Object> obj = info.This();
|
|
|
|
|
|
|
|
CefRefPtr<CefV8Accessor> accessorPtr;
|
|
|
|
|
2013-12-17 23:04:35 +01:00
|
|
|
V8TrackObject* tracker = V8TrackObject::Unwrap(isolate, obj);
|
2012-04-03 03:34:16 +02:00
|
|
|
if (tracker)
|
|
|
|
accessorPtr = tracker->GetAccessor();
|
|
|
|
|
|
|
|
if (accessorPtr.get()) {
|
|
|
|
CefRefPtr<CefV8Value> retval;
|
2013-12-17 23:04:35 +01:00
|
|
|
CefRefPtr<CefV8Value> object = new CefV8ValueImpl(isolate, obj);
|
2012-04-03 03:34:16 +02:00
|
|
|
CefString name, exception;
|
|
|
|
GetCefString(property, name);
|
|
|
|
if (accessorPtr->Get(name, object, retval, exception)) {
|
|
|
|
if (!exception.empty()) {
|
2013-07-24 22:15:18 +02:00
|
|
|
info.GetReturnValue().Set(
|
2013-12-17 23:04:35 +01:00
|
|
|
isolate->ThrowException(
|
|
|
|
v8::Exception::Error(GetV8String(isolate, exception))));
|
2013-07-24 22:15:18 +02:00
|
|
|
return;
|
2012-04-03 03:34:16 +02:00
|
|
|
} else {
|
|
|
|
CefV8ValueImpl* rv = static_cast<CefV8ValueImpl*>(retval.get());
|
2013-07-24 22:15:18 +02:00
|
|
|
if (rv && rv->IsValid()) {
|
|
|
|
info.GetReturnValue().Set(rv->GetV8Value(true));
|
|
|
|
return;
|
|
|
|
}
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-24 22:15:18 +02:00
|
|
|
return info.GetReturnValue().SetUndefined();
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
2013-07-24 22:15:18 +02:00
|
|
|
void AccessorSetterCallbackImpl(
|
|
|
|
v8::Local<v8::String> property,
|
|
|
|
v8::Local<v8::Value> value,
|
|
|
|
const v8::PropertyCallbackInfo<void>& info) {
|
2013-12-17 23:04:35 +01:00
|
|
|
v8::Isolate* isolate = info.GetIsolate();
|
2012-09-04 17:18:04 +02:00
|
|
|
WebCore::V8RecursionScope recursion_scope(
|
2013-12-17 23:04:35 +01:00
|
|
|
WebCore::toExecutionContext(isolate->GetCurrentContext()));
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
v8::Handle<v8::Object> obj = info.This();
|
|
|
|
|
|
|
|
CefRefPtr<CefV8Accessor> accessorPtr;
|
|
|
|
|
2013-12-17 23:04:35 +01:00
|
|
|
V8TrackObject* tracker = V8TrackObject::Unwrap(isolate, obj);
|
2012-04-03 03:34:16 +02:00
|
|
|
if (tracker)
|
|
|
|
accessorPtr = tracker->GetAccessor();
|
|
|
|
|
|
|
|
if (accessorPtr.get()) {
|
2013-12-17 23:04:35 +01:00
|
|
|
CefRefPtr<CefV8Value> object = new CefV8ValueImpl(isolate, obj);
|
|
|
|
CefRefPtr<CefV8Value> cefValue = new CefV8ValueImpl(isolate, value);
|
2012-04-03 03:34:16 +02:00
|
|
|
CefString name, exception;
|
|
|
|
GetCefString(property, name);
|
|
|
|
accessorPtr->Set(name, object, cefValue, exception);
|
|
|
|
if (!exception.empty()) {
|
2013-12-17 23:04:35 +01:00
|
|
|
isolate->ThrowException(
|
|
|
|
v8::Exception::Error(GetV8String(isolate, exception)));
|
2012-04-03 03:34:16 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-03 18:24:24 +01:00
|
|
|
v8::Local<v8::Value> CallV8Function(v8::Handle<v8::Context> context,
|
|
|
|
v8::Handle<v8::Function> function,
|
|
|
|
v8::Handle<v8::Object> receiver,
|
|
|
|
int argc,
|
2013-10-16 02:25:38 +02:00
|
|
|
v8::Handle<v8::Value> args[],
|
|
|
|
v8::Isolate* isolate) {
|
2013-01-03 18:24:24 +01:00
|
|
|
v8::Local<v8::Value> func_rv;
|
|
|
|
|
|
|
|
// Execute the function call using the ScriptController so that inspector
|
|
|
|
// instrumentation works.
|
|
|
|
if (CEF_CURRENTLY_ON_RT()) {
|
|
|
|
RefPtr<WebCore::Frame> frame = WebCore::toFrameIfNotDetached(context);
|
|
|
|
DCHECK(frame);
|
|
|
|
if (frame &&
|
2013-10-29 18:53:18 +01:00
|
|
|
frame->script().canExecuteScripts(WebCore::AboutToExecuteScript)) {
|
|
|
|
func_rv = frame->script().callFunction(function, receiver, argc, args);
|
2013-01-03 18:24:24 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
WebCore::WorkerScriptController* controller =
|
2014-02-05 21:35:45 +01:00
|
|
|
WebCore::WorkerScriptController::controllerForContext(isolate);
|
2013-01-03 18:24:24 +01:00
|
|
|
DCHECK(controller);
|
|
|
|
if (controller) {
|
2013-10-17 01:09:07 +02:00
|
|
|
func_rv = WebCore::ScriptController::callFunction(
|
2013-10-29 18:53:18 +01:00
|
|
|
controller->workerGlobalScope().executionContext(),
|
2013-10-16 02:25:38 +02:00
|
|
|
function, receiver, argc, args, isolate);
|
2013-01-03 18:24:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return func_rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-04-03 03:34:16 +02:00
|
|
|
// V8 extension registration.
|
|
|
|
|
|
|
|
class ExtensionWrapper : public v8::Extension {
|
|
|
|
public:
|
|
|
|
ExtensionWrapper(const char* extension_name,
|
|
|
|
const char* javascript_code,
|
|
|
|
CefV8Handler* handler)
|
|
|
|
: v8::Extension(extension_name, javascript_code), handler_(handler) {
|
|
|
|
}
|
|
|
|
|
2013-12-17 23:04:35 +01:00
|
|
|
virtual v8::Handle<v8::FunctionTemplate> GetNativeFunctionTemplate(
|
|
|
|
v8::Isolate* isolate,
|
|
|
|
v8::Handle<v8::String> name) OVERRIDE {
|
2012-04-03 03:34:16 +02:00
|
|
|
if (!handler_)
|
|
|
|
return v8::Handle<v8::FunctionTemplate>();
|
|
|
|
|
2013-12-17 23:04:35 +01:00
|
|
|
return v8::FunctionTemplate::New(isolate,
|
|
|
|
FunctionCallbackImpl,
|
|
|
|
v8::External::New(isolate, handler_));
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
CefV8Handler* handler_;
|
|
|
|
};
|
|
|
|
|
|
|
|
class CefV8ExceptionImpl : public CefV8Exception {
|
|
|
|
public:
|
|
|
|
explicit CefV8ExceptionImpl(v8::Handle<v8::Message> message)
|
|
|
|
: line_number_(0),
|
|
|
|
start_position_(0),
|
|
|
|
end_position_(0),
|
|
|
|
start_column_(0),
|
|
|
|
end_column_(0) {
|
|
|
|
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_; }
|
|
|
|
|
|
|
|
protected:
|
|
|
|
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);
|
|
|
|
};
|
|
|
|
|
2013-01-03 18:24:24 +01:00
|
|
|
void MessageListenerCallbackImpl(v8::Handle<v8::Message> message,
|
|
|
|
v8::Handle<v8::Value> data) {
|
|
|
|
CefRefPtr<CefApp> application = CefContentClient::Get()->application();
|
|
|
|
if (!application.get())
|
|
|
|
return;
|
|
|
|
|
|
|
|
CefRefPtr<CefRenderProcessHandler> handler =
|
|
|
|
application->GetRenderProcessHandler();
|
|
|
|
if (!handler.get())
|
|
|
|
return;
|
|
|
|
|
2013-12-17 23:04:35 +01:00
|
|
|
v8::Isolate* isolate = GetIsolateManager()->isolate();
|
2013-01-03 18:24:24 +01:00
|
|
|
CefRefPtr<CefV8Context> context = CefV8Context::GetCurrentContext();
|
|
|
|
v8::Handle<v8::StackTrace> v8Stack = message->GetStackTrace();
|
|
|
|
DCHECK(!v8Stack.IsEmpty());
|
2013-12-17 23:04:35 +01:00
|
|
|
CefRefPtr<CefV8StackTrace> stackTrace =
|
|
|
|
new CefV8StackTraceImpl(isolate, v8Stack);
|
2013-01-03 18:24:24 +01:00
|
|
|
|
|
|
|
CefRefPtr<CefV8Exception> exception = new CefV8ExceptionImpl(message);
|
|
|
|
|
|
|
|
if (CEF_CURRENTLY_ON_RT()) {
|
|
|
|
handler->OnUncaughtException(context->GetBrowser(), context->GetFrame(),
|
|
|
|
context, exception, stackTrace);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-03 03:34:16 +02:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
|
|
|
|
// Global functions.
|
|
|
|
|
2013-01-03 18:24:24 +01:00
|
|
|
void CefV8IsolateCreated() {
|
|
|
|
g_v8_state.Pointer()->CreateIsolateManager();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefV8IsolateDestroyed() {
|
|
|
|
g_v8_state.Pointer()->DestroyIsolateManager();
|
|
|
|
}
|
|
|
|
|
2012-10-29 22:46:02 +01:00
|
|
|
void CefV8ReleaseContext(v8::Handle<v8::Context> context) {
|
2013-01-03 18:24:24 +01:00
|
|
|
GetIsolateManager()->ReleaseContext(context);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefV8SetUncaughtExceptionStackSize(int stack_size) {
|
|
|
|
GetIsolateManager()->SetUncaughtExceptionStackSize(stack_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefV8SetWorkerAttributes(int worker_id, const GURL& worker_url) {
|
|
|
|
GetIsolateManager()->SetWorkerAttributes(worker_id, worker_url);
|
2012-10-29 22:46:02 +01:00
|
|
|
}
|
|
|
|
|
2012-04-03 03:34:16 +02:00
|
|
|
bool CefRegisterExtension(const CefString& extension_name,
|
|
|
|
const CefString& javascript_code,
|
|
|
|
CefRefPtr<CefV8Handler> handler) {
|
|
|
|
// Verify that this method was called on the correct thread.
|
|
|
|
CEF_REQUIRE_RT_RETURN(false);
|
|
|
|
|
2013-12-17 23:04:35 +01:00
|
|
|
CefV8IsolateManager* isolate_manager = GetIsolateManager();
|
|
|
|
|
2012-04-03 03:34:16 +02:00
|
|
|
V8TrackString* name = new V8TrackString(extension_name);
|
2013-12-17 23:04:35 +01:00
|
|
|
isolate_manager->AddGlobalTrackObject(name);
|
2012-04-03 03:34:16 +02:00
|
|
|
V8TrackString* code = new V8TrackString(javascript_code);
|
2013-12-17 23:04:35 +01:00
|
|
|
isolate_manager->AddGlobalTrackObject(code);
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2013-12-17 23:04:35 +01:00
|
|
|
if (handler) {
|
|
|
|
// The reference will be released when the process exits.
|
|
|
|
V8TrackObject* object = new V8TrackObject(isolate_manager->isolate());
|
|
|
|
object->SetHandler(handler);
|
|
|
|
isolate_manager->AddGlobalTrackObject(object);
|
|
|
|
}
|
|
|
|
|
|
|
|
ExtensionWrapper* wrapper = new ExtensionWrapper(
|
|
|
|
name->GetString(),
|
|
|
|
code->GetString(),
|
|
|
|
handler.get());
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
content::RenderThread::Get()->RegisterExtension(wrapper);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-12-30 22:16:59 +01:00
|
|
|
// Helper macros
|
|
|
|
|
2013-01-03 18:24:24 +01:00
|
|
|
#define CEF_V8_HAS_ISOLATE() (!!GetIsolateManager())
|
2012-12-30 22:16:59 +01:00
|
|
|
#define CEF_V8_REQUIRE_ISOLATE_RETURN(var) \
|
|
|
|
if (!CEF_V8_HAS_ISOLATE()) { \
|
|
|
|
NOTREACHED() << "V8 isolate is not valid"; \
|
|
|
|
return var; \
|
|
|
|
}
|
|
|
|
|
2013-06-27 00:33:44 +02:00
|
|
|
#define CEF_V8_CURRENTLY_ON_MLT() \
|
|
|
|
(!handle_.get() || handle_->BelongsToCurrentThread())
|
2012-12-30 22:16:59 +01:00
|
|
|
#define CEF_V8_REQUIRE_MLT_RETURN(var) \
|
|
|
|
CEF_V8_REQUIRE_ISOLATE_RETURN(var); \
|
|
|
|
if (!CEF_V8_CURRENTLY_ON_MLT()) { \
|
|
|
|
NOTREACHED() << "called on incorrect thread"; \
|
|
|
|
return var; \
|
|
|
|
}
|
|
|
|
|
2013-06-27 00:33:44 +02:00
|
|
|
#define CEF_V8_HANDLE_IS_VALID() (handle_.get() && handle_->IsValid())
|
|
|
|
#define CEF_V8_REQUIRE_VALID_HANDLE_RETURN(ret) \
|
2012-12-30 22:16:59 +01:00
|
|
|
CEF_V8_REQUIRE_MLT_RETURN(ret); \
|
|
|
|
if (!CEF_V8_HANDLE_IS_VALID()) { \
|
|
|
|
NOTREACHED() << "V8 handle is not valid"; \
|
|
|
|
return ret; \
|
|
|
|
}
|
|
|
|
|
|
|
|
#define CEF_V8_IS_VALID() \
|
|
|
|
(CEF_V8_HAS_ISOLATE() && \
|
|
|
|
CEF_V8_CURRENTLY_ON_MLT() && \
|
|
|
|
CEF_V8_HANDLE_IS_VALID())
|
|
|
|
|
|
|
|
#define CEF_V8_REQUIRE_OBJECT_RETURN(ret) \
|
2013-06-27 00:33:44 +02:00
|
|
|
CEF_V8_REQUIRE_VALID_HANDLE_RETURN(ret); \
|
|
|
|
if (type_ != TYPE_OBJECT) { \
|
2012-12-30 22:16:59 +01:00
|
|
|
NOTREACHED() << "V8 value is not an object"; \
|
|
|
|
return ret; \
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-10-29 22:46:02 +01:00
|
|
|
// CefV8HandleBase
|
|
|
|
|
2012-12-30 22:16:59 +01:00
|
|
|
CefV8HandleBase::~CefV8HandleBase() {
|
|
|
|
DCHECK(BelongsToCurrentThread());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CefV8HandleBase::BelongsToCurrentThread() const {
|
2013-01-03 18:24:24 +01:00
|
|
|
return task_runner_->RunsTasksOnCurrentThread();
|
2012-12-30 22:16:59 +01:00
|
|
|
}
|
|
|
|
|
2013-12-17 23:04:35 +01:00
|
|
|
CefV8HandleBase::CefV8HandleBase(v8::Isolate* isolate,
|
|
|
|
v8::Handle<v8::Context> context)
|
|
|
|
: isolate_(isolate) {
|
|
|
|
DCHECK(isolate_);
|
|
|
|
|
2013-01-03 18:24:24 +01:00
|
|
|
CefV8IsolateManager* manager = GetIsolateManager();
|
|
|
|
DCHECK(manager);
|
2013-12-17 23:04:35 +01:00
|
|
|
DCHECK_EQ(isolate_, manager->isolate());
|
2013-03-12 21:23:24 +01:00
|
|
|
|
2013-01-03 18:24:24 +01:00
|
|
|
task_runner_ = manager->task_runner();
|
|
|
|
context_state_ = manager->GetContextState(context);
|
2012-10-29 22:46:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-25 13:50:35 +02:00
|
|
|
// CefV8Context
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
// static
|
|
|
|
CefRefPtr<CefV8Context> CefV8Context::GetCurrentContext() {
|
|
|
|
CefRefPtr<CefV8Context> context;
|
2012-12-30 22:16:59 +01:00
|
|
|
CEF_V8_REQUIRE_ISOLATE_RETURN(context);
|
2013-12-17 23:04:35 +01:00
|
|
|
v8::Isolate* isolate = GetIsolateManager()->isolate();
|
|
|
|
if (isolate->InContext()) {
|
|
|
|
v8::HandleScope handle_scope(isolate);
|
|
|
|
context = new CefV8ContextImpl(isolate, isolate->GetCurrentContext());
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
return context;
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
CefRefPtr<CefV8Context> CefV8Context::GetEnteredContext() {
|
|
|
|
CefRefPtr<CefV8Context> context;
|
2012-12-30 22:16:59 +01:00
|
|
|
CEF_V8_REQUIRE_ISOLATE_RETURN(context);
|
2013-12-17 23:04:35 +01:00
|
|
|
v8::Isolate* isolate = GetIsolateManager()->isolate();
|
|
|
|
if (isolate->InContext()) {
|
|
|
|
v8::HandleScope handle_scope(isolate);
|
|
|
|
context = new CefV8ContextImpl(isolate, isolate->GetEnteredContext());
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
return context;
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
bool CefV8Context::InContext() {
|
2012-12-30 22:16:59 +01:00
|
|
|
CEF_V8_REQUIRE_ISOLATE_RETURN(false);
|
2013-12-17 23:04:35 +01:00
|
|
|
v8::Isolate* isolate = GetIsolateManager()->isolate();
|
|
|
|
return isolate->InContext();
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-25 13:50:35 +02:00
|
|
|
// CefV8ContextImpl
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2013-12-17 23:04:35 +01:00
|
|
|
CefV8ContextImpl::CefV8ContextImpl(v8::Isolate* isolate,
|
|
|
|
v8::Handle<v8::Context> context)
|
|
|
|
: handle_(new Handle(isolate, context, context))
|
2012-04-03 03:34:16 +02:00
|
|
|
#ifndef NDEBUG
|
2012-07-25 13:50:35 +02:00
|
|
|
, enter_count_(0)
|
2012-04-03 03:34:16 +02:00
|
|
|
#endif
|
|
|
|
{ // NOLINT(whitespace/braces)
|
|
|
|
}
|
|
|
|
|
|
|
|
CefV8ContextImpl::~CefV8ContextImpl() {
|
|
|
|
DLOG_ASSERT(0 == enter_count_);
|
|
|
|
}
|
|
|
|
|
2013-01-03 18:24:24 +01:00
|
|
|
CefRefPtr<CefTaskRunner> CefV8ContextImpl::GetTaskRunner() {
|
|
|
|
return new CefTaskRunnerImpl(handle_->task_runner());
|
|
|
|
}
|
|
|
|
|
2012-10-29 22:46:02 +01:00
|
|
|
bool CefV8ContextImpl::IsValid() {
|
2012-12-30 22:16:59 +01:00
|
|
|
return CEF_V8_IS_VALID();
|
2012-10-29 22:46:02 +01:00
|
|
|
}
|
|
|
|
|
2012-04-03 03:34:16 +02:00
|
|
|
CefRefPtr<CefBrowser> CefV8ContextImpl::GetBrowser() {
|
|
|
|
CefRefPtr<CefBrowser> browser;
|
2013-06-27 00:33:44 +02:00
|
|
|
CEF_V8_REQUIRE_VALID_HANDLE_RETURN(browser);
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2013-01-03 18:24:24 +01:00
|
|
|
// Return NULL for WebWorkers.
|
|
|
|
if (!CEF_CURRENTLY_ON_RT())
|
|
|
|
return browser;
|
|
|
|
|
2013-11-08 22:28:56 +01:00
|
|
|
blink::WebFrame* webframe = GetWebFrame();
|
2012-04-03 03:34:16 +02:00
|
|
|
if (webframe)
|
|
|
|
browser = CefBrowserImpl::GetBrowserForMainFrame(webframe->top());
|
|
|
|
|
|
|
|
return browser;
|
|
|
|
}
|
|
|
|
|
|
|
|
CefRefPtr<CefFrame> CefV8ContextImpl::GetFrame() {
|
|
|
|
CefRefPtr<CefFrame> frame;
|
2013-06-27 00:33:44 +02:00
|
|
|
CEF_V8_REQUIRE_VALID_HANDLE_RETURN(frame);
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2013-01-03 18:24:24 +01:00
|
|
|
// Return NULL for WebWorkers.
|
|
|
|
if (!CEF_CURRENTLY_ON_RT())
|
|
|
|
return frame;
|
|
|
|
|
2013-11-08 22:28:56 +01:00
|
|
|
blink::WebFrame* webframe = GetWebFrame();
|
2012-04-03 03:34:16 +02:00
|
|
|
if (webframe) {
|
|
|
|
CefRefPtr<CefBrowserImpl> browser =
|
|
|
|
CefBrowserImpl::GetBrowserForMainFrame(webframe->top());
|
|
|
|
frame = browser->GetFrame(webframe->identifier());
|
|
|
|
}
|
|
|
|
|
|
|
|
return frame;
|
|
|
|
}
|
|
|
|
|
|
|
|
CefRefPtr<CefV8Value> CefV8ContextImpl::GetGlobal() {
|
2013-06-27 00:33:44 +02:00
|
|
|
CEF_V8_REQUIRE_VALID_HANDLE_RETURN(NULL);
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2013-12-17 23:04:35 +01:00
|
|
|
v8::Isolate* isolate = handle_->isolate();
|
|
|
|
v8::HandleScope handle_scope(isolate);
|
2013-06-27 00:33:44 +02:00
|
|
|
v8::Handle<v8::Context> context = GetV8Context();
|
|
|
|
v8::Context::Scope context_scope(context);
|
2013-12-17 23:04:35 +01:00
|
|
|
return new CefV8ValueImpl(isolate, context->Global());
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CefV8ContextImpl::Enter() {
|
2013-06-27 00:33:44 +02:00
|
|
|
CEF_V8_REQUIRE_VALID_HANDLE_RETURN(false);
|
|
|
|
|
2013-10-16 02:25:38 +02:00
|
|
|
v8::HandleScope handle_scope(handle_->isolate());
|
2012-10-29 22:46:02 +01:00
|
|
|
|
2012-09-26 19:41:44 +02:00
|
|
|
WebCore::V8PerIsolateData::current()->incrementRecursionLevel();
|
2013-06-27 00:33:44 +02:00
|
|
|
handle_->GetNewV8Handle()->Enter();
|
2012-04-03 03:34:16 +02:00
|
|
|
#ifndef NDEBUG
|
|
|
|
++enter_count_;
|
|
|
|
#endif
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CefV8ContextImpl::Exit() {
|
2013-06-27 00:33:44 +02:00
|
|
|
CEF_V8_REQUIRE_VALID_HANDLE_RETURN(false);
|
|
|
|
|
2013-10-16 02:25:38 +02:00
|
|
|
v8::HandleScope handle_scope(handle_->isolate());
|
2012-10-29 22:46:02 +01:00
|
|
|
|
2012-04-03 03:34:16 +02:00
|
|
|
DLOG_ASSERT(enter_count_ > 0);
|
2013-06-27 00:33:44 +02:00
|
|
|
handle_->GetNewV8Handle()->Exit();
|
2012-09-26 19:41:44 +02:00
|
|
|
WebCore::V8PerIsolateData::current()->decrementRecursionLevel();
|
2012-04-03 03:34:16 +02:00
|
|
|
#ifndef NDEBUG
|
|
|
|
--enter_count_;
|
|
|
|
#endif
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CefV8ContextImpl::IsSame(CefRefPtr<CefV8Context> that) {
|
2013-06-27 00:33:44 +02:00
|
|
|
CEF_V8_REQUIRE_VALID_HANDLE_RETURN(false);
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
CefV8ContextImpl* impl = static_cast<CefV8ContextImpl*>(that.get());
|
2013-06-27 00:33:44 +02:00
|
|
|
if (!impl || !impl->IsValid())
|
|
|
|
return false;
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2013-06-27 00:33:44 +02:00
|
|
|
return (handle_->GetPersistentV8Handle() ==
|
|
|
|
impl->handle_->GetPersistentV8Handle());
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CefV8ContextImpl::Eval(const CefString& code,
|
|
|
|
CefRefPtr<CefV8Value>& retval,
|
|
|
|
CefRefPtr<CefV8Exception>& exception) {
|
2013-06-27 00:33:44 +02:00
|
|
|
CEF_V8_REQUIRE_VALID_HANDLE_RETURN(false);
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
if (code.empty()) {
|
|
|
|
NOTREACHED() << "invalid input parameter";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-12-17 23:04:35 +01:00
|
|
|
v8::Isolate* isolate = handle_->isolate();
|
|
|
|
v8::HandleScope handle_scope(isolate);
|
2013-06-27 00:33:44 +02:00
|
|
|
v8::Local<v8::Context> context = GetV8Context();
|
2013-01-03 18:24:24 +01:00
|
|
|
v8::Context::Scope context_scope(context);
|
|
|
|
v8::Local<v8::Object> obj = context->Global();
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
// Retrieve the eval function.
|
2013-12-17 23:04:35 +01:00
|
|
|
v8::Local<v8::Value> val = obj->Get(v8::String::NewFromUtf8(isolate, "eval"));
|
2012-04-03 03:34:16 +02:00
|
|
|
if (val.IsEmpty() || !val->IsFunction())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
v8::Local<v8::Function> func = v8::Local<v8::Function>::Cast(val);
|
2013-12-17 23:04:35 +01:00
|
|
|
v8::Handle<v8::Value> code_val = GetV8String(isolate, code);
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
v8::TryCatch try_catch;
|
2012-04-24 20:01:48 +02:00
|
|
|
try_catch.SetVerbose(true);
|
|
|
|
|
|
|
|
retval = NULL;
|
|
|
|
exception = NULL;
|
|
|
|
|
2013-01-03 18:24:24 +01:00
|
|
|
v8::Local<v8::Value> func_rv =
|
2013-10-16 02:25:38 +02:00
|
|
|
CallV8Function(context, func, obj, 1, &code_val, handle_->isolate());
|
2012-04-24 20:01:48 +02:00
|
|
|
|
2012-04-03 03:34:16 +02:00
|
|
|
if (try_catch.HasCaught()) {
|
|
|
|
exception = new CefV8ExceptionImpl(try_catch.Message());
|
|
|
|
return false;
|
2012-04-24 20:01:48 +02:00
|
|
|
} else if (!func_rv.IsEmpty()) {
|
2013-12-17 23:04:35 +01:00
|
|
|
retval = new CefV8ValueImpl(isolate, func_rv);
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
2012-04-24 20:01:48 +02:00
|
|
|
return true;
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
2013-06-27 00:33:44 +02:00
|
|
|
v8::Handle<v8::Context> CefV8ContextImpl::GetV8Context() {
|
|
|
|
return handle_->GetNewV8Handle();
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
2013-11-08 22:28:56 +01:00
|
|
|
blink::WebFrame* CefV8ContextImpl::GetWebFrame() {
|
2013-01-03 18:24:24 +01:00
|
|
|
CEF_REQUIRE_RT();
|
2013-10-16 02:25:38 +02:00
|
|
|
v8::HandleScope handle_scope(handle_->isolate());
|
2013-06-27 00:33:44 +02:00
|
|
|
v8::Context::Scope context_scope(GetV8Context());
|
2013-11-08 22:28:56 +01:00
|
|
|
blink::WebFrame* frame = blink::WebFrame::frameForCurrentContext();
|
2012-04-03 03:34:16 +02:00
|
|
|
return frame;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-25 13:50:35 +02:00
|
|
|
// CefV8ValueImpl::Handle
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2012-07-25 13:50:35 +02:00
|
|
|
CefV8ValueImpl::Handle::~Handle() {
|
2013-03-12 21:23:24 +01:00
|
|
|
// Persist the handle (call MakeWeak) if:
|
|
|
|
// A. The handle has been passed into a V8 function or used as a return value
|
2012-10-30 21:49:43 +01:00
|
|
|
// from a V8 callback, and
|
2013-03-12 21:23:24 +01:00
|
|
|
// B. The associated context, if any, is still valid.
|
|
|
|
if (should_persist_ && (!context_state_.get() || context_state_->IsValid())) {
|
2013-12-17 23:04:35 +01:00
|
|
|
handle_.SetWeak(
|
|
|
|
(tracker_ ?
|
|
|
|
new CefV8MakeWeakParam(isolate(), context_state_, tracker_) : NULL),
|
2013-03-12 21:23:24 +01:00
|
|
|
TrackDestructor);
|
2014-01-02 23:41:11 +01:00
|
|
|
} else if (tracker_) {
|
|
|
|
delete tracker_;
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
2014-01-02 23:41:11 +01:00
|
|
|
|
|
|
|
// Always call Reset() on a persistent handle to avoid the
|
|
|
|
// CHECK(state() != NEAR_DEATH) in V8's PostGarbageCollectionProcessing.
|
|
|
|
handle_.Reset();
|
2012-04-03 03:34:16 +02:00
|
|
|
tracker_ = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// CefV8Value
|
|
|
|
|
|
|
|
// static
|
|
|
|
CefRefPtr<CefV8Value> CefV8Value::CreateUndefined() {
|
2012-12-30 22:16:59 +01:00
|
|
|
CEF_V8_REQUIRE_ISOLATE_RETURN(NULL);
|
2013-12-17 23:04:35 +01:00
|
|
|
v8::Isolate* isolate = GetIsolateManager()->isolate();
|
|
|
|
CefRefPtr<CefV8ValueImpl> impl = new CefV8ValueImpl(isolate);
|
2013-06-27 00:33:44 +02:00
|
|
|
impl->InitUndefined();
|
|
|
|
return impl.get();
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
CefRefPtr<CefV8Value> CefV8Value::CreateNull() {
|
2012-12-30 22:16:59 +01:00
|
|
|
CEF_V8_REQUIRE_ISOLATE_RETURN(NULL);
|
2013-12-17 23:04:35 +01:00
|
|
|
v8::Isolate* isolate = GetIsolateManager()->isolate();
|
|
|
|
CefRefPtr<CefV8ValueImpl> impl = new CefV8ValueImpl(isolate);
|
2013-06-27 00:33:44 +02:00
|
|
|
impl->InitNull();
|
|
|
|
return impl.get();
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
CefRefPtr<CefV8Value> CefV8Value::CreateBool(bool value) {
|
2012-12-30 22:16:59 +01:00
|
|
|
CEF_V8_REQUIRE_ISOLATE_RETURN(NULL);
|
2013-12-17 23:04:35 +01:00
|
|
|
v8::Isolate* isolate = GetIsolateManager()->isolate();
|
|
|
|
CefRefPtr<CefV8ValueImpl> impl = new CefV8ValueImpl(isolate);
|
2013-06-27 00:33:44 +02:00
|
|
|
impl->InitBool(value);
|
|
|
|
return impl.get();
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
CefRefPtr<CefV8Value> CefV8Value::CreateInt(int32 value) {
|
2012-12-30 22:16:59 +01:00
|
|
|
CEF_V8_REQUIRE_ISOLATE_RETURN(NULL);
|
2013-12-17 23:04:35 +01:00
|
|
|
v8::Isolate* isolate = GetIsolateManager()->isolate();
|
|
|
|
CefRefPtr<CefV8ValueImpl> impl = new CefV8ValueImpl(isolate);
|
2013-06-27 00:33:44 +02:00
|
|
|
impl->InitInt(value);
|
|
|
|
return impl.get();
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
CefRefPtr<CefV8Value> CefV8Value::CreateUInt(uint32 value) {
|
2012-12-30 22:16:59 +01:00
|
|
|
CEF_V8_REQUIRE_ISOLATE_RETURN(NULL);
|
2013-12-17 23:04:35 +01:00
|
|
|
v8::Isolate* isolate = GetIsolateManager()->isolate();
|
|
|
|
CefRefPtr<CefV8ValueImpl> impl = new CefV8ValueImpl(isolate);
|
2013-06-27 00:33:44 +02:00
|
|
|
impl->InitUInt(value);
|
|
|
|
return impl.get();
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
CefRefPtr<CefV8Value> CefV8Value::CreateDouble(double value) {
|
2012-12-30 22:16:59 +01:00
|
|
|
CEF_V8_REQUIRE_ISOLATE_RETURN(NULL);
|
2013-12-17 23:04:35 +01:00
|
|
|
v8::Isolate* isolate = GetIsolateManager()->isolate();
|
|
|
|
CefRefPtr<CefV8ValueImpl> impl = new CefV8ValueImpl(isolate);
|
2013-06-27 00:33:44 +02:00
|
|
|
impl->InitDouble(value);
|
|
|
|
return impl.get();
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
2013-06-27 00:33:44 +02:00
|
|
|
CefRefPtr<CefV8Value> CefV8Value::CreateDate(const CefTime& value) {
|
2012-12-30 22:16:59 +01:00
|
|
|
CEF_V8_REQUIRE_ISOLATE_RETURN(NULL);
|
2013-12-17 23:04:35 +01:00
|
|
|
v8::Isolate* isolate = GetIsolateManager()->isolate();
|
|
|
|
CefRefPtr<CefV8ValueImpl> impl = new CefV8ValueImpl(isolate);
|
2013-06-27 00:33:44 +02:00
|
|
|
impl->InitDate(value);
|
|
|
|
return impl.get();
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
CefRefPtr<CefV8Value> CefV8Value::CreateString(const CefString& value) {
|
2012-12-30 22:16:59 +01:00
|
|
|
CEF_V8_REQUIRE_ISOLATE_RETURN(NULL);
|
2013-12-17 23:04:35 +01:00
|
|
|
v8::Isolate* isolate = GetIsolateManager()->isolate();
|
|
|
|
CefRefPtr<CefV8ValueImpl> impl = new CefV8ValueImpl(isolate);
|
2013-06-27 00:33:44 +02:00
|
|
|
CefString str(value);
|
|
|
|
impl->InitString(str);
|
|
|
|
return impl.get();
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
CefRefPtr<CefV8Value> CefV8Value::CreateObject(
|
|
|
|
CefRefPtr<CefV8Accessor> accessor) {
|
2012-12-30 22:16:59 +01:00
|
|
|
CEF_V8_REQUIRE_ISOLATE_RETURN(NULL);
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2013-12-17 23:04:35 +01:00
|
|
|
v8::Isolate* isolate = GetIsolateManager()->isolate();
|
|
|
|
v8::HandleScope handle_scope(isolate);
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2013-12-17 23:04:35 +01:00
|
|
|
v8::Local<v8::Context> context = isolate->GetCurrentContext();
|
2012-04-03 03:34:16 +02:00
|
|
|
if (context.IsEmpty()) {
|
|
|
|
NOTREACHED() << "not currently in a V8 context";
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the new V8 object.
|
2014-02-05 21:35:45 +01:00
|
|
|
v8::Local<v8::Object> obj = v8::Object::New(isolate);
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
// Create a tracker object that will cause the user data and/or accessor
|
|
|
|
// reference to be released when the V8 object is destroyed.
|
2013-12-17 23:04:35 +01:00
|
|
|
V8TrackObject* tracker = new V8TrackObject(isolate);
|
2012-04-03 03:34:16 +02:00
|
|
|
tracker->SetAccessor(accessor);
|
|
|
|
|
|
|
|
// Attach the tracker object.
|
|
|
|
tracker->AttachTo(obj);
|
|
|
|
|
2013-12-17 23:04:35 +01:00
|
|
|
CefRefPtr<CefV8ValueImpl> impl = new CefV8ValueImpl(isolate);
|
2013-06-27 00:33:44 +02:00
|
|
|
impl->InitObject(obj, tracker);
|
|
|
|
return impl.get();
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
CefRefPtr<CefV8Value> CefV8Value::CreateArray(int length) {
|
2012-12-30 22:16:59 +01:00
|
|
|
CEF_V8_REQUIRE_ISOLATE_RETURN(NULL);
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2013-12-17 23:04:35 +01:00
|
|
|
v8::Isolate* isolate = GetIsolateManager()->isolate();
|
|
|
|
v8::HandleScope handle_scope(isolate);
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2013-12-17 23:04:35 +01:00
|
|
|
v8::Local<v8::Context> context = isolate->GetCurrentContext();
|
2012-04-03 03:34:16 +02:00
|
|
|
if (context.IsEmpty()) {
|
|
|
|
NOTREACHED() << "not currently in a V8 context";
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a tracker object that will cause the user data reference to be
|
|
|
|
// released when the V8 object is destroyed.
|
2013-12-17 23:04:35 +01:00
|
|
|
V8TrackObject* tracker = new V8TrackObject(isolate);
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
// Create the new V8 array.
|
2013-12-17 23:04:35 +01:00
|
|
|
v8::Local<v8::Array> arr = v8::Array::New(isolate, length);
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
// Attach the tracker object.
|
|
|
|
tracker->AttachTo(arr);
|
|
|
|
|
2013-12-17 23:04:35 +01:00
|
|
|
CefRefPtr<CefV8ValueImpl> impl = new CefV8ValueImpl(isolate);
|
2013-06-27 00:33:44 +02:00
|
|
|
impl->InitObject(arr, tracker);
|
|
|
|
return impl.get();
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
CefRefPtr<CefV8Value> CefV8Value::CreateFunction(
|
|
|
|
const CefString& name,
|
|
|
|
CefRefPtr<CefV8Handler> handler) {
|
2012-12-30 22:16:59 +01:00
|
|
|
CEF_V8_REQUIRE_ISOLATE_RETURN(NULL);
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
if (!handler.get()) {
|
|
|
|
NOTREACHED() << "invalid parameter";
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-12-17 23:04:35 +01:00
|
|
|
v8::Isolate* isolate = GetIsolateManager()->isolate();
|
|
|
|
v8::HandleScope handle_scope(isolate);
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2013-12-17 23:04:35 +01:00
|
|
|
v8::Local<v8::Context> context = isolate->GetCurrentContext();
|
2012-04-03 03:34:16 +02:00
|
|
|
if (context.IsEmpty()) {
|
|
|
|
NOTREACHED() << "not currently in a V8 context";
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a new V8 function template.
|
2013-12-17 23:04:35 +01:00
|
|
|
v8::Local<v8::FunctionTemplate> tmpl = v8::FunctionTemplate::New(isolate);
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2013-12-17 23:04:35 +01:00
|
|
|
v8::Local<v8::Value> data = v8::External::New(isolate, handler.get());
|
2012-04-03 03:34:16 +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();
|
|
|
|
if (func.IsEmpty()) {
|
|
|
|
NOTREACHED() << "failed to create V8 function";
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-12-17 23:04:35 +01:00
|
|
|
func->SetName(GetV8String(isolate, name));
|
2012-04-03 03:34:16 +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.
|
2013-12-17 23:04:35 +01:00
|
|
|
V8TrackObject* tracker = new V8TrackObject(isolate);
|
2012-04-03 03:34:16 +02:00
|
|
|
tracker->SetHandler(handler);
|
|
|
|
|
|
|
|
// Attach the tracker object.
|
|
|
|
tracker->AttachTo(func);
|
|
|
|
|
|
|
|
// Create the CefV8ValueImpl and provide a tracker object that will cause
|
|
|
|
// the handler reference to be released when the V8 object is destroyed.
|
2013-12-17 23:04:35 +01:00
|
|
|
CefRefPtr<CefV8ValueImpl> impl = new CefV8ValueImpl(isolate);
|
2013-06-27 00:33:44 +02:00
|
|
|
impl->InitObject(func, tracker);
|
|
|
|
return impl.get();
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// CefV8ValueImpl
|
|
|
|
|
2013-12-17 23:04:35 +01:00
|
|
|
CefV8ValueImpl::CefV8ValueImpl(v8::Isolate* isolate)
|
|
|
|
: isolate_(isolate),
|
|
|
|
type_(TYPE_INVALID),
|
2012-07-25 13:50:35 +02:00
|
|
|
rethrow_exceptions_(false) {
|
2013-12-17 23:04:35 +01:00
|
|
|
DCHECK(isolate_);
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
2013-12-17 23:04:35 +01:00
|
|
|
CefV8ValueImpl::CefV8ValueImpl(v8::Isolate* isolate,
|
|
|
|
v8::Handle<v8::Value> value)
|
|
|
|
: isolate_(isolate),
|
|
|
|
type_(TYPE_INVALID),
|
2013-06-27 00:33:44 +02:00
|
|
|
rethrow_exceptions_(false) {
|
2013-12-17 23:04:35 +01:00
|
|
|
DCHECK(isolate_);
|
2013-06-27 00:33:44 +02:00
|
|
|
InitFromV8Value(value);
|
|
|
|
}
|
|
|
|
|
2012-04-03 03:34:16 +02:00
|
|
|
CefV8ValueImpl::~CefV8ValueImpl() {
|
2013-06-27 00:33:44 +02:00
|
|
|
if (type_ == TYPE_STRING)
|
|
|
|
cef_string_clear(&string_value_);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefV8ValueImpl::InitFromV8Value(v8::Handle<v8::Value> value) {
|
|
|
|
if (value->IsUndefined()) {
|
|
|
|
InitUndefined();
|
|
|
|
} else if (value->IsNull()) {
|
|
|
|
InitNull();
|
|
|
|
} else if (value->IsTrue()) {
|
|
|
|
InitBool(true);
|
|
|
|
} else if (value->IsFalse()) {
|
|
|
|
InitBool(false);
|
|
|
|
} else if (value->IsBoolean()) {
|
|
|
|
InitBool(value->ToBoolean()->Value());
|
|
|
|
} else if (value->IsInt32()) {
|
|
|
|
InitInt(value->ToInt32()->Value());
|
|
|
|
} else if (value->IsUint32()) {
|
|
|
|
InitUInt(value->ToUint32()->Value());
|
|
|
|
} else if (value->IsNumber()) {
|
|
|
|
InitDouble(value->ToNumber()->Value());
|
|
|
|
} else if (value->IsDate()) {
|
|
|
|
// Convert from milliseconds to seconds.
|
|
|
|
InitDate(CefTime(value->ToNumber()->Value() / 1000));
|
|
|
|
} else if (value->IsString()) {
|
|
|
|
CefString rv;
|
|
|
|
GetCefString(value->ToString(), rv);
|
|
|
|
InitString(rv);
|
|
|
|
} else if (value->IsObject()) {
|
|
|
|
InitObject(value, NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefV8ValueImpl::InitUndefined() {
|
|
|
|
DCHECK_EQ(type_, TYPE_INVALID);
|
|
|
|
type_ = TYPE_UNDEFINED;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefV8ValueImpl::InitNull() {
|
|
|
|
DCHECK_EQ(type_, TYPE_INVALID);
|
|
|
|
type_ = TYPE_NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefV8ValueImpl::InitBool(bool value) {
|
|
|
|
DCHECK_EQ(type_, TYPE_INVALID);
|
|
|
|
type_ = TYPE_BOOL;
|
|
|
|
bool_value_ = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefV8ValueImpl::InitInt(int32 value) {
|
|
|
|
DCHECK_EQ(type_, TYPE_INVALID);
|
|
|
|
type_ = TYPE_INT;
|
|
|
|
int_value_ = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefV8ValueImpl::InitUInt(uint32 value) {
|
|
|
|
DCHECK_EQ(type_, TYPE_INVALID);
|
|
|
|
type_ = TYPE_UINT;
|
|
|
|
uint_value_ = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefV8ValueImpl::InitDouble(double value) {
|
|
|
|
DCHECK_EQ(type_, TYPE_INVALID);
|
|
|
|
type_ = TYPE_DOUBLE;
|
|
|
|
double_value_ = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefV8ValueImpl::InitDate(const CefTime& value) {
|
|
|
|
DCHECK_EQ(type_, TYPE_INVALID);
|
|
|
|
type_ = TYPE_DATE;
|
|
|
|
date_value_ = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefV8ValueImpl::InitString(CefString& value) {
|
|
|
|
DCHECK_EQ(type_, TYPE_INVALID);
|
|
|
|
type_ = TYPE_STRING;
|
|
|
|
// Take ownership of the underling string value.
|
|
|
|
const cef_string_t* str = value.GetStruct();
|
|
|
|
if (str) {
|
|
|
|
string_value_ = *str;
|
2013-07-18 20:09:09 +02:00
|
|
|
cef_string_t* writable_struct = value.GetWritableStruct();
|
|
|
|
writable_struct->str = NULL;
|
|
|
|
writable_struct->length = 0;
|
2013-06-27 00:33:44 +02:00
|
|
|
} else {
|
|
|
|
string_value_.str = NULL;
|
2013-07-18 20:09:09 +02:00
|
|
|
string_value_.length = 0;
|
2013-06-27 00:33:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CefV8ValueImpl::InitObject(v8::Handle<v8::Value> value, CefTrackNode* tracker) {
|
|
|
|
DCHECK_EQ(type_, TYPE_INVALID);
|
|
|
|
type_ = TYPE_OBJECT;
|
2013-12-17 23:04:35 +01:00
|
|
|
handle_ = new Handle(isolate_, v8::Handle<v8::Context>(), value, tracker);
|
2013-06-27 00:33:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
v8::Handle<v8::Value> CefV8ValueImpl::GetV8Value(bool should_persist) {
|
|
|
|
switch (type_) {
|
|
|
|
case TYPE_UNDEFINED:
|
2013-12-17 23:04:35 +01:00
|
|
|
return v8::Undefined(isolate_);
|
2013-06-27 00:33:44 +02:00
|
|
|
case TYPE_NULL:
|
2013-12-17 23:04:35 +01:00
|
|
|
return v8::Null(isolate_);
|
2013-06-27 00:33:44 +02:00
|
|
|
case TYPE_BOOL:
|
2013-12-17 23:04:35 +01:00
|
|
|
return v8::Boolean::New(isolate_, bool_value_);
|
2013-06-27 00:33:44 +02:00
|
|
|
case TYPE_INT:
|
2013-12-17 23:04:35 +01:00
|
|
|
return v8::Int32::New(isolate_, int_value_);
|
2013-06-27 00:33:44 +02:00
|
|
|
case TYPE_UINT:
|
2013-12-17 23:04:35 +01:00
|
|
|
return v8::Uint32::New(isolate_, uint_value_);
|
2013-06-27 00:33:44 +02:00
|
|
|
case TYPE_DOUBLE:
|
2013-12-17 23:04:35 +01:00
|
|
|
return v8::Number::New(isolate_, double_value_);
|
2013-06-27 00:33:44 +02:00
|
|
|
case TYPE_DATE:
|
|
|
|
// Convert from seconds to milliseconds.
|
2013-12-17 23:04:35 +01:00
|
|
|
return v8::Date::New(isolate_, CefTime(date_value_).GetDoubleT() * 1000);
|
2013-06-27 00:33:44 +02:00
|
|
|
case TYPE_STRING:
|
2013-12-17 23:04:35 +01:00
|
|
|
return GetV8String(isolate_, CefString(&string_value_));
|
2013-06-27 00:33:44 +02:00
|
|
|
case TYPE_OBJECT:
|
|
|
|
return handle_->GetNewV8Handle(should_persist);
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
NOTREACHED() << "Invalid type for CefV8ValueImpl";
|
|
|
|
return v8::Handle<v8::Value>();
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
2012-10-29 22:46:02 +01:00
|
|
|
bool CefV8ValueImpl::IsValid() {
|
2013-06-27 00:33:44 +02:00
|
|
|
if (!CEF_V8_HAS_ISOLATE() || type_ == TYPE_INVALID ||
|
|
|
|
(type_ == TYPE_OBJECT &&
|
|
|
|
(!handle_->BelongsToCurrentThread() || !handle_->IsValid()))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2012-10-29 22:46:02 +01:00
|
|
|
}
|
|
|
|
|
2012-04-03 03:34:16 +02:00
|
|
|
bool CefV8ValueImpl::IsUndefined() {
|
2013-06-27 00:33:44 +02:00
|
|
|
CEF_V8_REQUIRE_ISOLATE_RETURN(false);
|
|
|
|
return (type_ == TYPE_UNDEFINED);
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CefV8ValueImpl::IsNull() {
|
2013-06-27 00:33:44 +02:00
|
|
|
CEF_V8_REQUIRE_ISOLATE_RETURN(false);
|
|
|
|
return (type_ == TYPE_NULL);
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CefV8ValueImpl::IsBool() {
|
2013-06-27 00:33:44 +02:00
|
|
|
CEF_V8_REQUIRE_ISOLATE_RETURN(false);
|
|
|
|
return (type_ == TYPE_BOOL);
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CefV8ValueImpl::IsInt() {
|
2013-06-27 00:33:44 +02:00
|
|
|
CEF_V8_REQUIRE_ISOLATE_RETURN(false);
|
|
|
|
return (type_ == TYPE_INT || type_ == TYPE_UINT);
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CefV8ValueImpl::IsUInt() {
|
2013-06-27 00:33:44 +02:00
|
|
|
CEF_V8_REQUIRE_ISOLATE_RETURN(false);
|
|
|
|
return (type_ == TYPE_INT || type_ == TYPE_UINT);
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CefV8ValueImpl::IsDouble() {
|
2013-06-27 00:33:44 +02:00
|
|
|
CEF_V8_REQUIRE_ISOLATE_RETURN(false);
|
|
|
|
return (type_ == TYPE_INT || type_ == TYPE_UINT || type_ == TYPE_DOUBLE);
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CefV8ValueImpl::IsDate() {
|
2013-06-27 00:33:44 +02:00
|
|
|
CEF_V8_REQUIRE_ISOLATE_RETURN(false);
|
|
|
|
return (type_ == TYPE_DATE);
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CefV8ValueImpl::IsString() {
|
2013-06-27 00:33:44 +02:00
|
|
|
CEF_V8_REQUIRE_ISOLATE_RETURN(false);
|
|
|
|
return (type_ == TYPE_STRING);
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CefV8ValueImpl::IsObject() {
|
2013-06-27 00:33:44 +02:00
|
|
|
CEF_V8_REQUIRE_ISOLATE_RETURN(false);
|
|
|
|
return (type_ == TYPE_OBJECT);
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CefV8ValueImpl::IsArray() {
|
2013-06-27 00:33:44 +02:00
|
|
|
CEF_V8_REQUIRE_MLT_RETURN(false);
|
|
|
|
if (type_ == TYPE_OBJECT) {
|
2013-10-16 02:25:38 +02:00
|
|
|
v8::HandleScope handle_scope(handle_->isolate());
|
2013-06-27 00:33:44 +02:00
|
|
|
return handle_->GetNewV8Handle(false)->IsArray();
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CefV8ValueImpl::IsFunction() {
|
2013-06-27 00:33:44 +02:00
|
|
|
CEF_V8_REQUIRE_MLT_RETURN(false);
|
|
|
|
if (type_ == TYPE_OBJECT) {
|
2013-10-16 02:25:38 +02:00
|
|
|
v8::HandleScope handle_scope(handle_->isolate());
|
2013-06-27 00:33:44 +02:00
|
|
|
return handle_->GetNewV8Handle(false)->IsFunction();
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CefV8ValueImpl::IsSame(CefRefPtr<CefV8Value> that) {
|
2013-06-27 00:33:44 +02:00
|
|
|
CEF_V8_REQUIRE_MLT_RETURN(false);
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2013-06-27 00:33:44 +02:00
|
|
|
CefV8ValueImpl* thatValue = static_cast<CefV8ValueImpl*>(that.get());
|
|
|
|
if (!thatValue || !thatValue->IsValid() || type_ != thatValue->type_)
|
|
|
|
return false;
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2013-06-27 00:33:44 +02:00
|
|
|
switch (type_) {
|
|
|
|
case TYPE_UNDEFINED:
|
|
|
|
case TYPE_NULL:
|
|
|
|
return true;
|
|
|
|
case TYPE_BOOL:
|
|
|
|
return (bool_value_ == thatValue->bool_value_);
|
|
|
|
case TYPE_INT:
|
|
|
|
return (int_value_ == thatValue->int_value_);
|
|
|
|
case TYPE_UINT:
|
|
|
|
return (uint_value_ == thatValue->uint_value_);
|
|
|
|
case TYPE_DOUBLE:
|
|
|
|
return (double_value_ == thatValue->double_value_);
|
|
|
|
case TYPE_DATE:
|
|
|
|
return (CefTime(date_value_).GetTimeT() ==
|
|
|
|
CefTime(thatValue->date_value_).GetTimeT());
|
|
|
|
case TYPE_STRING:
|
|
|
|
return (CefString(&string_value_) ==
|
|
|
|
CefString(&thatValue->string_value_));
|
|
|
|
case TYPE_OBJECT: {
|
|
|
|
return (handle_->GetPersistentV8Handle() ==
|
|
|
|
thatValue->handle_->GetPersistentV8Handle());
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2013-06-27 00:33:44 +02:00
|
|
|
return false;
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CefV8ValueImpl::GetBoolValue() {
|
2013-06-27 00:33:44 +02:00
|
|
|
CEF_V8_REQUIRE_ISOLATE_RETURN(false);
|
|
|
|
if (type_ == TYPE_BOOL)
|
|
|
|
return bool_value_;
|
|
|
|
return false;
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int32 CefV8ValueImpl::GetIntValue() {
|
2013-06-27 00:33:44 +02:00
|
|
|
CEF_V8_REQUIRE_ISOLATE_RETURN(0);
|
|
|
|
if (type_ == TYPE_INT || type_ == TYPE_UINT)
|
|
|
|
return int_value_;
|
|
|
|
return 0;
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32 CefV8ValueImpl::GetUIntValue() {
|
2013-06-27 00:33:44 +02:00
|
|
|
CEF_V8_REQUIRE_ISOLATE_RETURN(0);
|
|
|
|
if (type_ == TYPE_INT || type_ == TYPE_UINT)
|
|
|
|
return uint_value_;
|
|
|
|
return 0;
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
double CefV8ValueImpl::GetDoubleValue() {
|
2013-06-27 00:33:44 +02:00
|
|
|
CEF_V8_REQUIRE_ISOLATE_RETURN(0.);
|
|
|
|
if (type_ == TYPE_DOUBLE)
|
|
|
|
return double_value_;
|
|
|
|
else if (type_ == TYPE_INT)
|
|
|
|
return int_value_;
|
|
|
|
else if (type_ == TYPE_UINT)
|
|
|
|
return uint_value_;
|
|
|
|
return 0.;
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
CefTime CefV8ValueImpl::GetDateValue() {
|
2013-06-27 00:33:44 +02:00
|
|
|
CEF_V8_REQUIRE_ISOLATE_RETURN(CefTime(0.));
|
|
|
|
if (type_ == TYPE_DATE)
|
|
|
|
return date_value_;
|
|
|
|
return CefTime(0.);
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
CefString CefV8ValueImpl::GetStringValue() {
|
|
|
|
CefString rv;
|
2013-06-27 00:33:44 +02:00
|
|
|
CEF_V8_REQUIRE_ISOLATE_RETURN(rv);
|
|
|
|
if (type_ == TYPE_STRING)
|
|
|
|
rv = CefString(&string_value_);
|
2012-04-03 03:34:16 +02:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CefV8ValueImpl::IsUserCreated() {
|
|
|
|
CEF_V8_REQUIRE_OBJECT_RETURN(false);
|
|
|
|
|
2013-12-17 23:04:35 +01:00
|
|
|
v8::Isolate* isolate = handle_->isolate();
|
|
|
|
v8::HandleScope handle_scope(isolate);
|
2013-06-27 00:33:44 +02:00
|
|
|
v8::Handle<v8::Value> value = handle_->GetNewV8Handle(false);
|
|
|
|
v8::Handle<v8::Object> obj = value->ToObject();
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2013-12-17 23:04:35 +01:00
|
|
|
V8TrackObject* tracker = V8TrackObject::Unwrap(isolate, obj);
|
2012-04-03 03:34:16 +02:00
|
|
|
return (tracker != NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CefV8ValueImpl::HasException() {
|
|
|
|
CEF_V8_REQUIRE_OBJECT_RETURN(false);
|
|
|
|
|
|
|
|
return (last_exception_.get() != NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
CefRefPtr<CefV8Exception> CefV8ValueImpl::GetException() {
|
|
|
|
CEF_V8_REQUIRE_OBJECT_RETURN(NULL);
|
|
|
|
|
|
|
|
return last_exception_;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CefV8ValueImpl::ClearException() {
|
2012-08-04 02:59:58 +02:00
|
|
|
CEF_V8_REQUIRE_OBJECT_RETURN(false);
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
last_exception_ = NULL;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CefV8ValueImpl::WillRethrowExceptions() {
|
|
|
|
CEF_V8_REQUIRE_OBJECT_RETURN(false);
|
|
|
|
|
|
|
|
return rethrow_exceptions_;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CefV8ValueImpl::SetRethrowExceptions(bool rethrow) {
|
|
|
|
CEF_V8_REQUIRE_OBJECT_RETURN(false);
|
|
|
|
|
|
|
|
rethrow_exceptions_ = rethrow;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CefV8ValueImpl::HasValue(const CefString& key) {
|
|
|
|
CEF_V8_REQUIRE_OBJECT_RETURN(false);
|
|
|
|
|
2013-12-17 23:04:35 +01:00
|
|
|
v8::Isolate* isolate = handle_->isolate();
|
|
|
|
v8::HandleScope handle_scope(isolate);
|
2013-06-27 00:33:44 +02:00
|
|
|
v8::Handle<v8::Value> value = handle_->GetNewV8Handle(false);
|
|
|
|
v8::Handle<v8::Object> obj = value->ToObject();
|
2013-12-17 23:04:35 +01:00
|
|
|
return obj->Has(GetV8String(isolate, key));
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CefV8ValueImpl::HasValue(int index) {
|
|
|
|
CEF_V8_REQUIRE_OBJECT_RETURN(false);
|
|
|
|
|
|
|
|
if (index < 0) {
|
|
|
|
NOTREACHED() << "invalid input parameter";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-10-16 02:25:38 +02:00
|
|
|
v8::HandleScope handle_scope(handle_->isolate());
|
2013-06-27 00:33:44 +02:00
|
|
|
v8::Handle<v8::Value> value = handle_->GetNewV8Handle(false);
|
|
|
|
v8::Handle<v8::Object> obj = value->ToObject();
|
2012-04-03 03:34:16 +02:00
|
|
|
return obj->Has(index);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CefV8ValueImpl::DeleteValue(const CefString& key) {
|
|
|
|
CEF_V8_REQUIRE_OBJECT_RETURN(false);
|
|
|
|
|
2013-12-17 23:04:35 +01:00
|
|
|
v8::Isolate* isolate = handle_->isolate();
|
|
|
|
v8::HandleScope handle_scope(isolate);
|
2013-06-27 00:33:44 +02:00
|
|
|
v8::Handle<v8::Value> value = handle_->GetNewV8Handle(false);
|
|
|
|
v8::Handle<v8::Object> obj = value->ToObject();
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
v8::TryCatch try_catch;
|
2012-04-24 20:01:48 +02:00
|
|
|
try_catch.SetVerbose(true);
|
2013-12-17 23:04:35 +01:00
|
|
|
bool del = obj->Delete(GetV8String(isolate, key));
|
2012-04-03 03:34:16 +02:00
|
|
|
return (!HasCaught(try_catch) && del);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CefV8ValueImpl::DeleteValue(int index) {
|
|
|
|
CEF_V8_REQUIRE_OBJECT_RETURN(false);
|
|
|
|
|
|
|
|
if (index < 0) {
|
|
|
|
NOTREACHED() << "invalid input parameter";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-10-16 02:25:38 +02:00
|
|
|
v8::HandleScope handle_scope(handle_->isolate());
|
2013-06-27 00:33:44 +02:00
|
|
|
v8::Handle<v8::Value> value = handle_->GetNewV8Handle(false);
|
|
|
|
v8::Handle<v8::Object> obj = value->ToObject();
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
v8::TryCatch try_catch;
|
2012-04-24 20:01:48 +02:00
|
|
|
try_catch.SetVerbose(true);
|
2012-04-03 03:34:16 +02:00
|
|
|
bool del = obj->Delete(index);
|
|
|
|
return (!HasCaught(try_catch) && del);
|
|
|
|
}
|
|
|
|
|
|
|
|
CefRefPtr<CefV8Value> CefV8ValueImpl::GetValue(const CefString& key) {
|
|
|
|
CEF_V8_REQUIRE_OBJECT_RETURN(NULL);
|
|
|
|
|
2013-12-17 23:04:35 +01:00
|
|
|
v8::Isolate* isolate = handle_->isolate();
|
|
|
|
v8::HandleScope handle_scope(isolate);
|
2013-06-27 00:33:44 +02:00
|
|
|
v8::Handle<v8::Value> value = handle_->GetNewV8Handle(false);
|
|
|
|
v8::Handle<v8::Object> obj = value->ToObject();
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
v8::TryCatch try_catch;
|
2012-04-24 20:01:48 +02:00
|
|
|
try_catch.SetVerbose(true);
|
2013-12-17 23:04:35 +01:00
|
|
|
v8::Local<v8::Value> ret_value = obj->Get(GetV8String(isolate, key));
|
2013-06-27 00:33:44 +02:00
|
|
|
if (!HasCaught(try_catch) && !ret_value.IsEmpty())
|
2013-12-17 23:04:35 +01:00
|
|
|
return new CefV8ValueImpl(isolate, ret_value);
|
2012-04-03 03:34:16 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
CefRefPtr<CefV8Value> CefV8ValueImpl::GetValue(int index) {
|
|
|
|
CEF_V8_REQUIRE_OBJECT_RETURN(NULL);
|
|
|
|
|
|
|
|
if (index < 0) {
|
|
|
|
NOTREACHED() << "invalid input parameter";
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-12-17 23:04:35 +01:00
|
|
|
v8::Isolate* isolate = handle_->isolate();
|
|
|
|
v8::HandleScope handle_scope(isolate);
|
2013-06-27 00:33:44 +02:00
|
|
|
v8::Handle<v8::Value> value = handle_->GetNewV8Handle(false);
|
|
|
|
v8::Handle<v8::Object> obj = value->ToObject();
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
v8::TryCatch try_catch;
|
2012-04-24 20:01:48 +02:00
|
|
|
try_catch.SetVerbose(true);
|
2013-12-17 23:04:35 +01:00
|
|
|
v8::Local<v8::Value> ret_value = obj->Get(v8::Number::New(isolate, index));
|
2013-06-27 00:33:44 +02:00
|
|
|
if (!HasCaught(try_catch) && !ret_value.IsEmpty())
|
2013-12-17 23:04:35 +01:00
|
|
|
return new CefV8ValueImpl(isolate, ret_value);
|
2012-04-03 03:34:16 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CefV8ValueImpl::SetValue(const CefString& key,
|
|
|
|
CefRefPtr<CefV8Value> value,
|
|
|
|
PropertyAttribute attribute) {
|
|
|
|
CEF_V8_REQUIRE_OBJECT_RETURN(false);
|
|
|
|
|
|
|
|
CefV8ValueImpl* impl = static_cast<CefV8ValueImpl*>(value.get());
|
2012-12-30 22:16:59 +01:00
|
|
|
if (impl && impl->IsValid()) {
|
2013-12-17 23:04:35 +01:00
|
|
|
v8::Isolate* isolate = handle_->isolate();
|
|
|
|
v8::HandleScope handle_scope(isolate);
|
2013-06-27 00:33:44 +02:00
|
|
|
v8::Handle<v8::Value> value = handle_->GetNewV8Handle(false);
|
|
|
|
v8::Handle<v8::Object> obj = value->ToObject();
|
2012-04-24 20:01:48 +02:00
|
|
|
|
2012-04-03 03:34:16 +02:00
|
|
|
v8::TryCatch try_catch;
|
2012-04-24 20:01:48 +02:00
|
|
|
try_catch.SetVerbose(true);
|
2013-12-17 23:04:35 +01:00
|
|
|
bool set = obj->Set(GetV8String(isolate, key), impl->GetV8Value(true),
|
2012-04-03 03:34:16 +02:00
|
|
|
static_cast<v8::PropertyAttribute>(attribute));
|
|
|
|
return (!HasCaught(try_catch) && set);
|
|
|
|
} else {
|
|
|
|
NOTREACHED() << "invalid input parameter";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CefV8ValueImpl::SetValue(int index, CefRefPtr<CefV8Value> value) {
|
|
|
|
CEF_V8_REQUIRE_OBJECT_RETURN(false);
|
|
|
|
|
|
|
|
if (index < 0) {
|
|
|
|
NOTREACHED() << "invalid input parameter";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
CefV8ValueImpl* impl = static_cast<CefV8ValueImpl*>(value.get());
|
2012-12-30 22:16:59 +01:00
|
|
|
if (impl && impl->IsValid()) {
|
2013-10-16 02:25:38 +02:00
|
|
|
v8::HandleScope handle_scope(handle_->isolate());
|
2013-06-27 00:33:44 +02:00
|
|
|
v8::Handle<v8::Value> value = handle_->GetNewV8Handle(false);
|
|
|
|
v8::Handle<v8::Object> obj = value->ToObject();
|
2012-04-24 20:01:48 +02:00
|
|
|
|
2012-04-03 03:34:16 +02:00
|
|
|
v8::TryCatch try_catch;
|
2012-04-24 20:01:48 +02:00
|
|
|
try_catch.SetVerbose(true);
|
2013-06-27 00:33:44 +02:00
|
|
|
bool set = obj->Set(index, impl->GetV8Value(true));
|
2012-04-03 03:34:16 +02:00
|
|
|
return (!HasCaught(try_catch) && set);
|
|
|
|
} else {
|
|
|
|
NOTREACHED() << "invalid input parameter";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CefV8ValueImpl::SetValue(const CefString& key, AccessControl settings,
|
|
|
|
PropertyAttribute attribute) {
|
|
|
|
CEF_V8_REQUIRE_OBJECT_RETURN(false);
|
|
|
|
|
2013-12-17 23:04:35 +01:00
|
|
|
v8::Isolate* isolate = handle_->isolate();
|
|
|
|
v8::HandleScope handle_scope(isolate);
|
2013-06-27 00:33:44 +02:00
|
|
|
v8::Handle<v8::Value> value = handle_->GetNewV8Handle(false);
|
|
|
|
v8::Handle<v8::Object> obj = value->ToObject();
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
CefRefPtr<CefV8Accessor> accessorPtr;
|
|
|
|
|
2013-12-17 23:04:35 +01:00
|
|
|
V8TrackObject* tracker = V8TrackObject::Unwrap(isolate, obj);
|
2012-04-03 03:34:16 +02:00
|
|
|
if (tracker)
|
|
|
|
accessorPtr = tracker->GetAccessor();
|
|
|
|
|
|
|
|
// Verify that an accessor exists for this object.
|
|
|
|
if (!accessorPtr.get())
|
|
|
|
return false;
|
|
|
|
|
2013-07-24 22:15:18 +02:00
|
|
|
v8::AccessorGetterCallback getter = AccessorGetterCallbackImpl;
|
|
|
|
v8::AccessorSetterCallback setter =
|
|
|
|
(attribute & V8_PROPERTY_ATTRIBUTE_READONLY) ?
|
|
|
|
NULL : AccessorSetterCallbackImpl;
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
v8::TryCatch try_catch;
|
2012-04-24 20:01:48 +02:00
|
|
|
try_catch.SetVerbose(true);
|
2013-12-17 23:04:35 +01:00
|
|
|
bool set = obj->SetAccessor(GetV8String(isolate, key), getter, setter, obj,
|
2012-04-03 03:34:16 +02:00
|
|
|
static_cast<v8::AccessControl>(settings),
|
|
|
|
static_cast<v8::PropertyAttribute>(attribute));
|
|
|
|
return (!HasCaught(try_catch) && set);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CefV8ValueImpl::GetKeys(std::vector<CefString>& keys) {
|
|
|
|
CEF_V8_REQUIRE_OBJECT_RETURN(false);
|
|
|
|
|
2013-12-17 23:04:35 +01:00
|
|
|
v8::Isolate* isolate = handle_->isolate();
|
|
|
|
v8::HandleScope handle_scope(isolate);
|
2013-06-27 00:33:44 +02:00
|
|
|
v8::Handle<v8::Value> value = handle_->GetNewV8Handle(false);
|
|
|
|
v8::Handle<v8::Object> obj = value->ToObject();
|
|
|
|
|
2012-04-03 03:34:16 +02:00
|
|
|
v8::Local<v8::Array> arr_keys = obj->GetPropertyNames();
|
|
|
|
uint32_t len = arr_keys->Length();
|
|
|
|
for (uint32_t i = 0; i < len; ++i) {
|
2013-12-17 23:04:35 +01:00
|
|
|
v8::Local<v8::Value> value = arr_keys->Get(v8::Integer::New(isolate, i));
|
2012-04-03 03:34:16 +02:00
|
|
|
CefString str;
|
|
|
|
GetCefString(value->ToString(), str);
|
|
|
|
keys.push_back(str);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CefV8ValueImpl::SetUserData(CefRefPtr<CefBase> user_data) {
|
|
|
|
CEF_V8_REQUIRE_OBJECT_RETURN(false);
|
|
|
|
|
2013-12-17 23:04:35 +01:00
|
|
|
v8::Isolate* isolate = handle_->isolate();
|
|
|
|
v8::HandleScope handle_scope(isolate);
|
2013-06-27 00:33:44 +02:00
|
|
|
v8::Handle<v8::Value> value = handle_->GetNewV8Handle(false);
|
|
|
|
v8::Handle<v8::Object> obj = value->ToObject();
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2013-12-17 23:04:35 +01:00
|
|
|
V8TrackObject* tracker = V8TrackObject::Unwrap(isolate, obj);
|
2012-04-03 03:34:16 +02:00
|
|
|
if (tracker) {
|
|
|
|
tracker->SetUserData(user_data);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
CefRefPtr<CefBase> CefV8ValueImpl::GetUserData() {
|
|
|
|
CEF_V8_REQUIRE_OBJECT_RETURN(NULL);
|
|
|
|
|
2013-12-17 23:04:35 +01:00
|
|
|
v8::Isolate* isolate = handle_->isolate();
|
|
|
|
v8::HandleScope handle_scope(isolate);
|
2013-06-27 00:33:44 +02:00
|
|
|
v8::Handle<v8::Value> value = handle_->GetNewV8Handle(false);
|
|
|
|
v8::Handle<v8::Object> obj = value->ToObject();
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2013-12-17 23:04:35 +01:00
|
|
|
V8TrackObject* tracker = V8TrackObject::Unwrap(isolate, obj);
|
2012-04-03 03:34:16 +02:00
|
|
|
if (tracker)
|
|
|
|
return tracker->GetUserData();
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int CefV8ValueImpl::GetExternallyAllocatedMemory() {
|
|
|
|
CEF_V8_REQUIRE_OBJECT_RETURN(0);
|
|
|
|
|
2013-12-17 23:04:35 +01:00
|
|
|
v8::Isolate* isolate = handle_->isolate();
|
|
|
|
v8::HandleScope handle_scope(isolate);
|
2013-06-27 00:33:44 +02:00
|
|
|
v8::Handle<v8::Value> value = handle_->GetNewV8Handle(false);
|
|
|
|
v8::Handle<v8::Object> obj = value->ToObject();
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2013-12-17 23:04:35 +01:00
|
|
|
V8TrackObject* tracker = V8TrackObject::Unwrap(isolate, obj);
|
2012-04-03 03:34:16 +02:00
|
|
|
if (tracker)
|
|
|
|
return tracker->GetExternallyAllocatedMemory();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int CefV8ValueImpl::AdjustExternallyAllocatedMemory(int change_in_bytes) {
|
|
|
|
CEF_V8_REQUIRE_OBJECT_RETURN(0);
|
|
|
|
|
2013-12-17 23:04:35 +01:00
|
|
|
v8::Isolate* isolate = handle_->isolate();
|
|
|
|
v8::HandleScope handle_scope(isolate);
|
2013-06-27 00:33:44 +02:00
|
|
|
v8::Handle<v8::Value> value = handle_->GetNewV8Handle(false);
|
|
|
|
v8::Handle<v8::Object> obj = value->ToObject();
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2013-12-17 23:04:35 +01:00
|
|
|
V8TrackObject* tracker = V8TrackObject::Unwrap(isolate, obj);
|
2012-04-03 03:34:16 +02:00
|
|
|
if (tracker)
|
|
|
|
return tracker->AdjustExternallyAllocatedMemory(change_in_bytes);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int CefV8ValueImpl::GetArrayLength() {
|
2013-06-27 00:33:44 +02:00
|
|
|
CEF_V8_REQUIRE_OBJECT_RETURN(0);
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2013-10-16 02:25:38 +02:00
|
|
|
v8::HandleScope handle_scope(handle_->isolate());
|
2013-06-27 00:33:44 +02:00
|
|
|
v8::Handle<v8::Value> value = handle_->GetNewV8Handle(false);
|
|
|
|
if (!value->IsArray()) {
|
|
|
|
NOTREACHED() << "V8 value is not an array";
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
v8::Handle<v8::Object> obj = value->ToObject();
|
|
|
|
v8::Local<v8::Array> arr = v8::Handle<v8::Array>::Cast(obj);
|
2012-04-03 03:34:16 +02:00
|
|
|
return arr->Length();
|
|
|
|
}
|
|
|
|
|
|
|
|
CefString CefV8ValueImpl::GetFunctionName() {
|
|
|
|
CefString rv;
|
2013-06-27 00:33:44 +02:00
|
|
|
CEF_V8_REQUIRE_OBJECT_RETURN(rv);
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2013-10-16 02:25:38 +02:00
|
|
|
v8::HandleScope handle_scope(handle_->isolate());
|
2013-06-27 00:33:44 +02:00
|
|
|
v8::Handle<v8::Value> value = handle_->GetNewV8Handle(false);
|
|
|
|
if (!value->IsFunction()) {
|
|
|
|
NOTREACHED() << "V8 value is not a function";
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
v8::Handle<v8::Object> obj = value->ToObject();
|
|
|
|
v8::Handle<v8::Function> func = v8::Handle<v8::Function>::Cast(obj);
|
2012-04-03 03:34:16 +02:00
|
|
|
GetCefString(v8::Handle<v8::String>::Cast(func->GetName()), rv);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
CefRefPtr<CefV8Handler> CefV8ValueImpl::GetFunctionHandler() {
|
2013-06-27 00:33:44 +02:00
|
|
|
CEF_V8_REQUIRE_OBJECT_RETURN(NULL);
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2013-12-17 23:04:35 +01:00
|
|
|
v8::Isolate* isolate = handle_->isolate();
|
|
|
|
v8::HandleScope handle_scope(isolate);
|
2013-06-27 00:33:44 +02:00
|
|
|
v8::Handle<v8::Value> value = handle_->GetNewV8Handle(false);
|
|
|
|
if (!value->IsFunction()) {
|
|
|
|
NOTREACHED() << "V8 value is not a function";
|
|
|
|
return 0;
|
|
|
|
}
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2013-06-27 00:33:44 +02:00
|
|
|
v8::Handle<v8::Object> obj = value->ToObject();
|
2013-12-17 23:04:35 +01:00
|
|
|
V8TrackObject* tracker = V8TrackObject::Unwrap(isolate, obj);
|
2012-04-03 03:34:16 +02:00
|
|
|
if (tracker)
|
|
|
|
return tracker->GetHandler();
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
CefRefPtr<CefV8Value> CefV8ValueImpl::ExecuteFunction(
|
|
|
|
CefRefPtr<CefV8Value> object,
|
|
|
|
const CefV8ValueList& arguments) {
|
|
|
|
// An empty context value defaults to the current context.
|
|
|
|
CefRefPtr<CefV8Context> context;
|
|
|
|
return ExecuteFunctionWithContext(context, object, arguments);
|
|
|
|
}
|
|
|
|
|
|
|
|
CefRefPtr<CefV8Value> CefV8ValueImpl::ExecuteFunctionWithContext(
|
|
|
|
CefRefPtr<CefV8Context> context,
|
|
|
|
CefRefPtr<CefV8Value> object,
|
|
|
|
const CefV8ValueList& arguments) {
|
2013-06-27 00:33:44 +02:00
|
|
|
CEF_V8_REQUIRE_OBJECT_RETURN(NULL);
|
|
|
|
|
2013-12-17 23:04:35 +01:00
|
|
|
v8::Isolate* isolate = handle_->isolate();
|
|
|
|
v8::HandleScope handle_scope(isolate);
|
2013-06-27 00:33:44 +02:00
|
|
|
v8::Handle<v8::Value> value = handle_->GetNewV8Handle(false);
|
|
|
|
if (!value->IsFunction()) {
|
|
|
|
NOTREACHED() << "V8 value is not a function";
|
|
|
|
return 0;
|
|
|
|
}
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2012-12-30 22:16:59 +01:00
|
|
|
if (context.get() && !context->IsValid()) {
|
|
|
|
NOTREACHED() << "invalid V8 context parameter";
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (object.get() && (!object->IsValid() || !object->IsObject())) {
|
|
|
|
NOTREACHED() << "invalid V8 object parameter";
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int argc = arguments.size();
|
|
|
|
if (argc > 0) {
|
|
|
|
for (int i = 0; i < argc; ++i) {
|
|
|
|
if (!arguments[i].get() || !arguments[i]->IsValid()) {
|
|
|
|
NOTREACHED() << "invalid V8 arguments parameter";
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-03 03:34:16 +02:00
|
|
|
v8::Local<v8::Context> context_local;
|
|
|
|
if (context.get()) {
|
|
|
|
CefV8ContextImpl* context_impl =
|
|
|
|
static_cast<CefV8ContextImpl*>(context.get());
|
2013-06-27 00:33:44 +02:00
|
|
|
context_local = context_impl->GetV8Context();
|
2012-04-03 03:34:16 +02:00
|
|
|
} else {
|
2013-12-17 23:04:35 +01:00
|
|
|
context_local = isolate->GetCurrentContext();
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
v8::Context::Scope context_scope(context_local);
|
|
|
|
|
2013-06-27 00:33:44 +02:00
|
|
|
v8::Handle<v8::Object> obj = value->ToObject();
|
|
|
|
v8::Handle<v8::Function> func = v8::Handle<v8::Function>::Cast(obj);
|
2012-04-03 03:34:16 +02:00
|
|
|
v8::Handle<v8::Object> recv;
|
|
|
|
|
2012-12-30 22:16:59 +01:00
|
|
|
// Default to the global object if no object was provided.
|
|
|
|
if (object.get()) {
|
2012-04-03 03:34:16 +02:00
|
|
|
CefV8ValueImpl* recv_impl = static_cast<CefV8ValueImpl*>(object.get());
|
2013-06-27 00:33:44 +02:00
|
|
|
recv = v8::Handle<v8::Object>::Cast(recv_impl->GetV8Value(true));
|
2012-04-03 03:34:16 +02:00
|
|
|
} else {
|
|
|
|
recv = context_local->Global();
|
|
|
|
}
|
|
|
|
|
|
|
|
v8::Handle<v8::Value> *argv = NULL;
|
|
|
|
if (argc > 0) {
|
|
|
|
argv = new v8::Handle<v8::Value>[argc];
|
2012-10-30 21:49:43 +01:00
|
|
|
for (int i = 0; i < argc; ++i) {
|
|
|
|
argv[i] =
|
2013-06-27 00:33:44 +02:00
|
|
|
static_cast<CefV8ValueImpl*>(arguments[i].get())->GetV8Value(true);
|
2012-10-30 21:49:43 +01:00
|
|
|
}
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
CefRefPtr<CefV8Value> retval;
|
|
|
|
|
2012-04-24 20:01:48 +02:00
|
|
|
{
|
|
|
|
v8::TryCatch try_catch;
|
|
|
|
try_catch.SetVerbose(true);
|
|
|
|
|
2013-01-03 18:24:24 +01:00
|
|
|
v8::Local<v8::Value> func_rv =
|
2013-10-16 02:25:38 +02:00
|
|
|
CallV8Function(context_local, func, recv, argc, argv,
|
|
|
|
handle_->isolate());
|
2012-04-24 20:01:48 +02:00
|
|
|
|
|
|
|
if (!HasCaught(try_catch) && !func_rv.IsEmpty())
|
2013-12-17 23:04:35 +01:00
|
|
|
retval = new CefV8ValueImpl(isolate, func_rv);
|
2012-04-24 20:01:48 +02:00
|
|
|
}
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
if (argv)
|
|
|
|
delete [] argv;
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
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-07-25 13:50:35 +02:00
|
|
|
|
|
|
|
|
|
|
|
// CefV8StackTrace
|
|
|
|
|
|
|
|
// static
|
|
|
|
CefRefPtr<CefV8StackTrace> CefV8StackTrace::GetCurrent(int frame_limit) {
|
2012-12-30 22:16:59 +01:00
|
|
|
CEF_V8_REQUIRE_ISOLATE_RETURN(NULL);
|
2012-07-25 13:50:35 +02:00
|
|
|
|
2013-12-17 23:04:35 +01:00
|
|
|
v8::Isolate* isolate = GetIsolateManager()->isolate();
|
|
|
|
v8::HandleScope handle_scope(isolate);
|
2012-07-25 13:50:35 +02:00
|
|
|
v8::Handle<v8::StackTrace> stackTrace =
|
|
|
|
v8::StackTrace::CurrentStackTrace(
|
2013-12-17 23:04:35 +01:00
|
|
|
isolate, frame_limit, v8::StackTrace::kDetailed);
|
2012-07-25 13:50:35 +02:00
|
|
|
if (stackTrace.IsEmpty())
|
|
|
|
return NULL;
|
2013-12-17 23:04:35 +01:00
|
|
|
return new CefV8StackTraceImpl(isolate, stackTrace);
|
2012-07-25 13:50:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// CefV8StackTraceImpl
|
|
|
|
|
2013-12-17 23:04:35 +01:00
|
|
|
CefV8StackTraceImpl::CefV8StackTraceImpl(
|
|
|
|
v8::Isolate* isolate,
|
|
|
|
v8::Handle<v8::StackTrace> handle) {
|
2013-06-27 00:33:44 +02:00
|
|
|
int frame_count = handle->GetFrameCount();
|
|
|
|
if (frame_count > 0) {
|
|
|
|
frames_.reserve(frame_count);
|
|
|
|
for (int i = 0; i < frame_count; ++i)
|
2013-12-17 23:04:35 +01:00
|
|
|
frames_.push_back(new CefV8StackFrameImpl(isolate, handle->GetFrame(i)));
|
2013-06-27 00:33:44 +02:00
|
|
|
}
|
2012-07-25 13:50:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
CefV8StackTraceImpl::~CefV8StackTraceImpl() {
|
|
|
|
}
|
|
|
|
|
2012-10-29 22:46:02 +01:00
|
|
|
bool CefV8StackTraceImpl::IsValid() {
|
2013-06-27 00:33:44 +02:00
|
|
|
return true;
|
2012-10-29 22:46:02 +01:00
|
|
|
}
|
|
|
|
|
2012-07-25 13:50:35 +02:00
|
|
|
int CefV8StackTraceImpl::GetFrameCount() {
|
2013-06-27 00:33:44 +02:00
|
|
|
return frames_.size();
|
2012-07-25 13:50:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
CefRefPtr<CefV8StackFrame> CefV8StackTraceImpl::GetFrame(int index) {
|
2013-06-27 00:33:44 +02:00
|
|
|
if (index < 0 || index >= static_cast<int>(frames_.size()))
|
2012-07-25 13:50:35 +02:00
|
|
|
return NULL;
|
2013-06-27 00:33:44 +02:00
|
|
|
return frames_[index];
|
2012-07-25 13:50:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// CefV8StackFrameImpl
|
|
|
|
|
2013-12-17 23:04:35 +01:00
|
|
|
CefV8StackFrameImpl::CefV8StackFrameImpl(
|
|
|
|
v8::Isolate* isolate,
|
|
|
|
v8::Handle<v8::StackFrame> handle)
|
2013-06-27 00:33:44 +02:00
|
|
|
: line_number_(0),
|
|
|
|
column_(0),
|
|
|
|
is_eval_(false),
|
|
|
|
is_constructor_(false) {
|
|
|
|
if (handle.IsEmpty())
|
|
|
|
return;
|
|
|
|
GetCefString(v8::Handle<v8::String>::Cast(handle->GetScriptName()),
|
|
|
|
script_name_);
|
|
|
|
GetCefString(
|
|
|
|
v8::Handle<v8::String>::Cast(handle->GetScriptNameOrSourceURL()),
|
|
|
|
script_name_or_source_url_);
|
|
|
|
GetCefString(v8::Handle<v8::String>::Cast(handle->GetFunctionName()),
|
|
|
|
function_name_);
|
|
|
|
line_number_ = handle->GetLineNumber();
|
|
|
|
column_ = handle->GetColumn();
|
|
|
|
is_eval_ = handle->IsEval();
|
|
|
|
is_constructor_ = handle->IsConstructor();
|
2012-07-25 13:50:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
CefV8StackFrameImpl::~CefV8StackFrameImpl() {
|
|
|
|
}
|
|
|
|
|
2012-10-29 22:46:02 +01:00
|
|
|
bool CefV8StackFrameImpl::IsValid() {
|
2013-06-27 00:33:44 +02:00
|
|
|
return true;
|
2012-10-29 22:46:02 +01:00
|
|
|
}
|
|
|
|
|
2012-07-25 13:50:35 +02:00
|
|
|
CefString CefV8StackFrameImpl::GetScriptName() {
|
2013-06-27 00:33:44 +02:00
|
|
|
return script_name_;
|
2012-07-25 13:50:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
CefString CefV8StackFrameImpl::GetScriptNameOrSourceURL() {
|
2013-06-27 00:33:44 +02:00
|
|
|
return script_name_or_source_url_;
|
2012-07-25 13:50:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
CefString CefV8StackFrameImpl::GetFunctionName() {
|
2013-06-27 00:33:44 +02:00
|
|
|
return function_name_;
|
2012-07-25 13:50:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int CefV8StackFrameImpl::GetLineNumber() {
|
2013-06-27 00:33:44 +02:00
|
|
|
return line_number_;
|
2012-07-25 13:50:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int CefV8StackFrameImpl::GetColumn() {
|
2013-06-27 00:33:44 +02:00
|
|
|
return column_;
|
2012-07-25 13:50:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CefV8StackFrameImpl::IsEval() {
|
2013-06-27 00:33:44 +02:00
|
|
|
return is_eval_;
|
2012-07-25 13:50:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CefV8StackFrameImpl::IsConstructor() {
|
2013-06-27 00:33:44 +02:00
|
|
|
return is_constructor_;
|
2012-07-25 13:50:35 +02:00
|
|
|
}
|