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 #include "remoting/host/linux/certificate_watcher.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/bind_helpers.h" | |
| 9 #include "base/location.h" | |
| 10 #include "base/logging.h" | |
| 11 #include "base/message_loop/message_loop.h" | |
| 12 #include "base/path_service.h" | |
| 13 #include "base/thread_task_runner_handle.h" | |
| 14 | |
| 15 namespace remoting { | |
| 16 | |
| 17 // Delay time to restart the host when a change of certificate is detected. | |
| 18 // This is to avoid repeating restarts when continuous writes to the database | |
| 19 // occur. | |
| 20 const int kRestartDelayInSecond = 2; | |
| 21 | |
| 22 // Full Path: $HOME/.pki/nssdb | |
| 23 const char kCertDirectoryPath[] = ".pki/nssdb"; | |
| 24 | |
| 25 CertificateWatcher::CertificateWatcher( | |
| 26 const base::Closure& restart_action, | |
| 27 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner) | |
| 28 : restart_action_(restart_action), | |
| 29 network_task_runner_(base::ThreadTaskRunnerHandle::Get()), | |
|
Sergey Ulanov
2016/04/05 01:14:32
nit: rename this to caller_task_runner_, to avoid
Yuwei
2016/04/05 20:46:23
Done.
| |
| 30 io_task_runner_(io_task_runner), | |
| 31 delay_(base::TimeDelta::FromSeconds(kRestartDelayInSecond)) { | |
| 32 if (!base::PathService::Get(base::DIR_HOME, &cert_watch_path_)) { | |
| 33 LOG(ERROR) << "Failed to get path of the home directory. "; | |
|
Sergey Ulanov
2016/04/05 01:14:32
Looking at the implementation of base::PathService
Yuwei
2016/04/05 20:46:22
Done.
| |
| 34 return; | |
| 35 } | |
| 36 cert_watch_path_ = cert_watch_path_.AppendASCII(kCertDirectoryPath); | |
| 37 } | |
| 38 | |
| 39 void CertificateWatcher::OnCertDirectoryChanged( | |
|
Sergey Ulanov
2016/04/05 01:14:32
add "// static" comment above this line to make it
| |
| 40 scoped_refptr<base::SingleThreadTaskRunner> network_task_runner, | |
| 41 base::WeakPtr<CertificateWatcher> watcher_, const base::FilePath& path, | |
|
Sergey Ulanov
2016/04/05 01:14:32
One argument per line please (run clang-format)
Yuwei
2016/04/05 01:25:36
I think clang-format actually moved |path| to the
Sergey Ulanov
2016/04/05 18:35:21
maybe it's somehow configured incorrectly on your
Yuwei
2016/04/05 19:15:51
Tried git cl format and looks like it's not in the
Sergey Ulanov
2016/04/05 19:34:46
It's a chromium-specific rule, see https://www.chr
Yuwei
2016/04/05 20:46:22
Done.
| |
| 42 bool error) { | |
| 43 network_task_runner->PostTask( | |
| 44 FROM_HERE, | |
| 45 base::Bind(&CertificateWatcher::DirectoryChanged, watcher_, path, error)); | |
| 46 } | |
| 47 | |
| 48 void CertificateWatcher::DirectoryChanged(const base::FilePath& path, | |
| 49 bool error) { | |
| 50 DCHECK(network_task_runner_->BelongsToCurrentThread()); | |
| 51 | |
| 52 if (error || path != cert_watch_path_) { | |
| 53 LOG(WARNING) << "Unexpected file update callback. " | |
| 54 << "Path: " << path.MaybeAsASCII() << "; " | |
| 55 << "Error: " << error; | |
| 56 return; | |
| 57 } | |
| 58 | |
| 59 restart_timer_->Reset(); | |
| 60 } | |
| 61 | |
| 62 void CertificateWatcher::OnTimer() { | |
| 63 DCHECK(network_task_runner_->BelongsToCurrentThread()); | |
| 64 | |
| 65 if (inhibit_mode_) { | |
| 66 restart_pending_ = true; | |
| 67 return; | |
| 68 } | |
| 69 | |
| 70 VLOG(1) << "Certificate was updated. Calling restart..."; | |
| 71 restart_action_.Run(); | |
| 72 } | |
| 73 | |
| 74 CertificateWatcher::~CertificateWatcher() { | |
|
Sergey Ulanov
2016/04/05 01:14:32
Move this after the constructor.
Sergey Ulanov
2016/04/05 01:14:32
Add DCHECK(network_task_runner_->BelongsToCurrentT
Yuwei
2016/04/05 20:46:22
Checked in Stop()
| |
| 75 if (monitor_) { | |
| 76 monitor_->RemoveStatusObserver(this); | |
| 77 } | |
| 78 DCHECK(io_task_runner_->DeleteSoon(FROM_HERE, file_watcher_.release())); | |
|
Sergey Ulanov
2016/04/05 01:14:32
Don't put any statements with side-effects in [D]C
Yuwei
2016/04/05 01:25:36
Would we do `bool result = ...; DCHECK(result);` o
Sergey Ulanov
2016/04/05 18:35:21
Sure you can do that. The statement _inside_ DCHEC
Yuwei
2016/04/05 20:09:30
DeleteSoon here is causing memory leakage for the
| |
| 79 | |
| 80 VLOG(1) << "Stopped watching certificate changes."; | |
| 81 } | |
| 82 | |
| 83 bool CertificateWatcher::Start() { | |
|
Sergey Ulanov
2016/04/05 01:14:32
Reorder methods in this file to match the declarat
Yuwei
2016/04/05 20:46:22
Done.
| |
| 84 DCHECK(network_task_runner_->BelongsToCurrentThread()); | |
| 85 | |
| 86 if (cert_watch_path_.empty()) { | |
|
Sergey Ulanov
2016/04/05 01:14:32
Given that base::PathService::Get() should never f
Yuwei
2016/04/05 20:46:22
Done.
| |
| 87 LOG(ERROR) << "Failed to start watching. " | |
| 88 << "The certificate path is not correctly set up."; | |
| 89 return false; | |
| 90 } | |
| 91 | |
| 92 file_watcher_.reset(new base::FilePathWatcher()); | |
| 93 io_task_runner_->PostTask( | |
| 94 FROM_HERE, | |
| 95 base::Bind(base::IgnoreResult(&base::FilePathWatcher::Watch), | |
| 96 base::Unretained(file_watcher_.get()), cert_watch_path_, true, | |
| 97 base::Bind(&CertificateWatcher::OnCertDirectoryChanged, | |
| 98 network_task_runner_, this->AsWeakPtr()))); | |
| 99 restart_timer_.reset(new base::DelayTimer(FROM_HERE, delay_, this, | |
| 100 &CertificateWatcher::OnTimer)); | |
| 101 | |
| 102 VLOG(1) << "Started watching certificate changes."; | |
| 103 return true; | |
| 104 } | |
| 105 | |
| 106 bool CertificateWatcher::is_started() const { return file_watcher_ != nullptr; } | |
| 107 | |
| 108 void CertificateWatcher::SetMonitor(base::WeakPtr<HostStatusMonitor> monitor) { | |
| 109 if (monitor_) { | |
|
Sergey Ulanov
2016/04/05 01:14:31
Add DCHECK(is_started());
Yuwei
2016/04/05 20:46:22
Done.
| |
| 110 monitor_->RemoveStatusObserver(this); | |
| 111 } | |
| 112 DCHECK(monitor); | |
|
Sergey Ulanov
2016/04/05 01:14:32
DCHECKs for arguments should normally be in the be
Yuwei
2016/04/05 20:46:22
Removed
| |
| 113 monitor->AddStatusObserver(this); | |
| 114 monitor_ = monitor; | |
| 115 } | |
| 116 | |
| 117 void CertificateWatcher::OnClientConnected(const std::string& jid) { | |
| 118 if (!is_started()) { | |
|
Sergey Ulanov
2016/04/05 01:14:32
Don't need this if you add DCHECK(is_started());
Yuwei
2016/04/05 20:46:22
Done.
| |
| 119 return; | |
| 120 } | |
| 121 DCHECK(network_task_runner_->BelongsToCurrentThread()); | |
| 122 inhibit_mode_ = true; | |
| 123 } | |
| 124 | |
| 125 void CertificateWatcher::OnClientDisconnected(const std::string& jid) { | |
| 126 if (!is_started()) { | |
|
Sergey Ulanov
2016/04/05 01:14:32
don't need this.
Yuwei
2016/04/05 20:46:22
Done.
| |
| 127 return; | |
| 128 } | |
| 129 DCHECK(network_task_runner_->BelongsToCurrentThread()); | |
| 130 inhibit_mode_ = false; | |
| 131 if (restart_pending_) { | |
| 132 restart_pending_ = false; | |
| 133 restart_action_.Run(); | |
| 134 } | |
| 135 } | |
| 136 | |
| 137 void CertificateWatcher::SetDelayForTests(const base::TimeDelta& delay) { | |
| 138 DCHECK(!is_started()); | |
| 139 delay_ = delay; | |
| 140 } | |
| 141 | |
| 142 void CertificateWatcher::SetWatchPathForTests( | |
| 143 const base::FilePath& watch_path) { | |
| 144 DCHECK(!is_started()); | |
| 145 cert_watch_path_ = watch_path; | |
| 146 } | |
| 147 | |
| 148 } // namespace remoting | |
| OLD | NEW |