This source file includes following definitions.
- createInjectedScriptHostV8Wrapper
- createInjectedScript
- canAccessInspectedWindow
- setWeakCallback
#include "config.h"
#include "core/inspector/InjectedScriptManager.h"
#include "V8InjectedScriptHost.h"
#include "V8Window.h"
#include "bindings/v8/BindingSecurity.h"
#include "bindings/v8/ScopedPersistent.h"
#include "bindings/v8/ScriptDebugServer.h"
#include "bindings/v8/ScriptObject.h"
#include "bindings/v8/V8Binding.h"
#include "bindings/v8/V8ObjectConstructor.h"
#include "bindings/v8/V8ScriptRunner.h"
#include "core/inspector/InjectedScriptHost.h"
#include "core/frame/DOMWindow.h"
#include "wtf/RefPtr.h"
namespace WebCore {
struct InjectedScriptManager::CallbackData {
ScopedPersistent<v8::Object> handle;
RefPtr<InjectedScriptHost> host;
};
static v8::Local<v8::Object> createInjectedScriptHostV8Wrapper(InjectedScriptHost* host, v8::Isolate* isolate)
{
v8::Local<v8::Function> function = V8InjectedScriptHost::domTemplate(isolate)->GetFunction();
if (function.IsEmpty()) {
return v8::Local<v8::Object>();
}
v8::Local<v8::Object> instanceTemplate = V8ObjectConstructor::newInstance(isolate, function);
if (instanceTemplate.IsEmpty()) {
return v8::Local<v8::Object>();
}
V8DOMWrapper::setNativeInfo(instanceTemplate, &V8InjectedScriptHost::wrapperTypeInfo, host);
InjectedScriptManager::CallbackData* data = new InjectedScriptManager::CallbackData;
data->host = host;
data->handle.set(isolate, instanceTemplate);
data->handle.setWeak(data, &InjectedScriptManager::setWeakCallback);
return instanceTemplate;
}
ScriptObject InjectedScriptManager::createInjectedScript(const String& scriptSource, ScriptState* inspectedScriptState, int id)
{
v8::Isolate* isolate = inspectedScriptState->isolate();
v8::HandleScope handleScope(isolate);
v8::Local<v8::Context> inspectedContext = inspectedScriptState->context();
v8::Context::Scope contextScope(inspectedContext);
v8::Local<v8::Object> scriptHostWrapper = createInjectedScriptHostV8Wrapper(m_injectedScriptHost.get(), inspectedContext->GetIsolate());
if (scriptHostWrapper.IsEmpty())
return ScriptObject();
v8::Local<v8::Value> value = V8ScriptRunner::compileAndRunInternalScript(v8String(isolate, scriptSource), isolate);
ASSERT(!value.IsEmpty());
ASSERT(value->IsFunction());
v8::Local<v8::Object> windowGlobal = inspectedContext->Global();
v8::Handle<v8::Value> info[] = { scriptHostWrapper, windowGlobal, v8::Number::New(inspectedContext->GetIsolate(), id) };
v8::Local<v8::Value> injectedScriptValue = V8ScriptRunner::callInternalFunction(v8::Local<v8::Function>::Cast(value), windowGlobal, WTF_ARRAY_LENGTH(info), info, inspectedContext->GetIsolate());
return ScriptObject(inspectedScriptState, v8::Handle<v8::Object>::Cast(injectedScriptValue));
}
bool InjectedScriptManager::canAccessInspectedWindow(ScriptState* scriptState)
{
v8::HandleScope handleScope(scriptState->isolate());
v8::Local<v8::Context> context = scriptState->context();
v8::Local<v8::Object> global = context->Global();
if (global.IsEmpty())
return false;
v8::Handle<v8::Object> holder = V8Window::findInstanceInPrototypeChain(global, context->GetIsolate());
if (holder.IsEmpty())
return false;
LocalFrame* frame = V8Window::toNative(holder)->frame();
v8::Context::Scope contextScope(context);
return BindingSecurity::shouldAllowAccessToFrame(scriptState->isolate(), frame, DoNotReportSecurityError);
}
void InjectedScriptManager::setWeakCallback(const v8::WeakCallbackData<v8::Object, InjectedScriptManager::CallbackData>& data)
{
data.GetParameter()->handle.clear();
data.GetParameter()->host.clear();
delete data.GetParameter();
}
}