global-overlay-keymap: fix to work with new keyboard setup stuff
[conkeror.git] / modules / load-spec.js
blobbd92157dab1e10489d3fe5f1fa11aed7d3de3584
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  * suggest_filename_from_uri
34  *              optional    boolean         Specifies whether to attempt to generate a filename from the URI.
35  *                                          Defaults to true.
36  */
37 function load_spec(x) { x.__proto__ = load_spec.prototype; return x; }
39 function is_load_spec(x) {
40     return (typeof(x) == "string") || (x instanceof load_spec);
43 function load_spec_document(x) {
44     return x.document;
47 function load_spec_title(x) {
48     if (x.title)
49         return x.title;
50     if (x.document)
51         return x.document.title;
52     return null;
55 function load_spec_mime_type(x) {
56     if (typeof(x) == "object") {
57         if (x.mime_type)
58             return x.mime_type;
59         if (x.document)
60             return x.document.contentType || "application/octet-stream";
61     }
62     return mime_type_from_uri(load_spec_uri(x));
65 function load_spec_filename(x) {
66     return x.filename;
69 function load_spec_filename_extension(x) {
70     return x.filename_extension;
73 function get_web_navigation_for_frame(frame) {
74     var ifr = frame.QueryInterface(Ci.nsIInterfaceRequestor);
75     return ifr.getInterface(Ci.nsIWebNavigation);
78 function get_SHEntry_for_document(doc)
80     try
81     {
82         var frame = doc.defaultView;
83         var webNav = get_web_navigation_for_frame(frame);
84         var pageLoader = webNav.QueryInterface(Ci.nsIWebPageDescriptor);
85         var desc = pageLoader.currentDescriptor.QueryInterface(Ci.nsISHEntry);
86         return desc;
87     } catch (e) { return null; }
90 function load_spec_set_properties_from_sh_entry(x) {
91     var sh_entry = get_SHEntry_for_document(x.document);
92     if (sh_entry != null) {
93         x.cache_key = sh_entry;
94         x.referrer = sh_entry.referrerURI;
95         x.post_data = sh_entry.postData;
96     }
99 function load_spec_referrer(x) {
100     if (x.referrer)
101         return x.referrer;
102     if (x.document) {
103         load_spec_set_properties_from_sh_entry(x);
104         return x.referrer;
105     }
106     if (x.source_frame) {
107         x.referrer = x.source_frame.document.documentURIObject;
108         return x.referrer;
109     }
110     return null;
113 function load_spec_post_data(x) {
114     if (x.post_data)
115         return x.post_data;
116     if (x.document) {
117         load_spec_set_properties_from_sh_entry(x);
118         return x.post_data;
119     }
120     return null;
123 function load_spec_cache_key(x) {
124     if (x.cache_key)
125         return x.cache_key;
126     if (x.document) {
127         load_spec_set_properties_from_sh_entry(x);
128         return x.cache_key;
129     }
130     return null;
133 function load_spec_source_frame(x) {
134     if (x.source_frame)
135         return x.source_frame;
136     if (x.document)
137         return x.document.defaultView;
138     return null;
141 function load_spec_uri_string(x) {
142     if (typeof(x) == "string")
143         return x;
144     if (x.uri)
145         return x.uri;
146     if (x.document)
147         return x.document.documentURI;
148     return null;
151 function load_spec_uri(x) {
152     if (x.document)
153         return x.document.documentURIObject;
154     return make_uri(load_spec_uri_string(x));
157 function load_spec_flags(x) {
158     return x.load_spec_flags;
161 function load_spec_mime_info(x) {
162     var type = load_spec_mime_type(x);
163     return mime_info_from_mime_type(type);
166 function load_spec_default_shell_command(x) {
167     var mime_type = load_spec_mime_type(x);
168     return get_external_handler_for_mime_type(mime_type);
171 /* Target can be either a content_buffer or an nsIWebNavigation */
172 function apply_load_spec(target, spec) {
173     var uri = load_spec_uri_string(spec);
174     var flags = load_spec_flags(spec);
175     var referrer = load_spec_referrer(spec);
176     var post_data = load_spec_post_data(spec);
178     if (flags == null)
179         flags = Ci.nsIWebNavigation.LOAD_FLAGS_NONE;
181     if (target instanceof content_buffer) {
182         target._display_URI = uri;
183         target = target.web_navigation;
184         //buffer_description_change_hook.run(target);
185     }
186     target.loadURI(uri, flags, referrer, post_data, null /* headers */);