OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "base/values.h" | 9 #include "base/values.h" |
10 #include "content/public/browser/browser_thread.h" | 10 #include "content/public/browser/browser_thread.h" |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
54 } | 54 } |
55 | 55 |
56 private: | 56 private: |
57 scoped_refptr<FileBasedPolicyLoader> loader_; | 57 scoped_refptr<FileBasedPolicyLoader> loader_; |
58 DISALLOW_COPY_AND_ASSIGN(FileBasedPolicyWatcherDelegate); | 58 DISALLOW_COPY_AND_ASSIGN(FileBasedPolicyWatcherDelegate); |
59 }; | 59 }; |
60 | 60 |
61 void FileBasedPolicyLoader::OnFilePathChanged( | 61 void FileBasedPolicyLoader::OnFilePathChanged( |
62 const FilePath& path) { | 62 const FilePath& path) { |
63 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 63 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
64 Reload(); | 64 Reload(false); |
65 } | 65 } |
66 | 66 |
67 void FileBasedPolicyLoader::OnFilePathError(const FilePath& path) { | 67 void FileBasedPolicyLoader::OnFilePathError(const FilePath& path) { |
68 LOG(ERROR) << "FilePathWatcher on " << path.value() | 68 LOG(ERROR) << "FilePathWatcher on " << path.value() |
69 << " failed."; | 69 << " failed."; |
70 } | 70 } |
71 | 71 |
72 void FileBasedPolicyLoader::Reload() { | 72 void FileBasedPolicyLoader::Reload(bool force) { |
73 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 73 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
74 | 74 |
75 if (!delegate()) | 75 if (!delegate()) |
76 return; | 76 return; |
77 | 77 |
78 // Check the directory time in order to see whether a reload is required. | 78 // Check the directory time in order to see whether a reload is required. |
79 base::TimeDelta delay; | 79 base::TimeDelta delay; |
80 base::Time now = base::Time::Now(); | 80 base::Time now = base::Time::Now(); |
81 if (!IsSafeToReloadPolicy(now, &delay)) { | 81 if (!force && !IsSafeToReloadPolicy(now, &delay)) { |
82 ScheduleReloadTask(delay); | 82 ScheduleReloadTask(delay); |
83 return; | 83 return; |
84 } | 84 } |
85 | 85 |
86 // Load the policy definitions. | 86 // Load the policy definitions. |
87 scoped_ptr<DictionaryValue> new_policy(delegate()->Load()); | 87 scoped_ptr<DictionaryValue> new_policy(delegate()->Load()); |
88 | 88 |
89 // Check again in case the directory has changed while reading it. | 89 // Check again in case the directory has changed while reading it. |
90 if (!IsSafeToReloadPolicy(now, &delay)) { | 90 if (!force && !IsSafeToReloadPolicy(now, &delay)) { |
91 ScheduleReloadTask(delay); | 91 ScheduleReloadTask(delay); |
92 return; | 92 return; |
93 } | 93 } |
94 | 94 |
95 PostUpdatePolicyTask(new_policy.release()); | 95 PostUpdatePolicyTask(new_policy.release()); |
96 | 96 |
97 ScheduleFallbackReloadTask(); | 97 ScheduleFallbackReloadTask(); |
98 } | 98 } |
99 | 99 |
100 void FileBasedPolicyLoader::InitOnFileThread() { | 100 void FileBasedPolicyLoader::InitOnFileThread() { |
101 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 101 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
102 watcher_.reset(new FilePathWatcher); | 102 watcher_.reset(new FilePathWatcher); |
103 const FilePath& path = config_file_path(); | 103 const FilePath& path = config_file_path(); |
104 if (!path.empty() && | 104 if (!path.empty() && |
105 !watcher_->Watch(path, new FileBasedPolicyWatcherDelegate(this))) { | 105 !watcher_->Watch(path, new FileBasedPolicyWatcherDelegate(this))) { |
106 OnFilePathError(path); | 106 OnFilePathError(path); |
107 } | 107 } |
108 | 108 |
109 // There might have been changes to the directory in the time between | 109 // There might have been changes to the directory in the time between |
110 // construction of the loader and initialization of the watcher. Call reload | 110 // construction of the loader and initialization of the watcher. Call reload |
111 // to detect if that is the case. | 111 // to detect if that is the case. |
112 Reload(); | 112 Reload(false); |
113 | 113 |
114 ScheduleFallbackReloadTask(); | 114 ScheduleFallbackReloadTask(); |
115 } | 115 } |
116 | 116 |
117 void FileBasedPolicyLoader::StopOnFileThread() { | 117 void FileBasedPolicyLoader::StopOnFileThread() { |
118 watcher_.reset(); | 118 watcher_.reset(); |
119 AsynchronousPolicyLoader::StopOnFileThread(); | 119 AsynchronousPolicyLoader::StopOnFileThread(); |
120 } | 120 } |
121 | 121 |
122 bool FileBasedPolicyLoader::IsSafeToReloadPolicy( | 122 bool FileBasedPolicyLoader::IsSafeToReloadPolicy( |
(...skipping 20 matching lines...) Expand all Loading... |
143 base::TimeDelta age = now - last_modification_clock_; | 143 base::TimeDelta age = now - last_modification_clock_; |
144 if (age < settle_interval_) { | 144 if (age < settle_interval_) { |
145 *delay = settle_interval_ - age; | 145 *delay = settle_interval_ - age; |
146 return false; | 146 return false; |
147 } | 147 } |
148 | 148 |
149 return true; | 149 return true; |
150 } | 150 } |
151 | 151 |
152 } // namespace policy | 152 } // namespace policy |
OLD | NEW |