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/device_management_policy_cache.h" | 5 #include "chrome/browser/policy/device_management_policy_cache.h" |
6 | 6 |
7 #include <limits> | 7 #include <limits> |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/file_util.h" | 10 #include "base/file_util.h" |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "base/task.h" | 12 #include "base/task.h" |
13 #include "base/values.h" | 13 #include "base/values.h" |
14 #include "chrome/browser/browser_thread.h" | 14 #include "chrome/browser/browser_thread.h" |
15 #include "chrome/browser/policy/proto/device_management_constants.h" | 15 #include "chrome/browser/policy/proto/device_management_constants.h" |
16 #include "chrome/browser/policy/proto/device_management_local.pb.h" | 16 #include "chrome/browser/policy/proto/device_management_local.pb.h" |
17 | 17 |
18 using google::protobuf::RepeatedField; | 18 using google::protobuf::RepeatedField; |
19 using google::protobuf::RepeatedPtrField; | 19 using google::protobuf::RepeatedPtrField; |
20 | 20 |
21 namespace policy { | 21 namespace policy { |
22 | 22 |
23 // Saves policy information to a file. | 23 // Saves policy information to a file. |
24 class PersistPolicyTask : public Task { | 24 class PersistPolicyTask : public Task { |
25 public: | 25 public: |
26 PersistPolicyTask(const FilePath& path, | 26 PersistPolicyTask(const FilePath& path, |
27 const em::DevicePolicyResponse* policy, | 27 const em::DevicePolicyResponse* policy, |
28 const base::Time& timestamp, | 28 const base::Time& timestamp); |
29 const bool is_device_unmanaged) | |
30 : path_(path), | |
31 policy_(policy), | |
32 timestamp_(timestamp), | |
33 is_device_unmanaged_(is_device_unmanaged) {} | |
34 | 29 |
35 private: | 30 private: |
36 // Task override. | 31 // Task override. |
37 virtual void Run(); | 32 virtual void Run(); |
38 | 33 |
39 const FilePath path_; | 34 const FilePath path_; |
40 scoped_ptr<const em::DevicePolicyResponse> policy_; | 35 scoped_ptr<const em::DevicePolicyResponse> policy_; |
41 const base::Time timestamp_; | 36 const base::Time timestamp_; |
42 const bool is_device_unmanaged_; | |
43 }; | 37 }; |
44 | 38 |
| 39 PersistPolicyTask::PersistPolicyTask(const FilePath& path, |
| 40 const em::DevicePolicyResponse* policy, |
| 41 const base::Time& timestamp) |
| 42 : path_(path), |
| 43 policy_(policy), |
| 44 timestamp_(timestamp) { |
| 45 } |
| 46 |
45 void PersistPolicyTask::Run() { | 47 void PersistPolicyTask::Run() { |
46 std::string data; | 48 std::string data; |
47 em::CachedDevicePolicyResponse cached_policy; | 49 em::CachedDevicePolicyResponse cached_policy; |
48 if (policy_.get()) | 50 cached_policy.mutable_policy()->CopyFrom(*policy_); |
49 cached_policy.mutable_policy()->CopyFrom(*policy_); | |
50 if (is_device_unmanaged_) | |
51 cached_policy.set_unmanaged(true); | |
52 cached_policy.set_timestamp(timestamp_.ToInternalValue()); | 51 cached_policy.set_timestamp(timestamp_.ToInternalValue()); |
53 if (!cached_policy.SerializeToString(&data)) { | 52 if (!cached_policy.SerializeToString(&data)) { |
54 LOG(WARNING) << "Failed to serialize policy data"; | 53 LOG(WARNING) << "Failed to serialize policy data"; |
55 return; | 54 return; |
56 } | 55 } |
57 | 56 |
58 int size = data.size(); | 57 int size = data.size(); |
59 if (file_util::WriteFile(path_, data.c_str(), size) != size) { | 58 if (file_util::WriteFile(path_, data.c_str(), size) != size) { |
60 LOG(WARNING) << "Failed to write " << path_.value(); | 59 LOG(WARNING) << "Failed to write " << path_.value(); |
61 return; | 60 return; |
62 } | 61 } |
63 } | 62 } |
64 | 63 |
65 DeviceManagementPolicyCache::DeviceManagementPolicyCache( | 64 DeviceManagementPolicyCache::DeviceManagementPolicyCache( |
66 const FilePath& backing_file_path) | 65 const FilePath& backing_file_path) |
67 : backing_file_path_(backing_file_path), | 66 : backing_file_path_(backing_file_path), |
68 policy_(new DictionaryValue), | 67 policy_(new DictionaryValue), |
69 fresh_policy_(false), | 68 fresh_policy_(false) { |
70 is_device_unmanaged_(false) { | |
71 } | 69 } |
72 | 70 |
73 void DeviceManagementPolicyCache::LoadPolicyFromFile() { | 71 void DeviceManagementPolicyCache::LoadPolicyFromFile() { |
74 if (!file_util::PathExists(backing_file_path_) || fresh_policy_) | 72 if (!file_util::PathExists(backing_file_path_) || fresh_policy_) |
75 return; | 73 return; |
76 | 74 |
77 // Read the protobuf from the file. | 75 // Read the protobuf from the file. |
78 std::string data; | 76 std::string data; |
79 if (!file_util::ReadFileToString(backing_file_path_, &data)) { | 77 if (!file_util::ReadFileToString(backing_file_path_, &data)) { |
80 LOG(WARNING) << "Failed to read policy data from " | 78 LOG(WARNING) << "Failed to read policy data from " |
81 << backing_file_path_.value(); | 79 << backing_file_path_.value(); |
82 return; | 80 return; |
83 } | 81 } |
84 | 82 |
85 em::CachedDevicePolicyResponse cached_policy; | 83 em::CachedDevicePolicyResponse cached_policy; |
86 if (!cached_policy.ParseFromArray(data.c_str(), data.size())) { | 84 if (!cached_policy.ParseFromArray(data.c_str(), data.size())) { |
87 LOG(WARNING) << "Failed to parse policy data read from " | 85 LOG(WARNING) << "Failed to parse policy data read from " |
88 << backing_file_path_.value(); | 86 << backing_file_path_.value(); |
89 return; | 87 return; |
90 } | 88 } |
91 | 89 |
92 // Reject files that claim to be from the future. | 90 // Reject files that claim to be from the future. |
93 base::Time timestamp = base::Time::FromInternalValue( | 91 base::Time timestamp = base::Time::FromInternalValue( |
94 cached_policy.timestamp()); | 92 cached_policy.timestamp()); |
95 if (timestamp > base::Time::NowFromSystemTime()) { | 93 if (timestamp > base::Time::NowFromSystemTime()) { |
96 LOG(WARNING) << "Rejected policy data from " << backing_file_path_.value() | 94 LOG(WARNING) << "Rejected policy data from " << backing_file_path_.value() |
97 << ", file is from the future."; | 95 << ", file is from the future."; |
98 return; | 96 return; |
99 } | 97 } |
100 is_device_unmanaged_ = cached_policy.unmanaged(); | |
101 | 98 |
102 // Decode and swap in the new policy information. | 99 // Decode and swap in the new policy information. |
103 scoped_ptr<DictionaryValue> value(DecodePolicy(cached_policy.policy())); | 100 scoped_ptr<DictionaryValue> value(DecodePolicy(cached_policy.policy())); |
104 { | 101 { |
105 AutoLock lock(lock_); | 102 AutoLock lock(lock_); |
106 if (!fresh_policy_) | 103 if (!fresh_policy_) |
107 policy_.reset(value.release()); | 104 policy_.reset(value.release()); |
108 last_policy_refresh_time_ = timestamp; | 105 last_policy_refresh_time_ = timestamp; |
109 } | 106 } |
110 } | 107 } |
111 | 108 |
112 bool DeviceManagementPolicyCache::SetPolicy( | 109 bool DeviceManagementPolicyCache::SetPolicy( |
113 const em::DevicePolicyResponse& policy) { | 110 const em::DevicePolicyResponse& policy) { |
114 is_device_unmanaged_ = false; | |
115 DictionaryValue* value = DeviceManagementPolicyCache::DecodePolicy(policy); | 111 DictionaryValue* value = DeviceManagementPolicyCache::DecodePolicy(policy); |
116 const bool new_policy_differs = !(value->Equals(policy_.get())); | 112 const bool new_policy_differs = !(value->Equals(policy_.get())); |
117 base::Time now(base::Time::NowFromSystemTime()); | 113 base::Time now(base::Time::Now()); |
118 { | 114 { |
119 AutoLock lock(lock_); | 115 AutoLock lock(lock_); |
120 policy_.reset(value); | 116 policy_.reset(value); |
121 fresh_policy_ = true; | 117 fresh_policy_ = true; |
122 last_policy_refresh_time_ = now; | 118 last_policy_refresh_time_ = now; |
123 } | 119 } |
124 | 120 |
125 em::DevicePolicyResponse* policy_copy = new em::DevicePolicyResponse; | 121 em::DevicePolicyResponse* policy_copy = new em::DevicePolicyResponse; |
126 policy_copy->CopyFrom(policy); | 122 policy_copy->CopyFrom(policy); |
127 BrowserThread::PostTask( | 123 BrowserThread::PostTask( |
128 BrowserThread::FILE, | 124 BrowserThread::FILE, |
129 FROM_HERE, | 125 FROM_HERE, |
130 new PersistPolicyTask(backing_file_path_, policy_copy, now, false)); | 126 new PersistPolicyTask(backing_file_path_, policy_copy, |
| 127 base::Time::NowFromSystemTime())); |
131 return new_policy_differs; | 128 return new_policy_differs; |
132 } | 129 } |
133 | 130 |
134 DictionaryValue* DeviceManagementPolicyCache::GetPolicy() { | 131 DictionaryValue* DeviceManagementPolicyCache::GetPolicy() { |
135 AutoLock lock(lock_); | 132 AutoLock lock(lock_); |
136 return static_cast<DictionaryValue*>(policy_->DeepCopy()); | 133 return static_cast<DictionaryValue*>(policy_->DeepCopy()); |
137 } | 134 } |
138 | 135 |
139 void DeviceManagementPolicyCache::SetDeviceUnmanaged(bool is_device_unmanaged) { | |
140 if (is_device_unmanaged_ == is_device_unmanaged) | |
141 return; | |
142 | |
143 is_device_unmanaged_ = is_device_unmanaged; | |
144 base::Time now(base::Time::NowFromSystemTime()); | |
145 DictionaryValue* empty = new DictionaryValue(); | |
146 { | |
147 AutoLock lock(lock_); | |
148 policy_.reset(empty); | |
149 last_policy_refresh_time_ = now; | |
150 } | |
151 BrowserThread::PostTask( | |
152 BrowserThread::FILE, | |
153 FROM_HERE, | |
154 new PersistPolicyTask(backing_file_path_, | |
155 (is_device_unmanaged ? NULL | |
156 : new em::DevicePolicyResponse()), | |
157 now, | |
158 is_device_unmanaged_)); | |
159 } | |
160 | |
161 // static | 136 // static |
162 Value* DeviceManagementPolicyCache::DecodeIntegerValue( | 137 Value* DeviceManagementPolicyCache::DecodeIntegerValue( |
163 google::protobuf::int64 value) { | 138 google::protobuf::int64 value) { |
164 if (value < std::numeric_limits<int>::min() || | 139 if (value < std::numeric_limits<int>::min() || |
165 value > std::numeric_limits<int>::max()) { | 140 value > std::numeric_limits<int>::max()) { |
166 LOG(WARNING) << "Integer value " << value | 141 LOG(WARNING) << "Integer value " << value |
167 << " out of numeric limits, ignoring."; | 142 << " out of numeric limits, ignoring."; |
168 return NULL; | 143 return NULL; |
169 } | 144 } |
170 | 145 |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
267 DeviceManagementPolicyCache::DecodeValue(named_value->value()); | 242 DeviceManagementPolicyCache::DecodeValue(named_value->value()); |
268 if (decoded_value) | 243 if (decoded_value) |
269 result->Set(named_value->name(), decoded_value); | 244 result->Set(named_value->name(), decoded_value); |
270 } | 245 } |
271 } | 246 } |
272 } | 247 } |
273 return result; | 248 return result; |
274 } | 249 } |
275 | 250 |
276 } // namespace policy | 251 } // namespace policy |
OLD | NEW |