Bug 826185 - Make filter constructed from accept attribute the default one unless unknown mime types are found. r=smaug
authorArnaud Bienner <arnaud.bienner@gmail.com>
Wed, 29 Oct 2014 00:32:28 +0100
changeset 213900 23c0b0d9d09a83e0ecea7622daf52fb7386a0a59
parent 213899 221ca54f4df091b39f197f6805f4bfbd2d7af893
child 213901 d83bcbd7e83c231e12a1d180a5163a7c5658b05f
push id27768
push userkwierso@gmail.com
push dateWed, 05 Nov 2014 02:19:03 +0000
treeherdermozilla-central@a1823d3c7365 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssmaug
bugs826185
milestone36.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 826185 - Make filter constructed from accept attribute the default one unless unknown mime types are found. r=smaug
dom/html/HTMLInputElement.cpp
dom/html/HTMLInputElement.h
dom/html/test/forms/test_input_file_picker.html
--- a/dom/html/HTMLInputElement.cpp
+++ b/dom/html/HTMLInputElement.cpp
@@ -7264,16 +7264,18 @@ HTMLInputElement::SetFilePickerFiltersFr
   nsAutoString accept;
   GetAttr(kNameSpaceID_None, nsGkAtoms::accept, accept);
 
   HTMLSplitOnSpacesTokenizer tokenizer(accept, ',');
 
   nsTArray<nsFilePickerFilter> filters;
   nsString allExtensionsList;
 
+  bool allFiltersAreValid = true;
+
   // Retrieve all filters
   while (tokenizer.hasMoreTokens()) {
     const nsDependentSubstring& token = tokenizer.nextToken();
 
     if (token.IsEmpty()) {
       continue;
     }
 
@@ -7297,16 +7299,17 @@ HTMLInputElement::SetFilePickerFiltersFr
     } else {
       //... if no image/audio/video filter is found, check mime types filters
       nsCOMPtr<nsIMIMEInfo> mimeInfo;
       if (NS_FAILED(mimeService->GetFromTypeAndExtension(
                       NS_ConvertUTF16toUTF8(token),
                       EmptyCString(), // No extension
                       getter_AddRefs(mimeInfo))) ||
           !mimeInfo) {
+        allFiltersAreValid =  false;
         continue;
       }
 
       // Get a name for the filter: first try the description, then the mime type
       // name if there is no description
       mimeInfo->GetDescription(filterName);
       if (filterName.IsEmpty()) {
         nsCString mimeTypeName;
@@ -7329,16 +7332,17 @@ HTMLInputElement::SetFilePickerFiltersFr
         }
         extensionListStr += NS_LITERAL_STRING("*.") +
                             NS_ConvertUTF8toUTF16(extension);
       }
     }
 
     if (!filterMask && (extensionListStr.IsEmpty() || filterName.IsEmpty())) {
       // No valid filter found
+      allFiltersAreValid = false;
       continue;
     }
 
     // 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;
     if (filterMask) {
       filter = nsFilePickerFilter(filterMask);
@@ -7358,31 +7362,29 @@ HTMLInputElement::SetFilePickerFiltersFr
   // Add "All Supported Types" filter
   if (filters.Length() > 1) {
     nsXPIDLString title;
     nsContentUtils::GetLocalizedString(nsContentUtils::eFORMS_PROPERTIES,
                                        "AllSupportedTypes", title);
     filePicker->AppendFilter(title, allExtensionsList);
   }
 
-  // Add each filter, and check if all filters are trusted
-  bool allFilterAreTrusted = true;
+  // Add each filter
   for (uint32_t i = 0; i < filters.Length(); ++i) {
     const nsFilePickerFilter& filter = filters[i];
     if (filter.mFilterMask) {
       filePicker->AppendFilters(filter.mFilterMask);
     } else {
       filePicker->AppendFilter(filter.mTitle, filter.mFilter);
     }
-    allFilterAreTrusted &= filter.mIsTrusted;
-  }
-
-  // If all filters are trusted, select the first filter as default;
+  }
+
+  // If all filters are known/valid, select the first filter as default;
   // otherwise filterAll will remain the default filter
-  if (filters.Length() >= 1 && allFilterAreTrusted) {
+  if (filters.Length() >= 1 && allFiltersAreValid) {
     // |filterAll| will always use index=0 so we need to set index=1 as the
     // current filter.
     filePicker->SetFilterIndex(1);
   }
 }
 
 Decimal
 HTMLInputElement::GetStepScaleFactor() const
