2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
|
|
|
*
|
2012-05-21 04:12:37 -07:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* 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/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
|
|
|
|
#include "nsIComponentManager.h"
|
|
|
|
#include "nsIInterfaceRequestorUtils.h"
|
|
|
|
#include "nsIDOMWindow.h"
|
|
|
|
#include "nsIDocShellTreeItem.h"
|
2008-04-23 14:36:17 -07:00
|
|
|
#include "nsIDOMDocument.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsDocShellEditorData.h"
|
|
|
|
|
|
|
|
/*---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
nsDocShellEditorData
|
|
|
|
|
|
|
|
----------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
nsDocShellEditorData::nsDocShellEditorData(nsIDocShell* inOwningDocShell)
|
|
|
|
: mDocShell(inOwningDocShell)
|
2011-10-17 07:59:28 -07:00
|
|
|
, mMakeEditable(false)
|
|
|
|
, mIsDetached(false)
|
|
|
|
, mDetachedMakeEditable(false)
|
2009-02-24 12:06:34 -08:00
|
|
|
, mDetachedEditingState(nsIHTMLDocument::eOff)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(mDocShell, "Where is my docShell?");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
~nsDocShellEditorData
|
|
|
|
|
|
|
|
----------------------------------------------------------------------------*/
|
|
|
|
nsDocShellEditorData::~nsDocShellEditorData()
|
2007-12-02 18:07:16 -08:00
|
|
|
{
|
|
|
|
TearDownEditor();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsDocShellEditorData::TearDownEditor()
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2008-04-23 14:36:17 -07:00
|
|
|
if (mEditor) {
|
2011-10-17 07:59:28 -07:00
|
|
|
mEditor->PreDestroy(false);
|
2008-04-23 14:36:17 -07:00
|
|
|
mEditor = nsnull;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2008-04-23 14:36:17 -07:00
|
|
|
mEditingSession = nsnull;
|
2011-10-17 07:59:28 -07:00
|
|
|
mIsDetached = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
MakeEditable
|
|
|
|
|
|
|
|
----------------------------------------------------------------------------*/
|
|
|
|
nsresult
|
2011-09-28 23:19:26 -07:00
|
|
|
nsDocShellEditorData::MakeEditable(bool inWaitForUriLoad)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
if (mMakeEditable)
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
// if we are already editable, and are getting turned off,
|
|
|
|
// nuke the editor.
|
|
|
|
if (mEditor)
|
|
|
|
{
|
|
|
|
NS_WARNING("Destroying existing editor on frame");
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
mEditor->PreDestroy(false);
|
2007-03-22 10:30:00 -07:00
|
|
|
mEditor = nsnull;
|
|
|
|
}
|
|
|
|
|
2007-06-27 19:48:16 -07:00
|
|
|
if (inWaitForUriLoad)
|
2011-10-17 07:59:28 -07:00
|
|
|
mMakeEditable = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
GetEditable
|
|
|
|
|
|
|
|
----------------------------------------------------------------------------*/
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2007-03-22 10:30:00 -07:00
|
|
|
nsDocShellEditorData::GetEditable()
|
|
|
|
{
|
|
|
|
return mMakeEditable || (mEditor != nsnull);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
CreateEditor
|
|
|
|
|
|
|
|
----------------------------------------------------------------------------*/
|
|
|
|
nsresult
|
|
|
|
nsDocShellEditorData::CreateEditor()
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIEditingSession> editingSession;
|
|
|
|
nsresult rv = GetEditingSession(getter_AddRefs(editingSession));
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMWindow> domWindow = do_GetInterface(mDocShell);
|
|
|
|
rv = editingSession->SetupEditorOnWindow(domWindow);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
GetEditingSession
|
|
|
|
|
|
|
|
----------------------------------------------------------------------------*/
|
|
|
|
nsresult
|
|
|
|
nsDocShellEditorData::GetEditingSession(nsIEditingSession **outEditingSession)
|
|
|
|
{
|
|
|
|
nsresult rv = EnsureEditingSession();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
NS_ADDREF(*outEditingSession = mEditingSession);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
GetEditor
|
|
|
|
|
|
|
|
----------------------------------------------------------------------------*/
|
|
|
|
nsresult
|
|
|
|
nsDocShellEditorData::GetEditor(nsIEditor **outEditor)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(outEditor);
|
|
|
|
NS_IF_ADDREF(*outEditor = mEditor);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
SetEditor
|
|
|
|
|
|
|
|
----------------------------------------------------------------------------*/
|
|
|
|
nsresult
|
|
|
|
nsDocShellEditorData::SetEditor(nsIEditor *inEditor)
|
|
|
|
{
|
|
|
|
// 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
|
|
|
|
if (mEditor.get() != inEditor)
|
|
|
|
{
|
|
|
|
if (mEditor)
|
|
|
|
{
|
2011-10-17 07:59:28 -07:00
|
|
|
mEditor->PreDestroy(false);
|
2007-03-22 10:30:00 -07:00
|
|
|
mEditor = nsnull;
|
|
|
|
}
|
|
|
|
|
|
|
|
mEditor = inEditor; // owning addref
|
2007-06-27 19:48:16 -07:00
|
|
|
if (!mEditor)
|
2011-10-17 07:59:28 -07:00
|
|
|
mMakeEditable = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
EnsureEditingSession
|
|
|
|
|
|
|
|
This creates the editing session on the content docShell that owns
|
|
|
|
'this'.
|
|
|
|
|
|
|
|
----------------------------------------------------------------------------*/
|
|
|
|
nsresult
|
|
|
|
nsDocShellEditorData::EnsureEditingSession()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(mDocShell, "Should have docShell here");
|
2008-04-23 14:36:17 -07:00
|
|
|
NS_ASSERTION(!mIsDetached, "This will stomp editing session!");
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
|
|
|
|
if (!mEditingSession)
|
|
|
|
{
|
|
|
|
mEditingSession =
|
|
|
|
do_CreateInstance("@mozilla.org/editor/editingsession;1", &rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2008-04-23 14:36:17 -07:00
|
|
|
nsresult
|
|
|
|
nsDocShellEditorData::DetachFromWindow()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(mEditingSession,
|
|
|
|
"Can't detach when we don't have a session to detach!");
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMWindow> domWindow = do_GetInterface(mDocShell);
|
|
|
|
nsresult rv = mEditingSession->DetachFromWindow(domWindow);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
mIsDetached = true;
|
2008-04-23 14:36:17 -07:00
|
|
|
mDetachedMakeEditable = mMakeEditable;
|
2011-10-17 07:59:28 -07:00
|
|
|
mMakeEditable = false;
|
2008-04-23 14:36:17 -07:00
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMDocument> domDoc;
|
|
|
|
domWindow->GetDocument(getter_AddRefs(domDoc));
|
|
|
|
nsCOMPtr<nsIHTMLDocument> htmlDoc = do_QueryInterface(domDoc);
|
|
|
|
if (htmlDoc)
|
|
|
|
mDetachedEditingState = htmlDoc->GetEditingState();
|
|
|
|
|
2008-05-02 04:36:29 -07:00
|
|
|
mDocShell = nsnull;
|
|
|
|
|
2008-04-23 14:36:17 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2008-05-02 04:36:29 -07:00
|
|
|
nsDocShellEditorData::ReattachToWindow(nsIDocShell* aDocShell)
|
2008-04-23 14:36:17 -07:00
|
|
|
{
|
2008-05-02 04:36:29 -07:00
|
|
|
mDocShell = aDocShell;
|
|
|
|
|
2008-04-23 14:36:17 -07:00
|
|
|
nsCOMPtr<nsIDOMWindow> domWindow = do_GetInterface(mDocShell);
|
|
|
|
nsresult rv = mEditingSession->ReattachToWindow(domWindow);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
mIsDetached = false;
|
2008-04-23 14:36:17 -07:00
|
|
|
mMakeEditable = mDetachedMakeEditable;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMDocument> domDoc;
|
|
|
|
domWindow->GetDocument(getter_AddRefs(domDoc));
|
|
|
|
nsCOMPtr<nsIHTMLDocument> htmlDoc = do_QueryInterface(domDoc);
|
|
|
|
if (htmlDoc)
|
|
|
|
htmlDoc->SetEditingState(mDetachedEditingState);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|