tor-browser

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

nsRefreshObservers.cpp (1812B)


      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 #include "nsRefreshObservers.h"
      8 
      9 #include "mozilla/Likely.h"
     10 #include "nsPresContext.h"
     11 
     12 namespace mozilla {
     13 
     14 ManagedPostRefreshObserver::ManagedPostRefreshObserver(nsPresContext* aPc,
     15                                                       Action&& aAction)
     16    : mPresContext(aPc), mAction(std::move(aAction)) {}
     17 
     18 ManagedPostRefreshObserver::ManagedPostRefreshObserver(nsPresContext* aPc)
     19    : mPresContext(aPc) {
     20  MOZ_ASSERT(mPresContext, "Can't observe without a nsPresContext");
     21 }
     22 
     23 ManagedPostRefreshObserver::~ManagedPostRefreshObserver() = default;
     24 
     25 void ManagedPostRefreshObserver::Cancel() {
     26  // Caller holds a strong reference, so no need to reference stuff from here.
     27  if (mAction) {
     28    mAction(true);
     29  }
     30  mAction = nullptr;
     31  mPresContext = nullptr;
     32 }
     33 
     34 void ManagedPostRefreshObserver::DidRefresh() {
     35  if (MOZ_UNLIKELY(!mAction)) {
     36    return;
     37  }
     38 
     39  RefPtr<ManagedPostRefreshObserver> thisObject = this;
     40  auto action = std::move(mAction);
     41  Unregister unregister = action(false);
     42 
     43  if (unregister == Unregister::Yes) {
     44    if (RefPtr<nsPresContext> pc = std::move(mPresContext)) {
     45      // We have to null-check mPresContext here because in theory, mAction
     46      // could've ended up in `Cancel` being called, which would clear
     47      // mPresContext. In that case, we're already unregistered so no need to
     48      // do anything.
     49      pc->UnregisterManagedPostRefreshObserver(this);
     50    }
     51  } else {
     52    mAction = std::move(action);
     53  }
     54 }
     55 
     56 }  // namespace mozilla