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 "certificate_watcher.h" | |
|
Sergey Ulanov
2016/04/04 19:29:37
Specify pull path here please.
Yuwei
2016/04/04 21:40:24
full path I guess?
Sergey Ulanov
2016/04/04 21:44:38
yes
Yuwei
2016/04/04 23:42:24
Done.
| |
| 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 | |
| 14 namespace remoting { | |
| 15 | |
| 16 // 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 | |
| 18 // occur. | |
| 19 const int kRestartDelayInSecond = 2; | |
| 20 | |
| 21 // Full Path: $HOME/.pki/nssdb | |
| 22 const char kCertDirectoryPath[] = ".pki/nssdb"; | |
| 23 | |
| 24 CertificateWatcher::CertificateWatcher( | |
| 25 const base::Closure& restart_action, | |
| 26 scoped_refptr<base::SingleThreadTaskRunner> network_runner, | |
| 27 scoped_refptr<base::SingleThreadTaskRunner> io_runner) | |
| 28 : restart_action_(restart_action), | |
| 29 network_runner_(network_runner), | |
|
Sergey Ulanov
2016/04/04 19:29:37
Don't need to pass it as parameter explicitly. Ins
Yuwei
2016/04/04 23:42:24
Done.
| |
| 30 io_runner_(io_runner), | |
|
Sergey Ulanov
2016/04/04 19:29:37
Call this io_task_runner_.
Yuwei
2016/04/04 23:42:24
Done.
| |
| 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. "; | |
| 34 return; | |
| 35 } | |
| 36 | |
|
Sergey Ulanov
2016/04/04 19:29:37
nit: remove this empty line. Otherwise the followi
Yuwei
2016/04/04 23:42:24
Done.
| |
| 37 cert_watch_path_ = cert_watch_path_.AppendASCII(kCertDirectoryPath); | |
|
Sergey Ulanov
2016/04/04 19:29:37
nit: incorrect indentation.
Yuwei
2016/04/04 23:42:24
Done.
| |
| 38 } | |
| 39 | |
| 40 void CertificateWatcher::OnCertDirectoryChanged(const base::FilePath& path, | |
| 41 bool error) { | |
| 42 DCHECK(io_runner_->BelongsToCurrentThread()); | |
| 43 | |
| 44 if (error || path != cert_watch_path_) { | |
| 45 LOG(WARNING) << "Unexpected file update callback. " | |
| 46 << "Path: " << path.MaybeAsASCII() << "; " | |
| 47 << "Error: " << error; | |
| 48 return; | |
| 49 } | |
| 50 | |
| 51 network_runner_->PostTask( | |
| 52 FROM_HERE, base::Bind(&base::DelayTimer::Reset, | |
| 53 base::Unretained(restart_timer_.get()))); | |
| 54 } | |
| 55 | |
| 56 void CertificateWatcher::OnTimer() { | |
| 57 DCHECK(network_runner_->BelongsToCurrentThread()); | |
| 58 | |
| 59 if (inhibit_mode_) { | |
| 60 restart_pending_ = true; | |
| 61 return; | |
| 62 } | |
| 63 | |
| 64 VLOG(1) << "Certificate was updated. Calling restart..."; | |
| 65 restart_action_.Run(); | |
| 66 } | |
| 67 | |
| 68 CertificateWatcher::~CertificateWatcher() { | |
| 69 if (monitor_) { | |
| 70 monitor_->RemoveStatusObserver(this); | |
| 71 } | |
| 72 | |
| 73 VLOG(1) << "Stopped watching certificate changes."; | |
| 74 } | |
| 75 | |
| 76 void CertificateWatcher::Start() { | |
|
Sergey Ulanov
2016/04/04 19:29:37
Do you really need this separate from the construc
Yuwei
2016/04/04 21:45:20
It's basically to allow unit tests to override par
Sergey Ulanov
2016/04/04 21:47:15
I see. Then maybe make it return a bool value (fal
Yuwei
2016/04/04 23:42:24
Done.
| |
| 77 if (cert_watch_path_.empty()) { | |
| 78 LOG(ERROR) << "Failed to start watching. " | |
|
Sergey Ulanov
2016/04/04 19:29:37
This essentially duplicates the error you have in
| |
| 79 << "The certificate path is not correctly set up."; | |
| 80 return; | |
| 81 } | |
| 82 | |
| 83 io_runner_->PostTask(FROM_HERE, base::Bind( | |
| 84 &CertificateWatcher::StartWatcherOnIo, base::Unretained(this))); | |
|
Sergey Ulanov
2016/04/04 19:29:37
base::Unretained() is not safe here. CertificateWa
Yuwei
2016/04/04 23:42:24
Done.
| |
| 85 | |
| 86 network_runner_->PostTask(FROM_HERE, base::Bind( | |
| 87 &CertificateWatcher::StartTimerOnNetwork, base::Unretained(this))); | |
|
Sergey Ulanov
2016/04/04 19:29:37
Why do you need to post a separate task here? netw
Yuwei
2016/04/04 21:40:24
I was thinking it's the main thread calling this f
Yuwei
2016/04/04 23:42:24
Not posting task now
| |
| 88 | |
| 89 VLOG(1) << "Started watching certificate changes."; | |
| 90 } | |
| 91 | |
| 92 void CertificateWatcher::StartWatcherOnIo() { | |
| 93 DCHECK(io_runner_->BelongsToCurrentThread()); | |
| 94 | |
| 95 file_watcher_.reset(new base::FilePathWatcher()); | |
| 96 file_watcher_->Watch( | |
| 97 cert_watch_path_, true, | |
| 98 base::Bind(&CertificateWatcher::OnCertDirectoryChanged, | |
| 99 base::Unretained(this))); | |
|
Sergey Ulanov
2016/04/04 19:29:37
It's not safe to use base::Unretained() here. OnCe
Yuwei
2016/04/04 23:42:24
Done.
| |
| 100 } | |
| 101 | |
| 102 void CertificateWatcher::StartTimerOnNetwork() { | |
| 103 DCHECK(network_runner_->BelongsToCurrentThread()); | |
| 104 | |
| 105 restart_timer_.reset(new base::DelayTimer(FROM_HERE, delay_, this, | |
| 106 &CertificateWatcher::OnTimer)); | |
| 107 } | |
| 108 | |
| 109 bool CertificateWatcher::Started() const { | |
| 110 return file_watcher_ != nullptr; | |
| 111 } | |
| 112 | |
| 113 void CertificateWatcher::SetMonitor(base::WeakPtr<HostStatusMonitor> monitor) { | |
| 114 if (monitor_) { | |
| 115 monitor_->RemoveStatusObserver(this); | |
| 116 } | |
| 117 if (monitor) { | |
|
Sergey Ulanov
2016/04/04 19:29:37
I don't think this you need this condition: monito
Yuwei
2016/04/04 23:42:24
Changed to DCHECK
| |
| 118 monitor->AddStatusObserver(this); | |
| 119 } | |
| 120 monitor_ = monitor; | |
| 121 } | |
| 122 | |
| 123 void CertificateWatcher::OnClientConnected(const std::string& jid) { | |
| 124 if (!Started()) { | |
| 125 return; | |
| 126 } | |
| 127 DCHECK(network_runner_->BelongsToCurrentThread()); | |
| 128 inhibit_mode_ = true; | |
| 129 } | |
| 130 | |
| 131 void CertificateWatcher::OnClientDisconnected(const std::string& jid) { | |
| 132 if (!Started()) { | |
| 133 return; | |
| 134 } | |
| 135 DCHECK(network_runner_->BelongsToCurrentThread()); | |
| 136 inhibit_mode_ = false; | |
| 137 if (restart_pending_) { | |
| 138 restart_pending_ = false; | |
| 139 restart_action_.Run(); | |
| 140 } | |
| 141 } | |
| 142 | |
| 143 void CertificateWatcher::SetDelayForTests(const base::TimeDelta& delay) { | |
| 144 DCHECK(!Started()); | |
| 145 delay_ = delay; | |
| 146 } | |
| 147 | |
| 148 void CertificateWatcher::SetWatchPathForTests( | |
| 149 const base::FilePath& watch_path) { | |
| 150 DCHECK(!Started()); | |
| 151 cert_watch_path_ = watch_path; | |
| 152 } | |
| 153 | |
| 154 } // namespace remoting | |
| OLD | NEW |