summaryrefslogtreecommitdiffstats
path: root/chrome/browser/policy/user_policy_identity_strategy.h
blob: 6ab07c0e551c915bf67a00e2711ec78ec8adb5f1 (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
// 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_IDENTITY_STRATEGY_H_
#define CHROME_BROWSER_POLICY_USER_POLICY_IDENTITY_STRATEGY_H_
#pragma once

#include <string>

#include "base/file_path.h"
#include "base/ref_counted.h"
#include "base/weak_ptr.h"
#include "chrome/browser/policy/cloud_policy_identity_strategy.h"
#include "content/common/notification_observer.h"
#include "content/common/notification_registrar.h"

class Profile;

namespace policy {

class DeviceManagementBackend;

// A token provider implementation that provides a user device token for the
// user corresponding to a given profile.
class UserPolicyIdentityStrategy : public CloudPolicyIdentityStrategy,
                                   public NotificationObserver {
 public:
  UserPolicyIdentityStrategy(Profile* profile,
                             const FilePath& token_cache_file);
  virtual ~UserPolicyIdentityStrategy();

  // CloudPolicyIdentityStrategy implementation:
  virtual std::string GetDeviceToken();
  virtual std::string GetDeviceID();
  virtual std::string GetMachineID();
  virtual em::DeviceRegisterRequest_Type GetPolicyRegisterType();
  virtual std::string GetPolicyType();

  virtual bool GetCredentials(std::string* username,
                              std::string* auth_token);
  virtual void OnDeviceTokenAvailable(const std::string& token);

 private:
  class TokenCache;

  // Checks whether a new token should be fetched and if so, sends out a
  // notification.
  void CheckAndTriggerFetch();

  // Gets the current user.
  std::string GetCurrentUser();

  // Called from the token cache when the token has been loaded.
  void OnCacheLoaded(const std::string& token, const std::string& device_id);

  // NotificationObserver method overrides:
  virtual void Observe(NotificationType type,
                       const NotificationSource& source,
                       const NotificationDetails& details);

  // The profile this provider is associated with.
  Profile* profile_;

  // Keeps the on-disk copy of the token.
  scoped_refptr<TokenCache> cache_;

  // The device ID we use.
  std::string device_id_;

  // Current device token. Empty if not available.
  std::string device_token_;

  // Registers the provider for notification of successful Gaia logins.
  NotificationRegistrar registrar_;

  // Allows to construct weak ptrs.
  base::WeakPtrFactory<UserPolicyIdentityStrategy> weak_ptr_factory_;

  DISALLOW_COPY_AND_ASSIGN(UserPolicyIdentityStrategy);
};

}  // namespace policy

#endif  // CHROME_BROWSER_POLICY_USER_POLICY_IDENTITY_STRATEGY_H_