- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / policy / cloud / user_policy_signin_service_base.h
1 // Copyright 2013 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_POLICY_CLOUD_USER_POLICY_SIGNIN_SERVICE_BASE_H_
6 #define CHROME_BROWSER_POLICY_CLOUD_USER_POLICY_SIGNIN_SERVICE_BASE_H_
7
8 #include <string>
9
10 #include "base/basictypes.h"
11 #include "base/callback.h"
12 #include "base/compiler_specific.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/memory/weak_ptr.h"
16 #include "chrome/browser/policy/cloud/cloud_policy_client.h"
17 #include "chrome/browser/policy/cloud/cloud_policy_service.h"
18 #include "components/browser_context_keyed_service/browser_context_keyed_service.h"
19 #include "content/public/browser/notification_observer.h"
20 #include "content/public/browser/notification_registrar.h"
21
22 class PrefService;
23 class Profile;
24 class SigninManager;
25
26 namespace net {
27 class URLRequestContextGetter;
28 }
29
30 namespace policy {
31
32 class DeviceManagementService;
33 class UserCloudPolicyManager;
34
35 // The UserPolicySigninService is responsible for interacting with the policy
36 // infrastructure (mainly UserCloudPolicyManager) to load policy for the signed
37 // in user. This is the base class that contains shared behavior.
38 //
39 // At signin time, this class initializes the UCPM and loads policy before any
40 // other signed in services are initialized. After each restart, this class
41 // ensures that the CloudPolicyClient is registered (in case the policy server
42 // was offline during the initial policy fetch) and if not it initiates a fresh
43 // registration process.
44 //
45 // Finally, if the user signs out, this class is responsible for shutting down
46 // the policy infrastructure to ensure that any cached policy is cleared.
47 class UserPolicySigninServiceBase : public BrowserContextKeyedService,
48                                     public CloudPolicyClient::Observer,
49                                     public CloudPolicyService::Observer,
50                                     public content::NotificationObserver {
51  public:
52   // The callback invoked once policy registration is complete. Passed
53   // CloudPolicyClient parameter is null if DMToken fetch failed.
54   typedef base::Callback<void(scoped_ptr<CloudPolicyClient>)>
55       PolicyRegistrationCallback;
56
57   // The callback invoked once policy fetch is complete. Passed boolean
58   // parameter is set to true if the policy fetch succeeded.
59   typedef base::Callback<void(bool)> PolicyFetchCallback;
60
61   // Creates a UserPolicySigninServiceBase associated with the passed |profile|.
62   UserPolicySigninServiceBase(
63       Profile* profile,
64       PrefService* local_state,
65       scoped_refptr<net::URLRequestContextGetter> request_context,
66       DeviceManagementService* device_management_service);
67   virtual ~UserPolicySigninServiceBase();
68
69   // Initiates a policy fetch as part of user signin, using a CloudPolicyClient
70   // previously initialized via RegisterPolicyClient. |callback| is invoked
71   // once the policy fetch is complete, passing true if the policy fetch
72   // succeeded.
73   void FetchPolicyForSignedInUser(scoped_ptr<CloudPolicyClient> client,
74                                   const PolicyFetchCallback& callback);
75
76   // content::NotificationObserver implementation:
77   virtual void Observe(int type,
78                        const content::NotificationSource& source,
79                        const content::NotificationDetails& details) OVERRIDE;
80
81   // CloudPolicyService::Observer implementation:
82   virtual void OnInitializationCompleted(CloudPolicyService* service) OVERRIDE;
83
84   // CloudPolicyClient::Observer implementation:
85   virtual void OnPolicyFetched(CloudPolicyClient* client) OVERRIDE;
86   virtual void OnRegistrationStateChanged(CloudPolicyClient* client) OVERRIDE;
87   virtual void OnClientError(CloudPolicyClient* client) OVERRIDE;
88
89   // BrowserContextKeyedService implementation:
90   virtual void Shutdown() OVERRIDE;
91
92  protected:
93   // Returns true if policy should be loaded even when Gaia reports that the
94   // account doesn't have management enabled.
95   static bool ShouldForceLoadPolicy();
96
97   // Returns a CloudPolicyClient to perform a registration with the DM server,
98   // or NULL if |username| shouldn't register for policy management.
99   scoped_ptr<CloudPolicyClient> PrepareToRegister(const std::string& username);
100
101   // Returns false if cloud policy is disabled or if the passed |email_address|
102   // is definitely not from a hosted domain (according to the blacklist in
103   // BrowserPolicyConnector::IsNonEnterpriseUser()).
104   bool ShouldLoadPolicyForUser(const std::string& email_address);
105
106   // Invoked to initialize the UserPolicySigninService once its owning Profile
107   // becomes ready. If the Profile has a signed-in account associated with it
108   // at startup then this initializes the cloud policy manager by calling
109   // InitializeForSignedInUser(); otherwise it clears any stored policies.
110   void InitializeOnProfileReady();
111
112   // Invoked to initialize the cloud policy service for |username|, which is the
113   // account associated with the Profile that owns this service. This is invoked
114   // from InitializeOnProfileReady() if the Profile already has a signed-in
115   // account at startup, or (on the desktop platforms) as soon as the user
116   // signs-in and an OAuth2 login refresh token becomes available.
117   void InitializeForSignedInUser(const std::string& username);
118
119   // Initializes the cloud policy manager with the passed |client|. This is
120   // called from InitializeForSignedInUser() when the Profile already has a
121   // signed in account at startup, and from FetchPolicyForSignedInUser() during
122   // the initial policy fetch after signing in.
123   virtual void InitializeUserCloudPolicyManager(
124       scoped_ptr<CloudPolicyClient> client);
125
126   // Prepares for the UserCloudPolicyManager to be shutdown due to
127   // user signout or profile destruction.
128   virtual void PrepareForUserCloudPolicyManagerShutdown();
129
130   // Shuts down the UserCloudPolicyManager (for example, after the user signs
131   // out) and deletes any cached policy.
132   virtual void ShutdownUserCloudPolicyManager();
133
134   // Convenience helper to get the UserCloudPolicyManager for |profile_|.
135   UserCloudPolicyManager* GetManager();
136
137   Profile* profile() { return profile_; }
138   content::NotificationRegistrar* registrar() { return &registrar_; }
139   SigninManager* GetSigninManager();
140
141  private:
142   // Weak pointer to the profile this service is associated with.
143   Profile* profile_;
144
145   content::NotificationRegistrar registrar_;
146
147   PrefService* local_state_;
148   scoped_refptr<net::URLRequestContextGetter> request_context_;
149   DeviceManagementService* device_management_service_;
150
151   base::WeakPtrFactory<UserPolicySigninServiceBase> weak_factory_;
152
153   DISALLOW_COPY_AND_ASSIGN(UserPolicySigninServiceBase);
154 };
155
156 }  // namespace policy
157
158 #endif  // CHROME_BROWSER_POLICY_CLOUD_USER_POLICY_SIGNIN_SERVICE_BASE_H_