| OLD | NEW |
| (Empty) | |
| 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 |
| 5 #ifndef REMOTING_HOST_LINUX_CERTIFICATE_WATCHER_H_ |
| 6 #define REMOTING_HOST_LINUX_CERTIFICATE_WATCHER_H_ |
| 7 |
| 8 #include "base/files/file_path.h" |
| 9 #include "base/files/file_path_watcher.h" |
| 10 #include "base/macros.h" |
| 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/memory/weak_ptr.h" |
| 13 #include "base/timer/timer.h" |
| 14 #include "remoting/host/host_status_monitor.h" |
| 15 #include "remoting/host/host_status_observer.h" |
| 16 |
| 17 namespace remoting { |
| 18 |
| 19 // This class watches the cert database and notifies the host to restart when |
| 20 // a change of the database is detected. The runner script will restart the host |
| 21 // when the host is killed then the new host will capture any new changes of the |
| 22 // database. |
| 23 // |
| 24 // Acceptable false positives will be caused by desktop sessions and other |
| 25 // external programs. |
| 26 // |
| 27 // Implements HostStatusObserver to defer restart action when the host is |
| 28 // connected to a client. |
| 29 class CertificateWatcher : public remoting::HostStatusObserver { |
| 30 public: |
| 31 CertificateWatcher( |
| 32 const base::Closure& restart_action, |
| 33 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner); |
| 34 |
| 35 // The message loop of io_task_runner MUST be running after the destructor is |
| 36 // called, otherwise there will be memory leaks. |
| 37 ~CertificateWatcher() override; |
| 38 |
| 39 // Starts watching file changes |
| 40 // calling |restart_action_| when the host need to restart. |
| 41 void Start(); |
| 42 |
| 43 // Sets the monitor to observe connection/disconnection events to toggle |
| 44 // the inhibit mode. Should be called after the watcher starts. |
| 45 // Adds |this| as an observer to the monitor. |
| 46 // Removes |this| as an observer from the old monitor if it is not null. |
| 47 void SetMonitor(base::WeakPtr<HostStatusMonitor> monitor); |
| 48 |
| 49 // HostStatusObserver interface. |
| 50 void OnClientConnected(const std::string& jid) override; |
| 51 void OnClientDisconnected(const std::string& jid) override; |
| 52 |
| 53 // Will only work before the watcher starts. |
| 54 void SetDelayForTests(const base::TimeDelta& delay); |
| 55 void SetWatchPathForTests(const base::FilePath& watch_path); |
| 56 |
| 57 private: |
| 58 // Returns true if the watcher has started. |
| 59 bool is_started() const; |
| 60 |
| 61 // Callback passed to |file_watcher_|. Runs in IO thread. |
| 62 static void OnCertDirectoryChanged( |
| 63 scoped_refptr<base::SingleThreadTaskRunner> network_task_runner, |
| 64 base::WeakPtr<CertificateWatcher> watcher_, const base::FilePath& path, |
| 65 bool error); |
| 66 |
| 67 // Runs in the caller's thread. |
| 68 void DirectoryChanged(const base::FilePath& path, bool error); |
| 69 |
| 70 // Called by |restart_timer_| when it's time to reset the host. |
| 71 // It will defer restart if |inhibit_restart_scheduled_| flag is set to true. |
| 72 void OnTimer(); |
| 73 |
| 74 // Reference to the monitor |
| 75 base::WeakPtr<HostStatusMonitor> monitor_; |
| 76 |
| 77 // Called when a restart is scheduled. |
| 78 base::Closure restart_action_; |
| 79 |
| 80 // The runner that runs everything other than the file watcher. |
| 81 scoped_refptr<base::SingleThreadTaskRunner> caller_task_runner_; |
| 82 |
| 83 // The runner that runs the file watcher. |
| 84 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_; |
| 85 |
| 86 bool inhibit_mode_ = false; |
| 87 |
| 88 bool restart_pending_ = false; |
| 89 |
| 90 // Path of the certificate files/directories. |
| 91 base::FilePath cert_watch_path_; |
| 92 |
| 93 // The file watcher to watch changes inside the certificate folder. |
| 94 scoped_ptr<base::FilePathWatcher> file_watcher_; |
| 95 |
| 96 // The time to wait to restart when it is scheduled. |
| 97 base::TimeDelta delay_; |
| 98 |
| 99 // Timer to delay the restart action. |
| 100 scoped_ptr<base::DelayTimer> restart_timer_; |
| 101 |
| 102 base::WeakPtrFactory<CertificateWatcher> weak_factory_; |
| 103 |
| 104 DISALLOW_COPY_AND_ASSIGN(CertificateWatcher); |
| 105 }; |
| 106 |
| 107 } // namespace remoting |
| 108 |
| 109 #endif // REMOTING_HOST_LINUX_CERTIFICATE_WATCHER_H_ |
| OLD | NEW |