| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "components/policy/core/common/async_policy_loader.h" | 5 #include "components/policy/core/common/async_policy_loader.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/location.h" | 10 #include "base/location.h" |
| (...skipping 23 matching lines...) Expand all Loading... |
| 34 : task_runner_(task_runner), | 34 : task_runner_(task_runner), |
| 35 weak_factory_(this) {} | 35 weak_factory_(this) {} |
| 36 | 36 |
| 37 AsyncPolicyLoader::~AsyncPolicyLoader() {} | 37 AsyncPolicyLoader::~AsyncPolicyLoader() {} |
| 38 | 38 |
| 39 Time AsyncPolicyLoader::LastModificationTime() { | 39 Time AsyncPolicyLoader::LastModificationTime() { |
| 40 return Time(); | 40 return Time(); |
| 41 } | 41 } |
| 42 | 42 |
| 43 void AsyncPolicyLoader::Reload(bool force) { | 43 void AsyncPolicyLoader::Reload(bool force) { |
| 44 DCHECK(task_runner_->RunsTasksOnCurrentThread()); | 44 DCHECK(task_runner_->RunsTasksInCurrentSequence()); |
| 45 | 45 |
| 46 TimeDelta delay; | 46 TimeDelta delay; |
| 47 Time now = Time::Now(); | 47 Time now = Time::Now(); |
| 48 // Check if there was a recent modification to the underlying files. | 48 // Check if there was a recent modification to the underlying files. |
| 49 if (!force && !IsSafeToReload(now, &delay)) { | 49 if (!force && !IsSafeToReload(now, &delay)) { |
| 50 ScheduleNextReload(delay); | 50 ScheduleNextReload(delay); |
| 51 return; | 51 return; |
| 52 } | 52 } |
| 53 | 53 |
| 54 std::unique_ptr<PolicyBundle> bundle(Load()); | 54 std::unique_ptr<PolicyBundle> bundle(Load()); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 73 // installing the watches can be detected. | 73 // installing the watches can be detected. |
| 74 last_modification_time_ = LastModificationTime(); | 74 last_modification_time_ = LastModificationTime(); |
| 75 schema_map_ = schema_map; | 75 schema_map_ = schema_map; |
| 76 std::unique_ptr<PolicyBundle> bundle(Load()); | 76 std::unique_ptr<PolicyBundle> bundle(Load()); |
| 77 // Filter out mismatching policies. | 77 // Filter out mismatching policies. |
| 78 schema_map_->FilterBundle(bundle.get()); | 78 schema_map_->FilterBundle(bundle.get()); |
| 79 return bundle; | 79 return bundle; |
| 80 } | 80 } |
| 81 | 81 |
| 82 void AsyncPolicyLoader::Init(const UpdateCallback& update_callback) { | 82 void AsyncPolicyLoader::Init(const UpdateCallback& update_callback) { |
| 83 DCHECK(task_runner_->RunsTasksOnCurrentThread()); | 83 DCHECK(task_runner_->RunsTasksInCurrentSequence()); |
| 84 DCHECK(update_callback_.is_null()); | 84 DCHECK(update_callback_.is_null()); |
| 85 DCHECK(!update_callback.is_null()); | 85 DCHECK(!update_callback.is_null()); |
| 86 update_callback_ = update_callback; | 86 update_callback_ = update_callback; |
| 87 | 87 |
| 88 InitOnBackgroundThread(); | 88 InitOnBackgroundThread(); |
| 89 | 89 |
| 90 // There might have been changes to the underlying files since the initial | 90 // There might have been changes to the underlying files since the initial |
| 91 // load and before the watchers have been created. | 91 // load and before the watchers have been created. |
| 92 if (LastModificationTime() != last_modification_time_) | 92 if (LastModificationTime() != last_modification_time_) |
| 93 Reload(false); | 93 Reload(false); |
| 94 | 94 |
| 95 // Start periodic refreshes. | 95 // Start periodic refreshes. |
| 96 ScheduleNextReload(TimeDelta::FromSeconds(kReloadIntervalSeconds)); | 96 ScheduleNextReload(TimeDelta::FromSeconds(kReloadIntervalSeconds)); |
| 97 } | 97 } |
| 98 | 98 |
| 99 void AsyncPolicyLoader::RefreshPolicies(scoped_refptr<SchemaMap> schema_map) { | 99 void AsyncPolicyLoader::RefreshPolicies(scoped_refptr<SchemaMap> schema_map) { |
| 100 DCHECK(task_runner_->RunsTasksOnCurrentThread()); | 100 DCHECK(task_runner_->RunsTasksInCurrentSequence()); |
| 101 schema_map_ = schema_map; | 101 schema_map_ = schema_map; |
| 102 Reload(true); | 102 Reload(true); |
| 103 } | 103 } |
| 104 | 104 |
| 105 void AsyncPolicyLoader::ScheduleNextReload(TimeDelta delay) { | 105 void AsyncPolicyLoader::ScheduleNextReload(TimeDelta delay) { |
| 106 DCHECK(task_runner_->RunsTasksOnCurrentThread()); | 106 DCHECK(task_runner_->RunsTasksInCurrentSequence()); |
| 107 weak_factory_.InvalidateWeakPtrs(); | 107 weak_factory_.InvalidateWeakPtrs(); |
| 108 task_runner_->PostDelayedTask(FROM_HERE, | 108 task_runner_->PostDelayedTask(FROM_HERE, |
| 109 base::Bind(&AsyncPolicyLoader::Reload, | 109 base::Bind(&AsyncPolicyLoader::Reload, |
| 110 weak_factory_.GetWeakPtr(), | 110 weak_factory_.GetWeakPtr(), |
| 111 false /* force */), | 111 false /* force */), |
| 112 delay); | 112 delay); |
| 113 } | 113 } |
| 114 | 114 |
| 115 bool AsyncPolicyLoader::IsSafeToReload(const Time& now, TimeDelta* delay) { | 115 bool AsyncPolicyLoader::IsSafeToReload(const Time& now, TimeDelta* delay) { |
| 116 Time last_modification = LastModificationTime(); | 116 Time last_modification = LastModificationTime(); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 131 const TimeDelta age = now - last_modification_clock_; | 131 const TimeDelta age = now - last_modification_clock_; |
| 132 if (age < kSettleInterval) { | 132 if (age < kSettleInterval) { |
| 133 *delay = kSettleInterval - age; | 133 *delay = kSettleInterval - age; |
| 134 return false; | 134 return false; |
| 135 } | 135 } |
| 136 | 136 |
| 137 return true; | 137 return true; |
| 138 } | 138 } |
| 139 | 139 |
| 140 } // namespace policy | 140 } // namespace policy |
| OLD | NEW |