Bug 1387317 - part2: EditorEventListener should stop using interface methods as far as possible r=m_kato

MozReview-Commit-ID: EPQeBez2tJh

--HG--
extra : rebase_source : b18b3399f703d3cdcb6f26cec3fcb8f707fb9519
This commit is contained in:
Masayuki Nakano 2017-08-08 11:25:36 +09:00
parent 20fc8ab477
commit 7c54bc878b
11 changed files with 80 additions and 45 deletions

View File

@ -2572,7 +2572,7 @@ nsTextEditorState::SetValue(const nsAString& aValue, const nsAString* aOldValue,
mValueBeingSet = aValue;
mIsCommittingComposition = true;
RefPtr<TextEditor> textEditor = mTextEditor;
nsresult rv = textEditor->ForceCompositionEnd();
nsresult rv = textEditor->CommitComposition();
if (!self.get()) {
return true;
}

View File

@ -2193,11 +2193,13 @@ EditorBase::EndIMEComposition()
NS_IMETHODIMP
EditorBase::ForceCompositionEnd()
{
nsCOMPtr<nsIPresShell> ps = GetPresShell();
if (!ps) {
return NS_ERROR_NOT_AVAILABLE;
}
nsPresContext* pc = ps->GetPresContext();
return CommitComposition();
}
nsresult
EditorBase::CommitComposition()
{
nsPresContext* pc = GetPresContext();
if (!pc) {
return NS_ERROR_NOT_AVAILABLE;
}

View File

@ -251,6 +251,11 @@ public:
already_AddRefed<nsIDOMDocument> GetDOMDocument();
already_AddRefed<nsIDocument> GetDocument();
already_AddRefed<nsIPresShell> GetPresShell();
nsPresContext* GetPresContext()
{
RefPtr<nsIPresShell> presShell = GetPresShell();
return presShell ? presShell->GetPresContext() : nullptr;
}
already_AddRefed<nsIWidget> GetWidget();
nsISelectionController* GetSelectionController() const
{
@ -351,6 +356,15 @@ public:
WidgetCompositionEvent* aCompositionChangeEvet) = 0;
void EndIMEComposition();
/**
* Commit composition if there is.
* Note that when there is a composition, this requests to commit composition
* to native IME. Therefore, when there is composition, this can do anything.
* For example, the editor instance, the widget or the process itself may
* be destroyed.
*/
nsresult CommitComposition();
void SwitchTextDirectionTo(uint32_t aDirection);
protected:
@ -947,6 +961,36 @@ public:
*/
uint32_t Flags() const { return mFlags; }
nsresult AddFlags(uint32_t aFlags)
{
const uint32_t kOldFlags = Flags();
const uint32_t kNewFlags = (kOldFlags | aFlags);
if (kNewFlags == kOldFlags) {
return NS_OK;
}
return SetFlags(kNewFlags); // virtual call and may be expensive.
}
nsresult RemoveFlags(uint32_t aFlags)
{
const uint32_t kOldFlags = Flags();
const uint32_t kNewFlags = (kOldFlags & ~aFlags);
if (kNewFlags == kOldFlags) {
return NS_OK;
}
return SetFlags(kNewFlags); // virtual call and may be expensive.
}
nsresult AddAndRemoveFlags(uint32_t aAddingFlags, uint32_t aRemovingFlags)
{
MOZ_ASSERT(!(aAddingFlags & aRemovingFlags),
"Same flags are specified both adding and removing");
const uint32_t kOldFlags = Flags();
const uint32_t kNewFlags = ((kOldFlags | aAddingFlags) & ~aRemovingFlags);
if (kNewFlags == kOldFlags) {
return NS_OK;
}
return SetFlags(kNewFlags); // virtual call and may be expensive.
}
bool IsPlaintextEditor() const
{
return (mFlags & nsIPlaintextEditor::eEditorPlaintextMask) != 0;

View File

@ -12,6 +12,7 @@
#include "mozilla/EventListenerManager.h" // for EventListenerManager
#include "mozilla/IMEStateManager.h" // for IMEStateManager
#include "mozilla/Preferences.h" // for Preferences
#include "mozilla/TextEditor.h" // for TextEditor
#include "mozilla/TextEvents.h" // for WidgetCompositionEvent
#include "mozilla/dom/Element.h" // for Element
#include "mozilla/dom/Event.h" // for Event
@ -36,11 +37,8 @@
#include "nsIDOMMouseEvent.h" // for nsIDOMMouseEvent
#include "nsIDOMNode.h" // for nsIDOMNode
#include "nsIDocument.h" // for nsIDocument
#include "nsIEditor.h" // for EditorBase::GetSelection, etc.
#include "nsIEditorMailSupport.h" // for nsIEditorMailSupport
#include "nsIFocusManager.h" // for nsIFocusManager
#include "nsIFormControl.h" // for nsIFormControl, etc.
#include "nsIHTMLEditor.h" // for nsIHTMLEditor
#include "nsINode.h" // for nsINode, ::NODE_IS_EDITABLE, etc.
#include "nsIPlaintextEditor.h" // for nsIPlaintextEditor, etc.
#include "nsIPresShell.h" // for nsIPresShell
@ -366,7 +364,7 @@ EditorEventListener::EnsureCommitCompoisition()
{
MOZ_ASSERT(!DetachedFromEditor());
RefPtr<EditorBase> editorBase(mEditorBase);
editorBase->ForceCompositionEnd();
editorBase->CommitComposition();
return !DetachedFromEditor();
}
@ -711,19 +709,14 @@ EditorEventListener::HandleMiddleClickPaste(nsIDOMMouseEvent* aMouseEvent)
return NS_ERROR_NULL_POINTER;
}
RefPtr<EditorBase> editorBase(mEditorBase);
RefPtr<Selection> selection = editorBase->GetSelection();
RefPtr<TextEditor> textEditor = mEditorBase->AsTextEditor();
MOZ_ASSERT(textEditor);
RefPtr<Selection> selection = textEditor->GetSelection();
if (selection) {
selection->Collapse(parent, offset);
}
// If the ctrl key is pressed, we'll do paste as quotation.
// Would've used the alt key, but the kde wmgr treats alt-middle specially.
nsCOMPtr<nsIEditorMailSupport> mailEditor;
if (clickEvent->IsControl()) {
mailEditor = do_QueryObject(editorBase);
}
nsresult rv;
int32_t clipboard = nsIClipboard::kGlobalClipboard;
nsCOMPtr<nsIClipboard> clipboardService =
@ -736,10 +729,12 @@ EditorEventListener::HandleMiddleClickPaste(nsIDOMMouseEvent* aMouseEvent)
}
}
if (mailEditor) {
mailEditor->PasteAsQuotation(clipboard);
// If the ctrl key is pressed, we'll do paste as quotation.
// Would've used the alt key, but the kde wmgr treats alt-middle specially.
if (clickEvent->IsControl()) {
textEditor->PasteAsQuotation(clipboard);
} else {
editorBase->Paste(clipboard);
textEditor->Paste(clipboard);
}
// Prevent the event from propagating up to be possibly handled
@ -1191,11 +1186,8 @@ EditorEventListener::SpellCheckIfNeeded()
// If the spell check skip flag is still enabled from creation time,
// disable it because focused editors are allowed to spell check.
RefPtr<EditorBase> editorBase(mEditorBase);
uint32_t currentFlags = 0;
editorBase->GetFlags(&currentFlags);
if(currentFlags & nsIPlaintextEditor::eEditorSkipSpellCheck) {
currentFlags ^= nsIPlaintextEditor::eEditorSkipSpellCheck;
editorBase->SetFlags(currentFlags);
if(editorBase->ShouldSkipSpellCheck()) {
editorBase->RemoveFlags(nsIPlaintextEditor::eEditorSkipSpellCheck);
}
}

View File

@ -1203,7 +1203,7 @@ HTMLEditor::ReplaceHeadContentsWithHTML(const nsAString& aSourceToInsert)
RefPtr<Selection> selection = GetSelection();
NS_ENSURE_TRUE(selection, NS_ERROR_NULL_POINTER);
ForceCompositionEnd();
CommitComposition();
// Do not use AutoRules -- rules code won't let us insert in <head>. Use
// the head node as a parent and delete/insert directly.
@ -1273,7 +1273,7 @@ HTMLEditor::ReplaceHeadContentsWithHTML(const nsAString& aSourceToInsert)
NS_IMETHODIMP
HTMLEditor::RebuildDocumentFromSource(const nsAString& aSourceString)
{
ForceCompositionEnd();
CommitComposition();
RefPtr<Selection> selection = GetSelection();
NS_ENSURE_TRUE(selection, NS_ERROR_NULL_POINTER);
@ -1537,7 +1537,7 @@ HTMLEditor::InsertElementAtSelection(nsIDOMElement* aElement,
nsCOMPtr<nsIDOMNode> node = do_QueryInterface(aElement);
ForceCompositionEnd();
CommitComposition();
AutoEditBatch beginBatching(this);
AutoRules beginRulesSniffing(this, EditAction::insertElement,
nsIEditor::eNext);
@ -3580,7 +3580,7 @@ HTMLEditor::SelectEntireDocument(Selection* aSelection)
NS_IMETHODIMP
HTMLEditor::SelectAll()
{
ForceCompositionEnd();
CommitComposition();
RefPtr<Selection> selection = GetSelection();
NS_ENSURE_STATE(selection);
@ -4519,7 +4519,7 @@ nsresult
HTMLEditor::SetCSSBackgroundColor(const nsAString& aColor)
{
NS_ENSURE_TRUE(mRules, NS_ERROR_NOT_INITIALIZED);
ForceCompositionEnd();
CommitComposition();
// Protect the edit rules object from dying
nsCOMPtr<nsIEditRules> rules(mRules);

View File

@ -102,7 +102,7 @@ HTMLEditor::LoadHTML(const nsAString& aInputString)
NS_ENSURE_TRUE(mRules, NS_ERROR_NOT_INITIALIZED);
// force IME commit; set up rules sniffing and batching
ForceCompositionEnd();
CommitComposition();
AutoEditBatch beginBatching(this);
AutoRules beginRulesSniffing(this, EditAction::loadHTML, nsIEditor::eNext);
@ -197,7 +197,7 @@ HTMLEditor::DoInsertHTMLWithContext(const nsAString& aInputString,
nsCOMPtr<nsIEditRules> rules(mRules);
// force IME commit; set up rules sniffing and batching
ForceCompositionEnd();
CommitComposition();
AutoEditBatch beginBatching(this);
AutoRules beginRulesSniffing(this, EditAction::htmlPaste, nsIEditor::eNext);
@ -1494,7 +1494,7 @@ HTMLEditor::PasteNoFormatting(int32_t aSelectionType)
return NS_OK;
}
ForceCompositionEnd();
CommitComposition();
// Get Clipboard Service
nsresult rv;

View File

@ -18,8 +18,6 @@
#include "nsIDOMEventTarget.h"
#include "nsIDOMMouseEvent.h"
#include "nsIDOMNode.h"
#include "nsIHTMLInlineTableEditor.h"
#include "nsIHTMLObjectResizer.h"
#include "nsISupportsImpl.h"
#include "nsLiteralString.h"
#include "nsQueryObject.h"

View File

@ -12,7 +12,6 @@
namespace mozilla {
class EditorBase;
class HTMLEditor;
class HTMLEditorEventListener final : public EditorEventListener
{

View File

@ -110,7 +110,7 @@ HTMLEditor::SetInlineProperty(nsIAtom* aProperty,
NS_ENSURE_TRUE(aProperty, NS_ERROR_NULL_POINTER);
NS_ENSURE_TRUE(mRules, NS_ERROR_NOT_INITIALIZED);
nsCOMPtr<nsIEditRules> rules(mRules);
ForceCompositionEnd();
CommitComposition();
RefPtr<Selection> selection = GetSelection();
NS_ENSURE_TRUE(selection, NS_ERROR_NULL_POINTER);
@ -1202,7 +1202,7 @@ HTMLEditor::RemoveInlinePropertyImpl(nsIAtom* aProperty,
{
MOZ_ASSERT_IF(aProperty, aAttribute);
NS_ENSURE_TRUE(mRules, NS_ERROR_NOT_INITIALIZED);
ForceCompositionEnd();
CommitComposition();
RefPtr<Selection> selection = GetSelection();
NS_ENSURE_TRUE(selection, NS_ERROR_NULL_POINTER);
@ -1351,7 +1351,7 @@ HTMLEditor::DecreaseFontSize()
nsresult
HTMLEditor::RelativeFontChange(FontSize aDir)
{
ForceCompositionEnd();
CommitComposition();
// Get the selection
RefPtr<Selection> selection = GetSelection();

View File

@ -1080,7 +1080,7 @@ TextEditor::Undo(uint32_t aCount)
AutoUpdateViewBatch beginViewBatching(this);
ForceCompositionEnd();
CommitComposition();
NotifyEditorObservers(eNotifyEditorObserversOfBefore);
@ -1108,7 +1108,7 @@ TextEditor::Redo(uint32_t aCount)
AutoUpdateViewBatch beginViewBatching(this);
ForceCompositionEnd();
CommitComposition();
NotifyEditorObservers(eNotifyEditorObserversOfBefore);
@ -1150,7 +1150,7 @@ TextEditor::FireClipboardEvent(EventMessage aEventMessage,
bool* aActionTaken)
{
if (aEventMessage == ePaste) {
ForceCompositionEnd();
CommitComposition();
}
nsCOMPtr<nsIPresShell> presShell = GetPresShell();

View File

@ -163,7 +163,7 @@ TextEditor::InsertFromDataTransfer(DataTransfer* aDataTransfer,
nsresult
TextEditor::InsertFromDrop(nsIDOMEvent* aDropEvent)
{
ForceCompositionEnd();
CommitComposition();
nsCOMPtr<nsIDOMDragEvent> dragEvent(do_QueryInterface(aDropEvent));
NS_ENSURE_TRUE(dragEvent, NS_ERROR_FAILURE);