1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
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/. */
8 #include "nsDocShellEditorData.h"
9 #include "nsIInterfaceRequestorUtils.h"
10 #include "nsComponentManagerUtils.h"
11 #include "nsPIDOMWindow.h"
12 #include "nsIDOMDocument.h"
13 #include "nsIEditor.h"
14 #include "nsIEditingSession.h"
15 #include "nsIDocShell.h"
17 /*---------------------------------------------------------------------------
21 ----------------------------------------------------------------------------*/
23 nsDocShellEditorData::nsDocShellEditorData(nsIDocShell
* inOwningDocShell
)
24 : mDocShell(inOwningDocShell
)
25 , mMakeEditable(false)
27 , mDetachedMakeEditable(false)
28 , mDetachedEditingState(nsIHTMLDocument::eOff
)
30 NS_ASSERTION(mDocShell
, "Where is my docShell?");
34 /*---------------------------------------------------------------------------
38 ----------------------------------------------------------------------------*/
39 nsDocShellEditorData::~nsDocShellEditorData()
45 nsDocShellEditorData::TearDownEditor()
48 mEditor
->PreDestroy(false);
51 mEditingSession
= nullptr;
56 /*---------------------------------------------------------------------------
60 ----------------------------------------------------------------------------*/
62 nsDocShellEditorData::MakeEditable(bool inWaitForUriLoad
)
67 // if we are already editable, and are getting turned off,
71 NS_WARNING("Destroying existing editor on frame");
73 mEditor
->PreDestroy(false);
83 /*---------------------------------------------------------------------------
87 ----------------------------------------------------------------------------*/
89 nsDocShellEditorData::GetEditable()
91 return mMakeEditable
|| (mEditor
!= nullptr);
94 /*---------------------------------------------------------------------------
98 ----------------------------------------------------------------------------*/
100 nsDocShellEditorData::CreateEditor()
102 nsCOMPtr
<nsIEditingSession
> editingSession
;
103 nsresult rv
= GetEditingSession(getter_AddRefs(editingSession
));
104 if (NS_FAILED(rv
)) return rv
;
106 nsCOMPtr
<nsIDOMWindow
> domWindow
=
107 mDocShell
? mDocShell
->GetWindow() : nullptr;
108 rv
= editingSession
->SetupEditorOnWindow(domWindow
);
109 if (NS_FAILED(rv
)) return rv
;
115 /*---------------------------------------------------------------------------
119 ----------------------------------------------------------------------------*/
121 nsDocShellEditorData::GetEditingSession(nsIEditingSession
**outEditingSession
)
123 nsresult rv
= EnsureEditingSession();
124 NS_ENSURE_SUCCESS(rv
, rv
);
126 NS_ADDREF(*outEditingSession
= mEditingSession
);
132 /*---------------------------------------------------------------------------
136 ----------------------------------------------------------------------------*/
138 nsDocShellEditorData::GetEditor(nsIEditor
**outEditor
)
140 NS_ENSURE_ARG_POINTER(outEditor
);
141 NS_IF_ADDREF(*outEditor
= mEditor
);
146 /*---------------------------------------------------------------------------
150 ----------------------------------------------------------------------------*/
152 nsDocShellEditorData::SetEditor(nsIEditor
*inEditor
)
154 // destroy any editor that we have. Checks for equality are
155 // necessary to ensure that assigment into the nsCOMPtr does
156 // not temporarily reduce the refCount of the editor to zero
157 if (mEditor
.get() != inEditor
)
161 mEditor
->PreDestroy(false);
165 mEditor
= inEditor
; // owning addref
167 mMakeEditable
= false;
174 /*---------------------------------------------------------------------------
178 This creates the editing session on the content docShell that owns
181 ----------------------------------------------------------------------------*/
183 nsDocShellEditorData::EnsureEditingSession()
185 NS_ASSERTION(mDocShell
, "Should have docShell here");
186 NS_ASSERTION(!mIsDetached
, "This will stomp editing session!");
190 if (!mEditingSession
)
193 do_CreateInstance("@mozilla.org/editor/editingsession;1", &rv
);
200 nsDocShellEditorData::DetachFromWindow()
202 NS_ASSERTION(mEditingSession
,
203 "Can't detach when we don't have a session to detach!");
205 nsCOMPtr
<nsIDOMWindow
> domWindow
=
206 mDocShell
? mDocShell
->GetWindow() : nullptr;
207 nsresult rv
= mEditingSession
->DetachFromWindow(domWindow
);
208 NS_ENSURE_SUCCESS(rv
, rv
);
211 mDetachedMakeEditable
= mMakeEditable
;
212 mMakeEditable
= false;
214 nsCOMPtr
<nsIDOMDocument
> domDoc
;
215 domWindow
->GetDocument(getter_AddRefs(domDoc
));
216 nsCOMPtr
<nsIHTMLDocument
> htmlDoc
= do_QueryInterface(domDoc
);
218 mDetachedEditingState
= htmlDoc
->GetEditingState();
226 nsDocShellEditorData::ReattachToWindow(nsIDocShell
* aDocShell
)
228 mDocShell
= aDocShell
;
230 nsCOMPtr
<nsIDOMWindow
> domWindow
=
231 mDocShell
? mDocShell
->GetWindow() : nullptr;
232 nsresult rv
= mEditingSession
->ReattachToWindow(domWindow
);
233 NS_ENSURE_SUCCESS(rv
, rv
);
236 mMakeEditable
= mDetachedMakeEditable
;
238 nsCOMPtr
<nsIDOMDocument
> domDoc
;
239 domWindow
->GetDocument(getter_AddRefs(domDoc
));
240 nsCOMPtr
<nsIHTMLDocument
> htmlDoc
= do_QueryInterface(domDoc
);
242 htmlDoc
->SetEditingState(mDetachedEditingState
);