File: web_cache_manager.h

package info (click to toggle)
chromium 144.0.7559.109-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,915,868 kB
  • sloc: cpp: 35,866,215; ansic: 7,599,035; javascript: 3,623,761; python: 1,639,407; xml: 833,084; asm: 716,173; pascal: 185,323; sh: 88,763; perl: 88,699; objc: 79,984; sql: 58,217; cs: 42,430; fortran: 24,101; makefile: 20,747; tcl: 15,277; php: 14,022; yacc: 9,059; ruby: 7,553; awk: 3,720; lisp: 3,233; lex: 1,330; ada: 727; jsp: 228; sed: 36
file content (109 lines) | stat: -rw-r--r-- 4,111 bytes parent folder | download | duplicates (6)
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
97
98
99
100
101
102
103
104
105
106
107
108
109
// Copyright 2011 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// This is the browser side of the cache manager, handling required clearing of
// the renderer-side cache.

#ifndef COMPONENTS_WEB_CACHE_BROWSER_WEB_CACHE_MANAGER_H_
#define COMPONENTS_WEB_CACHE_BROWSER_WEB_CACHE_MANAGER_H_

#include <stddef.h>

#include <map>

#include "base/gtest_prod_util.h"
#include "base/memory/weak_ptr.h"
#include "base/no_destructor.h"
#include "base/scoped_multi_source_observation.h"
#include "components/web_cache/public/mojom/web_cache.mojom.h"
#include "content/public/browser/child_process_id.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/render_process_host_creation_observer.h"
#include "content/public/browser/render_process_host_observer.h"
#include "mojo/public/cpp/bindings/remote.h"

namespace web_cache {

// Note: memory usage uses uint64_t because potentially the browser could be
// 32 bit and the renderers 64 bits.
class WebCacheManager : public content::RenderProcessHostCreationObserver,
                        public content::RenderProcessHostObserver {
 public:
  // Gets the singleton WebCacheManager object.  The first time this method
  // is called, a WebCacheManager object is constructed and returned.
  // Subsequent calls will return the same object.
  static WebCacheManager* GetInstance();

  WebCacheManager(const WebCacheManager&) = delete;
  WebCacheManager& operator=(const WebCacheManager&) = delete;

  // Clears all in-memory caches.
  void ClearCache();

  // Instantly clears renderer cache for a process.
  // Must be called between Add(process_id) and Remove(process_id).
  void ClearCacheForProcess(content::ChildProcessId process_id);

  // Clears all in-memory caches when a tab is reloaded or the user navigates
  // to a different website.
  void ClearCacheOnNavigation();

  // content::RenderProcessHostCreationObserver:
  void OnRenderProcessHostCreated(
      content::RenderProcessHost* process_host) override;

  // content::RenderProcessHostObserver:
  void RenderProcessReady(content::RenderProcessHost* process_host) override;
  void RenderProcessExited(
      content::RenderProcessHost* process_host,
      const content::ChildProcessTerminationInfo& info) override;
  void RenderProcessHostDestroyed(
      content::RenderProcessHost* process_host) override;

 private:
  friend class base::NoDestructor<WebCacheManager>;
  FRIEND_TEST_ALL_PREFIXES(WebCacheManagerTest, AddRemoveRendererTest);
  FRIEND_TEST_ALL_PREFIXES(WebCacheManagerTest, OnlyClearCacheWhenProcessReady);
  FRIEND_TEST_ALL_PREFIXES(WebCacheManagerTest, PreExistingRenderProcessHost);

  // This class is a singleton.  Do not instantiate directly. Call GetInstance()
  // instead.
  WebCacheManager();
  ~WebCacheManager() override;

  // When a render process is created, it registers itself with the cache
  // manager host. The renderer will populate its cache, which may need to get
  // cleared later.
  void Add(content::RenderProcessHost* process_host);

  // Unregister the renderer when it gets destroyed.
  void Remove(content::RenderProcessHost* process_host);

  enum ClearCacheOccasion {
    // Instructs to clear the cache instantly.
    INSTANTLY,
    // Instructs to clear the cache when a navigation takes place (this
    // includes reloading a tab).
    ON_NAVIGATION
  };

  // Inform all renderers to clear their cache.
  void ClearRendererCache(ClearCacheOccasion occasion);

  // Maps every renderer_id with its corresponding
  // mojo::Remote<mojom::WebCache>. The key is the unique id of every render
  // process host.
  std::map<content::ChildProcessId, mojo::Remote<mojom::WebCache>>
      web_cache_services_;

  base::ScopedMultiSourceObservation<content::RenderProcessHost,
                                     content::RenderProcessHostObserver>
      rph_observations_{this};

  base::WeakPtrFactory<WebCacheManager> weak_factory_{this};
};

}  // namespace web_cache

#endif  // COMPONENTS_WEB_CACHE_BROWSER_WEB_CACHE_MANAGER_H_