Reviewed by Gustavo Noronha Silva.
[WebKit-https.git] / WebKit / gtk / webkit / webkitprivate.cpp
1 /*
2  * Copyright (C) 2007 Holger Hans Peter Freyther
3  * Copyright (C) 2008 Collabora Ltd.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public License
16  * along with this library; see the file COPYING.LIB.  If not, write to
17  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  */
20
21 #include "config.h"
22 #include "webkitprivate.h"
23
24 #include "ApplicationCacheStorage.h"
25 #include "Chrome.h"
26 #include "ChromeClientGtk.h"
27 #include "Frame.h"
28 #include "FrameLoader.h"
29 #include "FrameLoaderClientGtk.h"
30 #include "GtkVersioning.h"
31 #include "HTMLMediaElement.h"
32 #include "HTMLNames.h"
33 #include "HitTestResult.h"
34 #include "IconDatabase.h"
35 #include "Logging.h"
36 #include "PageCache.h"
37 #include "PageGroup.h"
38 #include "Pasteboard.h"
39 #include "PasteboardHelperGtk.h"
40 #include "ResourceHandle.h"
41 #include "ResourceHandleClient.h"
42 #include "ResourceHandleInternal.h"
43 #include "ResourceResponse.h"
44 #include "SecurityOrigin.h"
45 #include "TextEncodingRegistry.h"
46 #include "WebKitDOMBinding.h"
47 #include "webkitnetworkresponse.h"
48 #include "webkitsoupauthdialog.h"
49 #include "webkitversion.h"
50 #include <libintl.h>
51 #include <runtime/InitializeThreading.h>
52 #include <stdlib.h>
53 #include <wtf/Threading.h>
54
55 #if ENABLE(VIDEO)
56 #include "FullscreenVideoController.h"
57 #endif
58
59 #if ENABLE(DATABASE)
60 #include "DatabaseTracker.h"
61 #endif
62
63 using namespace WebCore;
64
65 namespace WebKit {
66
67 WebKitWebView* getViewFromFrame(WebKitWebFrame* frame)
68 {
69     WebKitWebFramePrivate* priv = frame->priv;
70     return priv->webView;
71 }
72
73 WebCore::Frame* core(WebKitWebFrame* frame)
74 {
75     if (!frame)
76         return 0;
77
78     WebKitWebFramePrivate* priv = frame->priv;
79     return priv ? priv->coreFrame : 0;
80 }
81
82 WebKitWebFrame* kit(WebCore::Frame* coreFrame)
83 {
84     if (!coreFrame)
85         return 0;
86
87     ASSERT(coreFrame->loader());
88     WebKit::FrameLoaderClient* client = static_cast<WebKit::FrameLoaderClient*>(coreFrame->loader()->client());
89     return client ? client->webFrame() : 0;
90 }
91
92 WebCore::Page* core(WebKitWebView* webView)
93 {
94     if (!webView)
95         return 0;
96
97     WebKitWebViewPrivate* priv = webView->priv;
98     return priv ? priv->corePage : 0;
99 }
100
101 WebKitWebView* kit(WebCore::Page* corePage)
102 {
103     if (!corePage)
104         return 0;
105
106     ASSERT(corePage->chrome());
107     WebKit::ChromeClient* client = static_cast<WebKit::ChromeClient*>(corePage->chrome()->client());
108     return client ? client->webView() : 0;
109 }
110
111 WebKitWebNavigationReason kit(WebCore::NavigationType type)
112 {
113     return (WebKitWebNavigationReason)type;
114 }
115
116 WebCore::NavigationType core(WebKitWebNavigationReason type)
117 {
118     return static_cast<WebCore::NavigationType>(type);
119 }
120
121 WebCore::ResourceRequest core(WebKitNetworkRequest* request)
122 {
123     SoupMessage* soupMessage = webkit_network_request_get_message(request);
124     if (soupMessage)
125         return ResourceRequest(soupMessage);
126
127     KURL url = KURL(KURL(), String::fromUTF8(webkit_network_request_get_uri(request)));
128     return ResourceRequest(url);
129 }
130
131 WebCore::ResourceResponse core(WebKitNetworkResponse* response)
132 {
133     SoupMessage* soupMessage = webkit_network_response_get_message(response);
134     if (soupMessage)
135         return ResourceResponse(soupMessage);
136
137     return ResourceResponse();
138 }
139
140 WebCore::EditingBehaviorType core(WebKitEditingBehavior type)
141 {
142     return (WebCore::EditingBehaviorType)type;
143 }
144
145 WebKitHitTestResult* kit(const WebCore::HitTestResult& result)
146 {
147     guint context = WEBKIT_HIT_TEST_RESULT_CONTEXT_DOCUMENT;
148     GOwnPtr<char> linkURI(0);
149     GOwnPtr<char> imageURI(0);
150     GOwnPtr<char> mediaURI(0);
151     WebKitDOMNode* node = 0;
152
153     if (!result.absoluteLinkURL().isEmpty()) {
154         context |= WEBKIT_HIT_TEST_RESULT_CONTEXT_LINK;
155         linkURI.set(g_strdup(result.absoluteLinkURL().string().utf8().data()));
156     }
157
158     if (!result.absoluteImageURL().isEmpty()) {
159         context |= WEBKIT_HIT_TEST_RESULT_CONTEXT_IMAGE;
160         imageURI.set(g_strdup(result.absoluteImageURL().string().utf8().data()));
161     }
162
163     if (!result.absoluteMediaURL().isEmpty()) {
164         context |= WEBKIT_HIT_TEST_RESULT_CONTEXT_MEDIA;
165         mediaURI.set(g_strdup(result.absoluteMediaURL().string().utf8().data()));
166     }
167
168     if (result.isSelected())
169         context |= WEBKIT_HIT_TEST_RESULT_CONTEXT_SELECTION;
170
171     if (result.isContentEditable())
172         context |= WEBKIT_HIT_TEST_RESULT_CONTEXT_EDITABLE;
173
174     if (result.innerNonSharedNode())
175         node = static_cast<WebKitDOMNode*>(kit(result.innerNonSharedNode()));
176
177     return WEBKIT_HIT_TEST_RESULT(g_object_new(WEBKIT_TYPE_HIT_TEST_RESULT,
178                                                "link-uri", linkURI.get(),
179                                                "image-uri", imageURI.get(),
180                                                "media-uri", mediaURI.get(),
181                                                "context", context,
182                                                "inner-node", node,
183                                                NULL));
184 }
185
186 PasteboardHelperGtk* pasteboardHelperInstance()
187 {
188     static PasteboardHelperGtk* helper = new PasteboardHelperGtk();
189     return helper;
190 }
191
192 } /** end namespace WebKit */
193
194 static GtkWidget* currentToplevelCallback(WebKitSoupAuthDialog* feature, SoupMessage* message, gpointer userData)
195 {
196     gpointer messageData = g_object_get_data(G_OBJECT(message), "resourceHandle");
197     if (!messageData)
198         return NULL;
199
200     ResourceHandle* handle = static_cast<ResourceHandle*>(messageData);
201     if (!handle)
202         return NULL;
203
204     ResourceHandleInternal* d = handle->getInternal();
205     if (!d)
206         return NULL;
207
208     WebCore::Frame* frame = d->m_frame;
209     if (!frame)
210         return NULL;
211
212     GtkWidget* toplevel =  gtk_widget_get_toplevel(GTK_WIDGET(frame->page()->chrome()->platformPageClient()));
213     if (gtk_widget_is_toplevel(toplevel))
214         return toplevel;
215     else
216         return NULL;
217 }
218
219 static void closeIconDatabaseOnExit()
220 {
221     iconDatabase()->close();
222 }
223
224 #ifdef HAVE_GSETTINGS
225 static bool isSchemaAvailable(const char* schemaID)
226 {
227     const char* const* availableSchemas = g_settings_list_schemas();
228     char* const* iter = const_cast<char* const*>(availableSchemas);
229
230     while (*iter) {
231         if (g_str_equal(schemaID, *iter))
232             return true;
233         iter++;
234     }
235
236     return false;
237 }
238
239 GSettings* inspectorGSettings()
240 {
241     static GSettings* settings = 0;
242
243     if (settings)
244         return settings;
245
246     const gchar* schemaID = "org.webkitgtk-"WEBKITGTK_API_VERSION_STRING".inspector";
247
248     // Unfortunately GSettings will abort the process execution if the
249     // schema is not installed, which is the case for when running
250     // tests, or even the introspection dump at build time, so check
251     // if we have the schema before trying to initialize it.
252     if (!isSchemaAvailable(schemaID)) {
253         g_warning("GSettings schema not found - settings will not be used or saved.");
254         return 0;
255     }
256
257     settings = g_settings_new(schemaID);
258
259     return settings;
260 }
261 #endif
262
263 void webkit_init()
264 {
265     static bool isInitialized = false;
266     if (isInitialized)
267         return;
268     isInitialized = true;
269
270     bindtextdomain(GETTEXT_PACKAGE, PACKAGE_LOCALE_DIR);
271     bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
272
273     JSC::initializeThreading();
274     WTF::initializeMainThread();
275
276     WebCore::InitializeLoggingChannelsIfNecessary();
277
278     // We make sure the text codecs have been initialized, because
279     // that may only be done by the main thread.
280     atomicCanonicalTextEncodingName("UTF-8");
281
282     // Page cache capacity (in pages). Comment from Mac port:
283     // (Research indicates that value / page drops substantially after 3 pages.)
284     // FIXME: Expose this with an API and/or calculate based on available resources
285     webkit_set_cache_model(WEBKIT_CACHE_MODEL_WEB_BROWSER);
286
287 #ifdef HAVE_GSETTINGS
288     // Initialize settings variables here to make sure this happens in
289     // the main thread.
290     inspectorGSettings();
291 #endif
292
293 #if ENABLE(DATABASE)
294     gchar* databaseDirectory = g_build_filename(g_get_user_data_dir(), "webkit", "databases", NULL);
295     webkit_set_web_database_directory_path(databaseDirectory);
296
297     // FIXME: It should be possible for client applications to override the default appcache location
298     WebCore::cacheStorage().setCacheDirectory(databaseDirectory);
299     g_free(databaseDirectory);
300 #endif
301
302     PageGroup::setShouldTrackVisitedLinks(true);
303
304     Pasteboard::generalPasteboard()->setHelper(WebKit::pasteboardHelperInstance());
305
306     iconDatabase()->setEnabled(true);
307
308     GOwnPtr<gchar> iconDatabasePath(g_build_filename(g_get_user_data_dir(), "webkit", "icondatabase", NULL));
309     iconDatabase()->open(iconDatabasePath.get());
310
311     atexit(closeIconDatabaseOnExit);
312
313     SoupSession* session = webkit_get_default_session();
314
315     SoupSessionFeature* authDialog = static_cast<SoupSessionFeature*>(g_object_new(WEBKIT_TYPE_SOUP_AUTH_DIALOG, NULL));
316     g_signal_connect(authDialog, "current-toplevel", G_CALLBACK(currentToplevelCallback), NULL);
317     soup_session_add_feature(session, authDialog);
318     g_object_unref(authDialog);
319
320     SoupSessionFeature* sniffer = static_cast<SoupSessionFeature*>(g_object_new(SOUP_TYPE_CONTENT_SNIFFER, NULL));
321     soup_session_add_feature(session, sniffer);
322     g_object_unref(sniffer);
323
324     soup_session_add_feature_by_type(session, SOUP_TYPE_CONTENT_DECODER);
325 }
326
327 void webkit_white_list_access_from_origin(const gchar* sourceOrigin, const gchar* destinationProtocol, const gchar* destinationHost, bool allowDestinationSubdomains)
328 {
329     SecurityOrigin::addOriginAccessWhitelistEntry(*SecurityOrigin::createFromString(sourceOrigin), destinationProtocol, destinationHost, allowDestinationSubdomains);
330 }
331
332 void webkit_reset_origin_access_white_lists()
333 {
334     SecurityOrigin::resetOriginAccessWhitelists();
335 }
336
337
338 void webkitWebViewEnterFullscreen(WebKitWebView* webView, Node* node)
339 {
340     if (!node->hasTagName(HTMLNames::videoTag))
341         return;
342
343 #if ENABLE(VIDEO)
344     HTMLMediaElement* videoElement = static_cast<HTMLMediaElement*>(node);
345     WebKitWebViewPrivate* priv = webView->priv;
346
347     // First exit Fullscreen for the old mediaElement.
348     if (priv->fullscreenVideoController)
349         priv->fullscreenVideoController->exitFullscreen();
350
351     priv->fullscreenVideoController = new FullscreenVideoController;
352     priv->fullscreenVideoController->setMediaElement(videoElement);
353     priv->fullscreenVideoController->enterFullscreen();
354 #endif
355 }
356
357 void webkitWebViewExitFullscreen(WebKitWebView* webView)
358 {
359 #if ENABLE(VIDEO)
360     WebKitWebViewPrivate* priv = webView->priv;
361     if (priv->fullscreenVideoController)
362         priv->fullscreenVideoController->exitFullscreen();
363 #endif
364 }
365