OSDN Git Service

Merge WebKit at r73109: Initial merge by git.
[android-x86/external-webkit.git] / WebKit / gtk / webkit / webkitwebdatasource.cpp
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
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
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
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
134 static void webkit_web_data_source_init(WebKitWebDataSource* webDataSource)
135 {
136     webDataSource->priv = WEBKIT_WEB_DATA_SOURCE_GET_PRIVATE(webDataSource);
137 }
138
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  */
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  */
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: (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.
206  *
207  * Since: 1.1.14
208  */
209 WebKitWebFrame* webkit_web_data_source_get_web_frame(WebKitWebDataSource* webDataSource)
210 {
211     g_return_val_if_fail(WEBKIT_IS_WEB_DATA_SOURCE(webDataSource), NULL);
212
213     WebKitWebDataSourcePrivate* priv = webDataSource->priv;
214     FrameLoader* frameLoader = priv->loader->frameLoader();
215
216     if (!frameLoader)
217         return NULL;
218
219     return static_cast<WebKit::FrameLoaderClient*>(frameLoader->client())->webFrame();
220 }
221
222 /**
223  * webkit_web_data_source_get_initial_request:
224  * @data_source: a #WebKitWebDataSource
225  *
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.
230  *
231  * Return value: (transfer none): the original #WebKitNetworkRequest
232  *
233  * Since: 1.1.14
234  */
235 WebKitNetworkRequest* webkit_web_data_source_get_initial_request(WebKitWebDataSource* webDataSource)
236 {
237     g_return_val_if_fail(WEBKIT_IS_WEB_DATA_SOURCE(webDataSource), NULL);
238
239     WebKitWebDataSourcePrivate* priv = webDataSource->priv;
240     ResourceRequest request = priv->loader->originalRequest();
241
242     if (priv->initialRequest)
243         g_object_unref(priv->initialRequest);
244
245     priv->initialRequest = webkit_network_request_new_with_core_request(request);
246     return priv->initialRequest;
247 }
248
249 /**
250  * webkit_web_data_source_get_request:
251  * @data_source: a #WebKitWebDataSource
252  *
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.
257  *
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.
261  *
262  * Since: 1.1.14
263  */
264 WebKitNetworkRequest* webkit_web_data_source_get_request(WebKitWebDataSource* webDataSource)
265 {
266     g_return_val_if_fail(WEBKIT_IS_WEB_DATA_SOURCE(webDataSource), NULL);
267
268     WebKitWebDataSourcePrivate* priv = webDataSource->priv;
269     FrameLoader* frameLoader = priv->loader->frameLoader();
270     if (!frameLoader || !frameLoader->frameHasLoaded())
271         return NULL;
272
273     ResourceRequest request = priv->loader->request();
274
275      if (priv->networkRequest)
276          g_object_unref(priv->networkRequest);
277
278      priv->networkRequest = webkit_network_request_new_with_core_request(request);
279      return priv->networkRequest;
280 }
281
282 /**
283  * webkit_web_data_source_get_encoding:
284  * @data_source: a #WebKitWebDataSource
285  *
286  * Returns the text encoding name as set in the #WebKitWebView, or if not, the
287  * text encoding of the response.
288  *
289  * Return value: the encoding name of the #WebKitWebView or of the response.
290  *
291  * Since: 1.1.14
292  */
293 G_CONST_RETURN gchar* webkit_web_data_source_get_encoding(WebKitWebDataSource* webDataSource)
294 {
295     g_return_val_if_fail(WEBKIT_IS_WEB_DATA_SOURCE(webDataSource), NULL);
296
297     WebKitWebDataSourcePrivate* priv = webDataSource->priv;
298     String textEncodingName = priv->loader->overrideEncoding();
299
300     if (!textEncodingName)
301         textEncodingName = priv->loader->response().textEncodingName();
302
303     CString encoding = textEncodingName.utf8();
304     g_free(priv->textEncoding);
305     priv->textEncoding = g_strdup(encoding.data());
306     return priv->textEncoding;
307 }
308
309 /**
310  * webkit_web_data_source_is_loading:
311  * @data_source: a #WebKitWebDataSource
312  *
313  * Determines whether the data source is in the process of loading its content.
314  *
315  * Return value: %TRUE if the @data_source is still loading, %FALSE otherwise
316  *
317  * Since: 1.1.14
318  */
319 gboolean webkit_web_data_source_is_loading(WebKitWebDataSource* webDataSource)
320 {
321     g_return_val_if_fail(WEBKIT_IS_WEB_DATA_SOURCE(webDataSource), FALSE);
322
323     WebKitWebDataSourcePrivate* priv = webDataSource->priv;
324
325     return priv->loader->isLoadingInAPISense();
326 }
327
328 /**
329  * webkit_web_data_source_get_data:
330  * @data_source: a #WebKitWebDataSource
331  *
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.
336  *
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.
340  *
341  * Since: 1.1.14
342  */
343 GString* webkit_web_data_source_get_data(WebKitWebDataSource* webDataSource)
344 {
345     g_return_val_if_fail(WEBKIT_IS_WEB_DATA_SOURCE(webDataSource), NULL);
346
347     WebKitWebDataSourcePrivate* priv = webDataSource->priv;
348
349     RefPtr<SharedBuffer> mainResourceData = priv->loader->mainResourceData();
350
351     if (!mainResourceData)
352         return NULL;
353
354     if (priv->data) {
355         g_string_free(priv->data, TRUE);
356         priv->data = NULL;
357     }
358
359     priv->data = g_string_new_len(mainResourceData->data(), mainResourceData->size());
360     return priv->data;
361 }
362
363 /**
364  * webkit_web_data_source_get_main_resource:
365  * @data_source: a #WebKitWebDataSource
366  *
367  * Returns the main resource of the @data_source
368  *
369  * Return value: (transfer none): a new #WebKitWebResource
370  * representing the main resource of the @data_source.
371  *
372  * Since: 1.1.14
373  */
374 WebKitWebResource* webkit_web_data_source_get_main_resource(WebKitWebDataSource* webDataSource)
375 {
376     g_return_val_if_fail(WEBKIT_IS_WEB_DATA_SOURCE(webDataSource), NULL);
377
378     WebKitWebDataSourcePrivate* priv = webDataSource->priv;
379
380     if (priv->mainresource)
381         return priv->mainresource;
382
383     WebKitWebFrame* webFrame = webkit_web_data_source_get_web_frame(webDataSource);
384     WebKitWebView* webView = getViewFromFrame(webFrame);
385
386     priv->mainresource = WEBKIT_WEB_RESOURCE(g_object_ref(webkit_web_view_get_main_resource(webView)));
387
388     return priv->mainresource;
389 }
390
391 /**
392  * webkit_web_data_source_get_unreachable_uri:
393  * @data_source: a #WebKitWebDataSource
394  *
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.
398  *
399  * Return value: the unreachable URL of @data_source or %NULL if there is no unreachable URL.
400  *
401  * Since: 1.1.14
402  */
403 G_CONST_RETURN gchar* webkit_web_data_source_get_unreachable_uri(WebKitWebDataSource* webDataSource)
404 {
405     g_return_val_if_fail(WEBKIT_IS_WEB_DATA_SOURCE(webDataSource), NULL);
406
407     WebKitWebDataSourcePrivate* priv = webDataSource->priv;
408     const KURL& unreachableURL = priv->loader->unreachableURL();
409
410     if (unreachableURL.isEmpty())
411         return NULL;
412
413     g_free(priv->unreachableURL);
414     priv->unreachableURL = g_strdup(unreachableURL.string().utf8().data());
415     return priv->unreachableURL;
416 }
417
418 /**
419  * webkit_web_data_source_get_subresources:
420  * @data_source: a #WebKitWebDataSource
421  *
422  * Gives you a #GList of #WebKitWebResource objects that compose the
423  * #WebKitWebView to which this #WebKitWebDataSource is attached.
424  *
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.
428  *
429  * Since: 1.1.15
430  */
431 GList* webkit_web_data_source_get_subresources(WebKitWebDataSource* webDataSource)
432 {
433     g_return_val_if_fail(WEBKIT_IS_WEB_DATA_SOURCE(webDataSource), NULL);
434
435     WebKitWebFrame* webFrame = webkit_web_data_source_get_web_frame(webDataSource);
436     WebKitWebView* webView = getViewFromFrame(webFrame);
437
438     return webkit_web_view_get_subresources(webView);
439 }