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 "base/file_util.h" | |
10 #include "chrome/browser/browser_process.h" | |
11 #include "chrome/browser/policy/browser_policy_connector.h" | |
12 #include "chrome/browser/policy/cloud_policy_subsystem.h" | |
13 #include "chrome/browser/policy/configuration_policy_pref_store.h" | |
14 #include "chrome/browser/policy/profile_policy_connector.h" | |
15 #include "chrome/browser/policy/user_policy_cache.h" | |
16 #include "chrome/browser/policy/user_policy_identity_strategy.h" | |
17 #include "chrome/browser/prefs/pref_service.h" | |
18 #include "chrome/browser/profiles/profile.h" | |
19 #include "chrome/common/chrome_switches.h" | |
20 | |
21 namespace { | |
22 | |
23 const FilePath::CharType kPolicyDir[] = FILE_PATH_LITERAL("Device Management"); | |
24 const FilePath::CharType kTokenCacheFile[] = FILE_PATH_LITERAL("Token"); | |
25 const FilePath::CharType kPolicyCacheFile[] = FILE_PATH_LITERAL("Policy"); | |
26 | |
27 } // namespace | |
28 | |
29 namespace policy { | |
30 | |
31 ProfilePolicyConnector::ProfilePolicyConnector(Profile* profile) | |
32 : profile_(profile) { | |
33 CommandLine* command_line = CommandLine::ForCurrentProcess(); | |
34 if (command_line->HasSwitch(switches::kDeviceManagementUrl)) { | |
35 FilePath policy_cache_dir(profile_->GetPath()); | |
36 policy_cache_dir = policy_cache_dir.Append(kPolicyDir); | |
37 | |
38 identity_strategy_.reset(new UserPolicyIdentityStrategy( | |
39 profile_, | |
40 policy_cache_dir.Append(kTokenCacheFile))); | |
41 cloud_policy_subsystem_.reset(new CloudPolicySubsystem( | |
42 identity_strategy_.get(), | |
43 new UserPolicyCache(policy_cache_dir.Append(kPolicyCacheFile)))); | |
44 | |
45 BrowserPolicyConnector* browser_connector = | |
46 g_browser_process->browser_policy_connector(); | |
47 | |
48 managed_cloud_provider_.reset(new MergingPolicyProvider( | |
49 browser_connector->GetManagedCloudProvider(), | |
50 cloud_policy_subsystem_->GetManagedPolicyProvider())); | |
51 recommended_cloud_provider_.reset(new MergingPolicyProvider( | |
52 browser_connector->GetRecommendedCloudProvider(), | |
53 cloud_policy_subsystem_->GetRecommendedPolicyProvider())); | |
54 } | |
55 } | |
56 | |
57 ProfilePolicyConnector::~ProfilePolicyConnector() { | |
58 managed_cloud_provider_.reset(); | |
59 recommended_cloud_provider_.reset(); | |
60 cloud_policy_subsystem_.reset(); | |
61 identity_strategy_.reset(); | |
62 } | |
63 | |
64 void ProfilePolicyConnector::Initialize() { | |
65 // TODO(jkummerow, mnissler): Move this out of the browser startup path. | |
66 if (identity_strategy_.get()) | |
67 identity_strategy_->LoadTokenCache(); | |
68 if (cloud_policy_subsystem_.get()) | |
69 cloud_policy_subsystem_->Initialize(profile_->GetPrefs()); | |
70 } | |
71 | |
72 void ProfilePolicyConnector::Shutdown() { | |
73 if (cloud_policy_subsystem_.get()) | |
74 cloud_policy_subsystem_->Shutdown(); | |
75 } | |
76 | |
77 ConfigurationPolicyProvider* | |
78 ProfilePolicyConnector::GetManagedCloudProvider() { | |
79 return managed_cloud_provider_.get(); | |
80 } | |
81 | |
82 ConfigurationPolicyProvider* | |
83 ProfilePolicyConnector::GetRecommendedCloudProvider() { | |
84 return recommended_cloud_provider_.get(); | |
85 } | |
86 | |
87 MergingPolicyProvider::MergingPolicyProvider( | |
88 ConfigurationPolicyProvider* browser_policy_provider, | |
89 ConfigurationPolicyProvider* profile_policy_provider) | |
90 : ConfigurationPolicyProvider( | |
91 ConfigurationPolicyPrefStore::GetChromePolicyDefinitionList()), | |
92 browser_policy_provider_(browser_policy_provider), | |
93 profile_policy_provider_(profile_policy_provider), | |
94 browser_registrar_(new ConfigurationPolicyObserverRegistrar()), | |
95 profile_registrar_(new ConfigurationPolicyObserverRegistrar()) { | |
96 if (browser_policy_provider_) | |
97 browser_registrar_->Init(browser_policy_provider_, this); | |
98 if (profile_policy_provider_) | |
99 profile_registrar_->Init(profile_policy_provider_, this); | |
100 } | |
101 | |
102 MergingPolicyProvider::~MergingPolicyProvider() { | |
103 if (browser_policy_provider_ || profile_policy_provider_) { | |
104 FOR_EACH_OBSERVER(ConfigurationPolicyProvider::Observer, | |
105 observer_list_, OnProviderGoingAway()); | |
106 } | |
107 } | |
108 | |
109 bool MergingPolicyProvider::Provide(ConfigurationPolicyStoreInterface* store) { | |
110 // First, apply the profile policies and observe if interesting policies | |
111 // have been applied. | |
112 ObservingPolicyStoreInterface observe(store); | |
113 bool rv = true; | |
114 if (profile_policy_provider_) | |
115 rv = profile_policy_provider_->Provide(&observe); | |
116 | |
117 // Now apply policies from the browser provider, if they were not applied | |
118 // by the profile provider. | |
119 // Currently, these include only the proxy settings. | |
120 if (browser_policy_provider_) { | |
121 FilteringPolicyStoreInterface filter(store, | |
122 !observe.IsProxyPolicyApplied()); | |
123 rv = rv && browser_policy_provider_->Provide(&filter); | |
124 } | |
125 | |
126 return rv; | |
127 } | |
128 | |
129 void MergingPolicyProvider::AddObserver( | |
130 ConfigurationPolicyProvider::Observer* observer) { | |
131 observer_list_.AddObserver(observer); | |
132 } | |
133 | |
134 void MergingPolicyProvider::RemoveObserver( | |
135 ConfigurationPolicyProvider::Observer* observer) { | |
136 observer_list_.RemoveObserver(observer); | |
137 } | |
138 | |
139 void MergingPolicyProvider::OnUpdatePolicy() { | |
140 FOR_EACH_OBSERVER(ConfigurationPolicyProvider::Observer, | |
141 observer_list_, OnUpdatePolicy()); | |
142 } | |
143 | |
144 void MergingPolicyProvider::OnProviderGoingAway() { | |
145 if (browser_policy_provider_ || profile_policy_provider_) { | |
146 FOR_EACH_OBSERVER(ConfigurationPolicyProvider::Observer, | |
147 observer_list_, OnProviderGoingAway()); | |
148 browser_registrar_.reset(); | |
149 profile_registrar_.reset(); | |
150 browser_policy_provider_ = NULL; | |
151 profile_policy_provider_ = NULL; | |
152 } | |
153 } | |
154 | |
155 } // namespace policy | |
OLD | NEW |