Source code

Revision control

Other Tools

1
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
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/. */
6
7
#include "nsDocShellEditorData.h"
8
#include "nsIInterfaceRequestorUtils.h"
9
#include "nsComponentManagerUtils.h"
10
#include "nsPIDOMWindow.h"
11
#include "nsEditingSession.h"
12
#include "nsIDocShell.h"
13
14
using namespace mozilla;
15
16
nsDocShellEditorData::nsDocShellEditorData(nsIDocShell* aOwningDocShell)
17
: mDocShell(aOwningDocShell),
18
mDetachedEditingState(Document::EditingState::eOff),
19
mMakeEditable(false),
20
mIsDetached(false),
21
mDetachedMakeEditable(false) {
22
NS_ASSERTION(mDocShell, "Where is my docShell?");
23
}
24
25
nsDocShellEditorData::~nsDocShellEditorData() { TearDownEditor(); }
26
27
void nsDocShellEditorData::TearDownEditor() {
28
if (mHTMLEditor) {
29
RefPtr<HTMLEditor> htmlEditor = std::move(mHTMLEditor);
30
htmlEditor->PreDestroy(false);
31
}
32
mEditingSession = nullptr;
33
mIsDetached = false;
34
}
35
36
nsresult nsDocShellEditorData::MakeEditable(bool aInWaitForUriLoad) {
37
if (mMakeEditable) {
38
return NS_OK;
39
}
40
41
// if we are already editable, and are getting turned off,
42
// nuke the editor.
43
if (mHTMLEditor) {
44
NS_WARNING("Destroying existing editor on frame");
45
46
RefPtr<HTMLEditor> htmlEditor = std::move(mHTMLEditor);
47
htmlEditor->PreDestroy(false);
48
}
49
50
if (aInWaitForUriLoad) {
51
mMakeEditable = true;
52
}
53
return NS_OK;
54
}
55
56
bool nsDocShellEditorData::GetEditable() {
57
return mMakeEditable || (mHTMLEditor != nullptr);
58
}
59
60
nsEditingSession* nsDocShellEditorData::GetEditingSession() {
61
EnsureEditingSession();
62
63
return mEditingSession.get();
64
}
65
66
nsresult nsDocShellEditorData::SetHTMLEditor(HTMLEditor* aHTMLEditor) {
67
// destroy any editor that we have. Checks for equality are
68
// necessary to ensure that assigment into the nsCOMPtr does
69
// not temporarily reduce the refCount of the editor to zero
70
if (mHTMLEditor == aHTMLEditor) {
71
return NS_OK;
72
}
73
74
if (mHTMLEditor) {
75
RefPtr<HTMLEditor> htmlEditor = std::move(mHTMLEditor);
76
htmlEditor->PreDestroy(false);
77
MOZ_ASSERT(!mHTMLEditor,
78
"Nested call of nsDocShellEditorData::SetHTMLEditor() detected");
79
}
80
81
mHTMLEditor = aHTMLEditor; // owning addref
82
if (!mHTMLEditor) {
83
mMakeEditable = false;
84
}
85
86
return NS_OK;
87
}
88
89
// This creates the editing session on the content docShell that owns 'this'.
90
void nsDocShellEditorData::EnsureEditingSession() {
91
NS_ASSERTION(mDocShell, "Should have docShell here");
92
NS_ASSERTION(!mIsDetached, "This will stomp editing session!");
93
94
if (!mEditingSession) {
95
mEditingSession = new nsEditingSession();
96
}
97
}
98
99
nsresult nsDocShellEditorData::DetachFromWindow() {
100
NS_ASSERTION(mEditingSession,
101
"Can't detach when we don't have a session to detach!");
102
103
nsCOMPtr<nsPIDOMWindowOuter> domWindow =
104
mDocShell ? mDocShell->GetWindow() : nullptr;
105
nsresult rv = mEditingSession->DetachFromWindow(domWindow);
106
NS_ENSURE_SUCCESS(rv, rv);
107
108
mIsDetached = true;
109
mDetachedMakeEditable = mMakeEditable;
110
mMakeEditable = false;
111
112
nsCOMPtr<dom::Document> doc = domWindow->GetDoc();
113
mDetachedEditingState = doc->GetEditingState();
114
115
mDocShell = nullptr;
116
117
return NS_OK;
118
}
119
120
nsresult nsDocShellEditorData::ReattachToWindow(nsIDocShell* aDocShell) {
121
mDocShell = aDocShell;
122
123
nsCOMPtr<nsPIDOMWindowOuter> domWindow =
124
mDocShell ? mDocShell->GetWindow() : nullptr;
125
nsresult rv = mEditingSession->ReattachToWindow(domWindow);
126
NS_ENSURE_SUCCESS(rv, rv);
127
128
mIsDetached = false;
129
mMakeEditable = mDetachedMakeEditable;
130
131
RefPtr<dom::Document> doc = domWindow->GetDoc();
132
doc->SetEditingState(mDetachedEditingState);
133
134
return NS_OK;
135
}