• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "FrameLoaderClientGtk.h"
26 #include "FrameLoader.h"
27 #include "KURL.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"
36 
37 #include <glib.h>
38 
39 /**
40  * SECTION:webkitwebdatasource
41  * @short_description: Encapsulates the content to be displayed in a #WebKitWebFrame.
42  * @see_also: #WebKitWebFrame
43  *
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.
57  */
58 
59 using namespace WebCore;
60 using namespace WebKit;
61 
62 struct _WebKitWebDataSourcePrivate {
63     WebKit::DocumentLoader* loader;
64 
65     WebKitNetworkRequest* initialRequest;
66     WebKitNetworkRequest* networkRequest;
67     WebKitWebResource* mainresource;
68 
69     GString* data;
70 
71     gchar* textEncoding;
72     gchar* unreachableURL;
73 };
74 
75 #define WEBKIT_WEB_DATA_SOURCE_GET_PRIVATE(obj)        (G_TYPE_INSTANCE_GET_PRIVATE((obj), WEBKIT_TYPE_WEB_DATA_SOURCE, WebKitWebDataSourcePrivate))
76 
77 G_DEFINE_TYPE(WebKitWebDataSource, webkit_web_data_source, G_TYPE_OBJECT);
78 
webkit_web_data_source_dispose(GObject * object)79 static void webkit_web_data_source_dispose(GObject* object)
80 {
81     WebKitWebDataSource* webDataSource = WEBKIT_WEB_DATA_SOURCE(object);
82     WebKitWebDataSourcePrivate* priv = webDataSource->priv;
83 
84     ASSERT(priv->loader);
85     ASSERT(!priv->loader->isLoading());
86     priv->loader->detachDataSource();
87     priv->loader->deref();
88 
89     if (priv->initialRequest) {
90         g_object_unref(priv->initialRequest);
91         priv->initialRequest = NULL;
92     }
93 
94     if (priv->networkRequest) {
95         g_object_unref(priv->networkRequest);
96         priv->networkRequest = NULL;
97     }
98 
99     if (priv->mainresource) {
100         g_object_unref(priv->mainresource);
101         priv->mainresource = NULL;
102     }
103 
104     G_OBJECT_CLASS(webkit_web_data_source_parent_class)->dispose(object);
105 }
106 
webkit_web_data_source_finalize(GObject * object)107 static void webkit_web_data_source_finalize(GObject* object)
108 {
109     WebKitWebDataSource* dataSource = WEBKIT_WEB_DATA_SOURCE(object);
110     WebKitWebDataSourcePrivate* priv = dataSource->priv;
111 
112     g_free(priv->unreachableURL);
113     g_free(priv->textEncoding);
114 
115     if (priv->data) {
116         g_string_free(priv->data, TRUE);
117         priv->data = NULL;
118     }
119 
120     G_OBJECT_CLASS(webkit_web_data_source_parent_class)->finalize(object);
121 }
122 
webkit_web_data_source_class_init(WebKitWebDataSourceClass * klass)123 static void webkit_web_data_source_class_init(WebKitWebDataSourceClass* klass)
124 {
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;
128 
129     webkit_init();
130 
131     g_type_class_add_private(gobject_class, sizeof(WebKitWebDataSourcePrivate));
132 }
133 
webkit_web_data_source_init(WebKitWebDataSource * webDataSource)134 static void webkit_web_data_source_init(WebKitWebDataSource* webDataSource)
135 {
136     webDataSource->priv = WEBKIT_WEB_DATA_SOURCE_GET_PRIVATE(webDataSource);
137 }
138 
webkit_web_data_source_new_with_loader(PassRefPtr<WebKit::DocumentLoader> loader)139 WebKitWebDataSource* webkit_web_data_source_new_with_loader(PassRefPtr<WebKit::DocumentLoader> loader)
140 {
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();
144 
145     return webDataSource;
146 }
147 
148 /**
149  * webkit_web_data_source_new:
150  *
151  * Creates a new #WebKitWebDataSource instance. The URL of the
152  * #WebKitWebDataSource will be set to "about:blank".
153  *
154  * Return: a new #WebKitWebDataSource.
155  *
156  * Since: 1.1.14
157  */
webkit_web_data_source_new()158 WebKitWebDataSource* webkit_web_data_source_new()
159 {
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);
163 
164     return datasource;
165 }
166 
167 /**
168  * webkit_web_data_source_new_with_request:
169  * @request: the #WebKitNetworkRequest to use to create this data source
170  *
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.
174  *
175  * Returns: a new #WebKitWebDataSource
176  *
177  * Since: 1.1.14
178  */
webkit_web_data_source_new_with_request(WebKitNetworkRequest * request)179 WebKitWebDataSource* webkit_web_data_source_new_with_request(WebKitNetworkRequest* request)
180 {
181     ASSERT(request);
182 
183     const gchar* uri = webkit_network_request_get_uri(request);
184 
185     WebKitWebDataSource* datasource;
186     datasource = webkit_web_data_source_new_with_loader(
187         WebKit::DocumentLoader::create(ResourceRequest(KURL(KURL(), String::fromUTF8(uri))),
188                                        SubstituteData()));
189 
190     WebKitWebDataSourcePrivate* priv = datasource->priv;
191     priv->initialRequest = request;
192 
193     return datasource;
194 }
195 
196 /**
197  * webkit_web_data_source_get_web_frame
198  * @data_source: a #WebKitWebDataSource
199  *
200  * Returns the #WebKitWebFrame that represents this data source
201  *
202  * Return value: the #WebKitWebFrame that represents the @data_source. The
203  * #WebKitWebFrame is owned by WebKit and should not be freed or destroyed.
204  * This will return %NULL of the @data_source is not attached to a frame.
205  *
206  * Since: 1.1.14
207  */
webkit_web_data_source_get_web_frame(WebKitWebDataSource * webDataSource)208 WebKitWebFrame* webkit_web_data_source_get_web_frame(WebKitWebDataSource* webDataSource)
209 {
210     g_return_val_if_fail(WEBKIT_IS_WEB_DATA_SOURCE(webDataSource), NULL);
211 
212     WebKitWebDataSourcePrivate* priv = webDataSource->priv;
213     FrameLoader* frameLoader = priv->loader->frameLoader();
214 
215     if (!frameLoader)
216         return NULL;
217 
218     return static_cast<WebKit::FrameLoaderClient*>(frameLoader->client())->webFrame();
219 }
220 
221 /**
222  * webkit_web_data_source_get_initial_request:
223  * @data_source: a #WebKitWebDataSource
224  *
225  * Returns a reference to the original request that was used to load the web
226  * content. The #WebKitNetworkRequest returned by this method is the request
227  * prior to the "committed" load state. See webkit_web_data_source_get_request
228  * for getting the "committed" request.
229  *
230  * Return value: the original #WebKitNetworkRequest
231  *
232  * Since: 1.1.14
233  */
webkit_web_data_source_get_initial_request(WebKitWebDataSource * webDataSource)234 WebKitNetworkRequest* webkit_web_data_source_get_initial_request(WebKitWebDataSource* webDataSource)
235 {
236     g_return_val_if_fail(WEBKIT_IS_WEB_DATA_SOURCE(webDataSource), NULL);
237 
238     WebKitWebDataSourcePrivate* priv = webDataSource->priv;
239     ResourceRequest request = priv->loader->originalRequest();
240 
241     if (priv->initialRequest)
242         g_object_unref(priv->initialRequest);
243 
244     priv->initialRequest = webkit_network_request_new_with_core_request(request);
245     return priv->initialRequest;
246 }
247 
248 /**
249  * webkit_web_data_source_get_request:
250  * @data_source: a #WebKitWebDataSource
251  *
252  * Returns a #WebKitNetworkRequest that was used to create this
253  * #WebKitWebDataSource. The #WebKitNetworkRequest returned by this method is
254  * the request that was "committed", and hence, different from the request you
255  * get from the webkit_web_data_source_get_initial_request method.
256  *
257  * Return value: the #WebKitNetworkRequest that created the @data_source or
258  * %NULL if the @data_source is not attached to the frame or the frame hasn't
259  * been loaded.
260  *
261  * Since: 1.1.14
262  */
webkit_web_data_source_get_request(WebKitWebDataSource * webDataSource)263 WebKitNetworkRequest* webkit_web_data_source_get_request(WebKitWebDataSource* webDataSource)
264 {
265     g_return_val_if_fail(WEBKIT_IS_WEB_DATA_SOURCE(webDataSource), NULL);
266 
267     WebKitWebDataSourcePrivate* priv = webDataSource->priv;
268     FrameLoader* frameLoader = priv->loader->frameLoader();
269     if (!frameLoader || !frameLoader->frameHasLoaded())
270         return NULL;
271 
272     ResourceRequest request = priv->loader->request();
273 
274      if (priv->networkRequest)
275          g_object_unref(priv->networkRequest);
276 
277      priv->networkRequest = webkit_network_request_new_with_core_request(request);
278      return priv->networkRequest;
279 }
280 
281 /**
282  * webkit_web_data_source_get_encoding:
283  * @data_source: a #WebKitWebDataSource
284  *
285  * Returns the text encoding name as set in the #WebKitWebView, or if not, the
286  * text encoding of the response.
287  *
288  * Return value: the encoding name of the #WebKitWebView or of the response.
289  *
290  * Since: 1.1.14
291  */
webkit_web_data_source_get_encoding(WebKitWebDataSource * webDataSource)292 G_CONST_RETURN gchar* webkit_web_data_source_get_encoding(WebKitWebDataSource* webDataSource)
293 {
294     g_return_val_if_fail(WEBKIT_IS_WEB_DATA_SOURCE(webDataSource), NULL);
295 
296     WebKitWebDataSourcePrivate* priv = webDataSource->priv;
297     String textEncodingName = priv->loader->overrideEncoding();
298 
299     if (!textEncodingName)
300         textEncodingName = priv->loader->response().textEncodingName();
301 
302     CString encoding = textEncodingName.utf8();
303     g_free(priv->textEncoding);
304     priv->textEncoding = g_strdup(encoding.data());
305     return priv->textEncoding;
306 }
307 
308 /**
309  * webkit_web_data_source_is_loading:
310  * @data_source: a #WebKitWebDataSource
311  *
312  * Determines whether the data source is in the process of loading its content.
313  *
314  * Return value: %TRUE if the @data_source is still loading, %FALSE otherwise
315  *
316  * Since: 1.1.14
317  */
webkit_web_data_source_is_loading(WebKitWebDataSource * webDataSource)318 gboolean webkit_web_data_source_is_loading(WebKitWebDataSource* webDataSource)
319 {
320     g_return_val_if_fail(WEBKIT_IS_WEB_DATA_SOURCE(webDataSource), NULL);
321 
322     WebKitWebDataSourcePrivate* priv = webDataSource->priv;
323 
324     return priv->loader->isLoadingInAPISense();
325 }
326 
327 /**
328  * webkit_web_data_source_get_data:
329  * @data_source: a #WebKitWebDataSource
330  *
331  * Returns the raw data that represents the the frame's content.The data will
332  * be incomplete until the data has finished loading. Returns %NULL if the web
333  * frame hasn't loaded any data. Use webkit_web_data_source_is_loading to test
334  * if data source is in the process of loading.
335  *
336  * Return value: a #GString which contains the raw data that represents the @data_source or %NULL if the
337  * @data_source hasn't loaded any data.
338  *
339  * Since: 1.1.14
340  */
webkit_web_data_source_get_data(WebKitWebDataSource * webDataSource)341 GString* webkit_web_data_source_get_data(WebKitWebDataSource* webDataSource)
342 {
343     g_return_val_if_fail(WEBKIT_IS_WEB_DATA_SOURCE(webDataSource), NULL);
344 
345     WebKitWebDataSourcePrivate* priv = webDataSource->priv;
346 
347     RefPtr<SharedBuffer> mainResourceData = priv->loader->mainResourceData();
348 
349     if (!mainResourceData)
350         return NULL;
351 
352     if (priv->data) {
353         g_string_free(priv->data, TRUE);
354         priv->data = NULL;
355     }
356 
357     priv->data = g_string_new_len(mainResourceData->data(), mainResourceData->size());
358     return priv->data;
359 }
360 
361 /**
362  * webkit_web_data_source_get_main_resource:
363  * @data_source: a #WebKitWebDataSource
364  *
365  * Returns the main resource of the @data_source
366  *
367  * Return value: a new #WebKitWebResource representing the main resource of
368  * the @data_source.
369  *
370  * Since: 1.1.14
371  */
webkit_web_data_source_get_main_resource(WebKitWebDataSource * webDataSource)372 WebKitWebResource* webkit_web_data_source_get_main_resource(WebKitWebDataSource* webDataSource)
373 {
374     g_return_val_if_fail(WEBKIT_IS_WEB_DATA_SOURCE(webDataSource), NULL);
375 
376     WebKitWebDataSourcePrivate* priv = webDataSource->priv;
377 
378     if (priv->mainresource)
379         return priv->mainresource;
380 
381     WebKitWebFrame* webFrame = webkit_web_data_source_get_web_frame(webDataSource);
382     WebKitWebView* webView = getViewFromFrame(webFrame);
383 
384     priv->mainresource = WEBKIT_WEB_RESOURCE(g_object_ref(webkit_web_view_get_main_resource(webView)));
385 
386     return priv->mainresource;
387 }
388 
389 /**
390  * webkit_web_data_source_get_unreachable_uri:
391  * @data_source: a #WebKitWebDataSource
392  *
393  * Return the unreachable URI of @data_source. The @data_source will have an
394  * unreachable URL if it was created using #WebKitWebFrame's
395  * webkit_web_frame_load_alternate_html_string method.
396  *
397  * Return value: the unreachable URL of @data_source or %NULL if there is no unreachable URL.
398  *
399  * Since: 1.1.14
400  */
webkit_web_data_source_get_unreachable_uri(WebKitWebDataSource * webDataSource)401 G_CONST_RETURN gchar* webkit_web_data_source_get_unreachable_uri(WebKitWebDataSource* webDataSource)
402 {
403     g_return_val_if_fail(WEBKIT_IS_WEB_DATA_SOURCE(webDataSource), NULL);
404 
405     WebKitWebDataSourcePrivate* priv = webDataSource->priv;
406     const KURL& unreachableURL = priv->loader->unreachableURL();
407 
408     if (unreachableURL.isEmpty())
409         return NULL;
410 
411     g_free(priv->unreachableURL);
412     priv->unreachableURL = g_strdup(unreachableURL.string().utf8().data());
413     return priv->unreachableURL;
414 }
415 
416 /**
417  * webkit_web_data_source_get_subresources
418  * @data_source: a #WebKitWebDataSource
419  *
420  * Gives you a #GList of #WebKitWebResource objects that compose the
421  * #WebView to which this #WebKitWebDataSource is attached.
422  *
423  * Return value: a #GList of #WebKitResource objects; the objects are
424  * owned by WebKit, but the GList must be freed.
425  *
426  * Since: 1.1.15
427  */
webkit_web_data_source_get_subresources(WebKitWebDataSource * webDataSource)428 GList* webkit_web_data_source_get_subresources(WebKitWebDataSource* webDataSource)
429 {
430     g_return_val_if_fail(WEBKIT_IS_WEB_DATA_SOURCE(webDataSource), NULL);
431 
432     WebKitWebFrame* webFrame = webkit_web_data_source_get_web_frame(webDataSource);
433     WebKitWebView* webView = getViewFromFrame(webFrame);
434 
435     return webkit_web_view_get_subresources(webView);
436 }
437