Bug 917322 part.2 Make CompositionStringSynthesizer just a wrapper of TextEventDispatcher r=smaug
authorMasayuki Nakano <masayuki@d-toybox.com>
Wed, 28 Jan 2015 15:27:31 +0900
changeset 239573 d7c660bbe955ffd01892590b4e38da9106a2ebc4
parent 239572 87c8054d9040f96a3babbd3decfb9c7ad2d6882a
child 239574 a3c0661108138058c16363814ac44855b5fb7d14
push id500
push userjoshua.m.grant@gmail.com
push dateThu, 29 Jan 2015 01:48:36 +0000
reviewerssmaug
bugs917322
milestone38.0a1
Bug 917322 part.2 Make CompositionStringSynthesizer just a wrapper of TextEventDispatcher r=smaug
dom/base/CompositionStringSynthesizer.cpp
dom/base/CompositionStringSynthesizer.h
dom/base/nsDOMWindowUtils.cpp
--- a/dom/base/CompositionStringSynthesizer.cpp
+++ b/dom/base/CompositionStringSynthesizer.cpp
@@ -15,146 +15,52 @@
 
 namespace mozilla {
 namespace dom {
 
 NS_IMPL_ISUPPORTS(CompositionStringSynthesizer,
                   nsICompositionStringSynthesizer)
 
 CompositionStringSynthesizer::CompositionStringSynthesizer(
-                                nsPIDOMWindow* aWindow)
+                                TextEventDispatcher* aDispatcher)
+  : mDispatcher(aDispatcher)
 {
-  mWindow = do_GetWeakReference(aWindow);
-  mClauses = new TextRangeArray();
-  ClearInternal();
 }
 
 CompositionStringSynthesizer::~CompositionStringSynthesizer()
 {
 }
 
-void
-CompositionStringSynthesizer::ClearInternal()
-{
-  mString.Truncate();
-  mClauses->Clear();
-  mCaret.mRangeType = 0;
-}
-
-nsIWidget*
-CompositionStringSynthesizer::GetWidget()
-{
-  nsCOMPtr<nsPIDOMWindow> window = do_QueryReferent(mWindow);
-  if (!window) {
-    return nullptr;
-  }
-  nsIDocShell *docShell = window->GetDocShell();
-  if (!docShell) {
-    return nullptr;
-  }
-  nsCOMPtr<nsIPresShell> presShell = docShell->GetPresShell();
-  if (!presShell) {
-    return nullptr;
-  }
-  nsIFrame* frame = presShell->GetRootFrame();
-  if (!frame) {
-    return nullptr;
-  }
-  return frame->GetView()->GetNearestWidget(nullptr);
-}
-
 NS_IMETHODIMP
 CompositionStringSynthesizer::SetString(const nsAString& aString)
 {
-  nsCOMPtr<nsIWidget> widget = GetWidget();
-  NS_ENSURE_TRUE(widget && !widget->Destroyed(), NS_ERROR_NOT_AVAILABLE);
-
-  mString = aString;
-  return NS_OK;
+  MOZ_RELEASE_ASSERT(nsContentUtils::IsCallerChrome());
+  return mDispatcher->SetPendingCompositionString(aString);
 }
 
 NS_IMETHODIMP
 CompositionStringSynthesizer::AppendClause(uint32_t aLength,
                                            uint32_t aAttribute)
 {
-  nsCOMPtr<nsIWidget> widget = GetWidget();
-  NS_ENSURE_TRUE(widget && !widget->Destroyed(), NS_ERROR_NOT_AVAILABLE);
-
-  switch (aAttribute) {
-    case ATTR_RAWINPUT:
-    case ATTR_SELECTEDRAWTEXT:
-    case ATTR_CONVERTEDTEXT:
-    case ATTR_SELECTEDCONVERTEDTEXT: {
-      TextRange textRange;
-      textRange.mStartOffset =
-        mClauses->IsEmpty() ? 0 : mClauses->LastElement().mEndOffset;
-      textRange.mEndOffset = textRange.mStartOffset + aLength;
-      textRange.mRangeType = aAttribute;
-      mClauses->AppendElement(textRange);
-      return NS_OK;
-    }
-    default:
-      return NS_ERROR_INVALID_ARG;
-  }
+  MOZ_RELEASE_ASSERT(nsContentUtils::IsCallerChrome());
+  return mDispatcher->AppendClauseToPendingComposition(aLength, aAttribute);
 }
 
 NS_IMETHODIMP
 CompositionStringSynthesizer::SetCaret(uint32_t aOffset, uint32_t aLength)
 {
-  nsCOMPtr<nsIWidget> widget = GetWidget();
-  NS_ENSURE_TRUE(widget && !widget->Destroyed(), NS_ERROR_NOT_AVAILABLE);
-
-  mCaret.mStartOffset = aOffset;
-  mCaret.mEndOffset = mCaret.mStartOffset + aLength;
-  mCaret.mRangeType = NS_TEXTRANGE_CARETPOSITION;
-  return NS_OK;
+  MOZ_RELEASE_ASSERT(nsContentUtils::IsCallerChrome());
+  return mDispatcher->SetCaretInPendingComposition(aOffset, aLength);
 }
 
 NS_IMETHODIMP
 CompositionStringSynthesizer::DispatchEvent(bool* aDefaultPrevented)
 {
+  MOZ_RELEASE_ASSERT(nsContentUtils::IsCallerChrome());
   NS_ENSURE_ARG_POINTER(aDefaultPrevented);
-  nsCOMPtr<nsIWidget> widget = GetWidget();
-  NS_ENSURE_TRUE(widget && !widget->Destroyed(), NS_ERROR_NOT_AVAILABLE);
-
-  if (!nsContentUtils::IsCallerChrome()) {
-    return NS_ERROR_DOM_SECURITY_ERR;
-  }
-
-  if (!mClauses->IsEmpty() &&
-      mClauses->LastElement().mEndOffset != mString.Length()) {
-    NS_WARNING("Sum of length of the all clauses must be same as the string "
-               "length");
-    ClearInternal();
-    return NS_ERROR_ILLEGAL_VALUE;
-  }
-  if (mCaret.mRangeType == NS_TEXTRANGE_CARETPOSITION) {
-    if (mCaret.mEndOffset > mString.Length()) {
-      NS_WARNING("Caret position is out of the composition string");
-      ClearInternal();
-      return NS_ERROR_ILLEGAL_VALUE;
-    }
-    mClauses->AppendElement(mCaret);
-  }
-
-  WidgetCompositionEvent compChangeEvent(true, NS_COMPOSITION_CHANGE, widget);
-  compChangeEvent.time = PR_IntervalNow();
-  compChangeEvent.mData = mString;
-  if (!mClauses->IsEmpty()) {
-    compChangeEvent.mRanges = mClauses;
-  }
-
-  // XXX How should we set false for this on b2g?
-  compChangeEvent.mFlags.mIsSynthesizedForTests = true;
-
   nsEventStatus status = nsEventStatus_eIgnore;
-  nsresult rv = widget->DispatchEvent(&compChangeEvent, status);
+  nsresult rv = mDispatcher->FlushPendingComposition(status);
   *aDefaultPrevented = (status == nsEventStatus_eConsumeNoDefault);
-
-  ClearInternal();
-
-  NS_ENSURE_SUCCESS(rv, rv);
-
-  return NS_OK;
+  return rv;
 }
 
 } // namespace dom
 } // namespace mozilla
