Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "remoting/host/linux/certificate_watcher.h" | 5 #include "remoting/host/linux/certificate_watcher.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
| 9 #include "base/files/file_util.h" | |
| 10 #include "base/hash.h" | |
| 9 #include "base/location.h" | 11 #include "base/location.h" |
| 10 #include "base/logging.h" | 12 #include "base/logging.h" |
| 11 #include "base/path_service.h" | 13 #include "base/path_service.h" |
| 12 #include "base/thread_task_runner_handle.h" | 14 #include "base/thread_task_runner_handle.h" |
| 13 | 15 |
| 14 namespace remoting { | 16 namespace remoting { |
| 15 | 17 |
| 18 namespace { | |
| 19 | |
| 16 // Delay time to restart the host when a change of certificate is detected. | 20 // Delay time to restart the host when a change of certificate is detected. |
| 17 // This is to avoid repeating restarts when continuous writes to the database | 21 // This is to avoid repeating restarts when continuous writes to the database |
| 18 // occur. | 22 // occur. |
| 19 const int kRestartDelayInSecond = 2; | 23 const int kRestartDelayInSecond = 2; |
| 20 | 24 |
| 21 // Full Path: $HOME/.pki/nssdb | 25 // Full Path: $HOME/.pki/nssdb |
| 22 const char kCertDirectoryPath[] = ".pki/nssdb"; | 26 const char kCertDirectoryPath[] = ".pki/nssdb"; |
| 23 | 27 |
| 28 const char* const kCertFiles[] = {"cert9.db", "key4.db", "pkcs11.txt"}; | |
|
Sergey Ulanov
2016/05/13 22:03:51
Declare it as const char kCertFiles[][]
Lambros
2016/05/14 01:27:52
This line fails to compile:
const char kCertFiles[
| |
| 29 | |
| 30 } // namespace | |
| 31 | |
| 32 // This class lives on the IO thread, watches the certificate database files, | |
| 33 // and notifies the caller_task_runner thread of any updates. | |
| 34 class CertificateWatcherImpl { | |
|
Sergey Ulanov
2016/05/13 22:03:51
It's confusing to have CertificateWatcherImpl whil
Lambros
2016/05/14 01:27:52
Done.
| |
| 35 public: | |
| 36 CertificateWatcherImpl( | |
| 37 base::WeakPtr<CertificateWatcher> watcher, | |
| 38 scoped_refptr<base::SingleThreadTaskRunner> caller_task_runner, | |
| 39 base::FilePath cert_watch_path); | |
| 40 ~CertificateWatcherImpl(); | |
| 41 | |
| 42 void StartWatching(); | |
| 43 | |
| 44 private: | |
| 45 // size_t is the return type of base::HashInts() which is used to accumulate | |
| 46 // a hash-code while iterating over the database files. | |
| 47 typedef size_t HashType; | |
|
Sergey Ulanov
2016/05/13 22:03:51
Call this HashValue? We normally don't use Type su
Lambros
2016/05/14 01:27:52
Done.
| |
| 48 | |
| 49 // Called by the FileWatcher when it detects any changes. | |
| 50 void OnCertDirectoryChanged(const base::FilePath& path, | |
| 51 bool error); | |
| 52 | |
| 53 // Reads the certificate database files and returns a hash of their contents. | |
| 54 HashType ComputeHash(); | |
| 55 | |
| 56 base::WeakPtr<CertificateWatcher> watcher_; | |
| 57 | |
| 58 // The TaskRunner to be notified of any changes. | |
| 59 scoped_refptr<base::SingleThreadTaskRunner> caller_task_runner_; | |
| 60 | |
| 61 // The file watcher to watch changes inside the certificate folder. | |
| 62 std::unique_ptr<base::FilePathWatcher> file_watcher_; | |
| 63 | |
| 64 // Path of the certificate files/directories. | |
| 65 base::FilePath cert_watch_path_; | |
| 66 | |
| 67 // The hash code of the current certificate database contents. When the | |
| 68 // FileWatcher detects changes, the code is re-computed and compared with | |
| 69 // this stored value. | |
| 70 HashType current_hash_; | |
| 71 | |
| 72 DISALLOW_COPY_AND_ASSIGN(CertificateWatcherImpl); | |
| 73 }; | |
| 74 | |
| 75 CertificateWatcherImpl::CertificateWatcherImpl( | |
| 76 base::WeakPtr<CertificateWatcher> watcher, | |
| 77 scoped_refptr<base::SingleThreadTaskRunner> caller_task_runner, | |
| 78 base::FilePath cert_watch_path) | |
| 79 : watcher_(watcher), | |
| 80 caller_task_runner_(caller_task_runner), | |
| 81 cert_watch_path_(cert_watch_path) { | |
| 82 } | |
| 83 | |
| 84 CertificateWatcherImpl::~CertificateWatcherImpl() {} | |
| 85 | |
| 86 void CertificateWatcherImpl::StartWatching() { | |
| 87 DCHECK(!cert_watch_path_.empty()); | |
| 88 | |
| 89 file_watcher_.reset(new base::FilePathWatcher()); | |
| 90 | |
| 91 // Initialize hash value. | |
| 92 current_hash_ = ComputeHash(); | |
| 93 | |
| 94 // base::Unretained() is safe since this class owns the FileWatcher. | |
| 95 file_watcher_->Watch( | |
| 96 cert_watch_path_, | |
| 97 true, | |
|
Sergey Ulanov
2016/05/13 22:03:51
this can fit on the previous line. clang-format pl
Lambros
2016/05/14 01:27:52
Done.
| |
| 98 base::Bind(&CertificateWatcherImpl::OnCertDirectoryChanged, | |
| 99 base::Unretained(this))); | |
| 100 } | |
| 101 | |
| 102 void CertificateWatcherImpl::OnCertDirectoryChanged(const base::FilePath& path, | |
| 103 bool error) { | |
| 104 HashType new_hash = ComputeHash(); | |
|
Sergey Ulanov
2016/05/13 22:03:51
whenever we get notification that the directory ha
Lambros
2016/05/14 01:27:51
Done.
| |
| 105 if (new_hash != current_hash_) { | |
| 106 current_hash_ = new_hash; | |
| 107 caller_task_runner_->PostTask( | |
| 108 FROM_HERE, | |
| 109 base::Bind(&CertificateWatcher::DatabaseChanged, watcher_, path, | |
| 110 error)); | |
| 111 } else { | |
| 112 VLOG(1) << "Directory changed but contents are the same."; | |
| 113 } | |
| 114 } | |
| 115 | |
| 116 CertificateWatcherImpl::HashType CertificateWatcherImpl::ComputeHash() { | |
| 117 HashType result = 0; | |
| 118 | |
| 119 for (const char* file : kCertFiles) { | |
| 120 base::FilePath path = cert_watch_path_.AppendASCII(file); | |
| 121 std::string content; | |
| 122 HashType file_hash = 0; | |
| 123 | |
| 124 // It's possible the file might not exist. Compute the overall hash in a | |
| 125 // consistent way for the set of files that do exist. If a new file comes | |
| 126 // into existence, the resulting hash-code should change. | |
| 127 if (base::ReadFileToString(path, &content)) { | |
| 128 file_hash = base::Hash(content); | |
| 129 } | |
| 130 result = base::HashInts(result, file_hash); | |
| 131 } | |
| 132 return result; | |
| 133 } | |
| 134 | |
| 24 CertificateWatcher::CertificateWatcher( | 135 CertificateWatcher::CertificateWatcher( |
| 25 const base::Closure& restart_action, | 136 const base::Closure& restart_action, |
| 26 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner) | 137 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner) |
| 27 : restart_action_(restart_action), | 138 : restart_action_(restart_action), |
| 28 caller_task_runner_(base::ThreadTaskRunnerHandle::Get()), | 139 caller_task_runner_(base::ThreadTaskRunnerHandle::Get()), |
| 29 io_task_runner_(io_task_runner), | 140 io_task_runner_(io_task_runner), |
| 30 delay_(base::TimeDelta::FromSeconds(kRestartDelayInSecond)), | 141 delay_(base::TimeDelta::FromSeconds(kRestartDelayInSecond)), |
| 31 weak_factory_(this) { | 142 weak_factory_(this) { |
| 32 if (!base::PathService::Get(base::DIR_HOME, &cert_watch_path_)) { | 143 if (!base::PathService::Get(base::DIR_HOME, &cert_watch_path_)) { |
| 33 LOG(FATAL) << "Failed to get path of the home directory."; | 144 LOG(FATAL) << "Failed to get path of the home directory."; |
| 34 } | 145 } |
| 35 cert_watch_path_ = cert_watch_path_.AppendASCII(kCertDirectoryPath); | 146 cert_watch_path_ = cert_watch_path_.AppendASCII(kCertDirectoryPath); |
| 36 } | 147 } |
| 37 | 148 |
| 38 CertificateWatcher::~CertificateWatcher() { | 149 CertificateWatcher::~CertificateWatcher() { |
| 39 DCHECK(caller_task_runner_->BelongsToCurrentThread()); | 150 DCHECK(caller_task_runner_->BelongsToCurrentThread()); |
| 40 | 151 |
| 41 if (!is_started()) { | 152 if (!is_started()) { |
| 42 return; | 153 return; |
| 43 } | 154 } |
| 44 if (monitor_) { | 155 if (monitor_) { |
| 45 monitor_->RemoveStatusObserver(this); | 156 monitor_->RemoveStatusObserver(this); |
| 46 } | 157 } |
| 47 io_task_runner_->DeleteSoon(FROM_HERE, file_watcher_.release()); | 158 io_task_runner_->DeleteSoon(FROM_HERE, watcher_impl_.release()); |
| 48 | 159 |
| 49 VLOG(1) << "Stopped watching certificate changes."; | 160 VLOG(1) << "Stopped watching certificate changes."; |
| 50 } | 161 } |
| 51 | 162 |
| 52 void CertificateWatcher::Start() { | 163 void CertificateWatcher::Start() { |
| 53 DCHECK(caller_task_runner_->BelongsToCurrentThread()); | 164 DCHECK(caller_task_runner_->BelongsToCurrentThread()); |
| 54 DCHECK(!cert_watch_path_.empty()); | 165 DCHECK(!cert_watch_path_.empty()); |
| 55 | 166 |
| 56 file_watcher_.reset(new base::FilePathWatcher()); | 167 watcher_impl_.reset(new CertificateWatcherImpl(weak_factory_.GetWeakPtr(), |
| 168 caller_task_runner_, | |
| 169 cert_watch_path_)); | |
| 170 | |
| 57 io_task_runner_->PostTask( | 171 io_task_runner_->PostTask( |
| 58 FROM_HERE, | 172 FROM_HERE, |
| 59 base::Bind(base::IgnoreResult(&base::FilePathWatcher::Watch), | 173 base::Bind(&CertificateWatcherImpl::StartWatching, |
| 60 base::Unretained(file_watcher_.get()), cert_watch_path_, true, | 174 base::Unretained(watcher_impl_.get()))); |
| 61 base::Bind(&CertificateWatcher::OnCertDirectoryChanged, | 175 |
| 62 caller_task_runner_, weak_factory_.GetWeakPtr()))); | |
| 63 restart_timer_.reset(new base::DelayTimer(FROM_HERE, delay_, this, | 176 restart_timer_.reset(new base::DelayTimer(FROM_HERE, delay_, this, |
| 64 &CertificateWatcher::OnTimer)); | 177 &CertificateWatcher::OnTimer)); |
| 65 | 178 |
| 66 VLOG(1) << "Started watching certificate changes."; | 179 VLOG(1) << "Started watching certificate changes."; |
| 67 } | 180 } |
| 68 | 181 |
| 69 void CertificateWatcher::SetMonitor(base::WeakPtr<HostStatusMonitor> monitor) { | 182 void CertificateWatcher::SetMonitor(base::WeakPtr<HostStatusMonitor> monitor) { |
| 70 DCHECK(is_started()); | 183 DCHECK(is_started()); |
| 71 if (monitor_) { | 184 if (monitor_) { |
| 72 monitor_->RemoveStatusObserver(this); | 185 monitor_->RemoveStatusObserver(this); |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 96 delay_ = delay; | 209 delay_ = delay; |
| 97 } | 210 } |
| 98 | 211 |
| 99 void CertificateWatcher::SetWatchPathForTests( | 212 void CertificateWatcher::SetWatchPathForTests( |
| 100 const base::FilePath& watch_path) { | 213 const base::FilePath& watch_path) { |
| 101 DCHECK(!is_started()); | 214 DCHECK(!is_started()); |
| 102 cert_watch_path_ = watch_path; | 215 cert_watch_path_ = watch_path; |
| 103 } | 216 } |
| 104 | 217 |
| 105 bool CertificateWatcher::is_started() const { | 218 bool CertificateWatcher::is_started() const { |
| 106 return file_watcher_ != nullptr; | 219 return watcher_impl_ != nullptr; |
| 107 } | 220 } |
| 108 | 221 |
| 109 // static | 222 void CertificateWatcher::DatabaseChanged(const base::FilePath& path, |
| 110 void CertificateWatcher::OnCertDirectoryChanged( | 223 bool error) { |
| 111 scoped_refptr<base::SingleThreadTaskRunner> network_task_runner, | |
| 112 base::WeakPtr<CertificateWatcher> watcher_, | |
| 113 const base::FilePath& path, | |
| 114 bool error) { | |
| 115 network_task_runner->PostTask( | |
| 116 FROM_HERE, | |
| 117 base::Bind(&CertificateWatcher::DirectoryChanged, watcher_, path, error)); | |
| 118 } | |
| 119 | |
| 120 void CertificateWatcher::DirectoryChanged(const base::FilePath& path, | |
| 121 bool error) { | |
| 122 DCHECK(caller_task_runner_->BelongsToCurrentThread()); | 224 DCHECK(caller_task_runner_->BelongsToCurrentThread()); |
| 123 DCHECK(path == cert_watch_path_); | 225 DCHECK(path == cert_watch_path_); |
| 124 | 226 |
| 125 if (error) { | 227 if (error) { |
| 126 LOG(FATAL) << "Error occurs when watching changes of file " | 228 LOG(FATAL) << "Error occurred while watching for changes of file: " |
| 127 << cert_watch_path_.MaybeAsASCII(); | 229 << cert_watch_path_.MaybeAsASCII(); |
| 128 } | 230 } |
| 129 | 231 |
| 130 restart_timer_->Reset(); | 232 restart_timer_->Reset(); |
| 131 } | 233 } |
| 132 | 234 |
| 133 void CertificateWatcher::OnTimer() { | 235 void CertificateWatcher::OnTimer() { |
| 134 DCHECK(caller_task_runner_->BelongsToCurrentThread()); | 236 DCHECK(caller_task_runner_->BelongsToCurrentThread()); |
| 135 | 237 |
| 136 if (inhibit_mode_) { | 238 if (inhibit_mode_) { |
| 137 restart_pending_ = true; | 239 restart_pending_ = true; |
| 138 return; | 240 return; |
| 139 } | 241 } |
| 140 | 242 |
| 141 VLOG(1) << "Certificate was updated. Calling restart..."; | 243 VLOG(1) << "Certificate was updated. Calling restart..."; |
| 142 restart_action_.Run(); | 244 restart_action_.Run(); |
| 143 } | 245 } |
| 144 | 246 |
| 145 } // namespace remoting | 247 } // namespace remoting |
| OLD | NEW |