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.

     1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
     2  *
     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/. */
     8 #include "nsDocShellEditorData.h"
     9 #include "nsIInterfaceRequestorUtils.h"
    10 #include "nsComponentManagerUtils.h"
    11 #include "nsIDOMWindow.h"
    12 #include "nsIDOMDocument.h"
    13 #include "nsIEditor.h"
    14 #include "nsIEditingSession.h"
    15 #include "nsIDocShell.h"
    17 /*---------------------------------------------------------------------------
    19   nsDocShellEditorData
    21 ----------------------------------------------------------------------------*/
    23 nsDocShellEditorData::nsDocShellEditorData(nsIDocShell* inOwningDocShell)
    24 : mDocShell(inOwningDocShell)
    25 , mMakeEditable(false)
    26 , mIsDetached(false)
    27 , mDetachedMakeEditable(false)
    28 , mDetachedEditingState(nsIHTMLDocument::eOff)
    29 {
    30   NS_ASSERTION(mDocShell, "Where is my docShell?");
    31 }
    34 /*---------------------------------------------------------------------------
    36   ~nsDocShellEditorData
    38 ----------------------------------------------------------------------------*/
    39 nsDocShellEditorData::~nsDocShellEditorData()
    40 {
    41   TearDownEditor();
    42 }
    44 void
    45 nsDocShellEditorData::TearDownEditor()
    46 {
    47   if (mEditor) {
    48     mEditor->PreDestroy(false);
    49     mEditor = nullptr;
    50   }
    51   mEditingSession = nullptr;
    52   mIsDetached = false;
    53 }
    56 /*---------------------------------------------------------------------------
    58   MakeEditable
    60 ----------------------------------------------------------------------------*/
    61 nsresult
    62 nsDocShellEditorData::MakeEditable(bool inWaitForUriLoad)
    63 {
    64   if (mMakeEditable)
    65     return NS_OK;
    67   // if we are already editable, and are getting turned off,
    68   // nuke the editor.
    69   if (mEditor)
    70   {
    71     NS_WARNING("Destroying existing editor on frame");
    73     mEditor->PreDestroy(false);
    74     mEditor = nullptr;
    75   }
    77   if (inWaitForUriLoad)
    78     mMakeEditable = true;
    79   return NS_OK;
    80 }
    83 /*---------------------------------------------------------------------------
    85   GetEditable
    87 ----------------------------------------------------------------------------*/
    88 bool
    89 nsDocShellEditorData::GetEditable()
    90 {
    91   return mMakeEditable || (mEditor != nullptr);
    92 }
    94 /*---------------------------------------------------------------------------
    96   CreateEditor
    98 ----------------------------------------------------------------------------*/
    99 nsresult
   100 nsDocShellEditorData::CreateEditor()
   101 {
   102   nsCOMPtr<nsIEditingSession>   editingSession;    
   103   nsresult rv = GetEditingSession(getter_AddRefs(editingSession));
   104   if (NS_FAILED(rv)) return rv;
   106   nsCOMPtr<nsIDOMWindow>    domWindow = do_GetInterface(mDocShell);
   107   rv = editingSession->SetupEditorOnWindow(domWindow);
   108   if (NS_FAILED(rv)) return rv;
   110   return NS_OK;
   111 }
   114 /*---------------------------------------------------------------------------
   116   GetEditingSession
   118 ----------------------------------------------------------------------------*/
   119 nsresult
   120 nsDocShellEditorData::GetEditingSession(nsIEditingSession **outEditingSession)
   121 {
   122   nsresult rv = EnsureEditingSession();
   123   NS_ENSURE_SUCCESS(rv, rv);
   125   NS_ADDREF(*outEditingSession = mEditingSession);
   127   return NS_OK;
   128 }
   131 /*---------------------------------------------------------------------------
   133   GetEditor
   135 ----------------------------------------------------------------------------*/
   136 nsresult
   137 nsDocShellEditorData::GetEditor(nsIEditor **outEditor)
   138 {
   139   NS_ENSURE_ARG_POINTER(outEditor);
   140   NS_IF_ADDREF(*outEditor = mEditor);
   141   return NS_OK;
   142 }
   145 /*---------------------------------------------------------------------------
   147   SetEditor
   149 ----------------------------------------------------------------------------*/
   150 nsresult
   151 nsDocShellEditorData::SetEditor(nsIEditor *inEditor)
   152 {
   153   // destroy any editor that we have. Checks for equality are
   154   // necessary to ensure that assigment into the nsCOMPtr does
   155   // not temporarily reduce the refCount of the editor to zero
   156   if (mEditor.get() != inEditor)
   157   {
   158     if (mEditor)
   159     {
   160       mEditor->PreDestroy(false);
   161       mEditor = nullptr;
   162     }
   164     mEditor = inEditor;    // owning addref
   165     if (!mEditor)
   166       mMakeEditable = false;
   167   }   
   169   return NS_OK;
   170 }
   173 /*---------------------------------------------------------------------------
   175   EnsureEditingSession
   177   This creates the editing session on the content docShell that owns
   178   'this'.
   180 ----------------------------------------------------------------------------*/
   181 nsresult
   182 nsDocShellEditorData::EnsureEditingSession()
   183 {
   184   NS_ASSERTION(mDocShell, "Should have docShell here");
   185   NS_ASSERTION(!mIsDetached, "This will stomp editing session!");
   187   nsresult rv = NS_OK;
   189   if (!mEditingSession)
   190   {
   191     mEditingSession =
   192       do_CreateInstance("@mozilla.org/editor/editingsession;1", &rv);
   193   }
   195   return rv;
   196 }
   198 nsresult
   199 nsDocShellEditorData::DetachFromWindow()
   200 {
   201   NS_ASSERTION(mEditingSession,
   202                "Can't detach when we don't have a session to detach!");
   204   nsCOMPtr<nsIDOMWindow> domWindow = do_GetInterface(mDocShell);
   205   nsresult rv = mEditingSession->DetachFromWindow(domWindow);
   206   NS_ENSURE_SUCCESS(rv, rv);
   208   mIsDetached = true;
   209   mDetachedMakeEditable = mMakeEditable;
   210   mMakeEditable = false;
   212   nsCOMPtr<nsIDOMDocument> domDoc;
   213   domWindow->GetDocument(getter_AddRefs(domDoc));
   214   nsCOMPtr<nsIHTMLDocument> htmlDoc = do_QueryInterface(domDoc);
   215   if (htmlDoc)
   216     mDetachedEditingState = htmlDoc->GetEditingState();
   218   mDocShell = nullptr;
   220   return NS_OK;
   221 }
   223 nsresult
   224 nsDocShellEditorData::ReattachToWindow(nsIDocShell* aDocShell)
   225 {
   226   mDocShell = aDocShell;
   228   nsCOMPtr<nsIDOMWindow> domWindow = do_GetInterface(mDocShell);
   229   nsresult rv = mEditingSession->ReattachToWindow(domWindow);
   230   NS_ENSURE_SUCCESS(rv, rv);
   232   mIsDetached = false;
   233   mMakeEditable = mDetachedMakeEditable;
   235   nsCOMPtr<nsIDOMDocument> domDoc;
   236   domWindow->GetDocument(getter_AddRefs(domDoc));
   237   nsCOMPtr<nsIHTMLDocument> htmlDoc = do_QueryInterface(domDoc);
   238   if (htmlDoc)
   239     htmlDoc->SetEditingState(mDetachedEditingState);
   241   return NS_OK;
   242 }

mercurial