Bug 684469 - Various cleanup in nsHtml5TreeOpExecutor; r=hsivonen
authorMs2ger <ms2ger@gmail.com>
Sun, 04 Sep 2011 12:49:29 +0200
changeset 76488 d6d6656d8bc690e54e9d5f94c273420ab596c987
parent 76487 2cd38970b65ad4a9520d0928151977029b591728
child 76523 ca5ce4ebff755b8432a40685d22fcf2b84180de2
push id21114
push userMs2ger@gmail.com
push dateSun, 04 Sep 2011 10:50:35 +0000
treeherdermozilla-central@d6d6656d8bc6 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewershsivonen
bugs684469
milestone9.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 684469 - Various cleanup in nsHtml5TreeOpExecutor; r=hsivonen
parser/html/nsHtml5TreeOpExecutor.cpp
parser/html/nsHtml5TreeOpExecutor.h
--- a/parser/html/nsHtml5TreeOpExecutor.cpp
+++ b/parser/html/nsHtml5TreeOpExecutor.cpp
@@ -53,16 +53,19 @@
 #include "nsContentUtils.h"
 #include "mozAutoDocUpdate.h"
 #include "nsNetUtil.h"
 #include "nsHtml5Parser.h"
 #include "nsHtml5Tokenizer.h"
 #include "nsHtml5TreeBuilder.h"
 #include "nsHtml5StreamParser.h"
 #include "mozilla/css/Loader.h"
+#include "mozilla/Util.h" // DebugOnly
+
+using namespace mozilla;
 
 NS_IMPL_CYCLE_COLLECTION_CLASS(nsHtml5TreeOpExecutor)
 
 NS_INTERFACE_TABLE_HEAD_CYCLE_COLLECTION_INHERITED(nsHtml5TreeOpExecutor)
   NS_INTERFACE_TABLE_INHERITED1(nsHtml5TreeOpExecutor, 
                                 nsIContentSink)
 NS_INTERFACE_TABLE_TAIL_INHERITING(nsContentSink)
 
@@ -126,17 +129,17 @@ nsHtml5TreeOpExecutor::DidBuildModel(PRB
     
     // If the above caused a call to nsIParser::Terminate(), let that call
     // win.
     if (!mParser) {
       return NS_OK;
     }
   }
   
