Bug 1445541 - Enable the character encoding menu when the document is decoded as UTF-8 and an error is found, instead of disabling it when no errors are found. r=hsivonen
authorMasatoshi Kimura <VYV03354@nifty.ne.jp>
Fri, 06 Apr 2018 06:20:28 +0900
changeset 412113 effaf62f3423dfed70794254fa613f3cdfcfe9f4
parent 412112 3c5dbe504e151a4f29033403fc8e7b746ab5e06d
child 412114 1d725bf734be646047bf90f9372b3b23496c4729
push id33784
push usernerli@mozilla.com
push dateFri, 06 Apr 2018 21:54:36 +0000
treeherdermozilla-central@0e0ec6c01e25 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewershsivonen
bugs1445541
milestone61.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 1445541 - Enable the character encoding menu when the document is decoded as UTF-8 and an error is found, instead of disabling it when no errors are found. r=hsivonen MozReview-Commit-ID: 1e6kQbAxcqm
dom/base/nsDocument.cpp
dom/base/nsIDocument.h
parser/html/nsHtml5StreamParser.cpp
parser/html/nsHtml5TreeBuilderCppSupplement.h
parser/html/nsHtml5TreeBuilderHSupplement.h
parser/html/nsHtml5TreeOperation.cpp
parser/html/nsHtml5TreeOperation.h
--- a/dom/base/nsDocument.cpp
+++ b/dom/base/nsDocument.cpp
@@ -3672,16 +3672,17 @@ nsIDocument::DefaultStyleAttrURLData()
   return mCachedURLData;
 }
 
 void
 nsIDocument::SetDocumentCharacterSet(NotNull<const Encoding*> aEncoding)
 {
   if (mCharacterSet != aEncoding) {
     mCharacterSet = aEncoding;
+    mEncodingMenuDisabled = aEncoding == UTF_8_ENCODING;
 
     if (nsPresContext* context = GetPresContext()) {
       context->DispatchCharSetChange(aEncoding);
     }
   }
 }
 
 void
--- a/dom/base/nsIDocument.h
+++ b/dom/base/nsIDocument.h
@@ -1108,19 +1108,19 @@ public:
   {
     mSandboxFlags = sandboxFlags;
   }
 
   /**
    * Called when the document was decoded as UTF-8 and decoder encountered no
    * errors.
    */
-  void DisableEncodingMenu()
+  void EnableEncodingMenu()
   {
-    mEncodingMenuDisabled = true;
+    mEncodingMenuDisabled = false;
   }
 
   /**
    * Access HTTP header data (this may also get set from other
    * sources, like HTML META tags).
    */
   void GetHeaderData(nsAtom* aHeaderField, nsAString& aData) const;
   void SetHeaderData(nsAtom* aheaderField, const nsAString& aData);
