dom/base/FullscreenChange.h
author Xidorn Quan <me@upsuper.org>
Mon, 17 Sep 2018 23:20:28 +0000
changeset 492641 824f566f9043419551c6d035441928d6ba12ffac
parent 492141 dom/base/FullscreenRequest.h@0665a323aec7354788b6be532a21ac79da3a3ff8
child 492642 32b021561f8cfd5e6911dfacb4372aa538d2b615
permissions -rw-r--r--
Bug 1491212 part 2 - Have a new FullscreenChange superclass split from FullscreenRequest. r=smaug The next patch would create another subclass of FullscreenChange for handling fullscreen exit. Depends on D5988 Differential Revision: https://phabricator.services.mozilla.com/D5989

/* -*- 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 https://mozilla.org/MPL/2.0/. */

/*
 * Struct for holding fullscreen request.
 */

#ifndef mozilla_FullscreenRequest_h
#define mozilla_FullscreenRequest_h

#include "mozilla/LinkedList.h"
#include "mozilla/PendingFullscreenEvent.h"
#include "mozilla/UniquePtr.h"
#include "mozilla/dom/Element.h"
#include "mozilla/dom/Promise.h"
#include "nsIDocument.h"
#include "nsIScriptError.h"

namespace mozilla {

class FullscreenChange : public LinkedListElement<FullscreenChange>
{
public:
  FullscreenChange(const FullscreenChange&) = delete;

  enum ChangeType
  {
    eEnter,
    eExit,
  };

  ChangeType Type() const { return mType; }
  nsIDocument* Document() const { return mDocument; }
  dom::Promise* GetPromise() const { return mPromise; }

  void MayResolvePromise() const
  {
    if (mPromise) {
      MOZ_ASSERT(mPromise->State() == Promise::PromiseState::Pending);
      mPromise->MaybeResolveWithUndefined();
    }
  }

  void MayRejectPromise() const
  {
    if (mPromise) {
      MOZ_ASSERT(mPromise->State() == Promise::PromiseState::Pending);
      mPromise->MaybeReject(NS_ERROR_DOM_TYPE_ERR);
    }
  }

protected:
  typedef dom::Promise Promise;

  FullscreenChange(ChangeType aType, nsIDocument* aDocument,
                   already_AddRefed<Promise> aPromise)
    : mType(aType)
    , mDocument(aDocument)
    , mPromise(aPromise)
  {
    MOZ_ASSERT(aDocument);
  }

  ~FullscreenChange()
  {
    MOZ_ASSERT_IF(mPromise,
                  mPromise->State() != Promise::PromiseState::Pending);
  }

private:
  ChangeType mType;
  nsCOMPtr<nsIDocument> mDocument;
  RefPtr<Promise> mPromise;
};

class FullscreenRequest : public FullscreenChange
{
public:
  static const ChangeType kType = eEnter;

  static UniquePtr<FullscreenRequest> Create(Element* aElement,
                                             dom::CallerType aCallerType,
                                             ErrorResult& aRv)
  {
    RefPtr<Promise> promise = Promise::Create(aElement->GetOwnerGlobal(), aRv);
    return WrapUnique(new FullscreenRequest(aElement, promise.forget(),
                                            aCallerType, true));
  }

  static UniquePtr<FullscreenRequest> CreateForRemote(Element* aElement)
  {
    return WrapUnique(new FullscreenRequest(aElement, nullptr,
                                            dom::CallerType::NonSystem,
                                            false));
  }

  ~FullscreenRequest()
  {
    MOZ_COUNT_DTOR(FullscreenRequest);
  }

  dom::Element* Element() const { return mElement; }

  // Reject the fullscreen request with the given reason.
  // It will dispatch the fullscreenerror event.
  void Reject(const char* aReason) const
  {
    if (nsPresContext* presContext = Document()->GetPresContext()) {
      auto pendingEvent = MakeUnique<PendingFullscreenEvent>(
          FullscreenEventType::Error, Document(), mElement);
      presContext->RefreshDriver()->
        ScheduleFullscreenEvent(std::move(pendingEvent));
    }
    MayRejectPromise();
    nsContentUtils::ReportToConsole(nsIScriptError::warningFlag,
                                    NS_LITERAL_CSTRING("DOM"),
                                    Document(),
                                    nsContentUtils::eDOM_PROPERTIES,
                                    aReason);
  }

private:
  RefPtr<dom::Element> mElement;

public:
  // This value should be true if the fullscreen request is
  // originated from system code.
  const dom::CallerType mCallerType;
  // This value denotes whether we should trigger a NewOrigin event if
  // requesting fullscreen in its document causes the origin which is
  // fullscreen to change. We may want *not* to trigger that event if
  // we're calling RequestFullscreen() as part of a continuation of a
  // request in a subdocument in different process, whereupon the caller
  // need to send some notification itself with the real origin.
  const bool mShouldNotifyNewOrigin;

private:
  FullscreenRequest(dom::Element* aElement,
                    already_AddRefed<dom::Promise> aPromise,
                    dom::CallerType aCallerType,
                    bool aShouldNotifyNewOrigin)
    : FullscreenChange(kType, aElement->OwnerDoc(), std::move(aPromise))
    , mElement(aElement)
    , mCallerType(aCallerType)
    , mShouldNotifyNewOrigin(aShouldNotifyNewOrigin)
  {
    MOZ_COUNT_CTOR(FullscreenRequest);
  }
};

} // namespace mozilla

#endif // mozilla_FullscreenRequest_h