summaryrefslogtreecommitdiffstats
path: root/WebKit/gtk/webkit/webkitwebinspector.cpp
blob: f2079068e3c78b1088ebc6a20580395989361e9c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
/*
 * Copyright (C) 2008 Gustavo Noronha Silva
 * Copyright (C) 2008, 2009 Holger Hans Peter Freyther
 * Copyright (C) 2009 Collabora Ltd.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#include "config.h"
#include "webkitwebinspector.h"

#include "FocusController.h"
#include "Frame.h"
#include <glib/gi18n-lib.h>
#include "HitTestRequest.h"
#include "HitTestResult.h"
#include "InspectorClientGtk.h"
#include "IntPoint.h"
#include "Page.h"
#include "RenderLayer.h"
#include "RenderView.h"
#include "webkitmarshal.h"
#include "webkitprivate.h"

/**
 * SECTION:webkitwebinspector
 * @short_description: Access to the WebKit Inspector
 *
 * The WebKit Inspector is a graphical tool to inspect and change
 * the content of a #WebKitWebView. It also includes an interactive
 * JavaScriptDebugger. Using this class one can get a GtkWidget which
 * can be embedded into an application to show the inspector.
 *
 * The inspector is available when the #WebKitWebSettings of the
 * #WebKitWebView has set the #WebKitWebSettings:enable-developer-extras
 * to true otherwise no inspector is available.
 *
 * <informalexample><programlisting>
 * /<!-- -->* Enable the developer extras *<!-- -->/
 * WebKitWebSettings *setting = webkit_web_view_get_settings (WEBKIT_WEB_VIEW(my_webview));
 * g_object_set (G_OBJECT(settings), "enable-developer-extras", TRUE, NULL);
 *
 * /<!-- -->* load some data or reload to be able to inspect the page*<!-- -->/
 * webkit_web_view_open (WEBKIT_WEB_VIEW(my_webview), "http://www.gnome.org");
 *
 * /<!-- -->* Embed the inspector somewhere *<!-- -->/
 * WebKitWebInspector *inspector = webkit_web_view_get_inspector (WEBKIT_WEB_VIEW(my_webview));
 * g_signal_connect (G_OBJECT (inspector), "inspect-web-view", G_CALLBACK(create_gtk_window_around_it), NULL);
 * g_signal_connect (G_OBJECT (inspector), "show-window", G_CALLBACK(show_inpector_window), NULL));
 * g_signal_connect (G_OBJECT (inspector), "notify::inspected-uri", G_CALLBACK(inspected_uri_changed_do_stuff), NULL);
 * </programlisting></informalexample>
 */

using namespace WebKit;
using namespace WebCore;

enum {
    INSPECT_WEB_VIEW,
    SHOW_WINDOW,
    ATTACH_WINDOW,
    DETACH_WINDOW,
    CLOSE_WINDOW,
    FINISHED,
    LAST_SIGNAL
};

static guint webkit_web_inspector_signals[LAST_SIGNAL] = { 0, };

enum {
    PROP_0,

    PROP_WEB_VIEW,
    PROP_INSPECTED_URI,
    PROP_JAVASCRIPT_PROFILING_ENABLED,
    PROP_TIMELINE_PROFILING_ENABLED    
};

G_DEFINE_TYPE(WebKitWebInspector, webkit_web_inspector, G_TYPE_OBJECT)

struct _WebKitWebInspectorPrivate {
    WebCore::Page* page;
    WebKitWebView* inspector_view;
    gchar* inspected_uri;
};

#define WEBKIT_WEB_INSPECTOR_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE((obj), WEBKIT_TYPE_WEB_INSPECTOR, WebKitWebInspectorPrivate))

static void webkit_web_inspector_finalize(GObject* object);

static void webkit_web_inspector_set_property(GObject* object, guint prop_id, const GValue* value, GParamSpec* pspec);

static void webkit_web_inspector_get_property(GObject* object, guint prop_id, GValue* value, GParamSpec* pspec);

