1 // Copyright (c) 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 "chromeos/tpm/stub_install_attributes.h"
6 
7 #include "components/policy/core/common/cloud/cloud_policy_constants.h"
8 
9 namespace chromeos {
10 
StubInstallAttributes()11 StubInstallAttributes::StubInstallAttributes() : InstallAttributes(nullptr) {
12   device_locked_ = true;
13 }
14 
15 // static
CreateUnset()16 std::unique_ptr<StubInstallAttributes> StubInstallAttributes::CreateUnset() {
17   auto result = std::make_unique<StubInstallAttributes>();
18   result->Clear();
19   return result;
20 }
21 
22 // static
23 std::unique_ptr<StubInstallAttributes>
CreateConsumerOwned()24 StubInstallAttributes::CreateConsumerOwned() {
25   auto result = std::make_unique<StubInstallAttributes>();
26   result->SetConsumerOwned();
27   return result;
28 }
29 
30 // static
31 std::unique_ptr<StubInstallAttributes>
CreateCloudManaged(const std::string & domain,const std::string & device_id)32 StubInstallAttributes::CreateCloudManaged(const std::string& domain,
33                                           const std::string& device_id) {
34   auto result = std::make_unique<StubInstallAttributes>();
35   result->SetCloudManaged(domain, device_id);
36   return result;
37 }
38 
39 // static
40 std::unique_ptr<StubInstallAttributes>
CreateActiveDirectoryManaged(const std::string & realm,const std::string & device_id)41 StubInstallAttributes::CreateActiveDirectoryManaged(
42     const std::string& realm,
43     const std::string& device_id) {
44   auto result = std::make_unique<StubInstallAttributes>();
45   result->SetActiveDirectoryManaged(realm, device_id);
46   return result;
47 }
48 
49 // static
CreateDemoMode(const std::string & device_id)50 std::unique_ptr<StubInstallAttributes> StubInstallAttributes::CreateDemoMode(
51     const std::string& device_id) {
52   auto result = std::make_unique<StubInstallAttributes>();
53   result->SetDemoMode(device_id);
54   return result;
55 }
56 
Clear()57 void StubInstallAttributes::Clear() {
58   registration_mode_ = policy::DEVICE_MODE_NOT_SET;
59   registration_domain_.clear();
60   registration_realm_.clear();
61   registration_device_id_.clear();
62 }
63 
SetConsumerOwned()64 void StubInstallAttributes::SetConsumerOwned() {
65   registration_mode_ = policy::DEVICE_MODE_CONSUMER;
66   registration_domain_.clear();
67   registration_realm_.clear();
68   registration_device_id_.clear();
69 }
70 
SetCloudManaged(const std::string & domain,const std::string & device_id)71 void StubInstallAttributes::SetCloudManaged(const std::string& domain,
72                                             const std::string& device_id) {
73   registration_mode_ = policy::DEVICE_MODE_ENTERPRISE;
74   registration_domain_ = domain;
75   registration_realm_.clear();
76   registration_device_id_ = device_id;
77 }
78 
SetActiveDirectoryManaged(const std::string & realm,const std::string & device_id)79 void StubInstallAttributes::SetActiveDirectoryManaged(
80     const std::string& realm,
81     const std::string& device_id) {
82   registration_mode_ = policy::DEVICE_MODE_ENTERPRISE_AD;
83   registration_realm_ = realm;
84   registration_domain_.clear();
85   registration_device_id_ = device_id;
86 }
87 
SetDemoMode(const std::string & device_id)88 void StubInstallAttributes::SetDemoMode(const std::string& device_id) {
89   registration_mode_ = policy::DEVICE_MODE_DEMO;
90   registration_domain_ = policy::kDemoModeDomain;
91   registration_realm_.clear();
92   registration_device_id_ = device_id;
93 }
94 
ScopedStubInstallAttributes()95 ScopedStubInstallAttributes::ScopedStubInstallAttributes()
96     : ScopedStubInstallAttributes(std::make_unique<StubInstallAttributes>()) {}
97 
ScopedStubInstallAttributes(std::unique_ptr<StubInstallAttributes> install_attributes)98 ScopedStubInstallAttributes::ScopedStubInstallAttributes(
99     std::unique_ptr<StubInstallAttributes> install_attributes)
100     : install_attributes_(std::move(install_attributes)) {
101   // The constructor calls SetForTesting with these install_attributes, so
102   // in the destructor we make the matching call to ShutdownForTesting.
103   InstallAttributes::SetForTesting(install_attributes_.get());
104 }
105 
~ScopedStubInstallAttributes()106 ScopedStubInstallAttributes::~ScopedStubInstallAttributes() {
107   // Make sure that the install_attributes_ that are held by this class
108   // are still the same as the global singleton - if not, then that singleton
109   // has been overwritten somewhere else, which is probably a bug.
110   CHECK_EQ(install_attributes_.get(), InstallAttributes::Get());
111 
112   InstallAttributes::ShutdownForTesting();  // Unset the global singleton.
113 }
114 
Get()115 StubInstallAttributes* ScopedStubInstallAttributes::Get() {
116   return install_attributes_.get();
117 }
118 
119 }  // namespace chromeos
120