--- a/parser/html/nsHtml5StreamParser.cpp
+++ b/parser/html/nsHtml5StreamParser.cpp
@@ -830,17 +830,22 @@ nsHtml5StreamParser::WriteStreamBytes(co
   for (;;) {
     auto dst = mLastBuffer->TailAsSpan(NS_HTML5_STREAM_PARSER_READ_BUFFER_SIZE);
     uint32_t result;
     size_t read;
     size_t written;
     bool hadErrors;
     Tie(result, read, written, hadErrors) =
       mUnicodeDecoder->DecodeToUTF16(src, dst, false);
-    mHasHadErrors |= hadErrors;
+    if (hadErrors && !mHasHadErrors) {
+      mHasHadErrors = true;
+      if (mEncoding == UTF_8_ENCODING) {
+        mTreeBuilder->TryToEnableEncodingMenu();
+      }
+    }
     src = src.From(read);
     totalRead += read;
     mLastBuffer->AdvanceEnd(written);
     if (result == kOutputFull) {
       RefPtr<nsHtml5OwningUTF16Buffer> newBuf =
         nsHtml5OwningUTF16Buffer::FalliblyCreate(
           NS_HTML5_STREAM_PARSER_READ_BUFFER_SIZE);
       if (!newBuf) {
@@ -1081,17 +1086,22 @@ nsHtml5StreamParser::DoStopRequest()
   for (;;) {
     auto dst = mLastBuffer->TailAsSpan(NS_HTML5_STREAM_PARSER_READ_BUFFER_SIZE);
     uint32_t result;
     size_t read;
     size_t written;
     bool hadErrors;
     Tie(result, read, written, hadErrors) =
       mUnicodeDecoder->DecodeToUTF16(src, dst, true);
-    mHasHadErrors |= hadErrors;
+    if (hadErrors && !mHasHadErrors) {
+      mHasHadErrors = true;
+      if (mEncoding == UTF_8_ENCODING) {
+        mTreeBuilder->TryToEnableEncodingMenu();
+      }
+    }
     MOZ_ASSERT(read == 0, "How come an empty span was read form?");
     mLastBuffer->AdvanceEnd(written);
     if (result == kOutputFull) {
       RefPtr<nsHtml5OwningUTF16Buffer> newBuf =
         nsHtml5OwningUTF16Buffer::FalliblyCreate(
           NS_HTML5_STREAM_PARSER_READ_BUFFER_SIZE);
       if (!newBuf) {
         MarkAsBroken(NS_ERROR_OUT_OF_MEMORY);
@@ -1437,19 +1447,16 @@ nsHtml5StreamParser::ParseAvailableData(
               }
             }
             return; // no more data for now but expecting more
           case STREAM_ENDED:
             if (mAtEOF) {
               return;
             }
             mAtEOF = true;
-            if (mEncoding == UTF_8_ENCODING && !mHasHadErrors) {
-              mTreeBuilder->TryToDisableEncodingMenu();
-            }
             if (mCharsetSource < kCharsetFromMetaTag) {
               if (mInitialEncodingWasFromParentFrame) {
                 // Unfortunately, this check doesn't take effect for
                 // cross-origin frames, so cross-origin ad frames that have
                 // no text and only an image or a Flash embed get the more
                 // severe message from the next if block. The message is
                 // technically accurate, though.
                 mTreeBuilder->MaybeComplainAboutCharset(
--- a/parser/html/nsHtml5TreeBuilderCppSupplement.h
+++ b/parser/html/nsHtml5TreeBuilderCppSupplement.h
@@ -1303,25 +1303,25 @@ nsHtml5TreeBuilder::MaybeComplainAboutCh
   if (MOZ_UNLIKELY(mBuilder)) {
     MOZ_ASSERT_UNREACHABLE("Must never complain about charset with builder.");
     return;
   }
   mOpQueue.AppendElement()->Init(aMsgId, aError, aLineNumber);
 }
 
 void
-nsHtml5TreeBuilder::TryToDisableEncodingMenu()
+nsHtml5TreeBuilder::TryToEnableEncodingMenu()
 {
   if (MOZ_UNLIKELY(mBuilder)) {
     MOZ_ASSERT_UNREACHABLE("Must never disable encoding menu with builder.");
     return;
   }
   nsHtml5TreeOperation* treeOp = mOpQueue.AppendElement();
   NS_ASSERTION(treeOp, "Tree op allocation failed.");
-  treeOp->Init(eTreeOpDisableEncodingMenu);
+  treeOp->Init(eTreeOpEnableEncodingMenu);
 }
 
 void
 nsHtml5TreeBuilder::AddSnapshotToScript(nsAHtml5TreeBuilderState* aSnapshot,
                                         int32_t aLine)
 {
   if (MOZ_UNLIKELY(mBuilder)) {
     MOZ_ASSERT_UNREACHABLE("Must never use snapshots with builder.");
--- a/parser/html/nsHtml5TreeBuilderHSupplement.h
+++ b/parser/html/nsHtml5TreeBuilderHSupplement.h
@@ -139,17 +139,17 @@ void
 NeedsCharsetSwitchTo(NotNull<const Encoding*> aEncoding,
                      int32_t aSource,
                      int32_t aLineNumber);
 
 void
 MaybeComplainAboutCharset(const char* aMsgId, bool aError, int32_t aLineNumber);
 
 void
-TryToDisableEncodingMenu();
+TryToEnableEncodingMenu();
 
 void
 AddSnapshotToScript(nsAHtml5TreeBuilderState* aSnapshot, int32_t aLine);
 
 void
 DropHandles();
 
 void
--- a/parser/html/nsHtml5TreeOperation.cpp
+++ b/parser/html/nsHtml5TreeOperation.cpp
@@ -1052,19 +1052,19 @@ nsHtml5TreeOperation::Perform(nsHtml5Tre
     }
     case eTreeOpMaybeComplainAboutCharset: {
       char* msgId = mOne.charPtr;
       bool error = mTwo.integer;
       int32_t lineNumber = mThree.integer;
       aBuilder->MaybeComplainAboutCharset(msgId, error, (uint32_t)lineNumber);
       return NS_OK;
     }
-    case eTreeOpDisableEncodingMenu: {
+    case eTreeOpEnableEncodingMenu: {
       nsIDocument* doc = aBuilder->GetDocument();
-      doc->DisableEncodingMenu();
+      doc->EnableEncodingMenu();
       return NS_OK;
     }
     case eTreeOpAddClass: {
       Element* element = (*(mOne.node))->AsElement();
       char16_t* str = mTwo.unicharPtr;
       nsDependentString depStr(str);
       // See viewsource.css for the possible classes
       nsAutoString klass;
--- a/parser/html/nsHtml5TreeOperation.h
+++ b/parser/html/nsHtml5TreeOperation.h
@@ -64,17 +64,17 @@ enum eHtml5TreeOperation
   eTreeOpSvgLoad,
   eTreeOpMaybeComplainAboutCharset,
   eTreeOpAddClass,
   eTreeOpAddViewSourceHref,
   eTreeOpAddViewSourceBase,
   eTreeOpAddError,
   eTreeOpAddLineNumberId,
   eTreeOpStartLayout,
-  eTreeOpDisableEncodingMenu
+  eTreeOpEnableEncodingMenu
 };
 
 class nsHtml5TreeOperationStringPair
 {
 private:
   nsString mPublicId;
   nsString mSystemId;