static gboolean webkit_inspect_web_view_request_handled(GSignalInvocationHint* ihint, GValue* returnAccu, const GValue* handlerReturn, gpointer dummy)
{
    gboolean continueEmission = TRUE;
    gpointer newWebView = g_value_get_object(handlerReturn);
    g_value_set_object(returnAccu, newWebView);

    if (newWebView)
        continueEmission = FALSE;

    return continueEmission;
}

static void webkit_web_inspector_class_init(WebKitWebInspectorClass* klass)
{
    GObjectClass* gobject_class = G_OBJECT_CLASS(klass);
    gobject_class->finalize = webkit_web_inspector_finalize;
    gobject_class->set_property = webkit_web_inspector_set_property;
    gobject_class->get_property = webkit_web_inspector_get_property;

    /**
     * WebKitWebInspector::inspect-web-view:
     * @web_inspector: the object on which the signal is emitted
     * @web_view: the #WebKitWebView which will be inspected
     *
     * Emitted when the user activates the 'inspect' context menu item
     * to inspect a web view. The application which is interested in
     * the inspector should create a window, or otherwise add the
     * #WebKitWebView it creates to an existing window.
     *
     * You don't need to handle the reference count of the
     * #WebKitWebView instance you create; the widget to which you add
     * it will do that.
     *
     * Return value: (transfer none): a newly allocated #WebKitWebView or %NULL
     *
     * Since: 1.0.3
     */
    webkit_web_inspector_signals[INSPECT_WEB_VIEW] = g_signal_new("inspect-web-view",
            G_TYPE_FROM_CLASS(klass),
            (GSignalFlags)G_SIGNAL_RUN_LAST,
            0,
            webkit_inspect_web_view_request_handled,
            NULL,
            webkit_marshal_OBJECT__OBJECT,
            WEBKIT_TYPE_WEB_VIEW , 1,
            WEBKIT_TYPE_WEB_VIEW);

    /**
     * WebKitWebInspector::show-window:
     * @web_inspector: the object on which the signal is emitted
     * @return: %TRUE if the signal has been handled
     *
     * Emitted when the inspector window should be displayed. Notice
     * that the window must have been created already by handling
     * #WebKitWebInspector::inspect-web-view.
     *
     * Since: 1.0.3
     */
    webkit_web_inspector_signals[SHOW_WINDOW] = g_signal_new("show-window",
            G_TYPE_FROM_CLASS(klass),
            (GSignalFlags)G_SIGNAL_RUN_LAST,
            0,
            g_signal_accumulator_true_handled,
            NULL,
            webkit_marshal_BOOLEAN__VOID,
            G_TYPE_BOOLEAN , 0);

    /**
     * WebKitWebInspector::attach-window:
     * @web_inspector: the object on which the signal is emitted
     * @return: %TRUE if the signal has been handled
     *
     * Emitted when the inspector should appear at the same window as
     * the #WebKitWebView being inspected.
     *
     * Since: 1.0.3
     */
    webkit_web_inspector_signals[ATTACH_WINDOW] = g_signal_new("attach-window",
            G_TYPE_FROM_CLASS(klass),
            (GSignalFlags)G_SIGNAL_RUN_LAST,
            0,
            g_signal_accumulator_true_handled,
            NULL,
            webkit_marshal_BOOLEAN__VOID,
            G_TYPE_BOOLEAN , 0);

    /**
     * WebKitWebInspector::detach-window:
     * @web_inspector: the object on which the signal is emitted
     * @return: %TRUE if the signal has been handled
     *
     * Emitted when the inspector should appear in a separate window.
     *
     * Since: 1.0.3
     */
    webkit_web_inspector_signals[DETACH_WINDOW] = g_signal_new("detach-window",
            G_TYPE_FROM_CLASS(klass),
            (GSignalFlags)G_SIGNAL_RUN_LAST,
            0,
            g_signal_accumulator_true_handled,
            NULL,
            webkit_marshal_BOOLEAN__VOID,
            G_TYPE_BOOLEAN , 0);

    /**
     * WebKitWebInspector::close-window:
     * @web_inspector: the object on which the signal is emitted
     * @return: %TRUE if the signal has been handled
     *
     * Emitted when the inspector window should be closed. You can
     * destroy the window or hide it so that it can be displayed again
     * by handling #WebKitWebInspector::show-window later on.
     *
     * Notice that the inspected #WebKitWebView may no longer exist
     * when this signal is emitted.
     *
     * Notice, too, that if you decide to destroy the window,
     * #WebKitWebInspector::inspect-web-view will be emmited again, when the user
     * inspects an element.
     *
     * Since: 1.0.3
     */
    webkit_web_inspector_signals[CLOSE_WINDOW] = g_signal_new("close-window",
            G_TYPE_FROM_CLASS(klass),
            (GSignalFlags)G_SIGNAL_RUN_LAST,
            0,
            g_signal_accumulator_true_handled,
            NULL,
            webkit_marshal_BOOLEAN__VOID,
            G_TYPE_BOOLEAN , 0);

    /**
     * WebKitWebInspector::finished:
     * @web_inspector: the object on which the signal is emitted
     *
     * Emitted when the inspection is done. You should release your
     * references on the inspector at this time. The inspected
     * #WebKitWebView may no longer exist when this signal is emitted.
     *
     * Since: 1.0.3
     */
    webkit_web_inspector_signals[FINISHED] = g_signal_new("finished",
            G_TYPE_FROM_CLASS(klass),
            (GSignalFlags)G_SIGNAL_RUN_LAST,
            0,
            NULL,
            NULL,
            g_cclosure_marshal_VOID__VOID,
            G_TYPE_NONE , 0);

    /*
     * properties
     */

    /**
     * WebKitWebInspector:web-view:
     *
     * The Web View that renders the Web Inspector itself.
     *
     * Since: 1.0.3
     */
    g_object_class_install_property(gobject_class, PROP_WEB_VIEW,
                                    g_param_spec_object("web-view",
                                                        _("Web View"),
                                                        _("The Web View that renders the Web Inspector itself"),
                                                        WEBKIT_TYPE_WEB_VIEW,
                                                        WEBKIT_PARAM_READABLE));

    /**
     * WebKitWebInspector:inspected-uri:
     *
     * The URI that is currently being inspected.
     *
     * Since: 1.0.3
     */
    g_object_class_install_property(gobject_class, PROP_INSPECTED_URI,
                                    g_param_spec_string("inspected-uri",
                                                        _("Inspected URI"),
                                                        _("The URI that is currently being inspected"),
                                                        NULL,
                                                        WEBKIT_PARAM_READABLE));

    /**
    * WebKitWebInspector:javascript-profiling-enabled
    *
    * This is enabling JavaScript profiling in the Inspector. This means
    * that Console.profiles will return the profiles.
    *
    * Since: 1.1.1
    */
    g_object_class_install_property(gobject_class,
                                    PROP_JAVASCRIPT_PROFILING_ENABLED,
                                    g_param_spec_boolean(
                                        "javascript-profiling-enabled",
                                        _("Enable JavaScript profiling"),
                                        _("Profile the executed JavaScript."),
                                        FALSE,
                                        WEBKIT_PARAM_READWRITE));

    /**
    * WebKitWebInspector:timeline-profiling-enabled
    *
    * This is enabling Timeline profiling in the Inspector.
    *
    * Since: 1.1.17
    */
    g_object_class_install_property(gobject_class,
                                    PROP_TIMELINE_PROFILING_ENABLED,
                                    g_param_spec_boolean(
                                        "timeline-profiling-enabled",
                                        _("Enable Timeline profiling"),
                                        _("Profile the WebCore instrumentation."),
                                        FALSE,
                                        WEBKIT_PARAM_READWRITE));

    g_type_class_add_private(klass, sizeof(WebKitWebInspectorPrivate));
}

