| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/policy/file_based_policy_loader.h" | 5 #include "chrome/browser/policy/file_based_policy_loader.h" |
| 6 | 6 |
| 7 namespace { | 7 namespace { |
| 8 | 8 |
| 9 // Amount of time we wait for the files on disk to settle before trying to load | 9 // Amount of time we wait for the files on disk to settle before trying to load |
| 10 // them. This alleviates the problem of reading partially written files and | 10 // them. This alleviates the problem of reading partially written files and |
| 11 // makes it possible to batch quasi-simultaneous changes. | 11 // makes it possible to batch quasi-simultaneous changes. |
| 12 const int kSettleIntervalSeconds = 5; | 12 const int kSettleIntervalSeconds = 5; |
| 13 | 13 |
| 14 // The time interval for rechecking policy. This is our fallback in case the | 14 // The time interval for rechecking policy. This is our fallback in case the |
| 15 // delegate never reports a change to the ReloadObserver. | 15 // delegate never reports a change to the ReloadObserver. |
| 16 const int kReloadIntervalMinutes = 15; | 16 const int kReloadIntervalMinutes = 15; |
| 17 | 17 |
| 18 } | 18 } |
| 19 | 19 |
| 20 namespace policy { | 20 namespace policy { |
| 21 | 21 |
| 22 FileBasedPolicyLoader::FileBasedPolicyLoader( | 22 FileBasedPolicyLoader::FileBasedPolicyLoader( |
| 23 FileBasedPolicyProvider::ProviderDelegate* provider_delegate) | 23 FileBasedPolicyProvider::ProviderDelegate* provider_delegate) |
| 24 : AsynchronousPolicyLoader(provider_delegate), | 24 : AsynchronousPolicyLoader(provider_delegate, |
| 25 kReloadIntervalMinutes), |
| 25 config_file_path_(provider_delegate->config_file_path()), | 26 config_file_path_(provider_delegate->config_file_path()), |
| 26 reload_task_(NULL), | |
| 27 reload_interval_(base::TimeDelta::FromMinutes(kReloadIntervalMinutes)), | |
| 28 settle_interval_(base::TimeDelta::FromSeconds(kSettleIntervalSeconds)) { | 27 settle_interval_(base::TimeDelta::FromSeconds(kSettleIntervalSeconds)) { |
| 29 } | 28 } |
| 30 | 29 |
| 30 FileBasedPolicyLoader::~FileBasedPolicyLoader() {} |
| 31 |
| 31 class FileBasedPolicyWatcherDelegate : public FilePathWatcher::Delegate { | 32 class FileBasedPolicyWatcherDelegate : public FilePathWatcher::Delegate { |
| 32 public: | 33 public: |
| 33 explicit FileBasedPolicyWatcherDelegate( | 34 explicit FileBasedPolicyWatcherDelegate( |
| 34 scoped_refptr<FileBasedPolicyLoader> loader) | 35 scoped_refptr<FileBasedPolicyLoader> loader) |
| 35 : loader_(loader) {} | 36 : loader_(loader) {} |
| 36 virtual ~FileBasedPolicyWatcherDelegate() {} | 37 virtual ~FileBasedPolicyWatcherDelegate() {} |
| 37 | 38 |
| 38 // FilePathWatcher::Delegate implementation: | 39 // FilePathWatcher::Delegate implementation: |
| 39 void OnFilePathChanged(const FilePath& path) { | 40 void OnFilePathChanged(const FilePath& path) { |
| 40 loader_->OnFilePathChanged(path); | 41 loader_->OnFilePathChanged(path); |
| 41 } | 42 } |
| 42 | 43 |
| 43 void OnError() { | 44 void OnError() { |
| 44 loader_->OnError(); | 45 loader_->OnError(); |
| 45 } | 46 } |
| 46 | 47 |
| 47 private: | 48 private: |
| 48 scoped_refptr<FileBasedPolicyLoader> loader_; | 49 scoped_refptr<FileBasedPolicyLoader> loader_; |
| 49 DISALLOW_COPY_AND_ASSIGN(FileBasedPolicyWatcherDelegate); | 50 DISALLOW_COPY_AND_ASSIGN(FileBasedPolicyWatcherDelegate); |
| 50 }; | 51 }; |
| 51 | 52 |
| 52 void FileBasedPolicyLoader::Init() { | |
| 53 AsynchronousPolicyLoader::Init(); | |
| 54 | |
| 55 // Initialization can happen early when the file thread is not yet available, | |
| 56 // but the watcher's initialization must be done on the file thread. Posting | |
| 57 // to a to the file directly before the file thread is initialized will cause | |
| 58 // the task to be forgotten. Instead, post a task to the ui thread to delay | |
| 59 // the remainder of initialization until threading is fully initialized. | |
| 60 BrowserThread::PostTask( | |
| 61 BrowserThread::UI, FROM_HERE, | |
| 62 NewRunnableMethod(this, | |
| 63 &FileBasedPolicyLoader::InitAfterFileThreadAvailable)); | |
| 64 } | |
| 65 | |
| 66 void FileBasedPolicyLoader::Stop() { | |
| 67 AsynchronousPolicyLoader::Stop(); | |
| 68 BrowserThread::PostTask( | |
| 69 BrowserThread::FILE, FROM_HERE, | |
| 70 NewRunnableMethod(this, &FileBasedPolicyLoader::StopOnFileThread)); | |
| 71 } | |
| 72 | |
| 73 void FileBasedPolicyLoader::OnFilePathChanged( | 53 void FileBasedPolicyLoader::OnFilePathChanged( |
| 74 const FilePath& path) { | 54 const FilePath& path) { |
| 75 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 55 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 76 Reload(); | 56 Reload(); |
| 77 } | 57 } |
| 78 | 58 |
| 79 void FileBasedPolicyLoader::OnError() { | 59 void FileBasedPolicyLoader::OnError() { |
| 80 LOG(ERROR) << "FilePathWatcher on " << config_file_path().value() | 60 LOG(ERROR) << "FilePathWatcher on " << config_file_path().value() |
| 81 << " failed."; | 61 << " failed."; |
| 82 } | 62 } |
| 83 | 63 |
| 84 FileBasedPolicyLoader::~FileBasedPolicyLoader() {} | |
| 85 | |
| 86 void FileBasedPolicyLoader::Reload() { | 64 void FileBasedPolicyLoader::Reload() { |
| 87 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 65 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 88 | 66 |
| 89 if (!delegate()) | 67 if (!delegate()) |
| 90 return; | 68 return; |
| 91 | 69 |
| 92 // Check the directory time in order to see whether a reload is required. | 70 // Check the directory time in order to see whether a reload is required. |
| 93 base::TimeDelta delay; | 71 base::TimeDelta delay; |
| 94 base::Time now = base::Time::Now(); | 72 base::Time now = base::Time::Now(); |
| 95 if (!IsSafeToReloadPolicy(now, &delay)) { | 73 if (!IsSafeToReloadPolicy(now, &delay)) { |
| 96 ScheduleReloadTask(delay); | 74 ScheduleReloadTask(delay); |
| 97 return; | 75 return; |
| 98 } | 76 } |
| 99 | 77 |
| 100 // Load the policy definitions. | 78 // Load the policy definitions. |
| 101 scoped_ptr<DictionaryValue> new_policy(delegate()->Load()); | 79 scoped_ptr<DictionaryValue> new_policy(delegate()->Load()); |
| 102 | 80 |
| 103 // Check again in case the directory has changed while reading it. | 81 // Check again in case the directory has changed while reading it. |
| 104 if (!IsSafeToReloadPolicy(now, &delay)) { | 82 if (!IsSafeToReloadPolicy(now, &delay)) { |
| 105 ScheduleReloadTask(delay); | 83 ScheduleReloadTask(delay); |
| 106 return; | 84 return; |
| 107 } | 85 } |
| 108 | 86 |
| 109 PostUpdatePolicyTask(new_policy.release()); | 87 PostUpdatePolicyTask(new_policy.release()); |
| 110 | 88 |
| 111 ScheduleFallbackReloadTask(); | 89 ScheduleFallbackReloadTask(); |
| 112 } | 90 } |
| 113 | 91 |
| 114 void FileBasedPolicyLoader::InitAfterFileThreadAvailable() { | |
| 115 if (provider()) { | |
| 116 BrowserThread::PostTask( | |
| 117 BrowserThread::FILE, FROM_HERE, | |
| 118 NewRunnableMethod(this, &FileBasedPolicyLoader::InitOnFileThread)); | |
| 119 } | |
| 120 } | |
| 121 | |
| 122 void FileBasedPolicyLoader::InitOnFileThread() { | 92 void FileBasedPolicyLoader::InitOnFileThread() { |
| 123 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 93 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 124 watcher_.reset(new FilePathWatcher); | 94 watcher_.reset(new FilePathWatcher); |
| 125 if (!config_file_path().empty() && | 95 if (!config_file_path().empty() && |
| 126 !watcher_->Watch(config_file_path(), | 96 !watcher_->Watch(config_file_path(), |
| 127 new FileBasedPolicyWatcherDelegate(this))) { | 97 new FileBasedPolicyWatcherDelegate(this))) { |
| 128 OnError(); | 98 OnError(); |
| 129 } | 99 } |
| 130 | 100 |
| 131 // There might have been changes to the directory in the time between | 101 // There might have been changes to the directory in the time between |
| 132 // construction of the loader and initialization of the watcher. Call reload | 102 // construction of the loader and initialization of the watcher. Call reload |
| 133 // to detect if that is the case. | 103 // to detect if that is the case. |
| 134 Reload(); | 104 Reload(); |
| 135 | 105 |
| 136 ScheduleFallbackReloadTask(); | 106 ScheduleFallbackReloadTask(); |
| 137 } | 107 } |
| 138 | 108 |
| 139 void FileBasedPolicyLoader::StopOnFileThread() { | 109 void FileBasedPolicyLoader::StopOnFileThread() { |
| 140 watcher_.reset(); | 110 watcher_.reset(); |
| 141 if (reload_task_) { | 111 AsynchronousPolicyLoader::StopOnFileThread(); |
| 142 reload_task_->Cancel(); | |
| 143 reload_task_ = NULL; | |
| 144 } | |
| 145 } | |
| 146 | |
| 147 void FileBasedPolicyLoader::ScheduleReloadTask( | |
| 148 const base::TimeDelta& delay) { | |
| 149 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
| 150 | |
| 151 if (reload_task_) | |
| 152 reload_task_->Cancel(); | |
| 153 | |
| 154 reload_task_ = | |
| 155 NewRunnableMethod(this, &FileBasedPolicyLoader::ReloadFromTask); | |
| 156 BrowserThread::PostDelayedTask(BrowserThread::FILE, FROM_HERE, reload_task_, | |
| 157 delay.InMilliseconds()); | |
| 158 } | |
| 159 | |
| 160 void FileBasedPolicyLoader::ScheduleFallbackReloadTask() { | |
| 161 // As a safeguard in case that the load delegate failed to timely notice a | |
| 162 // change in policy, schedule a reload task that'll make us recheck after a | |
| 163 // reasonable interval. | |
| 164 ScheduleReloadTask(reload_interval_); | |
| 165 } | |
| 166 | |
| 167 void FileBasedPolicyLoader::ReloadFromTask() { | |
| 168 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
| 169 | |
| 170 // Drop the reference to the reload task, since the task might be the only | |
| 171 // referer that keeps us alive, so we should not Cancel() it. | |
| 172 reload_task_ = NULL; | |
| 173 | |
| 174 Reload(); | |
| 175 } | 112 } |
| 176 | 113 |
| 177 bool FileBasedPolicyLoader::IsSafeToReloadPolicy( | 114 bool FileBasedPolicyLoader::IsSafeToReloadPolicy( |
| 178 const base::Time& now, | 115 const base::Time& now, |
| 179 base::TimeDelta* delay) { | 116 base::TimeDelta* delay) { |
| 180 DCHECK(delay); | 117 DCHECK(delay); |
| 181 | 118 |
| 182 // A null modification time indicates there's no data. | 119 // A null modification time indicates there's no data. |
| 183 FileBasedPolicyProvider::ProviderDelegate* provider_delegate = | 120 FileBasedPolicyProvider::ProviderDelegate* provider_delegate = |
| 184 static_cast<FileBasedPolicyProvider::ProviderDelegate*>(delegate()); | 121 static_cast<FileBasedPolicyProvider::ProviderDelegate*>(delegate()); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 198 base::TimeDelta age = now - last_modification_clock_; | 135 base::TimeDelta age = now - last_modification_clock_; |
| 199 if (age < settle_interval_) { | 136 if (age < settle_interval_) { |
| 200 *delay = settle_interval_ - age; | 137 *delay = settle_interval_ - age; |
| 201 return false; | 138 return false; |
| 202 } | 139 } |
| 203 | 140 |
| 204 return true; | 141 return true; |
| 205 } | 142 } |
| 206 | 143 |
| 207 } // namespace policy | 144 } // namespace policy |
| OLD | NEW |