certificate_watcher.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // Copyright 2016 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. #ifndef REMOTING_HOST_LINUX_CERTIFICATE_WATCHER_H_
  5. #define REMOTING_HOST_LINUX_CERTIFICATE_WATCHER_H_
  6. #include <memory>
  7. #include "base/files/file_path.h"
  8. #include "base/files/file_path_watcher.h"
  9. #include "base/memory/weak_ptr.h"
  10. #include "base/task/single_thread_task_runner.h"
  11. #include "base/time/time.h"
  12. #include "base/timer/timer.h"
  13. #include "remoting/host/host_status_monitor.h"
  14. #include "remoting/host/host_status_observer.h"
  15. namespace remoting {
  16. class CertDbContentWatcher;
  17. // This class watches the cert database and notifies the host to restart when
  18. // a change of the database is detected. The runner script will restart the host
  19. // when the host is killed then the new host will capture any new changes of the
  20. // database.
  21. //
  22. // Acceptable false positives will be caused by desktop sessions and other
  23. // external programs.
  24. //
  25. // Implements HostStatusObserver to defer restart action when the host is
  26. // connected to a client.
  27. class CertificateWatcher : public remoting::HostStatusObserver {
  28. public:
  29. CertificateWatcher(
  30. const base::RepeatingClosure& restart_action,
  31. scoped_refptr<base::SingleThreadTaskRunner> io_task_runner);
  32. CertificateWatcher(const CertificateWatcher&) = delete;
  33. CertificateWatcher& operator=(const CertificateWatcher&) = delete;
  34. // The message loop of io_task_runner MUST be running after the destructor is
  35. // called, otherwise there will be memory leaks.
  36. ~CertificateWatcher() override;
  37. // Starts watching file changes
  38. // calling |restart_action_| when the host need to restart.
  39. void Start();
  40. // Sets the monitor to observe connection/disconnection events to toggle
  41. // the inhibit mode. Should be called after the watcher starts.
  42. // Adds |this| as an observer to the monitor.
  43. // Removes |this| as an observer from the old monitor if it is not null.
  44. void SetMonitor(scoped_refptr<HostStatusMonitor> monitor);
  45. // HostStatusObserver interface.
  46. void OnClientConnected(const std::string& jid) override;
  47. void OnClientDisconnected(const std::string& jid) override;
  48. // Will only work before the watcher starts.
  49. void SetDelayForTests(const base::TimeDelta& delay);
  50. void SetWatchPathForTests(const base::FilePath& watch_path);
  51. private:
  52. friend class CertDbContentWatcher;
  53. // Returns true if the watcher has started.
  54. bool is_started() const;
  55. // Runs in the caller's thread. It will defer restarting the host if
  56. // |inhibit_restart_scheduled_| flag is set to true.
  57. void DatabaseChanged();
  58. // Reference to the monitor.
  59. scoped_refptr<HostStatusMonitor> monitor_;
  60. // Called when a restart is scheduled.
  61. base::RepeatingClosure restart_action_;
  62. // The runner that runs everything other than the file watcher.
  63. scoped_refptr<base::SingleThreadTaskRunner> caller_task_runner_;
  64. // The runner that runs the file watcher.
  65. scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_;
  66. bool inhibit_mode_ = false;
  67. bool restart_pending_ = false;
  68. // Path of the certificate files/directories.
  69. base::FilePath cert_watch_path_;
  70. // The watcher implementation that watches the files on the IO thread.
  71. std::unique_ptr<CertDbContentWatcher> content_watcher_;
  72. // The time to wait before re-reading the DB files after a change is
  73. // detected.
  74. base::TimeDelta delay_;
  75. base::WeakPtrFactory<CertificateWatcher> weak_factory_{this};
  76. };
  77. } // namespace remoting
  78. #endif // REMOTING_HOST_LINUX_CERTIFICATE_WATCHER_H_