blob: 0730adec37e923f41cc138f9badbf2791ee26237 [file] [log] [blame]
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/macros.h"
#include "third_party/blink/public/platform/web_memory_pressure_level.h"
#include "third_party/blink/renderer/platform/heap/handle.h"
#include "third_party/blink/renderer/platform/platform_export.h"
#include "third_party/blink/renderer/platform/scheduler/public/thread.h"
#include "third_party/blink/renderer/platform/wtf/threading_primitives.h"
namespace blink {
class PLATFORM_EXPORT MemoryPressureListener : public GarbageCollectedMixin {
virtual ~MemoryPressureListener() = default;
virtual void OnMemoryPressure(WebMemoryPressureLevel) {}
virtual void OnPurgeMemory() {}
// MemoryPressureListenerRegistry listens to some events which could be
// opportunities for reducing memory consumption and notifies its clients.
class PLATFORM_EXPORT MemoryPressureListenerRegistry final
: public GarbageCollectedFinalized<MemoryPressureListenerRegistry> {
static MemoryPressureListenerRegistry& Instance();
// Whether the device Blink runs on is a low-end device.
// Can be overridden in web tests via internals.
static bool IsLowEndDevice();
// Returns true when available memory is low.
// This is not cheap and should not be called repeatedly.
static bool IsCurrentlyLowMemory();
// Caches whether this device is a low-end device and the device physical
// memory in static members. instance() is not used as it's a heap allocated
// object - meaning it's not thread-safe as well as might break tests counting
// the heap size.
static void Initialize();
void RegisterThread(Thread*) LOCKS_EXCLUDED(threads_mutex_);
void UnregisterThread(Thread*) LOCKS_EXCLUDED(threads_mutex_);
void RegisterClient(MemoryPressureListener*);
void UnregisterClient(MemoryPressureListener*);
void OnMemoryPressure(WebMemoryPressureLevel);
void OnPurgeMemory();
void Trace(blink::Visitor*);
friend class Internals;
static void SetIsLowEndDeviceForTesting(bool);
void ClearMemory();
static void ClearThreadSpecificMemory();
static bool is_low_end_device_;
HeapHashSet<WeakMember<MemoryPressureListener>> clients_;
HashSet<Thread*> threads_;
Mutex threads_mutex_;
} // namespace blink