File: web_cache_manager.cc

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 (100 lines) | stat: -rw-r--r-- 3,378 bytes parent folder | download | duplicates (7)
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
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/web_cache/browser/web_cache_manager.h"

#include "base/no_destructor.h"

namespace web_cache {

// static
WebCacheManager* WebCacheManager::GetInstance() {
  static base::NoDestructor<WebCacheManager> s_instance;
  return s_instance.get();
}

WebCacheManager::WebCacheManager() {
  // The instance can be created once renderers are already running, thus we
  // cannot observe the creation of the previous processes.
  for (auto iter(content::RenderProcessHost::AllHostsIterator());
       !iter.IsAtEnd(); iter.Advance()) {
    content::RenderProcessHost* process_host = iter.GetCurrentValue();
    OnRenderProcessHostCreated(process_host);
    if (process_host->IsReady()) {
      RenderProcessReady(process_host);
    }
  }
}

WebCacheManager::~WebCacheManager() = default;

void WebCacheManager::Add(content::RenderProcessHost* process_host) {
  mojo::Remote<mojom::WebCache> service;
  process_host->BindReceiver(service.BindNewPipeAndPassReceiver());
  web_cache_services_[process_host->GetID()] = std::move(service);
}

void WebCacheManager::Remove(content::RenderProcessHost* process_host) {
  web_cache_services_.erase(process_host->GetID());
}

void WebCacheManager::ClearCache() {
  // Tell each renderer process to clear the cache.
  ClearRendererCache(INSTANTLY);
}

void WebCacheManager::ClearCacheOnNavigation() {
  // Tell each renderer process to clear the cache when a tab is reloaded or
  // the user navigates to a new website.
  ClearRendererCache(ON_NAVIGATION);
}

void WebCacheManager::OnRenderProcessHostCreated(
    content::RenderProcessHost* process_host) {
  // If the host is reused after the process exited, it is possible to get a
  // second created notification for the same host.
  if (!rph_observations_.IsObservingSource(process_host)) {
    rph_observations_.AddObservation(process_host);
  }
}

void WebCacheManager::RenderProcessReady(
    content::RenderProcessHost* process_host) {
  Add(process_host);
}

void WebCacheManager::RenderProcessExited(
    content::RenderProcessHost* process_host,
    const content::ChildProcessTerminationInfo& info) {
  RenderProcessHostDestroyed(process_host);
}

void WebCacheManager::RenderProcessHostDestroyed(
    content::RenderProcessHost* process_host) {
  rph_observations_.RemoveObservation(process_host);
  Remove(process_host);
}

void WebCacheManager::ClearCacheForProcess(content::ChildProcessId process_id) {
  auto it = web_cache_services_.find(process_id);
  if (it != web_cache_services_.end() &&
      content::RenderProcessHost::FromID(process_id)->IsReady()) {
    it->second->ClearCache(false);
  }
}

void WebCacheManager::ClearRendererCache(
    WebCacheManager::ClearCacheOccasion occasion) {
  for (auto& service : web_cache_services_) {
    // It is possible to have a renderer host that is not ready and is still
    // present in 'web_cache_services_' because the notification has not reach
    // this instance. This can happen if  'ClearRendererCache' is called by an
    // other observer during 'RenderProcessHostImpl::ProcessDied'.
    if (content::RenderProcessHost::FromID(service.first)->IsReady()) {
      service.second->ClearCache(occasion == ON_NAVIGATION);
    }
  }
}

}  // namespace web_cache