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 #include "geolocation_permission.h"
17 
18 #include <cstdint>
19 #include <vector>
20 
21 #include "nweb_data_base.h"
22 #include "nweb_helper.h"
23 #include "web_errors.h"
24 #include "securec.h"
25 
26 namespace {
27 constexpr int32_t INTERFACE_OK = 0;
28 constexpr int32_t INTERFACE_ERROR = -1;
29 constexpr int32_t ALLOW_PERMISSION_OPERATION = 1;
30 constexpr int32_t DELETE_PERMISSION_OPERATION = 2;
31 
32 } // namespace
33 
34 namespace OHOS {
35 namespace NWeb {
36 
ProcessActionByType(std::string origin, bool incognitoMode, int32_t operationType, int32_t *errCode)37 void GeolocationPermission::ProcessActionByType(std::string origin, bool incognitoMode,
38     int32_t operationType, int32_t *errCode)
39 {
40     std::shared_ptr<OHOS::NWeb::NWebDataBase> dataBase = OHOS::NWeb::NWebHelper::Instance().GetDataBase();
41     if (!dataBase) {
42         return;
43     }
44     if (operationType == ALLOW_PERMISSION_OPERATION) {
45         if (dataBase->SetPermissionByOrigin(origin, OHOS::NWeb::NWebDataBase::WebPermissionType::GEOLOCATION_TYPE, true,
46             incognitoMode) == NWebError::INVALID_ORIGIN) {
47             *errCode = NWebError::INVALID_ORIGIN;
48             return;
49         }
50     } else if (operationType == DELETE_PERMISSION_OPERATION) {
51         if (dataBase->ClearPermissionByOrigin(origin, OHOS::NWeb::NWebDataBase::WebPermissionType::GEOLOCATION_TYPE,
52             incognitoMode) == NWebError::INVALID_ORIGIN) {
53             *errCode =NWebError::INVALID_ORIGIN;
54             return;
55         }
56     }
57     return;
58 }
59 
ExecuteGetPermissionState(std::string origin, bool incognitoMode, int32_t *errCode)60 bool GeolocationPermission::ExecuteGetPermissionState(std::string origin, bool incognitoMode, int32_t *errCode)
61 {
62     bool retValue = false;
63     std::shared_ptr<OHOS::NWeb::NWebDataBase> dataBase = OHOS::NWeb::NWebHelper::Instance().GetDataBase();
64     if (!dataBase) {
65         *errCode = INTERFACE_ERROR;
66         return retValue;
67     }
68     if (dataBase->GetPermissionResultByOrigin(origin,
69         OHOS::NWeb::NWebDataBase::WebPermissionType::GEOLOCATION_TYPE, retValue, incognitoMode)) {
70         *errCode = INTERFACE_OK;
71     } else {
72         *errCode = NWebError::INVALID_ORIGIN;
73     }
74     return retValue;
75 }
76 
77 
ExecuteGetOrigins(bool incognitoMode, int32_t *errCode)78 std::vector<std::string> GeolocationPermission::ExecuteGetOrigins(bool incognitoMode, int32_t *errCode)
79 {
80     std::vector<std::string> origins;
81     std::shared_ptr<OHOS::NWeb::NWebDataBase> dataBase = OHOS::NWeb::NWebHelper::Instance().GetDataBase();
82     if (!dataBase) {
83         *errCode = INTERFACE_ERROR;
84         return origins;
85     }
86     origins = dataBase->GetOriginsByPermission(
87         OHOS::NWeb::NWebDataBase::WebPermissionType::GEOLOCATION_TYPE, incognitoMode);
88     *errCode = INTERFACE_OK;
89     return origins;
90 }
91 
CjAllowGeolocation(std::string origin, bool incognitoMode, int32_t *errCode)92 void GeolocationPermission::CjAllowGeolocation(std::string origin, bool incognitoMode, int32_t *errCode)
93 {
94     return ProcessActionByType(origin, incognitoMode, ALLOW_PERMISSION_OPERATION, errCode);
95 }
96 
CjDeleteGeolocation(std::string origin, bool incognitoMode, int32_t *errCode)97 void GeolocationPermission::CjDeleteGeolocation(std::string origin, bool incognitoMode, int32_t *errCode)
98 {
99     return ProcessActionByType(origin, incognitoMode, DELETE_PERMISSION_OPERATION, errCode);
100 }
101 
CjGetAccessibleGeolocation(std::string origin, bool incognitoMode, int32_t *errCode)102 bool GeolocationPermission::CjGetAccessibleGeolocation(std::string origin, bool incognitoMode, int32_t *errCode)
103 {
104     return ExecuteGetPermissionState(origin, incognitoMode, errCode);
105 }
106 
CjGetStoredGeolocation(bool incognitoMode, int32_t *errCode)107 std::vector<std::string> GeolocationPermission::CjGetStoredGeolocation(bool incognitoMode, int32_t *errCode)
108 {
109     return ExecuteGetOrigins(incognitoMode, errCode);
110 }
111 
CjDeleteAllGeolocation(bool incognitoMode, int32_t *errCode)112 void GeolocationPermission::CjDeleteAllGeolocation(bool incognitoMode, int32_t *errCode)
113 {
114     std::shared_ptr<OHOS::NWeb::NWebDataBase> dataBase = OHOS::NWeb::NWebHelper::Instance().GetDataBase();
115     if (dataBase != nullptr) {
116         dataBase->ClearAllPermission(OHOS::NWeb::NWebDataBase::WebPermissionType::GEOLOCATION_TYPE, incognitoMode);
117     }
118     return;
119 }
120 
121 } // namespace NWeb
122 } // namespace OHOS
123