OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/policy/user_cloud_policy_manager.h" | 5 #include "chrome/browser/policy/user_cloud_policy_manager.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
9 #include "chrome/browser/policy/cloud_policy_service.h" | 9 #include "chrome/browser/policy/cloud_policy_service.h" |
10 #include "chrome/browser/policy/policy_types.h" | 10 #include "chrome/browser/policy/policy_types.h" |
| 11 #include "chrome/browser/policy/user_cloud_policy_manager_factory.h" |
| 12 #include "chrome/browser/policy/user_cloud_policy_store.h" |
11 #include "chrome/common/pref_names.h" | 13 #include "chrome/common/pref_names.h" |
12 | 14 |
13 namespace policy { | 15 namespace policy { |
14 | 16 |
15 UserCloudPolicyManager::UserCloudPolicyManager( | 17 UserCloudPolicyManager::UserCloudPolicyManager( |
16 scoped_ptr<CloudPolicyStore> store, | 18 Profile* profile, |
17 bool wait_for_policy_fetch) | 19 scoped_ptr<UserCloudPolicyStore> store) |
18 : CloudPolicyManager(store.Pass()), | 20 : CloudPolicyManager(store.get()), |
19 wait_for_policy_fetch_(wait_for_policy_fetch) {} | 21 profile_(profile), |
| 22 store_(store.Pass()) { |
| 23 UserCloudPolicyManagerFactory::GetInstance()->Register(profile_, this); |
| 24 } |
20 | 25 |
21 UserCloudPolicyManager::~UserCloudPolicyManager() {} | 26 UserCloudPolicyManager::~UserCloudPolicyManager() { |
| 27 UserCloudPolicyManagerFactory::GetInstance()->Unregister(profile_, this); |
| 28 } |
22 | 29 |
23 // static | 30 // static |
24 scoped_ptr<UserCloudPolicyManager> UserCloudPolicyManager::Create( | 31 scoped_ptr<UserCloudPolicyManager> UserCloudPolicyManager::Create( |
25 Profile* profile, | 32 Profile* profile, |
26 PolicyInit policy_init) { | 33 bool force_immediate_load) { |
27 scoped_ptr<CloudPolicyStore> store = | 34 scoped_ptr<policy::UserCloudPolicyStore> store( |
28 CloudPolicyStore::CreateUserPolicyStore( | 35 policy::UserCloudPolicyStore::Create(profile)); |
29 profile, policy_init == POLICY_INIT_IMMEDIATELY); | 36 if (force_immediate_load) |
30 return make_scoped_ptr(new UserCloudPolicyManager( | 37 store->LoadImmediately(); |
31 store.Pass(), policy_init == POLICY_INIT_REFRESH_FROM_SERVER)); | 38 return make_scoped_ptr( |
| 39 new policy::UserCloudPolicyManager(profile, store.Pass())); |
32 } | 40 } |
33 | 41 |
34 void UserCloudPolicyManager::Initialize( | 42 void UserCloudPolicyManager::Initialize( |
35 PrefService* local_state, | 43 PrefService* local_state, |
36 DeviceManagementService* device_management_service, | 44 DeviceManagementService* device_management_service) { |
37 UserAffiliation user_affiliation) { | 45 InitializeService( |
38 DCHECK(device_management_service); | 46 make_scoped_ptr(new CloudPolicyClient(std::string(), std::string(), |
39 DCHECK(local_state); | 47 USER_AFFILIATION_NONE, |
40 local_state_ = local_state; | 48 POLICY_SCOPE_USER, NULL, |
41 scoped_ptr<CloudPolicyClient> client( | 49 device_management_service))); |
42 new CloudPolicyClient(std::string(), std::string(), user_affiliation, | 50 StartRefreshScheduler(local_state, prefs::kUserPolicyRefreshRate); |
43 POLICY_SCOPE_USER, NULL, | |
44 device_management_service)); | |
45 InitializeService(client.Pass()); | |
46 cloud_policy_client()->AddObserver(this); | |
47 | |
48 if (wait_for_policy_fetch_) { | |
49 // If we are supposed to wait for a policy fetch, we trigger an explicit | |
50 // policy refresh at startup that allows us to unblock initialization once | |
51 // done. The refresh scheduler only gets started once that refresh | |
52 // completes. Note that we might have to wait for registration to happen, | |
53 // see OnRegistrationStateChanged() below. | |
54 if (cloud_policy_client()->is_registered()) { | |
55 cloud_policy_service()->RefreshPolicy( | |
56 base::Bind(&UserCloudPolicyManager::OnInitialPolicyFetchComplete, | |
57 base::Unretained(this))); | |
58 } | |
59 } else { | |
60 CancelWaitForPolicyFetch(); | |
61 } | |
62 } | 51 } |
63 | 52 |
64 void UserCloudPolicyManager::ShutdownAndRemovePolicy() { | 53 void UserCloudPolicyManager::ShutdownAndRemovePolicy() { |
65 if (cloud_policy_client()) | |
66 cloud_policy_client()->RemoveObserver(this); | |
67 ShutdownService(); | 54 ShutdownService(); |
68 local_state_ = NULL; | 55 store_->Clear(); |
69 cloud_policy_store()->Clear(); | |
70 } | |
71 | |
72 void UserCloudPolicyManager::CancelWaitForPolicyFetch() { | |
73 wait_for_policy_fetch_ = false; | |
74 CheckAndPublishPolicy(); | |
75 | |
76 // Now that |wait_for_policy_fetch_| is guaranteed to be false, the scheduler | |
77 // can be started. | |
78 if (cloud_policy_service() && local_state_) | |
79 StartRefreshScheduler(local_state_, prefs::kUserPolicyRefreshRate); | |
80 } | 56 } |
81 | 57 |
82 bool UserCloudPolicyManager::IsClientRegistered() const { | 58 bool UserCloudPolicyManager::IsClientRegistered() const { |
83 return cloud_policy_client() && cloud_policy_client()->is_registered(); | 59 return cloud_policy_client() && cloud_policy_client()->is_registered(); |
84 } | 60 } |
85 | 61 |
86 void UserCloudPolicyManager::RegisterClient(const std::string& access_token) { | 62 void UserCloudPolicyManager::RegisterClient(const std::string& access_token) { |
87 DCHECK(cloud_policy_client()) << "Callers must invoke Initialize() first"; | 63 DCHECK(cloud_policy_client()) << "Callers must invoke Initialize() first"; |
88 if (!cloud_policy_client()->is_registered()) { | 64 if (!cloud_policy_client()->is_registered()) { |
89 DVLOG(1) << "Registering client with access token: " << access_token; | 65 DVLOG(1) << "Registering client with access token: " << access_token; |
90 cloud_policy_client()->Register(access_token); | 66 cloud_policy_client()->Register(access_token); |
91 } | 67 } |
92 } | 68 } |
93 | 69 |
94 void UserCloudPolicyManager::Shutdown() { | |
95 if (cloud_policy_client()) | |
96 cloud_policy_client()->RemoveObserver(this); | |
97 CloudPolicyManager::Shutdown(); | |
98 } | |
99 | |
100 bool UserCloudPolicyManager::IsInitializationComplete() const { | |
101 return CloudPolicyManager::IsInitializationComplete() && | |
102 !wait_for_policy_fetch_; | |
103 } | |
104 | |
105 void UserCloudPolicyManager::OnPolicyFetched(CloudPolicyClient* client) { | |
106 // No action required. If we're blocked on a policy fetch, we'll learn about | |
107 // completion of it through OnInitialPolicyFetchComplete(). | |
108 } | |
109 | |
110 void UserCloudPolicyManager::OnRegistrationStateChanged( | |
111 CloudPolicyClient* client) { | |
112 DCHECK_EQ(cloud_policy_client(), client); | |
113 if (wait_for_policy_fetch_) { | |
114 // If we're blocked on the policy fetch, now is a good time to issue it. | |
115 if (cloud_policy_client()->is_registered()) { | |
116 cloud_policy_service()->RefreshPolicy( | |
117 base::Bind(&UserCloudPolicyManager::OnInitialPolicyFetchComplete, | |
118 base::Unretained(this))); | |
119 } else { | |
120 // If the client has switched to not registered, we bail out as this | |
121 // indicates the cloud policy setup flow has been aborted. | |
122 CancelWaitForPolicyFetch(); | |
123 } | |
124 } | |
125 } | |
126 | |
127 void UserCloudPolicyManager::OnClientError(CloudPolicyClient* client) { | |
128 DCHECK_EQ(cloud_policy_client(), client); | |
129 CancelWaitForPolicyFetch(); | |
130 } | |
131 | |
132 void UserCloudPolicyManager::OnInitialPolicyFetchComplete() { | |
133 CancelWaitForPolicyFetch(); | |
134 } | |
135 | |
136 } // namespace policy | 70 } // namespace policy |
OLD | NEW |