tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

PostTraversalTask.h (3893B)


      1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
      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/. */
      6 
      7 #ifndef mozilla_PostTraversalTask_h
      8 #define mozilla_PostTraversalTask_h
      9 
     10 #include "nsString.h"
     11 #include "nscore.h"
     12 
     13 /* a task to be performed immediately after a Servo traversal */
     14 
     15 namespace mozilla {
     16 class ServoStyleSet;
     17 namespace dom {
     18 enum class FontFaceLoadedRejectReason : uint8_t;
     19 class FontFace;
     20 class FontFaceSet;
     21 class FontFaceSetImpl;
     22 }  // namespace dom
     23 namespace fontlist {
     24 struct Family;
     25 }  // namespace fontlist
     26 }  // namespace mozilla
     27 class gfxUserFontEntry;
     28 
     29 namespace mozilla {
     30 
     31 /**
     32 * A PostTraversalTask is a task to be performed immediately after a Servo
     33 * traversal.  There are just a few tasks we need to perform, so we use this
     34 * class rather than Runnables, to avoid virtual calls and some allocations.
     35 *
     36 * A PostTraversalTask is only safe to run immediately after the Servo
     37 * traversal, since it can hold raw pointers to DOM objects.
     38 */
     39 class PostTraversalTask {
     40 public:
     41  static PostTraversalTask ResolveFontFaceLoadedPromise(
     42      dom::FontFace* aFontFace) {
     43    auto task = PostTraversalTask(Type::ResolveFontFaceLoadedPromise);
     44    task.mTarget = aFontFace;
     45    return task;
     46  }
     47 
     48  static PostTraversalTask RejectFontFaceLoadedPromise(
     49      dom::FontFace* aFontFace, dom::FontFaceLoadedRejectReason aReason,
     50      nsCString&& aMessage) {
     51    auto task = PostTraversalTask(Type::ResolveFontFaceLoadedPromise);
     52    task.mTarget = aFontFace;
     53    task.mResult.emplace(aReason);
     54    task.mMessage = std::move(aMessage);
     55    return task;
     56  }
     57 
     58  static PostTraversalTask DispatchLoadingEventAndReplaceReadyPromise(
     59      dom::FontFaceSet* aFontFaceSet) {
     60    auto task =
     61        PostTraversalTask(Type::DispatchLoadingEventAndReplaceReadyPromise);
     62    task.mTarget = aFontFaceSet;
     63    return task;
     64  }
     65 
     66  static PostTraversalTask DispatchFontFaceSetCheckLoadingFinishedAfterDelay(
     67      dom::FontFaceSetImpl* aFontFaceSet) {
     68    auto task = PostTraversalTask(
     69        Type::DispatchFontFaceSetCheckLoadingFinishedAfterDelay);
     70    task.mTarget = aFontFaceSet;
     71    return task;
     72  }
     73 
     74  static PostTraversalTask LoadFontEntry(gfxUserFontEntry* aFontEntry) {
     75    auto task = PostTraversalTask(Type::LoadFontEntry);
     76    task.mTarget = aFontEntry;
     77    return task;
     78  }
     79 
     80  static PostTraversalTask InitializeFamily(fontlist::Family* aFamily) {
     81    auto task = PostTraversalTask(Type::InitializeFamily);
     82    task.mTarget = aFamily;
     83    return task;
     84  }
     85 
     86  static PostTraversalTask FontInfoUpdate(ServoStyleSet* aSet) {
     87    auto task = PostTraversalTask(Type::FontInfoUpdate);
     88    task.mTarget = aSet;
     89    return task;
     90  }
     91 
     92  void Run();
     93 
     94 private:
     95  // For any new raw pointer type that we need to store in a PostTraversalTask,
     96  // please add an assertion that class' destructor that we are not in a Servo
     97  // traversal, to protect against the possibility of having dangling pointers.
     98  enum class Type {
     99    // mTarget (FontFace*)
    100    ResolveFontFaceLoadedPromise,
    101 
    102    // mTarget (FontFace*)
    103    // mResult / mMessage
    104    RejectFontFaceLoadedPromise,
    105 
    106    // mTarget (FontFaceSet*)
    107    DispatchLoadingEventAndReplaceReadyPromise,
    108 
    109    // mTarget (FontFaceSetImpl*)
    110    DispatchFontFaceSetCheckLoadingFinishedAfterDelay,
    111 
    112    // mTarget (gfxUserFontEntry*)
    113    LoadFontEntry,
    114 
    115    // mTarget (fontlist::Family*)
    116    InitializeFamily,
    117 
    118    // mTarget (ServoStyleSet*)
    119    FontInfoUpdate,
    120  };
    121 
    122  explicit PostTraversalTask(Type aType) : mType(aType) {}
    123 
    124  const Type mType;
    125  void* mTarget = nullptr;
    126  nsCString mMessage;
    127  Maybe<dom::FontFaceLoadedRejectReason> mResult;
    128 };
    129 
    130 }  // namespace mozilla
    131 
    132 #endif  // mozilla_PostTraversalTask_h