Bug 1514364 - Add appendRawFilter to nsIFilePicker to expose actual accept filters to GV for onFilePrompt. r=snorp, smaug

This commit is contained in:
Dylan Roeh 2019-01-24 10:21:05 -06:00
parent de357e4379
commit 44dab4cd06
10 changed files with 37 additions and 56 deletions

View File

@ -6936,6 +6936,10 @@ void HTMLInputElement::SetFilePickerFiltersFromAccept(
continue;
}
// At this point we're sure the token represents a valid filter, so pass
// it directly as a raw filter.
filePicker->AppendRawFilter(token);
// If we arrived here, that means we have a valid filter: let's create it
// and add it to our list, if no similar filter is already present
nsFilePickerFilter filter;

View File

@ -241,6 +241,7 @@ mozilla::ipc::IPCResult FilePickerParent::RecvOpen(
const nsString& aDefaultFile, const nsString& aDefaultExtension,
InfallibleTArray<nsString>&& aFilters,
InfallibleTArray<nsString>&& aFilterNames,
InfallibleTArray<nsString>&& aRawFilters,
const nsString& aDisplayDirectory, const nsString& aDisplaySpecialDirectory,
const nsString& aOkButtonLabel) {
if (!CreateFilePicker()) {
@ -254,6 +255,10 @@ mozilla::ipc::IPCResult FilePickerParent::RecvOpen(
mFilePicker->AppendFilter(aFilterNames[i], aFilters[i]);
}
for (uint32_t i = 0; i < aRawFilters.Length(); ++i) {
mFilePicker->AppendRawFilter(aRawFilters[i]);
}
mFilePicker->SetDefaultString(aDefaultFile);
mFilePicker->SetDefaultExtension(aDefaultExtension);
mFilePicker->SetFilterIndex(aSelectedType);

View File

@ -42,6 +42,7 @@ class FilePickerParent : public PFilePickerParent {
const nsString& aDefaultFile, const nsString& aDefaultExtension,
InfallibleTArray<nsString>&& aFilters,
InfallibleTArray<nsString>&& aFilterNames,
InfallibleTArray<nsString>&& aRawFilters,
const nsString& aDisplayDirectory,
const nsString& aDisplaySpecialDirectory,
const nsString& aOkButtonLabel) override;

View File

@ -41,8 +41,8 @@ protocol PFilePicker
parent:
async Open(int16_t selectedType, bool addToRecentDocs, nsString defaultFile,
nsString defaultExtension, nsString[] filters, nsString[] filterNames,
nsString displayDirectory, nsString displaySpecialDirectory,
nsString okButtonLabel);
nsString[] rawFilters, nsString displayDirectory,
nsString displaySpecialDirectory, nsString okButtonLabel);
child:
async __delete__(MaybeInputData data, int16_t result);

View File

@ -812,44 +812,14 @@ FilePickerDelegate.prototype = {
};
this._mode = aMode;
this._mimeTypes = [];
this._extensions = [];
},
get mode() {
return this._mode;
},
appendFilters: function(aFilterMask) {
if (aFilterMask & Ci.nsIFilePicker.filterAll) {
this._mimeTypes.push("*/*");
}
if (aFilterMask & Ci.nsIFilePicker.filterAudio) {
this._mimeTypes.push("audio/*");
}
if (aFilterMask & Ci.nsIFilePicker.filterImages) {
this._mimeTypes.push("image/*");
}
if (aFilterMask & Ci.nsIFilePicker.filterVideo) {
this._mimeTypes.push("video/*");
}
if (aFilterMask & Ci.nsIFilePicker.filterHTML) {
this._mimeTypes.push("text/html");
}
if (aFilterMask & Ci.nsIFilePicker.filterText) {
this._mimeTypes.push("text/plain");
}
if (aFilterMask & Ci.nsIFilePicker.filterXML) {
this._mimeTypes.push("text/xml");
}
if (aFilterMask & Ci.nsIFilePicker.filterXUL) {
this._mimeTypes.push("application/vnd.mozilla.xul+xml");
}
},
appendFilter: function(aTitle, aFilter) {
// Only include filter that specify extensions (i.e. exclude generic ones like "*").
let filters = aFilter.split(/[\s,;]+/).filter(filter => filter.includes("."));
Array.prototype.push.apply(this._extensions, filters);
appendRawFilter: function(aFilter) {
this._mimeTypes.push(aFilter);
},
show: function() {
@ -858,9 +828,6 @@ FilePickerDelegate.prototype = {
open: function(aFilePickerShownCallback) {
this._msg.mimeTypes = this._mimeTypes;
if (this._extensions.length) {
this._msg.extensions = this._extensions;
}
this._prompt.asyncShowPrompt(this._msg, result => {
// OK: result
// Cancel: !result

View File

@ -2352,20 +2352,6 @@ public class GeckoSession implements Parcelable {
return;
}
String[] mimeTypes = message.getStringArray("mimeTypes");
final String[] extensions = message.getStringArray("extension");
if (extensions != null) {
final ArrayList<String> combined =
new ArrayList<>(mimeTypes.length + extensions.length);
combined.addAll(Arrays.asList(mimeTypes));
for (final String extension : extensions) {
final String mimeType =
URLConnection.guessContentTypeFromName(extension);
if (mimeType != null) {
combined.add(mimeType);
}
}
mimeTypes = combined.toArray(new String[combined.size()]);
}
delegate.onFilePrompt(session, title, intMode, mimeTypes, cb);
break;
}
@ -3609,9 +3595,10 @@ public class GeckoSession implements Parcelable {
* @param session GeckoSession that triggered the prompt
* @param title Title for the prompt dialog.
* @param type One of FILE_TYPE_* indicating the prompt type.
* @param mimeTypes Array of permissible MIME types for the selected files, in
* the form "type/subtype", where "type" and/or "subtype" can be
* "*" to indicate any value.
* @param mimeTypes Array of permissible MIME types or extensions for the selected
* files. MIME types are of the form "type/subtype", where "type"
* and/or "subtype" can be "*" to indicate any value. Extensions
* are of the form ".ext".
* @param callback Callback interface.
*/
@UiThread

View File

@ -232,6 +232,11 @@ nsBaseFilePicker::AppendFilters(int32_t aFilterMask) {
return NS_OK;
}
NS_IMETHODIMP nsBaseFilePicker::AppendRawFilter(const nsAString& aFilter) {
mRawFilters.AppendElement(aFilter);
return NS_OK;
}
// Set the filter index
NS_IMETHODIMP nsBaseFilePicker::GetFilterIndex(int32_t* aFilterIndex) {
*aFilterIndex = 0;

View File

@ -29,6 +29,7 @@ class nsBaseFilePicker : public nsIFilePicker {
NS_IMETHOD Open(nsIFilePickerShownCallback* aCallback) override;
NS_IMETHOD AppendFilters(int32_t filterMask) override;
NS_IMETHOD AppendRawFilter(const nsAString& aFilter) override;
NS_IMETHOD GetFilterIndex(int32_t* aFilterIndex) override;
NS_IMETHOD SetFilterIndex(int32_t aFilterIndex) override;
NS_IMETHOD GetFiles(nsISimpleEnumerator** aFiles) override;
@ -58,6 +59,7 @@ class nsBaseFilePicker : public nsIFilePicker {
nsCOMPtr<nsPIDOMWindowOuter> mParent;
int16_t mMode;
nsString mOkButtonLabel;
InfallibleTArray<nsString> mRawFilters;
};
#endif // nsBaseFilePicker_h__

View File

@ -125,8 +125,8 @@ nsFilePickerProxy::Open(nsIFilePickerShownCallback* aCallback) {
}
SendOpen(mSelectedType, mAddToRecentDocs, mDefault, mDefaultExtension,
mFilters, mFilterNames, displayDirectory, mDisplaySpecialDirectory,
mOkButtonLabel);
mFilters, mFilterNames, mRawFilters, displayDirectory,
mDisplaySpecialDirectory, mOkButtonLabel);
return NS_OK;
}

View File

@ -88,6 +88,16 @@ interface nsIFilePicker : nsISupports
void appendFilter(in AString title,
in AString filter);
/**
* Add a raw filter (eg, add a MIME type without transforming it to a list of
* extensions).
*
* @param filter a filter taken directly from the accept attribute
* without processing
*
*/
void appendRawFilter(in AString filter);
/**
* The filename that should be suggested to the user as a default. This should
* include the extension.