--- a/dom/html/HTMLInputElement.h
+++ b/dom/html/HTMLInputElement.h
@@ -1356,56 +1356,45 @@ private:
   static bool MayFireChangeOnBlur(uint8_t aType) {
     return IsSingleLineTextControl(false, aType) ||
            aType == NS_FORM_INPUT_RANGE ||
            aType == NS_FORM_INPUT_NUMBER;
   }
 
   struct nsFilePickerFilter {
     nsFilePickerFilter()
-      : mFilterMask(0), mIsTrusted(false) {}
+      : mFilterMask(0) {}
 
     explicit nsFilePickerFilter(int32_t aFilterMask)
-      : mFilterMask(aFilterMask), mIsTrusted(true) {}
+      : mFilterMask(aFilterMask) {}
 
     nsFilePickerFilter(const nsString& aTitle,
-                       const nsString& aFilter,
-                       const bool aIsTrusted = false)
-      : mFilterMask(0), mTitle(aTitle), mFilter(aFilter), mIsTrusted(aIsTrusted) {}
+                       const nsString& aFilter)
+      : mFilterMask(0), mTitle(aTitle), mFilter(aFilter) {}
 
     nsFilePickerFilter(const nsFilePickerFilter& other) {
       mFilterMask = other.mFilterMask;
       mTitle = other.mTitle;
       mFilter = other.mFilter;
-      mIsTrusted = other.mIsTrusted;
     }
 
     bool operator== (const nsFilePickerFilter& other) const {
       if ((mFilter == other.mFilter) && (mFilterMask == other.mFilterMask)) {
-        NS_ASSERTION(mIsTrusted == other.mIsTrusted,
-                     "Filter with similar list of extensions and mask should"
-                     " have the same trusted flag value");
         return true;
       } else {
         return false;
       }
     }
     
     // Filter mask, using values defined in nsIFilePicker
     int32_t mFilterMask;
     // If mFilterMask is defined, mTitle and mFilter are useless and should be
     // ignored
     nsString mTitle;
     nsString mFilter;
-    // mIsTrusted is true if mime type comes from a "trusted" source (e.g. our
-    // hard-coded set).
-    // false means it may come from an "untrusted" source (e.g. OS mime types
-    // mapping, which can be different accross OS, user's personal configuration, ...)
-    // For now, only mask filters are considered to be "trusted".
-    bool mIsTrusted; 
   };
 
   class nsFilePickerShownCallback
     : public nsIFilePickerShownCallback
   {
     virtual ~nsFilePickerShownCallback()
     { }
 
--- a/dom/html/test/forms/test_input_file_picker.html
+++ b/dom/html/test/forms/test_input_file_picker.html
@@ -69,22 +69,22 @@ var videoExtensionList = "*.avi; *.divx;
 // [ element name | number of filters | extension list or filter mask | filter index ]
 var testData = [["a", 1, MockFilePicker.filterImages, 1],
                 ["b", 1, MockFilePicker.filterAudio, 1],
                 ["c", 1, MockFilePicker.filterVideo, 1],
                 ["d", 3, imageExtensionList + "; " + audioExtensionList, 1],
                 ["e", 3, imageExtensionList + "; " + videoExtensionList, 1],
                 ["f", 3, audioExtensionList + "; " + videoExtensionList, 1],
                 ["g", 4, imageExtensionList + "; " + audioExtensionList + "; " + videoExtensionList, 1],
-                ["h", 1, MockFilePicker.filterImages, 1],
-                ["i", 1, MockFilePicker.filterVideo, 1],
+                ["h", 1, MockFilePicker.filterImages, 0],
+                ["i", 1, MockFilePicker.filterVideo, 0],
                 ["j", 1, MockFilePicker.filterAudio, 1],
-                ["k", 3, "*.gif; *.png", 0],
-                ["l", 4, imageExtensionList + "; " + "*.gif; *.png", 0],
-                ["m", 1, "*.gif", 0],
+                ["k", 3, "*.gif; *.png", 1],
+                ["l", 4, imageExtensionList + "; " + "*.gif; *.png", 1],
+                ["m", 1, "*.gif", 1],
                 ["n", 0, undefined, 0],
                 ["z", 0, undefined, 0],
                 ["hidden", 0, undefined, 0],
                 ["untrusted-click", 0, undefined, 0],
                 ["prevent-default", 0, undefined, 0, true],
                 ["prevent-default-false", 0, undefined, 0, true],
                 ["right-click", 0, undefined, 0, true],
                 ["middle-click", 0, undefined, 0, true],