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/policy/configuration_policy_pref_store.h" | |
10 #include "chrome/browser/policy/device_management_policy_provider.h" | |
11 #include "chrome/browser/policy/device_management_service.h" | |
12 #include "chrome/browser/policy/profile_policy_context.h" | |
13 #include "chrome/browser/prefs/pref_service.h" | |
14 #include "chrome/browser/profiles/profile.h" | |
15 #include "chrome/common/chrome_switches.h" | |
16 #include "chrome/common/notification_details.h" | |
17 #include "chrome/common/notification_source.h" | |
18 #include "chrome/common/pref_names.h" | |
19 | |
20 namespace { | |
21 | |
22 // Refresh rate sanity interval bounds. | |
23 const int64 kPolicyRefreshRateMinMs = 30 * 60 * 1000; // 30 minutes | |
24 const int64 kPolicyRefreshRateMaxMs = 24 * 60 * 60 * 1000; // 1 day | |
25 | |
26 } // namespace | |
27 | |
28 namespace policy { | |
29 | |
30 ProfilePolicyContext::ProfilePolicyContext(Profile* profile) | |
31 : profile_(profile) { | |
32 CommandLine* command_line = CommandLine::ForCurrentProcess(); | |
33 if (command_line->HasSwitch(switches::kDeviceManagementUrl)) { | |
34 device_management_service_.reset(new DeviceManagementService( | |
35 command_line->GetSwitchValueASCII(switches::kDeviceManagementUrl))); | |
36 device_management_policy_provider_.reset( | |
37 new policy::DeviceManagementPolicyProvider( | |
38 ConfigurationPolicyPrefStore::GetChromePolicyDefinitionList(), | |
39 device_management_service_->CreateBackend(), | |
40 profile_)); | |
41 } | |
42 } | |
43 | |
44 ProfilePolicyContext::~ProfilePolicyContext() { | |
45 device_management_policy_provider_.reset(); | |
46 device_management_service_.reset(); | |
47 } | |
48 | |
49 void ProfilePolicyContext::Initialize() { | |
50 if (device_management_service_.get()) | |
51 device_management_service_->Initialize(profile_->GetRequestContext()); | |
52 | |
53 policy_refresh_rate_.Init(prefs::kPolicyRefreshRate, profile_->GetPrefs(), | |
54 this); | |
55 UpdatePolicyRefreshRate(); | |
56 } | |
57 | |
58 void ProfilePolicyContext::Shutdown() { | |
59 if (device_management_service_.get()) | |
60 device_management_service_->Shutdown(); | |
61 } | |
62 | |
63 DeviceManagementPolicyProvider* | |
64 ProfilePolicyContext::GetDeviceManagementPolicyProvider() { | |
65 return device_management_policy_provider_.get(); | |
66 } | |
67 | |
68 // static | |
69 void ProfilePolicyContext::RegisterUserPrefs(PrefService* user_prefs) { | |
70 user_prefs->RegisterIntegerPref(prefs::kPolicyRefreshRate, | |
71 kDefaultPolicyRefreshRateInMilliseconds); | |
72 } | |
73 | |
74 void ProfilePolicyContext::UpdatePolicyRefreshRate() { | |
75 if (device_management_policy_provider_.get()) { | |
76 // Clamp to sane values. | |
77 int64 refresh_rate = policy_refresh_rate_.GetValue(); | |
78 refresh_rate = std::max(kPolicyRefreshRateMinMs, refresh_rate); | |
79 refresh_rate = std::min(kPolicyRefreshRateMaxMs, refresh_rate); | |
80 device_management_policy_provider_->SetRefreshRate(refresh_rate); | |
81 } | |
82 } | |
83 | |
84 void ProfilePolicyContext::Observe(NotificationType type, | |
85 const NotificationSource& source, | |
86 const NotificationDetails& details) { | |
87 if (type == NotificationType::PREF_CHANGED && | |
88 prefs::kPolicyRefreshRate == *(Details<std::string>(details).ptr()) && | |
89 profile_->GetPrefs() == Source<PrefService>(source).ptr()) { | |
90 UpdatePolicyRefreshRate(); | |
91 } else { | |
92 NOTREACHED(); | |
93 } | |
94 } | |
95 | |
96 } // namespace policy | |
OLD | NEW |