1 /* -*- Mode: IDL; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include
"nsISupports.idl"
14 * The MIME service is responsible for mapping file extensions to MIME-types
15 * (see RFC 2045). It also provides access to nsIMIMEInfo interfaces and
16 * acts as a general convenience wrapper of nsIMIMEInfo interfaces.
18 * The MIME service maintains a database with a <b>one</b> MIME type <b>to many</b>
19 * file extensions rule. Adding the same file extension to multiple MIME types
20 * is illegal and behavior is undefined.
24 [scriptable
, main_process_scriptable_only
, uuid(5b3675a1
-02db
-4f8f
-a560
-b34736635f47
)]
25 interface nsIMIMEService
: nsISupports
{
27 * Retrieves an nsIMIMEInfo using both the extension
28 * and the type of a file. The type is given preference
29 * during the lookup. One of aMIMEType and aFileExt
30 * can be an empty string. At least one of aMIMEType and aFileExt
33 nsIMIMEInfo getFromTypeAndExtension
(in ACString aMIMEType
, in AUTF8String aFileExt
);
36 * Retrieves a ACString representation of the MIME type
37 * associated with this file extension.
39 * @param A file extension (excluding the dot ('.')).
40 * @return The MIME type, if any.
42 ACString getTypeFromExtension
(in AUTF8String aFileExt
);
45 * Retrieves a ACString representation of the MIME type
46 * associated with this URI. The association is purely
47 * file extension to MIME type based. No attempt to determine
48 * the type via server headers or byte scanning is made.
50 * @param The URI the user wants MIME info on.
51 * @return The MIME type, if any.
53 ACString getTypeFromURI
(in nsIURI aURI
);
56 * Retrieves a ACString representation of the MIME type
57 * associated with this file extension. Only the default
58 * builtin list is examined. Unless you need a restricted
59 * set use getTypeFromURI.
61 * @param The URI the user wants MIME info on.
62 * @return The MIME type, if any.
64 ACString getDefaultTypeFromURI
(in nsIURI aURI
);
67 ACString getTypeFromFile
(in nsIFile aFile
);
70 * Given a Type/Extension combination, returns the default extension
71 * for this type. This may be identical to the passed-in extension.
73 * @param aMIMEType The Type to get information on. Must not be empty.
74 * @param aFileExt File Extension. Can be empty.
76 AUTF8String getPrimaryExtension
(in ACString aMIMEType
, in AUTF8String aFileExt
);
79 * Returns an nsIMIMEInfo for the provided MIME type and extension
80 * obtained from an OS lookup. If no handler is found for the type and
81 * extension, returns a generic nsIMIMEInfo object. The MIME type and
82 * extension can be the empty string. When the type and extension don't
83 * map to the same handler, the semantics/resolution are platform
84 * specific. See the platform implementations for details.
86 * @param aType The MIME type to get handler information for.
87 * @param aFileExtension The filename extension to use either alone
88 * or with the MIME type to get handler information
90 * @param [out] aFound Out param indicating whether a MIMEInfo could
91 * be found for the provided type and/or extension.
92 * Set to false when neither extension nor the MIME
93 * type are mapped to a handler.
94 * @return A nsIMIMEInfo object. This function must return
95 * a MIMEInfo object if it can allocate one. The
96 * only justifiable reason for not returning one is
97 * an out-of-memory error.
99 nsIMIMEInfo getMIMEInfoFromOS
(in ACString aType
,
100 in ACString aFileExtension
,
104 * Update the mime info's default app information based on OS
106 * Note: normally called automatically by nsIMIMEInfo. If you find
107 * yourself needing to call this from elsewhere, file a bug instead.
109 void updateDefaultAppInfo
(in nsIMIMEInfo aMIMEInfo
);
112 * Default filename validation for getValidFileName and
113 * validateFileNameForSaving where other flags are not true.
114 * That is, the extension is modified to fit the content type,
115 * duplicate whitespace is collapsed, and long filenames are
116 * truncated. A valid content type must be supplied. See the
117 * description of getValidFileName for more details about how
118 * the flags are used.
120 const long VALIDATE_DEFAULT
= 0;
123 * If true, then the filename is only validated to ensure that it is
124 * acceptable for the file system. If false, then the extension is also
125 * checked to ensure that it is valid for the content type. If the
126 * extension is not valid, the filename is modified to have the proper
129 const long VALIDATE_SANITIZE_ONLY
= 1;
132 * Don't collapse strings of duplicate whitespace into a single string.
134 const long VALIDATE_DONT_COLLAPSE_WHITESPACE
= 2;
137 * Don't truncate long filenames.
139 const long VALIDATE_DONT_TRUNCATE
= 4;
142 * True to ignore the content type and guess the type from any existing
143 * extension instead. "application/octet-stream" is used as the default
144 * if there is no extension or there is no information available for
147 const long VALIDATE_GUESS_FROM_EXTENSION
= 8;
150 * If the filename is empty, return the empty filename
151 * without modification.
153 const long VALIDATE_ALLOW_EMPTY
= 16;
156 * Don't apply a default filename if the non-extension portion of the
159 const long VALIDATE_NO_DEFAULT_FILENAME
= 32;
162 * When the filename has an invalid extension, force the the filename to
163 * have a valid extension appended to the end of the filename when that
164 * extension would normally be ignored for the given content type. This
165 * primarily is used when saving pages to ensure that the html extension
166 * is applied over any extension that might have been generated from a
169 const long VALIDATE_FORCE_APPEND_EXTENSION
= 64;
172 * Don't modify filenames or extensions that might be invalid or dangerous
173 * on some platforms. If this flag is not used, these filenames will be
174 * modified so that the operating system does not treat them specially.
176 const long VALIDATE_ALLOW_INVALID_FILENAMES
= 128;
179 * Generate a valid filename from the channel that can be used to save
180 * the content of the channel to the local disk.
182 * The filename is determined from the content disposition, the filename
183 * of the uri, or a default filename. The following modifications are
185 * - If the VALIDATE_SANITIZE_ONLY flag is not specified, then the
186 * extension of the filename is modified to suit the supplied content type.
187 * - Path separators (typically / and \) are replaced by underscores (_)
188 * - Characters that are not valid or would be confusing in filenames are
189 * replaced by spaces (*, :, etc)
190 * - Bidi related marks are replaced by underscores (_)
191 * - Whitespace and periods are removed from the beginning and end.
192 * - Unless VALIDATE_DONT_COLLAPSE_WHITESPACE is specified, multiple
193 * consecutive whitespace characters are collapsed to a single space
194 * character, either ' ' or an ideographic space 0x3000 if present.
195 * - Unless VALIDATE_DONT_TRUNCATE is specified, the filename is truncated
196 * to a maximum length, preserving the extension if possible.
197 * - Some filenames and extensions are invalid on certain platforms.
198 * These are replaced if possible unless VALIDATE_ALLOW_INVALID_FILENAMES
201 * If the VALIDATE_NO_DEFAULT_FILENAME flag is not specified, and after the
202 * rules above are applied, the resulting filename is empty, a default
205 * If the VALIDATE_ALLOW_EMPTY flag is specified, an empty string may be
206 * returned only if the filename could not be determined or was blank.
208 * If either the VALIDATE_SANITIZE_ONLY or VALIDATE_GUESS_FROM_EXTENSION flags
209 * are specified, then the content type may be empty. Otherwise, the type must
212 * The aOriginalURI would be specified if the channel is for a local file but
213 * it was originally sourced from a different uri.
215 * When saving an image, use validateFileNameForSaving instead and
216 * pass the result of imgIRequest::GetFileName() as the filename to
219 * @param aChannel The channel of the content to save.
220 * @param aType The MIME type to use, which would usually be the
221 * same as the content type of the channel.
222 * @param aOriginalURL the source url of the file, but may be null.
223 * @param aFlags one or more of the flags above.
224 * @returns The resulting filename.
226 AString getValidFileName
(in nsIChannel aChannel
,
228 in nsIURI aOriginalURI
,
229 in unsigned long aFlags
);
232 * Similar to getValidFileName, but used when a specific filename needs
233 * to be validated. The filename is modified as needed based on the
234 * content type in the same manner as getValidFileName.
236 * If the filename came from a uri, it should not be escaped, that is,
237 * any needed unescaping of the filename should happen before calling
240 * @param aType The MIME type to use.
241 * @param aFlags one or more of the flags above.
242 * @param aFileName The filename to validate.
243 * @returns The validated filename.
245 AString validateFileNameForSaving
(in AString aFileName
,
247 in unsigned long aFlags
);