File: certificate_watcher.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (105 lines) | stat: -rw-r--r-- 3,578 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
101
102
103
104
105
// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef REMOTING_HOST_LINUX_CERTIFICATE_WATCHER_H_
#define REMOTING_HOST_LINUX_CERTIFICATE_WATCHER_H_

#include <memory>

#include "base/files/file_path.h"
#include "base/files/file_path_watcher.h"
#include "base/memory/weak_ptr.h"
#include "base/task/single_thread_task_runner.h"
#include "base/time/time.h"
#include "base/timer/timer.h"
#include "remoting/host/host_status_monitor.h"
#include "remoting/host/host_status_observer.h"

namespace remoting {

class CertDbContentWatcher;

// This class watches the cert database and notifies the host to restart when
// a change of the database is detected. The runner script will restart the host
// when the host is killed then the new host will capture any new changes of the
// database.
//
// Acceptable false positives will be caused by desktop sessions and other
// external programs.
//
// Implements HostStatusObserver to defer restart action when the host is
// connected to a client.
class CertificateWatcher : public remoting::HostStatusObserver {
 public:
  CertificateWatcher(
      const base::RepeatingClosure& restart_action,
      scoped_refptr<base::SingleThreadTaskRunner> io_task_runner);

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

  // The message loop of io_task_runner MUST be running after the destructor is
  // called, otherwise there will be memory leaks.
  ~CertificateWatcher() override;

  // Starts watching file changes
  // calling |restart_action_| when the host need to restart.
  void Start();

  // Sets the monitor to observe connection/disconnection events to toggle
  // the inhibit mode. Should be called after the watcher starts.
  // Adds |this| as an observer to the monitor.
  // Removes |this| as an observer from the old monitor if it is not null.
  void SetMonitor(scoped_refptr<HostStatusMonitor> monitor);

  // HostStatusObserver interface.
  void OnClientConnected(const std::string& jid) override;
  void OnClientDisconnected(const std::string& jid) override;

  // Will only work before the watcher starts.
  void SetDelayForTests(const base::TimeDelta& delay);
  void SetWatchPathForTests(const base::FilePath& watch_path);

 private:
  friend class CertDbContentWatcher;

  // Returns true if the watcher has started.
  bool is_started() const;

  // Runs in the caller's thread. It will defer restarting the host if
  // |inhibit_restart_scheduled_| flag is set to true.
  void DatabaseChanged();

  // Reference to the monitor.
  scoped_refptr<HostStatusMonitor> monitor_;

  // Called when a restart is scheduled.
  base::RepeatingClosure restart_action_;

  // The runner that runs everything other than the file watcher.
  scoped_refptr<base::SingleThreadTaskRunner> caller_task_runner_;

  // The runner that runs the file watcher.
  scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_;

  bool inhibit_mode_ = false;

  bool restart_pending_ = false;

  // Path of the certificate files/directories.
  base::FilePath cert_watch_path_;

  // The watcher implementation that watches the files on the IO thread.
  std::unique_ptr<CertDbContentWatcher> content_watcher_;

  // The time to wait before re-reading the DB files after a change is
  // detected.
  base::TimeDelta delay_;

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

}  // namespace remoting

#endif  // REMOTING_HOST_LINUX_CERTIFICATE_WATCHER_H_