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 base {
14class SequencedTaskRunner;
15}
16
17namespace enterprise_management {
18class CachedCloudPolicyResponse;
19}
20
21namespace policy {
22
23// Handles the on-disk cache file used by UserPolicyCache. This class handles
24// the necessary thread switching and may outlive the associated UserPolicyCache
25// instance.
26class UserPolicyDiskCache
27    : public base::RefCountedThreadSafe<UserPolicyDiskCache> {
28 public:
29  // Indicates the status of a completed load operation.
30  enum LoadResult {
31    // Policy was loaded successfully.
32    LOAD_RESULT_SUCCESS,
33    // Cache file missing.
34    LOAD_RESULT_NOT_FOUND,
35    // Failed to read cache file.
36    LOAD_RESULT_READ_ERROR,
37    // Failed to parse cache file.
38    LOAD_RESULT_PARSE_ERROR,
39  };
40
41  // Delegate interface for observing loads operations.
42  class Delegate {
43   public:
44    virtual ~Delegate();
45    virtual void OnDiskCacheLoaded(
46        LoadResult result,
47        const enterprise_management::CachedCloudPolicyResponse& policy) = 0;
48  };
49
50  UserPolicyDiskCache(
51      const base::WeakPtr<Delegate>& delegate,
52      const base::FilePath& backing_file_path,
53      scoped_refptr<base::SequencedTaskRunner> background_task_runner);
54
55  // Starts reading the policy cache from disk. Passes the read policy
56  // information back to the hosting UserPolicyCache after a successful cache
57  // load through UserPolicyCache::OnDiskCacheLoaded().
58  void Load();
59
60  // Triggers a write operation to the disk cache on the FILE thread.
61  void Store(const enterprise_management::CachedCloudPolicyResponse& policy);
62
63 private:
64  friend class base::RefCountedThreadSafe<UserPolicyDiskCache>;
65  ~UserPolicyDiskCache();
66
67  // Tries to load the cache file on the FILE thread.
68  void LoadOnFileThread();
69
70  // Forwards the result to the UI thread.
71  void LoadDone(LoadResult result,
72                const enterprise_management::CachedCloudPolicyResponse& policy);
73
74  // Passes back the successfully read policy to the cache on the UI thread.
75  void ReportResultOnUIThread(
76      LoadResult result,
77      const enterprise_management::CachedCloudPolicyResponse& policy);
78
79  // Saves a policy blob on the FILE thread.
80  void StoreOnFileThread(
81      const enterprise_management::CachedCloudPolicyResponse& policy);
82
83  base::WeakPtr<Delegate> delegate_;
84  const base::FilePath backing_file_path_;
85  scoped_refptr<base::SequencedTaskRunner> origin_task_runner_;
86  scoped_refptr<base::SequencedTaskRunner> background_task_runner_;
87
88  DISALLOW_COPY_AND_ASSIGN(UserPolicyDiskCache);
89};
90
91}  // namespace policy
92
93#endif  // CHROME_BROWSER_CHROMEOS_POLICY_USER_POLICY_DISK_CACHE_H_
94