WebKit/gtk/webkit/webkitprivate.cpp
changeset 0 4f2f89ce4247
equal deleted inserted replaced
-1:000000000000 0:4f2f89ce4247
       
     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 <libintl.h>
       
    48 #include <runtime/InitializeThreading.h>
       
    49 #include <stdlib.h>
       
    50 #include <wtf/Threading.h>
       
    51 
       
    52 #if ENABLE(DATABASE)
       
    53 #include "DatabaseTracker.h"
       
    54 #endif
       
    55 
       
    56 using namespace WebCore;
       
    57 
       
    58 namespace WebKit {
       
    59 
       
    60 WebKitWebView* getViewFromFrame(WebKitWebFrame* frame)
       
    61 {
       
    62     WebKitWebFramePrivate* priv = frame->priv;
       
    63     return priv->webView;
       
    64 }
       
    65 
       
    66 WebCore::Frame* core(WebKitWebFrame* frame)
       
    67 {
       
    68     if (!frame)
       
    69         return 0;
       
    70 
       
    71     WebKitWebFramePrivate* priv = frame->priv;
       
    72     return priv ? priv->coreFrame : 0;
       
    73 }
       
    74 
       
    75 WebKitWebFrame* kit(WebCore::Frame* coreFrame)
       
    76 {
       
    77     if (!coreFrame)
       
    78         return 0;
       
    79 
       
    80     ASSERT(coreFrame->loader());
       
    81     WebKit::FrameLoaderClient* client = static_cast<WebKit::FrameLoaderClient*>(coreFrame->loader()->client());
       
    82     return client ? client->webFrame() : 0;
       
    83 }
       
    84 
       
    85 WebCore::Page* core(WebKitWebView* webView)
       
    86 {
       
    87     if (!webView)
       
    88         return 0;
       
    89 
       
    90     WebKitWebViewPrivate* priv = webView->priv;
       
    91     return priv ? priv->corePage : 0;
       
    92 }
       
    93 
       
    94 WebKitWebView* kit(WebCore::Page* corePage)
       
    95 {
       
    96     if (!corePage)
       
    97         return 0;
       
    98 
       
    99     ASSERT(corePage->chrome());
       
   100     WebKit::ChromeClient* client = static_cast<WebKit::ChromeClient*>(corePage->chrome()->client());
       
   101     return client ? client->webView() : 0;
       
   102 }
       
   103 
       
   104 WebKitWebNavigationReason kit(WebCore::NavigationType type)
       
   105 {
       
   106     return (WebKitWebNavigationReason)type;
       
   107 }
       
   108 
       
   109 WebCore::NavigationType core(WebKitWebNavigationReason type)
       
   110 {
       
   111     return static_cast<WebCore::NavigationType>(type);
       
   112 }
       
   113 
       
   114 WebCore::ResourceRequest core(WebKitNetworkRequest* request)
       
   115 {
       
   116     SoupMessage* soupMessage = webkit_network_request_get_message(request);
       
   117     if (soupMessage)
       
   118         return ResourceRequest(soupMessage);
       
   119 
       
   120     KURL url = KURL(KURL(), String::fromUTF8(webkit_network_request_get_uri(request)));
       
   121     return ResourceRequest(url);
       
   122 }
       
   123 
       
   124 WebCore::ResourceResponse core(WebKitNetworkResponse* response)
       
   125 {
       
   126     SoupMessage* soupMessage = webkit_network_response_get_message(response);
       
   127     if (soupMessage)
       
   128         return ResourceResponse(soupMessage);
       
   129 
       
   130     return ResourceResponse();
       
   131 }
       
   132 
       
   133 WebCore::EditingBehaviorType core(WebKitEditingBehavior type)
       
   134 {
       
   135     return (WebCore::EditingBehaviorType)type;
       
   136 }
       
   137 
       
   138 WebKitHitTestResult* kit(const WebCore::HitTestResult& result)
       
   139 {
       
   140     guint context = WEBKIT_HIT_TEST_RESULT_CONTEXT_DOCUMENT;
       
   141     GOwnPtr<char> linkURI(0);
       
   142     GOwnPtr<char> imageURI(0);
       
   143     GOwnPtr<char> mediaURI(0);
       
   144     WebKitDOMNode* node = 0;
       
   145 
       
   146     if (!result.absoluteLinkURL().isEmpty()) {
       
   147         context |= WEBKIT_HIT_TEST_RESULT_CONTEXT_LINK;
       
   148         linkURI.set(g_strdup(result.absoluteLinkURL().string().utf8().data()));
       
   149     }
       
   150 
       
   151     if (!result.absoluteImageURL().isEmpty()) {
       
   152         context |= WEBKIT_HIT_TEST_RESULT_CONTEXT_IMAGE;
       
   153         imageURI.set(g_strdup(result.absoluteImageURL().string().utf8().data()));
       
   154     }
       
   155 
       
   156     if (!result.absoluteMediaURL().isEmpty()) {
       
   157         context |= WEBKIT_HIT_TEST_RESULT_CONTEXT_MEDIA;
       
   158         mediaURI.set(g_strdup(result.absoluteMediaURL().string().utf8().data()));
       
   159     }
       
   160 
       
   161     if (result.isSelected())
       
   162         context |= WEBKIT_HIT_TEST_RESULT_CONTEXT_SELECTION;
       
   163 
       
   164     if (result.isContentEditable())
       
   165         context |= WEBKIT_HIT_TEST_RESULT_CONTEXT_EDITABLE;
       
   166 
       
   167     if (result.innerNonSharedNode())
       
   168         node = static_cast<WebKitDOMNode*>(kit(result.innerNonSharedNode()));
       
   169 
       
   170     return WEBKIT_HIT_TEST_RESULT(g_object_new(WEBKIT_TYPE_HIT_TEST_RESULT,
       
   171                                                "link-uri", linkURI.get(),
       
   172                                                "image-uri", imageURI.get(),
       
   173                                                "media-uri", mediaURI.get(),
       
   174                                                "context", context,
       
   175                                                "inner-node", node,
       
   176                                                NULL));
       
   177 }
       
   178 
       
   179 PasteboardHelperGtk* pasteboardHelperInstance()
       
   180 {
       
   181     static PasteboardHelperGtk* helper = new PasteboardHelperGtk();
       
   182     return helper;
       
   183 }
       
   184 
       
   185 } /** end namespace WebKit */
       
   186 
       
   187 static GtkWidget* currentToplevelCallback(WebKitSoupAuthDialog* feature, SoupMessage* message, gpointer userData)
       
   188 {
       
   189     gpointer messageData = g_object_get_data(G_OBJECT(message), "resourceHandle");
       
   190     if (!messageData)
       
   191         return NULL;
       
   192 
       
   193     ResourceHandle* handle = static_cast<ResourceHandle*>(messageData);
       
   194     if (!handle)
       
   195         return NULL;
       
   196 
       
   197     ResourceHandleInternal* d = handle->getInternal();
       
   198     if (!d)
       
   199         return NULL;
       
   200 
       
   201     WebCore::Frame* frame = d->m_frame;
       
   202     if (!frame)
       
   203         return NULL;
       
   204 
       
   205     GtkWidget* toplevel =  gtk_widget_get_toplevel(GTK_WIDGET(frame->page()->chrome()->platformPageClient()));
       
   206     if (gtk_widget_is_toplevel(toplevel))
       
   207         return toplevel;
       
   208     else
       
   209         return NULL;
       
   210 }
       
   211 
       
   212 static void closeIconDatabaseOnExit()
       
   213 {
       
   214     iconDatabase()->close();
       
   215 }
       
   216 
       
   217 void webkit_init()
       
   218 {
       
   219     static bool isInitialized = false;
       
   220     if (isInitialized)
       
   221         return;
       
   222     isInitialized = true;
       
   223 
       
   224     bindtextdomain(GETTEXT_PACKAGE, PACKAGE_LOCALE_DIR);
       
   225     bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
       
   226 
       
   227     JSC::initializeThreading();
       
   228     WTF::initializeMainThread();
       
   229 
       
   230     WebCore::InitializeLoggingChannelsIfNecessary();
       
   231 
       
   232     // We make sure the text codecs have been initialized, because
       
   233     // that may only be done by the main thread.
       
   234     atomicCanonicalTextEncodingName("UTF-8");
       
   235 
       
   236     // Page cache capacity (in pages). Comment from Mac port:
       
   237     // (Research indicates that value / page drops substantially after 3 pages.)
       
   238     // FIXME: Expose this with an API and/or calculate based on available resources
       
   239     webkit_set_cache_model(WEBKIT_CACHE_MODEL_WEB_BROWSER);
       
   240 
       
   241 #if ENABLE(DATABASE)
       
   242     gchar* databaseDirectory = g_build_filename(g_get_user_data_dir(), "webkit", "databases", NULL);
       
   243     webkit_set_web_database_directory_path(databaseDirectory);
       
   244 
       
   245     // FIXME: It should be possible for client applications to override the default appcache location
       
   246     WebCore::cacheStorage().setCacheDirectory(databaseDirectory);
       
   247     g_free(databaseDirectory);
       
   248 #endif
       
   249 
       
   250     PageGroup::setShouldTrackVisitedLinks(true);
       
   251 
       
   252     Pasteboard::generalPasteboard()->setHelper(WebKit::pasteboardHelperInstance());
       
   253 
       
   254     iconDatabase()->setEnabled(true);
       
   255 
       
   256     GOwnPtr<gchar> iconDatabasePath(g_build_filename(g_get_user_data_dir(), "webkit", "icondatabase", NULL));
       
   257     iconDatabase()->open(iconDatabasePath.get());
       
   258 
       
   259     atexit(closeIconDatabaseOnExit);
       
   260 
       
   261     SoupSession* session = webkit_get_default_session();
       
   262 
       
   263     SoupSessionFeature* authDialog = static_cast<SoupSessionFeature*>(g_object_new(WEBKIT_TYPE_SOUP_AUTH_DIALOG, NULL));
       
   264     g_signal_connect(authDialog, "current-toplevel", G_CALLBACK(currentToplevelCallback), NULL);
       
   265     soup_session_add_feature(session, authDialog);
       
   266     g_object_unref(authDialog);
       
   267 
       
   268     SoupSessionFeature* sniffer = static_cast<SoupSessionFeature*>(g_object_new(SOUP_TYPE_CONTENT_SNIFFER, NULL));
       
   269     soup_session_add_feature(session, sniffer);
       
   270     g_object_unref(sniffer);
       
   271 
       
   272     soup_session_add_feature_by_type(session, SOUP_TYPE_CONTENT_DECODER);
       
   273 }
       
   274 
       
   275 void webkit_white_list_access_from_origin(const gchar* sourceOrigin, const gchar* destinationProtocol, const gchar* destinationHost, bool allowDestinationSubdomains)
       
   276 {
       
   277     SecurityOrigin::addOriginAccessWhitelistEntry(*SecurityOrigin::createFromString(sourceOrigin), destinationProtocol, destinationHost, allowDestinationSubdomains);
       
   278 }
       
   279 
       
   280 void webkit_reset_origin_access_white_lists()
       
   281 {
       
   282     SecurityOrigin::resetOriginAccessWhitelists();
       
   283 }