bug 423036. decommit for huge allocations. patch from Jason Evans. r/sr=me a=vlad
[mozilla-central.git] / docshell / base / nsDocShellEditorData.cpp
blob9b69dce988771e96df1034b96039a252dda8ed24
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
14 * License.
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.
23 * Contributor(s):
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 /*---------------------------------------------------------------------------
52 nsDocShellEditorData
54 ----------------------------------------------------------------------------*/
56 nsDocShellEditorData::nsDocShellEditorData(nsIDocShell* inOwningDocShell)
57 : mDocShell(inOwningDocShell)
58 , mMakeEditable(PR_FALSE)
60 NS_ASSERTION(mDocShell, "Where is my docShell?");
64 /*---------------------------------------------------------------------------
66 ~nsDocShellEditorData
68 ----------------------------------------------------------------------------*/
69 nsDocShellEditorData::~nsDocShellEditorData()
71 TearDownEditor();
74 void
75 nsDocShellEditorData::TearDownEditor()
77 if (mEditingSession)
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 /*---------------------------------------------------------------------------
94 MakeEditable
96 ----------------------------------------------------------------------------*/
97 nsresult
98 nsDocShellEditorData::MakeEditable(PRBool inWaitForUriLoad /*, PRBool inEditable */)
100 if (mMakeEditable)
101 return NS_OK;
103 // if we are already editable, and are getting turned off,
104 // nuke the editor.
105 if (mEditor)
107 NS_WARNING("Destroying existing editor on frame");
109 mEditor->PreDestroy();
110 mEditor = nsnull;
113 if (inWaitForUriLoad)
114 mMakeEditable = PR_TRUE;
115 return NS_OK;
119 /*---------------------------------------------------------------------------
121 GetEditable
123 ----------------------------------------------------------------------------*/
124 PRBool
125 nsDocShellEditorData::GetEditable()
127 return mMakeEditable || (mEditor != nsnull);
130 /*---------------------------------------------------------------------------
132 CreateEditor
134 ----------------------------------------------------------------------------*/
135 nsresult
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;
146 return NS_OK;
150 /*---------------------------------------------------------------------------
152 GetEditingSession
154 ----------------------------------------------------------------------------*/
155 nsresult
156 nsDocShellEditorData::GetEditingSession(nsIEditingSession **outEditingSession)
158 nsresult rv = EnsureEditingSession();
159 NS_ENSURE_SUCCESS(rv, rv);
161 NS_ADDREF(*outEditingSession = mEditingSession);
163 return NS_OK;
167 /*---------------------------------------------------------------------------
169 GetEditor
171 ----------------------------------------------------------------------------*/
172 nsresult
173 nsDocShellEditorData::GetEditor(nsIEditor **outEditor)
175 NS_ENSURE_ARG_POINTER(outEditor);
176 NS_IF_ADDREF(*outEditor = mEditor);
177 return NS_OK;
181 /*---------------------------------------------------------------------------
183 SetEditor
185 ----------------------------------------------------------------------------*/
186 nsresult
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)
194 if (mEditor)
196 mEditor->PreDestroy();
197 mEditor = nsnull;
200 mEditor = inEditor; // owning addref
201 if (!mEditor)
202 mMakeEditable = PR_FALSE;
205 return NS_OK;
209 /*---------------------------------------------------------------------------
211 EnsureEditingSession
213 This creates the editing session on the content docShell that owns
214 'this'.
216 ----------------------------------------------------------------------------*/
217 nsresult
218 nsDocShellEditorData::EnsureEditingSession()
220 NS_ASSERTION(mDocShell, "Should have docShell here");
222 nsresult rv = NS_OK;
224 if (!mEditingSession)
226 mEditingSession =
227 do_CreateInstance("@mozilla.org/editor/editingsession;1", &rv);
230 return rv;