summaryrefslogtreecommitdiffstats
path: root/chrome/browser/policy/device_management_policy_cache.h
blob: 9c09a07c91a7da75016c4e55daa80e6d28bc0d5b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_POLICY_DEVICE_MANAGEMENT_POLICY_CACHE_H_
#define CHROME_BROWSER_POLICY_DEVICE_MANAGEMENT_POLICY_CACHE_H_

#include "base/file_path.h"
#include "base/gtest_prod_util.h"
#include "base/lock.h"
#include "base/ref_counted.h"
#include "base/scoped_ptr.h"
#include "base/time.h"
#include "chrome/browser/policy/proto/device_management_backend.pb.h"

class DictionaryValue;
class Value;

namespace policy {

namespace em = enterprise_management;

// Keeps the authoritative copy of cloud policy information as read from the
// persistence file or determined by the policy backend. The cache doesn't talk
// to the service directly, but receives updated policy information through
// SetPolicy() calls, which is then persisted and decoded into the internal
// Value representation chrome uses.
class DeviceManagementPolicyCache {
 public:
  explicit DeviceManagementPolicyCache(const FilePath& backing_file_path);
  ~DeviceManagementPolicyCache();

  // Loads policy information from the backing file. Non-existing or erroneous
  // cache files are ignored.
  void LoadPolicyFromFile();

  // Resets the policy information. Returns true if the new policy is different
  // from the previously stored policy.
  bool SetPolicy(const em::DevicePolicyResponse& policy);

  // Gets the policy information. Ownership of the return value is transferred
  // to the caller.
  DictionaryValue* GetPolicy();

  void SetDeviceUnmanaged(bool is_device_unmanaged);
  bool is_device_unmanaged() const {
    return is_device_unmanaged_;
  }

  // Returns the time as which the policy was last fetched.
  base::Time last_policy_refresh_time() const {
    return last_policy_refresh_time_;
  }

 private:
  friend class DeviceManagementPolicyCacheDecodeTest;
  FRIEND_TEST_ALL_PREFIXES(DeviceManagementPolicyCacheDecodeTest, DecodePolicy);

  // Decodes an int64 value. Checks whether the passed value fits the numeric
  // limits of the value representation. Returns a value (ownership is
  // transferred to the caller) on success, NULL on failure.
  static Value* DecodeIntegerValue(google::protobuf::int64 value);

  // Decode a GenericValue message to the Value representation used internally.
  // Returns NULL if |value| is invalid (i.e. contains no actual value).
  static Value* DecodeValue(const em::GenericValue& value);

  // Decodes a policy message and returns it in Value representation. Ownership
  // of the returned dictionary is transferred to the caller.
  static DictionaryValue* DecodePolicy(
      const em::DevicePolicyResponse& response);

  // The file in which we store a cached version of the policy information.
  const FilePath backing_file_path_;

  // Protects |policy_|.
  Lock lock_;

  // Policy key-value information.
  scoped_ptr<DictionaryValue> policy_;

  // Tracks whether the store received a SetPolicy() call, which overrides any
  // information loaded from the file.
  bool fresh_policy_;

  bool is_device_unmanaged_;

  // The time at which the policy was last refreshed.
  base::Time last_policy_refresh_time_;
};

}  // namespace policy

#endif  // CHROME_BROWSER_POLICY_DEVICE_MANAGEMENT_POLICY_CACHE_H_