Chromium Code Reviews| 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/base/auto_thread_task_runner.h" | |
|
Sergey Ulanov
2016/04/05 21:12:56
Don't need this.
Yuwei
2016/04/05 22:18:55
Done.
| |
| 15 #include "remoting/host/host_status_monitor.h" | |
| 16 #include "remoting/host/host_status_observer.h" | |
| 17 | |
| 18 namespace remoting { | |
| 19 | |
| 20 // This class watches the cert database and kills the host when a change of the | |
| 21 // database is detected. The runner script will restart the host when the host | |
| 22 // is killed then the new host will capture any new changes of the 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 base::SupportsWeakPtr<CertificateWatcher> { | |
|
Sergey Ulanov
2016/04/05 21:12:56
Please use WeakPtrFactory<> instead of SupportsWea
Yuwei
2016/04/05 22:18:55
Done.
| |
| 31 public: | |
| 32 CertificateWatcher( | |
| 33 const base::Closure& restart_action, | |
| 34 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner); | |
| 35 | |
| 36 ~CertificateWatcher() override; | |
| 37 | |
| 38 // Starts watching file changes on file thread and calling restart on | |
| 39 // network thread if necessary. | |
|
Sergey Ulanov
2016/04/05 21:12:56
Suggest rewording:
// Starts watching file change
Yuwei
2016/04/05 21:53:01
This comment may be unnecessary information for th
Yuwei
2016/04/05 22:18:55
Done.
| |
| 40 // returns true if the watcher has successfully started. | |
|
Sergey Ulanov
2016/04/05 21:12:56
Don't need to return anything as it never fails.
Yuwei
2016/04/05 22:18:55
Done.
| |
| 41 bool Start(); | |
| 42 | |
| 43 // Stops watching file changes and cleans up all the resources being used. | |
| 44 // Will do nothing if the certificate watcher is currently not running. | |
| 45 // You might not need to explicitly call this function since the destructor | |
| 46 // will call it. | |
| 47 // The message loop MUST be running after this function is called, otherwise | |
| 48 // there will be memory leaks. | |
| 49 void Stop(); | |
|
Sergey Ulanov
2016/04/05 21:12:56
I don't think you need this method. For the tests
| |
| 50 | |
| 51 // Sets the monitor to observe connection/disconnection events to toggle | |
| 52 // the inhibit mode. Should be called after the watcher starts. | |
| 53 // Adds |this| as an observer to the monitor. | |
| 54 // Removes |this| as an observer from the old monitor if it is not null. | |
| 55 void SetMonitor(base::WeakPtr<HostStatusMonitor> monitor); | |
| 56 | |
| 57 // HostStatusObserver interface. | |
| 58 void OnClientConnected(const std::string& jid) override; | |
| 59 void OnClientDisconnected(const std::string& jid) override; | |
| 60 | |
| 61 // Will only work before the watcher starts. | |
| 62 void SetDelayForTests(const base::TimeDelta& delay); | |
| 63 void SetWatchPathForTests(const base::FilePath& watch_path); | |
| 64 | |
| 65 private: | |
| 66 // Returns true if the watcher has started. | |
| 67 bool is_started() const; | |
| 68 | |
| 69 // Callback passed to |file_watcher_|. Run in IO thread. | |
|
Sergey Ulanov
2016/04/05 21:12:56
s/Run/Runs/
Yuwei
2016/04/05 22:18:55
Done.
| |
| 70 static void OnCertDirectoryChanged( | |
| 71 scoped_refptr<base::SingleThreadTaskRunner> network_task_runner, | |
| 72 base::WeakPtr<CertificateWatcher> watcher_, const base::FilePath& path, | |
| 73 bool error); | |
| 74 | |
| 75 // Run in network thread. | |
| 76 void DirectoryChanged(const base::FilePath& path, bool error); | |
| 77 | |
| 78 // Called by |restart_timer_| when it's time to reset the host. | |
| 79 // It will defer restart if |inhibit_restart_scheduled_| flag is set to true. | |
| 80 void OnTimer(); | |
| 81 | |
| 82 // Reference to the monitor | |
| 83 base::WeakPtr<HostStatusMonitor> monitor_; | |
| 84 | |
| 85 // Called when a restart is scheduled. | |
| 86 base::Closure restart_action_; | |
| 87 | |
| 88 // The runner that runs everything other than the file watcher. | |
| 89 scoped_refptr<base::SingleThreadTaskRunner> caller_task_runner_; | |
| 90 | |
| 91 // The runner that runs the file watcher. | |
| 92 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_; | |
| 93 | |
| 94 bool inhibit_mode_ = false; | |
| 95 | |
| 96 bool restart_pending_ = false; | |
| 97 | |
| 98 // Path of the certificate files/directories. | |
| 99 base::FilePath cert_watch_path_; | |
| 100 | |
| 101 // The file watcher to watch changes inside the certificate folder. | |
| 102 scoped_ptr<base::FilePathWatcher> file_watcher_; | |
| 103 | |
| 104 // The time to wait to restart when it is scheduled. | |
| 105 base::TimeDelta delay_; | |
| 106 | |
| 107 // Timer to delay the restart action. | |
| 108 scoped_ptr<base::DelayTimer> restart_timer_; | |
| 109 | |
| 110 DISALLOW_COPY_AND_ASSIGN(CertificateWatcher); | |
| 111 }; | |
| 112 | |
| 113 } // namespace remoting | |
| 114 | |
| 115 #endif // REMOTING_HOST_LINUX_CERTIFICATE_WATCHER_H_ | |
| OLD | NEW |