static void webkit_web_inspector_init(WebKitWebInspector* web_inspector)
{
    web_inspector->priv = WEBKIT_WEB_INSPECTOR_GET_PRIVATE(web_inspector);
}

static void webkit_web_inspector_finalize(GObject* object)
{
    WebKitWebInspector* web_inspector = WEBKIT_WEB_INSPECTOR(object);
    WebKitWebInspectorPrivate* priv = web_inspector->priv;

    if (priv->inspector_view)
        g_object_unref(priv->inspector_view);

    if (priv->inspected_uri)
        g_free(priv->inspected_uri);

    G_OBJECT_CLASS(webkit_web_inspector_parent_class)->finalize(object);
}

static void webkit_web_inspector_set_property(GObject* object, guint prop_id, const GValue* value, GParamSpec* pspec)
{
    WebKitWebInspector* web_inspector = WEBKIT_WEB_INSPECTOR(object);
    WebKitWebInspectorPrivate* priv = web_inspector->priv;

    switch(prop_id) {
    case PROP_JAVASCRIPT_PROFILING_ENABLED: {
#if ENABLE(JAVASCRIPT_DEBUGGER)
        bool enabled = g_value_get_boolean(value);
        WebCore::InspectorController* controller = priv->page->inspectorController();
        if (enabled)
            controller->enableProfiler();
        else
            controller->disableProfiler();
#else
        g_message("PROP_JAVASCRIPT_PROFILING_ENABLED is not work because of the javascript debugger is disabled\n");
#endif
        break;
    }
    case PROP_TIMELINE_PROFILING_ENABLED: {
        bool enabled = g_value_get_boolean(value);
        WebCore::InspectorController* controller = priv->page->inspectorController();
        if (enabled)
            controller->startTimelineProfiler();
        else
            controller->stopTimelineProfiler();
        break;
    }
    default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
        break;
    }
}

