1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "nsDocShellEditorData.h"
9 #include "mozilla/dom/Document.h"
10 #include "mozilla/HTMLEditor.h"
11 #include "nsIInterfaceRequestorUtils.h"
12 #include "nsComponentManagerUtils.h"
13 #include "nsPIDOMWindow.h"
14 #include "nsEditingSession.h"
15 #include "nsIDocShell.h"
17 using namespace mozilla
;
18 using namespace mozilla::dom
;
20 nsDocShellEditorData::nsDocShellEditorData(nsIDocShell
* aOwningDocShell
)
21 : mDocShell(aOwningDocShell
),
22 mDetachedEditingState(Document::EditingState::eOff
),
25 mDetachedMakeEditable(false) {
26 NS_ASSERTION(mDocShell
, "Where is my docShell?");
29 nsDocShellEditorData::~nsDocShellEditorData() { TearDownEditor(); }
31 void nsDocShellEditorData::TearDownEditor() {
33 RefPtr
<HTMLEditor
> htmlEditor
= std::move(mHTMLEditor
);
34 htmlEditor
->PreDestroy();
36 mEditingSession
= nullptr;
40 nsresult
nsDocShellEditorData::MakeEditable(bool aInWaitForUriLoad
) {
45 // if we are already editable, and are getting turned off,
48 NS_WARNING("Destroying existing editor on frame");
50 RefPtr
<HTMLEditor
> htmlEditor
= std::move(mHTMLEditor
);
51 htmlEditor
->PreDestroy();
54 if (aInWaitForUriLoad
) {
60 bool nsDocShellEditorData::GetEditable() {
61 return mMakeEditable
|| (mHTMLEditor
!= nullptr);
64 nsEditingSession
* nsDocShellEditorData::GetEditingSession() {
65 EnsureEditingSession();
67 return mEditingSession
.get();
70 nsresult
nsDocShellEditorData::SetHTMLEditor(HTMLEditor
* aHTMLEditor
) {
71 // destroy any editor that we have. Checks for equality are
72 // necessary to ensure that assigment into the nsCOMPtr does
73 // not temporarily reduce the refCount of the editor to zero
74 if (mHTMLEditor
== aHTMLEditor
) {
79 RefPtr
<HTMLEditor
> htmlEditor
= std::move(mHTMLEditor
);
80 htmlEditor
->PreDestroy();
81 MOZ_ASSERT(!mHTMLEditor
,
82 "Nested call of nsDocShellEditorData::SetHTMLEditor() detected");
85 mHTMLEditor
= aHTMLEditor
; // owning addref
87 mMakeEditable
= false;
93 // This creates the editing session on the content docShell that owns 'this'.
94 void nsDocShellEditorData::EnsureEditingSession() {
95 NS_ASSERTION(mDocShell
, "Should have docShell here");
96 NS_ASSERTION(!mIsDetached
, "This will stomp editing session!");
98 if (!mEditingSession
) {
99 mEditingSession
= new nsEditingSession();
103 nsresult
nsDocShellEditorData::DetachFromWindow() {
104 NS_ASSERTION(mEditingSession
,
105 "Can't detach when we don't have a session to detach!");
107 nsCOMPtr
<nsPIDOMWindowOuter
> domWindow
=
108 mDocShell
? mDocShell
->GetWindow() : nullptr;
109 nsresult rv
= mEditingSession
->DetachFromWindow(domWindow
);
110 NS_ENSURE_SUCCESS(rv
, rv
);
113 mDetachedMakeEditable
= mMakeEditable
;
114 mMakeEditable
= false;
116 nsCOMPtr
<dom::Document
> doc
= domWindow
->GetDoc();
117 mDetachedEditingState
= doc
->GetEditingState();
124 nsresult
nsDocShellEditorData::ReattachToWindow(nsIDocShell
* aDocShell
) {
125 mDocShell
= aDocShell
;
127 nsCOMPtr
<nsPIDOMWindowOuter
> domWindow
=
128 mDocShell
? mDocShell
->GetWindow() : nullptr;
129 nsresult rv
= mEditingSession
->ReattachToWindow(domWindow
);
130 NS_ENSURE_SUCCESS(rv
, rv
);
133 mMakeEditable
= mDetachedMakeEditable
;
135 RefPtr
<dom::Document
> doc
= domWindow
->GetDoc();
136 doc
->SetEditingState(mDetachedEditingState
);