save.js: pass extra arg to all calls to nsITransfer.init
[conkeror.git] / modules / save.js
blobce9e869a292ba39ab8fe218f49e66466f005cd07
1 /**
2  * (C) Copyright 2004-2007 Shawn Betts
3  * (C) Copyright 2007 John J. Foerch
4  * (C) Copyright 2007-2008 Jeremy Maitin-Shepard
5  *
6  * Use, modification, and distribution are subject to the terms specified in the
7  * COPYING file.
8 **/
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);
34     }
35     if (temp_file)
36         download_file = temp_file;
38     var cache_key = null;
39     var uri = load_spec_uri(lspec);
40     var referrer_uri = load_spec_referrer(lspec);
41     var post_data = load_spec_post_data(lspec);
42     if (use_cache)
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;
55     if (use_cache)
56         persist.persistFlags |= Ci.nsIWebBrowserPersist.PERSIST_FLAGS_FROM_CACHE;
57     else
58         persist.persistFlags |= Ci.nsIWebBrowserPersist.PERSIST_FLAGS_BYPASS_CACHE;
60     var info = register_download(buffer, uri, download_file);
61     if (temp_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;
67             }
68         });
69     }
70     if (prepare_download)
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 */,
77             null /* temp file */,
78             persist, false);
79     persist.progressListener = tr;
80     persist.saveURI(uri, cache_key, referrer_uri, post_data, null /* no extra headers */, file_uri);
82     return info;
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) {
96     case "text/html":
97     case "application/xhtml+xml":
98         mode |= SAVEMODE_COMPLETE_TEXT;
99         // Fall through
100     case "text/xml":
101     case "application/xml":
102         mode |= SAVEMODE_COMPLETE_DOM;
103         break;
104     }
105     return mode;
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;
134     var encoding_flags =
135         Ci.nsIWebBrowserPersist.ENCODE_FLAGS_FORMATTED |
136         Ci.nsIWebBrowserPersist.ENCODE_FLAGS_ABSOLUTE_LINKS |
137         Ci.nsIWebBrowserPersist.ENCODE_FLAGS_NOFRAMES_CONTENT;
139     if (use_cache)
140         persist.persistFlags |= Ci.nsIWebBrowserPersist.PERSIST_FLAGS_FROM_CACHE;
141     else
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 */,
153             persist, false);
154     persist.progressListener = tr;
155     persist.saveDocument(document, file_uri, null /* data path */,
156                          "text/plain", encoding_flags,
157                          arguments.$wrap_column);
158     return info;
161 define_keywords("$use_cache", "$buffer", "$prepare_download");
162 function save_document_complete (document, output_file, output_dir) {
163     keywords(arguments);
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;
189     var encoding_flags =
190         Ci.nsIWebBrowserPersist.ENCODE_FLAGS_BASIC_ENTITIES;
192     if (use_cache)
193         persist.persistFlags |= Ci.nsIWebBrowserPersist.PERSIST_FLAGS_FROM_CACHE;
194     else
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 */,
207             persist, false);
208     persist.progressListener = tr;
209     persist.saveDocument(document, file_uri, output_dir /* data path */,
210                          mime_type, encoding_flags,
211                          0);
212     return info;
215 function download_failed_error () {
216     var e = new Error("Download failed");
217     e.__proto__ = download_failed_error.prototype;
218     return e;
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
225  * done with it.
226  */
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 */]);
241     }
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;
249         switch (state) {
250         case DOWNLOAD_CANCELED:
251         case DOWNLOAD_FAILED:
252             info.remove(); // ensure that the download cannot be retried later
253             try {
254                 // Delete the temporary file
255                 file.remove(false /*non-recursive*/);
256             } catch (e) {}
257             cc.throw(download_failed_error());
258             break;
259         case DOWNLOAD_FINISHED:
260             cc([file, true /* temporary */]);
261             break;
262         }
263     }
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;
275                  }
276                  add_hook.call(info, "download_state_change_hook", handle_state_change);
277              });
279     var result = yield SUSPEND;
280     yield co_return(result);
283 provide("save");