accessible/xpcom/xpcAccessibleDocument.h
author L. David Baron <dbaron@dbaron.org>
Thu, 11 Oct 2018 10:04:04 +0000
changeset 499519 e816bcd461abb456bad50c9df457d7a0fdcbe83b
parent 474897 1b58640450884d282a25f7318c8ce115d6eba31c
child 508163 6f3709b3878117466168c40affa7bca0b60cf75b
permissions -rw-r--r--
Bug 1498030 [wpt PR 13459] - Sync Mozilla CSS tests as of 2018-10-10, a=testonly Automatic update from web-platform-testsSync Mozilla CSS tests as of https://hg.mozilla.org/mozilla-central/rev/91b4c3687d7563244fbba0f58075779eb89259fb . (#13459) This contains changes from [bug 1495470](https://bugzilla.mozilla.org/show_bug.cgi?id=1495470) by @dholbert, reviewed by me. -- wpt-commits: ad906e0d1ad2225635cde0b676575f84255348cf wpt-pr: 13459

/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=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/. */

#ifndef mozilla_a11y_xpcAccessibleDocument_h_
#define mozilla_a11y_xpcAccessibleDocument_h_

#include "nsIAccessibleDocument.h"

#include "DocAccessible.h"
#include "nsAccessibilityService.h"
#include "xpcAccessibleApplication.h"
#include "xpcAccessibleHyperText.h"

namespace mozilla {
namespace a11y {

/**
 * XPCOM wrapper around DocAccessible class.
 */
class xpcAccessibleDocument : public xpcAccessibleHyperText,
                              public nsIAccessibleDocument
{
public:
  explicit xpcAccessibleDocument(DocAccessible* aIntl) :
    xpcAccessibleHyperText(aIntl), mCache(kDefaultCacheLength), mRemote(false) { }

  xpcAccessibleDocument(ProxyAccessible* aProxy, uint32_t aInterfaces) :
    xpcAccessibleHyperText(aProxy, aInterfaces), mCache(kDefaultCacheLength),
    mRemote(true) {}

  NS_DECL_ISUPPORTS_INHERITED

  // nsIAccessibleDocument
  NS_IMETHOD GetURL(nsAString& aURL) final;
  NS_IMETHOD GetTitle(nsAString& aTitle) final;
  NS_IMETHOD GetMimeType(nsAString& aType) final;
  NS_IMETHOD GetDocType(nsAString& aType) final;
  NS_IMETHOD GetDOMDocument(nsIDocument** aDOMDocument) final;
  NS_IMETHOD GetWindow(mozIDOMWindowProxy** aDOMWindow) final;
  NS_IMETHOD GetParentDocument(nsIAccessibleDocument** aDocument)
    final;
  NS_IMETHOD GetChildDocumentCount(uint32_t* aCount) final;
  NS_IMETHOD GetChildDocumentAt(uint32_t aIndex,
                                nsIAccessibleDocument** aDocument)
    final;
  NS_IMETHOD GetVirtualCursor(nsIAccessiblePivot** aVirtualCursor)
    final;

  /**
   * Return XPCOM wrapper for the internal accessible.
   */
  xpcAccessibleGeneric* GetAccessible(Accessible* aAccessible);
  xpcAccessibleGeneric* GetXPCAccessible(ProxyAccessible* aProxy);

  virtual void Shutdown() override;

protected:
  virtual ~xpcAccessibleDocument() {}

private:
  DocAccessible* Intl()
  {
    if (Accessible* acc = mIntl.AsAccessible()) {
      return acc->AsDoc();
    }

    return nullptr;
  }

  void NotifyOfShutdown(Accessible* aAccessible)
  {
    MOZ_ASSERT(!mRemote);
    xpcAccessibleGeneric* xpcAcc = mCache.Get(aAccessible);
    if (xpcAcc) {
      xpcAcc->Shutdown();
    }

    mCache.Remove(aAccessible);
    if (mCache.Count() == 0 && mRefCnt == 1) {
      GetAccService()->RemoveFromXPCDocumentCache(
        mIntl.AsAccessible()->AsDoc());
    }
  }

  void NotifyOfShutdown(ProxyAccessible* aProxy)
  {
    MOZ_ASSERT(mRemote);
    xpcAccessibleGeneric* xpcAcc = mCache.Get(aProxy);
    if (xpcAcc) {
      xpcAcc->Shutdown();
    }

    mCache.Remove(aProxy);
    if (mCache.Count() == 0 && mRefCnt == 1) {
      GetAccService()->RemoveFromRemoteXPCDocumentCache(
        mIntl.AsProxy()->AsDoc());
    }
  }

  friend class DocManager;
  friend class DocAccessible;
  friend class ProxyAccessible;
  friend class ProxyAccessibleBase<ProxyAccessible>;
  friend class xpcAccessibleGeneric;

  xpcAccessibleDocument(const xpcAccessibleDocument&) = delete;
  xpcAccessibleDocument& operator =(const xpcAccessibleDocument&) = delete;

  nsDataHashtable<nsPtrHashKey<const void>, xpcAccessibleGeneric*> mCache;
  bool mRemote;
};

inline xpcAccessibleGeneric*
ToXPC(Accessible* aAccessible)
{
  if (!aAccessible)
    return nullptr;

  if (aAccessible->IsApplication())
    return XPCApplicationAcc();

  xpcAccessibleDocument* xpcDoc =
    GetAccService()->GetXPCDocument(aAccessible->Document());
  return xpcDoc ? xpcDoc->GetAccessible(aAccessible) : nullptr;
}

xpcAccessibleGeneric* ToXPC(AccessibleOrProxy aAcc);

inline xpcAccessibleHyperText*
ToXPCText(HyperTextAccessible* aAccessible)
{
  if (!aAccessible)
    return nullptr;

  xpcAccessibleDocument* xpcDoc =
    GetAccService()->GetXPCDocument(aAccessible->Document());
  return static_cast<xpcAccessibleHyperText*>(xpcDoc->GetAccessible(aAccessible));
}

inline xpcAccessibleDocument*
ToXPCDocument(DocAccessible* aAccessible)
{
  return GetAccService()->GetXPCDocument(aAccessible);
}

inline xpcAccessibleDocument*
ToXPCDocument(DocAccessibleParent* aAccessible)
{
  return GetAccService()->GetXPCDocument(aAccessible);
}

} // namespace a11y
} // namespace mozilla

#endif