Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(785)

Side by Side Diff: chrome/browser/policy/device_management_policy_cache.cc

Issue 5331008: Persist 'this device is not managed' information (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: port to ToT/5198006-ps14 Created 10 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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),
Mattias Nissler (ping if slow) 2010/11/26 15:07:30 fix indentation.
Jakob Kummerow 2010/11/26 16:04:03 Done.
31 policy_(policy),
32 timestamp_(timestamp),
33 is_device_unmanaged_(is_device_unmanaged) {
Mattias Nissler (ping if slow) 2010/11/26 15:07:30 You may move the closing brace to the end of this
Jakob Kummerow 2010/11/26 16:04:03 Done.
34 }
29 35
30 private: 36 private:
31 // Task override. 37 // Task override.
32 virtual void Run(); 38 virtual void Run();
33 39
34 const FilePath path_; 40 const FilePath path_;
35 scoped_ptr<const em::DevicePolicyResponse> policy_; 41 scoped_ptr<const em::DevicePolicyResponse> policy_;
36 const base::Time timestamp_; 42 const base::Time timestamp_;
43 const bool is_device_unmanaged_;
37 }; 44 };
38 45
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
47 void PersistPolicyTask::Run() { 46 void PersistPolicyTask::Run() {
48 std::string data; 47 std::string data;
49 em::CachedDevicePolicyResponse cached_policy; 48 em::CachedDevicePolicyResponse cached_policy;
50 cached_policy.mutable_policy()->CopyFrom(*policy_); 49 if (policy_.get())
50 cached_policy.mutable_policy()->CopyFrom(*policy_);
51 if (is_device_unmanaged_)
52 cached_policy.set_unmanaged(true);
51 cached_policy.set_timestamp(timestamp_.ToInternalValue()); 53 cached_policy.set_timestamp(timestamp_.ToInternalValue());
52 if (!cached_policy.SerializeToString(&data)) { 54 if (!cached_policy.SerializeToString(&data)) {
53 LOG(WARNING) << "Failed to serialize policy data"; 55 LOG(WARNING) << "Failed to serialize policy data";
54 return; 56 return;
55 } 57 }
56 58
57 int size = data.size(); 59 int size = data.size();
58 if (file_util::WriteFile(path_, data.c_str(), size) != size) { 60 if (file_util::WriteFile(path_, data.c_str(), size) != size) {
59 LOG(WARNING) << "Failed to write " << path_.value(); 61 LOG(WARNING) << "Failed to write " << path_.value();
60 return; 62 return;
61 } 63 }
62 } 64 }
63 65
64 DeviceManagementPolicyCache::DeviceManagementPolicyCache( 66 DeviceManagementPolicyCache::DeviceManagementPolicyCache(
65 const FilePath& backing_file_path) 67 const FilePath& backing_file_path)
66 : backing_file_path_(backing_file_path), 68 : backing_file_path_(backing_file_path),
67 policy_(new DictionaryValue), 69 policy_(new DictionaryValue),
68 fresh_policy_(false) { 70 fresh_policy_(false),
71 is_device_unmanaged_(false) {
69 } 72 }
70 73
71 void DeviceManagementPolicyCache::LoadPolicyFromFile() { 74 void DeviceManagementPolicyCache::LoadPolicyFromFile() {
72 if (!file_util::PathExists(backing_file_path_) || fresh_policy_) 75 if (!file_util::PathExists(backing_file_path_) || fresh_policy_)
73 return; 76 return;
74 77
75 // Read the protobuf from the file. 78 // Read the protobuf from the file.
76 std::string data; 79 std::string data;
77 if (!file_util::ReadFileToString(backing_file_path_, &data)) { 80 if (!file_util::ReadFileToString(backing_file_path_, &data)) {
78 LOG(WARNING) << "Failed to read policy data from " 81 LOG(WARNING) << "Failed to read policy data from "
79 << backing_file_path_.value(); 82 << backing_file_path_.value();
80 return; 83 return;
81 } 84 }
82 85
83 em::CachedDevicePolicyResponse cached_policy; 86 em::CachedDevicePolicyResponse cached_policy;
84 if (!cached_policy.ParseFromArray(data.c_str(), data.size())) { 87 if (!cached_policy.ParseFromArray(data.c_str(), data.size())) {
85 LOG(WARNING) << "Failed to parse policy data read from " 88 LOG(WARNING) << "Failed to parse policy data read from "
86 << backing_file_path_.value(); 89 << backing_file_path_.value();
87 return; 90 return;
88 } 91 }
89 92
90 // Reject files that claim to be from the future. 93 // Reject files that claim to be from the future.
91 base::Time timestamp = base::Time::FromInternalValue( 94 base::Time timestamp = base::Time::FromInternalValue(
92 cached_policy.timestamp()); 95 cached_policy.timestamp());
93 if (timestamp > base::Time::NowFromSystemTime()) { 96 if (timestamp > base::Time::NowFromSystemTime()) {
94 LOG(WARNING) << "Rejected policy data from " << backing_file_path_.value() 97 LOG(WARNING) << "Rejected policy data from " << backing_file_path_.value()
95 << ", file is from the future."; 98 << ", file is from the future.";
96 return; 99 return;
97 } 100 }
101 is_device_unmanaged_ = cached_policy.unmanaged();
98 102
99 // Decode and swap in the new policy information. 103 // Decode and swap in the new policy information.
100 scoped_ptr<DictionaryValue> value(DecodePolicy(cached_policy.policy())); 104 scoped_ptr<DictionaryValue> value(DecodePolicy(cached_policy.policy()));
101 { 105 {
102 AutoLock lock(lock_); 106 AutoLock lock(lock_);
103 if (!fresh_policy_) 107 if (!fresh_policy_)
104 policy_.reset(value.release()); 108 policy_.reset(value.release());
105 last_policy_refresh_time_ = timestamp; 109 last_policy_refresh_time_ = timestamp;
106 } 110 }
107 } 111 }
108 112
109 bool DeviceManagementPolicyCache::SetPolicy( 113 bool DeviceManagementPolicyCache::SetPolicy(
110 const em::DevicePolicyResponse& policy) { 114 const em::DevicePolicyResponse& policy) {
115 is_device_unmanaged_ = false;
111 DictionaryValue* value = DeviceManagementPolicyCache::DecodePolicy(policy); 116 DictionaryValue* value = DeviceManagementPolicyCache::DecodePolicy(policy);
112 const bool new_policy_differs = !(value->Equals(policy_.get())); 117 const bool new_policy_differs = !(value->Equals(policy_.get()));
113 base::Time now(base::Time::Now()); 118 base::Time now(base::Time::NowFromSystemTime());
114 { 119 {
115 AutoLock lock(lock_); 120 AutoLock lock(lock_);
116 policy_.reset(value); 121 policy_.reset(value);
117 fresh_policy_ = true; 122 fresh_policy_ = true;
118 last_policy_refresh_time_ = now; 123 last_policy_refresh_time_ = now;
119 } 124 }
120 125
121 em::DevicePolicyResponse* policy_copy = new em::DevicePolicyResponse; 126 em::DevicePolicyResponse* policy_copy = new em::DevicePolicyResponse;
122 policy_copy->CopyFrom(policy); 127 policy_copy->CopyFrom(policy);
123 BrowserThread::PostTask( 128 BrowserThread::PostTask(
124 BrowserThread::FILE, 129 BrowserThread::FILE,
125 FROM_HERE, 130 FROM_HERE,
126 new PersistPolicyTask(backing_file_path_, policy_copy, 131 new PersistPolicyTask(backing_file_path_, policy_copy, now, false));
127 base::Time::NowFromSystemTime()));
128 return new_policy_differs; 132 return new_policy_differs;
129 } 133 }
130 134
131 DictionaryValue* DeviceManagementPolicyCache::GetPolicy() { 135 DictionaryValue* DeviceManagementPolicyCache::GetPolicy() {
132 AutoLock lock(lock_); 136 AutoLock lock(lock_);
133 return static_cast<DictionaryValue*>(policy_->DeepCopy()); 137 return static_cast<DictionaryValue*>(policy_->DeepCopy());
134 } 138 }
135 139
140 void DeviceManagementPolicyCache::SetDeviceUnmanaged(bool is_device_unmanaged) {
141 if (is_device_unmanaged_ == is_device_unmanaged)
142 return;
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
136 // static 161 // static
137 Value* DeviceManagementPolicyCache::DecodeIntegerValue( 162 Value* DeviceManagementPolicyCache::DecodeIntegerValue(
138 google::protobuf::int64 value) { 163 google::protobuf::int64 value) {
139 if (value < std::numeric_limits<int>::min() || 164 if (value < std::numeric_limits<int>::min() ||
140 value > std::numeric_limits<int>::max()) { 165 value > std::numeric_limits<int>::max()) {
141 LOG(WARNING) << "Integer value " << value 166 LOG(WARNING) << "Integer value " << value
142 << " out of numeric limits, ignoring."; 167 << " out of numeric limits, ignoring.";
143 return NULL; 168 return NULL;
144 } 169 }
145 170
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after
242 DeviceManagementPolicyCache::DecodeValue(named_value->value()); 267 DeviceManagementPolicyCache::DecodeValue(named_value->value());
243 if (decoded_value) 268 if (decoded_value)
244 result->Set(named_value->name(), decoded_value); 269 result->Set(named_value->name(), decoded_value);
245 } 270 }
246 } 271 }
247 } 272 }
248 return result; 273 return result;
249 } 274 }
250 275
251 } // namespace policy 276 } // namespace policy
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698