1 /*
2  * Copyright (c) 2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef SECURITY_ACCESS_TOKEN_OPEN_CALLBACK_H
17 #define SECURITY_ACCESS_TOKEN_OPEN_CALLBACK_H
18 
19 #include "access_token_db_util.h"
20 #include "rdb_open_callback.h"
21 #include "rdb_store.h"
22 
23 namespace OHOS {
24 namespace Security {
25 namespace AccessToken {
26 
27 static constexpr const char* DATABASE_PATH = "/data/service/el1/public/access_token/";
28 
29 static constexpr const int32_t DATABASE_VERSION_1 = 1;
30 static constexpr const int32_t DATABASE_VERSION_2 = 2;
31 static constexpr const int32_t DATABASE_VERSION_3 = 3;
32 static constexpr const int32_t DATABASE_VERSION_4 = 4;
33 
34 class AccessTokenOpenCallback : public NativeRdb::RdbOpenCallback {
35 public:
36     /**
37      * Called when the database associate with this RdbStore is created with the first time.
38      * This is where the creation of tables and insert the initial data of tables should happen.
39      *
40      * param store The RdbStore object.
41      */
42     int32_t OnCreate(NativeRdb::RdbStore& rdbStore) override;
43     /**
44      * Called when the database associate whit this RdbStore needs to be upgrade.
45      *
46      * param store The RdbStore object.
47      * param oldVersion The old database version.
48      * param newVersion The new database version.
49      */
50     int32_t OnUpgrade(NativeRdb::RdbStore& rdbStore, int32_t currentVersion, int32_t targetVersion) override;
51 
52 private:
53     // OnCreate
54     int32_t CreateHapTokenInfoTable(NativeRdb::RdbStore& rdbStore);
55     int32_t CreateNativeTokenInfoTable(NativeRdb::RdbStore& rdbStore);
56     int32_t CreatePermissionDefinitionTable(NativeRdb::RdbStore& rdbStore);
57     int32_t CreatePermissionStateTable(NativeRdb::RdbStore& rdbStore);
58     int32_t CreatePermissionRequestToggleStatusTable(NativeRdb::RdbStore& rdbStore);
59 
60     // OnUpdate
61     int32_t AddAvailableTypeColumn(NativeRdb::RdbStore& rdbStore);
62     int32_t AddRequestToggleStatusColumn(NativeRdb::RdbStore& rdbStore);
63     int32_t AddPermDialogCapColumn(NativeRdb::RdbStore& rdbStore);
64     int32_t HandleUpdateWithFlag(NativeRdb::RdbStore& rdbStore, uint32_t flag);
65     int32_t UpdateFromVersionOne(NativeRdb::RdbStore& rdbStore, int32_t targetVersion);
66     int32_t UpdateFromVersionTwo(NativeRdb::RdbStore& rdbStore, int32_t targetVersion);
67     int32_t UpdateFromVersionThree(NativeRdb::RdbStore& rdbStore, int32_t targetVersion);
68 };
69 } // namespace AccessToken
70 } // namespace Security
71 } // namespace OHOS
72 
73 #endif // SECURITY_ACCESS_TOKEN_OPEN_CALLBACK_H
74