--- a/dom/base/CompositionStringSynthesizer.h
+++ b/dom/base/CompositionStringSynthesizer.h
@@ -2,44 +2,35 @@
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #ifndef mozilla_dom_compositionstringsynthesizer_h__
 #define mozilla_dom_compositionstringsynthesizer_h__
 
 #include "nsICompositionStringSynthesizer.h"
-#include "nsString.h"
-#include "nsWeakReference.h"
-#include "mozilla/Attributes.h"
-#include "mozilla/TextRange.h"
 
-class nsIWidget;
-class nsPIDOMWindow;
+#include "mozilla/TextEventDispatcher.h"
 
 namespace mozilla {
 namespace dom {
 
 class CompositionStringSynthesizer MOZ_FINAL :
   public nsICompositionStringSynthesizer
 {
+  typedef mozilla::widget::TextEventDispatcher TextEventDispatcher;
+
 public:
-  explicit CompositionStringSynthesizer(nsPIDOMWindow* aWindow);
+  explicit CompositionStringSynthesizer(TextEventDispatcher* aDispatcher);
 
   NS_DECL_ISUPPORTS
   NS_DECL_NSICOMPOSITIONSTRINGSYNTHESIZER
 
 private:
   ~CompositionStringSynthesizer();
 
-  nsWeakPtr mWindow; // refers an instance of nsPIDOMWindow
-  nsString mString;
-  nsRefPtr<TextRangeArray> mClauses;
-  TextRange mCaret;
-
-  nsIWidget* GetWidget();
-  void ClearInternal();
+  nsRefPtr<TextEventDispatcher> mDispatcher;
 };
 
 } // namespace dom
 } // namespace mozilla
 
 #endif // #ifndef mozilla_dom_compositionstringsynthesizer_h__
--- a/dom/base/nsDOMWindowUtils.cpp
+++ b/dom/base/nsDOMWindowUtils.cpp
@@ -35,16 +35,17 @@
 #include "nsCharsetSource.h"
 #include "nsJSEnvironment.h"
 #include "nsJSUtils.h"
 
 #include "mozilla/EventStateManager.h"
 #include "mozilla/MiscEvents.h"
 #include "mozilla/MouseEvents.h"
 #include "mozilla/TextEvents.h"
+#include "mozilla/TextEventDispatcher.h"
 #include "mozilla/TouchEvents.h"
 
 #include "nsViewManager.h"
 
 #include "nsIDOMHTMLCanvasElement.h"
 #include "nsLayoutUtils.h"
 #include "nsComputedDOMStyle.h"
 #include "nsIPresShell.h"
@@ -2195,20 +2196,24 @@ NS_IMETHODIMP
 nsDOMWindowUtils::CreateCompositionStringSynthesizer(
                     nsICompositionStringSynthesizer** aResult)
 {
   NS_ENSURE_ARG_POINTER(aResult);
   *aResult = nullptr;
 
   MOZ_RELEASE_ASSERT(nsContentUtils::IsCallerChrome());
 
-  nsCOMPtr<nsPIDOMWindow> window = do_QueryReferent(mWindow);
-  NS_ENSURE_TRUE(window, NS_ERROR_NOT_AVAILABLE);
-
-  NS_ADDREF(*aResult = new CompositionStringSynthesizer(window));
+  nsCOMPtr<nsIWidget> widget = GetWidget();
+  if (NS_WARN_IF(!widget)) {
+    return NS_ERROR_FAILURE;
+  }
+  nsRefPtr<TextEventDispatcher> dispatcher(widget->GetTextEventDispatcher());
+  // XXX Ignore the result of InitForTest() for now.
+  dispatcher->InitForTests();
+  NS_ADDREF(*aResult = new CompositionStringSynthesizer(dispatcher));
   return NS_OK;
 }
 
 NS_IMETHODIMP
 nsDOMWindowUtils::SendQueryContentEvent(uint32_t aType,
                                         uint32_t aOffset, uint32_t aLength,
                                         int32_t aX, int32_t aY,
                                         uint32_t aAdditionalFlags,