1 // Copyright 2020 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "chrome/browser/chromeos/process_snapshot_server.h"
6
7 #include <algorithm>
8
9 #include "base/bind.h"
10 #include "base/location.h"
11 #include "base/task/task_traits.h"
12 #include "base/task/thread_pool.h"
13 #include "content/public/browser/browser_thread.h"
14
15 namespace {
16
17 base::LazyInstance<ProcessSnapshotServer>::DestructorAtExit g_instance =
18 LAZY_INSTANCE_INITIALIZER;
19
20 // TODO(afakhry): Figure out if we can build a snapshot only the first time,
21 // then use CONFIG_PROC_EVENTS to listen to subsequent fork()s or exec()s in
22 // order to avoid having to regularly walk /proc fs to build the snapshot, and
23 // to avoid having a stale |snaphot_| in between two refreshes.
GetProcessSnapshot()24 base::ProcessIterator::ProcessEntries GetProcessSnapshot() {
25 return base::ProcessIterator(/*process_filter=*/nullptr).Snapshot();
26 }
27
28 } // namespace
29
30 // -----------------------------------------------------------------------------
31 // ProcessSnapshotServer::Observer:
32
Observer(base::TimeDelta desired_refresh_time)33 ProcessSnapshotServer::Observer::Observer(base::TimeDelta desired_refresh_time)
34 : desired_refresh_time_(desired_refresh_time) {}
35
36 // -----------------------------------------------------------------------------
37 // ProcessSnapshotServer:
38
39 ProcessSnapshotServer::~ProcessSnapshotServer() = default;
40
41 // static
Get()42 ProcessSnapshotServer* ProcessSnapshotServer::Get() {
43 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
44 return g_instance.Pointer();
45 }
46
AddObserver(Observer * observer)47 void ProcessSnapshotServer::AddObserver(Observer* observer) {
48 base::TimeDelta current_refresh_time =
49 timer_.IsRunning() ? timer_.GetCurrentDelay() : base::TimeDelta::Max();
50
51 observers_.AddObserver(observer);
52
53 // Only refresh the timer if this observer requires a higher refresh rate.
54 if (observer->desired_refresh_time() > current_refresh_time)
55 return;
56
57 RefreshTimer(observer->desired_refresh_time());
58 }
59
RemoveObserver(Observer * observer)60 void ProcessSnapshotServer::RemoveObserver(Observer* observer) {
61 observers_.RemoveObserver(observer);
62
63 base::TimeDelta min_refresh_time = base::TimeDelta::Max();
64 for (auto& observer : observers_) {
65 min_refresh_time =
66 std::min(min_refresh_time, observer.desired_refresh_time());
67 }
68
69 RefreshTimer(min_refresh_time);
70 }
71
ProcessSnapshotServer()72 ProcessSnapshotServer::ProcessSnapshotServer()
73 : task_runner_(base::ThreadPool::CreateSequencedTaskRunner(
74 {base::MayBlock(), base::TaskShutdownBehavior::SKIP_ON_SHUTDOWN})),
75 weak_ptr_factory_(this) {}
76
RefreshSnapshot()77 void ProcessSnapshotServer::RefreshSnapshot() {
78 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
79
80 task_runner_->PostTaskAndReplyWithResult(
81 FROM_HERE, base::BindOnce(&GetProcessSnapshot),
82 base::BindOnce(&ProcessSnapshotServer::OnSnapshotRefreshed,
83 weak_ptr_factory_.GetWeakPtr()));
84 }
85
OnSnapshotRefreshed(base::ProcessIterator::ProcessEntries snapshot)86 void ProcessSnapshotServer::OnSnapshotRefreshed(
87 base::ProcessIterator::ProcessEntries snapshot) {
88 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
89 snapshot_ = std::move(snapshot);
90
91 for (auto& observer : observers_)
92 observer.OnProcessSnapshotRefreshed(snapshot_);
93 }
94
RefreshTimer(base::TimeDelta new_refresh_time)95 void ProcessSnapshotServer::RefreshTimer(base::TimeDelta new_refresh_time) {
96 if (new_refresh_time == base::TimeDelta::Max()) {
97 timer_.Stop();
98 return;
99 }
100
101 // First time observer (i.e. when timer is not running) should trigger an
102 // immediate refresh.
103 if (!timer_.IsRunning())
104 RefreshSnapshot();
105 else if (new_refresh_time == timer_.GetCurrentDelay())
106 return;
107
108 timer_.Start(FROM_HERE, new_refresh_time,
109 base::BindRepeating(&ProcessSnapshotServer::RefreshSnapshot,
110 base::Unretained(this)));
111 }
112