2 * Copyright (C) 2009 Jan Michael C. Alonzo
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.
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.
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.
21 #include "webkitwebdatasource.h"
23 #include "ArchiveResource.h"
24 #include "DocumentLoaderGtk.h"
25 #include "FrameLoaderClientGtk.h"
26 #include "FrameLoader.h"
28 #include "PlatformString.h"
29 #include "ResourceRequest.h"
30 #include "runtime/InitializeThreading.h"
31 #include "SharedBuffer.h"
32 #include "SubstituteData.h"
33 #include "webkitwebresource.h"
34 #include "webkitprivate.h"
35 #include "wtf/Assertions.h"
40 * SECTION:webkitwebdatasource
41 * @short_description: Encapsulates the content to be displayed in a #WebKitWebFrame.
42 * @see_also: #WebKitWebFrame
44 * Data source encapsulates the content of a #WebKitWebFrame. A
45 * #WebKitWebFrame has a main resource and subresources and the data source
46 * provides access to these resources. When a request gets loaded initially,
47 * it is set to a provisional state. The application can request for the
48 * request that initiated the load by asking for the provisional data source
49 * and invoking the webkit_web_data_source_get_initial_request method of
50 * #WebKitWebDataSource. This data source may not have enough data and some
51 * methods may return empty values. To get a "full" data source with the data
52 * and resources loaded, you need to get the non-provisional data source
53 * through #WebKitWebFrame's webkit_web_frame_get_data_source method. This
54 * data source will have the data after everything was loaded. Make sure that
55 * the data source was finished loading before using any of its methods. You
56 * can do this via webkit_web_data_source_is_loading.
59 using namespace WebCore;
60 using namespace WebKit;
62 struct _WebKitWebDataSourcePrivate {
63 WebKit::DocumentLoader* loader;
65 WebKitNetworkRequest* initialRequest;
66 WebKitNetworkRequest* networkRequest;
67 WebKitWebResource* mainresource;
72 gchar* unreachableURL;
75 #define WEBKIT_WEB_DATA_SOURCE_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE((obj), WEBKIT_TYPE_WEB_DATA_SOURCE, WebKitWebDataSourcePrivate))
77 G_DEFINE_TYPE(WebKitWebDataSource, webkit_web_data_source, G_TYPE_OBJECT);
79 static void webkit_web_data_source_dispose(GObject* object)
81 WebKitWebDataSource* webDataSource = WEBKIT_WEB_DATA_SOURCE(object);
82 WebKitWebDataSourcePrivate* priv = webDataSource->priv;
85 ASSERT(!priv->loader->isLoading());
86 priv->loader->detachDataSource();
87 priv->loader->deref();
89 if (priv->initialRequest) {
90 g_object_unref(priv->initialRequest);
91 priv->initialRequest = NULL;
94 if (priv->networkRequest) {
95 g_object_unref(priv->networkRequest);
96 priv->networkRequest = NULL;
99 if (priv->mainresource) {
100 g_object_unref(priv->mainresource);
101 priv->mainresource = NULL;
104 G_OBJECT_CLASS(webkit_web_data_source_parent_class)->dispose(object);
107 static void webkit_web_data_source_finalize(GObject* object)
109 WebKitWebDataSource* dataSource = WEBKIT_WEB_DATA_SOURCE(object);
110 WebKitWebDataSourcePrivate* priv = dataSource->priv;
112 g_free(priv->unreachableURL);
113 g_free(priv->textEncoding);
116 g_string_free(priv->data, TRUE);
120 G_OBJECT_CLASS(webkit_web_data_source_parent_class)->finalize(object);
123 static void webkit_web_data_source_class_init(WebKitWebDataSourceClass* klass)
125 GObjectClass* gobject_class = G_OBJECT_CLASS(klass);
126 gobject_class->dispose = webkit_web_data_source_dispose;
127 gobject_class->finalize = webkit_web_data_source_finalize;
131 g_type_class_add_private(gobject_class, sizeof(WebKitWebDataSourcePrivate));
134 static void webkit_web_data_source_init(WebKitWebDataSource* webDataSource)
136 webDataSource->priv = WEBKIT_WEB_DATA_SOURCE_GET_PRIVATE(webDataSource);
139 WebKitWebDataSource* webkit_web_data_source_new_with_loader(PassRefPtr<WebKit::DocumentLoader> loader)
141 WebKitWebDataSource* webDataSource = WEBKIT_WEB_DATA_SOURCE(g_object_new(WEBKIT_TYPE_WEB_DATA_SOURCE, NULL));
142 WebKitWebDataSourcePrivate* priv = webDataSource->priv;
143 priv->loader = loader.releaseRef();
145 return webDataSource;
149 * webkit_web_data_source_new:
151 * Creates a new #WebKitWebDataSource instance. The URL of the
152 * #WebKitWebDataSource will be set to "about:blank".
154 * Return: a new #WebKitWebDataSource.
158 WebKitWebDataSource* webkit_web_data_source_new()
160 WebKitNetworkRequest* request = webkit_network_request_new("about:blank");
161 WebKitWebDataSource* datasource = webkit_web_data_source_new_with_request(request);
162 g_object_unref(request);
168 * webkit_web_data_source_new_with_request:
169 * @request: the #WebKitNetworkRequest to use to create this data source
171 * Creates a new #WebKitWebDataSource from a #WebKitNetworkRequest. Normally,
172 * #WebKitWebFrame objects create their data sources so you will almost never
173 * want to invoke this method directly.
175 * Returns: a new #WebKitWebDataSource
179 WebKitWebDataSource* webkit_web_data_source_new_with_request(WebKitNetworkRequest* request)
183 const gchar* uri = webkit_network_request_get_uri(request);
185 WebKitWebDataSource* datasource;
186 datasource = webkit_web_data_source_new_with_loader(
187 WebKit::DocumentLoader::create(ResourceRequest(KURL(KURL(), String::fromUTF8(uri))),
190 WebKitWebDataSourcePrivate* priv = datasource->priv;
191 priv->initialRequest = request;
197 * webkit_web_data_source_get_web_frame:
198 * @data_source: a #WebKitWebDataSource
200 * Returns the #WebKitWebFrame that represents this data source
202 * Return value: (transfer none): the #WebKitWebFrame that represents
203 * the @data_source. The #WebKitWebFrame is owned by WebKit and should
204 * not be freed or destroyed. This will return %NULL if the
205 * @data_source is not attached to a frame.
209 WebKitWebFrame* webkit_web_data_source_get_web_frame(WebKitWebDataSource* webDataSource)
211 g_return_val_if_fail(WEBKIT_IS_WEB_DATA_SOURCE(webDataSource), NULL);
213 WebKitWebDataSourcePrivate* priv = webDataSource->priv;
214 FrameLoader* frameLoader = priv->loader->frameLoader();
219 return static_cast<WebKit::FrameLoaderClient*>(frameLoader->client())->webFrame();
223 * webkit_web_data_source_get_initial_request:
224 * @data_source: a #WebKitWebDataSource
226 * Returns a reference to the original request that was used to load the web
227 * content. The #WebKitNetworkRequest returned by this method is the request
228 * prior to the "committed" load state. See webkit_web_data_source_get_request
229 * for getting the "committed" request.
231 * Return value: (transfer none): the original #WebKitNetworkRequest
235 WebKitNetworkRequest* webkit_web_data_source_get_initial_request(WebKitWebDataSource* webDataSource)
237 g_return_val_if_fail(WEBKIT_IS_WEB_DATA_SOURCE(webDataSource), NULL);
239 WebKitWebDataSourcePrivate* priv = webDataSource->priv;
240 ResourceRequest request = priv->loader->originalRequest();
242 if (priv->initialRequest)
243 g_object_unref(priv->initialRequest);
245 priv->initialRequest = webkit_network_request_new_with_core_request(request);
246 return priv->initialRequest;
250 * webkit_web_data_source_get_request:
251 * @data_source: a #WebKitWebDataSource
253 * Returns a #WebKitNetworkRequest that was used to create this
254 * #WebKitWebDataSource. The #WebKitNetworkRequest returned by this method is
255 * the request that was "committed", and hence, different from the request you
256 * get from the webkit_web_data_source_get_initial_request method.
258 * Return value: (transfer none): the #WebKitNetworkRequest that
259 * created the @data_source or %NULL if the @data_source is not
260 * attached to the frame or the frame hasn't been loaded.
264 WebKitNetworkRequest* webkit_web_data_source_get_request(WebKitWebDataSource* webDataSource)
266 g_return_val_if_fail(WEBKIT_IS_WEB_DATA_SOURCE(webDataSource), NULL);
268 WebKitWebDataSourcePrivate* priv = webDataSource->priv;
269 FrameLoader* frameLoader = priv->loader->frameLoader();
270 if (!frameLoader || !frameLoader->frameHasLoaded())
273 ResourceRequest request = priv->loader->request();
275 if (priv->networkRequest)
276 g_object_unref(priv->networkRequest);
278 priv->networkRequest = webkit_network_request_new_with_core_request(request);
279 return priv->networkRequest;
283 * webkit_web_data_source_get_encoding:
284 * @data_source: a #WebKitWebDataSource
286 * Returns the text encoding name as set in the #WebKitWebView, or if not, the
287 * text encoding of the response.
289 * Return value: the encoding name of the #WebKitWebView or of the response.
293 G_CONST_RETURN gchar* webkit_web_data_source_get_encoding(WebKitWebDataSource* webDataSource)
295 g_return_val_if_fail(WEBKIT_IS_WEB_DATA_SOURCE(webDataSource), NULL);
297 WebKitWebDataSourcePrivate* priv = webDataSource->priv;
298 String textEncodingName = priv->loader->overrideEncoding();
300 if (!textEncodingName)
301 textEncodingName = priv->loader->response().textEncodingName();
303 CString encoding = textEncodingName.utf8();
304 g_free(priv->textEncoding);
305 priv->textEncoding = g_strdup(encoding.data());
306 return priv->textEncoding;
310 * webkit_web_data_source_is_loading:
311 * @data_source: a #WebKitWebDataSource
313 * Determines whether the data source is in the process of loading its content.
315 * Return value: %TRUE if the @data_source is still loading, %FALSE otherwise
319 gboolean webkit_web_data_source_is_loading(WebKitWebDataSource* webDataSource)
321 g_return_val_if_fail(WEBKIT_IS_WEB_DATA_SOURCE(webDataSource), FALSE);
323 WebKitWebDataSourcePrivate* priv = webDataSource->priv;
325 return priv->loader->isLoadingInAPISense();
329 * webkit_web_data_source_get_data:
330 * @data_source: a #WebKitWebDataSource
332 * Returns the raw data that represents the the frame's content.The data will
333 * be incomplete until the data has finished loading. Returns %NULL if the web
334 * frame hasn't loaded any data. Use webkit_web_data_source_is_loading to test
335 * if data source is in the process of loading.
337 * Return value: (transfer none): a #GString which contains the raw
338 * data that represents the @data_source or %NULL if the @data_source
339 * hasn't loaded any data.
343 GString* webkit_web_data_source_get_data(WebKitWebDataSource* webDataSource)
345 g_return_val_if_fail(WEBKIT_IS_WEB_DATA_SOURCE(webDataSource), NULL);
347 WebKitWebDataSourcePrivate* priv = webDataSource->priv;
349 RefPtr<SharedBuffer> mainResourceData = priv->loader->mainResourceData();
351 if (!mainResourceData)
355 g_string_free(priv->data, TRUE);
359 priv->data = g_string_new_len(mainResourceData->data(), mainResourceData->size());
364 * webkit_web_data_source_get_main_resource:
365 * @data_source: a #WebKitWebDataSource
367 * Returns the main resource of the @data_source
369 * Return value: (transfer none): a new #WebKitWebResource
370 * representing the main resource of the @data_source.
374 WebKitWebResource* webkit_web_data_source_get_main_resource(WebKitWebDataSource* webDataSource)
376 g_return_val_if_fail(WEBKIT_IS_WEB_DATA_SOURCE(webDataSource), NULL);
378 WebKitWebDataSourcePrivate* priv = webDataSource->priv;
380 if (priv->mainresource)
381 return priv->mainresource;
383 WebKitWebFrame* webFrame = webkit_web_data_source_get_web_frame(webDataSource);
384 WebKitWebView* webView = getViewFromFrame(webFrame);
386 priv->mainresource = WEBKIT_WEB_RESOURCE(g_object_ref(webkit_web_view_get_main_resource(webView)));
388 return priv->mainresource;
392 * webkit_web_data_source_get_unreachable_uri:
393 * @data_source: a #WebKitWebDataSource
395 * Return the unreachable URI of @data_source. The @data_source will have an
396 * unreachable URL if it was created using #WebKitWebFrame's
397 * webkit_web_frame_load_alternate_html_string method.
399 * Return value: the unreachable URL of @data_source or %NULL if there is no unreachable URL.
403 G_CONST_RETURN gchar* webkit_web_data_source_get_unreachable_uri(WebKitWebDataSource* webDataSource)
405 g_return_val_if_fail(WEBKIT_IS_WEB_DATA_SOURCE(webDataSource), NULL);
407 WebKitWebDataSourcePrivate* priv = webDataSource->priv;
408 const KURL& unreachableURL = priv->loader->unreachableURL();
410 if (unreachableURL.isEmpty())
413 g_free(priv->unreachableURL);
414 priv->unreachableURL = g_strdup(unreachableURL.string().utf8().data());
415 return priv->unreachableURL;
419 * webkit_web_data_source_get_subresources:
420 * @data_source: a #WebKitWebDataSource
422 * Gives you a #GList of #WebKitWebResource objects that compose the
423 * #WebKitWebView to which this #WebKitWebDataSource is attached.
425 * Return value: (element-type WebKitWebResource) (transfer container):
426 * a #GList of #WebKitWebResource objects; the objects are owned by
427 * WebKit, but the GList must be freed.
431 GList* webkit_web_data_source_get_subresources(WebKitWebDataSource* webDataSource)
433 g_return_val_if_fail(WEBKIT_IS_WEB_DATA_SOURCE(webDataSource), NULL);
435 WebKitWebFrame* webFrame = webkit_web_data_source_get_web_frame(webDataSource);
436 WebKitWebView* webView = getViewFromFrame(webFrame);
438 return webkit_web_view_get_subresources(webView);