Bug 1467802 - part 2: Mark TextEditor::GetAndInitDocEncoder() as const r?m_kato draft
authorMasayuki Nakano <masayuki@d-toybox.com>
Wed, 18 Jul 2018 20:51:55 +0900
changeset 820222 8edbf2fe3c36aad39f21b785ee59cbd6a99e76a0
parent 820221 2240203267e189693fe2f76e4fb182418819755c
child 820223 32117d853d7551b912c72da20dc00975b18999b3
push id116753
push usermasayuki@d-toybox.com
push dateThu, 19 Jul 2018 06:22:48 +0000
reviewersm_kato
bugs1467802
milestone63.0a1
Bug 1467802 - part 2: Mark TextEditor::GetAndInitDocEncoder() as const r?m_kato TextEditor::GetAndInitDocEncoder() modifies only mCachedDocumentEncoder and mCachedDocumentEncoderType which are cache of the method to save recreation cost of document encoder when same type document encoder is requested. So, with marking them mutable, we can change the method to a const method. MozReview-Commit-ID: 80W0NtQhJOR
editor/libeditor/EditorBase.cpp
editor/libeditor/EditorBase.h
editor/libeditor/TextEditor.cpp
editor/libeditor/TextEditor.h
--- a/editor/libeditor/EditorBase.cpp
+++ b/editor/libeditor/EditorBase.cpp
@@ -708,17 +708,17 @@ EditorBase::DeleteSelection(EDirection a
 NS_IMETHODIMP
 EditorBase::GetSelection(Selection** aSelection)
 {
   return GetSelection(SelectionType::eNormal, aSelection);
 }
 
 nsresult
 EditorBase::GetSelection(SelectionType aSelectionType,
-                         Selection** aSelection)
+                         Selection** aSelection) const
 {
   NS_ENSURE_TRUE(aSelection, NS_ERROR_NULL_POINTER);
   *aSelection = nullptr;
   nsISelectionController* selcon = GetSelectionController();
   if (!selcon) {
     return NS_ERROR_NOT_INITIALIZED;
   }
   RefPtr<Selection> selection =
--- a/editor/libeditor/EditorBase.h
+++ b/editor/libeditor/EditorBase.h
@@ -286,20 +286,20 @@ public:
     if (!presShell) {
       return nullptr;
     }
     nsISelectionController* sc = static_cast<PresShell*>(presShell);
     return sc;
   }
 
   nsresult GetSelection(SelectionType aSelectionType,
-                        Selection** aSelection);
+                        Selection** aSelection) const;
 
   Selection* GetSelection(SelectionType aSelectionType =
-                                          SelectionType::eNormal)
+                                          SelectionType::eNormal) const
   {
     nsISelectionController* sc = GetSelectionController();
     if (!sc) {
       return nullptr;
     }
     Selection* selection = sc->GetSelection(ToRawSelectionType(aSelectionType));
     return selection;
   }
--- a/editor/libeditor/TextEditor.cpp
+++ b/editor/libeditor/TextEditor.cpp
@@ -1707,21 +1707,20 @@ TextEditor::CanCopy(bool* aCanCopy)
 NS_IMETHODIMP
 TextEditor::CanDelete(bool* aCanDelete)
 {
   NS_ENSURE_ARG_POINTER(aCanDelete);
   *aCanDelete = IsModifiable() && CanCutOrCopy(ePasswordFieldAllowed);
   return NS_OK;
 }
 
