Bug 1430982 - part 1: Make nsTextServicesDocument store editor with RefPtr<TextEditor> rather than nsWeakPtr r=m_kato

nsTextServicesDocument stores editor with nsWeakPtr for avoiding circular
reference.  However, both TextEditor and nsTextServicesDocument are cycle
collectable. So, we don't need to worry about the issue and we can simply
make it store with RefPtr<TextEditor>.  Then, we can make EditorBase and
nsTextServicesDocument access each other directly.

MozReview-Commit-ID: 9I4U8ivFlov

--HG--
extra : rebase_source : 4880c6f7d706acc70324f58aee70d4311db44059
This commit is contained in:
Masayuki Nakano 2018-01-18 14:33:31 +09:00
parent 2f4f0ba72d
commit 88c945974e
2 changed files with 33 additions and 56 deletions

View File

@ -8,6 +8,7 @@
#include "mozilla/Assertions.h" // for MOZ_ASSERT, etc
#include "mozilla/dom/Selection.h"
#include "mozilla/mozalloc.h" // for operator new, etc
#include "mozilla/TextEditor.h" // for TextEditor
#include "nsAString.h" // for nsAString::Length, etc
#include "nsContentUtils.h" // for nsContentUtils
#include "nsDebug.h" // for NS_ENSURE_TRUE, etc
@ -98,6 +99,7 @@ NS_INTERFACE_MAP_END
NS_IMPL_CYCLE_COLLECTION(nsTextServicesDocument,
mDOMDocument,
mSelCon,
mTextEditor,
mIterator,
mPrevTextBlock,
mNextTextBlock,
@ -168,7 +170,7 @@ nsTextServicesDocument::InitWithEditor(nsIEditor *aEditor)
}
}
mEditor = do_GetWeakReference(aEditor);
mTextEditor = aEditor->AsTextEditor();
rv = aEditor->AddEditActionListener(this);
@ -978,26 +980,16 @@ nsTextServicesDocument::ScrollSelectionIntoView()
NS_IMETHODIMP
nsTextServicesDocument::DeleteSelection()
{
// We don't allow deletion during a collapsed selection!
nsCOMPtr<nsIEditor> editor (do_QueryReferent(mEditor));
NS_ASSERTION(editor, "DeleteSelection called without an editor present!");
NS_ASSERTION(SelectionIsValid(), "DeleteSelection called without a valid selection!");
if (!editor || !SelectionIsValid()) {
if (NS_WARN_IF(!mTextEditor) || NS_WARN_IF(!SelectionIsValid())) {
return NS_ERROR_FAILURE;
}
if (SelectionIsCollapsed()) {
return NS_OK;
}
LOCK_DOC(this);
//**** KDEBUG ****
// printf("\n---- Before Delete\n");
// printf("Sel: (%2d, %4d) (%2d, %4d)\n", mSelStartIndex, mSelStartOffset, mSelEndIndex, mSelEndOffset);
// PrintOffsetTable();
//**** KDEBUG ****
// If we have an mExtent, save off its current set of
// end points so we can compare them against mExtent's
// set after the deletion of the content.
@ -1069,12 +1061,6 @@ nsTextServicesDocument::DeleteSelection()
}
}
//**** KDEBUG ****
// printf("\n---- Middle Delete\n");
// printf("Sel: (%2d, %4d) (%2d, %4d)\n", mSelStartIndex, mSelStartOffset, mSelEndIndex, mSelEndOffset);
// PrintOffsetTable();
//**** KDEBUG ****
if (i == mSelEndIndex) {
if (entry->mIsInsertedText) {
// Inserted text offset entries have no width when
@ -1130,10 +1116,9 @@ nsTextServicesDocument::DeleteSelection()
AdjustContentIterator();
// Now delete the actual content!
RefPtr<TextEditor> textEditor = mTextEditor;
nsresult rv =
editor->DeleteSelection(nsIEditor::ePrevious, nsIEditor::eStrip);
textEditor->DeleteSelection(nsIEditor::ePrevious, nsIEditor::eStrip);
if (NS_FAILED(rv)) {
UNLOCK_DOC(this);
return rv;
@ -1254,14 +1239,13 @@ nsTextServicesDocument::DeleteSelection()
NS_IMETHODIMP
nsTextServicesDocument::InsertText(const nsString *aText)
{
nsCOMPtr<nsIEditor> editor (do_QueryReferent(mEditor));
NS_ASSERTION(editor, "InsertText called without an editor present!");
if (!editor || !SelectionIsValid()) {
return NS_ERROR_FAILURE;
if (NS_WARN_IF(!aText)) {
return NS_ERROR_INVALID_ARG;
}
NS_ENSURE_TRUE(aText, NS_ERROR_NULL_POINTER);
if (NS_WARN_IF(!mTextEditor) || NS_WARN_IF(!SelectionIsValid())) {
return NS_ERROR_FAILURE;
}
// If the selection is not collapsed, we need to save
// off the selection offsets so we can restore the
@ -1286,30 +1270,20 @@ nsTextServicesDocument::InsertText(const nsString *aText)
LOCK_DOC(this);
nsresult rv = editor->BeginTransaction();
RefPtr<TextEditor> textEditor = mTextEditor;
nsresult rv = textEditor->BeginTransaction();
if (NS_FAILED(rv)) {
UNLOCK_DOC(this);
return rv;
}
nsCOMPtr<nsIPlaintextEditor> textEditor (do_QueryInterface(editor, &rv));
if (textEditor) {
rv = textEditor->InsertText(*aText);
}
rv = textEditor->InsertText(*aText);
if (NS_FAILED(rv)) {
editor->EndTransaction();
textEditor->EndTransaction();
UNLOCK_DOC(this);
return rv;
}
//**** KDEBUG ****
// printf("\n---- Before Insert\n");
// printf("Sel: (%2d, %4d) (%2d, %4d)\n", mSelStartIndex, mSelStartOffset, mSelEndIndex, mSelEndOffset);
// PrintOffsetTable();
//**** KDEBUG ****
int32_t strLength = aText->Length();
OffsetEntry *itEntry;
@ -1331,7 +1305,7 @@ nsTextServicesDocument::InsertText(const nsString *aText)
itEntry = new OffsetEntry(entry->mNode, entry->mStrOffset, strLength);
if (!itEntry) {
editor->EndTransaction();
textEditor->EndTransaction();
UNLOCK_DOC(this);
return NS_ERROR_OUT_OF_MEMORY;
}
@ -1340,7 +1314,7 @@ nsTextServicesDocument::InsertText(const nsString *aText)
itEntry->mNodeOffset = entry->mNodeOffset;
if (!mOffsetTable.InsertElementAt(mSelStartIndex, itEntry)) {
editor->EndTransaction();
textEditor->EndTransaction();
UNLOCK_DOC(this);
return NS_ERROR_FAILURE;
}
@ -1359,7 +1333,7 @@ nsTextServicesDocument::InsertText(const nsString *aText)
itEntry = mOffsetTable[i];
if (!itEntry) {
editor->EndTransaction();
textEditor->EndTransaction();
UNLOCK_DOC(this);
return NS_ERROR_FAILURE;
}
@ -1379,7 +1353,7 @@ nsTextServicesDocument::InsertText(const nsString *aText)
itEntry = new OffsetEntry(entry->mNode, mSelStartOffset, 0);
if (!itEntry) {
editor->EndTransaction();
textEditor->EndTransaction();
UNLOCK_DOC(this);
return NS_ERROR_OUT_OF_MEMORY;
}
@ -1404,7 +1378,7 @@ nsTextServicesDocument::InsertText(const nsString *aText)
RefPtr<Selection> selection =
mSelCon->GetDOMSelection(nsISelectionController::SELECTION_NORMAL);
if (NS_WARN_IF(!selection)) {
editor->EndTransaction();
textEditor->EndTransaction();
UNLOCK_DOC(this);
return rv;
}
@ -1413,7 +1387,7 @@ nsTextServicesDocument::InsertText(const nsString *aText)
itEntry->mNodeOffset + itEntry->mLength);
if (NS_FAILED(rv)) {
editor->EndTransaction();
textEditor->EndTransaction();
UNLOCK_DOC(this);
return rv;
}
@ -1428,7 +1402,7 @@ nsTextServicesDocument::InsertText(const nsString *aText)
rv = SplitOffsetEntry(mSelStartIndex, i);
if (NS_FAILED(rv)) {
editor->EndTransaction();
textEditor->EndTransaction();
UNLOCK_DOC(this);
return rv;
}
@ -1436,7 +1410,7 @@ nsTextServicesDocument::InsertText(const nsString *aText)
itEntry = new OffsetEntry(entry->mNode, mSelStartOffset, strLength);
if (!itEntry) {
editor->EndTransaction();
textEditor->EndTransaction();
UNLOCK_DOC(this);
return NS_ERROR_OUT_OF_MEMORY;
}
@ -1445,7 +1419,7 @@ nsTextServicesDocument::InsertText(const nsString *aText)
itEntry->mNodeOffset = entry->mNodeOffset + entry->mLength;
if (!mOffsetTable.InsertElementAt(mSelStartIndex + 1, itEntry)) {
editor->EndTransaction();
textEditor->EndTransaction();
UNLOCK_DOC(this);
return NS_ERROR_FAILURE;
}
@ -1477,7 +1451,7 @@ nsTextServicesDocument::InsertText(const nsString *aText)
rv = SetSelection(savedSelOffset, savedSelLength);
if (NS_FAILED(rv)) {
editor->EndTransaction();
textEditor->EndTransaction();
UNLOCK_DOC(this);
return rv;
}
@ -1485,13 +1459,13 @@ nsTextServicesDocument::InsertText(const nsString *aText)
rv = DeleteSelection();
if (NS_FAILED(rv)) {
editor->EndTransaction();
textEditor->EndTransaction();
UNLOCK_DOC(this);
return rv;
}
}
rv = editor->EndTransaction();
rv = textEditor->EndTransaction();
UNLOCK_DOC(this);

View File

@ -11,7 +11,6 @@
#include "nsIEditActionListener.h"
#include "nsISupportsImpl.h"
#include "nsITextServicesDocument.h"
#include "nsIWeakReferenceUtils.h"
#include "nsStringFwd.h"
#include "nsTArray.h"
#include "nscore.h"
@ -27,6 +26,10 @@ class nsISelection;
class nsISelectionController;
class nsITextServicesFilter;
namespace mozilla {
class TextEditor;
} // namespace mozilla
/** implementation of a text services object.
*
*/
@ -42,7 +45,7 @@ private:
nsCOMPtr<nsIDOMDocument> mDOMDocument;
nsCOMPtr<nsISelectionController>mSelCon;
nsWeakPtr mEditor; // avoid a cycle with the spell checker and editor
RefPtr<mozilla::TextEditor> mTextEditor;
nsCOMPtr<nsIContentIterator> mIterator;
TSDIteratorStatus mIteratorStatus;
nsCOMPtr<nsIContent> mPrevTextBlock;