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.
5 #ifndef CHROME_BROWSER_CHROMEOS_POLICY_USER_POLICY_DISK_CACHE_H_
6 #define CHROME_BROWSER_CHROMEOS_POLICY_USER_POLICY_DISK_CACHE_H_
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"
14 class SequencedTaskRunner
;
17 namespace enterprise_management
{
18 class CachedCloudPolicyResponse
;
23 // Handles the on-disk cache file used by UserPolicyCache. This class handles
24 // the necessary thread switching and may outlive the associated UserPolicyCache
26 class UserPolicyDiskCache
27 : public base::RefCountedThreadSafe
<UserPolicyDiskCache
> {
29 // Indicates the status of a completed load operation.
31 // Policy was loaded successfully.
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
,
41 // Delegate interface for observing loads operations.
45 virtual void OnDiskCacheLoaded(
47 const enterprise_management::CachedCloudPolicyResponse
& policy
) = 0;
51 const base::WeakPtr
<Delegate
>& delegate
,
52 const base::FilePath
& backing_file_path
,
53 scoped_refptr
<base::SequencedTaskRunner
> background_task_runner
);
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().
60 // Triggers a write operation to the disk cache on the FILE thread.
61 void Store(const enterprise_management::CachedCloudPolicyResponse
& policy
);
64 friend class base::RefCountedThreadSafe
<UserPolicyDiskCache
>;
65 ~UserPolicyDiskCache();
67 // Tries to load the cache file on the FILE thread.
68 void LoadOnFileThread();
70 // Forwards the result to the UI thread.
71 void LoadDone(LoadResult result
,
72 const enterprise_management::CachedCloudPolicyResponse
& policy
);
74 // Passes back the successfully read policy to the cache on the UI thread.
75 void ReportResultOnUIThread(
77 const enterprise_management::CachedCloudPolicyResponse
& policy
);
79 // Saves a policy blob on the FILE thread.
80 void StoreOnFileThread(
81 const enterprise_management::CachedCloudPolicyResponse
& policy
);
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_
;
88 DISALLOW_COPY_AND_ASSIGN(UserPolicyDiskCache
);
93 #endif // CHROME_BROWSER_CHROMEOS_POLICY_USER_POLICY_DISK_CACHE_H_