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.
5 #ifndef CHROME_BROWSER_CHROMEOS_OWNERSHIP_OWNER_SETTINGS_SERVICE_H_
6 #define CHROME_BROWSER_CHROMEOS_OWNERSHIP_OWNER_SETTINGS_SERVICE_H_
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"
26 // This class reloads owner key from profile NSS slots.
28 // TODO (ygorshenin@): move write path for device settings here
29 // (crbug.com/230018).
30 class OwnerSettingsService : public DeviceSettingsService::PrivateKeyDelegate,
32 public content::NotificationObserver,
33 public TPMTokenLoader::Observer,
34 public SessionManagerClient::Observer {
36 virtual ~OwnerSettingsService();
38 base::WeakPtr<OwnerSettingsService> as_weak_ptr() {
39 return weak_factory_.GetWeakPtr();
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;
49 // NotificationObserver implementation:
50 virtual void Observe(int type,
51 const content::NotificationSource& source,
52 const content::NotificationDetails& details) OVERRIDE;
54 // TPMTokenLoader::Observer:
55 virtual void OnTPMTokenReady() OVERRIDE;
57 // SessionManagerClient::Observer:
58 virtual void OwnerKeySet(bool success) OVERRIDE;
60 // Checks whether NSS slots with private key are mounted or
61 // not. Responds via |callback|.
62 static void IsPrivateKeyExistAsync(const IsOwnerCallback& callback);
64 static void SetOwnerKeyUtilForTesting(
65 const scoped_refptr<OwnerKeyUtil>& owner_key_util);
67 static void SetDeviceSettingsServiceForTesting(
68 DeviceSettingsService* device_settings_service);
71 friend class OwnerSettingsServiceFactory;
73 explicit OwnerSettingsService(Profile* profile);
75 // Reloads private key from profile's NSS slots. Responds via call
76 // to OnPrivateKeyLoaded().
77 void ReloadPrivateKey();
79 // Called when ReloadPrivateKey() completes it's work.
80 void OnPrivateKeyLoaded(scoped_ptr<crypto::RSAPrivateKey> private_key);
82 // Returns testing instance of OwnerKeyUtil when it's set, otherwise
83 // returns |owner_key_util_|.
84 scoped_refptr<OwnerKeyUtil> GetOwnerKeyUtil();
86 // Returns testing instance of DeviceSettingsService when it's set,
87 // otherwise returns pointer to a singleton instance, when it's
89 DeviceSettingsService* GetDeviceSettingsService();
91 // Profile this service instance belongs to.
94 scoped_refptr<PrivateKey> private_key_;
96 scoped_refptr<OwnerKeyUtil> owner_key_util_;
98 std::vector<IsOwnerCallback> pending_is_owner_callbacks_;
100 // Whether profile still needs to be initialized.
101 bool waiting_for_profile_creation_;
103 // Whether TPM token still needs to be initialized.
104 bool waiting_for_tpm_token_;
106 content::NotificationRegistrar registrar_;
108 base::ThreadChecker thread_checker_;
110 base::WeakPtrFactory<OwnerSettingsService> weak_factory_;
112 DISALLOW_COPY_AND_ASSIGN(OwnerSettingsService);
115 } // namespace chromeos
117 #endif // CHROME_BROWSER_CHROMEOS_OWNERSHIP_OWNER_SETTINGS_SERVICE_H_