|
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 |
|
22 #include "webkitwebresource.h" |
|
23 #include "webkitprivate.h" |
|
24 |
|
25 #include "ArchiveResource.h" |
|
26 #include "KURL.h" |
|
27 #include "PlatformString.h" |
|
28 #include "SharedBuffer.h" |
|
29 #include "webkitenumtypes.h" |
|
30 #include "webkitmarshal.h" |
|
31 #include "wtf/Assertions.h" |
|
32 #include <wtf/text/CString.h> |
|
33 |
|
34 #include <glib.h> |
|
35 #include <glib/gi18n-lib.h> |
|
36 |
|
37 /** |
|
38 * SECTION:webkitwebresource |
|
39 * @short_description: Represents a downloaded URI. |
|
40 * @see_also: #WebKitWebDataSource |
|
41 * |
|
42 * A web resource encapsulates the data of the download as well as the URI, |
|
43 * MIME type and frame name of the resource. |
|
44 */ |
|
45 |
|
46 using namespace WebCore; |
|
47 using namespace WebKit; |
|
48 |
|
49 enum { |
|
50 PROP_0, |
|
51 |
|
52 PROP_URI, |
|
53 PROP_MIME_TYPE, |
|
54 PROP_ENCODING, |
|
55 PROP_FRAME_NAME |
|
56 }; |
|
57 |
|
58 G_DEFINE_TYPE(WebKitWebResource, webkit_web_resource, G_TYPE_OBJECT); |
|
59 |
|
60 static void webkit_web_resource_get_property(GObject* object, guint prop_id, GValue* value, GParamSpec* pspec); |
|
61 static void webkit_web_resource_set_property(GObject* object, guint prop_id, const GValue* value, GParamSpec* pspec); |
|
62 |
|
63 static void webkit_web_resource_cleanup(WebKitWebResource* webResource) |
|
64 { |
|
65 WebKitWebResourcePrivate* priv = webResource->priv; |
|
66 |
|
67 g_free(priv->uri); |
|
68 priv->uri = NULL; |
|
69 |
|
70 g_free(priv->mimeType); |
|
71 priv->mimeType = NULL; |
|
72 |
|
73 g_free(priv->textEncoding); |
|
74 priv->textEncoding = NULL; |
|
75 |
|
76 g_free(priv->frameName); |
|
77 priv->frameName = NULL; |
|
78 |
|
79 if (priv->data) |
|
80 g_string_free(priv->data, TRUE); |
|
81 priv->data = NULL; |
|
82 } |
|
83 |
|
84 static void webkit_web_resource_dispose(GObject* object) |
|
85 { |
|
86 WebKitWebResource* webResource = WEBKIT_WEB_RESOURCE(object); |
|
87 WebKitWebResourcePrivate* priv = webResource->priv; |
|
88 |
|
89 if (priv->resource) { |
|
90 priv->resource->deref(); |
|
91 priv->resource = 0; |
|
92 } |
|
93 |
|
94 G_OBJECT_CLASS(webkit_web_resource_parent_class)->dispose(object); |
|
95 } |
|
96 |
|
97 static void webkit_web_resource_finalize(GObject* object) |
|
98 { |
|
99 WebKitWebResource* webResource = WEBKIT_WEB_RESOURCE(object); |
|
100 |
|
101 webkit_web_resource_cleanup(webResource); |
|
102 |
|
103 G_OBJECT_CLASS(webkit_web_resource_parent_class)->finalize(object); |
|
104 } |
|
105 |
|
106 static void webkit_web_resource_class_init(WebKitWebResourceClass* klass) |
|
107 { |
|
108 GObjectClass* gobject_class = G_OBJECT_CLASS(klass); |
|
109 |
|
110 gobject_class->dispose = webkit_web_resource_dispose; |
|
111 gobject_class->finalize = webkit_web_resource_finalize; |
|
112 gobject_class->get_property = webkit_web_resource_get_property; |
|
113 gobject_class->set_property = webkit_web_resource_set_property; |
|
114 |
|
115 /** |
|
116 * WebKitWebResource:uri: |
|
117 * |
|
118 * The URI of the web resource |
|
119 * |
|
120 * Since: 1.1.14 |
|
121 */ |
|
122 g_object_class_install_property(gobject_class, |
|
123 PROP_URI, |
|
124 g_param_spec_string( |
|
125 "uri", |
|
126 _("URI"), |
|
127 _("The uri of the resource"), |
|
128 NULL, |
|
129 (GParamFlags)(WEBKIT_PARAM_READWRITE|G_PARAM_CONSTRUCT_ONLY))); |
|
130 /** |
|
131 * WebKitWebResource:mime-type: |
|
132 * |
|
133 * The MIME type of the web resource. |
|
134 * |
|
135 * Since: 1.1.14 |
|
136 */ |
|
137 g_object_class_install_property(gobject_class, |
|
138 PROP_MIME_TYPE, |
|
139 g_param_spec_string( |
|
140 "mime-type", |
|
141 _("MIME Type"), |
|
142 _("The MIME type of the resource"), |
|
143 NULL, |
|
144 WEBKIT_PARAM_READABLE)); |
|
145 /** |
|
146 * WebKitWebResource:encoding: |
|
147 * |
|
148 * The encoding name to which the web resource was encoded in. |
|
149 * |
|
150 * Since: 1.1.14 |
|
151 */ |
|
152 g_object_class_install_property(gobject_class, |
|
153 PROP_ENCODING, |
|
154 g_param_spec_string( |
|
155 "encoding", |
|
156 _("Encoding"), |
|
157 _("The text encoding name of the resource"), |
|
158 NULL, |
|
159 WEBKIT_PARAM_READABLE)); |
|
160 |
|
161 /** |
|
162 * WebKitWebResource:frame-name: |
|
163 * |
|
164 * The frame name for the web resource. |
|
165 * |
|
166 * Since: 1.1.14 |
|
167 */ |
|
168 g_object_class_install_property(gobject_class, |
|
169 PROP_FRAME_NAME, |
|
170 g_param_spec_string( |
|
171 "frame-name", |
|
172 _("Frame Name"), |
|
173 _("The frame name of the resource"), |
|
174 NULL, |
|
175 WEBKIT_PARAM_READABLE)); |
|
176 |
|
177 g_type_class_add_private(gobject_class, sizeof(WebKitWebResourcePrivate)); |
|
178 } |
|
179 |
|
180 static void webkit_web_resource_get_property(GObject* object, guint prop_id, GValue* value, GParamSpec* pspec) |
|
181 { |
|
182 WebKitWebResource* webResource = WEBKIT_WEB_RESOURCE(object); |
|
183 |
|
184 switch (prop_id) { |
|
185 case PROP_URI: |
|
186 g_value_set_string(value, webkit_web_resource_get_uri(webResource)); |
|
187 break; |
|
188 case PROP_MIME_TYPE: |
|
189 g_value_set_string(value, webkit_web_resource_get_mime_type(webResource)); |
|
190 break; |
|
191 case PROP_ENCODING: |
|
192 g_value_set_string(value, webkit_web_resource_get_encoding(webResource)); |
|
193 break; |
|
194 case PROP_FRAME_NAME: |
|
195 g_value_set_string(value, webkit_web_resource_get_frame_name(webResource)); |
|
196 break; |
|
197 default: |
|
198 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec); |
|
199 break; |
|
200 } |
|
201 } |
|
202 |
|
203 static void webkit_web_resource_set_property(GObject* object, guint prop_id, const GValue* value, GParamSpec* pspec) |
|
204 { |
|
205 WebKitWebResource* webResource = WEBKIT_WEB_RESOURCE(object); |
|
206 |
|
207 switch (prop_id) { |
|
208 case PROP_URI: |
|
209 g_free(webResource->priv->uri); |
|
210 webResource->priv->uri = g_value_dup_string(value); |
|
211 break; |
|
212 default: |
|
213 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec); |
|
214 break; |
|
215 } |
|
216 } |
|
217 |
|
218 static void webkit_web_resource_init(WebKitWebResource* webResource) |
|
219 { |
|
220 webResource->priv = WEBKIT_WEB_RESOURCE_GET_PRIVATE(webResource); |
|
221 } |
|
222 |
|
223 // internal use only |
|
224 WebKitWebResource* webkit_web_resource_new_with_core_resource(PassRefPtr<ArchiveResource> resource) |
|
225 { |
|
226 WebKitWebResource* webResource = WEBKIT_WEB_RESOURCE(g_object_new(WEBKIT_TYPE_WEB_RESOURCE, NULL)); |
|
227 WebKitWebResourcePrivate* priv = webResource->priv; |
|
228 priv->resource = resource.releaseRef(); |
|
229 |
|
230 return webResource; |
|
231 } |
|
232 |
|
233 void webkit_web_resource_init_with_core_resource(WebKitWebResource* webResource, PassRefPtr<ArchiveResource> resource) |
|
234 { |
|
235 ASSERT(resource); |
|
236 |
|
237 WebKitWebResourcePrivate* priv = webResource->priv; |
|
238 |
|
239 if (priv->resource) |
|
240 priv->resource->deref(); |
|
241 |
|
242 priv->resource = resource.releaseRef(); |
|
243 } |
|
244 |
|
245 /** |
|
246 * webkit_web_resource_new: |
|
247 * @data: the data to initialize the #WebKitWebResource |
|
248 * @size: the length of @data |
|
249 * @uri: the uri of the #WebKitWebResource |
|
250 * @mime_type: the MIME type of the #WebKitWebResource |
|
251 * @encoding: the text encoding name of the #WebKitWebResource |
|
252 * @frame_name: the frame name of the #WebKitWebResource |
|
253 * |
|
254 * Returns a new #WebKitWebResource. The @encoding can be %NULL. The |
|
255 * @frame_name argument can be used if the resource represents contents of an |
|
256 * entire HTML frame, otherwise pass %NULL. |
|
257 * |
|
258 * Return value: a new #WebKitWebResource |
|
259 * |
|
260 * Since: 1.1.14 |
|
261 */ |
|
262 WebKitWebResource* webkit_web_resource_new(const gchar* data, |
|
263 gssize size, |
|
264 const gchar* uri, |
|
265 const gchar* mimeType, |
|
266 const gchar* encoding, |
|
267 const gchar* frameName) |
|
268 { |
|
269 g_return_val_if_fail(data, NULL); |
|
270 g_return_val_if_fail(uri, NULL); |
|
271 g_return_val_if_fail(mimeType, NULL); |
|
272 |
|
273 if (size < 0) |
|
274 size = strlen(data); |
|
275 |
|
276 RefPtr<SharedBuffer> buffer = SharedBuffer::create(data, size); |
|
277 WebKitWebResource* webResource = webkit_web_resource_new_with_core_resource(ArchiveResource::create(buffer, KURL(KURL(), String::fromUTF8(uri)), String::fromUTF8(mimeType), String::fromUTF8(encoding), String::fromUTF8(frameName))); |
|
278 |
|
279 return webResource; |
|
280 } |
|
281 |
|
282 /** |
|
283 * webkit_web_resource_get_data: |
|
284 * @web_resource: a #WebKitWebResource |
|
285 * |
|
286 * Returns the data of the @webResource. |
|
287 * |
|
288 * Return value: a #GString containing the character data of the @webResource. |
|
289 * The string is owned by WebKit and should not be freed or destroyed. |
|
290 * |
|
291 * Since: 1.1.14 |
|
292 */ |
|
293 GString* webkit_web_resource_get_data(WebKitWebResource* webResource) |
|
294 { |
|
295 g_return_val_if_fail(WEBKIT_IS_WEB_RESOURCE(webResource), NULL); |
|
296 |
|
297 WebKitWebResourcePrivate* priv = webResource->priv; |
|
298 |
|
299 if (!priv->resource) |
|
300 return NULL; |
|
301 |
|
302 if (!priv->data) |
|
303 priv->data = g_string_new_len(priv->resource->data()->data(), priv->resource->data()->size()); |
|
304 |
|
305 return priv->data; |
|
306 } |
|
307 |
|
308 /** |
|
309 * webkit_web_resource_get_uri: |
|
310 * @web_resource: a #WebKitWebResource |
|
311 * |
|
312 * Return value: the URI of the resource |
|
313 * |
|
314 * Since: 1.1.14 |
|
315 */ |
|
316 G_CONST_RETURN gchar* webkit_web_resource_get_uri(WebKitWebResource* webResource) |
|
317 { |
|
318 g_return_val_if_fail(WEBKIT_IS_WEB_RESOURCE(webResource), NULL); |
|
319 |
|
320 WebKitWebResourcePrivate* priv = webResource->priv; |
|
321 |
|
322 |
|
323 // We may have an URI without having a resource assigned to us (e.g., if the |
|
324 // FrameLoaderClient only had a ResourceRequest when we got created |
|
325 if (priv->uri) |
|
326 return priv->uri; |
|
327 |
|
328 if (!priv->resource) |
|
329 return NULL; |
|
330 |
|
331 priv->uri = g_strdup(priv->resource->url().string().utf8().data()); |
|
332 |
|
333 return priv->uri; |
|
334 } |
|
335 |
|
336 /** |
|
337 * webkit_web_resource_get_mime_type: |
|
338 * @web_resource: a #WebKitWebResource |
|
339 * |
|
340 * Return value: the MIME type of the resource |
|
341 * |
|
342 * Since: 1.1.14 |
|
343 */ |
|
344 G_CONST_RETURN gchar* webkit_web_resource_get_mime_type(WebKitWebResource* webResource) |
|
345 { |
|
346 g_return_val_if_fail(WEBKIT_IS_WEB_RESOURCE(webResource), NULL); |
|
347 |
|
348 WebKitWebResourcePrivate* priv = webResource->priv; |
|
349 if (!priv->resource) |
|
350 return NULL; |
|
351 |
|
352 if (!priv->mimeType) |
|
353 priv->mimeType = g_strdup(priv->resource->mimeType().utf8().data()); |
|
354 |
|
355 return priv->mimeType; |
|
356 } |
|
357 |
|
358 /** |
|
359 * webkit_web_resource_get_encoding: |
|
360 * @web_resource: a #WebKitWebResource |
|
361 * |
|
362 * Return value: the encoding name of the resource |
|
363 * |
|
364 * Since: 1.1.14 |
|
365 */ |
|
366 G_CONST_RETURN gchar* webkit_web_resource_get_encoding(WebKitWebResource* webResource) |
|
367 { |
|
368 g_return_val_if_fail(WEBKIT_IS_WEB_RESOURCE(webResource), NULL); |
|
369 |
|
370 WebKitWebResourcePrivate* priv = webResource->priv; |
|
371 if (!priv->resource) |
|
372 return NULL; |
|
373 |
|
374 if (!priv->textEncoding) |
|
375 priv->textEncoding = g_strdup(priv->resource->textEncoding().utf8().data()); |
|
376 |
|
377 return priv->textEncoding; |
|
378 } |
|
379 |
|
380 /** |
|
381 * webkit_web_resource_get_frame_name: |
|
382 * @web_resource: a #WebKitWebResource |
|
383 * |
|
384 * Return value: the frame name of the resource. |
|
385 * |
|
386 * Since: 1.1.14 |
|
387 */ |
|
388 G_CONST_RETURN gchar* webkit_web_resource_get_frame_name(WebKitWebResource* webResource) |
|
389 { |
|
390 g_return_val_if_fail(WEBKIT_IS_WEB_RESOURCE(webResource), NULL); |
|
391 |
|
392 WebKitWebResourcePrivate* priv = webResource->priv; |
|
393 if (!priv->resource) |
|
394 return NULL; |
|
395 |
|
396 if (!priv->frameName) |
|
397 priv->frameName = g_strdup(priv->resource->frameName().utf8().data()); |
|
398 |
|
399 return priv->frameName; |
|
400 } |
|
401 |