~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to src/3rdparty/webkit/WebCore/bindings/js/JSInspectorBackendCustom.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Alessandro Ghersi
  • Date: 2009-11-02 18:30:08 UTC
  • mfrom: (1.2.2 upstream)
  • mto: (15.2.5 experimental)
  • mto: This revision was merged to the branch mainline in revision 88.
  • Revision ID: james.westby@ubuntu.com-20091102183008-b6a4gcs128mvfb3m
Tags: upstream-4.6.0~beta1
ImportĀ upstreamĀ versionĀ 4.6.0~beta1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2007, 2008 Apple Inc. All rights reserved.
 
3
 * Copyright (C) 2008 Matt Lilek <webkit@mattlilek.com>
 
4
 * Copyright (C) 2009 Google Inc. All rights reserved.
 
5
 *
 
6
 * Redistribution and use in source and binary forms, with or without
 
7
 * modification, are permitted provided that the following conditions are
 
8
 * met:
 
9
 *
 
10
 *     * Redistributions of source code must retain the above copyright
 
11
 * notice, this list of conditions and the following disclaimer.
 
12
 *     * Redistributions in binary form must reproduce the above
 
13
 * copyright notice, this list of conditions and the following disclaimer
 
14
 * in the documentation and/or other materials provided with the
 
15
 * distribution.
 
16
 *     * Neither the name of Google Inc. nor the names of its
 
17
 * contributors may be used to endorse or promote products derived from
 
18
 * this software without specific prior written permission.
 
19
 *
 
20
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 
21
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 
22
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 
23
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 
24
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 
25
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 
26
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 
27
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 
28
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 
29
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 
30
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
31
 */
 
32
 
 
33
#include "config.h"
 
34
#include "JSInspectorBackend.h"
 
35
 
 
36
#if ENABLE(INSPECTOR)
 
37
 
 
38
#include "Console.h"
 
39
#if ENABLE(DATABASE)
 
40
#include "Database.h"
 
41
#include "JSDatabase.h"
 
42
#endif
 
43
#include "ExceptionCode.h"
 
44
#include "Frame.h"
 
45
#include "FrameLoader.h"
 
46
#include "InspectorBackend.h"
 
47
#include "InspectorController.h"
 
48
#include "InspectorResource.h"
 
49
#include "JSDOMWindow.h"
 
50
#include "JSInspectedObjectWrapper.h"
 
51
#include "JSInspectorCallbackWrapper.h"
 
52
#include "JSNode.h"
 
53
#include "JSRange.h"
 
54
#include "Node.h"
 
55
#include "Page.h"
 
56
#if ENABLE(DOM_STORAGE)
 
57
#include "Storage.h"
 
58
#include "JSStorage.h"
 
59
#endif
 
60
#include "TextIterator.h"
 
61
#include "VisiblePosition.h"
 
62
#include <runtime/JSArray.h>
 
63
#include <runtime/JSLock.h>
 
64
#include <wtf/Vector.h>
 
65
 
 
66
#if ENABLE(JAVASCRIPT_DEBUGGER)
 
67
#include "JavaScriptCallFrame.h"
 
68
#include "JavaScriptDebugServer.h"
 
69
#include "JavaScriptProfile.h"
 
70
#include "JSJavaScriptCallFrame.h"
 
71
#include <profiler/Profile.h>
 
72
#include <profiler/Profiler.h>
 
73
#endif
 
74
 
 
75
using namespace JSC;
 
