OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/policy/cloud_policy_subsystem.h" | |
6 | |
7 #include <algorithm> | |
8 #include <string> | |
9 | |
10 #include "base/command_line.h" | |
11 #include "chrome/browser/browser_process.h" | |
12 #include "chrome/browser/policy/cloud_policy_cache_base.h" | |
13 #include "chrome/browser/policy/cloud_policy_controller.h" | |
14 #include "chrome/browser/policy/cloud_policy_data_store.h" | |
15 #include "chrome/browser/policy/device_management_service.h" | |
16 #include "chrome/browser/policy/device_token_fetcher.h" | |
17 #include "chrome/browser/policy/policy_notifier.h" | |
18 #include "chrome/browser/prefs/pref_service.h" | |
19 #include "chrome/common/chrome_notification_types.h" | |
20 #include "chrome/common/chrome_switches.h" | |
21 #include "chrome/common/pref_names.h" | |
22 #include "content/public/browser/notification_details.h" | |
23 #include "content/public/browser/notification_source.h" | |
24 | |
25 namespace { | |
26 | |
27 // Default refresh rate. | |
28 const int64 kDefaultPolicyRefreshRateMs = 3 * 60 * 60 * 1000; // 3 hours. | |
29 | |
30 // Refresh rate sanity interval bounds. | |
31 const int64 kPolicyRefreshRateMinMs = 30 * 60 * 1000; // 30 minutes | |
32 const int64 kPolicyRefreshRateMaxMs = 24 * 60 * 60 * 1000; // 1 day | |
33 | |
34 } // namespace | |
35 | |
36 namespace policy { | |
37 | |
38 CloudPolicySubsystem::ObserverRegistrar::ObserverRegistrar( | |
39 CloudPolicySubsystem* cloud_policy_subsystem, | |
40 CloudPolicySubsystem::Observer* observer) | |
41 : observer_(observer) { | |
42 policy_notifier_ = cloud_policy_subsystem->notifier(); | |
43 policy_notifier_->AddObserver(observer); | |
44 } | |
45 | |
46 CloudPolicySubsystem::ObserverRegistrar::~ObserverRegistrar() { | |
47 if (policy_notifier_) | |
48 policy_notifier_->RemoveObserver(observer_); | |
49 } | |
50 | |
51 CloudPolicySubsystem::CloudPolicySubsystem( | |
52 CloudPolicyDataStore* data_store, | |
53 CloudPolicyCacheBase* policy_cache, | |
54 const std::string& device_management_url) { | |
55 Initialize(data_store, policy_cache, device_management_url); | |
56 } | |
57 | |
58 CloudPolicySubsystem::~CloudPolicySubsystem() { | |
59 cloud_policy_controller_.reset(); | |
60 device_token_fetcher_.reset(); | |
61 cloud_policy_cache_.reset(); | |
62 device_management_service_.reset(); | |
63 net::NetworkChangeNotifier::RemoveIPAddressObserver(this); | |
64 } | |
65 | |
66 void CloudPolicySubsystem::OnIPAddressChanged() { | |
67 if (state() == CloudPolicySubsystem::NETWORK_ERROR && | |
68 cloud_policy_controller_.get()) { | |
69 cloud_policy_controller_->Retry(); | |
70 } | |
71 } | |
72 | |
73 void CloudPolicySubsystem::Initialize( | |
74 CloudPolicyDataStore* data_store, | |
75 CloudPolicyCacheBase* policy_cache, | |
76 const std::string& device_management_url) { | |
77 device_management_url_ = device_management_url; | |
78 data_store_ = data_store; | |
79 net::NetworkChangeNotifier::AddIPAddressObserver(this); | |
80 notifier_.reset(new PolicyNotifier()); | |
81 if (!device_management_url_.empty()) { | |
82 device_management_service_.reset(new DeviceManagementService( | |
83 device_management_url)); | |
84 cloud_policy_cache_.reset(policy_cache); | |
85 cloud_policy_cache_->set_policy_notifier(notifier_.get()); | |
86 cloud_policy_cache_->Load(); | |
87 CreateDeviceTokenFetcher(); | |
88 } | |
89 } | |
90 | |
91 void CloudPolicySubsystem::CompleteInitialization( | |
92 const char* refresh_pref_name, | |
93 int64 delay_milliseconds) { | |
94 if (!device_management_url_.empty()) { | |
95 DCHECK(device_management_service_.get()); | |
96 DCHECK(cloud_policy_cache_.get()); | |
97 DCHECK(device_token_fetcher_.get()); | |
98 | |
99 refresh_pref_name_ = refresh_pref_name; | |
100 CreateCloudPolicyController(); | |
101 device_management_service_->ScheduleInitialization(delay_milliseconds); | |
102 | |
103 PrefService* local_state = g_browser_process->local_state(); | |
104 DCHECK(pref_change_registrar_.IsEmpty()); | |
105 pref_change_registrar_.Init(local_state); | |
106 pref_change_registrar_.Add( | |
107 refresh_pref_name_, | |
108 base::Bind(&CloudPolicySubsystem::OnRefreshPrefChanged, | |
109 base::Unretained(this))); | |
110 UpdatePolicyRefreshRate(local_state->GetInteger(refresh_pref_name_)); | |
111 } | |
112 } | |
113 | |
114 void CloudPolicySubsystem::Shutdown() { | |
115 if (device_management_service_.get()) | |
116 device_management_service_->Shutdown(); | |
117 cloud_policy_controller_.reset(); | |
118 cloud_policy_cache_.reset(); | |
119 pref_change_registrar_.RemoveAll(); | |
120 } | |
121 | |
122 CloudPolicySubsystem::PolicySubsystemState CloudPolicySubsystem::state() { | |
123 return notifier_->state(); | |
124 } | |
125 | |
126 CloudPolicySubsystem::ErrorDetails CloudPolicySubsystem::error_details() { | |
127 return notifier_->error_details(); | |
128 } | |
129 | |
130 void CloudPolicySubsystem::Reset() { | |
131 data_store_->Reset(); | |
132 cloud_policy_cache_->Reset(); | |
133 cloud_policy_controller_->Reset(); | |
134 device_token_fetcher_->Reset(); | |
135 } | |
136 | |
137 void CloudPolicySubsystem::RefreshPolicies(bool wait_for_auth_token) { | |
138 data_store_->set_policy_fetching_enabled(true); | |
139 if (cloud_policy_controller_.get()) | |
140 cloud_policy_controller_->RefreshPolicies(wait_for_auth_token); | |
141 // Make sure the |device_management_service_| is rolling. | |
142 device_management_service_->ScheduleInitialization(0); | |
143 } | |
144 | |
145 // static | |
146 void CloudPolicySubsystem::RegisterPrefs(PrefServiceSimple* pref_service) { | |
147 pref_service->RegisterIntegerPref(prefs::kDevicePolicyRefreshRate, | |
148 kDefaultPolicyRefreshRateMs); | |
149 pref_service->RegisterIntegerPref(prefs::kUserPolicyRefreshRate, | |
150 kDefaultPolicyRefreshRateMs); | |
151 } | |
152 | |
153 void CloudPolicySubsystem::UpdatePolicyRefreshRate(int64 refresh_rate) { | |
154 if (cloud_policy_controller_.get()) { | |
155 // Clamp to sane values. | |
156 refresh_rate = std::max(kPolicyRefreshRateMinMs, refresh_rate); | |
157 refresh_rate = std::min(kPolicyRefreshRateMaxMs, refresh_rate); | |
158 cloud_policy_controller_->SetRefreshRate(refresh_rate); | |
159 } | |
160 } | |
161 | |
162 void CloudPolicySubsystem::OnRefreshPrefChanged() { | |
163 PrefService* local_state = g_browser_process->local_state(); | |
164 UpdatePolicyRefreshRate(local_state->GetInteger(refresh_pref_name_)); | |
165 } | |
166 | |
167 void CloudPolicySubsystem::ScheduleServiceInitialization( | |
168 int64 delay_milliseconds) { | |
169 if (device_management_service_.get()) | |
170 device_management_service_->ScheduleInitialization(delay_milliseconds); | |
171 } | |
172 | |
173 | |
174 void CloudPolicySubsystem::CreateDeviceTokenFetcher() { | |
175 device_token_fetcher_.reset( | |
176 new DeviceTokenFetcher(device_management_service_.get(), | |
177 cloud_policy_cache_.get(), | |
178 data_store_, | |
179 notifier_.get())); | |
180 } | |
181 | |
182 void CloudPolicySubsystem::CreateCloudPolicyController() { | |
183 DCHECK(!cloud_policy_controller_.get()); | |
184 cloud_policy_controller_.reset( | |
185 new CloudPolicyController(device_management_service_.get(), | |
186 cloud_policy_cache_.get(), | |
187 device_token_fetcher_.get(), | |
188 data_store_, | |
189 notifier_.get())); | |
190 } | |
191 | |
192 CloudPolicyCacheBase* CloudPolicySubsystem::GetCloudPolicyCacheBase() const { | |
193 return cloud_policy_cache_.get(); | |
194 } | |
195 | |
196 CloudPolicySubsystem::CloudPolicySubsystem() | |
197 : refresh_pref_name_(NULL), | |
198 data_store_(NULL) { | |
199 } | |
200 | |
201 } // namespace policy | |
OLD | NEW |