static void webkit_web_inspector_get_property(GObject* object, guint prop_id, GValue* value, GParamSpec* pspec)
{
    WebKitWebInspector* web_inspector = WEBKIT_WEB_INSPECTOR(object);
    WebKitWebInspectorPrivate* priv = web_inspector->priv;

    switch (prop_id) {
    case PROP_WEB_VIEW:
        g_value_set_object(value, priv->inspector_view);
        break;
    case PROP_INSPECTED_URI:
        g_value_set_string(value, priv->inspected_uri);
        break;
    case PROP_JAVASCRIPT_PROFILING_ENABLED:
#if ENABLE(JAVASCRIPT_DEBUGGER)
        g_value_set_boolean(value, priv->page->inspectorController()->profilerEnabled());
#else
        g_message("PROP_JAVASCRIPT_PROFILING_ENABLED is not work because of the javascript debugger is disabled\n");
#endif
        break;
    case PROP_TIMELINE_PROFILING_ENABLED:
        g_value_set_boolean(value, priv->page->inspectorController()->timelineAgent() != 0);
        break;
    default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
        break;
    }
}

// internal use only
void webkit_web_inspector_set_web_view(WebKitWebInspector *web_inspector, WebKitWebView *web_view)
{
    g_return_if_fail(WEBKIT_IS_WEB_INSPECTOR(web_inspector));
    g_return_if_fail(WEBKIT_IS_WEB_VIEW(web_view));

    WebKitWebInspectorPrivate* priv = web_inspector->priv;

    if (priv->inspector_view)
        g_object_unref(priv->inspector_view);

    g_object_ref(web_view);
    priv->inspector_view = web_view;
}

/**
 * webkit_web_inspector_get_web_view:
 *
 * Obtains the #WebKitWebView that is used to render the
 * inspector. The #WebKitWebView instance is created by the
 * application, by handling the #WebKitWebInspector::inspect-web-view signal. This means
 * that this method may return %NULL if the user hasn't inspected
 * anything.
 *
 * Returns: (transfer none): the #WebKitWebView instance that is used
 * to render the inspector or %NULL if it is not yet created.
 *
 * Since: 1.0.3
 **/
WebKitWebView* webkit_web_inspector_get_web_view(WebKitWebInspector *web_inspector)
{
    WebKitWebInspectorPrivate* priv = web_inspector->priv;

    return priv->inspector_view;
}

