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

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

Issue 5174006: Move DeviceManagementPolicyProvider into the profile. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Address feedback. Created 10 years, 1 month 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) 2010 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2010 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 "chrome/browser/policy/device_management_policy_provider.h" 5 #include "chrome/browser/policy/device_management_policy_provider.h"
6 6
7 #include "base/command_line.h" 7 #include "base/command_line.h"
8 #include "base/file_util.h" 8 #include "base/file_util.h"
9 #include "base/path_service.h" 9 #include "base/path_service.h"
10 #include "base/task.h" 10 #include "base/task.h"
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
49 provider->InitializeAfterIOThreadExists(); 49 provider->InitializeAfterIOThreadExists();
50 } 50 }
51 51
52 private: 52 private:
53 base::WeakPtr<DeviceManagementPolicyProvider> provider_; 53 base::WeakPtr<DeviceManagementPolicyProvider> provider_;
54 }; 54 };
55 55
56 DeviceManagementPolicyProvider::DeviceManagementPolicyProvider( 56 DeviceManagementPolicyProvider::DeviceManagementPolicyProvider(
57 const ConfigurationPolicyProvider::PolicyDefinitionList* policy_list, 57 const ConfigurationPolicyProvider::PolicyDefinitionList* policy_list,
58 DeviceManagementBackend* backend, 58 DeviceManagementBackend* backend,
59 TokenService* token_service,
59 const FilePath& storage_dir) 60 const FilePath& storage_dir)
60 : ConfigurationPolicyProvider(policy_list), 61 : ConfigurationPolicyProvider(policy_list),
61 backend_(backend), 62 backend_(backend),
63 token_service_(token_service),
62 storage_dir_(GetOrCreateDeviceManagementDir(storage_dir)), 64 storage_dir_(GetOrCreateDeviceManagementDir(storage_dir)),
63 policy_request_pending_(false) { 65 policy_request_pending_(false) {
64 Initialize(); 66 Initialize();
65 } 67 }
66 68
67 DeviceManagementPolicyProvider::DeviceManagementPolicyProvider(
68 const ConfigurationPolicyProvider::PolicyDefinitionList* policy_list)
69 : ConfigurationPolicyProvider(policy_list),
70 policy_request_pending_(false) {
71 FilePath user_dir;
72 if (!PathService::Get(chrome::DIR_USER_DATA, &user_dir))
73 NOTREACHED();
74 storage_dir_ = GetOrCreateDeviceManagementDir(user_dir);
75 Initialize();
76 }
77
78 DeviceManagementPolicyProvider::~DeviceManagementPolicyProvider() {} 69 DeviceManagementPolicyProvider::~DeviceManagementPolicyProvider() {}
79 70
80 bool DeviceManagementPolicyProvider::Provide( 71 bool DeviceManagementPolicyProvider::Provide(
81 ConfigurationPolicyStoreInterface* policy_store) { 72 ConfigurationPolicyStoreInterface* policy_store) {
82 scoped_ptr<DictionaryValue> policies(cache_->GetPolicy()); 73 scoped_ptr<DictionaryValue> policies(cache_->GetPolicy());
83 DecodePolicyValueTree(policies.get(), policy_store); 74 DecodePolicyValueTree(policies.get(), policy_store);
84 return true; 75 return true;
85 } 76 }
86 77
87 void DeviceManagementPolicyProvider::Observe( 78 void DeviceManagementPolicyProvider::Observe(
88 NotificationType type, 79 NotificationType type,
89 const NotificationSource& source, 80 const NotificationSource& source,
90 const NotificationDetails& details) { 81 const NotificationDetails& details) {
91 if ((type == NotificationType::DEVICE_TOKEN_AVAILABLE) && 82 if (type == NotificationType::DEVICE_TOKEN_AVAILABLE) {
92 (token_fetcher_.get() == Source<DeviceTokenFetcher>(source).ptr())) { 83 if (token_fetcher_.get() == Source<DeviceTokenFetcher>(source).ptr() &&
93 if (!policy_request_pending_) { 84 !policy_request_pending_ &&
94 if (IsPolicyStale()) 85 IsPolicyStale()) {
95 SendPolicyRequest(); 86 SendPolicyRequest();
96 } 87 }
97 } else { 88 } else {
98 NOTREACHED(); 89 NOTREACHED();
99 } 90 }
100 } 91 }
101 92
102 void DeviceManagementPolicyProvider::HandlePolicyResponse( 93 void DeviceManagementPolicyProvider::HandlePolicyResponse(
103 const em::DevicePolicyResponse& response) { 94 const em::DevicePolicyResponse& response) {
104 cache_->SetPolicy(response); 95 cache_->SetPolicy(response);
105 NotifyStoreOfPolicyChange(); 96 NotifyStoreOfPolicyChange();
106 policy_request_pending_ = false; 97 policy_request_pending_ = false;
107 } 98 }
108 99
109 void DeviceManagementPolicyProvider::OnError( 100 void DeviceManagementPolicyProvider::OnError(
110 DeviceManagementBackend::ErrorCode code) { 101 DeviceManagementBackend::ErrorCode code) {
111 LOG(WARNING) << "could not provide policy from the device manager (error = " 102 LOG(WARNING) << "could not provide policy from the device manager (error = "
112 << code << ")"; 103 << code << ")";
113 policy_request_pending_ = false; 104 policy_request_pending_ = false;
114 // TODO(danno): do something sensible in the error case, perhaps retry later? 105 // TODO(danno): do something sensible in the error case, perhaps retry later?
115 } 106 }
116 107
108 void DeviceManagementPolicyProvider::Shutdown() {
109 token_service_ = NULL;
110 if (token_fetcher_)
111 token_fetcher_->Shutdown();
112 }
113
117 DeviceManagementBackend* DeviceManagementPolicyProvider::GetBackend() { 114 DeviceManagementBackend* DeviceManagementPolicyProvider::GetBackend() {
118 if (!backend_.get()) { 115 if (!backend_.get()) {
119 backend_.reset(new DeviceManagementBackendImpl( 116 backend_.reset(new DeviceManagementBackendImpl(
120 GetDeviceManagementURL())); 117 GetDeviceManagementURL()));
121 } 118 }
122 return backend_.get(); 119 return backend_.get();
123 } 120 }
124 121
125 void DeviceManagementPolicyProvider::Initialize() { 122 void DeviceManagementPolicyProvider::Initialize() {
126 registrar_.Add(this, 123 registrar_.Add(this,
127 NotificationType::DEVICE_TOKEN_AVAILABLE, 124 NotificationType::DEVICE_TOKEN_AVAILABLE,
128 NotificationService::AllSources()); 125 NotificationService::AllSources());
129 126
130 const FilePath policy_path = storage_dir_.Append( 127 const FilePath policy_path = storage_dir_.Append(
131 FILE_PATH_LITERAL("Policy")); 128 FILE_PATH_LITERAL("Policy"));
132 cache_.reset(new DeviceManagementPolicyCache(policy_path)); 129 cache_.reset(new DeviceManagementPolicyCache(policy_path));
133 cache_->LoadPolicyFromFile(); 130 cache_->LoadPolicyFromFile();
134 131
135 // Defer initialization that requires the IOThread until after the IOThread 132 // Defer initialization that requires the IOThread until after the IOThread
136 // has been initialized. 133 // has been initialized.
137 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, 134 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
138 new InitializeAfterIOThreadExistsTask(AsWeakPtr())); 135 new InitializeAfterIOThreadExistsTask(AsWeakPtr()));
139 } 136 }
140 137
141 void DeviceManagementPolicyProvider::InitializeAfterIOThreadExists() { 138 void DeviceManagementPolicyProvider::InitializeAfterIOThreadExists() {
142 const FilePath token_path = storage_dir_.Append( 139 const FilePath token_path = storage_dir_.Append(
143 FILE_PATH_LITERAL("Token")); 140 FILE_PATH_LITERAL("Token"));
144 token_fetcher_ = new DeviceTokenFetcher(GetBackend(), token_path); 141 if (token_service_) {
145 token_fetcher_->StartFetching(); 142 token_fetcher_ =
143 new DeviceTokenFetcher(GetBackend(), token_service_, token_path);
144 token_fetcher_->StartFetching();
145 }
146 } 146 }
147 147
148 void DeviceManagementPolicyProvider::SendPolicyRequest() { 148 void DeviceManagementPolicyProvider::SendPolicyRequest() {
149 if (!policy_request_pending_) { 149 if (!policy_request_pending_) {
150 em::DevicePolicyRequest policy_request; 150 em::DevicePolicyRequest policy_request;
151 policy_request.set_policy_scope(kChromePolicyScope); 151 policy_request.set_policy_scope(kChromePolicyScope);
152 em::DevicePolicySettingRequest* setting = 152 em::DevicePolicySettingRequest* setting =
153 policy_request.add_setting_request(); 153 policy_request.add_setting_request();
154 setting->set_key(kChromeDevicePolicySettingKey); 154 setting->set_key(kChromeDevicePolicySettingKey);
155 GetBackend()->ProcessPolicyRequest(token_fetcher_->GetDeviceToken(), 155 GetBackend()->ProcessPolicyRequest(token_fetcher_->GetDeviceToken(),
(...skipping 25 matching lines...) Expand all
181 const FilePath device_management_dir = user_data_dir.Append( 181 const FilePath device_management_dir = user_data_dir.Append(
182 FILE_PATH_LITERAL("Device Management")); 182 FILE_PATH_LITERAL("Device Management"));
183 if (!file_util::DirectoryExists(device_management_dir)) { 183 if (!file_util::DirectoryExists(device_management_dir)) {
184 if (!file_util::CreateDirectory(device_management_dir)) 184 if (!file_util::CreateDirectory(device_management_dir))
185 NOTREACHED(); 185 NOTREACHED();
186 } 186 }
187 return device_management_dir; 187 return device_management_dir;
188 } 188 }
189 189
190 } // namespace policy 190 } // namespace policy
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698