-// Used by OutputToString
 already_AddRefed<nsIDocumentEncoder>
 TextEditor::GetAndInitDocEncoder(const nsAString& aFormatType,
-                                 uint32_t aFlags,
-                                 const nsACString& aCharset)
+                                 uint32_t aDocumentEncoderFlags,
+                                 const nsACString& aCharset) const
 {
   nsCOMPtr<nsIDocumentEncoder> docEncoder;
   if (!mCachedDocumentEncoder ||
       !mCachedDocumentEncoderType.Equals(aFormatType)) {
     nsAutoCString formatType(NS_DOC_ENCODER_CONTRACTID_BASE);
     LossyAppendUTF16toASCII(aFormatType, formatType);
     docEncoder = do_CreateInstance(formatType.get());
     if (NS_WARN_IF(!docEncoder)) {
@@ -1734,34 +1733,35 @@ TextEditor::GetAndInitDocEncoder(const n
   }
 
   nsCOMPtr<nsIDocument> doc = GetDocument();
   NS_ASSERTION(doc, "Need a document");
 
   nsresult rv =
     docEncoder->NativeInit(
                   doc, aFormatType,
-                  aFlags | nsIDocumentEncoder::RequiresReinitAfterOutput);
+                  aDocumentEncoderFlags |
+                    nsIDocumentEncoder::RequiresReinitAfterOutput);
   if (NS_WARN_IF(NS_FAILED(rv))) {
     return nullptr;
   }
 
   if (!aCharset.IsEmpty() && !aCharset.EqualsLiteral("null")) {
     docEncoder->SetCharset(aCharset);
   }
 
   int32_t wrapWidth = WrapWidth();
   if (wrapWidth >= 0) {
     Unused << docEncoder->SetWrapColumn(wrapWidth);
   }
 
   // Set the selection, if appropriate.
   // We do this either if the OutputSelectionOnly flag is set,
   // in which case we use our existing selection ...
-  if (aFlags & nsIDocumentEncoder::OutputSelectionOnly) {
+  if (aDocumentEncoderFlags & nsIDocumentEncoder::OutputSelectionOnly) {
     RefPtr<Selection> selection = GetSelection();
     if (NS_WARN_IF(!selection)) {
       return nullptr;
     }
     rv = docEncoder->SetSelection(selection);
     if (NS_WARN_IF(NS_FAILED(rv))) {
       return nullptr;
     }
@@ -1779,17 +1779,16 @@ TextEditor::GetAndInitDocEncoder(const n
         return nullptr;
       }
     }
   }
 
   return docEncoder.forget();
 }
 
-
 NS_IMETHODIMP
 TextEditor::OutputToString(const nsAString& aFormatType,
                            uint32_t aFlags,
                            nsAString& aOutputString)
 {
   // Protect the edit rules object from dying
   RefPtr<TextEditRules> rules(mRules);
 
--- a/editor/libeditor/TextEditor.h
+++ b/editor/libeditor/TextEditor.h
@@ -322,20 +322,29 @@ protected: // Shouldn't be used by frien
    * Return true if the data is safe to insert as the source and destination
    * principals match, or we are in a editor context where this doesn't matter.
    * Otherwise, the data must be sanitized first.
    */
   bool IsSafeToInsertData(nsIDocument* aSourceDoc);
 
   virtual nsresult InitRules();
 
-  already_AddRefed<nsIDocumentEncoder> GetAndInitDocEncoder(
-                                         const nsAString& aFormatType,
-                                         uint32_t aFlags,
-                                         const nsACString& aCharset);
+  /**
+   * GetAndInitDocEncoder() returns a document encoder instance for aFormatType
+   * after initializing it.  The result may be cached for saving recreation
+   * cost.
+   *
+   * @param aFormatType             MIME type like "text/plain".
+   * @param aDocumentEncoderFlags   Flags of nsIDocumentEncoder.
+   * @param aCharset                Encoding of the document.
+   */
+  already_AddRefed<nsIDocumentEncoder>
+  GetAndInitDocEncoder(const nsAString& aFormatType,
+                       uint32_t aDocumentEncoderFlags,
+                       const nsACString& aCharset) const;
 
   /**
    * Factored methods for handling insertion of data from transferables
    * (drag&drop or clipboard).
    */
   virtual nsresult PrepareTransferable(nsITransferable** transferable);
 
   nsresult InsertTextFromTransferable(nsITransferable* transferable);
@@ -388,18 +397,18 @@ protected: // Shouldn't be used by frien
    *                    a composition event handler in web contents moved focus
    *                    for committing the composition, returns false.
    */
   bool EnsureComposition(WidgetCompositionEvent& aCompositionEvent);
 
   virtual already_AddRefed<nsIContent> GetInputEventTargetContent() override;
 
 protected:
-  nsCOMPtr<nsIDocumentEncoder> mCachedDocumentEncoder;
-  nsString mCachedDocumentEncoderType;
+  mutable nsCOMPtr<nsIDocumentEncoder> mCachedDocumentEncoder;
+  mutable nsString mCachedDocumentEncoderType;
   int32_t mWrapColumn;
   int32_t mMaxTextLength;
   int32_t mInitTriggerCounter;
   int32_t mNewlineHandling;
   int32_t mCaretStyle;
 
   friend class AutoEditInitRulesTrigger;
   friend class TextEditRules;