| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 #include "base/files/file_path_watcher.h" | 7 #include "base/files/file_path_watcher.h" |
| 8 #include "base/memory/ref_counted.h" | 8 #include "base/memory/ref_counted.h" |
| 9 #include "chrome/browser/policy/policy_map.h" | 9 #include "chrome/browser/policy/policy_bundle.h" |
| 10 #include "content/public/browser/browser_thread.h" | 10 #include "content/public/browser/browser_thread.h" |
| 11 | 11 |
| 12 using ::base::files::FilePathWatcher; | 12 using ::base::files::FilePathWatcher; |
| 13 using content::BrowserThread; | 13 using content::BrowserThread; |
| 14 | 14 |
| 15 namespace { | 15 namespace { |
| 16 | 16 |
| 17 // Amount of time we wait for the files on disk to settle before trying to load | 17 // Amount of time we wait for the files on disk to settle before trying to load |
| 18 // them. This alleviates the problem of reading partially written files and | 18 // them. This alleviates the problem of reading partially written files and |
| 19 // makes it possible to batch quasi-simultaneous changes. | 19 // makes it possible to batch quasi-simultaneous changes. |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 67 | 67 |
| 68 void FileBasedPolicyLoader::OnFilePathError(const FilePath& path) { | 68 void FileBasedPolicyLoader::OnFilePathError(const FilePath& path) { |
| 69 LOG(ERROR) << "FilePathWatcher on " << path.value() | 69 LOG(ERROR) << "FilePathWatcher on " << path.value() |
| 70 << " failed."; | 70 << " failed."; |
| 71 } | 71 } |
| 72 | 72 |
| 73 void FileBasedPolicyLoader::Reload(bool force) { | 73 void FileBasedPolicyLoader::Reload(bool force) { |
| 74 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 74 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 75 | 75 |
| 76 if (!delegate()) { | 76 if (!delegate()) { |
| 77 PostUpdatePolicyTask(NULL); | 77 scoped_ptr<PolicyBundle> empty_bundle; |
| 78 PostUpdatePolicyTask(empty_bundle.Pass()); |
| 78 return; | 79 return; |
| 79 } | 80 } |
| 80 | 81 |
| 81 // Check the directory time in order to see whether a reload is required. | 82 // Check the directory time in order to see whether a reload is required. |
| 82 base::TimeDelta delay; | 83 base::TimeDelta delay; |
| 83 base::Time now = base::Time::Now(); | 84 base::Time now = base::Time::Now(); |
| 84 if (!force && !IsSafeToReloadPolicy(now, &delay)) { | 85 if (!force && !IsSafeToReloadPolicy(now, &delay)) { |
| 85 ScheduleReloadTask(delay); | 86 ScheduleReloadTask(delay); |
| 86 return; | 87 return; |
| 87 } | 88 } |
| 88 | 89 |
| 89 // Load the policy definitions. | 90 // Load the policy definitions. |
| 90 scoped_ptr<PolicyMap> new_policy(delegate()->Load()); | 91 scoped_ptr<PolicyBundle> bundle(delegate()->Load()); |
| 91 | 92 |
| 92 // Check again in case the directory has changed while reading it. | 93 // Check again in case the directory has changed while reading it. |
| 93 if (!force && !IsSafeToReloadPolicy(now, &delay)) { | 94 if (!force && !IsSafeToReloadPolicy(now, &delay)) { |
| 94 ScheduleReloadTask(delay); | 95 ScheduleReloadTask(delay); |
| 95 return; | 96 return; |
| 96 } | 97 } |
| 97 | 98 |
| 98 PostUpdatePolicyTask(new_policy.release()); | 99 PostUpdatePolicyTask(bundle.Pass()); |
| 99 | 100 |
| 100 ScheduleFallbackReloadTask(); | 101 ScheduleFallbackReloadTask(); |
| 101 } | 102 } |
| 102 | 103 |
| 103 void FileBasedPolicyLoader::InitOnFileThread() { | 104 void FileBasedPolicyLoader::InitOnFileThread() { |
| 104 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 105 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 105 watcher_.reset(new FilePathWatcher); | 106 watcher_.reset(new FilePathWatcher); |
| 106 const FilePath& path = config_file_path(); | 107 const FilePath& path = config_file_path(); |
| 107 if (!path.empty() && | 108 if (!path.empty() && |
| 108 !watcher_->Watch(path, new FileBasedPolicyWatcherDelegate(this))) { | 109 !watcher_->Watch(path, new FileBasedPolicyWatcherDelegate(this))) { |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 146 base::TimeDelta age = now - last_modification_clock_; | 147 base::TimeDelta age = now - last_modification_clock_; |
| 147 if (age < settle_interval_) { | 148 if (age < settle_interval_) { |
| 148 *delay = settle_interval_ - age; | 149 *delay = settle_interval_ - age; |
| 149 return false; | 150 return false; |
| 150 } | 151 } |
| 151 | 152 |
| 152 return true; | 153 return true; |
| 153 } | 154 } |
| 154 | 155 |
| 155 } // namespace policy | 156 } // namespace policy |
| OLD | NEW |