| 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/async_policy_loader.h" | 5 #include "chrome/browser/policy/async_policy_loader.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/location.h" | 8 #include "base/location.h" |
| 9 #include "base/sequenced_task_runner.h" | 9 #include "base/sequenced_task_runner.h" |
| 10 #include "chrome/browser/policy/policy_bundle.h" | 10 #include "chrome/browser/policy/policy_bundle.h" |
| 11 #include "chrome/browser/policy/policy_domain_descriptor.h" | |
| 12 | 11 |
| 13 using base::Time; | 12 using base::Time; |
| 14 using base::TimeDelta; | 13 using base::TimeDelta; |
| 15 | 14 |
| 16 namespace policy { | 15 namespace policy { |
| 17 | 16 |
| 18 namespace { | 17 namespace { |
| 19 | 18 |
| 20 // Amount of time to wait for the files on disk to settle before trying to load | 19 // Amount of time to wait for the files on disk to settle before trying to load |
| 21 // them. This alleviates the problem of reading partially written files and | 20 // them. This alleviates the problem of reading partially written files and |
| 22 // makes it possible to batch quasi-simultaneous changes. | 21 // makes it possible to batch quasi-simultaneous changes. |
| 23 const int kSettleIntervalSeconds = 5; | 22 const int kSettleIntervalSeconds = 5; |
| 24 | 23 |
| 25 // The time interval for rechecking policy. This is the fallback in case the | 24 // The time interval for rechecking policy. This is the fallback in case the |
| 26 // implementation never detects changes. | 25 // implementation never detects changes. |
| 27 const int kReloadIntervalSeconds = 15 * 60; | 26 const int kReloadIntervalSeconds = 15 * 60; |
| 28 | 27 |
| 29 } // namespace | 28 } // namespace |
| 30 | 29 |
| 31 AsyncPolicyLoader::AsyncPolicyLoader( | 30 AsyncPolicyLoader::AsyncPolicyLoader( |
| 32 scoped_refptr<base::SequencedTaskRunner> task_runner) | 31 scoped_refptr<base::SequencedTaskRunner> task_runner) |
| 33 : task_runner_(task_runner), | 32 : task_runner_(task_runner), |
| 34 weak_factory_(this) {} | 33 weak_factory_(this) {} |
| 35 | 34 |
| 36 AsyncPolicyLoader::~AsyncPolicyLoader() {} | 35 AsyncPolicyLoader::~AsyncPolicyLoader() {} |
| 37 | 36 |
| 38 base::Time AsyncPolicyLoader::LastModificationTime() { | 37 Time AsyncPolicyLoader::LastModificationTime() { |
| 39 return base::Time(); | 38 return Time(); |
| 40 } | 39 } |
| 41 | 40 |
| 42 void AsyncPolicyLoader::Reload(bool force) { | 41 void AsyncPolicyLoader::Reload(bool force) { |
| 43 DCHECK(task_runner_->RunsTasksOnCurrentThread()); | 42 DCHECK(task_runner_->RunsTasksOnCurrentThread()); |
| 44 | 43 |
| 45 TimeDelta delay; | 44 TimeDelta delay; |
| 46 Time now = Time::Now(); | 45 Time now = Time::Now(); |
| 47 // Check if there was a recent modification to the underlying files. | 46 // Check if there was a recent modification to the underlying files. |
| 48 if (!force && !IsSafeToReload(now, &delay)) { | 47 if (!force && !IsSafeToReload(now, &delay)) { |
| 49 ScheduleNextReload(delay); | 48 ScheduleNextReload(delay); |
| 50 return; | 49 return; |
| 51 } | 50 } |
| 52 | 51 |
| 53 scoped_ptr<PolicyBundle> bundle(Load()); | 52 scoped_ptr<PolicyBundle> bundle(Load()); |
| 54 | 53 |
| 55 // Check if there was a modification while reading. | 54 // Check if there was a modification while reading. |
| 56 if (!force && !IsSafeToReload(now, &delay)) { | 55 if (!force && !IsSafeToReload(now, &delay)) { |
| 57 ScheduleNextReload(delay); | 56 ScheduleNextReload(delay); |
| 58 return; | 57 return; |
| 59 } | 58 } |
| 60 | 59 |
| 61 // Filter out mismatching policies. | 60 // Filter out mismatching policies. |
| 62 for (DescriptorMap::iterator it = descriptor_map_.begin(); | 61 schema_map_->FilterBundle(bundle.get()); |
| 63 it != descriptor_map_.end(); ++it) { | |
| 64 it->second->FilterBundle(bundle.get()); | |
| 65 } | |
| 66 | 62 |
| 67 update_callback_.Run(bundle.Pass()); | 63 update_callback_.Run(bundle.Pass()); |
| 68 ScheduleNextReload(TimeDelta::FromSeconds(kReloadIntervalSeconds)); | 64 ScheduleNextReload(TimeDelta::FromSeconds(kReloadIntervalSeconds)); |
| 69 } | 65 } |
| 70 | 66 |
| 71 void AsyncPolicyLoader::RegisterPolicyDomain( | 67 scoped_ptr<PolicyBundle> AsyncPolicyLoader::InitialLoad( |
| 72 scoped_refptr<const PolicyDomainDescriptor> descriptor) { | 68 const scoped_refptr<SchemaMap>& schema_map) { |
| 73 if (descriptor->domain() != POLICY_DOMAIN_CHROME) { | |
| 74 descriptor_map_[descriptor->domain()] = descriptor; | |
| 75 Reload(true); | |
| 76 } | |
| 77 } | |
| 78 | |
| 79 scoped_ptr<PolicyBundle> AsyncPolicyLoader::InitialLoad() { | |
| 80 // This is the first load, early during startup. Use this to record the | 69 // This is the first load, early during startup. Use this to record the |
| 81 // initial |last_modification_time_|, so that potential changes made before | 70 // initial |last_modification_time_|, so that potential changes made before |
| 82 // installing the watches can be detected. | 71 // installing the watches can be detected. |
| 83 last_modification_time_ = LastModificationTime(); | 72 last_modification_time_ = LastModificationTime(); |
| 73 schema_map_ = schema_map; |
| 84 return Load(); | 74 return Load(); |
| 85 } | 75 } |
| 86 | 76 |
| 87 void AsyncPolicyLoader::Init(const UpdateCallback& update_callback) { | 77 void AsyncPolicyLoader::Init(const UpdateCallback& update_callback) { |
| 88 DCHECK(task_runner_->RunsTasksOnCurrentThread()); | 78 DCHECK(task_runner_->RunsTasksOnCurrentThread()); |
| 89 DCHECK(update_callback_.is_null()); | 79 DCHECK(update_callback_.is_null()); |
| 90 DCHECK(!update_callback.is_null()); | 80 DCHECK(!update_callback.is_null()); |
| 91 update_callback_ = update_callback; | 81 update_callback_ = update_callback; |
| 92 | 82 |
| 93 InitOnBackgroundThread(); | 83 InitOnBackgroundThread(); |
| 94 | 84 |
| 95 // There might have been changes to the underlying files since the initial | 85 // There might have been changes to the underlying files since the initial |
| 96 // load and before the watchers have been created. | 86 // load and before the watchers have been created. |
| 97 if (LastModificationTime() != last_modification_time_) | 87 if (LastModificationTime() != last_modification_time_) |
| 98 Reload(false); | 88 Reload(false); |
| 99 | 89 |
| 100 // Start periodic refreshes. | 90 // Start periodic refreshes. |
| 101 ScheduleNextReload(TimeDelta::FromSeconds(kReloadIntervalSeconds)); | 91 ScheduleNextReload(TimeDelta::FromSeconds(kReloadIntervalSeconds)); |
| 102 } | 92 } |
| 103 | 93 |
| 94 void AsyncPolicyLoader::RefreshPolicies(scoped_refptr<SchemaMap> schema_map) { |
| 95 DCHECK(task_runner_->RunsTasksOnCurrentThread()); |
| 96 schema_map_ = schema_map; |
| 97 Reload(true); |
| 98 } |
| 99 |
| 104 void AsyncPolicyLoader::ScheduleNextReload(TimeDelta delay) { | 100 void AsyncPolicyLoader::ScheduleNextReload(TimeDelta delay) { |
| 105 DCHECK(task_runner_->RunsTasksOnCurrentThread()); | 101 DCHECK(task_runner_->RunsTasksOnCurrentThread()); |
| 106 weak_factory_.InvalidateWeakPtrs(); | 102 weak_factory_.InvalidateWeakPtrs(); |
| 107 task_runner_->PostDelayedTask(FROM_HERE, | 103 task_runner_->PostDelayedTask(FROM_HERE, |
| 108 base::Bind(&AsyncPolicyLoader::Reload, | 104 base::Bind(&AsyncPolicyLoader::Reload, |
| 109 weak_factory_.GetWeakPtr(), | 105 weak_factory_.GetWeakPtr(), |
| 110 false /* force */), | 106 false /* force */), |
| 111 delay); | 107 delay); |
| 112 } | 108 } |
| 113 | 109 |
| 114 bool AsyncPolicyLoader::IsSafeToReload(const Time& now, TimeDelta* delay) { | 110 bool AsyncPolicyLoader::IsSafeToReload(const Time& now, TimeDelta* delay) { |
| 115 Time last_modification = LastModificationTime(); | 111 Time last_modification = LastModificationTime(); |
| 116 if (last_modification.is_null()) | 112 if (last_modification.is_null()) |
| 117 return true; | 113 return true; |
| 118 | 114 |
| 119 // If there was a change since the last recorded modification, wait some more. | 115 // If there was a change since the last recorded modification, wait some more. |
| 120 const TimeDelta kSettleInterval( | 116 const TimeDelta kSettleInterval( |
| 121 TimeDelta::FromSeconds(kSettleIntervalSeconds)); | 117 TimeDelta::FromSeconds(kSettleIntervalSeconds)); |
| 122 if (last_modification != last_modification_time_) { | 118 if (last_modification != last_modification_time_) { |
| 123 last_modification_time_ = last_modification; | 119 last_modification_time_ = last_modification; |
| 124 last_modification_clock_ = now; | 120 last_modification_clock_ = now; |
| 125 *delay = kSettleInterval; | 121 *delay = kSettleInterval; |
| 126 return false; | 122 return false; |
| 127 } | 123 } |
| 128 | 124 |
| 129 // Check whether the settle interval has elapsed. | 125 // Check whether the settle interval has elapsed. |
| 130 const base::TimeDelta age = now - last_modification_clock_; | 126 const TimeDelta age = now - last_modification_clock_; |
| 131 if (age < kSettleInterval) { | 127 if (age < kSettleInterval) { |
| 132 *delay = kSettleInterval - age; | 128 *delay = kSettleInterval - age; |
| 133 return false; | 129 return false; |
| 134 } | 130 } |
| 135 | 131 |
| 136 return true; | 132 return true; |
| 137 } | 133 } |
| 138 | 134 |
| 139 } // namespace policy | 135 } // namespace policy |
| OLD | NEW |