Bumping manifests a=b2g-bump
[gecko.git] / docshell / base / nsDocShellEditorData.cpp
blob67391c4d4a1f81c8796553a56cd2b9789f762967
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 /*---------------------------------------------------------------------------
19 nsDocShellEditorData
21 ----------------------------------------------------------------------------*/
23 nsDocShellEditorData::nsDocShellEditorData(nsIDocShell* inOwningDocShell)
24 : mDocShell(inOwningDocShell)
25 , mMakeEditable(false)
26 , mIsDetached(false)
27 , mDetachedMakeEditable(false)
28 , mDetachedEditingState(nsIHTMLDocument::eOff)
30 NS_ASSERTION(mDocShell, "Where is my docShell?");
34 /*---------------------------------------------------------------------------
36 ~nsDocShellEditorData
38 ----------------------------------------------------------------------------*/
39 nsDocShellEditorData::~nsDocShellEditorData()
41 TearDownEditor();
44 void
45 nsDocShellEditorData::TearDownEditor()
47 if (mEditor) {
48 mEditor->PreDestroy(false);
49 mEditor = nullptr;
51 mEditingSession = nullptr;
52 mIsDetached = false;
56 /*---------------------------------------------------------------------------
58 MakeEditable
60 ----------------------------------------------------------------------------*/
61 nsresult
62 nsDocShellEditorData::MakeEditable(bool inWaitForUriLoad)
64 if (mMakeEditable)
65 return NS_OK;
67 // if we are already editable, and are getting turned off,
68 // nuke the editor.
69 if (mEditor)
71 NS_WARNING("Destroying existing editor on frame");
73 mEditor->PreDestroy(false);
74 mEditor = nullptr;
77 if (inWaitForUriLoad)
78 mMakeEditable = true;
79 return NS_OK;
83 /*---------------------------------------------------------------------------
85 GetEditable
87 ----------------------------------------------------------------------------*/
88 bool
89 nsDocShellEditorData::GetEditable()
91 return mMakeEditable || (mEditor != nullptr);
94 /*---------------------------------------------------------------------------
96 CreateEditor
98 ----------------------------------------------------------------------------*/
99 nsresult
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;
111 return NS_OK;
115 /*---------------------------------------------------------------------------
117 GetEditingSession
119 ----------------------------------------------------------------------------*/
120 nsresult
121 nsDocShellEditorData::GetEditingSession(nsIEditingSession **outEditingSession)
123 nsresult rv = EnsureEditingSession();
124 NS_ENSURE_SUCCESS(rv, rv);
126 NS_ADDREF(*outEditingSession = mEditingSession);
128 return NS_OK;
132 /*---------------------------------------------------------------------------
134 GetEditor
136 ----------------------------------------------------------------------------*/
137 nsresult
138 nsDocShellEditorData::GetEditor(nsIEditor **outEditor)
140 NS_ENSURE_ARG_POINTER(outEditor);
141 NS_IF_ADDREF(*outEditor = mEditor);
142 return NS_OK;
146 /*---------------------------------------------------------------------------
148 SetEditor
150 ----------------------------------------------------------------------------*/
151 nsresult
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)
159 if (mEditor)
161 mEditor->PreDestroy(false);
162 mEditor = nullptr;
165 mEditor = inEditor; // owning addref
166 if (!mEditor)
167 mMakeEditable = false;
170 return NS_OK;
174 /*---------------------------------------------------------------------------
176 EnsureEditingSession
178 This creates the editing session on the content docShell that owns
179 'this'.
181 ----------------------------------------------------------------------------*/
182 nsresult
183 nsDocShellEditorData::EnsureEditingSession()
185 NS_ASSERTION(mDocShell, "Should have docShell here");
186 NS_ASSERTION(!mIsDetached, "This will stomp editing session!");
188 nsresult rv = NS_OK;
190 if (!mEditingSession)
192 mEditingSession =
193 do_CreateInstance("@mozilla.org/editor/editingsession;1", &rv);
196 return rv;
199 nsresult
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);
210 mIsDetached = true;
211 mDetachedMakeEditable = mMakeEditable;
212 mMakeEditable = false;
214 nsCOMPtr<nsIDOMDocument> domDoc;
215 domWindow->GetDocument(getter_AddRefs(domDoc));
216 nsCOMPtr<nsIHTMLDocument> htmlDoc = do_QueryInterface(domDoc);
217 if (htmlDoc)
218 mDetachedEditingState = htmlDoc->GetEditingState();
220 mDocShell = nullptr;
222 return NS_OK;
225 nsresult
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);
235 mIsDetached = false;
236 mMakeEditable = mDetachedMakeEditable;
238 nsCOMPtr<nsIDOMDocument> domDoc;
239 domWindow->GetDocument(getter_AddRefs(domDoc));
240 nsCOMPtr<nsIHTMLDocument> htmlDoc = do_QueryInterface(domDoc);
241 if (htmlDoc)
242 htmlDoc->SetEditingState(mDetachedEditingState);
244 return NS_OK;