76
 
 
77
namespace WebCore {
 
78
 
 
79
JSValue JSInspectorBackend::highlightDOMNode(JSC::ExecState* exec, const JSC::ArgList& args)
 
80
{
 
81
    if (args.size() < 1)
 
82
        return jsUndefined();
 
83
 
 
84
    impl()->highlight(args.at(0).toInt32(exec));
 
85
    return jsUndefined();
 
86
}
 
87
 
 
88
JSValue JSInspectorBackend::search(ExecState* exec, const ArgList& args)
 
89
{
 
90
    if (args.size() < 2)
 
91
        return jsUndefined();
 
92
 
 
93
    Node* node = toNode(args.at(0));
 
94
    if (!node)
 
95
        return jsUndefined();
 
96
 
 
97
    String target = args.at(1).toString(exec);
 
98
    if (exec->hadException())
 
99
        return jsUndefined();
 
100
 
 
101
    MarkedArgumentBuffer result;
 
102
    RefPtr<Range> searchRange(rangeOfContents(node));
 
103
 
 
104
    ExceptionCode ec = 0;
 
105
    do {
 
106
        RefPtr<Range> resultRange(findPlainText(searchRange.get(), target, true, false));
 
107
        if (resultRange->collapsed(ec))
 
108
            break;
 
109
 
 
110
        // A non-collapsed result range can in some funky whitespace cases still not
 
111
        // advance the range's start position (4509328). Break to avoid infinite loop.
 
112
        VisiblePosition newStart = endVisiblePosition(resultRange.get(), DOWNSTREAM);
 
113
        if (newStart == startVisiblePosition(searchRange.get(), DOWNSTREAM))
 
114
            break;
 
115
 
 
116
        result.append(toJS(exec, resultRange.get()));
 
117
 
 
118
        setStart(searchRange.get(), newStart);
 
119
    } while (true);
 
120
 
 
121
    return constructArray(exec, result);
 
122
}
 
123
 
 
124
#if ENABLE(DATABASE)
 
125
JSValue JSInspectorBackend::databaseTableNames(ExecState* exec, const ArgList& args)
 
126
{
 
127
    if (args.size() < 1)
 
128
        return jsUndefined();
 
129
 
 
130
    JSQuarantinedObjectWrapper* wrapper = JSQuarantinedObjectWrapper::asWrapper(args.at(0));
 
131
    if (!wrapper)
 
132
        return jsUndefined();
 
133
 
 
134
    Database* database = toDatabase(wrapper->unwrappedObject());
 
135
    if (!database)
 
136
        return jsUndefined();
 
137
 
 
138
    MarkedArgumentBuffer result;
 
139
 
 
140
    Vector<String> tableNames = database->tableNames();
 
141
    unsigned length = tableNames.size();
 
142
    for (unsigned i = 0; i < length; ++i)
 
143
        result.append(jsString(exec, tableNames[i]));
 
144
 
 
145
    return constructArray(exec, result);
 
146
}
 
147
#endif
 
148
 
 
149
JSValue JSInspectorBackend::inspectedWindow(ExecState*, const ArgList&)
 
150
{
 
151
    InspectorController* ic = impl()->inspectorController();
 
152
    if (!ic)
 
153
        return jsUndefined();
 
154
    JSDOMWindow* inspectedWindow = toJSDOMWindow(ic->inspectedPage()->mainFrame());
 
155
    return JSInspectedObjectWrapper::wrap(inspectedWindow->globalExec(), inspectedWindow);
 
156
}
 
157
 
 
158
JSValue JSInspectorBackend::setting(ExecState* exec, const ArgList& args)
 
159
{
 
160
    if (args.size() < 1)
 
161
        return jsUndefined();
 
162
 
 
163
    String key = args.at(0).toString(exec);
 
164
    if (exec->hadException())
 
165
        return jsUndefined();
 
166
 
 
167
    InspectorController* ic = impl()->inspectorController();
 
168
    if (!ic)
 
169
        return jsUndefined();
 
170
    const InspectorController::Setting& setting = ic->setting(key);
 
171
 
 
172
    switch (setting.type()) {
 
173
        default:
 
174
        case InspectorController::Setting::NoType:
 
175
            return jsUndefined();
 
176
        case InspectorController::Setting::StringType:
 
177
            return jsString(exec, setting.string());
 
178
        case InspectorController::Setting::DoubleType:
 
179
            return jsNumber(exec, setting.doubleValue());
 
180
        case InspectorController::Setting::IntegerType:
 
181
            return jsNumber(exec, setting.integerValue());
 
182
        case InspectorController::Setting::BooleanType:
 
183
            return jsBoolean(setting.booleanValue());
 
184
        case InspectorController::Setting::StringVectorType: {
 
185
            MarkedArgumentBuffer stringsArray;
 
186
            const Vector<String>& strings = setting.stringVector();
 
187
            const unsigned length = strings.size();
 
188
            for (unsigned i = 0; i < length; ++i)
 
189
                stringsArray.append(jsString(exec, strings[i]));
 
190
            return constructArray(exec, stringsArray);
 
191
        }
 
192
    }
 
193
}
 
194
 
 
195
JSValue JSInspectorBackend::setSetting(ExecState* exec, const ArgList& args)
 
196
{
 
197
    if (args.size() < 2)
 
198
        return jsUndefined();
 
199
 
 
200
    String key = args.at(0).toString(exec);
 
201
    if (exec->hadException())
 
202
        return jsUndefined();
 
203
 
 
204
    InspectorController::Setting setting;
 
205
 
 
206
    JSValue value = args.at(1);
 
207
    if (value.isUndefined() || value.isNull()) {
 
208
        // Do nothing. The setting is already NoType.
 
209
        ASSERT(setting.type() == InspectorController::Setting::NoType);
 
210
    } else if (value.isString())
 
211
        setting.set(value.toString(exec));
 
212
    else if (value.isNumber())
 
213
        setting.set(value.toNumber(exec));
 
214
    else if (value.isBoolean())
 
215
        setting.set(value.toBoolean(exec));
 
216
    else {
 
217
        JSArray* jsArray = asArray(value);
 
218
        if (!jsArray)
 
219
            return jsUndefined();
 
220
        Vector<String> strings;
 
221
        for (unsigned i = 0; i < jsArray->length(); ++i) {
 
222
            String item = jsArray->get(exec, i).toString(exec);
 
223
            if (exec->hadException())
 
224
                return jsUndefined();
 
225
            strings.append(item);
 
226
        }
 
227
        setting.set(strings);
 
228
    }
 
229
 
 
230
    if (exec->hadException())
 
231
        return jsUndefined();
 
232
 
 
233
    InspectorController* ic = impl()->inspectorController();
 
234
    if (ic)
 
235
        ic->setSetting(key, setting);
 
236
 
 
237
    return jsUndefined();
 
238
}
 
239
 
 
240
JSValue JSInspectorBackend::wrapCallback(ExecState* exec, const ArgList& args)
 
241
{
 
242
    if (args.size() < 1)
 
243
        return jsUndefined();
 
244
 
 
245
    return JSInspectorCallbackWrapper::wrap(exec, args.at(0));
 
246
}
 
247
 
 
248
#if ENABLE(JAVASCRIPT_DEBUGGER)
 
249
 
 
250
JSValue JSInspectorBackend::currentCallFrame(ExecState* exec, const ArgList&)
 
251
{
 
252
    JavaScriptCallFrame* callFrame = impl()->currentCallFrame();
 
253
    if (!callFrame || !callFrame->isValid())
 
254
        return jsUndefined();
 
255
 
 
256
    // FIXME: I am not sure if this is actually needed. Can we just use exec?
 
257
    ExecState* globalExec = callFrame->scopeChain()->globalObject->globalExec();
 
258
 
 
259
    JSLock lock(SilenceAssertionsOnly);
 
260
    return JSInspectedObjectWrapper::wrap(globalExec, toJS(exec, callFrame));
 
261
}
 
262
 
 
263
JSValue JSInspectorBackend::profiles(JSC::ExecState* exec, const JSC::ArgList&)
 
264
{
 
265
    JSLock lock(SilenceAssertionsOnly);
 
266
    MarkedArgumentBuffer result;
 
267
    InspectorController* ic = impl()->inspectorController();
 
268
    if (!ic)
 
269
        return jsUndefined();
 
270
    const Vector<RefPtr<Profile> >& profiles = ic->profiles();
 
271
 
 
272
    for (size_t i = 0; i < profiles.size(); ++i)
 
273
        result.append(toJS(exec, profiles[i].get()));
 
274
 
 
275
    return constructArray(exec, result);
 
276
}
 
277
 
 
278
#endif
 
279
 
 
280
JSValue JSInspectorBackend::nodeForId(ExecState* exec, const ArgList& args)
 
281
{
 
282
    if (args.size() < 1)
 
283
        return jsUndefined();
 
284
 
 
285
    Node* node = impl()->nodeForId(args.at(0).toInt32(exec));
 
286
    if (!node)
 
287
        return jsUndefined();
 
288
 
 
289
    InspectorController* ic = impl()->inspectorController();
 
290
    if (!ic)
 
291
        return jsUndefined();
 
292
 
 
293
    JSLock lock(SilenceAssertionsOnly);
 
294
    JSDOMWindow* inspectedWindow = toJSDOMWindow(ic->inspectedPage()->mainFrame());
 
295
    return JSInspectedObjectWrapper::wrap(inspectedWindow->globalExec(), toJS(exec, deprecatedGlobalObjectForPrototype(inspectedWindow->globalExec()), node));
 
296
}
 
297
 
 
298
JSValue JSInspectorBackend::wrapObject(ExecState*, const ArgList& args)
 
299
{
 
300
    if (args.size() < 1)
 
301
        return jsUndefined();
 
302
 
 
303
    return impl()->wrapObject(ScriptValue(args.at(0))).jsValue();
 
304
}
 
305
 
 
306
JSValue JSInspectorBackend::unwrapObject(ExecState* exec, const ArgList& args)
 
307
{
 
308
    if (args.size() < 1)
 
309
        return jsUndefined();
 
310
 
 
311
    return impl()->unwrapObject(args.at(0).toString(exec)).jsValue();
 
312
}
 
313
 
 
314
JSValue JSInspectorBackend::pushNodePathToFrontend(ExecState* exec, const ArgList& args)
 
315
{
 
316
    if (args.size() < 2)
 
317
        return jsUndefined();
 
318
 
 
319
    JSQuarantinedObjectWrapper* wrapper = JSQuarantinedObjectWrapper::asWrapper(args.at(0));
 
320
    if (!wrapper)
 
321
        return jsUndefined();
 
322
 
 
323
    Node* node = toNode(wrapper->unwrappedObject());
 
324
    if (!node)
 
325
        return jsUndefined();
 
326
 
 
327
    bool selectInUI = args.at(1).toBoolean(exec);
 
328
    return jsNumber(exec, impl()->pushNodePathToFrontend(node, selectInUI));
 
329
}
 
330
 
 
331
#if ENABLE(DATABASE)
 
332
JSValue JSInspectorBackend::selectDatabase(ExecState*, const ArgList& args)
 
333
{
 
334
    if (args.size() < 1)
 
335
        return jsUndefined();
 
336
 
 
337
    JSQuarantinedObjectWrapper* wrapper = JSQuarantinedObjectWrapper::asWrapper(args.at(0));
 
338
    if (!wrapper)
 
339
        return jsUndefined();
 
340
 
 
341
    Database* database = toDatabase(wrapper->unwrappedObject());
 
342
    if (database)
 
343
        impl()->selectDatabase(database);
 
344
    return jsUndefined();
 
345
}
 
346
#endif
 
347
 
 
348
#if ENABLE(DOM_STORAGE)
 
349
JSValue JSInspectorBackend::selectDOMStorage(ExecState*, const ArgList& args)
 
350
{
 
351
    if (args.size() < 1)
 
352
        return jsUndefined();
 
353
    InspectorController* ic = impl()->inspectorController();
 
354
    if (!ic)
 
355
        return jsUndefined();
 
356
 
 
357
    JSQuarantinedObjectWrapper* wrapper = JSQuarantinedObjectWrapper::asWrapper(args.at(0));
 
358
    if (!wrapper)
 
359
        return jsUndefined();
 
360
 
 
361
    Storage* storage = toStorage(wrapper->unwrappedObject());
 
362
    if (storage)
 
363
        impl()->selectDOMStorage(storage);
 
364
    return jsUndefined();
 
365
}
 
366
#endif
 
367
 
 
368
} // namespace WebCore
 
369
 
 
370
#endif // ENABLE(INSPECTOR)