nsDocShellEditorData.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #include "nsDocShellEditorData.h"
  6. #include "nsIInterfaceRequestorUtils.h"
  7. #include "nsComponentManagerUtils.h"
  8. #include "nsPIDOMWindow.h"
  9. #include "nsIDocument.h"
  10. #include "nsIDOMDocument.h"
  11. #include "nsIEditor.h"
  12. #include "nsIEditingSession.h"
  13. #include "nsIDocShell.h"
  14. nsDocShellEditorData::nsDocShellEditorData(nsIDocShell* aOwningDocShell)
  15. : mDocShell(aOwningDocShell)
  16. , mMakeEditable(false)
  17. , mIsDetached(false)
  18. , mDetachedMakeEditable(false)
  19. , mDetachedEditingState(nsIHTMLDocument::eOff)
  20. {
  21. NS_ASSERTION(mDocShell, "Where is my docShell?");
  22. }
  23. nsDocShellEditorData::~nsDocShellEditorData()
  24. {
  25. TearDownEditor();
  26. }
  27. void
  28. nsDocShellEditorData::TearDownEditor()
  29. {
  30. if (mEditor) {
  31. mEditor->PreDestroy(false);
  32. mEditor = nullptr;
  33. }
  34. mEditingSession = nullptr;
  35. mIsDetached = false;
  36. }
  37. nsresult
  38. nsDocShellEditorData::MakeEditable(bool aInWaitForUriLoad)
  39. {
  40. if (mMakeEditable) {
  41. return NS_OK;
  42. }
  43. // if we are already editable, and are getting turned off,
  44. // nuke the editor.
  45. if (mEditor) {
  46. NS_WARNING("Destroying existing editor on frame");
  47. mEditor->PreDestroy(false);
  48. mEditor = nullptr;
  49. }
  50. if (aInWaitForUriLoad) {
  51. mMakeEditable = true;
  52. }
  53. return NS_OK;
  54. }
  55. bool
  56. nsDocShellEditorData::GetEditable()
  57. {
  58. return mMakeEditable || (mEditor != nullptr);
  59. }
  60. nsresult
  61. nsDocShellEditorData::CreateEditor()
  62. {
  63. nsCOMPtr<nsIEditingSession> editingSession;
  64. nsresult rv = GetEditingSession(getter_AddRefs(editingSession));
  65. if (NS_FAILED(rv)) {
  66. return rv;
  67. }
  68. nsCOMPtr<nsPIDOMWindowOuter> domWindow =
  69. mDocShell ? mDocShell->GetWindow() : nullptr;
  70. rv = editingSession->SetupEditorOnWindow(domWindow);
  71. if (NS_FAILED(rv)) {
  72. return rv;
  73. }
  74. return NS_OK;
  75. }
  76. nsresult
  77. nsDocShellEditorData::GetEditingSession(nsIEditingSession** aResult)
  78. {
  79. nsresult rv = EnsureEditingSession();
  80. NS_ENSURE_SUCCESS(rv, rv);
  81. NS_ADDREF(*aResult = mEditingSession);
  82. return NS_OK;
  83. }
  84. nsresult
  85. nsDocShellEditorData::GetEditor(nsIEditor** aResult)
  86. {
  87. NS_ENSURE_ARG_POINTER(aResult);
  88. NS_IF_ADDREF(*aResult = mEditor);
  89. return NS_OK;
  90. }
  91. nsresult
  92. nsDocShellEditorData::SetEditor(nsIEditor* aEditor)
  93. {
  94. // destroy any editor that we have. Checks for equality are
  95. // necessary to ensure that assigment into the nsCOMPtr does
  96. // not temporarily reduce the refCount of the editor to zero
  97. if (mEditor.get() != aEditor) {
  98. if (mEditor) {
  99. mEditor->PreDestroy(false);
  100. mEditor = nullptr;
  101. }
  102. mEditor = aEditor; // owning addref
  103. if (!mEditor) {
  104. mMakeEditable = false;
  105. }
  106. }
  107. return NS_OK;
  108. }
  109. // This creates the editing session on the content docShell that owns 'this'.
  110. nsresult
  111. nsDocShellEditorData::EnsureEditingSession()
  112. {
  113. NS_ASSERTION(mDocShell, "Should have docShell here");
  114. NS_ASSERTION(!mIsDetached, "This will stomp editing session!");
  115. nsresult rv = NS_OK;
  116. if (!mEditingSession) {
  117. mEditingSession =
  118. do_CreateInstance("@mozilla.org/editor/editingsession;1", &rv);
  119. }
  120. return rv;
  121. }
  122. nsresult
  123. nsDocShellEditorData::DetachFromWindow()
  124. {
  125. NS_ASSERTION(mEditingSession,
  126. "Can't detach when we don't have a session to detach!");
  127. nsCOMPtr<nsPIDOMWindowOuter> domWindow =
  128. mDocShell ? mDocShell->GetWindow() : nullptr;
  129. nsresult rv = mEditingSession->DetachFromWindow(domWindow);
  130. NS_ENSURE_SUCCESS(rv, rv);
  131. mIsDetached = true;
  132. mDetachedMakeEditable = mMakeEditable;
  133. mMakeEditable = false;
  134. nsCOMPtr<nsIDocument> doc = domWindow->GetDoc();
  135. nsCOMPtr<nsIHTMLDocument> htmlDoc = do_QueryInterface(doc);
  136. if (htmlDoc) {
  137. mDetachedEditingState = htmlDoc->GetEditingState();
  138. }
  139. mDocShell = nullptr;
  140. return NS_OK;
  141. }
  142. nsresult
  143. nsDocShellEditorData::ReattachToWindow(nsIDocShell* aDocShell)
  144. {
  145. mDocShell = aDocShell;
  146. nsCOMPtr<nsPIDOMWindowOuter> domWindow =
  147. mDocShell ? mDocShell->GetWindow() : nullptr;
  148. nsresult rv = mEditingSession->ReattachToWindow(domWindow);
  149. NS_ENSURE_SUCCESS(rv, rv);
  150. mIsDetached = false;
  151. mMakeEditable = mDetachedMakeEditable;
  152. nsCOMPtr<nsIDocument> doc = domWindow->GetDoc();
  153. nsCOMPtr<nsIHTMLDocument> htmlDoc = do_QueryInterface(doc);
  154. if (htmlDoc) {
  155. htmlDoc->SetEditingState(mDetachedEditingState);
  156. }
  157. return NS_OK;
  158. }