File: navigation_entry_screenshot_manager.h

package info (click to toggle)
chromium 120.0.6099.224-1~deb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 6,112,112 kB
  • sloc: cpp: 32,907,025; ansic: 8,148,123; javascript: 3,679,536; python: 2,031,248; asm: 959,718; java: 804,675; xml: 617,256; sh: 111,417; objc: 100,835; perl: 88,443; cs: 53,032; makefile: 29,579; fortran: 24,137; php: 21,162; tcl: 21,147; sql: 20,809; ruby: 17,735; pascal: 12,864; yacc: 8,045; lisp: 3,388; lex: 1,323; ada: 727; awk: 329; jsp: 267; csh: 117; exp: 43; sed: 37
file content (96 lines) | stat: -rw-r--r-- 4,090 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CONTENT_BROWSER_RENDERER_HOST_NAVIGATION_TRANSITIONS_NAVIGATION_ENTRY_SCREENSHOT_MANAGER_H_
#define CONTENT_BROWSER_RENDERER_HOST_NAVIGATION_TRANSITIONS_NAVIGATION_ENTRY_SCREENSHOT_MANAGER_H_

#include "base/containers/lru_cache.h"
#include "base/gtest_prod_util.h"
#include "base/memory/memory_pressure_listener.h"
#include "base/memory/safe_ref.h"
#include "content/common/content_export.h"

namespace content {

class NavigationEntryScreenshotCacheEvictor;

// This class manages the metadata for all `NavigationEntryScreenshot`s captured
// for the backward and forward navigation entries per `FrameTree`. The
// screenshots are used to present users with previews of the previous pages
// when the users initiate back/forward-navigations. This class is owned by a
// `BrowserContext`. All primary `FrameTree`s sharing the same `BrowserContext`
// share the same manager. The manager should only be accessed by the
// `NavigationEntryScreenshotCache` and tests.
class CONTENT_EXPORT NavigationEntryScreenshotManager {
 public:
  NavigationEntryScreenshotManager();
  NavigationEntryScreenshotManager(const NavigationEntryScreenshotManager&) =
      delete;
  NavigationEntryScreenshotManager& operator=(
      const NavigationEntryScreenshotManager&) = delete;
  ~NavigationEntryScreenshotManager();

  // Called when a screenshot is stashed into a `NavigationEntry`, or when a
  // screenshot is removed from the entry (for preview, or during the
  // destruction of the entry).
  void OnScreenshotCached(NavigationEntryScreenshotCacheEvictor* cache,
                          size_t size);
  void OnScreenshotRemoved(NavigationEntryScreenshotCacheEvictor* cache,
                           size_t size);

  // Called when a cache's owning `NavigationController` becomes visible. This
  // cache is the the most recently used cache.
  void OnCacheBecameVisible(NavigationEntryScreenshotCacheEvictor* cache);

  bool IsEmpty() const;

  size_t GetCurrentCacheSize() const { return current_cache_size_in_bytes_; }
  size_t GetMaxCacheSize() const { return max_cache_size_in_bytes_; }

  // Allow tests to customize memory budget.
  void SetMemoryBudgetForTesting(size_t size) {
    max_cache_size_in_bytes_ = size;
  }

  base::SafeRef<NavigationEntryScreenshotManager> GetSafeRef() const {
    return weak_factory_.GetSafeRef();
  }

 private:
  // Called when the first screenshot is cached into `cache`, and when the last
  // screenshot is removed from `cache`.
  void Register(NavigationEntryScreenshotCacheEvictor* cache);
  void Unregister(NavigationEntryScreenshotCacheEvictor* cache);

  // Called at the end of `OnScreenshotCached`.
  void EvictIfOutOfMemoryBudget();

  // Used by `listener_`. When the system memory is under critical pressure, all
  // screenshots under this `Profile` are purged.
  void OnMemoryPressure(
      base::MemoryPressureListener::MemoryPressureLevel memory_pressure_level);
  FRIEND_TEST_ALL_PREFIXES(NavigationEntryScreenshotCacheTest,
                           OnMemoryPressureCritical);

  size_t max_cache_size_in_bytes_;
  size_t current_cache_size_in_bytes_ = 0U;

  // The `listener_` monitors the system memory pressure, and calls
  // `NavigationEntryScreenshotManager::OnMemoryPressure` when the system
  // memory pressure level changes.
  std::unique_ptr<base::MemoryPressureListener> listener_;

  // The most recently used cache is stored at the front of the
  // `base::LRUCacheSet`. A limited interface to the tab's cache is used so that
  // this BrowserContext-wide manager does not have access to details like URLs
  // or pixels within each tab.
  base::LRUCacheSet<NavigationEntryScreenshotCacheEvictor*> managed_caches_;

  SEQUENCE_CHECKER(sequence_checker_);

  base::WeakPtrFactory<NavigationEntryScreenshotManager> weak_factory_{this};
};
}  // namespace content

#endif  // CONTENT_BROWSER_RENDERER_HOST_NAVIGATION_TRANSITIONS_NAVIGATION_ENTRY_SCREENSHOT_MANAGER_H_