1 // Copyright 2013 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 "components/policy/core/common/cloud/user_cloud_policy_manager.h" 6 7 #include "base/bind.h" 8 #include "base/bind_helpers.h" 9 #include "base/sequenced_task_runner.h" 10 #include "components/policy/core/common/cloud/cloud_external_data_manager.h" 11 #include "components/policy/core/common/cloud/cloud_policy_constants.h" 12 #include "components/policy/core/common/cloud/cloud_policy_service.h" 13 #include "components/policy/core/common/cloud/user_cloud_policy_store.h" 14 #include "components/policy/core/common/policy_pref_names.h" 15 #include "components/policy/core/common/policy_types.h" 16 #include "net/url_request/url_request_context_getter.h" 17 18 namespace em = enterprise_management; 19 20 namespace policy { 21 22 UserCloudPolicyManager::UserCloudPolicyManager( 23 scoped_ptr<UserCloudPolicyStore> store, 24 const base::FilePath& component_policy_cache_path, 25 scoped_ptr<CloudExternalDataManager> external_data_manager, 26 const scoped_refptr<base::SequencedTaskRunner>& task_runner, 27 const scoped_refptr<base::SequencedTaskRunner>& file_task_runner, 28 const scoped_refptr<base::SequencedTaskRunner>& io_task_runner) 29 : CloudPolicyManager( 30 PolicyNamespaceKey(GetChromeUserPolicyType(), std::string()), 31 store.get(), 32 task_runner, 33 file_task_runner, 34 io_task_runner), 35 store_(store.Pass()), 36 component_policy_cache_path_(component_policy_cache_path), 37 external_data_manager_(external_data_manager.Pass()) {} 38 39 UserCloudPolicyManager::~UserCloudPolicyManager() {} 40 41 void UserCloudPolicyManager::Shutdown() { 42 if (external_data_manager_) 43 external_data_manager_->Disconnect(); 44 CloudPolicyManager::Shutdown(); 45 } 46 47 void UserCloudPolicyManager::SetSigninUsername(const std::string& username) { 48 store_->SetSigninUsername(username); 49 } 50 51 void UserCloudPolicyManager::Connect( 52 PrefService* local_state, 53 scoped_refptr<net::URLRequestContextGetter> request_context, 54 scoped_ptr<CloudPolicyClient> client) { 55 core()->Connect(client.Pass()); 56 core()->StartRefreshScheduler(); 57 core()->TrackRefreshDelayPref(local_state, 58 policy_prefs::kUserPolicyRefreshRate); 59 if (external_data_manager_) 60 external_data_manager_->Connect(request_context); 61 62 CreateComponentCloudPolicyService(component_policy_cache_path_, 63 request_context); 64 } 65 66 // static 67 scoped_ptr<CloudPolicyClient> 68 UserCloudPolicyManager::CreateCloudPolicyClient( 69 DeviceManagementService* device_management_service, 70 scoped_refptr<net::URLRequestContextGetter> request_context) { 71 return make_scoped_ptr( 72 new CloudPolicyClient( 73 std::string(), 74 std::string(), 75 USER_AFFILIATION_NONE, 76 NULL, 77 device_management_service, 78 request_context)).Pass(); 79 } 80 81 void UserCloudPolicyManager::DisconnectAndRemovePolicy() { 82 if (external_data_manager_) 83 external_data_manager_->Disconnect(); 84 core()->Disconnect(); 85 86 // store_->Clear() will publish the updated, empty policy. The component 87 // policy service must be cleared before OnStoreLoaded() is issued, so that 88 // component policies are also empty at CheckAndPublishPolicy(). 89 ClearAndDestroyComponentCloudPolicyService(); 90 91 // When the |store_| is cleared, it informs the |external_data_manager_| that 92 // all external data references have been removed, causing the 93 // |external_data_manager_| to clear its cache as well. 94 store_->Clear(); 95 } 96 97 bool UserCloudPolicyManager::IsClientRegistered() const { 98 return client() && client()->is_registered(); 99 } 100 101 } // namespace policy 102