// internal use only
void webkit_web_inspector_set_inspected_uri(WebKitWebInspector* web_inspector, const gchar* inspected_uri)
{
    g_return_if_fail(WEBKIT_IS_WEB_INSPECTOR(web_inspector));

    WebKitWebInspectorPrivate* priv = web_inspector->priv;

    g_free(priv->inspected_uri);
    priv->inspected_uri = g_strdup(inspected_uri);
}

/**
 * webkit_web_inspector_get_inspected_uri:
 *
 * Obtains the URI that is currently being inspected.
 *
 * Returns: a pointer to the URI as an internally allocated string; it
 * should not be freed, modified or stored.
 *
 * Since: 1.0.3
 **/
const gchar* webkit_web_inspector_get_inspected_uri(WebKitWebInspector *web_inspector)
{
    WebKitWebInspectorPrivate* priv = web_inspector->priv;

    return priv->inspected_uri;
}

void
webkit_web_inspector_set_inspector_client(WebKitWebInspector* web_inspector, WebCore::Page* page)
{
    WebKitWebInspectorPrivate* priv = web_inspector->priv;

    priv->page = page;
}

/**
 * webkit_web_inspector_show:
 * @webInspector: the #WebKitWebInspector that will be shown
 *
 * Causes the Web Inspector to be shown.
 *
 * Since: 1.1.17
 */
void webkit_web_inspector_show(WebKitWebInspector* webInspector)
{
    g_return_if_fail(WEBKIT_IS_WEB_INSPECTOR(webInspector));

    WebKitWebInspectorPrivate* priv = webInspector->priv;

    Frame* frame = priv->page->focusController()->focusedOrMainFrame();
    FrameView* view = frame->view();

    if (!view)
        return;

    priv->page->inspectorController()->show();
}

/**
 * webkit_web_inspector_inspect_coordinates:
 * @web_inspector: the #WebKitWebInspector that will do the inspection
 * @x: the X coordinate of the node to be inspected
 * @y: the Y coordinate of the node to be inspected
 *
 * Causes the Web Inspector to inspect the node that is located at the
 * given coordinates of the widget. The coordinates should be relative
 * to the #WebKitWebView widget, not to the scrollable content, and
 * may be obtained from a #GdkEvent directly.
 *
 * This means @x, and @y being zero doesn't guarantee you will hit the
 * left-most top corner of the content, since the contents may have
 * been scrolled.
 *
 * Since: 1.1.17
 */
void webkit_web_inspector_inspect_coordinates(WebKitWebInspector* webInspector, gdouble x, gdouble y)
{
    g_return_if_fail(WEBKIT_IS_WEB_INSPECTOR(webInspector));
    g_return_if_fail(x >= 0 && y >= 0);

    WebKitWebInspectorPrivate* priv = webInspector->priv;

    Frame* frame = priv->page->focusController()->focusedOrMainFrame();
    FrameView* view = frame->view();

    if (!view)
        return;

    HitTestRequest request(HitTestRequest::ReadOnly | HitTestRequest::Active);
    IntPoint documentPoint = view->windowToContents(IntPoint(static_cast<int>(x), static_cast<int>(y)));
    HitTestResult result(documentPoint);

    frame->contentRenderer()->layer()->hitTest(request, result);
    priv->page->inspectorController()->inspect(result.innerNonSharedNode());
}

/**
 * webkit_web_inspector_close:
 * @webInspector: the #WebKitWebInspector that will be closed
 *
 * Causes the Web Inspector to be closed.
 *
 * Since: 1.1.17
 */
void webkit_web_inspector_close(WebKitWebInspector* webInspector)
{
    g_return_if_fail(WEBKIT_IS_WEB_INSPECTOR(webInspector));

    WebKitWebInspectorPrivate* priv = webInspector->priv;
    priv->page->inspectorController()->close();
}

void webkit_web_inspector_execute_script(WebKitWebInspector* webInspector, long callId, const gchar* script)
{
    g_return_if_fail(WEBKIT_IS_WEB_INSPECTOR(webInspector));
    g_return_if_fail(script);

    WebKitWebInspectorPrivate* priv = webInspector->priv;
    priv->page->inspectorController()->evaluateForTestInFrontend(callId, script);
}