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/location.h" | |
| 9 #include "base/logging.h" | |
| 10 | |
| 11 namespace remoting { | |
| 12 | |
| 13 // Full Path: $HOME/.pki/nssdb | |
| 14 const char kNSSEnvironmentPrefix[] = "HOME"; | |
| 15 const char kNSSWatchPathToHome[] = "/.pki/nssdb"; | |
| 16 | |
| 17 /* Impl Class */ | |
| 18 | |
| 19 class CertificateWatcherImpl : public CertificateWatcherImplInterface { | |
| 20 public: | |
| 21 // Will not take ownership of watcher | |
| 22 CertificateWatcherImpl(scoped_refptr<base::SingleThreadTaskRunner> | |
| 23 io_task_runner, | |
| 24 scoped_refptr<base::SingleThreadTaskRunner> | |
| 25 suicide_task_runner, | |
| 26 int suicide_delay, | |
| 27 const CertificateWatcher::SuicideAction& | |
| 28 suicide_action, | |
| 29 CertificateWatcher* watcher); | |
| 30 void Start() override; | |
| 31 void Stop() override; | |
| 32 void ScheduleSuicide() override; | |
| 33 | |
| 34 private: | |
| 35 // The time to way to execute suicide_action_ after the suicide is scheduled. | |
| 36 int suicide_delay_; | |
| 37 | |
| 38 // Will be called when a suicide is scheduled | |
| 39 const CertificateWatcher::SuicideAction suicide_action_; | |
| 40 | |
| 41 CertificateWatcher* watcher_; | |
| 42 | |
| 43 // The thread to watch file changes | |
| 44 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_; | |
| 45 | |
| 46 // The thread to post suicide task | |
| 47 scoped_refptr<base::SingleThreadTaskRunner> suicide_task_runner_; | |
| 48 | |
| 49 // The file watcher to watch certificate | |
| 50 scoped_ptr<base::FilePathWatcher> file_watcher_; | |
| 51 | |
| 52 // path of the NSS files/directories | |
| 53 base::FilePath nss_watch_path_; | |
| 54 | |
| 55 void WatchOnIO(); | |
| 56 void StopOnIO(); | |
| 57 void OnNSSUpdate(const base::FilePath& path, bool error); | |
| 58 | |
| 59 DISALLOW_COPY_AND_ASSIGN(CertificateWatcherImpl); | |
| 60 }; | |
| 61 | |
| 62 | |
| 63 CertificateWatcherImpl::CertificateWatcherImpl | |
| 64 (scoped_refptr<base::SingleThreadTaskRunner> io_task_runner, | |
| 65 scoped_refptr<base::SingleThreadTaskRunner> suicide_task_runner, | |
| 66 int suicide_delay, | |
| 67 const CertificateWatcher::SuicideAction& suicide_action, | |
| 68 CertificateWatcher* watcher) : | |
| 69 suicide_delay_(suicide_delay), | |
| 70 suicide_action_(suicide_action), | |
| 71 watcher_(watcher), | |
| 72 io_task_runner_(io_task_runner), | |
| 73 suicide_task_runner_(suicide_task_runner), | |
| 74 nss_watch_path_(getenv(kNSSEnvironmentPrefix) + | |
| 75 std::string(kNSSWatchPathToHome)) {} | |
| 76 | |
| 77 void CertificateWatcherImpl::Start() { | |
| 78 io_task_runner_->PostTask(FROM_HERE, | |
| 79 base::Bind(&CertificateWatcherImpl::WatchOnIO, | |
| 80 base::Unretained(this))); | |
| 81 } | |
| 82 | |
| 83 void CertificateWatcherImpl::Stop() { | |
| 84 io_task_runner_->PostTask(FROM_HERE, | |
| 85 base::Bind(&CertificateWatcherImpl::StopOnIO, | |
| 86 base::Unretained(this))); | |
| 87 } | |
| 88 | |
| 89 void CertificateWatcherImpl::ScheduleSuicide() { | |
| 90 suicide_task_runner_->PostDelayedTask(FROM_HERE, suicide_action_, | |
| 91 base::TimeDelta::FromSeconds(suicide_delay_)); | |
|
Sergey Ulanov
2016/03/29 19:40:04
this line is not indented correctly. Please use cl
Yuwei
2016/03/29 19:57:02
Acknowledged.
Yuwei
2016/03/30 18:47:45
Done. Have run clang-format
| |
| 92 } | |
| 93 | |
| 94 void CertificateWatcherImpl::WatchOnIO() { | |
| 95 DCHECK(io_task_runner_->BelongsToCurrentThread()); | |
| 96 | |
| 97 file_watcher_.reset(new base::FilePathWatcher()); | |
| 98 file_watcher_->Watch(nss_watch_path_, true, | |
| 99 base::Bind(&CertificateWatcherImpl::OnNSSUpdate, | |
| 100 base::Unretained(this))); | |
| 101 } | |
| 102 | |
| 103 void CertificateWatcherImpl::StopOnIO() { | |
| 104 DCHECK(io_task_runner_->BelongsToCurrentThread()); | |
| 105 | |
| 106 file_watcher_.reset(); | |
| 107 } | |
| 108 | |
| 109 void CertificateWatcherImpl::OnNSSUpdate(const base::FilePath& path, | |
| 110 bool error) { | |
| 111 if (!error && path == nss_watch_path_) { | |
| 112 watcher_->OnUpdate(); | |
| 113 } | |
| 114 } | |
| 115 | |
| 116 /* Watcher */ | |
| 117 | |
| 118 CertificateWatcher::CertificateWatcher( | |
| 119 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner, | |
| 120 scoped_refptr<base::SingleThreadTaskRunner> suicide_task_runner, | |
| 121 int suicide_delay, const SuicideAction& suicide_action) : | |
| 122 impl_(new CertificateWatcherImpl(io_task_runner, suicide_task_runner, | |
| 123 suicide_delay, suicide_action, | |
| 124 this)) {} | |
| 125 | |
| 126 CertificateWatcher::CertificateWatcher(CertificateWatcherImplInterface* impl) : | |
| 127 impl_(impl) {} | |
| 128 | |
| 129 CertificateWatcher::~CertificateWatcher() { | |
| 130 Stop(); | |
| 131 } | |
| 132 | |
| 133 void CertificateWatcher::Start() { | |
| 134 impl_->Start(); | |
| 135 LOG(INFO) << "Started watching certificate changes."; | |
| 136 } | |
| 137 | |
| 138 void CertificateWatcher::Stop() { | |
| 139 impl_->Stop(); | |
| 140 LOG(INFO) << "Stopped watching certificate changes."; | |
| 141 } | |
| 142 | |
| 143 void CertificateWatcher::Inhibit() { | |
| 144 inhibit_mode_ = true; | |
| 145 LOG(INFO) << "Inhibit mode on. Will not suicide until connection drops."; | |
| 146 } | |
| 147 | |
| 148 void CertificateWatcher::Uninhibit() { | |
| 149 inhibit_mode_ = false; | |
| 150 if (suicide_scheduled_) { | |
| 151 impl_->ScheduleSuicide(); | |
| 152 LOG(INFO) << "Certificate was updated in inhibit mode. Schedule a suicide."; | |
| 153 } | |
| 154 } | |
| 155 | |
| 156 void CertificateWatcher::OnUpdate() { | |
| 157 if (!suicide_scheduled_) { | |
| 158 suicide_scheduled_ = true; | |
| 159 if (inhibit_mode_) { | |
| 160 LOG(INFO) << "Inhibit mode is on. " | |
| 161 "Will postpone suicide until disconnection."; | |
| 162 return; | |
| 163 } | |
| 164 impl_->ScheduleSuicide(); | |
| 165 LOG(INFO) << "Certificate updated. Scheduled a suicide."; | |
| 166 } | |
| 167 } | |
| 168 | |
| 169 } | |
|
Sergey Ulanov
2016/03/29 19:40:04
// namespace remoting
Yuwei
2016/03/29 19:57:02
Acknowledged.
Yuwei
2016/03/30 18:47:44
Done.
| |
| OLD | NEW |