user_policy_disk_cache.h revision 2a99a7e74a7f215066514fe81d2bfa6639d9eddd
1// Copyright (c) 2012 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef CHROME_BROWSER_CHROMEOS_POLICY_USER_POLICY_DISK_CACHE_H_
6#define CHROME_BROWSER_CHROMEOS_POLICY_USER_POLICY_DISK_CACHE_H_
7
8#include "base/basictypes.h"
9#include "base/files/file_path.h"
10#include "base/memory/ref_counted.h"
11#include "base/memory/weak_ptr.h"
12
13namespace enterprise_management {
14class CachedCloudPolicyResponse;
15}
16
17namespace policy {
18
19// Handles the on-disk cache file used by UserPolicyCache. This class handles
20// the necessary thread switching and may outlive the associated UserPolicyCache
21// instance.
22class UserPolicyDiskCache
23    : public base::RefCountedThreadSafe<UserPolicyDiskCache> {
24 public:
25  // Indicates the status of a completed load operation.
26  enum LoadResult {
27    // Policy was loaded successfully.
28    LOAD_RESULT_SUCCESS,
29    // Cache file missing.
30    LOAD_RESULT_NOT_FOUND,
31    // Failed to read cache file.
32    LOAD_RESULT_READ_ERROR,
33    // Failed to parse cache file.
34    LOAD_RESULT_PARSE_ERROR,
35  };
36
37  // Delegate interface for observing loads operations.
38  class Delegate {
39   public:
40    virtual ~Delegate();
41    virtual void OnDiskCacheLoaded(
42        LoadResult result,
43        const enterprise_management::CachedCloudPolicyResponse& policy) = 0;
44  };
45
46  UserPolicyDiskCache(const base::WeakPtr<Delegate>& delegate,
47                      const base::FilePath& backing_file_path);
48
49  // Starts reading the policy cache from disk. Passes the read policy
50  // information back to the hosting UserPolicyCache after a successful cache
51  // load through UserPolicyCache::OnDiskCacheLoaded().
52  void Load();
53
54  // Triggers a write operation to the disk cache on the FILE thread.
55  void Store(const enterprise_management::CachedCloudPolicyResponse& policy);
56
57 private:
58  friend class base::RefCountedThreadSafe<UserPolicyDiskCache>;
59  ~UserPolicyDiskCache();
60
61  // Tries to load the cache file on the FILE thread.
62  void LoadOnFileThread();
63
64  // Forwards the result to the UI thread.
65  void LoadDone(LoadResult result,
66                const enterprise_management::CachedCloudPolicyResponse& policy);
67
68  // Passes back the successfully read policy to the cache on the UI thread.
69  void ReportResultOnUIThread(
70      LoadResult result,
71      const enterprise_management::CachedCloudPolicyResponse& policy);
72
73  // Saves a policy blob on the FILE thread.
74  void StoreOnFileThread(
75      const enterprise_management::CachedCloudPolicyResponse& policy);
76
77  base::WeakPtr<Delegate> delegate_;
78  const base::FilePath backing_file_path_;
79
80  DISALLOW_COPY_AND_ASSIGN(UserPolicyDiskCache);
81};
82
83}  // namespace policy
84
85#endif  // CHROME_BROWSER_CHROMEOS_POLICY_USER_POLICY_DISK_CACHE_H_
86