Revamped and simplified load_spec infrastructure
[conkeror.git] / modules / load-spec.js
blob00155d3e20fd4e9851c3b42679e59996e43303e4
2 /**
3  * A load_spec has the following properties:
4  *
5  * Name:        Required?   Type:           Description:
6  * -----        ---------   -----           ------------
7  * uri          required    string          Specifies the URI of the target.
8  *
9  * document     optional    nsIDOMDocument  Specifies a document corresponding to the target.
10  *                                          Can also provide a default value for the mime_type property,
11  *                                          the title property, and the source_frame property.
12  *
13  * flags        optional    number          Specifies flags to pass to nsIWebNavigation.loadURI
14  *
15  * cache_key    optional    nsISHEntry      Specifies a key for accessing the target from the cache.
16  *
17  * referrer     optional    nsIURI          Specifies the referrer URI to use to access the target.
18  *
19  * post_data    optional    nsIInputStream  Specifies POST data to use to access the target.
20  *
21  * mime_type    optional    string          Specifies the MIME type of the target.
22  *
23  * title        optional    string          Specifies a title/description text associated with the target.
24  *
25  * source_frame optional    nsIDOMWindow    Specifies the frame from which the link to the target was "obtained".
26  *                                          Can provide a default value for referrer if document is not specified.
27  *
28  * filename     optional    string          Specifies a default filename to use to save the target.
29  *
30  * filename_extension
31  *              optional    string          Specifies a default filename extension to use to save the target.
32  */
33 function load_spec(x) { x.__proto__ = load_spec.prototype; return x; }
35 function is_load_spec(x) {
36     return (typeof(x) == "string") || (x instanceof load_spec);
39 function load_spec_document(x) {
40     return x.document;
43 function load_spec_title(x) {
44     if (x.title)
45         return x.title;
46     if (x.document)
47         return x.document.title;
48     return null;
51 function load_spec_mime_type(x) {
52     if (typeof(x) == "object") {
53         if (x.mime_type)
54             return x.mime_type;
55         if (x.document)
56             return x.document.contentType || "application/octet-stream";
57     }
58     return mime_type_from_uri(load_spec_uri(x));
61 function load_spec_filename(x) {
62     return x.filename;
65 function load_spec_filename_extension(x) {
66     return x.filename_extension;
69 function get_web_navigation_for_frame(frame) {
70     var ifr = frame.QueryInterface(Ci.nsIInterfaceRequestor);
71     return ifr.getInterface(Ci.nsIWebNavigation);
74 function get_SHEntry_for_document(doc)
76     try
77     {
78         var frame = doc.defaultView;
79         var webNav = get_web_navigation_for_frame(frame);
80         var pageLoader = webNav.QueryInterface(Ci.nsIWebPageDescriptor);
81         var desc = pageLoader.currentDescriptor.QueryInterface(Ci.nsISHEntry);
82         return desc;
83     } catch (e) { return null; }
86 function load_spec_set_properties_from_sh_entry(x) {
87     var sh_entry = get_SHEntry_for_document(x.document);
88     if (sh_entry != null) {
89         x.cache_key = sh_entry;
90         x.referrer = sh_entry.referrerURI;
91         x.post_data = sh_entry.postData;
92     }
95 function load_spec_referrer(x) {
96     if (x.referrer)
97         return x.referrer;
98     if (x.document) {
99         load_spec_set_properties_from_sh_entry(x);
100         return x.referrer;
101     }
102     if (x.source_frame) {
103         x.referrer = x.source_frame.document.documentURIObject;
104         return x.referrer;
105     }
106     return null;
109 function load_spec_post_data(x) {
110     if (x.post_data)
111         return x.post_data;
112     if (x.document) {
113         load_spec_set_properties_from_sh_entry(x);
114         return x.post_data;
115     }
116     return null;
119 function load_spec_cache_key(x) {
120     if (x.cache_key)
121         return x.cache_key;
122     if (x.document) {
123         load_spec_set_properties_from_sh_entry(x);
124         return x.cache_key;
125     }
126     return null;
129 function load_spec_source_frame(x) {
130     if (x.source_frame)
131         return x.source_frame;
132     if (x.document)
133         return x.document.defaultView;
134     return null;
137 function load_spec_uri_string(x) {
138     if (typeof(x) == "string")
139         return x;
140     if (x.uri)
141         return x.uri;
142     if (x.document)
143         return x.document.documentURI;
144     return null;
147 function load_spec_uri(x) {
148     if (x.document)
149         return x.document.documentURIObject;
150     return make_uri(load_spec_uri_string(x));
153 function load_spec_flags(x) {
154     return x.load_spec_flags;
157 function load_spec_mime_info(x) {
158     var type = load_spec_mime_type(x);
159     return mime_info_from_mime_type(type);
162 function load_spec_default_shell_command(x) {
163     var mime_type = load_spec_mime_type(x);
164     return get_external_handler_for_mime_type(mime_type);
167 /* Target can be either a content_buffer or an nsIWebNavigation */
168 function apply_load_spec(target, spec) {
169     var uri = load_spec_uri_string(spec);
170     var flags = load_spec_flags(spec);
171     var referrer = load_spec_referrer(spec);
172     var post_data = load_spec_post_data(spec);
174     if (flags == null)
175         flags = Ci.nsIWebNavigation.LOAD_FLAGS_NONE;
177     if (target instanceof content_buffer) {
178         target._display_URI = uri;
179         target = target.web_navigation;
180         //buffer_description_change_hook.run(target);
181     }
182     target.loadURI(uri, flags, referrer, post_data, null /* headers */);