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 "FrameLoader.h"
26 #include "FrameLoaderClientGtk.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"
43 * SECTION:webkitwebdatasource
44 * @short_description: Encapsulates the content to be displayed in a #WebKitWebFrame.
45 * @see_also: #WebKitWebFrame
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.
62 using namespace WebCore;
63 using namespace WebKit;
65 struct _WebKitWebDataSourcePrivate {
66 WebKit::DocumentLoader* loader;
68 WebKitNetworkRequest* initialRequest;
69 WebKitNetworkRequest* networkRequest;
70 WebKitWebResource* mainresource;
75 gchar* unreachableURL;
78 G_DEFINE_TYPE(WebKitWebDataSource, webkit_web_data_source, G_TYPE_OBJECT);
80 static void webkit_web_data_source_dispose(GObject* object)
82 WebKitWebDataSource* webDataSource = WEBKIT_WEB_DATA_SOURCE(object);
83 WebKitWebDataSourcePrivate* priv = webDataSource->priv;
86 ASSERT(!priv->loader->isLoading());
87 priv->loader->detachDataSource();
88 priv->loader->deref();
90 if (priv->initialRequest) {
91 g_object_unref(priv->initialRequest);
92 priv->initialRequest = NULL;
95 if (priv->networkRequest) {
96 g_object_unref(priv->networkRequest);
97 priv->networkRequest = NULL;
100 if (priv->mainresource) {
101 g_object_unref(priv->mainresource);
102 priv->mainresource = NULL;
105 G_OBJECT_CLASS(webkit_web_data_source_parent_class)->dispose(object);
108 static void webkit_web_data_source_finalize(GObject* object)
110 WebKitWebDataSource* dataSource = WEBKIT_WEB_DATA_SOURCE(object);
111 WebKitWebDataSourcePrivate* priv = dataSource->priv;
113 g_free(priv->unreachableURL);
114 g_free(priv->textEncoding);
117 g_string_free(priv->data, TRUE);
121 G_OBJECT_CLASS(webkit_web_data_source_parent_class)->finalize(object);
124 static void webkit_web_data_source_class_init(WebKitWebDataSourceClass* klass)
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;
132 g_type_class_add_private(gobject_class, sizeof(WebKitWebDataSourcePrivate));
135 static void webkit_web_data_source_init(WebKitWebDataSource* webDataSource)
137 webDataSource->priv = G_TYPE_INSTANCE_GET_PRIVATE(webDataSource, WEBKIT_TYPE_WEB_DATA_SOURCE, WebKitWebDataSourcePrivate);
141 * webkit_web_data_source_new:
143 * Creates a new #WebKitWebDataSource instance. The URL of the
144 * #WebKitWebDataSource will be set to "about:blank".
146 * Return: a new #WebKitWebDataSource.
150 WebKitWebDataSource* webkit_web_data_source_new()
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);
160 * webkit_web_data_source_new_with_request:
161 * @request: the #WebKitNetworkRequest to use to create this data source
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.
167 * Returns: a new #WebKitWebDataSource
171 WebKitWebDataSource* webkit_web_data_source_new_with_request(WebKitNetworkRequest* request)
175 const gchar* uri = webkit_network_request_get_uri(request);
177 ResourceRequest resourceRequest(ResourceRequest(KURL(KURL(), String::fromUTF8(uri))));
178 WebKitWebDataSource* datasource = kitNew(WebKit::DocumentLoader::create(resourceRequest, SubstituteData()));
180 WebKitWebDataSourcePrivate* priv = datasource->priv;
181 priv->initialRequest = request;
187 * webkit_web_data_source_get_web_frame:
188 * @data_source: a #WebKitWebDataSource
190 * Returns the #WebKitWebFrame that represents this data source
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.
199 WebKitWebFrame* webkit_web_data_source_get_web_frame(WebKitWebDataSource* webDataSource)
201 g_return_val_if_fail(WEBKIT_IS_WEB_DATA_SOURCE(webDataSource), NULL);
203 WebKitWebDataSourcePrivate* priv = webDataSource->priv;
204 FrameLoader* frameLoader = priv->loader->frameLoader();
209 return static_cast<WebKit::FrameLoaderClient*>(frameLoader->client())->webFrame();
213 * webkit_web_data_source_get_initial_request:
214 * @data_source: a #WebKitWebDataSource
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.
221 * Return value: (transfer none): the original #WebKitNetworkRequest
225 WebKitNetworkRequest* webkit_web_data_source_get_initial_request(WebKitWebDataSource* webDataSource)
227 g_return_val_if_fail(WEBKIT_IS_WEB_DATA_SOURCE(webDataSource), NULL);
229 WebKitWebDataSourcePrivate* priv = webDataSource->priv;
230 ResourceRequest request = priv->loader->originalRequest();
232 if (priv->initialRequest)
233 g_object_unref(priv->initialRequest);
235 priv->initialRequest = kitNew(request);
236 return priv->initialRequest;
240 * webkit_web_data_source_get_request:
241 * @data_source: a #WebKitWebDataSource
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.
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.
254 WebKitNetworkRequest* webkit_web_data_source_get_request(WebKitWebDataSource* webDataSource)
256 g_return_val_if_fail(WEBKIT_IS_WEB_DATA_SOURCE(webDataSource), NULL);
258 WebKitWebDataSourcePrivate* priv = webDataSource->priv;
259 FrameLoader* frameLoader = priv->loader->frameLoader();
260 if (!frameLoader || !frameLoader->frameHasLoaded())
263 ResourceRequest request = priv->loader->request();
265 if (priv->networkRequest)
266 g_object_unref(priv->networkRequest);
268 priv->networkRequest = kitNew(request);
269 return priv->networkRequest;
273 * webkit_web_data_source_get_encoding:
274 * @data_source: a #WebKitWebDataSource
276 * Returns the text encoding name as set in the #WebKitWebView, or if not, the
277 * text encoding of the response.
279 * Return value: the encoding name of the #WebKitWebView or of the response.
283 const gchar* webkit_web_data_source_get_encoding(WebKitWebDataSource* webDataSource)
285 g_return_val_if_fail(WEBKIT_IS_WEB_DATA_SOURCE(webDataSource), NULL);
287 WebKitWebDataSourcePrivate* priv = webDataSource->priv;
288 String textEncodingName = priv->loader->overrideEncoding();
290 if (!textEncodingName)
291 textEncodingName = priv->loader->response().textEncodingName();
293 CString encoding = textEncodingName.utf8();
294 g_free(priv->textEncoding);
295 priv->textEncoding = g_strdup(encoding.data());
296 return priv->textEncoding;
300 * webkit_web_data_source_is_loading:
301 * @data_source: a #WebKitWebDataSource
303 * Determines whether the data source is in the process of loading its content.
305 * Return value: %TRUE if the @data_source is still loading, %FALSE otherwise
309 gboolean webkit_web_data_source_is_loading(WebKitWebDataSource* webDataSource)
311 g_return_val_if_fail(WEBKIT_IS_WEB_DATA_SOURCE(webDataSource), FALSE);
313 WebKitWebDataSourcePrivate* priv = webDataSource->priv;
315 return priv->loader->isLoadingInAPISense();
319 * webkit_web_data_source_get_data:
320 * @data_source: a #WebKitWebDataSource
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.
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.
333 GString* webkit_web_data_source_get_data(WebKitWebDataSource* webDataSource)
335 g_return_val_if_fail(WEBKIT_IS_WEB_DATA_SOURCE(webDataSource), NULL);
337 WebKitWebDataSourcePrivate* priv = webDataSource->priv;
339 RefPtr<SharedBuffer> mainResourceData = priv->loader->mainResourceData();
341 if (!mainResourceData)
345 g_string_free(priv->data, TRUE);
349 priv->data = g_string_new_len(mainResourceData->data(), mainResourceData->size());
354 * webkit_web_data_source_get_main_resource:
355 * @data_source: a #WebKitWebDataSource
357 * Returns the main resource of the @data_source
359 * Return value: (transfer none): a new #WebKitWebResource
360 * representing the main resource of the @data_source.
364 WebKitWebResource* webkit_web_data_source_get_main_resource(WebKitWebDataSource* webDataSource)
366 g_return_val_if_fail(WEBKIT_IS_WEB_DATA_SOURCE(webDataSource), NULL);
368 WebKitWebDataSourcePrivate* priv = webDataSource->priv;
370 if (priv->mainresource)
371 return priv->mainresource;
373 WebKitWebFrame* webFrame = webkit_web_data_source_get_web_frame(webDataSource);
374 WebKitWebView* webView = getViewFromFrame(webFrame);
376 priv->mainresource = WEBKIT_WEB_RESOURCE(g_object_ref(webkit_web_view_get_main_resource(webView)));
378 return priv->mainresource;
382 * webkit_web_data_source_get_unreachable_uri:
383 * @data_source: a #WebKitWebDataSource
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.
389 * Return value: the unreachable URL of @data_source or %NULL if there is no unreachable URL.
393 const gchar* webkit_web_data_source_get_unreachable_uri(WebKitWebDataSource* webDataSource)
395 g_return_val_if_fail(WEBKIT_IS_WEB_DATA_SOURCE(webDataSource), NULL);
397 WebKitWebDataSourcePrivate* priv = webDataSource->priv;
398 const KURL& unreachableURL = priv->loader->unreachableURL();
400 if (unreachableURL.isEmpty())
403 g_free(priv->unreachableURL);
404 priv->unreachableURL = g_strdup(unreachableURL.string().utf8().data());
405 return priv->unreachableURL;
409 * webkit_web_data_source_get_subresources:
410 * @data_source: a #WebKitWebDataSource
412 * Gives you a #GList of #WebKitWebResource objects that compose the
413 * #WebKitWebView to which this #WebKitWebDataSource is attached.
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.
421 GList* webkit_web_data_source_get_subresources(WebKitWebDataSource* webDataSource)
423 g_return_val_if_fail(WEBKIT_IS_WEB_DATA_SOURCE(webDataSource), NULL);
425 WebKitWebFrame* webFrame = webkit_web_data_source_get_web_frame(webDataSource);
426 WebKitWebView* webView = getViewFromFrame(webFrame);
428 return webkit_web_view_get_subresources(webView);
433 WebKitWebDataSource* kitNew(PassRefPtr<WebKit::DocumentLoader> loader)
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();
439 return webDataSource;