1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
3 * ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
16 * The Original Code is the Mozilla browser.
18 * The Initial Developer of the Original Code is
19 * Netscape Communications, Inc.
20 * Portions created by the Initial Developer are Copyright (C) 1999
21 * the Initial Developer. All Rights Reserved.
24 * Simon Fraser <sfraser@netscape.com>
26 * Alternatively, the contents of this file may be used under the terms of
27 * either of the GNU General Public License Version 2 or later (the "GPL"),
28 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 * in which case the provisions of the GPL or the LGPL are applicable instead
30 * of those above. If you wish to allow use of your version of this file only
31 * under the terms of either the GPL or the LGPL, and not to allow others to
32 * use your version of this file under the terms of the MPL, indicate your
33 * decision by deleting the provisions above and replace them with the notice
34 * and other provisions required by the GPL or the LGPL. If you do not delete
35 * the provisions above, a recipient may use your version of this file under
36 * the terms of any one of the MPL, the GPL or the LGPL.
38 * ***** END LICENSE BLOCK ***** */
41 #include "nsIComponentManager.h"
42 #include "nsIInterfaceRequestorUtils.h"
44 #include "nsIDOMWindow.h"
45 #include "nsIDocShellTreeItem.h"
47 #include "nsDocShellEditorData.h"
50 /*---------------------------------------------------------------------------
54 ----------------------------------------------------------------------------*/
56 nsDocShellEditorData::nsDocShellEditorData(nsIDocShell
* inOwningDocShell
)
57 : mDocShell(inOwningDocShell
)
58 , mMakeEditable(PR_FALSE
)
60 NS_ASSERTION(mDocShell
, "Where is my docShell?");
64 /*---------------------------------------------------------------------------
68 ----------------------------------------------------------------------------*/
69 nsDocShellEditorData::~nsDocShellEditorData()
75 nsDocShellEditorData::TearDownEditor()
79 nsCOMPtr
<nsIDOMWindow
> domWindow
= do_GetInterface(mDocShell
);
80 // This will eventually call nsDocShellEditorData::SetEditor(nsnull)
81 // which will call mEditorPreDestroy() and delete the editor
82 mEditingSession
->TearDownEditorOnWindow(domWindow
);
84 else if (mEditor
) // Should never have this w/o nsEditingSession!
86 mEditor
->PreDestroy();
87 mEditor
= nsnull
; // explicit clear to make destruction order predictable
92 /*---------------------------------------------------------------------------
96 ----------------------------------------------------------------------------*/
98 nsDocShellEditorData::MakeEditable(PRBool inWaitForUriLoad
/*, PRBool inEditable */)
103 // if we are already editable, and are getting turned off,
107 NS_WARNING("Destroying existing editor on frame");
109 mEditor
->PreDestroy();
113 if (inWaitForUriLoad
)
114 mMakeEditable
= PR_TRUE
;
119 /*---------------------------------------------------------------------------
123 ----------------------------------------------------------------------------*/
125 nsDocShellEditorData::GetEditable()
127 return mMakeEditable
|| (mEditor
!= nsnull
);
130 /*---------------------------------------------------------------------------
134 ----------------------------------------------------------------------------*/
136 nsDocShellEditorData::CreateEditor()
138 nsCOMPtr
<nsIEditingSession
> editingSession
;
139 nsresult rv
= GetEditingSession(getter_AddRefs(editingSession
));
140 if (NS_FAILED(rv
)) return rv
;
142 nsCOMPtr
<nsIDOMWindow
> domWindow
= do_GetInterface(mDocShell
);
143 rv
= editingSession
->SetupEditorOnWindow(domWindow
);
144 if (NS_FAILED(rv
)) return rv
;
150 /*---------------------------------------------------------------------------
154 ----------------------------------------------------------------------------*/
156 nsDocShellEditorData::GetEditingSession(nsIEditingSession
**outEditingSession
)
158 nsresult rv
= EnsureEditingSession();
159 NS_ENSURE_SUCCESS(rv
, rv
);
161 NS_ADDREF(*outEditingSession
= mEditingSession
);
167 /*---------------------------------------------------------------------------
171 ----------------------------------------------------------------------------*/
173 nsDocShellEditorData::GetEditor(nsIEditor
**outEditor
)
175 NS_ENSURE_ARG_POINTER(outEditor
);
176 NS_IF_ADDREF(*outEditor
= mEditor
);
181 /*---------------------------------------------------------------------------
185 ----------------------------------------------------------------------------*/
187 nsDocShellEditorData::SetEditor(nsIEditor
*inEditor
)
189 // destroy any editor that we have. Checks for equality are
190 // necessary to ensure that assigment into the nsCOMPtr does
191 // not temporarily reduce the refCount of the editor to zero
192 if (mEditor
.get() != inEditor
)
196 mEditor
->PreDestroy();
200 mEditor
= inEditor
; // owning addref
202 mMakeEditable
= PR_FALSE
;
209 /*---------------------------------------------------------------------------
213 This creates the editing session on the content docShell that owns
216 ----------------------------------------------------------------------------*/
218 nsDocShellEditorData::EnsureEditingSession()
220 NS_ASSERTION(mDocShell
, "Should have docShell here");
224 if (!mEditingSession
)
227 do_CreateInstance("@mozilla.org/editor/editingsession;1", &rv
);