2015-02-13 22:36:47 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
2012-05-21 15:12:37 +04:00
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2002-01-30 07:44:14 +03:00
|
|
|
|
2013-09-04 00:17:26 +04:00
|
|
|
#include "nsDocShellEditorData.h"
|
2002-01-30 07:44:14 +03:00
|
|
|
#include "nsIInterfaceRequestorUtils.h"
|
2013-09-04 00:17:26 +04:00
|
|
|
#include "nsComponentManagerUtils.h"
|
2014-01-10 06:03:47 +04:00
|
|
|
#include "nsPIDOMWindow.h"
|
2013-09-04 00:17:26 +04:00
|
|
|
#include "nsIEditor.h"
|
2018-09-08 19:38:01 +03:00
|
|
|
#include "nsEditingSession.h"
|
2013-09-04 00:17:26 +04:00
|
|
|
#include "nsIDocShell.h"
|
2002-01-30 07:44:14 +03:00
|
|
|
|
2017-08-04 15:42:13 +03:00
|
|
|
using namespace mozilla;
|
|
|
|
|
2015-02-13 22:36:37 +03:00
|
|
|
nsDocShellEditorData::nsDocShellEditorData(nsIDocShell* aOwningDocShell)
|
|
|
|
: mDocShell(aOwningDocShell),
|
2017-08-04 15:42:13 +03:00
|
|
|
mDetachedEditingState(nsIHTMLDocument::eOff),
|
2015-02-13 22:36:37 +03:00
|
|
|
mMakeEditable(false),
|
|
|
|
mIsDetached(false),
|
|
|
|
mDetachedMakeEditable(false) {
|
2002-01-30 07:44:14 +03:00
|
|
|
NS_ASSERTION(mDocShell, "Where is my docShell?");
|
|
|
|
}
|
|
|
|
|
|
|
|
nsDocShellEditorData::~nsDocShellEditorData() { TearDownEditor(); }
|
2007-12-03 05:07:16 +03:00
|
|
|
|
|
|
|
void nsDocShellEditorData::TearDownEditor() {
|
2017-08-04 15:42:13 +03:00
|
|
|
if (mHTMLEditor) {
|
|
|
|
RefPtr<HTMLEditor> htmlEditor = mHTMLEditor.forget();
|
|
|
|
htmlEditor->PreDestroy(false);
|
2002-01-30 07:44:14 +03:00
|
|
|
}
|
2012-07-30 18:20:58 +04:00
|
|
|
mEditingSession = nullptr;
|
2011-10-17 18:59:28 +04:00
|
|
|
mIsDetached = false;
|
2002-01-30 07:44:14 +03:00
|
|
|
}
|
|
|
|
|
2015-02-13 22:36:37 +03:00
|
|
|
nsresult nsDocShellEditorData::MakeEditable(bool aInWaitForUriLoad) {
|
|
|
|
if (mMakeEditable) {
|
2002-01-30 07:44:14 +03:00
|
|
|
return NS_OK;
|
2015-02-13 22:36:37 +03:00
|
|
|
}
|
|
|
|
|
2002-01-30 07:44:14 +03:00
|
|
|
// if we are already editable, and are getting turned off,
|
|
|
|
// nuke the editor.
|
2017-08-04 15:42:13 +03:00
|
|
|
if (mHTMLEditor) {
|
2002-01-30 07:44:14 +03:00
|
|
|
NS_WARNING("Destroying existing editor on frame");
|
2015-02-13 22:36:37 +03:00
|
|
|
|
2017-08-04 15:42:13 +03:00
|
|
|
RefPtr<HTMLEditor> htmlEditor = mHTMLEditor.forget();
|
|
|
|
htmlEditor->PreDestroy(false);
|
2002-01-30 07:44:14 +03:00
|
|
|
}
|
2015-02-13 22:36:37 +03:00
|
|
|
|
|
|
|
if (aInWaitForUriLoad) {
|
2011-10-17 18:59:28 +04:00
|
|
|
mMakeEditable = true;
|
2015-02-13 22:36:37 +03:00
|
|
|
}
|
2002-01-30 07:44:14 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool nsDocShellEditorData::GetEditable() {
|
2017-08-04 15:42:13 +03:00
|
|
|
return mMakeEditable || (mHTMLEditor != nullptr);
|
2002-01-30 07:44:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult nsDocShellEditorData::CreateEditor() {
|
2015-02-13 22:36:37 +03:00
|
|
|
nsCOMPtr<nsIEditingSession> editingSession;
|
2002-01-30 07:44:14 +03:00
|
|
|
nsresult rv = GetEditingSession(getter_AddRefs(editingSession));
|
2015-02-13 22:36:37 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2016-01-30 20:05:36 +03:00
|
|
|
nsCOMPtr<nsPIDOMWindowOuter> domWindow =
|
2014-01-10 06:03:47 +04:00
|
|
|
mDocShell ? mDocShell->GetWindow() : nullptr;
|
2002-01-30 07:44:14 +03:00
|
|
|
rv = editingSession->SetupEditorOnWindow(domWindow);
|
2015-02-13 22:36:37 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2002-01-30 07:44:14 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-02-13 22:36:37 +03:00
|
|
|
nsresult nsDocShellEditorData::GetEditingSession(nsIEditingSession** aResult) {
|
2018-09-08 19:38:01 +03:00
|
|
|
EnsureEditingSession();
|
2005-02-24 00:27:20 +03:00
|
|
|
|
2015-02-13 22:36:37 +03:00
|
|
|
NS_ADDREF(*aResult = mEditingSession);
|
2005-02-24 00:27:20 +03:00
|
|
|
|
|
|
|
return NS_OK;
|
2002-01-30 07:44:14 +03:00
|
|
|
}
|
|
|
|
|
2017-08-04 15:42:13 +03:00
|
|
|
nsresult nsDocShellEditorData::SetHTMLEditor(HTMLEditor* aHTMLEditor) {
|
2002-01-30 07:44:14 +03:00
|
|
|
// destroy any editor that we have. Checks for equality are
|
|
|
|
// necessary to ensure that assigment into the nsCOMPtr does
|
|
|
|
// not temporarily reduce the refCount of the editor to zero
|
2017-08-04 15:42:13 +03:00
|
|
|
if (mHTMLEditor == aHTMLEditor) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mHTMLEditor) {
|
|
|
|
RefPtr<HTMLEditor> htmlEditor = mHTMLEditor.forget();
|
|
|
|
htmlEditor->PreDestroy(false);
|
|
|
|
MOZ_ASSERT(!mHTMLEditor,
|
|
|
|
"Nested call of nsDocShellEditorData::SetHTMLEditor() detected");
|
|
|
|
}
|
|
|
|
|
|
|
|
mHTMLEditor = aHTMLEditor; // owning addref
|
|
|
|
if (!mHTMLEditor) {
|
|
|
|
mMakeEditable = false;
|
2015-02-13 22:36:37 +03:00
|
|
|
}
|
|
|
|
|
2002-01-30 07:44:14 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-02-13 22:36:37 +03:00
|
|
|
// This creates the editing session on the content docShell that owns 'this'.
|
2005-02-24 00:27:20 +03:00
|
|
|
void nsDocShellEditorData::EnsureEditingSession() {
|
2002-01-30 07:44:14 +03:00
|
|
|
NS_ASSERTION(mDocShell, "Should have docShell here");
|
2008-04-24 01:36:17 +04:00
|
|
|
NS_ASSERTION(!mIsDetached, "This will stomp editing session!");
|
2015-02-13 22:36:37 +03:00
|
|
|
|
|
|
|
if (!mEditingSession) {
|
2018-09-08 19:38:01 +03:00
|
|
|
mEditingSession = new nsEditingSession();
|
2002-01-30 07:44:14 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-24 01:36:17 +04:00
|
|
|
nsresult nsDocShellEditorData::DetachFromWindow() {
|
|
|
|
NS_ASSERTION(mEditingSession,
|
|
|
|
"Can't detach when we don't have a session to detach!");
|
2015-02-13 22:36:37 +03:00
|
|
|
|
2016-01-30 20:05:36 +03:00
|
|
|
nsCOMPtr<nsPIDOMWindowOuter> domWindow =
|
2014-01-10 06:03:47 +04:00
|
|
|
mDocShell ? mDocShell->GetWindow() : nullptr;
|
2008-04-24 01:36:17 +04:00
|
|
|
nsresult rv = mEditingSession->DetachFromWindow(domWindow);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
mIsDetached = true;
|
2008-04-24 01:36:17 +04:00
|
|
|
mDetachedMakeEditable = mMakeEditable;
|
2011-10-17 18:59:28 +04:00
|
|
|
mMakeEditable = false;
|
2008-04-24 01:36:17 +04:00
|
|
|
|
2016-01-30 20:05:36 +03:00
|
|
|
nsCOMPtr<nsIDocument> doc = domWindow->GetDoc();
|
2015-10-27 00:37:32 +03:00
|
|
|
nsCOMPtr<nsIHTMLDocument> htmlDoc = do_QueryInterface(doc);
|
2015-02-13 22:36:37 +03:00
|
|
|
if (htmlDoc) {
|
2008-04-24 01:36:17 +04:00
|
|
|
mDetachedEditingState = htmlDoc->GetEditingState();
|
2015-02-13 22:36:37 +03:00
|
|
|
}
|
2008-04-24 01:36:17 +04:00
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
mDocShell = nullptr;
|
2008-05-02 15:36:29 +04:00
|
|
|
|
2008-04-24 01:36:17 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2008-05-02 15:36:29 +04:00
|
|
|
nsresult nsDocShellEditorData::ReattachToWindow(nsIDocShell* aDocShell) {
|
|
|
|
mDocShell = aDocShell;
|
|
|
|
|
2016-01-30 20:05:36 +03:00
|
|
|
nsCOMPtr<nsPIDOMWindowOuter> domWindow =
|
2014-01-10 06:03:47 +04:00
|
|
|
mDocShell ? mDocShell->GetWindow() : nullptr;
|
2008-04-24 01:36:17 +04:00
|
|
|
nsresult rv = mEditingSession->ReattachToWindow(domWindow);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
mIsDetached = false;
|
2008-04-24 01:36:17 +04:00
|
|
|
mMakeEditable = mDetachedMakeEditable;
|
|
|
|
|
2016-01-30 20:05:36 +03:00
|
|
|
nsCOMPtr<nsIDocument> doc = domWindow->GetDoc();
|
2015-10-27 00:37:32 +03:00
|
|
|
nsCOMPtr<nsIHTMLDocument> htmlDoc = do_QueryInterface(doc);
|
2015-02-13 22:36:37 +03:00
|
|
|
if (htmlDoc) {
|
2008-04-24 01:36:17 +04:00
|
|
|
htmlDoc->SetEditingState(mDetachedEditingState);
|
2015-02-13 22:36:37 +03:00
|
|
|
}
|
|
|
|
|
2008-04-24 01:36:17 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|