1 // Copyright 2014 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 #ifndef CHROME_BROWSER_CHROMEOS_OWNERSHIP_OWNER_SETTINGS_SERVICE_H_ 6 #define CHROME_BROWSER_CHROMEOS_OWNERSHIP_OWNER_SETTINGS_SERVICE_H_ 7 8 #include <vector> 9 10 #include "base/compiler_specific.h" 11 #include "base/macros.h" 12 #include "base/memory/weak_ptr.h" 13 #include "base/threading/thread_checker.h" 14 #include "chrome/browser/chromeos/settings/device_settings_service.h" 15 #include "chrome/browser/chromeos/settings/owner_key_util.h" 16 #include "chromeos/dbus/session_manager_client.h" 17 #include "chromeos/tpm_token_loader.h" 18 #include "components/keyed_service/core/keyed_service.h" 19 #include "content/public/browser/notification_observer.h" 20 #include "content/public/browser/notification_registrar.h" 21 22 class Profile; 23 24 namespace chromeos { 25 26 // This class reloads owner key from profile NSS slots. 27 // 28 // TODO (ygorshenin@): move write path for device settings here 29 // (crbug.com/230018). 30 class OwnerSettingsService : public DeviceSettingsService::PrivateKeyDelegate, 31 public KeyedService, 32 public content::NotificationObserver, 33 public TPMTokenLoader::Observer, 34 public SessionManagerClient::Observer { 35 public: 36 virtual ~OwnerSettingsService(); 37 38 base::WeakPtr<OwnerSettingsService> as_weak_ptr() { 39 return weak_factory_.GetWeakPtr(); 40 } 41 42 // DeviceSettingsService::PrivateKeyDelegate implementation: 43 virtual bool IsOwner() OVERRIDE; 44 virtual void IsOwnerAsync(const IsOwnerCallback& callback) OVERRIDE; 45 virtual bool AssembleAndSignPolicyAsync( 46 scoped_ptr<enterprise_management::PolicyData> policy, 47 const AssembleAndSignPolicyCallback& callback) OVERRIDE; 48 49 // NotificationObserver implementation: 50 virtual void Observe(int type, 51 const content::NotificationSource& source, 52 const content::NotificationDetails& details) OVERRIDE; 53 54 // TPMTokenLoader::Observer: 55 virtual void OnTPMTokenReady() OVERRIDE; 56 57 // SessionManagerClient::Observer: 58 virtual void OwnerKeySet(bool success) OVERRIDE; 59 60 // Checks whether NSS slots with private key are mounted or 61 // not. Responds via |callback|. 62 static void IsPrivateKeyExistAsync(const IsOwnerCallback& callback); 63 64 static void SetOwnerKeyUtilForTesting( 65 const scoped_refptr<OwnerKeyUtil>& owner_key_util); 66 67 static void SetDeviceSettingsServiceForTesting( 68 DeviceSettingsService* device_settings_service); 69 70 private: 71 friend class OwnerSettingsServiceFactory; 72 73 explicit OwnerSettingsService(Profile* profile); 74 75 // Reloads private key from profile's NSS slots. Responds via call 76 // to OnPrivateKeyLoaded(). 77 void ReloadPrivateKey(); 78 79 // Called when ReloadPrivateKey() completes it's work. 80 void OnPrivateKeyLoaded(scoped_ptr<crypto::RSAPrivateKey> private_key); 81 82 // Returns testing instance of OwnerKeyUtil when it's set, otherwise 83 // returns |owner_key_util_|. 84 scoped_refptr<OwnerKeyUtil> GetOwnerKeyUtil(); 85 86 // Returns testing instance of DeviceSettingsService when it's set, 87 // otherwise returns pointer to a singleton instance, when it's 88 // initialized. 89 DeviceSettingsService* GetDeviceSettingsService(); 90 91 // Profile this service instance belongs to. 92 Profile* profile_; 93 94 scoped_refptr<PrivateKey> private_key_; 95 96 scoped_refptr<OwnerKeyUtil> owner_key_util_; 97 98 std::vector<IsOwnerCallback> pending_is_owner_callbacks_; 99 100 // Whether profile still needs to be initialized. 101 bool waiting_for_profile_creation_; 102 103 // Whether TPM token still needs to be initialized. 104 bool waiting_for_tpm_token_; 105 106 content::NotificationRegistrar registrar_; 107 108 base::ThreadChecker thread_checker_; 109 110 base::WeakPtrFactory<OwnerSettingsService> weak_factory_; 111 112 DISALLOW_COPY_AND_ASSIGN(OwnerSettingsService); 113 }; 114 115 } // namespace chromeos 116 117 #endif // CHROME_BROWSER_CHROMEOS_OWNERSHIP_OWNER_SETTINGS_SERVICE_H_ 118