Home | History | Annotate | Download | only in webkit
      1 /*
      2  * Copyright (C) 2009 Jan Michael C. Alonzo
      3  *
      4  * This library is free software; you can redistribute it and/or
      5  * modify it under the terms of the GNU Library General Public
      6  * License as published by the Free Software Foundation; either
      7  * version 2 of the License, or (at your option) any later version.
      8  *
      9  * This library is distributed in the hope that it will be useful,
     10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     12  * Library General Public License for more details.
     13  *
     14  * You should have received a copy of the GNU Library General Public License
     15  * along with this library; see the file COPYING.LIB.  If not, write to
     16  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
     17  * Boston, MA 02110-1301, USA.
     18  */
     19 
     20 #include "config.h"
     21 #include "webkitwebdatasource.h"
     22 
     23 #include "ArchiveResource.h"
     24 #include "DocumentLoaderGtk.h"
     25 #include "FrameLoader.h"
     26 #include "FrameLoaderClientGtk.h"
     27 #include "KURL.h"
     28 #include "PlatformString.h"
     29 #include "ResourceRequest.h"
     30 #include "SharedBuffer.h"
     31 #include "SubstituteData.h"
     32 #include "runtime/InitializeThreading.h"
     33 #include "webkitglobalsprivate.h"
     34 #include "webkitnetworkrequestprivate.h"
     35 #include "webkitwebdatasourceprivate.h"
     36 #include "webkitwebframeprivate.h"
     37 #include "webkitwebresource.h"
     38 #include "webkitwebviewprivate.h"
     39 #include "wtf/Assertions.h"
     40 #include <glib.h>
     41 
     42 /**
     43  * SECTION:webkitwebdatasource
     44  * @short_description: Encapsulates the content to be displayed in a #WebKitWebFrame.
     45  * @see_also: #WebKitWebFrame
     46  *
     47  * Data source encapsulates the content of a #WebKitWebFrame. A
     48  * #WebKitWebFrame has a main resource and subresources and the data source
     49  * provides access to these resources. When a request gets loaded initially,
     50  * it is set to a provisional state. The application can request for the
     51  * request that initiated the load by asking for the provisional data source
     52  * and invoking the webkit_web_data_source_get_initial_request method of
     53  * #WebKitWebDataSource. This data source may not have enough data and some
     54  * methods may return empty values. To get a "full" data source with the data
     55  * and resources loaded, you need to get the non-provisional data source
     56  * through #WebKitWebFrame's webkit_web_frame_get_data_source method. This
     57  * data source will have the data after everything was loaded. Make sure that
     58  * the data source was finished loading before using any of its methods. You
     59  * can do this via webkit_web_data_source_is_loading.
     60  */
     61 
     62 using namespace WebCore;
     63 using namespace WebKit;
     64 
     65 struct _WebKitWebDataSourcePrivate {
     66     WebKit::DocumentLoader* loader;
     67 
     68     WebKitNetworkRequest* initialRequest;
     69     WebKitNetworkRequest* networkRequest;
     70     WebKitWebResource* mainresource;
     71 
     72     GString* data;
     73 
     74     gchar* textEncoding;
     75     gchar* unreachableURL;
     76 };
     77 
     78 G_DEFINE_TYPE(WebKitWebDataSource, webkit_web_data_source, G_TYPE_OBJECT);
     79 
     80 static void webkit_web_data_source_dispose(GObject* object)
     81 {
     82     WebKitWebDataSource* webDataSource = WEBKIT_WEB_DATA_SOURCE(object);
     83     WebKitWebDataSourcePrivate* priv = webDataSource->priv;
     84 
     85     ASSERT(priv->loader);
     86     ASSERT(!priv->loader->isLoading());
     87     priv->loader->detachDataSource();
     88     priv->loader->deref();
     89 
     90     if (priv->initialRequest) {
     91         g_object_unref(priv->initialRequest);
     92         priv->initialRequest = NULL;
     93     }
     94 
     95     if (priv->networkRequest) {
     96         g_object_unref(priv->networkRequest);
     97         priv->networkRequest = NULL;
     98     }
     99 
    100     if (priv->mainresource) {
    101         g_object_unref(priv->mainresource);
    102         priv->mainresource = NULL;
    103     }
    104 
    105     G_OBJECT_CLASS(webkit_web_data_source_parent_class)->dispose(object);
    106 }
    107 
    108 static void webkit_web_data_source_finalize(GObject* object)
    109 {
    110     WebKitWebDataSource* dataSource = WEBKIT_WEB_DATA_SOURCE(object);
    111     WebKitWebDataSourcePrivate* priv = dataSource->priv;
    112 
    113     g_free(priv->unreachableURL);
    114     g_free(priv->textEncoding);
    115 
    116     if (priv->data) {
    117         g_string_free(priv->data, TRUE);
    118         priv->data = NULL;
    119     }
    120 
    121     G_OBJECT_CLASS(webkit_web_data_source_parent_class)->finalize(object);
    122 }
    123 
    124 static void webkit_web_data_source_class_init(WebKitWebDataSourceClass* klass)
    125 {
    126     GObjectClass* gobject_class = G_OBJECT_CLASS(klass);
    127     gobject_class->dispose = webkit_web_data_source_dispose;
    128     gobject_class->finalize = webkit_web_data_source_finalize;
    129 
    130     webkitInit();
    131 
    132     g_type_class_add_private(gobject_class, sizeof(WebKitWebDataSourcePrivate));
    133 }
    134 
    135 static void webkit_web_data_source_init(WebKitWebDataSource* webDataSource)
    136 {
    137     webDataSource->priv = G_TYPE_INSTANCE_GET_PRIVATE(webDataSource, WEBKIT_TYPE_WEB_DATA_SOURCE, WebKitWebDataSourcePrivate);
    138 }
    139 
    140 /**
    141  * webkit_web_data_source_new:
    142  *
    143  * Creates a new #WebKitWebDataSource instance. The URL of the
    144  * #WebKitWebDataSource will be set to "about:blank".
    145  *
    146  * Return: a new #WebKitWebDataSource.
    147  *
    148  * Since: 1.1.14
    149  */
    150 WebKitWebDataSource* webkit_web_data_source_new()
    151 {
    152     WebKitNetworkRequest* request = webkit_network_request_new("about:blank");
    153     WebKitWebDataSource* datasource = webkit_web_data_source_new_with_request(request);
    154     g_object_unref(request);
    155 
    156     return datasource;
    157 }
    158 
    159 /**
    160  * webkit_web_data_source_new_with_request:
    161  * @request: the #WebKitNetworkRequest to use to create this data source
    162  *
    163  * Creates a new #WebKitWebDataSource from a #WebKitNetworkRequest. Normally,
    164  * #WebKitWebFrame objects create their data sources so you will almost never
    165  * want to invoke this method directly.
    166  *
    167  * Returns: a new #WebKitWebDataSource
    168  *
    169  * Since: 1.1.14
    170  */
    171 WebKitWebDataSource* webkit_web_data_source_new_with_request(WebKitNetworkRequest* request)
    172 {
    173     ASSERT(request);
    174 
    175     const gchar* uri = webkit_network_request_get_uri(request);
    176 
    177     ResourceRequest resourceRequest(ResourceRequest(KURL(KURL(), String::fromUTF8(uri))));
    178     WebKitWebDataSource* datasource = kitNew(WebKit::DocumentLoader::create(resourceRequest, SubstituteData()));
    179 
    180     WebKitWebDataSourcePrivate* priv = datasource->priv;
    181     priv->initialRequest = request;
    182 
    183     return datasource;
    184 }
    185 
    186 /**
    187  * webkit_web_data_source_get_web_frame:
    188  * @data_source: a #WebKitWebDataSource
    189  *
    190  * Returns the #WebKitWebFrame that represents this data source
    191  *
    192  * Return value: (transfer none): the #WebKitWebFrame that represents
    193  * the @data_source. The #WebKitWebFrame is owned by WebKit and should
    194  * not be freed or destroyed.  This will return %NULL if the
    195  * @data_source is not attached to a frame.
    196  *
    197  * Since: 1.1.14
    198  */
    199 WebKitWebFrame* webkit_web_data_source_get_web_frame(WebKitWebDataSource* webDataSource)
    200 {
    201     g_return_val_if_fail(WEBKIT_IS_WEB_DATA_SOURCE(webDataSource), NULL);
    202 
    203     WebKitWebDataSourcePrivate* priv = webDataSource->priv;
    204     FrameLoader* frameLoader = priv->loader->frameLoader();
    205 
    206     if (!frameLoader)
    207         return NULL;
    208 
    209     return static_cast<WebKit::FrameLoaderClient*>(frameLoader->client())->webFrame();
    210 }
    211 
    212 /**
    213  * webkit_web_data_source_get_initial_request:
    214  * @data_source: a #WebKitWebDataSource
    215  *
    216  * Returns a reference to the original request that was used to load the web
    217  * content. The #WebKitNetworkRequest returned by this method is the request
    218  * prior to the "committed" load state. See webkit_web_data_source_get_request
    219  * for getting the "committed" request.
    220  *
    221  * Return value: (transfer none): the original #WebKitNetworkRequest
    222  *
    223  * Since: 1.1.14
    224  */
    225 WebKitNetworkRequest* webkit_web_data_source_get_initial_request(WebKitWebDataSource* webDataSource)
    226 {
    227     g_return_val_if_fail(WEBKIT_IS_WEB_DATA_SOURCE(webDataSource), NULL);
    228 
    229     WebKitWebDataSourcePrivate* priv = webDataSource->priv;
    230     ResourceRequest request = priv->loader->originalRequest();
    231 
    232     if (priv->initialRequest)
    233         g_object_unref(priv->initialRequest);
    234 
    235     priv->initialRequest = kitNew(request);
    236     return priv->initialRequest;
    237 }
    238 
    239 /**
    240  * webkit_web_data_source_get_request:
    241  * @data_source: a #WebKitWebDataSource
    242  *
    243  * Returns a #WebKitNetworkRequest that was used to create this
    244  * #WebKitWebDataSource. The #WebKitNetworkRequest returned by this method is
    245  * the request that was "committed", and hence, different from the request you
    246  * get from the webkit_web_data_source_get_initial_request method.
    247  *
    248  * Return value: (transfer none): the #WebKitNetworkRequest that
    249  * created the @data_source or %NULL if the @data_source is not
    250  * attached to the frame or the frame hasn't been loaded.
    251  *
    252  * Since: 1.1.14
    253  */
    254 WebKitNetworkRequest* webkit_web_data_source_get_request(WebKitWebDataSource* webDataSource)
    255 {
    256     g_return_val_if_fail(WEBKIT_IS_WEB_DATA_SOURCE(webDataSource), NULL);
    257 
    258     WebKitWebDataSourcePrivate* priv = webDataSource->priv;
    259     FrameLoader* frameLoader = priv->loader->frameLoader();
    260     if (!frameLoader || !frameLoader->frameHasLoaded())
    261         return NULL;
    262 
    263     ResourceRequest request = priv->loader->request();
    264 
    265      if (priv->networkRequest)
    266          g_object_unref(priv->networkRequest);
    267 
    268      priv->networkRequest = kitNew(request);
    269      return priv->networkRequest;
    270 }
    271 
    272 /**
    273  * webkit_web_data_source_get_encoding:
    274  * @data_source: a #WebKitWebDataSource
    275  *
    276  * Returns the text encoding name as set in the #WebKitWebView, or if not, the
    277  * text encoding of the response.
    278  *
    279  * Return value: the encoding name of the #WebKitWebView or of the response.
    280  *
    281  * Since: 1.1.14
    282  */
    283 G_CONST_RETURN gchar* webkit_web_data_source_get_encoding(WebKitWebDataSource* webDataSource)
    284 {
    285     g_return_val_if_fail(WEBKIT_IS_WEB_DATA_SOURCE(webDataSource), NULL);
    286 
    287     WebKitWebDataSourcePrivate* priv = webDataSource->priv;
    288     String textEncodingName = priv->loader->overrideEncoding();
    289 
    290     if (!textEncodingName)
    291         textEncodingName = priv->loader->response().textEncodingName();
    292 
    293     CString encoding = textEncodingName.utf8();
    294     g_free(priv->textEncoding);
    295     priv->textEncoding = g_strdup(encoding.data());
    296     return priv->textEncoding;
    297 }
    298 
    299 /**
    300  * webkit_web_data_source_is_loading:
    301  * @data_source: a #WebKitWebDataSource
    302  *
    303  * Determines whether the data source is in the process of loading its content.
    304  *
    305  * Return value: %TRUE if the @data_source is still loading, %FALSE otherwise
    306  *
    307  * Since: 1.1.14
    308  */
    309 gboolean webkit_web_data_source_is_loading(WebKitWebDataSource* webDataSource)
    310 {
    311     g_return_val_if_fail(WEBKIT_IS_WEB_DATA_SOURCE(webDataSource), FALSE);
    312 
    313     WebKitWebDataSourcePrivate* priv = webDataSource->priv;
    314 
    315     return priv->loader->isLoadingInAPISense();
    316 }
    317 
    318 /**
    319  * webkit_web_data_source_get_data:
    320  * @data_source: a #WebKitWebDataSource
    321  *
    322  * Returns the raw data that represents the the frame's content.The data will
    323  * be incomplete until the data has finished loading. Returns %NULL if the web
    324  * frame hasn't loaded any data. Use webkit_web_data_source_is_loading to test
    325  * if data source is in the process of loading.
    326  *
    327  * Return value: (transfer none): a #GString which contains the raw
    328  * data that represents the @data_source or %NULL if the @data_source
    329  * hasn't loaded any data.
    330  *
    331  * Since: 1.1.14
    332  */
    333 GString* webkit_web_data_source_get_data(WebKitWebDataSource* webDataSource)
    334 {
    335     g_return_val_if_fail(WEBKIT_IS_WEB_DATA_SOURCE(webDataSource), NULL);
    336 
    337     WebKitWebDataSourcePrivate* priv = webDataSource->priv;
    338 
    339     RefPtr<SharedBuffer> mainResourceData = priv->loader->mainResourceData();
    340 
    341     if (!mainResourceData)
    342         return NULL;
    343 
    344     if (priv->data) {
    345         g_string_free(priv->data, TRUE);
    346         priv->data = NULL;
    347     }
    348 
    349     priv->data = g_string_new_len(mainResourceData->data(), mainResourceData->size());
    350     return priv->data;
    351 }
    352 
    353 /**
    354  * webkit_web_data_source_get_main_resource:
    355  * @data_source: a #WebKitWebDataSource
    356  *
    357  * Returns the main resource of the @data_source
    358  *
    359  * Return value: (transfer none): a new #WebKitWebResource
    360  * representing the main resource of the @data_source.
    361  *
    362  * Since: 1.1.14
    363  */
    364 WebKitWebResource* webkit_web_data_source_get_main_resource(WebKitWebDataSource* webDataSource)
    365 {
    366     g_return_val_if_fail(WEBKIT_IS_WEB_DATA_SOURCE(webDataSource), NULL);
    367 
    368     WebKitWebDataSourcePrivate* priv = webDataSource->priv;
    369 
    370     if (priv->mainresource)
    371         return priv->mainresource;
    372 
    373     WebKitWebFrame* webFrame = webkit_web_data_source_get_web_frame(webDataSource);
    374     WebKitWebView* webView = getViewFromFrame(webFrame);
    375 
    376     priv->mainresource = WEBKIT_WEB_RESOURCE(g_object_ref(webkit_web_view_get_main_resource(webView)));
    377 
    378     return priv->mainresource;
    379 }
    380 
    381 /**
    382  * webkit_web_data_source_get_unreachable_uri:
    383  * @data_source: a #WebKitWebDataSource
    384  *
    385  * Return the unreachable URI of @data_source. The @data_source will have an
    386  * unreachable URL if it was created using #WebKitWebFrame's
    387  * webkit_web_frame_load_alternate_html_string method.
    388  *
    389  * Return value: the unreachable URL of @data_source or %NULL if there is no unreachable URL.
    390  *
    391  * Since: 1.1.14
    392  */
    393 G_CONST_RETURN gchar* webkit_web_data_source_get_unreachable_uri(WebKitWebDataSource* webDataSource)
    394 {
    395     g_return_val_if_fail(WEBKIT_IS_WEB_DATA_SOURCE(webDataSource), NULL);
    396 
    397     WebKitWebDataSourcePrivate* priv = webDataSource->priv;
    398     const KURL& unreachableURL = priv->loader->unreachableURL();
    399 
    400     if (unreachableURL.isEmpty())
    401         return NULL;
    402 
    403     g_free(priv->unreachableURL);
    404     priv->unreachableURL = g_strdup(unreachableURL.string().utf8().data());
    405     return priv->unreachableURL;
    406 }
    407 
    408 /**
    409  * webkit_web_data_source_get_subresources:
    410  * @data_source: a #WebKitWebDataSource
    411  *
    412  * Gives you a #GList of #WebKitWebResource objects that compose the
    413  * #WebKitWebView to which this #WebKitWebDataSource is attached.
    414  *
    415  * Return value: (element-type WebKitWebResource) (transfer container):
    416  * a #GList of #WebKitWebResource objects; the objects are owned by
    417  * WebKit, but the GList must be freed.
    418  *
    419  * Since: 1.1.15
    420  */
    421 GList* webkit_web_data_source_get_subresources(WebKitWebDataSource* webDataSource)
    422 {
    423     g_return_val_if_fail(WEBKIT_IS_WEB_DATA_SOURCE(webDataSource), NULL);
    424 
    425     WebKitWebFrame* webFrame = webkit_web_data_source_get_web_frame(webDataSource);
    426     WebKitWebView* webView = getViewFromFrame(webFrame);
    427 
    428     return webkit_web_view_get_subresources(webView);
    429 }
    430 
    431 namespace WebKit {
    432 
    433 WebKitWebDataSource* kitNew(PassRefPtr<WebKit::DocumentLoader> loader)
    434 {
    435     WebKitWebDataSource* webDataSource = WEBKIT_WEB_DATA_SOURCE(g_object_new(WEBKIT_TYPE_WEB_DATA_SOURCE, NULL));
    436     WebKitWebDataSourcePrivate* priv = webDataSource->priv;
    437     priv->loader = loader.releaseRef();
    438 
    439     return webDataSource;
    440 }
    441 
    442 }
    443