tor-browser

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

LineBreakCache.cpp (1428B)


      1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* This Source Code Form is subject to the terms of the Mozilla Public
      3 * License, v. 2.0. If a copy of the MPL was not distributed with this
      4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      5 
      6 #include "LineBreakCache.h"
      7 
      8 #include "nsIObserverService.h"
      9 #include "mozilla/Services.h"
     10 
     11 using namespace mozilla;
     12 using namespace mozilla::intl;
     13 
     14 StaticAutoPtr<LineBreakCache> LineBreakCache::sBreakCache;
     15 
     16 void LineBreakCache::Initialize() {
     17  MOZ_ASSERT(NS_IsMainThread());
     18 
     19  nsCOMPtr<nsIObserverService> obs = services::GetObserverService();
     20  if (obs) {
     21    obs->AddObserver(new LineBreakCache::Observer(), "memory-pressure", false);
     22  }
     23 }
     24 
     25 void LineBreakCache::Shutdown() {
     26  MOZ_ASSERT(NS_IsMainThread());
     27  sBreakCache = nullptr;
     28 }
     29 
     30 NS_IMPL_ISUPPORTS(LineBreakCache::Observer, nsIObserver)
     31 
     32 NS_IMETHODIMP LineBreakCache::Observer::Observe(nsISupports* aSubject,
     33                                                const char* aTopic,
     34                                                const char16_t* aData) {
     35  MOZ_ASSERT(NS_IsMainThread());
     36 
     37  if (strcmp(aTopic, "memory-pressure") == 0) {
     38    // We don't delete the cache itself, as it would almost certainly just
     39    // get immediately re-created; just clear its contents.
     40    if (LineBreakCache::sBreakCache) {
     41      LineBreakCache::sBreakCache->Clear();
     42    }
     43  }
     44 
     45  return NS_OK;
     46 }