docshell/base/nsDocShellEditorData.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
michael@0 2 *
michael@0 3 * This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7
michael@0 8 #include "nsDocShellEditorData.h"
michael@0 9 #include "nsIInterfaceRequestorUtils.h"
michael@0 10 #include "nsComponentManagerUtils.h"
michael@0 11 #include "nsIDOMWindow.h"
michael@0 12 #include "nsIDOMDocument.h"
michael@0 13 #include "nsIEditor.h"
michael@0 14 #include "nsIEditingSession.h"
michael@0 15 #include "nsIDocShell.h"
michael@0 16
michael@0 17 /*---------------------------------------------------------------------------
michael@0 18
michael@0 19 nsDocShellEditorData
michael@0 20
michael@0 21 ----------------------------------------------------------------------------*/
michael@0 22
michael@0 23 nsDocShellEditorData::nsDocShellEditorData(nsIDocShell* inOwningDocShell)
michael@0 24 : mDocShell(inOwningDocShell)
michael@0 25 , mMakeEditable(false)
michael@0 26 , mIsDetached(false)
michael@0 27 , mDetachedMakeEditable(false)
michael@0 28 , mDetachedEditingState(nsIHTMLDocument::eOff)
michael@0 29 {
michael@0 30 NS_ASSERTION(mDocShell, "Where is my docShell?");
michael@0 31 }
michael@0 32
michael@0 33
michael@0 34 /*---------------------------------------------------------------------------
michael@0 35
michael@0 36 ~nsDocShellEditorData
michael@0 37
michael@0 38 ----------------------------------------------------------------------------*/
michael@0 39 nsDocShellEditorData::~nsDocShellEditorData()
michael@0 40 {
michael@0 41 TearDownEditor();
michael@0 42 }
michael@0 43
michael@0 44 void
michael@0 45 nsDocShellEditorData::TearDownEditor()
michael@0 46 {
michael@0 47 if (mEditor) {
michael@0 48 mEditor->PreDestroy(false);
michael@0 49 mEditor = nullptr;
michael@0 50 }
michael@0 51 mEditingSession = nullptr;
michael@0 52 mIsDetached = false;
michael@0 53 }
michael@0 54
michael@0 55
michael@0 56 /*---------------------------------------------------------------------------
michael@0 57
michael@0 58 MakeEditable
michael@0 59
michael@0 60 ----------------------------------------------------------------------------*/
michael@0 61 nsresult
michael@0 62 nsDocShellEditorData::MakeEditable(bool inWaitForUriLoad)
michael@0 63 {
michael@0 64 if (mMakeEditable)
michael@0 65 return NS_OK;
michael@0 66
michael@0 67 // if we are already editable, and are getting turned off,
michael@0 68 // nuke the editor.
michael@0 69 if (mEditor)
michael@0 70 {
michael@0 71 NS_WARNING("Destroying existing editor on frame");
michael@0 72
michael@0 73 mEditor->PreDestroy(false);
michael@0 74 mEditor = nullptr;
michael@0 75 }
michael@0 76
michael@0 77 if (inWaitForUriLoad)
michael@0 78 mMakeEditable = true;
michael@0 79 return NS_OK;
michael@0 80 }
michael@0 81
michael@0 82
michael@0 83 /*---------------------------------------------------------------------------
michael@0 84
michael@0 85 GetEditable
michael@0 86
michael@0 87 ----------------------------------------------------------------------------*/
michael@0 88 bool
michael@0 89 nsDocShellEditorData::GetEditable()
michael@0 90 {
michael@0 91 return mMakeEditable || (mEditor != nullptr);
michael@0 92 }
michael@0 93
michael@0 94 /*---------------------------------------------------------------------------
michael@0 95
michael@0 96 CreateEditor
michael@0 97
michael@0 98 ----------------------------------------------------------------------------*/
michael@0 99 nsresult
michael@0 100 nsDocShellEditorData::CreateEditor()
michael@0 101 {
michael@0 102 nsCOMPtr<nsIEditingSession> editingSession;
michael@0 103 nsresult rv = GetEditingSession(getter_AddRefs(editingSession));
michael@0 104 if (NS_FAILED(rv)) return rv;
michael@0 105
michael@0 106 nsCOMPtr<nsIDOMWindow> domWindow = do_GetInterface(mDocShell);
michael@0 107 rv = editingSession->SetupEditorOnWindow(domWindow);
michael@0 108 if (NS_FAILED(rv)) return rv;
michael@0 109
michael@0 110 return NS_OK;
michael@0 111 }
michael@0 112
michael@0 113
michael@0 114 /*---------------------------------------------------------------------------
michael@0 115
michael@0 116 GetEditingSession
michael@0 117
michael@0 118 ----------------------------------------------------------------------------*/
michael@0 119 nsresult
michael@0 120 nsDocShellEditorData::GetEditingSession(nsIEditingSession **outEditingSession)
michael@0 121 {
michael@0 122 nsresult rv = EnsureEditingSession();
michael@0 123 NS_ENSURE_SUCCESS(rv, rv);
michael@0 124
michael@0 125 NS_ADDREF(*outEditingSession = mEditingSession);
michael@0 126
michael@0 127 return NS_OK;
michael@0 128 }
michael@0 129
michael@0 130
michael@0 131 /*---------------------------------------------------------------------------
michael@0 132
michael@0 133 GetEditor
michael@0 134
michael@0 135 ----------------------------------------------------------------------------*/
michael@0 136 nsresult
michael@0 137 nsDocShellEditorData::GetEditor(nsIEditor **outEditor)
michael@0 138 {
michael@0 139 NS_ENSURE_ARG_POINTER(outEditor);
michael@0 140 NS_IF_ADDREF(*outEditor = mEditor);
michael@0 141 return NS_OK;
michael@0 142 }
michael@0 143
michael@0 144
michael@0 145 /*---------------------------------------------------------------------------
michael@0 146
michael@0 147 SetEditor
michael@0 148
michael@0 149 ----------------------------------------------------------------------------*/
michael@0 150 nsresult
michael@0 151 nsDocShellEditorData::SetEditor(nsIEditor *inEditor)
michael@0 152 {
michael@0 153 // destroy any editor that we have. Checks for equality are
michael@0 154 // necessary to ensure that assigment into the nsCOMPtr does
michael@0 155 // not temporarily reduce the refCount of the editor to zero
michael@0 156 if (mEditor.get() != inEditor)
michael@0 157 {
michael@0 158 if (mEditor)
michael@0 159 {
michael@0 160 mEditor->PreDestroy(false);
michael@0 161 mEditor = nullptr;
michael@0 162 }
michael@0 163
michael@0 164 mEditor = inEditor; // owning addref
michael@0 165 if (!mEditor)
michael@0 166 mMakeEditable = false;
michael@0 167 }
michael@0 168
michael@0 169 return NS_OK;
michael@0 170 }
michael@0 171
michael@0 172
michael@0 173 /*---------------------------------------------------------------------------
michael@0 174
michael@0 175 EnsureEditingSession
michael@0 176
michael@0 177 This creates the editing session on the content docShell that owns
michael@0 178 'this'.
michael@0 179
michael@0 180 ----------------------------------------------------------------------------*/
michael@0 181 nsresult
michael@0 182 nsDocShellEditorData::EnsureEditingSession()
michael@0 183 {
michael@0 184 NS_ASSERTION(mDocShell, "Should have docShell here");
michael@0 185 NS_ASSERTION(!mIsDetached, "This will stomp editing session!");
michael@0 186
michael@0 187 nsresult rv = NS_OK;
michael@0 188
michael@0 189 if (!mEditingSession)
michael@0 190 {
michael@0 191 mEditingSession =
michael@0 192 do_CreateInstance("@mozilla.org/editor/editingsession;1", &rv);
michael@0 193 }
michael@0 194
michael@0 195 return rv;
michael@0 196 }
michael@0 197
michael@0 198 nsresult
michael@0 199 nsDocShellEditorData::DetachFromWindow()
michael@0 200 {
michael@0 201 NS_ASSERTION(mEditingSession,
michael@0 202 "Can't detach when we don't have a session to detach!");
michael@0 203
michael@0 204 nsCOMPtr<nsIDOMWindow> domWindow = do_GetInterface(mDocShell);
michael@0 205 nsresult rv = mEditingSession->DetachFromWindow(domWindow);
michael@0 206 NS_ENSURE_SUCCESS(rv, rv);
michael@0 207
michael@0 208 mIsDetached = true;
michael@0 209 mDetachedMakeEditable = mMakeEditable;
michael@0 210 mMakeEditable = false;
michael@0 211
michael@0 212 nsCOMPtr<nsIDOMDocument> domDoc;
michael@0 213 domWindow->GetDocument(getter_AddRefs(domDoc));
michael@0 214 nsCOMPtr<nsIHTMLDocument> htmlDoc = do_QueryInterface(domDoc);
michael@0 215 if (htmlDoc)
michael@0 216 mDetachedEditingState = htmlDoc->GetEditingState();
michael@0 217
michael@0 218 mDocShell = nullptr;
michael@0 219
michael@0 220 return NS_OK;
michael@0 221 }
michael@0 222
michael@0 223 nsresult
michael@0 224 nsDocShellEditorData::ReattachToWindow(nsIDocShell* aDocShell)
michael@0 225 {
michael@0 226 mDocShell = aDocShell;
michael@0 227
michael@0 228 nsCOMPtr<nsIDOMWindow> domWindow = do_GetInterface(mDocShell);
michael@0 229 nsresult rv = mEditingSession->ReattachToWindow(domWindow);
michael@0 230 NS_ENSURE_SUCCESS(rv, rv);
michael@0 231
michael@0 232 mIsDetached = false;
michael@0 233 mMakeEditable = mDetachedMakeEditable;
michael@0 234
michael@0 235 nsCOMPtr<nsIDOMDocument> domDoc;
michael@0 236 domWindow->GetDocument(getter_AddRefs(domDoc));
michael@0 237 nsCOMPtr<nsIHTMLDocument> htmlDoc = do_QueryInterface(domDoc);
michael@0 238 if (htmlDoc)
michael@0 239 htmlDoc->SetEditingState(mDetachedEditingState);
michael@0 240
michael@0 241 return NS_OK;
michael@0 242 }

mercurial