OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2011 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 #include <algorithm> | |
6 #include <string> | |
7 | |
8 #include "base/command_line.h" | |
9 #include "chrome/browser/browser_process.h" | |
10 #include "chrome/browser/net/gaia/token_service.h" | |
11 #include "chrome/browser/policy/browser_policy_connector.h" | |
12 #include "chrome/browser/policy/cloud_policy_provider.h" | |
13 #include "chrome/browser/policy/cloud_policy_subsystem.h" | |
14 #include "chrome/browser/policy/configuration_policy_pref_store.h" | |
15 #include "chrome/browser/policy/user_policy_connector.h" | |
16 #include "chrome/browser/policy/user_policy_cache.h" | |
17 #include "chrome/browser/policy/user_policy_identity_strategy.h" | |
18 #include "chrome/browser/prefs/pref_service.h" | |
19 #include "chrome/common/net/gaia/gaia_constants.h" | |
20 #include "chrome/common/chrome_switches.h" | |
21 #include "content/common/notification_details.h" | |
22 #include "content/common/notification_service.h" | |
23 #include "content/common/notification_source.h" | |
24 | |
25 #if defined(OS_CHROMEOS) | |
26 #include "chrome/browser/chromeos/login/user_manager.h" | |
27 #endif | |
28 | |
29 namespace { | |
30 | |
31 const FilePath::CharType kPolicyDir[] = FILE_PATH_LITERAL("Device Management"); | |
32 const FilePath::CharType kTokenCacheFile[] = FILE_PATH_LITERAL("Token"); | |
33 const FilePath::CharType kPolicyCacheFile[] = FILE_PATH_LITERAL("Policy"); | |
34 | |
35 } // namespace | |
36 | |
37 namespace policy { | |
38 | |
39 // static | |
40 UserPolicyConnector* UserPolicyConnector::Create() { | |
41 return new UserPolicyConnector(); | |
42 } | |
Joao da Silva
2011/05/31 14:50:23
Why a static Create() instead of making the ctor p
sfeuz
2011/06/03 08:30:35
Obsolete.
| |
43 | |
44 UserPolicyConnector::UserPolicyConnector() { | |
45 managed_cloud_provider_.reset(new CloudPolicyProvider( | |
46 ConfigurationPolicyPrefStore::GetChromePolicyDefinitionList(), | |
47 CloudPolicyCacheBase::POLICY_LEVEL_MANDATORY)); | |
48 recommended_cloud_provider_.reset(new CloudPolicyProvider( | |
49 ConfigurationPolicyPrefStore::GetChromePolicyDefinitionList(), | |
50 CloudPolicyCacheBase::POLICY_LEVEL_RECOMMENDED)); | |
51 } | |
52 | |
53 UserPolicyConnector::~UserPolicyConnector() { | |
54 managed_cloud_provider_.reset(); | |
55 recommended_cloud_provider_.reset(); | |
56 cloud_policy_subsystem_.reset(); | |
57 identity_strategy_.reset(); | |
58 } | |
59 | |
60 void UserPolicyConnector::Initialize(std::string& user_name, | |
61 const FilePath& policy_dir, | |
Mattias Nissler (ping if slow)
2011/05/31 14:14:19
indentation
sfeuz
2011/06/03 08:30:35
Done.
| |
62 TokenService* token_service) { | |
63 // Throw away the old backend. | |
64 cloud_policy_subsystem_.reset(); | |
65 identity_strategy_.reset(); | |
66 registrar_.RemoveAll(); | |
67 | |
68 CommandLine* command_line = CommandLine::ForCurrentProcess(); | |
69 if (command_line->HasSwitch(switches::kDeviceManagementUrl)) { | |
70 token_service_ = token_service; | |
71 registrar_.Add(this, | |
72 NotificationType::TOKEN_AVAILABLE, | |
73 Source<TokenService>(token_service_)); | |
74 | |
75 // Register for the event of user login on CrOS to make sure that the user | |
76 // is not changing while the UserPolicyConnector is active. | |
77 #if defined(OS_CHROMEOS) | |
78 registrar_.Add(this, | |
79 NotificationType::LOGIN_USER_CHANGED, | |
80 NotificationService::AllSources()); | |
81 #endif | |
82 FilePath policy_cache_dir = policy_dir.Append(kPolicyDir); | |
83 UserPolicyCache* user_policy_cache = | |
84 new UserPolicyCache(policy_cache_dir.Append(kPolicyCacheFile)); | |
Mattias Nissler (ping if slow)
2011/05/31 14:14:19
indentation
sfeuz
2011/06/03 08:30:35
Done.
| |
85 managed_cloud_provider_->set_cache(user_policy_cache); | |
86 recommended_cloud_provider_->set_cache(user_policy_cache); | |
87 identity_strategy_.reset(new UserPolicyIdentityStrategy(user_name, | |
88 policy_cache_dir.Append(kTokenCacheFile))); | |
89 cloud_policy_subsystem_.reset(new CloudPolicySubsystem( | |
90 identity_strategy_.get(), | |
91 user_policy_cache)); | |
92 | |
93 // Initiate the DM-Token load. | |
94 identity_strategy_->LoadTokenCache(); | |
95 | |
96 // In case the token of |token_service_| is already available we set it | |
97 // directly, since there will be no notification for it. | |
98 if (token_service_->HasTokenForService( | |
99 GaiaConstants::kDeviceManagementService)) { | |
100 identity_strategy_->SetAuthToken( | |
101 token_service_->GetTokenForService( | |
102 GaiaConstants::kDeviceManagementService)); | |
103 } | |
104 | |
105 // TODO(sfeuz): This already assumes that user policy refresh rate | |
106 // preference lives in local_state. Adapted once the PolicyRefreshRate CL is | |
107 // landed. | |
108 cloud_policy_subsystem_->Initialize(g_browser_process->local_state()); | |
109 } | |
110 } | |
111 | |
112 CloudPolicyProvider* | |
113 UserPolicyConnector::GetManagedCloudProvider() const { | |
114 return managed_cloud_provider_.get(); | |
115 } | |
116 | |
117 CloudPolicyProvider* | |
118 UserPolicyConnector::GetRecommendedCloudProvider() const { | |
119 return recommended_cloud_provider_.get(); | |
120 } | |
121 | |
122 void UserPolicyConnector::Observe(NotificationType type, | |
123 const NotificationSource& source, | |
Joao da Silva
2011/05/31 14:50:23
Nit: indentation.
sfeuz
2011/06/03 08:30:35
Done.
| |
124 const NotificationDetails& details) { | |
125 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
126 if (type == NotificationType::TOKEN_AVAILABLE) { | |
127 const TokenService::TokenAvailableDetails* token_details = | |
128 Details<const TokenService::TokenAvailableDetails>(details).ptr(); | |
129 if (token_details->service() == GaiaConstants::kDeviceManagementService) | |
Mattias Nissler (ping if slow)
2011/05/31 14:14:19
You should also make sure that |source| correspond
sfeuz
2011/06/03 08:30:35
Done.
| |
130 if (identity_strategy_.get()) | |
131 identity_strategy_->SetAuthToken(token_details->token()); | |
132 #if defined(OS_CHROMEOS) | |
133 } else if (type == NotificationType::LOGIN_USER_CHANGED) { | |
Mattias Nissler (ping if slow)
2011/05/31 14:14:19
I don't understand why we require handling this no
sfeuz
2011/06/03 08:30:35
Basically yes.
Also still debugging what happens i
| |
134 const chromeos::UserManager::User* user_details = | |
135 Details<const chromeos::UserManager::User>(details).ptr(); | |
136 std::string current_username, current_auth_token; | |
137 identity_strategy_->GetCredentials(¤t_username, ¤t_auth_token); | |
138 DCHECK_EQ(current_username, user_details->email()); | |
139 #endif | |
140 } else { | |
141 NOTREACHED(); | |
142 } | |
143 } | |
144 | |
145 } // namespace policy | |
OLD | NEW |