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
|
/*
* Copyright (C) 2012 Igalia S.L.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2,1 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 "WebInspectorProxy.h"
#include "WebKitMarshal.h"
#include "WebKitWebInspectorPrivate.h"
#include <glib/gi18n-lib.h>
#include <wtf/gobject/GRefPtr.h>
#include <wtf/text/CString.h>
using namespace WebKit;
/**
* SECTION: WebKitWebInspector
* @Short_description: Access to the WebKit inspector
* @Title: WebKitWebInspector
*
* The WebKit Inspector is a graphical tool to inspect and change the
* content of a #WebKitWebView. It also includes an interactive
* JavaScript debugger. 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 #WebKitSettings of the
* #WebKitWebView has set the #WebKitSettings:enable-developer-extras
* to true, otherwise no inspector is available.
*
* <informalexample><programlisting>
* /<!-- -->* Enable the developer extras *<!-- -->/
* WebKitSettings *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_load_uri (WEBKIT_WEB_VIEW(my_webview), "http://www.gnome.org");
*
* /<!-- -->* Show the inspector *<!-- -->/
* WebKitWebInspector *inspector = webkit_web_view_get_inspector (WEBKIT_WEB_VIEW(my_webview));
* webkit_web_inspector_show (WEBKIT_WEB_INSPECTOR(inspector));
* </programlisting></informalexample>
*
*/
enum {
OPEN_WINDOW,
BRING_TO_FRONT,
CLOSED,
ATTACH,
DETACH,
LAST_SIGNAL
};
enum {
PROP_0,
PROP_INSPECTED_URI,
PROP_ATTACHED_HEIGHT
};
struct _WebKitWebInspectorPrivate {
~_WebKitWebInspectorPrivate()
{
WKInspectorSetInspectorClientGtk(toAPI(webInspector.get()), 0);
}
RefPtr<WebInspectorProxy> webInspector;
CString inspectedURI;
unsigned attachedHeight;
};
WEBKIT_DEFINE_TYPE(WebKitWebInspector, webkit_web_inspector, G_TYPE_OBJECT)
static guint signals[LAST_SIGNAL] = { 0, };
static void webkitWebInspectorGetProperty(GObject* object, guint propId, GValue* value, GParamSpec* paramSpec)
{
WebKitWebInspector* inspector = WEBKIT_WEB_INSPECTOR(object);
switch (propId) {
case PROP_INSPECTED_URI:
g_value_set_string(value, webkit_web_inspector_get_inspected_uri(inspector));
break;
case PROP_ATTACHED_HEIGHT:
g_value_set_uint(value, webkit_web_inspector_get_attached_height(inspector));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, propId, paramSpec);
}
}
static void webkit_web_inspector_class_init(WebKitWebInspectorClass* findClass)
{
GObjectClass* gObjectClass = G_OBJECT_CLASS(findClass);
gObjectClass->get_property = webkitWebInspectorGetProperty;
/**
* WebKitWebInspector:inspected-uri:
*
* The URI that is currently being inspected.
*/
g_object_class_install_property(gObjectClass,
PROP_INSPECTED_URI,
g_param_spec_string("inspected-uri",
_("Inspected URI"),
_("The URI that is currently being inspected"),
0,
WEBKIT_PARAM_READABLE));
/**
* WebKitWebInspector:attached-height:
*
* The height that the inspector view should have when it is attached.
*/
g_object_class_install_property(gObjectClass,
PROP_ATTACHED_HEIGHT,
g_param_spec_uint("attached-height",
_("Attached Height"),
_("The height that the inspector view should have when it is attached"),
0, G_MAXUINT, 0,
WEBKIT_PARAM_READABLE));
/**
* WebKitWebInspector::open-window:
* @inspector: the #WebKitWebInspector on which the signal is emitted
*
* Emitted when the inspector is requested to open in a separate window.
* If this signal is not handled, a #GtkWindow with the inspector will be
* created and shown, so you only need to handle this signal if you want
* to use your own window.
* This signal is emitted after #WebKitWebInspector::detach to show
* the inspector in a separate window after being detached.
*
* To prevent the inspector from being shown you can connect to this
* signal and simply return %TRUE
*
* Returns: %TRUE to stop other handlers from being invoked for the event.
* %FALSE to propagate the event further.
*/
signals[OPEN_WINDOW] =
g_signal_new("open-window",
G_TYPE_FROM_CLASS(gObjectClass),
G_SIGNAL_RUN_LAST,
0,
g_signal_accumulator_true_handled, 0,
webkit_marshal_BOOLEAN__VOID,
G_TYPE_BOOLEAN, 0);
/**
* WebKitWebInspector::bring-to-front:
* @inspector: the #WebKitWebInspector on which the signal is emitted
*
* Emitted when the inspector should be shown.
*
* If the inspector is not attached the inspector window should be shown
* on top of any other windows.
* If the inspector is attached the inspector view should be made visible.
* For example, if the inspector view is attached using a tab in a browser
* window, the browser window should be raised and the tab containing the
* inspector view should be the active one.
* In both cases, if this signal is not handled, the default implementation
* calls gtk_window_present() on the current toplevel #GtkWindow of the
* inspector view.
*
* Returns: %TRUE to stop other handlers from being invoked for the event.
* %FALSE to propagate the event further.
*/
signals[BRING_TO_FRONT] =
g_signal_new("bring-to-front",
G_TYPE_FROM_CLASS(gObjectClass),
G_SIGNAL_RUN_LAST,
0,
g_signal_accumulator_true_handled, 0,
webkit_marshal_BOOLEAN__VOID,
G_TYPE_BOOLEAN, 0);
/**
* WebKitWebInspector::closed:
* @inspector: the #WebKitWebInspector on which the signal is emitted
*
* Emitted when the inspector page is closed. If you are using your own
* inspector window, you should connect to this signal and destroy your
* window.
*/
signals[CLOSED] =
g_signal_new("closed",
G_TYPE_FROM_CLASS(gObjectClass),
G_SIGNAL_RUN_LAST,
0, 0, 0,
g_cclosure_marshal_VOID__VOID,
G_TYPE_NONE, 0);
/**
* WebKitWebInspector::attach:
* @inspector: the #WebKitWebInspector on which the signal is emitted
*
* Emitted when the inspector is requested to be attached to the window
* where the inspected web view is.
* If this signal is not handled the inspector view will be automatically
* attached to the inspected view, so you only need to handle this signal
* if you want to attach the inspector view yourself (for example, to add
* the inspector view to a browser tab).
*
* To prevent the inspector vew from being attached you can connect to this
* signal and simply return %TRUE.
*
* Returns: %TRUE to stop other handlers from being invoked for the event.
* %FALSE to propagate the event further.
*/
signals[ATTACH] =
g_signal_new("attach",
G_TYPE_FROM_CLASS(gObjectClass),
G_SIGNAL_RUN_LAST,
0,
g_signal_accumulator_true_handled, 0,
webkit_marshal_BOOLEAN__VOID,
G_TYPE_BOOLEAN, 0);
/**
* WebKitWebInspector::detach:
* @inspector: the #WebKitWebInspector on which the signal is emitted
*
* Emitted when the inspector is requested to be detached from the window
* it is currently attached to. The inspector is detached when the inspector page
* is about to be closed, and this signal is emitted right before
* #WebKitWebInspector::closed, or when the user clicks on the detach button
* in the inspector view to show the inspector in a separate window. In this case
* the signal #WebKitWebInspector::open-window is emitted after this one.
*
* To prevent the inspector vew from being detached you can connect to this
* signal and simply return %TRUE.
*
* Returns: %TRUE to stop other handlers from being invoked for the event.
* %FALSE to propagate the event further.
*/
signals[DETACH] =
g_signal_new("detach",
G_TYPE_FROM_CLASS(gObjectClass),
G_SIGNAL_RUN_LAST,
0,
g_signal_accumulator_true_handled, 0,
webkit_marshal_BOOLEAN__VOID,
G_TYPE_BOOLEAN, 0);
}
static bool openWindow(WKInspectorRef, const void* clientInfo)
{
gboolean returnValue;
g_signal_emit(WEBKIT_WEB_INSPECTOR(clientInfo), signals[OPEN_WINDOW], 0, &returnValue);
return returnValue;
}
static void didClose(WKInspectorRef, const void* clientInfo)
{
g_signal_emit(WEBKIT_WEB_INSPECTOR(clientInfo), signals[CLOSED], 0);
}
static bool bringToFront(WKInspectorRef, const void* clientInfo)
{
gboolean returnValue;
g_signal_emit(WEBKIT_WEB_INSPECTOR(clientInfo), signals[BRING_TO_FRONT], 0, &returnValue);
return returnValue;
}
static void inspectedURLChanged(WKInspectorRef, WKStringRef url, const void* clientInfo)
{
WebKitWebInspector* inspector = WEBKIT_WEB_INSPECTOR(clientInfo);
CString uri = toImpl(url)->string().utf8();
if (uri == inspector->priv->inspectedURI)
return;
inspector->priv->inspectedURI = uri;
g_object_notify(G_OBJECT(inspector), "inspected-uri");
}
static bool attach(WKInspectorRef, const void* clientInfo)
{
gboolean returnValue;
g_signal_emit(WEBKIT_WEB_INSPECTOR(clientInfo), signals[ATTACH], 0, &returnValue);
return returnValue;
}
static bool detach(WKInspectorRef inspector, const void* clientInfo)
{
gboolean returnValue;
g_signal_emit(WEBKIT_WEB_INSPECTOR(clientInfo), signals[DETACH], 0, &returnValue);
return returnValue;
}
static void didChangeAttachedHeight(WKInspectorRef, unsigned height, const void* clientInfo)
{
WebKitWebInspector* inspector = WEBKIT_WEB_INSPECTOR(clientInfo);
if (inspector->priv->attachedHeight == height)
return;
inspector->priv->attachedHeight = height;
g_object_notify(G_OBJECT(inspector), "attached-height");
}
WebKitWebInspector* webkitWebInspectorCreate(WebInspectorProxy* webInspector)
{
WebKitWebInspector* inspector = WEBKIT_WEB_INSPECTOR(g_object_new(WEBKIT_TYPE_WEB_INSPECTOR, NULL));
inspector->priv->webInspector = webInspector;
WKInspectorClientGtk wkInspectorClientGtk = {
kWKInspectorClientGtkCurrentVersion,
inspector, // clientInfo
openWindow,
didClose,
bringToFront,
inspectedURLChanged,
attach,
detach,
didChangeAttachedHeight
};
WKInspectorSetInspectorClientGtk(toAPI(webInspector), &wkInspectorClientGtk);
return inspector;
}
/**
* webkit_web_inspector_get_web_view:
* @inspector: a #WebKitWebInspector
*
* Get the #WebKitWebViewBase used to display the inspector.
* This might be %NULL if the inspector hasn't been loaded yet,
* or it has been closed.
*
* Returns: (transfer none): the #WebKitWebViewBase used to display the inspector or %NULL
*/
WebKitWebViewBase* webkit_web_inspector_get_web_view(WebKitWebInspector* inspector)
{
g_return_val_if_fail(WEBKIT_IS_WEB_INSPECTOR(inspector), 0);
return WEBKIT_WEB_VIEW_BASE(inspector->priv->webInspector->inspectorView());
}
/**
* webkit_web_inspector_get_inspected_uri:
* @inspector: a #WebKitWebInspector
*
* Get the URI that is currently being inspected. This can be %NULL if
* nothing has been loaded yet in the inspected view, if the inspector
* has been closed or when inspected view was loaded from a HTML string
* instead of a URI.
*
* Returns: the URI that is currently being inspected or %NULL
*/
const char* webkit_web_inspector_get_inspected_uri(WebKitWebInspector* inspector)
{
g_return_val_if_fail(WEBKIT_IS_WEB_INSPECTOR(inspector), 0);
return inspector->priv->inspectedURI.data();
}
/**
* webkit_web_inspector_is_attached:
* @inspector: a #WebKitWebInspector
*
* Whether the @inspector view is currently attached to the same window that contains
* the inspected view.
*
* Returns: %TRUE if @inspector is currently attached or %FALSE otherwise
*/
gboolean webkit_web_inspector_is_attached(WebKitWebInspector* inspector)
{
g_return_val_if_fail(WEBKIT_IS_WEB_INSPECTOR(inspector), FALSE);
return inspector->priv->webInspector->isAttached();
}
/**
* webkit_web_inspector_attach:
* @inspector: a #WebKitWebInspector
*
* Request @inspector to be attached. The signal #WebKitWebInspector::attach
* will be emitted. If the inspector is already attached it does nothing.
*/
void webkit_web_inspector_attach(WebKitWebInspector* inspector)
{
g_return_if_fail(WEBKIT_IS_WEB_INSPECTOR(inspector));
if (inspector->priv->webInspector->isAttached())
return;
inspector->priv->webInspector->attach();
}
/**
* webkit_web_inspector_detach:
* @inspector: a #WebKitWebInspector
*
* Request @inspector to be detached. The signal #WebKitWebInspector::detach
* will be emitted. If the inspector is already detached it does nothing.
*/
void webkit_web_inspector_detach(WebKitWebInspector* inspector)
{
g_return_if_fail(WEBKIT_IS_WEB_INSPECTOR(inspector));
if (!inspector->priv->webInspector->isAttached())
return;
inspector->priv->webInspector->detach();
}
/**
* webkit_web_inspector_show:
* @inspector: a #WebKitWebInspector
*
* Request @inspector to be shown.
*/
void webkit_web_inspector_show(WebKitWebInspector* inspector)
{
g_return_if_fail(WEBKIT_IS_WEB_INSPECTOR(inspector));
inspector->priv->webInspector->show();
}
/**
* webkit_web_inspector_close:
* @inspector: a #WebKitWebInspector
*
* Request @inspector to be closed.
*/
void webkit_web_inspector_close(WebKitWebInspector* inspector)
{
g_return_if_fail(WEBKIT_IS_WEB_INSPECTOR(inspector));
inspector->priv->webInspector->close();
}
/**
* webkit_web_inspector_get_attached_height:
* @inspector: a #WebKitWebInspector
*
* Get the height that the inspector view should have when
* it's attached. If the inspector view is not attached this
* returns 0.
*
* Returns: the height of the inspector view when attached
*/
guint webkit_web_inspector_get_attached_height(WebKitWebInspector* inspector)
{
g_return_val_if_fail(WEBKIT_IS_WEB_INSPECTOR(inspector), 0);
if (!inspector->priv->webInspector->isAttached())
return 0;
return inspector->priv->attachedHeight;
}
|