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" | |
| 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 repeating restarts when continuous writes to the database occur. | |
|
Sergey Ulanov
2016/03/31 22:36:15
s/to repeating/to avoid repeating/
Yuwei
2016/04/01 23:41:02
Done.
| |
| 18 const int kRestartDelayInSecond = 2; | |
| 19 | |
| 20 // Full Path: $HOME/.pki/nssdb | |
| 21 const base::BasePathKey kNssBasePath = base::DIR_HOME; | |
|
Sergey Ulanov
2016/03/31 22:36:15
Don't need this. Just pass base::DIR_HOME when cal
Yuwei
2016/04/01 23:41:02
Done.
| |
| 22 const char kNssCertDirectoryPath[] = ".pki/nssdb"; | |
| 23 | |
| 24 CertificateWatcher::CertificateWatcher(base::WeakPtr<HostStatusMonitor> monitor, | |
| 25 const base::Closure& restart_action) | |
| 26 : monitor_(monitor), | |
| 27 delay_(base::TimeDelta::FromSeconds(kRestartDelayInSecond)), | |
| 28 restart_action_(restart_action), | |
| 29 restart_deferred_action_(base::Bind(&base::DoNothing)) { | |
| 30 base::PathService::Get(kNssBasePath, &nss_watch_path_); | |
|
Sergey Ulanov
2016/03/31 22:36:15
Verify that you get correct result from here. If w
Yuwei
2016/04/01 23:41:02
Done.
| |
| 31 nss_watch_path_ = nss_watch_path_.AppendASCII( | |
|
Sergey Ulanov
2016/03/31 22:36:15
call it watch_path_ instead of nss_watch_path_
Yuwei
2016/04/01 23:41:02
Done.
| |
| 32 std::string(kNssCertDirectoryPath)); | |
|
Sergey Ulanov
2016/03/31 22:36:15
Don't need std::string(). Compiler will convert it
Yuwei
2016/04/01 23:41:02
Done.
| |
| 33 if (monitor_) { | |
| 34 monitor_->AddStatusObserver(this); | |
| 35 } | |
| 36 } | |
| 37 | |
| 38 void CertificateWatcher::OnCertDirectoryChanged(const base::FilePath& path, | |
| 39 bool error) { | |
| 40 if (error || path != nss_watch_path_) { | |
| 41 LOG(WARNING) << "Unexpected file update callback. " | |
| 42 << "Path: " << path.MaybeAsASCII() << "; " | |
| 43 << "Error: " << error; | |
| 44 return; | |
| 45 } | |
| 46 | |
| 47 restart_timer_->Reset(); | |
| 48 | |
| 49 if (inhibit_mode_) { | |
| 50 restart_deferred_action_.Run(); | |
|
Sergey Ulanov
2016/03/31 22:36:15
Why do you need this?
Yuwei
2016/03/31 23:08:33
For testing reason. We need some way to quit the m
Sergey Ulanov
2016/04/01 17:49:23
Just post a delayed Quit task on that message loop
Yuwei
2016/04/01 18:28:04
That's time dependent and sounds a little bit flak
Sergey Ulanov
2016/04/01 20:11:00
The test can work something like this:
1. OnClien
Yuwei
2016/04/01 20:53:22
Okay... Sounds reasonable
Yuwei
2016/04/01 23:41:02
Done.
| |
| 51 } | |
| 52 } | |
| 53 | |
| 54 void CertificateWatcher::OnTimer() { | |
| 55 if (inhibit_mode_) { | |
| 56 restart_timer_->Reset(); | |
|
Sergey Ulanov
2016/03/31 22:36:15
I don't think you want to rest the timer here. It
Yuwei
2016/03/31 23:08:33
I guess I misunderstood your last comment about al
Sergey Ulanov
2016/04/01 17:49:23
Starting/resetting the timer makes sense only in r
Yuwei
2016/04/01 18:28:04
Okay...
Yuwei
2016/04/01 23:41:02
Done.
| |
| 57 return; | |
| 58 } | |
| 59 VLOG(1) << "NSS certificate was updated. Calling restart..."; | |
| 60 restart_action_.Run(); | |
| 61 } | |
| 62 | |
| 63 CertificateWatcher::~CertificateWatcher() { | |
| 64 if (monitor_) { | |
| 65 monitor_->RemoveStatusObserver(this); | |
| 66 } | |
| 67 | |
| 68 VLOG(1) << "Stopped watching certificate changes."; | |
| 69 } | |
| 70 | |
| 71 void CertificateWatcher::Start() { | |
| 72 file_watcher_.reset(new base::FilePathWatcher()); | |
| 73 file_watcher_->Watch( | |
| 74 nss_watch_path_, true, | |
| 75 base::Bind(&CertificateWatcher::OnCertDirectoryChanged, | |
| 76 base::Unretained(this))); | |
| 77 | |
| 78 auto raw_timer = | |
|
Sergey Ulanov
2016/03/31 22:36:15
Don't need this variable:
restart_timer.reset(ne
Yuwei
2016/04/01 23:41:02
Done.
| |
| 79 new base::DelayTimer(FROM_HERE, | |
| 80 delay_, | |
|
Sergey Ulanov
2016/03/31 22:36:15
Please clang-format this code. It will move this l
Yuwei
2016/04/01 23:41:02
Done.
| |
| 81 this, | |
| 82 &CertificateWatcher::OnTimer); | |
| 83 restart_timer_.reset(raw_timer); | |
| 84 | |
| 85 VLOG(1) << "Started watching certificate changes."; | |
| 86 } | |
| 87 | |
| 88 void CertificateWatcher::SetInhibit(bool inhibit) { inhibit_mode_ = inhibit; } | |
|
Sergey Ulanov
2016/03/31 22:36:15
Don't need this method. Just set the variable in O
Yuwei
2016/04/01 23:41:02
Done.
| |
| 89 | |
| 90 void CertificateWatcher::OnClientConnected(const std::string& jid) { | |
| 91 SetInhibit(true); | |
| 92 } | |
| 93 | |
| 94 void CertificateWatcher::OnClientDisconnected(const std::string& jid) { | |
| 95 SetInhibit(false); | |
| 96 } | |
|
Sergey Ulanov
2016/03/31 22:36:15
if (restart_pending_) {
restart_action_.Run();
}
Yuwei
2016/04/01 23:41:02
Done.
| |
| 97 | |
| 98 void CertificateWatcher::SetDelay(const base::TimeDelta& delay) { | |
|
Sergey Ulanov
2016/03/31 22:36:15
Call it SetDelayForTests.
Yuwei
2016/04/01 23:41:03
Done.
| |
| 99 delay_ = delay; | |
| 100 } | |
| 101 | |
| 102 void CertificateWatcher::SetWatchPath(const base::FilePath& watch_path) { | |
| 103 nss_watch_path_ = watch_path; | |
| 104 } | |
| 105 | |
| 106 void CertificateWatcher::SetRestartDeferredAction( | |
|
Sergey Ulanov
2016/03/31 22:36:15
Why do you need this?
Yuwei
2016/03/31 23:08:33
See above... For quitting the message loop
Yuwei
2016/04/01 23:41:02
Removed.
| |
| 107 const base::Closure& restart_action) { | |
| 108 restart_deferred_action_ = restart_action; | |
| 109 } | |
| 110 | |
| 111 } // namespace remoting | |
| OLD | NEW |