2 * Copyright (C) 2008 Gustavo Noronha Silva
3 * Copyright (C) 2008, 2009 Holger Hans Peter Freyther
4 * Copyright (C) 2009 Collabora Ltd.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public License
17 * along with this library; see the file COPYING.LIB. If not, write to
18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
23 #include "webkitwebinspector.h"
25 #include "DumpRenderTreeSupportGtk.h"
26 #include "FocusController.h"
28 #include "HitTestRequest.h"
29 #include "HitTestResult.h"
30 #include "InspectorClientGtk.h"
31 #include "InspectorController.h"
32 #include "InspectorInstrumentation.h"
35 #include "RenderLayer.h"
36 #include "RenderView.h"
37 #include "webkit/WebKitDOMNodePrivate.h"
38 #include "webkitglobalsprivate.h"
39 #include "webkitmarshal.h"
40 #include "webkitwebinspectorprivate.h"
41 #include <glib/gi18n-lib.h>
44 * SECTION:webkitwebinspector
45 * @short_description: Access to the WebKit Inspector
47 * The WebKit Inspector is a graphical tool to inspect and change
48 * the content of a #WebKitWebView. It also includes an interactive
49 * JavaScriptDebugger. Using this class one can get a GtkWidget which
50 * can be embedded into an application to show the inspector.
52 * The inspector is available when the #WebKitWebSettings of the
53 * #WebKitWebView has set the #WebKitWebSettings:enable-developer-extras
54 * to true otherwise no inspector is available.
56 * <informalexample><programlisting>
57 * /<!-- -->* Enable the developer extras *<!-- -->/
58 * WebKitWebSettings *setting = webkit_web_view_get_settings (WEBKIT_WEB_VIEW(my_webview));
59 * g_object_set (G_OBJECT(settings), "enable-developer-extras", TRUE, NULL);
61 * /<!-- -->* load some data or reload to be able to inspect the page*<!-- -->/
62 * webkit_web_view_open (WEBKIT_WEB_VIEW(my_webview), "http://www.gnome.org");
64 * /<!-- -->* Embed the inspector somewhere *<!-- -->/
65 * WebKitWebInspector *inspector = webkit_web_view_get_inspector (WEBKIT_WEB_VIEW(my_webview));
66 * g_signal_connect (G_OBJECT (inspector), "inspect-web-view", G_CALLBACK(create_gtk_window_around_it), NULL);
67 * g_signal_connect (G_OBJECT (inspector), "show-window", G_CALLBACK(show_inpector_window), NULL));
68 * g_signal_connect (G_OBJECT (inspector), "notify::inspected-uri", G_CALLBACK(inspected_uri_changed_do_stuff), NULL);
69 * </programlisting></informalexample>
72 using namespace WebKit;
73 using namespace WebCore;
85 static guint webkit_web_inspector_signals[LAST_SIGNAL] = { 0, };
92 PROP_JAVASCRIPT_PROFILING_ENABLED,
93 PROP_TIMELINE_PROFILING_ENABLED
96 G_DEFINE_TYPE(WebKitWebInspector, webkit_web_inspector, G_TYPE_OBJECT)
98 struct _WebKitWebInspectorPrivate {
100 WebKitWebView* inspector_view;
101 gchar* inspected_uri;
104 static void webkit_web_inspector_finalize(GObject* object);
106 static void webkit_web_inspector_set_property(GObject* object, guint prop_id, const GValue* value, GParamSpec* pspec);
108 static void webkit_web_inspector_get_property(GObject* object, guint prop_id, GValue* value, GParamSpec* pspec);
110 static gboolean webkit_inspect_web_view_request_handled(GSignalInvocationHint* ihint, GValue* returnAccu, const GValue* handlerReturn, gpointer dummy)
112 gboolean continueEmission = TRUE;
113 gpointer newWebView = g_value_get_object(handlerReturn);
114 g_value_set_object(returnAccu, newWebView);
117 continueEmission = FALSE;
119 return continueEmission;
122 static void webkit_web_inspector_class_init(WebKitWebInspectorClass* klass)
124 GObjectClass* gobject_class = G_OBJECT_CLASS(klass);
125 gobject_class->finalize = webkit_web_inspector_finalize;
126 gobject_class->set_property = webkit_web_inspector_set_property;
127 gobject_class->get_property = webkit_web_inspector_get_property;
130 * WebKitWebInspector::inspect-web-view:
131 * @web_inspector: the object on which the signal is emitted
132 * @web_view: the #WebKitWebView which will be inspected
134 * Emitted when the user activates the 'inspect' context menu item
135 * to inspect a web view. The application which is interested in
136 * the inspector should create a window, or otherwise add the
137 * #WebKitWebView it creates to an existing window.
139 * You don't need to handle the reference count of the
140 * #WebKitWebView instance you create; the widget to which you add
143 * Return value: (transfer none): a newly allocated #WebKitWebView or %NULL
147 webkit_web_inspector_signals[INSPECT_WEB_VIEW] = g_signal_new("inspect-web-view",
148 G_TYPE_FROM_CLASS(klass),
149 (GSignalFlags)G_SIGNAL_RUN_LAST,
151 webkit_inspect_web_view_request_handled,
153 webkit_marshal_OBJECT__OBJECT,
154 WEBKIT_TYPE_WEB_VIEW , 1,
155 WEBKIT_TYPE_WEB_VIEW);
158 * WebKitWebInspector::show-window:
159 * @web_inspector: the object on which the signal is emitted
160 * @return: %TRUE if the signal has been handled
162 * Emitted when the inspector window should be displayed. Notice
163 * that the window must have been created already by handling
164 * #WebKitWebInspector::inspect-web-view.
168 webkit_web_inspector_signals[SHOW_WINDOW] = g_signal_new("show-window",
169 G_TYPE_FROM_CLASS(klass),
170 (GSignalFlags)G_SIGNAL_RUN_LAST,
172 g_signal_accumulator_true_handled,
174 webkit_marshal_BOOLEAN__VOID,
178 * WebKitWebInspector::attach-window:
179 * @web_inspector: the object on which the signal is emitted
180 * @return: %TRUE if the signal has been handled
182 * Emitted when the inspector should appear at the same window as
183 * the #WebKitWebView being inspected.
187 webkit_web_inspector_signals[ATTACH_WINDOW] = g_signal_new("attach-window",
188 G_TYPE_FROM_CLASS(klass),
189 (GSignalFlags)G_SIGNAL_RUN_LAST,
191 g_signal_accumulator_true_handled,
193 webkit_marshal_BOOLEAN__VOID,
197 * WebKitWebInspector::detach-window:
198 * @web_inspector: the object on which the signal is emitted
199 * @return: %TRUE if the signal has been handled
201 * Emitted when the inspector should appear in a separate window.
205 webkit_web_inspector_signals[DETACH_WINDOW] = g_signal_new("detach-window",
206 G_TYPE_FROM_CLASS(klass),
207 (GSignalFlags)G_SIGNAL_RUN_LAST,
209 g_signal_accumulator_true_handled,
211 webkit_marshal_BOOLEAN__VOID,
215 * WebKitWebInspector::close-window:
216 * @web_inspector: the object on which the signal is emitted
217 * @return: %TRUE if the signal has been handled
219 * Emitted when the inspector window should be closed. You can
220 * destroy the window or hide it so that it can be displayed again
221 * by handling #WebKitWebInspector::show-window later on.
223 * Notice that the inspected #WebKitWebView may no longer exist
224 * when this signal is emitted.
226 * Notice, too, that if you decide to destroy the window,
227 * #WebKitWebInspector::inspect-web-view will be emmited again, when the user
228 * inspects an element.
232 webkit_web_inspector_signals[CLOSE_WINDOW] = g_signal_new("close-window",
233 G_TYPE_FROM_CLASS(klass),
234 (GSignalFlags)G_SIGNAL_RUN_LAST,
236 g_signal_accumulator_true_handled,
238 webkit_marshal_BOOLEAN__VOID,
242 * WebKitWebInspector::finished:
243 * @web_inspector: the object on which the signal is emitted
245 * Emitted when the inspection is done. You should release your
246 * references on the inspector at this time. The inspected
247 * #WebKitWebView may no longer exist when this signal is emitted.
251 webkit_web_inspector_signals[FINISHED] = g_signal_new("finished",
252 G_TYPE_FROM_CLASS(klass),
253 (GSignalFlags)G_SIGNAL_RUN_LAST,
257 g_cclosure_marshal_VOID__VOID,
265 * WebKitWebInspector:web-view:
267 * The Web View that renders the Web Inspector itself.
271 g_object_class_install_property(gobject_class, PROP_WEB_VIEW,
272 g_param_spec_object("web-view",
274 _("The Web View that renders the Web Inspector itself"),
275 WEBKIT_TYPE_WEB_VIEW,
276 WEBKIT_PARAM_READABLE));
279 * WebKitWebInspector:inspected-uri:
281 * The URI that is currently being inspected.
285 g_object_class_install_property(gobject_class, PROP_INSPECTED_URI,
286 g_param_spec_string("inspected-uri",
288 _("The URI that is currently being inspected"),
290 WEBKIT_PARAM_READABLE));
293 * WebKitWebInspector:javascript-profiling-enabled
295 * This is enabling JavaScript profiling in the Inspector. This means
296 * that Console.profiles will return the profiles.
300 g_object_class_install_property(gobject_class,
301 PROP_JAVASCRIPT_PROFILING_ENABLED,
302 g_param_spec_boolean(
303 "javascript-profiling-enabled",
304 _("Enable JavaScript profiling"),
305 _("Profile the executed JavaScript."),
307 WEBKIT_PARAM_READWRITE));
310 * WebKitWebInspector:timeline-profiling-enabled
312 * This is enabling Timeline profiling in the Inspector.
316 g_object_class_install_property(gobject_class,
317 PROP_TIMELINE_PROFILING_ENABLED,
318 g_param_spec_boolean(
319 "timeline-profiling-enabled",
320 _("Enable Timeline profiling"),
321 _("Profile the WebCore instrumentation."),
323 WEBKIT_PARAM_READWRITE));
325 g_type_class_add_private(klass, sizeof(WebKitWebInspectorPrivate));
328 static void webkit_web_inspector_init(WebKitWebInspector* web_inspector)
330 web_inspector->priv = G_TYPE_INSTANCE_GET_PRIVATE(web_inspector, WEBKIT_TYPE_WEB_INSPECTOR, WebKitWebInspectorPrivate);
333 static void webkit_web_inspector_finalize(GObject* object)
335 WebKitWebInspector* web_inspector = WEBKIT_WEB_INSPECTOR(object);
336 WebKitWebInspectorPrivate* priv = web_inspector->priv;
338 if (priv->inspector_view)
339 g_object_unref(priv->inspector_view);
341 if (priv->inspected_uri)
342 g_free(priv->inspected_uri);
344 G_OBJECT_CLASS(webkit_web_inspector_parent_class)->finalize(object);
347 static void webkit_web_inspector_set_property(GObject* object, guint prop_id, const GValue* value, GParamSpec* pspec)
349 WebKitWebInspector* web_inspector = WEBKIT_WEB_INSPECTOR(object);
350 WebKitWebInspectorPrivate* priv = web_inspector->priv;
353 case PROP_JAVASCRIPT_PROFILING_ENABLED: {
354 #if ENABLE(JAVASCRIPT_DEBUGGER)
355 bool enabled = g_value_get_boolean(value);
356 WebCore::InspectorController* controller = priv->page->inspectorController();
358 controller->enableProfiler();
360 controller->disableProfiler();
362 g_message("PROP_JAVASCRIPT_PROFILING_ENABLED is not work because of the javascript debugger is disabled\n");
366 case PROP_TIMELINE_PROFILING_ENABLED: {
367 bool enabled = g_value_get_boolean(value);
368 WebCore::InspectorController* controller = priv->page->inspectorController();
370 controller->startTimelineProfiler();
372 controller->stopTimelineProfiler();
376 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
381 static void webkit_web_inspector_get_property(GObject* object, guint prop_id, GValue* value, GParamSpec* pspec)
383 WebKitWebInspector* web_inspector = WEBKIT_WEB_INSPECTOR(object);
384 WebKitWebInspectorPrivate* priv = web_inspector->priv;
388 g_value_set_object(value, priv->inspector_view);
390 case PROP_INSPECTED_URI:
391 g_value_set_string(value, priv->inspected_uri);
393 case PROP_JAVASCRIPT_PROFILING_ENABLED:
394 #if ENABLE(JAVASCRIPT_DEBUGGER)
395 g_value_set_boolean(value, priv->page->inspectorController()->profilerEnabled());
397 g_message("PROP_JAVASCRIPT_PROFILING_ENABLED is not work because of the javascript debugger is disabled\n");
400 case PROP_TIMELINE_PROFILING_ENABLED:
401 g_value_set_boolean(value, priv->page->inspectorController()->timelineProfilerEnabled());
404 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
410 void webkit_web_inspector_set_web_view(WebKitWebInspector *web_inspector, WebKitWebView *web_view)
412 g_return_if_fail(WEBKIT_IS_WEB_INSPECTOR(web_inspector));
413 g_return_if_fail(WEBKIT_IS_WEB_VIEW(web_view));
415 WebKitWebInspectorPrivate* priv = web_inspector->priv;
417 if (priv->inspector_view)
418 g_object_unref(priv->inspector_view);
420 g_object_ref(web_view);
421 priv->inspector_view = web_view;
425 * webkit_web_inspector_get_web_view:
427 * Obtains the #WebKitWebView that is used to render the
428 * inspector. The #WebKitWebView instance is created by the
429 * application, by handling the #WebKitWebInspector::inspect-web-view signal. This means
430 * that this method may return %NULL if the user hasn't inspected
433 * Returns: (transfer none): the #WebKitWebView instance that is used
434 * to render the inspector or %NULL if it is not yet created.
438 WebKitWebView* webkit_web_inspector_get_web_view(WebKitWebInspector *web_inspector)
440 WebKitWebInspectorPrivate* priv = web_inspector->priv;
442 return priv->inspector_view;
446 void webkit_web_inspector_set_inspected_uri(WebKitWebInspector* web_inspector, const gchar* inspected_uri)
448 g_return_if_fail(WEBKIT_IS_WEB_INSPECTOR(web_inspector));
450 WebKitWebInspectorPrivate* priv = web_inspector->priv;
452 g_free(priv->inspected_uri);
453 priv->inspected_uri = g_strdup(inspected_uri);
457 * webkit_web_inspector_get_inspected_uri:
459 * Obtains the URI that is currently being inspected.
461 * Returns: a pointer to the URI as an internally allocated string; it
462 * should not be freed, modified or stored.
466 const gchar* webkit_web_inspector_get_inspected_uri(WebKitWebInspector *web_inspector)
468 WebKitWebInspectorPrivate* priv = web_inspector->priv;
470 return priv->inspected_uri;
474 webkit_web_inspector_set_inspector_client(WebKitWebInspector* web_inspector, WebCore::Page* page)
476 WebKitWebInspectorPrivate* priv = web_inspector->priv;
482 * webkit_web_inspector_show:
483 * @webInspector: the #WebKitWebInspector that will be shown
485 * Causes the Web Inspector to be shown.
489 void webkit_web_inspector_show(WebKitWebInspector* webInspector)
491 g_return_if_fail(WEBKIT_IS_WEB_INSPECTOR(webInspector));
493 WebKitWebInspectorPrivate* priv = webInspector->priv;
495 Frame* frame = priv->page->focusController()->focusedOrMainFrame();
496 FrameView* view = frame->view();
501 priv->page->inspectorController()->show();
505 * webkit_web_inspector_inspect_node:
506 * @web_inspector: the #WebKitWebInspector that will do the inspection
507 * @node: the #WebKitDOMNode to inspect
509 * Causes the Web Inspector to inspect the given node.
513 void webkit_web_inspector_inspect_node(WebKitWebInspector* webInspector, WebKitDOMNode* node)
515 g_return_if_fail(WEBKIT_IS_WEB_INSPECTOR(webInspector));
516 g_return_if_fail(WEBKIT_DOM_IS_NODE(node));
518 webInspector->priv->page->inspectorController()->inspect(core(node));
522 * webkit_web_inspector_inspect_coordinates:
523 * @web_inspector: the #WebKitWebInspector that will do the inspection
524 * @x: the X coordinate of the node to be inspected
525 * @y: the Y coordinate of the node to be inspected
527 * Causes the Web Inspector to inspect the node that is located at the
528 * given coordinates of the widget. The coordinates should be relative
529 * to the #WebKitWebView widget, not to the scrollable content, and
530 * may be obtained from a #GdkEvent directly.
532 * This means @x, and @y being zero doesn't guarantee you will hit the
533 * left-most top corner of the content, since the contents may have
538 void webkit_web_inspector_inspect_coordinates(WebKitWebInspector* webInspector, gdouble x, gdouble y)
540 g_return_if_fail(WEBKIT_IS_WEB_INSPECTOR(webInspector));
541 g_return_if_fail(x >= 0 && y >= 0);
543 WebKitWebInspectorPrivate* priv = webInspector->priv;
545 Frame* frame = priv->page->focusController()->focusedOrMainFrame();
546 FrameView* view = frame->view();
551 HitTestRequest request(HitTestRequest::ReadOnly | HitTestRequest::Active);
552 IntPoint documentPoint = view->windowToContents(IntPoint(static_cast<int>(x), static_cast<int>(y)));
553 HitTestResult result(documentPoint);
555 frame->contentRenderer()->layer()->hitTest(request, result);
556 priv->page->inspectorController()->inspect(result.innerNonSharedNode());
560 * webkit_web_inspector_close:
561 * @webInspector: the #WebKitWebInspector that will be closed
563 * Causes the Web Inspector to be closed.
567 void webkit_web_inspector_close(WebKitWebInspector* webInspector)
569 g_return_if_fail(WEBKIT_IS_WEB_INSPECTOR(webInspector));
571 WebKitWebInspectorPrivate* priv = webInspector->priv;
572 priv->page->inspectorController()->close();
575 void webkit_web_inspector_execute_script(WebKitWebInspector* webInspector, long callId, const gchar* script)
577 g_return_if_fail(WEBKIT_IS_WEB_INSPECTOR(webInspector));
578 g_return_if_fail(script);
580 WebKitWebInspectorPrivate* priv = webInspector->priv;
581 priv->page->inspectorController()->evaluateForTestInFrontend(callId, script);
584 #ifdef HAVE_GSETTINGS
585 static bool isSchemaAvailable(const char* schemaID)
587 const char* const* availableSchemas = g_settings_list_schemas();
588 char* const* iter = const_cast<char* const*>(availableSchemas);
591 if (g_str_equal(schemaID, *iter))
599 GSettings* inspectorGSettings()
601 static GSettings* settings = 0;
605 // Unfortunately GSettings will abort the process execution if the schema is not
606 // installed, which is the case for when running tests, or even the introspection dump
607 // at build time, so check if we have the schema before trying to initialize it.
608 const gchar* schemaID = "org.webkitgtk-"WEBKITGTK_API_VERSION_STRING".inspector";
609 if (!isSchemaAvailable(schemaID)) {
611 // This warning is very common on the build bots, which hides valid warnings.
612 // Skip printing it if we are running inside DumpRenderTree.
613 if (!DumpRenderTreeSupportGtk::dumpRenderTreeModeEnabled())
614 g_warning("GSettings schema not found - settings will not be used or saved.");
618 settings = g_settings_new(schemaID);