-  static_cast<nsHtml5Parser*> (mParser.get())->DropStreamParser();
+  GetParser()->DropStreamParser();
 
   // This comes from nsXMLContentSink and nsHTMLContentSink
   DidBuildModelImpl(aTerminated);
 
   if (!mLayoutStarted) {
     // We never saw the body, and layout never got started. Force
     // layout *now*, to get an initial reflow.
 
@@ -264,17 +267,17 @@ void
 nsHtml5TreeOpExecutor::UpdateChildCounts()
 {
   // No-op
 }
 
 nsresult
 nsHtml5TreeOpExecutor::FlushTags()
 {
-    return NS_OK;
+  return NS_OK;
 }
 
 void
 nsHtml5TreeOpExecutor::PostEvaluateScript(nsIScriptElement *aElement)
 {
   nsCOMPtr<nsIHTMLDocument> htmlDocument = do_QueryInterface(mDocument);
   NS_ASSERTION(htmlDocument, "Document didn't QI into HTML document.");
   htmlDocument->ScriptExecuted(aElement);
@@ -311,17 +314,17 @@ nsHtml5TreeOpExecutor::UpdateStyleSheet(
   nsresult rv = ssle->UpdateStyleSheet(mFragmentMode ? nsnull : this,
                                        &willNotify,
                                        &isAlternate);
   if (NS_SUCCEEDED(rv) && willNotify && !isAlternate && !mFragmentMode) {
     ++mPendingSheetCount;
     mScriptLoader->AddExecuteBlocker();
   }
 
-  if (aElement->IsHTML() && aElement->Tag() == nsGkAtoms::link) {
+  if (aElement->IsHTML(nsGkAtoms::link)) {
     // look for <link rel="next" href="url">
     nsAutoString relVal;
     aElement->GetAttr(kNameSpaceID_None, nsGkAtoms::rel, relVal);
     if (!relVal.IsEmpty()) {
       // XXX seems overkill to generate this string array
       nsAutoTArray<nsString, 4> linkTypes;
       nsStyleLinkElement::ParseLinkTypes(relVal, linkTypes);
       PRBool hasPrefetch = linkTypes.Contains(NS_LITERAL_STRING("prefetch"));
@@ -351,18 +354,18 @@ nsHtml5TreeOpExecutor::FlushSpeculativeL
 {
   if (NS_UNLIKELY(!mParser)) {
     return;
   }
   nsTArray<nsHtml5SpeculativeLoad> speculativeLoadQueue;
   mStage.MoveSpeculativeLoadsTo(speculativeLoadQueue);
   const nsHtml5SpeculativeLoad* start = speculativeLoadQueue.Elements();
   const nsHtml5SpeculativeLoad* end = start + speculativeLoadQueue.Length();
-  for (nsHtml5SpeculativeLoad* iter = (nsHtml5SpeculativeLoad*)start;
-       iter < end; 
+  for (nsHtml5SpeculativeLoad* iter = const_cast<nsHtml5SpeculativeLoad*>(start);
+       iter < end;
        ++iter) {
     iter->Perform(this);
   }
 }
 
 class nsHtml5FlushLoopGuard
 {
   private:
@@ -453,20 +456,20 @@ nsHtml5TreeOpExecutor::RunFlushLoop()
       }
     } else {
       FlushSpeculativeLoads(); // Make sure speculative loads never start after
                                // the corresponding normal loads for the same
                                // URLs.
       // Not sure if this grip is still needed, but previously, the code
       // gripped before calling ParseUntilBlocked();
       nsRefPtr<nsHtml5StreamParser> streamKungFuDeathGrip = 
-        static_cast<nsHtml5Parser*> (mParser.get())->GetStreamParser();
+        GetParser()->GetStreamParser();
       // Now parse content left in the document.write() buffer queue if any.
       // This may generate tree ops on its own or dequeue a speculation.
-      static_cast<nsHtml5Parser*> (mParser.get())->ParseUntilBlocked();
+      GetParser()->ParseUntilBlocked();
     }
 
     if (mOpQueue.IsEmpty()) {
       // Avoid bothering the rest of the engine with a doc update if there's 
       // nothing to do.
       return;
     }
 
@@ -477,17 +480,17 @@ nsHtml5TreeOpExecutor::RunFlushLoop()
     BeginDocUpdate();
 
     PRUint32 numberOfOpsToFlush = mOpQueue.Length();
 
     mElementsSeenInThisAppendBatch.SetCapacity(numberOfOpsToFlush * 2);
 
     const nsHtml5TreeOperation* first = mOpQueue.Elements();
     const nsHtml5TreeOperation* last = first + numberOfOpsToFlush - 1;
-    for (nsHtml5TreeOperation* iter = (nsHtml5TreeOperation*)first;;) {
+    for (nsHtml5TreeOperation* iter = const_cast<nsHtml5TreeOperation*>(first);;) {
       if (NS_UNLIKELY(!mParser)) {
         // The previous tree op caused a call to nsIParser::Terminate().
         break;
       }
       NS_ASSERTION(mFlushState == eInDocUpdate, 
         "Tried to perform tree op outside update batch.");
       iter->Perform(this, &scriptElement);
 
@@ -576,18 +579,18 @@ nsHtml5TreeOpExecutor::FlushDocumentWrit
   BeginDocUpdate();
 
   PRUint32 numberOfOpsToFlush = mOpQueue.Length();
 
   mElementsSeenInThisAppendBatch.SetCapacity(numberOfOpsToFlush * 2);
 
   const nsHtml5TreeOperation* start = mOpQueue.Elements();
   const nsHtml5TreeOperation* end = start + numberOfOpsToFlush;
-  for (nsHtml5TreeOperation* iter = (nsHtml5TreeOperation*)start;
-       iter < end; 
+  for (nsHtml5TreeOperation* iter = const_cast<nsHtml5TreeOperation*>(start);
+       iter < end;
        ++iter) {
     if (NS_UNLIKELY(!mParser)) {
       // The previous tree op caused a call to nsIParser::Terminate().
       break;
     }
     NS_ASSERTION(mFlushState == eInDocUpdate, 
       "Tried to perform tree op outside update batch.");
     iter->Perform(this, &scriptElement);
@@ -658,17 +661,17 @@ nsHtml5TreeOpExecutor::SetDocumentMode(n
 void
 nsHtml5TreeOpExecutor::StartLayout() {
   if (mLayoutStarted || !mDocument) {
     return;
   }
 
   EndDocUpdate();
 
-  if(NS_UNLIKELY(!mParser)) {
+  if (NS_UNLIKELY(!mParser)) {
     // got terminate
     return;
   }
 
   nsContentSink::StartLayout(PR_FALSE);
 
   BeginDocUpdate();
 }
@@ -769,17 +772,17 @@ nsHtml5TreeOpExecutor::Start()
 }
 
 void
 nsHtml5TreeOpExecutor::NeedsCharsetSwitchTo(const char* aEncoding,
                                             PRInt32 aSource)
 {
   EndDocUpdate();
 
-  if(NS_UNLIKELY(!mParser)) {
+  if (NS_UNLIKELY(!mParser)) {
     // got terminate
     return;
   }
   
   nsCOMPtr<nsIWebShellServices> wss = do_QueryInterface(mDocShell);
   if (!wss) {
     return;
   }
@@ -791,25 +794,31 @@ nsHtml5TreeOpExecutor::NeedsCharsetSwitc
   // if the charset switch was accepted, wss has called Terminate() on the
   // parser by now
 
   if (!mParser) {
     // success
     return;
   }
 
-  (static_cast<nsHtml5Parser*> (mParser.get()))->ContinueAfterFailedCharsetSwitch();
+  GetParser()->ContinueAfterFailedCharsetSwitch();
 
   BeginDocUpdate();
 }
 
+nsHtml5Parser*
+nsHtml5TreeOpExecutor::GetParser()
+{
+  return static_cast<nsHtml5Parser*>(mParser.get());
+}
+
 nsHtml5Tokenizer*
 nsHtml5TreeOpExecutor::GetTokenizer()
 {
-  return (static_cast<nsHtml5Parser*> (mParser.get()))->GetTokenizer();
+  return GetParser()->GetTokenizer();
 }
 
 void
 nsHtml5TreeOpExecutor::Reset()
 {
   DropHeldElements();
   mReadingFromStage = PR_FALSE;
   mOpQueue.Clear();
@@ -839,17 +848,17 @@ nsHtml5TreeOpExecutor::MoveOpsFrom(nsTAr
     return;
   }
   mOpQueue.MoveElementsFrom(aOpQueue);
 }
 
 void
 nsHtml5TreeOpExecutor::InitializeDocWriteParserState(nsAHtml5TreeBuilderState* aState, PRInt32 aLine)
 {
-  static_cast<nsHtml5Parser*> (mParser.get())->InitializeDocWriteParserState(aState, aLine);
+  GetParser()->InitializeDocWriteParserState(aState, aLine);
 }
 
 // Speculative loading
 
 already_AddRefed<nsIURI>
 nsHtml5TreeOpExecutor::ConvertIfNotPreloadedYet(const nsAString& aURL)
 {
   // The URL of the document without <base>
@@ -872,19 +881,17 @@ nsHtml5TreeOpExecutor::ConvertIfNotPrelo
     return nsnull;
   }
   nsCAutoString spec;
   uri->GetSpec(spec);
   if (mPreloadedURLs.Contains(spec)) {
     return nsnull;
   }
   mPreloadedURLs.Put(spec);
-  nsIURI* retURI = uri;
-  NS_ADDREF(retURI);
-  return retURI;
+  return uri.forget();
 }
 
 void
 nsHtml5TreeOpExecutor::PreloadScript(const nsAString& aURL,
                                      const nsAString& aCharset,
                                      const nsAString& aType)
 {
   nsCOMPtr<nsIURI> uri = ConvertIfNotPreloadedYet(aURL);
@@ -919,21 +926,19 @@ nsHtml5TreeOpExecutor::PreloadImage(cons
 void
 nsHtml5TreeOpExecutor::SetSpeculationBase(const nsAString& aURL)
 {
   if (mSpeculationBaseURI) {
     // the first one wins
     return;
   }
   const nsCString& charset = mDocument->GetDocumentCharacterSet();
-  nsresult rv = NS_NewURI(getter_AddRefs(mSpeculationBaseURI), aURL,
-      charset.get(), mDocument->GetDocumentURI());
-  if (NS_FAILED(rv)) {
-    NS_WARNING("Failed to create a URI");
-  }
+  DebugOnly<nsresult> rv = NS_NewURI(getter_AddRefs(mSpeculationBaseURI), aURL,
+                                     charset.get(), mDocument->GetDocumentURI());
+  NS_WARN_IF_FALSE(NS_SUCCEEDED(rv), "Failed to create a URI");
 }
 
 #ifdef DEBUG_NS_HTML5_TREE_OP_EXECUTOR_FLUSH
 PRUint32 nsHtml5TreeOpExecutor::sAppendBatchMaxSize = 0;
 PRUint32 nsHtml5TreeOpExecutor::sAppendBatchSlotsExamined = 0;
 PRUint32 nsHtml5TreeOpExecutor::sAppendBatchExaminations = 0;
 PRUint32 nsHtml5TreeOpExecutor::sLongestTimeOffTheEventLoop = 0;
 PRUint32 nsHtml5TreeOpExecutor::sTimesFlushLoopInterrupted = 0;
--- a/parser/html/nsHtml5TreeOpExecutor.h
+++ b/parser/html/nsHtml5TreeOpExecutor.h
@@ -54,16 +54,17 @@
 #include "nsIScriptElement.h"
 #include "nsIParser.h"
 #include "nsCOMArray.h"
 #include "nsAHtml5TreeOpSink.h"
 #include "nsHtml5TreeOpStage.h"
 #include "nsHashSets.h"
 #include "nsIURI.h"
 
+class nsHtml5Parser;
 class nsHtml5TreeBuilder;
 class nsHtml5Tokenizer;
 class nsHtml5StreamParser;
 
 typedef nsIContent* nsIContentPtr;
 
 enum eHtml5FlushState {
   eNotFlushing = 0,  // not flushing
@@ -397,16 +398,17 @@ class nsHtml5TreeOpExecutor : public nsC
 
     void PreloadStyle(const nsAString& aURL, const nsAString& aCharset);
 
     void PreloadImage(const nsAString& aURL, const nsAString& aCrossOrigin);
 
     void SetSpeculationBase(const nsAString& aURL);
 
   private:
+    nsHtml5Parser* GetParser();
 
     nsHtml5Tokenizer* GetTokenizer();
 
     /**
      * Get a nsIURI for an nsString if the URL hasn't been preloaded yet.
      */
     already_AddRefed<nsIURI> ConvertIfNotPreloadedYet(const nsAString& aURL);