Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(380)

Side by Side Diff: chrome/browser/policy/profile_policy_context.cc

Issue 6312121: Add initial device policy infrastructure. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: cleanup/compile fixes. Created 9 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 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 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 "base/command_line.h" 5 #include "base/file_util.h"
6 #include "chrome/browser/policy/configuration_policy_pref_store.h" 6 #include "chrome/browser/policy/cloud_policy_context.h"
7 #include "chrome/browser/policy/device_management_policy_provider.h"
8 #include "chrome/browser/policy/device_management_service.h"
9 #include "chrome/browser/policy/profile_policy_context.h" 7 #include "chrome/browser/policy/profile_policy_context.h"
10 #include "chrome/browser/prefs/pref_service.h" 8 #include "chrome/browser/policy/user_policy_controller.h"
11 #include "chrome/browser/profiles/profile.h" 9 #include "chrome/browser/profiles/profile.h"
12 #include "chrome/common/chrome_switches.h" 10 #include "chrome/common/net/url_request_context_getter.h"
13 #include "chrome/common/notification_details.h"
14 #include "chrome/common/notification_source.h"
15 #include "chrome/common/pref_names.h"
16 11
17 namespace { 12 namespace {
18 13
19 // Refresh rate sanity interval bounds. 14 // Refresh rate sanity interval bounds.
20 const int64 kPolicyRefreshRateMinMs = 30 * 60 * 1000; // 30 minutes 15 const int64 kPolicyRefreshRateMinMs = 30 * 60 * 1000; // 30 minutes
21 const int64 kPolicyRefreshRateMaxMs = 24 * 60 * 60 * 1000; // 1 day 16 const int64 kPolicyRefreshRateMaxMs = 24 * 60 * 60 * 1000; // 1 day
22 17
18 const FilePath::CharType kPolicyDir[] = FILE_PATH_LITERAL("Device Management");
19 const FilePath::CharType kTokenCacheFile[] = FILE_PATH_LITERAL("Token");
20 const FilePath::CharType kPolicyCacheFile[] = FILE_PATH_LITERAL("Policy");
21
23 } 22 }
24 23
25 namespace policy { 24 namespace policy {
26 25
27 ProfilePolicyContext::ProfilePolicyContext(Profile* profile) 26 ProfilePolicyContext::ProfilePolicyContext(Profile* profile)
28 : profile_(profile) { 27 : profile_(profile) {
29 CommandLine* command_line = CommandLine::ForCurrentProcess(); 28 // TODO(mnissler): We access the file system here. The cloud policy context
30 if (command_line->HasSwitch(switches::kDeviceManagementUrl)) { 29 // below needs to do so anyway, since it needs to read the policy cache from
31 device_management_service_.reset(new DeviceManagementService( 30 // disk. If this proves to be a problem, we need to do this initialization
32 command_line->GetSwitchValueASCII(switches::kDeviceManagementUrl))); 31 // asynchronously on the file thread and put in synchronization that allows us
33 device_management_policy_provider_.reset( 32 // to wait for the cache to be read during the browser startup code paths.
34 new policy::DeviceManagementPolicyProvider( 33 // Another option would be to provide a generic IO-safe initializer called
35 ConfigurationPolicyPrefStore::GetChromePolicyDefinitionList(), 34 // from the PrefService that we could hook up with through the policy
36 device_management_service_->CreateBackend(), 35 // provider.
37 profile_)); 36 FilePath policy_cache_dir(profile_->GetPath());
37 policy_cache_dir = policy_cache_dir.Append(kPolicyDir);
38 if (!file_util::CreateDirectory(policy_cache_dir)) {
39 LOG(WARNING) << "Failed to create policy state dir "
40 << policy_cache_dir.value()
41 << ", skipping cloud policy initialization.";
42 return;
43 }
44
45 controller_.reset(
46 new UserPolicyController(profile_,
47 policy_cache_dir.Append(kTokenCacheFile)));
48 cloud_context_.reset(
49 new CloudPolicyContext(policy_cache_dir.Append(kPolicyCacheFile),
50 controller_.get()));
51 }
52
53 ProfilePolicyContext::~ProfilePolicyContext() {
54 cloud_context_.reset();
55 controller_.reset();
56 }
57
58 void ProfilePolicyContext::Initialize() {
59 if (cloud_context_.get()) {
60 cloud_context_->Initialize(profile_->GetPrefs(),
61 profile_->GetRequestContext());
38 } 62 }
39 } 63 }
40 64
41 ProfilePolicyContext::~ProfilePolicyContext() { 65 void ProfilePolicyContext::Shutdown() {
42 device_management_policy_provider_.reset(); 66 if (cloud_context_.get())
43 device_management_service_.reset(); 67 cloud_context_->Shutdown();
44 } 68 }
45 69
46 void ProfilePolicyContext::Initialize() { 70 ConfigurationPolicyProvider* ProfilePolicyContext::GetManagedPolicyProvider() {
47 if (device_management_service_.get()) 71 if (cloud_context_.get())
48 device_management_service_->Initialize(profile_->GetRequestContext()); 72 return cloud_context_->GetManagedPolicyProvider();
49 73
50 policy_refresh_rate_.Init(prefs::kPolicyRefreshRate, profile_->GetPrefs(), 74 return NULL;
51 this);
52 UpdatePolicyRefreshRate();
53 } 75 }
54 76
55 void ProfilePolicyContext::Shutdown() { 77 ConfigurationPolicyProvider*
56 if (device_management_service_.get()) 78 ProfilePolicyContext::GetRecommendedPolicyProvider() {
57 device_management_service_->Shutdown(); 79 if (cloud_context_.get())
58 } 80 return cloud_context_->GetRecommendedPolicyProvider();
59 81
60 DeviceManagementPolicyProvider* 82 return NULL;
61 ProfilePolicyContext::GetDeviceManagementPolicyProvider() {
62 return device_management_policy_provider_.get();
63 }
64
65 // static
66 void ProfilePolicyContext::RegisterUserPrefs(PrefService* user_prefs) {
67 user_prefs->RegisterIntegerPref(prefs::kPolicyRefreshRate,
68 kDefaultPolicyRefreshRateInMilliseconds);
69 }
70
71 void ProfilePolicyContext::UpdatePolicyRefreshRate() {
72 if (device_management_policy_provider_.get()) {
73 // Clamp to sane values.
74 int64 refresh_rate = policy_refresh_rate_.GetValue();
75 refresh_rate = std::max(kPolicyRefreshRateMinMs, refresh_rate);
76 refresh_rate = std::min(kPolicyRefreshRateMaxMs, refresh_rate);
77 device_management_policy_provider_->SetRefreshRate(refresh_rate);
78 }
79 }
80
81 void ProfilePolicyContext::Observe(NotificationType type,
82 const NotificationSource& source,
83 const NotificationDetails& details) {
84 if (type == NotificationType::PREF_CHANGED &&
85 prefs::kPolicyRefreshRate == *(Details<std::string>(details).ptr()) &&
86 profile_->GetPrefs() == Source<PrefService>(source).ptr()) {
87 UpdatePolicyRefreshRate();
88 } else {
89 NOTREACHED();
90 }
91 } 83 }
92 84
93 } // namespace policy 85 } // namespace policy
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698