2 * (C) Copyright 2004-2007 Shawn Betts
3 * (C) Copyright 2007 John J. Foerch
4 * (C) Copyright 2007-2008 Jeremy Maitin-Shepard
6 * Use, modification, and distribution are subject to the terms specified in the
10 require("content-buffer.js");
11 require("load-spec.js");
12 require("suggest-file-name.js");
14 /* buffer is used only to associate with the download */
15 define_keywords("$use_cache", "$buffer", "$prepare_download", "$temp_file");
16 function save_uri (lspec, output_file) {
17 keywords(arguments, $use_cache = true);
19 var use_cache = arguments.$use_cache;
21 var buffer = arguments.$buffer;
23 var prepare_download = arguments.$prepare_download;
25 var download_file = output_file;
27 var temp_file = arguments.$temp_file;
28 if (temp_file === true) {
29 temp_file = Cc["@mozilla.org/file/local;1"]
30 .createInstance(Ci.nsILocalFile);
31 temp_file.initWithFile(output_file);
32 temp_file.leafName = "temp";
33 temp_file.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666);
36 download_file = temp_file;
39 var uri = load_spec_uri(lspec);
40 var referrer_uri = load_spec_referrer(lspec);
41 var post_data = load_spec_post_data(lspec);
43 cache_key = load_spec_cache_key(lspec);
45 var file_uri = make_uri(download_file);
47 var persist = Cc["@mozilla.org/embedding/browser/nsWebBrowserPersist;1"]
48 .createInstance(Ci.nsIWebBrowserPersist);
50 persist.persistFlags =
51 Ci.nsIWebBrowserPersist.PERSIST_FLAGS_REPLACE_EXISTING_FILES |
52 Ci.nsIWebBrowserPersist.PERSIST_FLAGS_AUTODETECT_APPLY_CONVERSION |
53 Ci.nsIWebBrowserPersist.PERSIST_FLAGS_CLEANUP_ON_FAILURE;
56 persist.persistFlags |= Ci.nsIWebBrowserPersist.PERSIST_FLAGS_FROM_CACHE;
58 persist.persistFlags |= Ci.nsIWebBrowserPersist.PERSIST_FLAGS_BYPASS_CACHE;
60 var info = register_download(buffer, uri, download_file);
62 // We want this hook so that we run before the download buffer updates.
63 add_hook.call(info, "download_state_change_hook", function() {
64 if (info.state == DOWNLOAD_FINISHED) {
65 temp_file.moveTo(null, output_file.leafName);
66 info.target_file.leafName = output_file.leafName;
71 prepare_download(info);
73 var tr = Cc["@mozilla.org/transfer;1"].createInstance(Ci.nsITransfer);
74 tr.init(uri, file_uri, output_file.leafName,
75 load_spec_mime_info(lspec),
76 null /* start time */,
79 persist.progressListener = tr;
80 persist.saveURI(uri, cache_key, referrer_uri, post_data, null /* no extra headers */, file_uri);
86 // We have no DOM, and can only save the URL as is.
87 const SAVEMODE_FILEONLY = 0x00;
88 // We have a DOM and can save as complete.
89 const SAVEMODE_COMPLETE_DOM = 0x01;
90 // We have a DOM which we can serialize as text.
91 const SAVEMODE_COMPLETE_TEXT = 0x02;
93 function get_save_mode_from_content_type (content_type) {
94 var mode = SAVEMODE_FILEONLY;
95 switch (content_type) {
97 case "application/xhtml+xml":
98 mode |= SAVEMODE_COMPLETE_TEXT;
101 case "application/xml":
102 mode |= SAVEMODE_COMPLETE_DOM;
108 define_keywords("$use_cache", "$buffer", "$wrap_column", "$prepare_download");
109 function save_document_as_text (document, output_file) {
110 keywords(arguments, $use_cache = true, $wrap_column = 80);
112 var mode = get_save_mode_from_content_type(document.contentType);
113 if (!(mode & SAVEMODE_COMPLETE_TEXT))
114 throw interactive_error("Document cannot be saved as text.");
116 var use_cache = arguments.$use_cache;
118 var prepare_download = arguments.$prepare_download;
120 var buffer = arguments.$buffer;
122 var uri = document.documentURIObject;
124 var file_uri = make_uri(output_file);
126 var persist = Cc["@mozilla.org/embedding/browser/nsWebBrowserPersist;1"]
127 .createInstance(Ci.nsIWebBrowserPersist);
129 persist.persistFlags =
130 Ci.nsIWebBrowserPersist.PERSIST_FLAGS_REPLACE_EXISTING_FILES |
131 Ci.nsIWebBrowserPersist.PERSIST_FLAGS_AUTODETECT_APPLY_CONVERSION |
132 Ci.nsIWebBrowserPersist.PERSIST_FLAGS_CLEANUP_ON_FAILURE;
135 Ci.nsIWebBrowserPersist.ENCODE_FLAGS_FORMATTED |
136 Ci.nsIWebBrowserPersist.ENCODE_FLAGS_ABSOLUTE_LINKS |
137 Ci.nsIWebBrowserPersist.ENCODE_FLAGS_NOFRAMES_CONTENT;
140 persist.persistFlags |= Ci.nsIWebBrowserPersist.PERSIST_FLAGS_FROM_CACHE;
142 persist.persistFlags |= Ci.nsIWebBrowserPersist.PERSIST_FLAGS_BYPASS_CACHE;
144 var info = register_download(buffer, uri);
145 if (prepare_download)
146 prepare_download(info);
148 var tr = Cc["@mozilla.org/transfer;1"].createInstance(Ci.nsITransfer);
149 tr.init(uri, file_uri, output_file.leafName,
150 mime_info_from_mime_type("text/plain"),
151 null /* start time */,
152 null /* temp file */,
154 persist.progressListener = tr;
155 persist.saveDocument(document, file_uri, null /* data path */,
156 "text/plain", encoding_flags,
157 arguments.$wrap_column);
161 define_keywords("$use_cache", "$buffer", "$prepare_download");
162 function save_document_complete (document, output_file, output_dir) {
165 var mime_type = document.contentType;
167 var mode = get_save_mode_from_content_type(mime_type);
168 if (!(mode & SAVEMODE_COMPLETE_DOM))
169 throw interactive_error("Complete document cannot be saved.");
171 var use_cache = arguments.$use_cache;
173 var buffer = arguments.$buffer;
175 var prepare_download = arguments.$prepare_download;
177 var uri = document.documentURIObject;
179 var file_uri = make_uri(output_file);
181 var persist = Cc["@mozilla.org/embedding/browser/nsWebBrowserPersist;1"]
182 .createInstance(Ci.nsIWebBrowserPersist);
184 persist.persistFlags =
185 Ci.nsIWebBrowserPersist.PERSIST_FLAGS_REPLACE_EXISTING_FILES |
186 Ci.nsIWebBrowserPersist.PERSIST_FLAGS_AUTODETECT_APPLY_CONVERSION |
187 Ci.nsIWebBrowserPersist.PERSIST_FLAGS_CLEANUP_ON_FAILURE;
190 Ci.nsIWebBrowserPersist.ENCODE_FLAGS_BASIC_ENTITIES;
193 persist.persistFlags |= Ci.nsIWebBrowserPersist.PERSIST_FLAGS_FROM_CACHE;
195 persist.persistFlags |= Ci.nsIWebBrowserPersist.PERSIST_FLAGS_BYPASS_CACHE;
198 var info = register_download(buffer, uri);
199 if (prepare_download)
200 prepare_download(info);
202 var tr = Cc["@mozilla.org/transfer;1"].createInstance(Ci.nsITransfer);
203 tr.init(uri, file_uri, output_file.leafName,
204 mime_info_from_mime_type(mime_type),
205 null /* start time */,
206 null /* temp file */,
208 persist.progressListener = tr;
209 persist.saveDocument(document, file_uri, output_dir /* data path */,
210 mime_type, encoding_flags,
215 function download_failed_error () {
216 var e = new Error("Download failed");
217 e.__proto__ = download_failed_error.prototype;
220 download_failed_error.prototype.__proto__ = Error.prototype;
222 /* Returns an array of two elements: the first element is the
223 * nsILocalFile object, the second element is a boolean indicating
224 * whether the file is temporary and should be deleted when the caller is
227 define_keywords("$action", "$shell_command", "$shell_command_cwd", "$buffer", "$use_cache");
228 function download_as_temporary (lspec) {
229 keywords(arguments, $use_cache = true);
231 var action_description = arguments.$action;
232 var shell_command = arguments.$shell_command;
233 var shell_command_cwd = arguments.$shell_command_cwd;
235 var uri = load_spec_uri(lspec);
236 // If it is local file, there is no need to download it
237 if (uri.scheme == "file") {
238 let file = uri.QueryInterface(Ci.nsIFileURL).file;
240 yield co_return([file, false /* not temporary */]);
243 var file = get_temporary_file(suggest_file_name(lspec));
245 var cc = yield CONTINUATION;
247 function handle_state_change (info) {
248 var state = info.state;
250 case DOWNLOAD_CANCELED:
251 case DOWNLOAD_FAILED:
252 info.remove(); // ensure that the download cannot be retried later
254 // Delete the temporary file
255 file.remove(false /*non-recursive*/);
257 cc.throw(download_failed_error());
259 case DOWNLOAD_FINISHED:
260 cc([file, true /* temporary */]);
265 save_uri(lspec, file,
266 $use_cache = arguments.$use_cache,
267 $buffer = arguments.$buffer,
268 $prepare_download = function (info) {
269 if (action_description != null) {
270 info.action_description = action_description;
271 info.temporary_status = DOWNLOAD_TEMPORARY_FOR_ACTION;
272 } else if (shell_command != null) {
273 info.set_shell_command(shell_command, shell_command_cwd);
274 info.temporary_status = DOWNLOAD_TEMPORARY_FOR_COMMAND;
276 add_hook.call(info, "download_state_change_hook", handle_state_change);
279 var result = yield SUSPEND;
280 yield co_return(result);