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 | |
| 13 namespace remoting { | |
| 14 | |
| 15 // Full Path: $HOME/.pki/nssdb | |
| 16 const char kNSSEnvironmentPrefix[] = "HOME"; | |
|
Sergey Ulanov
2016/03/30 21:02:44
Please use base::PathService with base::DIR_HOME t
Yuwei
2016/03/31 17:40:03
Done.
| |
| 17 const char kNSSWatchPathToHome[] = "/.pki/nssdb"; | |
|
Sergey Ulanov
2016/03/30 21:02:44
Maybe call this kNssCertDirectoryPath - notice low
Yuwei
2016/03/31 17:40:03
Done.
| |
| 18 | |
| 19 CertificateWatcher::CertificateWatcher(int delay, | |
| 20 const base::Closure& restart_action) | |
| 21 : CertificateWatcher(delay, restart_action, | |
| 22 base::Bind(&base::DoNothing), | |
| 23 base::FilePath(getenv(kNSSEnvironmentPrefix) + | |
| 24 std::string(kNSSWatchPathToHome))) {} | |
| 25 | |
| 26 CertificateWatcher::CertificateWatcher( | |
| 27 int delay, | |
| 28 const base::Closure& restart_action, | |
| 29 const base::Closure& restart_deferred_action, const base::FilePath& path) | |
|
Sergey Ulanov
2016/03/30 21:02:44
one argument per line please (clang-format?)
Yuwei
2016/03/30 21:37:00
Okay... In fact this file has been processed by cl
| |
| 30 : delay_(delay), | |
| 31 restart_action_(restart_action), | |
| 32 restart_deferred_action_(restart_deferred_action), | |
| 33 nss_watch_path_(path) { | |
| 34 if (monitor_) { | |
| 35 monitor_->AddStatusObserver(this); | |
| 36 } | |
| 37 } | |
| 38 | |
| 39 void CertificateWatcher::OnNSSUpdate(const base::FilePath& path, 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 if (inhibit_mode_) { | |
|
Sergey Ulanov
2016/03/30 21:02:44
I think we want to always start the timer here. Th
Yuwei
2016/03/30 21:37:00
This sounds better although I don't think the clie
Yuwei
2016/03/31 17:40:03
Done.
| |
| 48 inhibit_restart_scheduled_ = true; | |
| 49 restart_deferred_action_.Run(); | |
| 50 return; | |
| 51 } | |
| 52 | |
| 53 restart_timer_->Reset(); | |
| 54 } | |
| 55 | |
| 56 void CertificateWatcher::OnTimer() { | |
| 57 LOG(INFO) << "NSS certificate was updated. Calling restart..."; | |
| 58 restart_action_.Run(); | |
| 59 } | |
| 60 | |
| 61 CertificateWatcher::~CertificateWatcher() { | |
| 62 Stop(); | |
| 63 } | |
| 64 | |
| 65 void CertificateWatcher::StartOn(scoped_refptr<AutoThreadTaskRunner> runner, | |
| 66 base::WeakPtr<HostStatusMonitor> monitor) { | |
| 67 runner->PostTask(FROM_HERE, base::Bind(&CertificateWatcher::Start, | |
| 68 base::Unretained(this), | |
| 69 monitor)); | |
| 70 } | |
| 71 | |
| 72 void CertificateWatcher::Start(base::WeakPtr<HostStatusMonitor> monitor) { | |
| 73 monitor_ = monitor; | |
| 74 file_watcher_.reset(new base::FilePathWatcher()); | |
| 75 file_watcher_->Watch( | |
| 76 nss_watch_path_, true, | |
| 77 base::Bind(&CertificateWatcher::OnNSSUpdate, base::Unretained(this))); | |
| 78 | |
| 79 auto raw_timer = | |
| 80 new base::DelayTimer(FROM_HERE, base::TimeDelta::FromSeconds(delay_), | |
| 81 this, &CertificateWatcher::OnTimer); | |
| 82 restart_timer_.reset(raw_timer); | |
| 83 | |
| 84 LOG(INFO) << "Started watching certificate changes."; | |
|
Sergey Ulanov
2016/03/30 21:02:44
I don't think you need this comment. At least not
Yuwei
2016/03/31 17:40:04
Done.
| |
| 85 } | |
| 86 | |
| 87 void CertificateWatcher::Stop() { | |
|
Sergey Ulanov
2016/03/30 21:02:44
This method is not used anywhere except the destru
Yuwei
2016/03/31 17:40:03
Done.
| |
| 88 file_watcher_.reset(); | |
| 89 restart_timer_.reset(); | |
| 90 | |
| 91 if (monitor_) { | |
| 92 monitor_->RemoveStatusObserver(this); | |
| 93 } | |
| 94 | |
| 95 LOG(INFO) << "Stopped watching certificate changes."; | |
|
Sergey Ulanov
2016/03/30 21:02:44
same as above.
Yuwei
2016/03/31 17:40:04
Done.
| |
| 96 } | |
| 97 | |
| 98 void CertificateWatcher::Inhibit() { inhibit_mode_ = true; } | |
| 99 | |
| 100 void CertificateWatcher::Uninhibit() { | |
| 101 inhibit_mode_ = false; | |
| 102 if (inhibit_restart_scheduled_) { | |
| 103 restart_timer_->Reset(); | |
|
Sergey Ulanov
2016/03/30 21:02:44
I don't think you really want to activate the time
Yuwei
2016/03/31 17:40:03
Logic changed so now we don't need to call restart
| |
| 104 LOG(INFO) << "Certificate was updated in inhibit mode. " | |
| 105 "Scheduled a restart."; | |
| 106 } | |
| 107 } | |
| 108 | |
| 109 // HostStatusObserver Impl: | |
|
Sergey Ulanov
2016/03/30 21:02:44
remove this comment
Yuwei
2016/03/31 17:40:04
Done.
| |
| 110 void CertificateWatcher::OnClientConnected(const std::string& jid) { | |
| 111 Inhibit(); | |
| 112 } | |
| 113 | |
| 114 void CertificateWatcher::OnClientDisconnected(const std::string& jid) { | |
| 115 Uninhibit(); | |
| 116 } | |
| 117 | |
| 118 } // namespace remoting | |
| OLD | NEW |