• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 "js_uri_perm_mgr.h"
17 
18 #include "ability_business_error.h"
19 #include "ability_manager_errors.h"
20 #include "ability_runtime_error_util.h"
21 #include "hilog_wrapper.h"
22 #include "ipc_skeleton.h"
23 #include "js_error_utils.h"
24 #include "js_runtime_utils.h"
25 #include "napi_common_util.h"
26 #include "tokenid_kit.h"
27 #include "uri.h"
28 #include "uri_permission_manager_client.h"
29 
30 namespace OHOS {
31 namespace AbilityRuntime {
32 namespace {
33 constexpr int32_t ERR_OK = 0;
34 constexpr int32_t argCountFour = 4;
35 constexpr int32_t argCountThree = 3;
36 constexpr int32_t argCountTwo = 2;
37 }
38 class JsUriPermMgr {
39 public:
40     JsUriPermMgr() = default;
41     ~JsUriPermMgr() = default;
42 
Finalizer(NativeEngine * engine,void * data,void * hint)43     static void Finalizer(NativeEngine* engine, void* data, void* hint)
44     {
45         HILOG_INFO("JsUriPermMgr::Finalizer is called");
46         std::unique_ptr<JsUriPermMgr>(static_cast<JsUriPermMgr*>(data));
47     }
48 
GrantUriPermission(NativeEngine * engine,NativeCallbackInfo * info)49     static NativeValue* GrantUriPermission(NativeEngine* engine, NativeCallbackInfo* info)
50     {
51         JsUriPermMgr* me = CheckParamsAndGetThis<JsUriPermMgr>(engine, info);
52         return (me != nullptr) ? me->OnGrantUriPermission(*engine, *info) : nullptr;
53     }
54 
RevokeUriPermission(NativeEngine * engine,NativeCallbackInfo * info)55     static NativeValue* RevokeUriPermission(NativeEngine* engine, NativeCallbackInfo* info)
56     {
57         JsUriPermMgr* me = CheckParamsAndGetThis<JsUriPermMgr>(engine, info);
58         return (me != nullptr) ? me->OnRevokeUriPermission(*engine, *info) : nullptr;
59     }
60 
61 private:
OnGrantUriPermission(NativeEngine & engine,NativeCallbackInfo & info)62     NativeValue* OnGrantUriPermission(NativeEngine& engine, NativeCallbackInfo& info)
63     {
64         HILOG_DEBUG("Grant Uri Permission start");
65         if (info.argc != argCountThree && info.argc != argCountFour) {
66             HILOG_ERROR("The number of parameter is invalid.");
67             ThrowTooFewParametersError(engine);
68             return engine.CreateUndefined();
69         }
70         std::string uriStr;
71         if (!OHOS::AppExecFwk::UnwrapStringFromJS2(reinterpret_cast<napi_env>(&engine),
72             reinterpret_cast<napi_value>(info.argv[0]), uriStr)) {
73             HILOG_ERROR("The uriStr is invalid.");
74             ThrowError(engine, AbilityErrorCode::ERROR_CODE_INVALID_PARAM);
75             return engine.CreateUndefined();
76         }
77         int flag = 0;
78         if (!OHOS::AppExecFwk::UnwrapInt32FromJS2(reinterpret_cast<napi_env>(&engine),
79             reinterpret_cast<napi_value>(info.argv[1]), flag)) {
80             HILOG_ERROR("The flag is invalid.");
81             ThrowError(engine, AbilityErrorCode::ERROR_CODE_INVALID_PARAM);
82             return engine.CreateUndefined();
83         }
84         std::string targetBundleName;
85         if (!OHOS::AppExecFwk::UnwrapStringFromJS2(reinterpret_cast<napi_env>(&engine),
86             reinterpret_cast<napi_value>(info.argv[argCountTwo]), targetBundleName)) {
87             HILOG_ERROR("The targetBundleName is invalid.");
88             ThrowError(engine, AbilityErrorCode::ERROR_CODE_INVALID_PARAM);
89             return engine.CreateUndefined();
90         }
91         auto selfToken = IPCSkeleton::GetSelfTokenID();
92         if (!Security::AccessToken::TokenIdKit::IsSystemAppByFullTokenID(selfToken)) {
93             HILOG_ERROR("This application is not system-app, can not use system-api");
94             ThrowError(engine, AbilityErrorCode::ERROR_CODE_NOT_SYSTEM_APP);
95             return engine.CreateUndefined();
96         }
97         AsyncTask::CompleteCallback complete =
98         [uriStr, flag, targetBundleName](NativeEngine& engine, AsyncTask& task, int32_t status) {
99             Uri uri(uriStr);
100             auto errCode = AAFwk::UriPermissionManagerClient::GetInstance().GrantUriPermission(uri, flag,
101                 targetBundleName, 0);
102             if (errCode == ERR_OK) {
103                 task.ResolveWithNoError(engine, engine.CreateUndefined());
104             } else if (errCode ==  AAFwk::CHECK_PERMISSION_FAILED) {
105                 task.Reject(engine, CreateNoPermissionError(engine, "ohos.permission.PROXY_AUTHORIZATION_URI"));
106             } else if (errCode == AAFwk::ERR_CODE_INVALID_URI_FLAG) {
107                 task.Reject(engine, CreateJsError(engine, ERR_ABILITY_RUNTIME_EXTERNAL_INVALID_URI_FLAG,
108                 "Invalid URI flag."));
109             } else if (errCode == AAFwk::ERR_CODE_INVALID_URI_TYPE) {
110                 task.Reject(engine, CreateJsError(engine, ERR_ABILITY_RUNTIME_EXTERNAL_INVALID_URI_TYPE,
111                 "Only support file URI."));
112             } else if (errCode == AAFwk::ERR_CODE_GRANT_URI_PERMISSION) {
113                 task.Reject(engine, CreateJsError(engine, ERR_ABILITY_RUNTIME_EXTERNAL_GRANT_URI_PERMISSION,
114                 "Sandbox application can not grant URI permission."));
115             } else {
116                 task.Reject(engine, CreateJsError(engine, ERR_ABILITY_RUNTIME_EXTERNAL_INTERNAL_ERROR,
117                 "Internal Error."));
118             }
119         };
120         NativeValue* lastParam = (info.argc == argCountFour) ? info.argv[argCountThree] : nullptr;
121         NativeValue* result = nullptr;
122         AsyncTask::ScheduleHighQos("JsUriPermMgr::OnGrantUriPermission",
123             engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
124         return result;
125     }
126 
OnRevokeUriPermission(NativeEngine & engine,NativeCallbackInfo & info)127     NativeValue* OnRevokeUriPermission(NativeEngine& engine, NativeCallbackInfo& info)
128     {
129         // only support 2 or 3 params (2 parameter and 1 optional callback)
130         if (info.argc != argCountThree && info.argc != argCountTwo) {
131             HILOG_ERROR("Invalid arguments");
132             ThrowTooFewParametersError(engine);
133             return engine.CreateUndefined();
134         }
135         std::string uriStr;
136         if (!OHOS::AppExecFwk::UnwrapStringFromJS2(reinterpret_cast<napi_env>(&engine),
137             reinterpret_cast<napi_value>(info.argv[0]), uriStr)) {
138             HILOG_ERROR("The uriStr is invalid.");
139             ThrowError(engine, AbilityErrorCode::ERROR_CODE_INVALID_PARAM);
140             return engine.CreateUndefined();
141         }
142         std::string bundleName;
143         if (!OHOS::AppExecFwk::UnwrapStringFromJS2(reinterpret_cast<napi_env>(&engine),
144             reinterpret_cast<napi_value>(info.argv[1]), bundleName)) {
145             HILOG_ERROR("The bundleName is invalid.");
146             ThrowError(engine, AbilityErrorCode::ERROR_CODE_INVALID_PARAM);
147             return engine.CreateUndefined();
148         }
149         auto selfToken = IPCSkeleton::GetSelfTokenID();
150         if (!Security::AccessToken::TokenIdKit::IsSystemAppByFullTokenID(selfToken)) {
151             HILOG_ERROR("This application is not system-app, can not use system-api");
152             ThrowError(engine, AbilityErrorCode::ERROR_CODE_NOT_SYSTEM_APP);
153             return engine.CreateUndefined();
154         }
155         AsyncTask::CompleteCallback complete =
156         [uriStr, bundleName](NativeEngine& engine, AsyncTask& task, int32_t status) {
157             Uri uri(uriStr);
158             auto errCode = AAFwk::UriPermissionManagerClient::GetInstance().RevokeUriPermissionManually(uri,
159                 bundleName);
160             if (errCode == ERR_OK) {
161                 task.ResolveWithNoError(engine, engine.CreateUndefined());
162             } else if (errCode == AAFwk::CHECK_PERMISSION_FAILED) {
163                 task.Reject(engine, CreateNoPermissionError(engine,
164                     "Do not have permission ohos.permission.PROXY_AUTHORIZATION_URI"));
165             } else if (errCode == AAFwk::ERR_CODE_INVALID_URI_TYPE) {
166                 task.Reject(engine, CreateJsError(engine, ERR_ABILITY_RUNTIME_EXTERNAL_INVALID_URI_TYPE,
167                 "Only support file URI."));
168             } else {
169                 task.Reject(engine, CreateJsError(engine, ERR_ABILITY_RUNTIME_EXTERNAL_INTERNAL_ERROR,
170                 "Internal Error."));
171             }
172         };
173         NativeValue* lastParam = (info.argc == argCountThree) ? info.argv[argCountTwo] : nullptr;
174         NativeValue* result = nullptr;
175         AsyncTask::ScheduleHighQos("JsUriPermMgr::OnRevokeUriPermission",
176             engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
177         return result;
178     }
179 };
180 
CreateJsUriPermMgr(NativeEngine * engine,NativeValue * exportObj)181 NativeValue* CreateJsUriPermMgr(NativeEngine* engine, NativeValue* exportObj)
182 {
183     HILOG_INFO("CreateJsUriPermMgr is called");
184     if (engine == nullptr || exportObj == nullptr) {
185         HILOG_INFO("Invalid input parameters");
186         return nullptr;
187     }
188 
189     NativeObject* object = ConvertNativeValueTo<NativeObject>(exportObj);
190     if (object == nullptr) {
191         HILOG_INFO("object is nullptr");
192         return nullptr;
193     }
194 
195     std::unique_ptr<JsUriPermMgr> jsUriPermMgr = std::make_unique<JsUriPermMgr>();
196     object->SetNativePointer(jsUriPermMgr.release(), JsUriPermMgr::Finalizer, nullptr);
197 
198     const char *moduleName = "JsUriPermMgr";
199     BindNativeFunction(*engine, *object, "grantUriPermission", moduleName, JsUriPermMgr::GrantUriPermission);
200     BindNativeFunction(*engine, *object, "revokeUriPermission", moduleName, JsUriPermMgr::RevokeUriPermission);
201     return engine->CreateUndefined();
202 }
203 }  // namespace AbilityRuntime
204 }  // namespace OHOS
205