2010-08-17 Gustavo Noronha Silva <gustavo.noronha@collabora.co.uk>
[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 "HitTestResult.h"
32 #include "IconDatabase.h"
33 #include "Logging.h"
34 #include "PageCache.h"
35 #include "PageGroup.h"
36 #include "Pasteboard.h"
37 #include "PasteboardHelperGtk.h"
38 #include "ResourceHandle.h"
39 #include "ResourceHandleClient.h"
40 #include "ResourceHandleInternal.h"
41 #include "ResourceResponse.h"
42 #include "SecurityOrigin.h"
43 #include "TextEncodingRegistry.h"
44 #include "WebKitDOMBinding.h"
45 #include "webkitnetworkresponse.h"
46 #include "webkitsoupauthdialog.h"
47 #include "webkitversion.h"
48 #include <libintl.h>
49 #include <runtime/InitializeThreading.h>
50 #include <stdlib.h>
51 #include <wtf/Threading.h>
52
53 #if ENABLE(DATABASE)
54 #include "DatabaseTracker.h"
55 #endif
56
57 using namespace WebCore;
58
59 namespace WebKit {
60
61 WebKitWebView* getViewFromFrame(WebKitWebFrame* frame)
62 {
63     WebKitWebFramePrivate* priv = frame->priv;
64     return priv->webView;
65 }
66
67 WebCore::Frame* core(WebKitWebFrame* frame)
68 {
69     if (!frame)
70         return 0;
71
72     WebKitWebFramePrivate* priv = frame->priv;
73     return priv ? priv->coreFrame : 0;
74 }
75
76 WebKitWebFrame* kit(WebCore::Frame* coreFrame)
77 {
78     if (!coreFrame)
79         return 0;
80
81     ASSERT(coreFrame->loader());
82     WebKit::FrameLoaderClient* client = static_cast<WebKit::FrameLoaderClient*>(coreFrame->loader()->client());
83     return client ? client->webFrame() : 0;
84 }
85
86 WebCore::Page* core(WebKitWebView* webView)
87 {
88     if (!webView)
89         return 0;
90
91     WebKitWebViewPrivate* priv = webView->priv;
92     return priv ? priv->corePage : 0;
93 }
94
95 WebKitWebView* kit(WebCore::Page* corePage)
96 {
97     if (!corePage)
98         return 0;
99
100     ASSERT(corePage->chrome());
101     WebKit::ChromeClient* client = static_cast<WebKit::ChromeClient*>(corePage->chrome()->client());
102     return client ? client->webView() : 0;
103 }
104
105 WebKitWebNavigationReason kit(WebCore::NavigationType type)
106 {
107     return (WebKitWebNavigationReason)type;
108 }
109
110 WebCore::NavigationType core(WebKitWebNavigationReason type)
111 {
112     return static_cast<WebCore::NavigationType>(type);
113 }
114
115 WebCore::ResourceRequest core(WebKitNetworkRequest* request)
116 {
117     SoupMessage* soupMessage = webkit_network_request_get_message(request);
118     if (soupMessage)
119         return ResourceRequest(soupMessage);
120
121     KURL url = KURL(KURL(), String::fromUTF8(webkit_network_request_get_uri(request)));
122     return ResourceRequest(url);
123 }
124
125 WebCore::ResourceResponse core(WebKitNetworkResponse* response)
126 {
127     SoupMessage* soupMessage = webkit_network_response_get_message(response);
128     if (soupMessage)
129         return ResourceResponse(soupMessage);
130
131     return ResourceResponse();
132 }
133
134 WebCore::EditingBehaviorType core(WebKitEditingBehavior type)
135 {
136     return (WebCore::EditingBehaviorType)type;
137 }
138
139 WebKitHitTestResult* kit(const WebCore::HitTestResult& result)
140 {
141     guint context = WEBKIT_HIT_TEST_RESULT_CONTEXT_DOCUMENT;
142     GOwnPtr<char> linkURI(0);
143     GOwnPtr<char> imageURI(0);
144     GOwnPtr<char> mediaURI(0);
145     WebKitDOMNode* node = 0;
146
147     if (!result.absoluteLinkURL().isEmpty()) {
148         context |= WEBKIT_HIT_TEST_RESULT_CONTEXT_LINK;
149         linkURI.set(g_strdup(result.absoluteLinkURL().string().utf8().data()));
150     }
151
152     if (!result.absoluteImageURL().isEmpty()) {
153         context |= WEBKIT_HIT_TEST_RESULT_CONTEXT_IMAGE;
154         imageURI.set(g_strdup(result.absoluteImageURL().string().utf8().data()));
155     }
156
157     if (!result.absoluteMediaURL().isEmpty()) {
158         context |= WEBKIT_HIT_TEST_RESULT_CONTEXT_MEDIA;
159         mediaURI.set(g_strdup(result.absoluteMediaURL().string().utf8().data()));
160     }
161
162     if (result.isSelected())
163         context |= WEBKIT_HIT_TEST_RESULT_CONTEXT_SELECTION;
164
165     if (result.isContentEditable())
166         context |= WEBKIT_HIT_TEST_RESULT_CONTEXT_EDITABLE;
167
168     if (result.innerNonSharedNode())
169         node = static_cast<WebKitDOMNode*>(kit(result.innerNonSharedNode()));
170
171     return WEBKIT_HIT_TEST_RESULT(g_object_new(WEBKIT_TYPE_HIT_TEST_RESULT,
172                                                "link-uri", linkURI.get(),
173                                                "image-uri", imageURI.get(),
174                                                "media-uri", mediaURI.get(),
175                                                "context", context,
176                                                "inner-node", node,
177                                                NULL));
178 }
179
180 PasteboardHelperGtk* pasteboardHelperInstance()
181 {
182     static PasteboardHelperGtk* helper = new PasteboardHelperGtk();
183     return helper;
184 }
185
186 } /** end namespace WebKit */
187
188 static GtkWidget* currentToplevelCallback(WebKitSoupAuthDialog* feature, SoupMessage* message, gpointer userData)
189 {
190     gpointer messageData = g_object_get_data(G_OBJECT(message), "resourceHandle");
191     if (!messageData)
192         return NULL;
193
194     ResourceHandle* handle = static_cast<ResourceHandle*>(messageData);
195     if (!handle)
196         return NULL;
197
198     ResourceHandleInternal* d = handle->getInternal();
199     if (!d)
200         return NULL;
201
202     WebCore::Frame* frame = d->m_frame;
203     if (!frame)
204         return NULL;
205
206     GtkWidget* toplevel =  gtk_widget_get_toplevel(GTK_WIDGET(frame->page()->chrome()->platformPageClient()));
207     if (gtk_widget_is_toplevel(toplevel))
208         return toplevel;
209     else
210         return NULL;
211 }
212
213 static void closeIconDatabaseOnExit()
214 {
215     iconDatabase()->close();
216 }
217
218 #ifdef HAVE_GSETTINGS
219 static bool isSchemaAvailable(char* schemaID)
220 {
221     const char* const* availableSchemas = g_settings_list_schemas();
222     char* const* iter = const_cast<char* const*>(availableSchemas);
223
224     while (*iter) {
225         if (g_str_equal(schemaID, *iter))
226             return true;
227         iter++;
228     }
229
230     return false;
231 }
232
233 GSettings* inspectorGSettings()
234 {
235     static GSettings* settings = 0;
236
237     if (settings)
238         return settings;
239
240     GOwnPtr<gchar> schemaID(g_strdup_printf("org.webkitgtk-%.1f.inspector", WEBKITGTK_API_VERSION));
241
242     // Unfortunately GSettings will abort the process execution if the
243     // schema is not installed, which is the case for when running
244     // tests, or even the introspection dump at build time, so check
245     // if we have the schema before trying to initialize it.
246     if (!isSchemaAvailable(schemaID.get())) {
247         g_warning("GSettings schema not found - settings will not be used or saved.");
248         return 0;
249     }
250
251     settings = g_settings_new(schemaID.get());
252
253     return settings;
254 }
255 #endif
256
257 void webkit_init()
258 {
259     static bool isInitialized = false;
260     if (isInitialized)
261         return;
262     isInitialized = true;
263
264     bindtextdomain(GETTEXT_PACKAGE, PACKAGE_LOCALE_DIR);
265     bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
266
267     JSC::initializeThreading();
268     WTF::initializeMainThread();
269
270     WebCore::InitializeLoggingChannelsIfNecessary();
271
272     // We make sure the text codecs have been initialized, because
273     // that may only be done by the main thread.
274     atomicCanonicalTextEncodingName("UTF-8");
275
276     // Page cache capacity (in pages). Comment from Mac port:
277     // (Research indicates that value / page drops substantially after 3 pages.)
278     // FIXME: Expose this with an API and/or calculate based on available resources
279     webkit_set_cache_model(WEBKIT_CACHE_MODEL_WEB_BROWSER);
280
281 #ifdef HAVE_GSETTINGS
282     // Initialize settings variables here to make sure this happens in
283     // the main thread.
284     inspectorGSettings();
285 #endif
286
287 #if ENABLE(DATABASE)
288     gchar* databaseDirectory = g_build_filename(g_get_user_data_dir(), "webkit", "databases", NULL);
289     webkit_set_web_database_directory_path(databaseDirectory);
290
291     // FIXME: It should be possible for client applications to override the default appcache location
292     WebCore::cacheStorage().setCacheDirectory(databaseDirectory);
293     g_free(databaseDirectory);
294 #endif
295
296     PageGroup::setShouldTrackVisitedLinks(true);
297
298     Pasteboard::generalPasteboard()->setHelper(WebKit::pasteboardHelperInstance());
299
300     iconDatabase()->setEnabled(true);
301
302     GOwnPtr<gchar> iconDatabasePath(g_build_filename(g_get_user_data_dir(), "webkit", "icondatabase", NULL));
303     iconDatabase()->open(iconDatabasePath.get());
304
305     atexit(closeIconDatabaseOnExit);
306
307     SoupSession* session = webkit_get_default_session();
308
309     SoupSessionFeature* authDialog = static_cast<SoupSessionFeature*>(g_object_new(WEBKIT_TYPE_SOUP_AUTH_DIALOG, NULL));
310     g_signal_connect(authDialog, "current-toplevel", G_CALLBACK(currentToplevelCallback), NULL);
311     soup_session_add_feature(session, authDialog);
312     g_object_unref(authDialog);
313
314     SoupSessionFeature* sniffer = static_cast<SoupSessionFeature*>(g_object_new(SOUP_TYPE_CONTENT_SNIFFER, NULL));
315     soup_session_add_feature(session, sniffer);
316     g_object_unref(sniffer);
317
318     soup_session_add_feature_by_type(session, SOUP_TYPE_CONTENT_DECODER);
319 }
320
321 void webkit_white_list_access_from_origin(const gchar* sourceOrigin, const gchar* destinationProtocol, const gchar* destinationHost, bool allowDestinationSubdomains)
322 {
323     SecurityOrigin::addOriginAccessWhitelistEntry(*SecurityOrigin::createFromString(sourceOrigin), destinationProtocol, destinationHost, allowDestinationSubdomains);
324 }
325
326 void webkit_reset_origin_access_white_lists()
327 {
328     SecurityOrigin::resetOriginAccessWhitelists();
329 }