1/*
2 * Copyright (C) 2007-2011 Google Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
8 *     * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *     * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 *     * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31#include "config.h"
32#include "V8InjectedScriptHost.h"
33
34#include "Database.h"
35#include "InjectedScript.h"
36#include "InjectedScriptHost.h"
37#include "InspectorDebuggerAgent.h"
38#include "InspectorValues.h"
39#include "ScriptDebugServer.h"
40#include "ScriptValue.h"
41#include "V8Binding.h"
42#include "V8BindingState.h"
43#include "V8Database.h"
44#include "V8HiddenPropertyName.h"
45#include "V8JavaScriptCallFrame.h"
46#include "V8Node.h"
47#include "V8Proxy.h"
48#include "V8Storage.h"
49
50namespace WebCore {
51
52Node* InjectedScriptHost::scriptValueAsNode(ScriptValue value)
53{
54    if (!value.isObject() || value.isNull())
55        return 0;
56    return V8Node::toNative(v8::Handle<v8::Object>::Cast(value.v8Value()));
57}
58
59ScriptValue InjectedScriptHost::nodeAsScriptValue(ScriptState* state, Node* node)
60{
61    v8::HandleScope scope;
62    v8::Local<v8::Context> context = state->context();
63    v8::Context::Scope contextScope(context);
64
65    return ScriptValue(toV8(node));
66}
67
68v8::Handle<v8::Value> V8InjectedScriptHost::inspectedNodeCallback(const v8::Arguments& args)
69{
70    INC_STATS("InjectedScriptHost.inspectedNode()");
71    if (args.Length() < 1)
72        return v8::Undefined();
73
74    InjectedScriptHost* host = V8InjectedScriptHost::toNative(args.Holder());
75
76    Node* node = host->inspectedNode(args[0]->ToInt32()->Value());
77    if (!node)
78        return v8::Undefined();
79
80    return toV8(node);
81}
82
83v8::Handle<v8::Value> V8InjectedScriptHost::internalConstructorNameCallback(const v8::Arguments& args)
84{
85    INC_STATS("InjectedScriptHost.internalConstructorName()");
86    if (args.Length() < 1)
87        return v8::Undefined();
88
89    if (!args[0]->IsObject())
90        return v8::Undefined();
91
92    return args[0]->ToObject()->GetConstructorName();
93}
94
95v8::Handle<v8::Value> V8InjectedScriptHost::inspectCallback(const v8::Arguments& args)
96{
97    INC_STATS("InjectedScriptHost.inspect()");
98    if (args.Length() < 2)
99        return v8::Undefined();
100
101    InjectedScriptHost* host = V8InjectedScriptHost::toNative(args.Holder());
102    ScriptValue object(args[0]);
103    ScriptValue hints(args[1]);
104    host->inspectImpl(object.toInspectorValue(ScriptState::current()), hints.toInspectorValue(ScriptState::current()));
105
106    return v8::Undefined();
107}
108
109v8::Handle<v8::Value> V8InjectedScriptHost::currentCallFrameCallback(const v8::Arguments& args)
110{
111#if ENABLE(JAVASCRIPT_DEBUGGER)
112    INC_STATS("InjectedScriptHost.currentCallFrame()");
113    InjectedScriptHost* host = V8InjectedScriptHost::toNative(args.Holder());
114    return toV8(host->debuggerAgent()->scriptDebugServer().currentCallFrame());
115#else
116    UNUSED_PARAM(args);
117    return v8::Undefined();
118#endif
119}
120
121v8::Handle<v8::Value> V8InjectedScriptHost::databaseIdCallback(const v8::Arguments& args)
122{
123    INC_STATS("InjectedScriptHost.databaseId()");
124    if (args.Length() < 1)
125        return v8::Undefined();
126#if ENABLE(DATABASE)
127    InjectedScriptHost* host = V8InjectedScriptHost::toNative(args.Holder());
128    Database* database = V8Database::toNative(v8::Handle<v8::Object>::Cast(args[0]));
129    if (database)
130        return v8::Number::New(host->databaseIdImpl(database));
131#endif
132    return v8::Undefined();
133}
134
135v8::Handle<v8::Value> V8InjectedScriptHost::storageIdCallback(const v8::Arguments& args)
136{
137    if (args.Length() < 1)
138        return v8::Undefined();
139#if ENABLE(DOM_STORAGE)
140    INC_STATS("InjectedScriptHost.storageId()");
141    InjectedScriptHost* host = V8InjectedScriptHost::toNative(args.Holder());
142    Storage* storage = V8Storage::toNative(v8::Handle<v8::Object>::Cast(args[0]));
143    if (storage)
144        return v8::Number::New(host->storageIdImpl(storage));
145#endif
146    return v8::Undefined();
147}
148
149} // namespace WebCore
150