Line 0
Link Here
|
|
|
1 |
/* |
2 |
* Copyright (C) 2008 Collabora Ltd. |
3 |
* Copyright (C) 2009 Gustavo Noronha Silva <gns@gnome.org> |
4 |
* |
5 |
* This library is free software; you can redistribute it and/or |
6 |
* modify it under the terms of the GNU Library General Public |
7 |
* License as published by the Free Software Foundation; either |
8 |
* version 2 of the License, or (at your option) any later version. |
9 |
* |
10 |
* This library is distributed in the hope that it will be useful, |
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 |
* Library General Public License for more details. |
14 |
* |
15 |
* You should have received a copy of the GNU Library General Public License |
16 |
* along with this library; see the file COPYING.LIB. If not, write to |
17 |
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
18 |
* Boston, MA 02110-1301, USA. |
19 |
*/ |
20 |
|
21 |
#include "config.h" |
22 |
|
23 |
#include "CString.h" |
24 |
#include "Noncopyable.h" |
25 |
#include "NotImplemented.h" |
26 |
#include "ResourceHandleClient.h" |
27 |
#include "ResourceRequest.h" |
28 |
#include "ResourceResponse.h" |
29 |
#include "webkitmarshal.h" |
30 |
#include "webkitdownload.h" |
31 |
#include "webkitprivate.h" |
32 |
#include <glib/gstdio.h> |
33 |
|
34 |
using namespace WebKit; |
35 |
using namespace WebCore; |
36 |
|
37 |
extern "C" { |
38 |
|
39 |
class DownloadClient : Noncopyable, public ResourceHandleClient { |
40 |
public: |
41 |
DownloadClient(WebKitDownload*); |
42 |
|
43 |
virtual void didReceiveResponse(ResourceHandle*, const ResourceResponse&); |
44 |
virtual void didReceiveData(ResourceHandle*, const char*, int, int); |
45 |
virtual void didFinishLoading(ResourceHandle*); |
46 |
virtual void didFail(ResourceHandle*, const ResourceError&); |
47 |
virtual void wasBlocked(ResourceHandle*); |
48 |
virtual void cannotShowURL(ResourceHandle*); |
49 |
|
50 |
private: |
51 |
WebKitDownload* m_download; |
52 |
}; |
53 |
|
54 |
#define WEBKIT_DOWNLOAD_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE((obj), WEBKIT_TYPE_DOWNLOAD, WebKitDownloadPrivate)) |
55 |
|
56 |
struct _WebKitDownloadPrivate { |
57 |
gchar* destination_uri; |
58 |
gchar* suggested_filename; |
59 |
guint current_size; |
60 |
GTimer* timer; |
61 |
WebKitDownloadState state; |
62 |
GFileOutputStream* output_stream; |
63 |
DownloadClient* download_client; |
64 |
WebKitNetworkRequest* network_request; |
65 |
WebCore::ResourceResponse* network_response; |
66 |
RefPtr<WebCore::ResourceHandle> resource_handle; |
67 |
}; |
68 |
|
69 |
enum { |
70 |
/* normal signals */ |
71 |
ERROR, |
72 |
LAST_SIGNAL |
73 |
}; |
74 |
|
75 |
static guint webkit_download_signals[LAST_SIGNAL] = { 0, }; |
76 |
|
77 |
enum { |
78 |
PROP_0, |
79 |
|
80 |
PROP_NETWORK_REQUEST, |
81 |
PROP_DESTINATION_URI, |
82 |
PROP_SUGGESTED_FILENAME, |
83 |
PROP_PROGRESS, |
84 |
PROP_CURRENT_SIZE, |
85 |
PROP_TOTAL_SIZE, |
86 |
}; |
87 |
|
88 |
G_DEFINE_TYPE(WebKitDownload, webkit_download, G_TYPE_OBJECT); |
89 |
|
90 |
static void webkit_download_dispose(GObject* object) |
91 |
{ |
92 |
WebKitDownload* download = WEBKIT_DOWNLOAD(object); |
93 |
WebKitDownloadPrivate* priv = download->priv; |
94 |
|
95 |
if (priv->output_stream) { |
96 |
g_object_unref(priv->output_stream); |
97 |
priv->output_stream = NULL; |
98 |
} |
99 |
|
100 |
if (priv->network_request) { |
101 |
g_object_unref(priv->network_request); |
102 |
priv->network_request = NULL; |
103 |
} |
104 |
|
105 |
G_OBJECT_CLASS(webkit_download_parent_class)->dispose(object); |
106 |
} |
107 |
|
108 |
static void webkit_download_finalize(GObject* object) |
109 |
{ |
110 |
WebKitDownload* download = WEBKIT_DOWNLOAD(object); |
111 |
WebKitDownloadPrivate* priv = download->priv; |
112 |
|
113 |
/* We don't call webkit_download_cancel() because we don't want to emit |
114 |
* signals when finalising an object. */ |
115 |
if (priv->resource_handle) { |
116 |
if (priv->state == WEBKIT_DOWNLOAD_STATE_STARTED) { |
117 |
priv->resource_handle->setClient(0); |
118 |
priv->resource_handle->cancel(); |
119 |
} |
120 |
priv->resource_handle.release(); |
121 |
} |
122 |
|
123 |
delete priv->download_client; |
124 |
delete priv->network_response; |
125 |
|
126 |
g_timer_destroy(priv->timer); |
127 |
|
128 |
g_free(priv->destination_uri); |
129 |
g_free(priv->suggested_filename); |
130 |
|
131 |
G_OBJECT_CLASS(webkit_download_parent_class)->finalize(object); |
132 |
} |
133 |
|
134 |
static void webkit_download_get_property(GObject* object, guint prop_id, GValue* value, GParamSpec* pspec) |
135 |
{ |
136 |
WebKitDownload* download = WEBKIT_DOWNLOAD(object); |
137 |
|
138 |
switch(prop_id) { |
139 |
case PROP_NETWORK_REQUEST: |
140 |
g_value_set_object(value, webkit_download_get_network_request(download)); |
141 |
break; |
142 |
case PROP_DESTINATION_URI: |
143 |
g_value_set_string(value, webkit_download_get_destination_uri(download)); |
144 |
break; |
145 |
case PROP_SUGGESTED_FILENAME: |
146 |
g_value_set_string(value, webkit_download_get_suggested_filename(download)); |
147 |
break; |
148 |
case PROP_PROGRESS: |
149 |
g_value_set_double(value, webkit_download_get_progress(download)); |
150 |
break; |
151 |
case PROP_CURRENT_SIZE: |
152 |
g_value_set_uint64(value, webkit_download_get_current_size(download)); |
153 |
break; |
154 |
case PROP_TOTAL_SIZE: |
155 |
g_value_set_uint64(value, webkit_download_get_total_size(download)); |
156 |
break; |
157 |
default: |
158 |
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec); |
159 |
} |
160 |
} |
161 |
|
162 |
static void webkit_download_set_property(GObject* object, guint prop_id, const GValue* value, GParamSpec *pspec) |
163 |
{ |
164 |
WebKitDownload* download = WEBKIT_DOWNLOAD(object); |
165 |
WebKitDownloadPrivate* priv = download->priv; |
166 |
|
167 |
switch(prop_id) { |
168 |
case PROP_NETWORK_REQUEST: |
169 |
priv->network_request = (WebKitNetworkRequest*)g_value_dup_object(value); |
170 |
/* This is safe as network-request is a construct only property and suggested_filename |
171 |
* is initially null. */ |
172 |
priv->suggested_filename = g_path_get_basename(webkit_network_request_get_uri(priv->network_request)); |
173 |
break; |
174 |
case PROP_DESTINATION_URI: |
175 |
webkit_download_set_destination_uri(download, g_value_get_string(value)); |
176 |
break; |
177 |
default: |
178 |
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec); |
179 |
} |
180 |
} |
181 |
|
182 |
static void webkit_download_class_init(WebKitDownloadClass* downloadClass) |
183 |
{ |
184 |
/* |
185 |
* implementations of virtual methods |
186 |
*/ |
187 |
GObjectClass* objectClass = G_OBJECT_CLASS(downloadClass); |
188 |
objectClass->dispose = webkit_download_dispose; |
189 |
objectClass->finalize = webkit_download_finalize; |
190 |
objectClass->get_property = webkit_download_get_property; |
191 |
objectClass->set_property = webkit_download_set_property; |
192 |
|
193 |
/** |
194 |
* WebKitDownload::error: |
195 |
* @download: the object on which the signal is emitted |
196 |
* @current_bytes: the current count of bytes downloaded |
197 |
* @total_bytes: the total bytes count in the downloaded file, aka file size. |
198 |
* |
199 |
* Indicates an error in the download. |
200 |
* |
201 |
* Since: 1.1.1 |
202 |
*/ |
203 |
webkit_download_signals[ERROR] = g_signal_new("error", |
204 |
G_TYPE_FROM_CLASS(downloadClass), |
205 |
(GSignalFlags)(G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION), |
206 |
0, |
207 |
g_signal_accumulator_true_handled, |
208 |
NULL, |
209 |
webkit_marshal_BOOLEAN__INT_INT_STRING, |
210 |
G_TYPE_BOOLEAN, 3, |
211 |
G_TYPE_INT, |
212 |
G_TYPE_INT, |
213 |
G_TYPE_STRING); |
214 |
|
215 |
/* |
216 |
* properties |
217 |
*/ |
218 |
|
219 |
/** |
220 |
* WebKitDownload:network-request |
221 |
* |
222 |
* The #WebKitNetworkRequest instance associated with the download. |
223 |
* |
224 |
* Since: 1.1.1 |
225 |
*/ |
226 |
g_object_class_install_property(objectClass, |
227 |
PROP_NETWORK_REQUEST, |
228 |
g_param_spec_object( |
229 |
"network-request", |
230 |
"Network Request", |
231 |
"The network request for the URI that should be downloaded", |
232 |
WEBKIT_TYPE_NETWORK_REQUEST, |
233 |
(GParamFlags)(WEBKIT_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY))); |
234 |
|
235 |
/** |
236 |
* WebKitDownload:destination-uri |
237 |
* |
238 |
* The URI of the save location for this download. |
239 |
* |
240 |
* Since: 1.1.1 |
241 |
*/ |
242 |
g_object_class_install_property(objectClass, |
243 |
PROP_DESTINATION_URI, |
244 |
g_param_spec_string( |
245 |
"destination-uri", |
246 |
"Destination URI", |
247 |
"The destination URI where to save the file", |
248 |
"", |
249 |
WEBKIT_PARAM_READWRITE)); |
250 |
|
251 |
/** |
252 |
* WebKitDownload:suggested-filename |
253 |
* |
254 |
* The file name suggested as default when saving |
255 |
* |
256 |
* Since: 1.1.1 |
257 |
*/ |
258 |
g_object_class_install_property(objectClass, |
259 |
PROP_SUGGESTED_FILENAME, |
260 |
g_param_spec_string( |
261 |
"suggested-filename", |
262 |
"Suggested Filename", |
263 |
"The filename suggested as default when saving", |
264 |
"", |
265 |
WEBKIT_PARAM_READABLE)); |
266 |
|
267 |
/** |
268 |
* WebKitDownload:progress: |
269 |
* |
270 |
* Determines the current progress of the download. |
271 |
* |
272 |
* Since: 1.1.1 |
273 |
*/ |
274 |
g_object_class_install_property(objectClass, PROP_PROGRESS, |
275 |
g_param_spec_double("progress", |
276 |
"Progress", |
277 |
"Determines the current progress of the download", |
278 |
0.0, 1.0, 1.0, |
279 |
WEBKIT_PARAM_READABLE)); |
280 |
|
281 |
/** |
282 |
* WebKitDownload:current-size |
283 |
* |
284 |
* The length of the data already downloaded |
285 |
* |
286 |
* Since: 1.1.1 |
287 |
*/ |
288 |
g_object_class_install_property(objectClass, |
289 |
PROP_CURRENT_SIZE, |
290 |
g_param_spec_uint64( |
291 |
"current-size", |
292 |
"Current Size", |
293 |
"The length of the data already downloaded", |
294 |
0, G_MAXUINT64, 0, |
295 |
WEBKIT_PARAM_READABLE)); |
296 |
|
297 |
/** |
298 |
* WebKitDownload:total-size |
299 |
* |
300 |
* The total size of the file |
301 |
* |
302 |
* Since: 1.1.1 |
303 |
*/ |
304 |
g_object_class_install_property(objectClass, |
305 |
PROP_CURRENT_SIZE, |
306 |
g_param_spec_uint64( |
307 |
"total-size", |
308 |
"Total Size", |
309 |
"The total size of the file", |
310 |
0, G_MAXUINT64, 0, |
311 |
WEBKIT_PARAM_READABLE)); |
312 |
|
313 |
g_type_class_add_private(downloadClass, sizeof(WebKitDownloadPrivate)); |
314 |
} |
315 |
|
316 |
static void webkit_download_init(WebKitDownload* download) |
317 |
{ |
318 |
WebKitDownloadPrivate* priv = WEBKIT_DOWNLOAD_GET_PRIVATE(download); |
319 |
download->priv = priv; |
320 |
|
321 |
priv->download_client = new DownloadClient(download); |
322 |
priv->current_size = 0; |
323 |
priv->state = WEBKIT_DOWNLOAD_STATE_CREATED; |
324 |
} |
325 |
|
326 |
/** |
327 |
* webkit_download_new: |
328 |
* @uri: a URI |
329 |
* |
330 |
* Creates a new #WebKitDownload object for the given URI. |
331 |
* |
332 |
* Returns: the new #WebKitDownload |
333 |
* |
334 |
* Since: 1.1.1 |
335 |
*/ |
336 |
WebKitDownload* webkit_download_new(const gchar* uri) |
337 |
{ |
338 |
g_return_val_if_fail(uri, 0); |
339 |
|
340 |
WebKitNetworkRequest* request = webkit_network_request_new(uri); |
341 |
WebKitDownload* download = webkit_download_new_from_request(request); |
342 |
g_object_unref(request); |
343 |
return download; |
344 |
} |
345 |
|
346 |
/** |
347 |
* webkit_download_new_from_request: |
348 |
* @request: a #WebKitNetworkRequest |
349 |
* |
350 |
* Creates a new #WebKitDownload object for the given |
351 |
* #WebKitNetworkRequest object. |
352 |
* |
353 |
* Returns: the new #WebKitDownload |
354 |
* |
355 |
* Since: 1.1.1 |
356 |
*/ |
357 |
WebKitDownload* webkit_download_new_from_request(WebKitNetworkRequest* request) |
358 |
{ |
359 |
g_return_val_if_fail(request, 0); |
360 |
|
361 |
WebKitDownload* download = WEBKIT_DOWNLOAD(g_object_new(WEBKIT_TYPE_DOWNLOAD, "network-request", request, NULL)); |
362 |
return download; |
363 |
} |
364 |
|
365 |
static gboolean webkit_download_open_stream_for_uri(WebKitDownload* download, const gchar* uri, gboolean append=FALSE) |
366 |
{ |
367 |
g_return_val_if_fail(uri, FALSE); |
368 |
|
369 |
WebKitDownloadPrivate* priv = download->priv; |
370 |
GFile* file = g_file_new_for_uri(uri); |
371 |
GError* error = NULL; |
372 |
|
373 |
if (append) |
374 |
priv->output_stream = g_file_append_to(file, G_FILE_CREATE_NONE, NULL, &error); |
375 |
else |
376 |
priv->output_stream = g_file_replace(file, NULL, TRUE, G_FILE_CREATE_NONE, NULL, &error); |
377 |
|
378 |
g_object_unref(file); |
379 |
|
380 |
if(error) { |
381 |
gboolean handled; |
382 |
g_signal_emit_by_name(download, "error", 0, WEBKIT_DOWNLOAD_ERROR_DESTINATION, error->message, &handled); |
383 |
g_error_free(error); |
384 |
return FALSE; |
385 |
} |
386 |
|
387 |
return TRUE; |
388 |
} |
389 |
|
390 |
static void webkit_download_close_stream(WebKitDownload* download) |
391 |
{ |
392 |
WebKitDownloadPrivate* priv = download->priv; |
393 |
if (priv->output_stream) { |
394 |
g_object_unref(priv->output_stream); |
395 |
priv->output_stream = NULL; |
396 |
} |
397 |
} |
398 |
|
399 |
/** |
400 |
* webkit_download_start: |
401 |
* @download: the #WebKitDownload |
402 |
* |
403 |
* Initiates the download. Notice that you must have set the |
404 |
* destination-uri property before calling this method. |
405 |
* |
406 |
* Since: 1.1.1 |
407 |
*/ |
408 |
void webkit_download_start(WebKitDownload* download) |
409 |
{ |
410 |
g_return_if_fail(WEBKIT_IS_DOWNLOAD(download)); |
411 |
|
412 |
WebKitDownloadPrivate* priv = download->priv; |
413 |
g_return_if_fail(priv->destination_uri); |
414 |
g_return_if_fail(priv->state == WEBKIT_DOWNLOAD_STATE_CREATED); |
415 |
g_return_if_fail(priv->timer == NULL); |
416 |
|
417 |
if (priv->resource_handle) |
418 |
priv->resource_handle->setClient(priv->download_client); |
419 |
else { |
420 |
// FIXME use kit(priv->network_request) when WebKitNetworkRequest will be finished. |
421 |
ResourceRequest request(webkit_network_request_get_uri(priv->network_request)); |
422 |
priv->resource_handle = ResourceHandle::create(request, priv->download_client, 0, false, false, false); |
423 |
} |
424 |
|
425 |
priv->timer = g_timer_new (); |
426 |
webkit_download_open_stream_for_uri(download, priv->destination_uri); |
427 |
} |
428 |
|
429 |
/** |
430 |
* webkit_download_cancel: |
431 |
* @download: the #WebKitDownload |
432 |
* |
433 |
* Cancels the download. Calling this will not free the |
434 |
* #WebKitDownload object, so you still need to call |
435 |
* g_object_unref() on it, if you are the owner of a reference. Notice |
436 |
* that cancelling the download provokes the emission of the |
437 |
* WebKitDownload::error signal, reporting that the download was |
438 |
* cancelled. |
439 |
* |
440 |
* Since: 1.1.1 |
441 |
*/ |
442 |
void webkit_download_cancel(WebKitDownload* download) |
443 |
{ |
444 |
g_return_if_fail(WEBKIT_IS_DOWNLOAD(download)); |
445 |
|
446 |
WebKitDownloadPrivate* priv = download->priv; |
447 |
|
448 |
g_timer_stop(priv->timer); |
449 |
|
450 |
if (priv->resource_handle) |
451 |
priv->resource_handle->cancel(); |
452 |
|
453 |
priv->state = WEBKIT_DOWNLOAD_STATE_CANCELLED; |
454 |
|
455 |
gboolean handled; |
456 |
g_signal_emit_by_name(download, "error", 0, WEBKIT_DOWNLOAD_ERROR_CANCELLED_BY_USER, "User cancelled the download", &handled); |
457 |
} |
458 |
|
459 |
/** |
460 |
* webkit_download_get_uri: |
461 |
* @download: the #WebKitDownload |
462 |
* |
463 |
* Convenience method to retrieve the URI from the |
464 |
* #WebKitNetworkRequest which is being downloaded. |
465 |
* |
466 |
* Returns: the uri |
467 |
* |
468 |
* Since: 1.1.1 |
469 |
*/ |
470 |
const gchar* webkit_download_get_uri(WebKitDownload* download) |
471 |
{ |
472 |
g_return_val_if_fail(WEBKIT_IS_DOWNLOAD(download), NULL); |
473 |
|
474 |
WebKitDownloadPrivate* priv = download->priv; |
475 |
return webkit_network_request_get_uri(priv->network_request); |
476 |
} |
477 |
|
478 |
/** |
479 |
* webkit_download_get_network_request: |
480 |
* @download: the #WebKitDownload |
481 |
* |
482 |
* Retrieves the #WebKitNetworkRequest object that backs the download |
483 |
* process. |
484 |
* |
485 |
* Returns: the #WebKitNetworkRequest instance |
486 |
* |
487 |
* Since: 1.1.1 |
488 |
*/ |
489 |
WebKitNetworkRequest* webkit_download_get_network_request(WebKitDownload* download) |
490 |
{ |
491 |
g_return_val_if_fail(WEBKIT_IS_DOWNLOAD(download), NULL); |
492 |
|
493 |
WebKitDownloadPrivate* priv = download->priv; |
494 |
return priv->network_request; |
495 |
} |
496 |
|
497 |
static void webkit_download_set_response(WebKitDownload* download, const ResourceResponse& response) |
498 |
{ |
499 |
// FIXME Use WebKitNetworkResponse when it's merged |
500 |
WebKitDownloadPrivate* priv = download->priv; |
501 |
priv->network_response = new ResourceResponse(response); |
502 |
} |
503 |
|
504 |
/** |
505 |
* webkit_download_get_suggested_filename: |
506 |
* @download: the #WebKitDownload |
507 |
* |
508 |
* Retrieves the filename that was suggested by the server, or the one |
509 |
* derived by WebKit from the URI. |
510 |
* |
511 |
* Returns: the suggested filename |
512 |
* |
513 |
* Since: 1.1.1 |
514 |
*/ |
515 |
const gchar* webkit_download_get_suggested_filename(WebKitDownload* download) |
516 |
{ |
517 |
g_return_val_if_fail(WEBKIT_IS_DOWNLOAD(download), NULL); |
518 |
|
519 |
WebKitDownloadPrivate* priv = download->priv; |
520 |
return priv->suggested_filename; |
521 |
} |
522 |
|
523 |
/** |
524 |
* webkit_download_get_destination_uri: |
525 |
* @download: the #WebKitDownload |
526 |
* |
527 |
* Obtains the URI to which the downloaded file will be written. This |
528 |
* must have been set by the application before calling |
529 |
* webkit_download_start(), and may be %NULL. |
530 |
* |
531 |
* Returns: the destination URI or %NULL |
532 |
* |
533 |
* Since: 1.1.1 |
534 |
*/ |
535 |
const gchar* webkit_download_get_destination_uri(WebKitDownload* download) |
536 |
{ |
537 |
g_return_val_if_fail(WEBKIT_IS_DOWNLOAD(download), NULL); |
538 |
|
539 |
WebKitDownloadPrivate* priv = download->priv; |
540 |
return priv->destination_uri; |
541 |
} |
542 |
|
543 |
/** |
544 |
* webkit_download_get_destination_uri: |
545 |
* @download: the #WebKitDownload |
546 |
* @destination_uri: the destination URI |
547 |
* |
548 |
* Defines the URI that should be used to save the downloaded file to. |
549 |
* |
550 |
* Since: 1.1.1 |
551 |
*/ |
552 |
void webkit_download_set_destination_uri(WebKitDownload* download, const gchar* destination_uri) |
553 |
{ |
554 |
g_return_if_fail(WEBKIT_IS_DOWNLOAD(download)); |
555 |
g_return_if_fail(destination_uri); |
556 |
|
557 |
WebKitDownloadPrivate* priv = download->priv; |
558 |
if (priv->destination_uri && !strcmp(priv->destination_uri, destination_uri)) |
559 |
return; |
560 |
|
561 |
// FIXME can we have a better check? |
562 |
if (priv->state != WEBKIT_DOWNLOAD_STATE_CREATED && priv->state != WEBKIT_DOWNLOAD_STATE_CANCELLED) { |
563 |
ASSERT(priv->destination_uri); |
564 |
|
565 |
gboolean downloading = priv->output_stream != NULL; |
566 |
if (downloading) |
567 |
webkit_download_close_stream(download); |
568 |
|
569 |
GFile* src = g_file_new_for_uri(priv->destination_uri); |
570 |
GFile* dest = g_file_new_for_uri(destination_uri); |
571 |
GError *error = NULL; |
572 |
|
573 |
g_file_move(src, dest, G_FILE_COPY_BACKUP, NULL, NULL, NULL, &error); |
574 |
|
575 |
g_object_unref(src); |
576 |
g_object_unref(dest); |
577 |
|
578 |
if (error) { |
579 |
gboolean handled; |
580 |
g_signal_emit_by_name(download, "error", 0, WEBKIT_DOWNLOAD_ERROR_DESTINATION, error->message, &handled); |
581 |
g_error_free(error); |
582 |
return; |
583 |
} |
584 |
|
585 |
if (downloading) { |
586 |
if (!webkit_download_open_stream_for_uri(download, destination_uri, TRUE)) { |
587 |
webkit_download_cancel(download); |
588 |
return; |
589 |
} |
590 |
} |
591 |
} |
592 |
|
593 |
g_free(priv->destination_uri); |
594 |
priv->destination_uri = g_strdup(destination_uri); |
595 |
g_object_notify(G_OBJECT(download), "destination-uri"); |
596 |
} |
597 |
|
598 |
/** |
599 |
* webkit_download_get_state: |
600 |
* @download: the #WebKitDownload |
601 |
* |
602 |
* Obtains the current state of the download, as a |
603 |
* #WebKitDownloadState. |
604 |
* |
605 |
* Returns: the current #WebKitDownloadState |
606 |
* |
607 |
* Since: 1.1.1 |
608 |
*/ |
609 |
WebKitDownloadState webkit_download_get_state (WebKitDownload* download) |
610 |
{ |
611 |
g_return_val_if_fail(WEBKIT_IS_DOWNLOAD(download), WEBKIT_DOWNLOAD_STATE_ERROR); |
612 |
|
613 |
WebKitDownloadPrivate* priv = download->priv; |
614 |
return priv->state; |
615 |
} |
616 |
|
617 |
/** |
618 |
* webkit_download_get_total_size: |
619 |
* @download: the #WebKitDownload |
620 |
* |
621 |
* Returns the expected total size of the download. This is expected |
622 |
* because the server may provide incorrect or missing |
623 |
* Content-Length. Notice that this may grow over time, as it will be |
624 |
* always the same as current_size in the cases where current size |
625 |
* surpasses it. |
626 |
* |
627 |
* Returns: the expected total size of the downloaded file |
628 |
* |
629 |
* Since: 1.1.1 |
630 |
*/ |
631 |
guint64 webkit_download_get_total_size (WebKitDownload* download) |
632 |
{ |
633 |
g_return_val_if_fail(WEBKIT_IS_DOWNLOAD(download), 0); |
634 |
|
635 |
WebKitDownloadPrivate* priv = download->priv; |
636 |
if (!priv->network_response) |
637 |
return 0; |
638 |
|
639 |
return MAX(priv->current_size, priv->network_response->expectedContentLength()); |
640 |
} |
641 |
|
642 |
/** |
643 |
* webkit_download_get_current_size: |
644 |
* @download: the #WebKitDownload |
645 |
* |
646 |
* Current already downloaded size. |
647 |
* |
648 |
* Returns: the already downloaded size |
649 |
* |
650 |
* Since: 1.1.1 |
651 |
*/ |
652 |
guint64 webkit_download_get_current_size (WebKitDownload* download) |
653 |
{ |
654 |
g_return_val_if_fail(WEBKIT_IS_DOWNLOAD(download), 0); |
655 |
|
656 |
WebKitDownloadPrivate* priv = download->priv; |
657 |
return priv->current_size; |
658 |
} |
659 |
|
660 |
/** |
661 |
* webkit_download_get_progress: |
662 |
* @web_view: a #WebKitDownload |
663 |
* |
664 |
* Determines the current progress of the download. |
665 |
* |
666 |
* Returns: a #gdouble ranging from 0.0 to 1.0. |
667 |
* |
668 |
* Since: 1.1.1 |
669 |
*/ |
670 |
gdouble webkit_download_get_progress(WebKitDownload* download) |
671 |
{ |
672 |
g_return_val_if_fail(WEBKIT_IS_DOWNLOAD(download), 1.0); |
673 |
|
674 |
WebKitDownloadPrivate* priv = download->priv; |
675 |
gdouble total_size = (gdouble)priv->network_response->expectedContentLength(); |
676 |
|
677 |
if (total_size == 0) |
678 |
return 1.0; |
679 |
|
680 |
return ((gdouble)priv->current_size) / total_size; |
681 |
} |
682 |
|
683 |
/** |
684 |
* webkit_download_get_elapsed_time: |
685 |
* @web_view: a #WebKitDownload |
686 |
* |
687 |
* Elapsed time for the download in seconds, including any fractional |
688 |
* part. If the download is finished, had an error or was cancelled |
689 |
* this is the time between its start and the event. |
690 |
* |
691 |
* Returns: seconds since the download was started, as a #gdouble |
692 |
* |
693 |
* Since: 1.1.1 |
694 |
*/ |
695 |
gdouble webkit_download_get_elapsed_time(WebKitDownload* download) |
696 |
{ |
697 |
g_return_val_if_fail(WEBKIT_IS_DOWNLOAD(download), 0.0); |
698 |
|
699 |
WebKitDownloadPrivate* priv = download->priv; |
700 |
return g_timer_elapsed(priv->timer, NULL); |
701 |
} |
702 |
|
703 |
static void webkit_download_received_data(WebKitDownload* download, const gchar* data, int length) |
704 |
{ |
705 |
WebKitDownloadPrivate* priv = download->priv; |
706 |
|
707 |
if (priv->current_size == 0) { |
708 |
priv->state = WEBKIT_DOWNLOAD_STATE_STARTED; |
709 |
} |
710 |
|
711 |
ASSERT(priv->output_stream); |
712 |
|
713 |
gsize bytes_written; |
714 |
GError *error = NULL; |
715 |
|
716 |
g_output_stream_write_all(G_OUTPUT_STREAM(priv->output_stream), |
717 |
data, length, &bytes_written, NULL, &error); |
718 |
|
719 |
if (error) { |
720 |
gboolean handled; |
721 |
g_signal_emit_by_name(download, "error", 0, WEBKIT_DOWNLOAD_ERROR_DESTINATION, error->message, &handled); |
722 |
g_error_free(error); |
723 |
return; |
724 |
} |
725 |
|
726 |
priv->current_size += length; |
727 |
g_object_notify(G_OBJECT(download), "current-size"); |
728 |
|
729 |
ASSERT(priv->network_response); |
730 |
if (priv->current_size > priv->network_response->expectedContentLength()) |
731 |
g_object_notify(G_OBJECT(download), "total-size"); |
732 |
|
733 |
/* FIXME throttle the number of updates? |
734 |
* should we remove the previous g_object_notify()s if we are going to throttle |
735 |
* the progress updates? */ |
736 |
g_object_notify(G_OBJECT(download), "progress"); |
737 |
} |
738 |
|
739 |
static void webkit_download_finished_loading(WebKitDownload* download) |
740 |
{ |
741 |
webkit_download_close_stream(download); |
742 |
|
743 |
WebKitDownloadPrivate* priv = download->priv; |
744 |
|
745 |
g_timer_stop(priv->timer); |
746 |
priv->state = WEBKIT_DOWNLOAD_STATE_FINISHED; |
747 |
|
748 |
g_object_notify(G_OBJECT(download), "progress"); |
749 |
} |
750 |
|
751 |
static void webkit_download_error(WebKitDownload* download, const ResourceError& error) |
752 |
{ |
753 |
webkit_download_close_stream(download); |
754 |
|
755 |
WebKitDownloadPrivate* priv = download->priv; |
756 |
|
757 |
g_timer_stop(priv->timer); |
758 |
priv->state = WEBKIT_DOWNLOAD_STATE_ERROR; |
759 |
|
760 |
gboolean handled; |
761 |
g_signal_emit_by_name(download, "error", 0, WEBKIT_DOWNLOAD_ERROR_NETWORK, error.localizedDescription().utf8().data(), &handled); |
762 |
} |
763 |
|
764 |
DownloadClient::DownloadClient(WebKitDownload* download) |
765 |
: m_download(download) |
766 |
{ |
767 |
} |
768 |
|
769 |
void DownloadClient::didReceiveResponse(ResourceHandle*, const ResourceResponse& response) |
770 |
{ |
771 |
webkit_download_set_response(m_download, response); |
772 |
} |
773 |
|
774 |
void DownloadClient::didReceiveData(ResourceHandle*, const char* data, int length, int lengthReceived) |
775 |
{ |
776 |
webkit_download_received_data(m_download, data, length); |
777 |
} |
778 |
|
779 |
void DownloadClient::didFinishLoading(ResourceHandle*) |
780 |
{ |
781 |
webkit_download_finished_loading(m_download); |
782 |
} |
783 |
|
784 |
void DownloadClient::didFail(ResourceHandle*, const ResourceError& error) |
785 |
{ |
786 |
webkit_download_error(m_download, error); |
787 |
} |
788 |
|
789 |
void DownloadClient::wasBlocked(ResourceHandle*) |
790 |
{ |
791 |
// FIXME when we have the new frame loader signals and error handling. |
792 |
notImplemented(); |
793 |
} |
794 |
|
795 |
void DownloadClient::cannotShowURL(ResourceHandle*) |
796 |
{ |
797 |
// FIXME when we have the new frame loader signals and error handling. |
798 |
notImplemented(); |
799 |
} |
800 |
|
801 |
} |