Line data Source code
1 : /*
2 : *
3 : * Copyright (c) 2024 Project CHIP Authors
4 : * All rights reserved.
5 : *
6 : * Licensed under the Apache License, Version 2.0 (the "License");
7 : * you may not use this file except in compliance with the License.
8 : * You may obtain a copy of the License at
9 : *
10 : * http://www.apache.org/licenses/LICENSE-2.0
11 : *
12 : * Unless required by applicable law or agreed to in writing, software
13 : * distributed under the License is distributed on an "AS IS" BASIS,
14 : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 : * See the License for the specific language governing permissions and
16 : * limitations under the License.
17 : */
18 :
19 : #pragma once
20 :
21 : #include "TermsAndConditionsProvider.h"
22 :
23 : #include <stdint.h>
24 :
25 : #include <lib/core/CHIPError.h>
26 : #include <lib/core/CHIPPersistentStorageDelegate.h>
27 : #include <lib/core/Optional.h>
28 :
29 : namespace chip {
30 : namespace app {
31 :
32 : /**
33 : * @brief Abstract interface for storing and retrieving terms and conditions acceptance status.
34 : *
35 : * This class defines the methods required to interact with the underlying storage system
36 : * for saving, retrieving, and deleting the user's acceptance of terms and conditions.
37 : */
38 : class TermsAndConditionsStorageDelegate
39 : {
40 : public:
41 62 : virtual ~TermsAndConditionsStorageDelegate() = default;
42 :
43 : /**
44 : * @brief Deletes the persisted terms and conditions acceptance status from storage.
45 : *
46 : * This method deletes the stored record of the user's acceptance of the terms and conditions,
47 : * effectively resetting their acceptance status in the persistent storage.
48 : *
49 : * @retval CHIP_NO_ERROR if the record was successfully deleted.
50 : * @retval CHIP_ERROR_UNINITIALIZED if the storage delegate is not properly initialized.
51 : * @retval CHIP_ERROR_* for other errors.
52 : */
53 : virtual CHIP_ERROR Delete() = 0;
54 :
55 : /**
56 : * @brief Retrieves the persisted terms and conditions acceptance status from storage.
57 : *
58 : * This method attempts to retrieve the previously accepted terms and conditions from
59 : * persistent storage. If no such record exists, it returns an empty `Optional`.
60 : *
61 : * @param[out] outTermsAndConditions The retrieved terms and conditions, if any exist.
62 : *
63 : * @retval CHIP_NO_ERROR if the terms were successfully retrieved.
64 : * @retval CHIP_ERROR_UNINITIALIZED if the storage delegate is not properly initialized.
65 : * @retval CHIP_ERROR_* for other errors.
66 : */
67 : virtual CHIP_ERROR Get(Optional<TermsAndConditions> & outTermsAndConditions) = 0;
68 :
69 : /**
70 : * @brief Persists the user's acceptance of the terms and conditions.
71 : *
72 : * This method stores the provided terms and conditions acceptance status in persistent
73 : * storage, allowing the user's acceptance to be retrieved later.
74 : *
75 : * @param[in] inTermsAndConditions The terms and conditions to be saved.
76 : *
77 : * @retval CHIP_NO_ERROR if the terms were successfully stored.
78 : * @retval CHIP_ERROR_UNINITIALIZED if the storage delegate is not properly initialized.
79 : * @retval CHIP_ERROR_* for other errors.
80 : */
81 : virtual CHIP_ERROR Set(const TermsAndConditions & inTermsAndConditions) = 0;
82 : };
83 :
84 : /**
85 : * @brief Default implementation of the TermsAndConditionsStorageDelegate using a persistent storage backend.
86 : *
87 : * This class provides an implementation of the TermsAndConditionsStorageDelegate interface, storing
88 : * and retrieving the user's terms and conditions acceptance from persistent storage. It requires a
89 : * PersistentStorageDelegate to interface with the storage system.
90 : */
91 : class DefaultTermsAndConditionsStorageDelegate : public TermsAndConditionsStorageDelegate
92 : {
93 : public:
94 : /**
95 : * @brief Initializes the storage delegate with a persistent storage backend.
96 : *
97 : * This method initializes the storage delegate with the provided persistent storage delegate.
98 : * The storage delegate must be initialized before performing any operations.
99 : *
100 : * @param[in] inPersistentStorageDelegate The storage backend used for saving and retrieving data.
101 : *
102 : * @retval CHIP_NO_ERROR if the storage delegate was successfully initialized.
103 : * @retval CHIP_ERROR_INVALID_ARGUMENT if the provided storage delegate is null.
104 : */
105 : CHIP_ERROR Init(PersistentStorageDelegate * inPersistentStorageDelegate);
106 :
107 : CHIP_ERROR Delete() override;
108 :
109 : CHIP_ERROR Get(Optional<TermsAndConditions> & inTermsAndConditions) override;
110 :
111 : CHIP_ERROR Set(const TermsAndConditions & inTermsAndConditions) override;
112 :
113 : private:
114 : PersistentStorageDelegate * mStorageDelegate = nullptr;
115 : };
116 :
117 : class DefaultTermsAndConditionsProvider : public TermsAndConditionsProvider
118 : {
119 : public:
120 : /**
121 : * @brief Initializes the TermsAndConditionsProvider.
122 : *
123 : * @param[in] inStorageDelegate Storage delegate dependency.
124 : * @param[in] inRequiredTermsAndConditions The required terms and conditions that must be met.
125 : */
126 : CHIP_ERROR Init(TermsAndConditionsStorageDelegate * inStorageDelegate,
127 : const Optional<TermsAndConditions> & inRequiredTermsAndConditions);
128 :
129 : CHIP_ERROR CommitAcceptance() override;
130 :
131 : CHIP_ERROR GetAcceptance(Optional<TermsAndConditions> & outTermsAndConditions) const override;
132 :
133 : CHIP_ERROR GetAcknowledgementsRequired(bool & outAcknowledgementsRequired) const override;
134 :
135 : CHIP_ERROR GetRequirements(Optional<TermsAndConditions> & outTermsAndConditions) const override;
136 :
137 : CHIP_ERROR GetUpdateAcceptanceDeadline(Optional<uint32_t> & outUpdateAcceptanceDeadline) const override;
138 :
139 : CHIP_ERROR ResetAcceptance() override;
140 :
141 : CHIP_ERROR RevertAcceptance() override;
142 :
143 : CHIP_ERROR SetAcceptance(const Optional<TermsAndConditions> & inTermsAndConditions) override;
144 :
145 : private:
146 : TermsAndConditionsStorageDelegate * mTermsAndConditionsStorageDelegate;
147 : Optional<TermsAndConditions> mTemporalAcceptance;
148 : Optional<TermsAndConditions> mRequiredAcknowledgements;
149 : };
150 :
151 : } // namespace app
152 : } // namespace chip
|