docshell/base/nsDocShellEditorData.cpp
author Iain Ireland <iireland@mozilla.com>
Fri, 11 Jan 2019 18:05:36 +0000
changeset 453523 025feea5945bffc625e9cca2e23be51ee8670315
parent 452446 f0a91d36587266d7454a450c6044d573664fbed5
child 466740 447c87c2d1396aa1d0980d023067f1629d972c25
permissions -rw-r--r--
Bug 1480390: Move ForOfIterClose logic inside TryNoteIter r=tcampbell This patch was intended to be a pure refactoring of existing code with no side-effects, moving the logic for handling for-of/for-of-iterclose trynotes inside TryNoteIter to avoid duplicating logic in all users of TryNoteIter. However, it turns out that there was a subtle preexisting bug in TryNoteIter that is fixed by the refactoring. Specifically, the logic to skip from a for-of-iterclose to its enclosing for-of must run before the logic to skip trynotes based on stack depth. Otherwise, the stack depth code may filter out the enclosing for-of (see the attached test case for an example) and we will skip too many try-notes. Differential Revision: https://phabricator.services.mozilla.com/D14783

/* -*- 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
 * 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/. */

#include "nsDocShellEditorData.h"
#include "nsIInterfaceRequestorUtils.h"
#include "nsComponentManagerUtils.h"
#include "nsPIDOMWindow.h"
#include "nsIEditor.h"
#include "nsEditingSession.h"
#include "nsIDocShell.h"

using namespace mozilla;

nsDocShellEditorData::nsDocShellEditorData(nsIDocShell* aOwningDocShell)
    : mDocShell(aOwningDocShell),
      mDetachedEditingState(nsIHTMLDocument::eOff),
      mMakeEditable(false),
      mIsDetached(false),
      mDetachedMakeEditable(false) {
  NS_ASSERTION(mDocShell, "Where is my docShell?");
}

nsDocShellEditorData::~nsDocShellEditorData() { TearDownEditor(); }

void nsDocShellEditorData::TearDownEditor() {
  if (mHTMLEditor) {
    RefPtr<HTMLEditor> htmlEditor = mHTMLEditor.forget();
    htmlEditor->PreDestroy(false);
  }
  mEditingSession = nullptr;
  mIsDetached = false;
}

nsresult nsDocShellEditorData::MakeEditable(bool aInWaitForUriLoad) {
  if (mMakeEditable) {
    return NS_OK;
  }

  // if we are already editable, and are getting turned off,
  // nuke the editor.
  if (mHTMLEditor) {
    NS_WARNING("Destroying existing editor on frame");

    RefPtr<HTMLEditor> htmlEditor = mHTMLEditor.forget();
    htmlEditor->PreDestroy(false);
  }

  if (aInWaitForUriLoad) {
    mMakeEditable = true;
  }
  return NS_OK;
}

bool nsDocShellEditorData::GetEditable() {
  return mMakeEditable || (mHTMLEditor != nullptr);
}

nsresult nsDocShellEditorData::CreateEditor() {
  nsCOMPtr<nsIEditingSession> editingSession;
  nsresult rv = GetEditingSession(getter_AddRefs(editingSession));
  if (NS_FAILED(rv)) {
    return rv;
  }

  nsCOMPtr<nsPIDOMWindowOuter> domWindow =
      mDocShell ? mDocShell->GetWindow() : nullptr;
  rv = editingSession->SetupEditorOnWindow(domWindow);
  if (NS_FAILED(rv)) {
    return rv;
  }

  return NS_OK;
}

nsresult nsDocShellEditorData::GetEditingSession(nsIEditingSession** aResult) {
  EnsureEditingSession();

  NS_ADDREF(*aResult = mEditingSession);

  return NS_OK;
}

nsresult nsDocShellEditorData::SetHTMLEditor(HTMLEditor* aHTMLEditor) {
  // 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 (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;
  }

  return NS_OK;
}

// This creates the editing session on the content docShell that owns 'this'.
void nsDocShellEditorData::EnsureEditingSession() {
  NS_ASSERTION(mDocShell, "Should have docShell here");
  NS_ASSERTION(!mIsDetached, "This will stomp editing session!");

  if (!mEditingSession) {
    mEditingSession = new nsEditingSession();
  }
}

nsresult nsDocShellEditorData::DetachFromWindow() {
  NS_ASSERTION(mEditingSession,
               "Can't detach when we don't have a session to detach!");

  nsCOMPtr<nsPIDOMWindowOuter> domWindow =
      mDocShell ? mDocShell->GetWindow() : nullptr;
  nsresult rv = mEditingSession->DetachFromWindow(domWindow);
  NS_ENSURE_SUCCESS(rv, rv);

  mIsDetached = true;
  mDetachedMakeEditable = mMakeEditable;
  mMakeEditable = false;

  nsCOMPtr<dom::Document> doc = domWindow->GetDoc();
  nsCOMPtr<nsIHTMLDocument> htmlDoc = do_QueryInterface(doc);
  if (htmlDoc) {
    mDetachedEditingState = htmlDoc->GetEditingState();
  }

  mDocShell = nullptr;

  return NS_OK;
}

nsresult nsDocShellEditorData::ReattachToWindow(nsIDocShell* aDocShell) {
  mDocShell = aDocShell;

  nsCOMPtr<nsPIDOMWindowOuter> domWindow =
      mDocShell ? mDocShell->GetWindow() : nullptr;
  nsresult rv = mEditingSession->ReattachToWindow(domWindow);
  NS_ENSURE_SUCCESS(rv, rv);

  mIsDetached = false;
  mMakeEditable = mDetachedMakeEditable;

  RefPtr<dom::Document> doc = domWindow->GetDoc();
  nsCOMPtr<nsIHTMLDocument> htmlDoc = do_QueryInterface(doc);
  if (htmlDoc) {
    htmlDoc->SetEditingState(mDetachedEditingState);
  }

  return NS_OK;
}