summaryrefslogtreecommitdiffstats
path: root/chrome/browser/policy/user_policy_token_cache.h
blob: e9ab99d84a1e799d28df83a1012536eb6b52d746 (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
// Copyright (c) 2011 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_USER_POLICY_TOKEN_CACHE_H_
#define CHROME_BROWSER_POLICY_USER_POLICY_TOKEN_CACHE_H_
#pragma once

#include <string>

#include "base/basictypes.h"
#include "base/file_path.h"
#include "base/memory/ref_counted.h"
#include "base/memory/weak_ptr.h"
#include "chrome/browser/policy/proto/device_management_local.pb.h"

namespace policy {

// Responsible for managing the on-disk token cache.
class UserPolicyTokenCache
    : public base::RefCountedThreadSafe<UserPolicyTokenCache> {
 public:
  // Callback interface for reporting a successfull load.
  class Delegate {
   public:
    virtual ~Delegate();
    virtual void OnTokenCacheLoaded(const std::string& token,
                                    const std::string& device_id) = 0;
  };

  UserPolicyTokenCache(const base::WeakPtr<Delegate>& delegate,
                       const FilePath& cache_file);

  // Starts loading the disk cache. After the load is finished, the result is
  // reported through the delegate.
  void Load();

  // Stores credentials asynchronously to disk.
  void Store(const std::string& token, const std::string& device_id);

 private:
  friend class base::RefCountedThreadSafe<UserPolicyTokenCache>;
  ~UserPolicyTokenCache();

  void LoadOnFileThread();
  void NotifyOnUIThread(const std::string& token,
                        const std::string& device_id);
  void StoreOnFileThread(const std::string& token,
                         const std::string& device_id);

  const base::WeakPtr<Delegate> delegate_;
  const FilePath cache_file_;

  DISALLOW_COPY_AND_ASSIGN(UserPolicyTokenCache);
};

}  // namespace policy

#endif  // CHROME_BROWSER_POLICY_USER_POLICY_TOKEN_CACHE_H_