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

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: fix build on Windows 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),
31 policy_(policy),
32 timestamp_(timestamp),
33 is_device_unmanaged_(is_device_unmanaged) {}
29 34
30 private: 35 private:
31 // Task override. 36 // Task override.
32 virtual void Run(); 37 virtual void Run();
33 38
34 const FilePath path_; 39 const FilePath path_;
35 scoped_ptr<const em::DevicePolicyResponse> policy_; 40 scoped_ptr<const em::DevicePolicyResponse> policy_;
36 const base::Time timestamp_; 41 const base::Time timestamp_;
42 const bool is_device_unmanaged_;
37 }; 43 };
38 44
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() { 45 void PersistPolicyTask::Run() {
48 std::string data; 46 std::string data;
49 em::CachedDevicePolicyResponse cached_policy; 47 em::CachedDevicePolicyResponse cached_policy;
50 cached_policy.mutable_policy()->CopyFrom(*policy_); 48 if (policy_.get())
49 cached_policy.mutable_policy()->CopyFrom(*policy_);
50 if (is_device_unmanaged_)
51 cached_policy.set_unmanaged(true);
51 cached_policy.set_timestamp(timestamp_.ToInternalValue()); 52 cached_policy.set_timestamp(timestamp_.ToInternalValue());
52 if (!cached_policy.SerializeToString(&data)) { 53 if (!cached_policy.SerializeToString(&data)) {
53 LOG(WARNING) << "Failed to serialize policy data"; 54 LOG(WARNING) << "Failed to serialize policy data";
54 return; 55 return;
55 } 56 }
56 57
57 int size = data.size(); 58 int size = data.size();
58 if (file_util::WriteFile(path_, data.c_str(), size) != size) { 59 if (file_util::WriteFile(path_, data.c_str(), size) != size) {
59 LOG(WARNING) << "Failed to write " << path_.value(); 60 LOG(WARNING) << "Failed to write " << path_.value();
60 return; 61 return;
61 } 62 }
62 } 63 }
63 64
64 DeviceManagementPolicyCache::DeviceManagementPolicyCache( 65 DeviceManagementPolicyCache::DeviceManagementPolicyCache(
65 const FilePath& backing_file_path) 66 const FilePath& backing_file_path)
66 : backing_file_path_(backing_file_path), 67 : backing_file_path_(backing_file_path),
67 policy_(new DictionaryValue), 68 policy_(new DictionaryValue),
68 fresh_policy_(false) { 69 fresh_policy_(false),
70 is_device_unmanaged_(false) {
69 } 71 }
70 72
71 void DeviceManagementPolicyCache::LoadPolicyFromFile() { 73 void DeviceManagementPolicyCache::LoadPolicyFromFile() {
72 if (!file_util::PathExists(backing_file_path_) || fresh_policy_) 74 if (!file_util::PathExists(backing_file_path_) || fresh_policy_)
73 return; 75 return;
74 76
75 // Read the protobuf from the file. 77 // Read the protobuf from the file.
76 std::string data; 78 std::string data;
77 if (!file_util::ReadFileToString(backing_file_path_, &data)) { 79 if (!file_util::ReadFileToString(backing_file_path_, &data)) {
78 LOG(WARNING) << "Failed to read policy data from " 80 LOG(WARNING) << "Failed to read policy data from "
79 << backing_file_path_.value(); 81 << backing_file_path_.value();
80 return; 82 return;
81 } 83 }
82 84
83 em::CachedDevicePolicyResponse cached_policy; 85 em::CachedDevicePolicyResponse cached_policy;
84 if (!cached_policy.ParseFromArray(data.c_str(), data.size())) { 86 if (!cached_policy.ParseFromArray(data.c_str(), data.size())) {
85 LOG(WARNING) << "Failed to parse policy data read from " 87 LOG(WARNING) << "Failed to parse policy data read from "
86 << backing_file_path_.value(); 88 << backing_file_path_.value();
87 return; 89 return;
88 } 90 }
89 91
90 // Reject files that claim to be from the future. 92 // Reject files that claim to be from the future.
91 base::Time timestamp = base::Time::FromInternalValue( 93 base::Time timestamp = base::Time::FromInternalValue(
92 cached_policy.timestamp()); 94 cached_policy.timestamp());
93 if (timestamp > base::Time::NowFromSystemTime()) { 95 if (timestamp > base::Time::NowFromSystemTime()) {
94 LOG(WARNING) << "Rejected policy data from " << backing_file_path_.value() 96 LOG(WARNING) << "Rejected policy data from " << backing_file_path_.value()
95 << ", file is from the future."; 97 << ", file is from the future.";
96 return; 98 return;
97 } 99 }
100 is_device_unmanaged_ = cached_policy.unmanaged();
98 101
99 // Decode and swap in the new policy information. 102 // Decode and swap in the new policy information.
100 scoped_ptr<DictionaryValue> value(DecodePolicy(cached_policy.policy())); 103 scoped_ptr<DictionaryValue> value(DecodePolicy(cached_policy.policy()));
101 { 104 {
102 AutoLock lock(lock_); 105 AutoLock lock(lock_);
103 if (!fresh_policy_) 106 if (!fresh_policy_)
104 policy_.reset(value.release()); 107 policy_.reset(value.release());
105 last_policy_refresh_time_ = timestamp; 108 last_policy_refresh_time_ = timestamp;
106 } 109 }
107 } 110 }
108 111
109 bool DeviceManagementPolicyCache::SetPolicy( 112 bool DeviceManagementPolicyCache::SetPolicy(
110 const em::DevicePolicyResponse& policy) { 113 const em::DevicePolicyResponse& policy) {
114 is_device_unmanaged_ = false;
111 DictionaryValue* value = DeviceManagementPolicyCache::DecodePolicy(policy); 115 DictionaryValue* value = DeviceManagementPolicyCache::DecodePolicy(policy);
112 const bool new_policy_differs = !(value->Equals(policy_.get())); 116 const bool new_policy_differs = !(value->Equals(policy_.get()));
113 base::Time now(base::Time::Now()); 117 base::Time now(base::Time::NowFromSystemTime());
114 { 118 {
115 AutoLock lock(lock_); 119 AutoLock lock(lock_);
116 policy_.reset(value); 120 policy_.reset(value);
117 fresh_policy_ = true; 121 fresh_policy_ = true;
118 last_policy_refresh_time_ = now; 122 last_policy_refresh_time_ = now;
119 } 123 }
120 124
121 em::DevicePolicyResponse* policy_copy = new em::DevicePolicyResponse; 125 em::DevicePolicyResponse* policy_copy = new em::DevicePolicyResponse;
122 policy_copy->CopyFrom(policy); 126 policy_copy->CopyFrom(policy);
123 BrowserThread::PostTask( 127 BrowserThread::PostTask(
124 BrowserThread::FILE, 128 BrowserThread::FILE,
125 FROM_HERE, 129 FROM_HERE,
126 new PersistPolicyTask(backing_file_path_, policy_copy, 130 new PersistPolicyTask(backing_file_path_, policy_copy, now, false));
127 base::Time::NowFromSystemTime()));
128 return new_policy_differs; 131 return new_policy_differs;
129 } 132 }
130 133
131 DictionaryValue* DeviceManagementPolicyCache::GetPolicy() { 134 DictionaryValue* DeviceManagementPolicyCache::GetPolicy() {
132 AutoLock lock(lock_); 135 AutoLock lock(lock_);
133 return static_cast<DictionaryValue*>(policy_->DeepCopy()); 136 return static_cast<DictionaryValue*>(policy_->DeepCopy());
134 } 137 }
135 138
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
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
« no previous file with comments | « chrome/browser/policy/device_management_policy_cache.h ('k') | chrome/browser/policy/device_management_policy_provider.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698