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"
|
2008-04-24 01:36:17 +04:00
|
|
|
#include "nsIDOMDocument.h"
|
2013-09-04 00:17:26 +04:00
|
|
|
#include "nsIEditor.h"
|
|
|
|
#include "nsIEditingSession.h"
|
|
|
|
#include "nsIDocShell.h"
|
2002-01-30 07:44:14 +03:00
|
|
|
|
2015-02-13 22:36:37 +03:00
|
|
|
nsDocShellEditorData::nsDocShellEditorData(nsIDocShell* aOwningDocShell)
|
|
|
|
: mDocShell(aOwningDocShell)
|
|
|
|
, mMakeEditable(false)
|
|
|
|
, mIsDetached(false)
|
|
|
|
, mDetachedMakeEditable(false)
|
|
|
|
, mDetachedEditingState(nsIHTMLDocument::eOff)
|
2002-01-30 07:44:14 +03:00
|
|
|
{
|
|
|
|
NS_ASSERTION(mDocShell, "Where is my docShell?");
|
|
|
|
}
|
|
|
|
|
|
|
|
nsDocShellEditorData::~nsDocShellEditorData()
|
2007-12-03 05:07:16 +03:00
|
|
|
{
|
|
|
|
TearDownEditor();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsDocShellEditorData::TearDownEditor()
|
2002-01-30 07:44:14 +03:00
|
|
|
{
|
2008-04-24 01:36:17 +04:00
|
|
|
if (mEditor) {
|
2011-10-17 18:59:28 +04:00
|
|
|
mEditor->PreDestroy(false);
|
2012-07-30 18:20:58 +04:00
|
|
|
mEditor = nullptr;
|
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
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2015-02-13 22:36:37 +03:00
|
|
|
nsDocShellEditorData::MakeEditable(bool aInWaitForUriLoad)
|
2002-01-30 07:44:14 +03:00
|
|
|
{
|
2015-02-13 22:36:37 +03:00
|
|
|
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.
|
2015-02-13 22:36:37 +03:00
|
|
|
if (mEditor) {
|
2002-01-30 07:44:14 +03:00
|
|
|
NS_WARNING("Destroying existing editor on frame");
|
2015-02-13 22:36:37 +03:00
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
mEditor->PreDestroy(false);
|
2012-07-30 18:20:58 +04:00
|
|
|
mEditor = nullptr;
|
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;
|
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2002-01-30 07:44:14 +03:00
|
|
|
nsDocShellEditorData::GetEditable()
|
|
|
|
{
|
2012-07-30 18:20:58 +04:00
|
|
|
return mMakeEditable || (mEditor != 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMWindow> 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2015-02-13 22:36:37 +03:00
|
|
|
nsDocShellEditorData::GetEditingSession(nsIEditingSession** aResult)
|
2002-01-30 07:44:14 +03:00
|
|
|
{
|
2005-02-24 00:27:20 +03:00
|
|
|
nsresult rv = EnsureEditingSession();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2015-02-13 22:36:37 +03:00
|
|
|
nsDocShellEditorData::GetEditor(nsIEditor** aResult)
|
2002-01-30 07:44:14 +03:00
|
|
|
{
|
2015-02-13 22:36:37 +03:00
|
|
|
NS_ENSURE_ARG_POINTER(aResult);
|
|
|
|
NS_IF_ADDREF(*aResult = mEditor);
|
2002-01-30 07:44:14 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2015-02-13 22:36:37 +03:00
|
|
|
nsDocShellEditorData::SetEditor(nsIEditor* aEditor)
|
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
|
2015-02-13 22:36:37 +03:00
|
|
|
if (mEditor.get() != aEditor) {
|
|
|
|
if (mEditor) {
|
2011-10-17 18:59:28 +04:00
|
|
|
mEditor->PreDestroy(false);
|
2012-07-30 18:20:58 +04:00
|
|
|
mEditor = nullptr;
|
2002-01-30 07:44:14 +03:00
|
|
|
}
|
2015-02-13 22:36:37 +03:00
|
|
|
|
|
|
|
mEditor = aEditor; // owning addref
|
|
|
|
if (!mEditor) {
|
2011-10-17 18:59:28 +04:00
|
|
|
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'.
|
2002-01-30 07:44:14 +03:00
|
|
|
nsresult
|
2005-02-24 00:27:20 +03:00
|
|
|
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
|
|
|
|
2002-01-30 07:44:14 +03:00
|
|
|
nsresult rv = NS_OK;
|
2015-02-13 22:36:37 +03:00
|
|
|
|
|
|
|
if (!mEditingSession) {
|
2005-02-24 00:27:20 +03:00
|
|
|
mEditingSession =
|
|
|
|
do_CreateInstance("@mozilla.org/editor/editingsession;1", &rv);
|
2002-01-30 07:44:14 +03:00
|
|
|
}
|
2005-02-24 00:27:20 +03:00
|
|
|
|
|
|
|
return rv;
|
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
|
|
|
|
2014-01-10 06:03:47 +04:00
|
|
|
nsCOMPtr<nsIDOMWindow> domWindow =
|
|
|
|
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
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMDocument> domDoc;
|
|
|
|
domWindow->GetDocument(getter_AddRefs(domDoc));
|
|
|
|
nsCOMPtr<nsIHTMLDocument> htmlDoc = do_QueryInterface(domDoc);
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2008-05-02 15:36:29 +04:00
|
|
|
nsDocShellEditorData::ReattachToWindow(nsIDocShell* aDocShell)
|
2008-04-24 01:36:17 +04:00
|
|
|
{
|
2008-05-02 15:36:29 +04:00
|
|
|
mDocShell = aDocShell;
|
|
|
|
|
2014-01-10 06:03:47 +04:00
|
|
|
nsCOMPtr<nsIDOMWindow> domWindow =
|
|
|
|
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;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMDocument> domDoc;
|
|
|
|
domWindow->GetDocument(getter_AddRefs(domDoc));
|
|
|
|
nsCOMPtr<nsIHTMLDocument> htmlDoc = do_QueryInterface(domDoc);
|
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;
|
|
|
|
}
|