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 "fence_extension_context.h"
17 #include "ability_business_error.h"
18 #include "ability_manager_client.h"
19 #include "ability_manager_errors.h"
20 #include "accesstoken_kit.h"
21 #include "ipc_skeleton.h"
22 #include "tokenid_kit.h"
23 #include "location_log.h"
24 
25 namespace OHOS {
26 namespace Location {
27 namespace {
28 constexpr const char *PERMISSION_START_ABILITIES_FROM_BACKGROUND = "ohos.permission.START_ABILITIES_FROM_BACKGROUND";
29 }
30 
FenceExtensionContext()31 FenceExtensionContext::FenceExtensionContext(){};
32 
~FenceExtensionContext()33 FenceExtensionContext::~FenceExtensionContext(){};
34 
StartServiceExtensionAbility(const AAFwk::Want & want,int32_t accountId) const35 ErrCode FenceExtensionContext::StartServiceExtensionAbility(const AAFwk::Want &want, int32_t accountId) const
36 {
37     ErrCode err = ERR_OK;
38 
39     std::string callerBundleName = GetBundleName();
40     std::string calledBundleName = want.GetBundle();
41     if (calledBundleName != callerBundleName) {
42         LBSLOGE(FENCE_EXTENSION, "This application won't start no-self-ability.");
43         err = AAFwk::ERR_NOT_SELF_APPLICATION;
44         return err;
45     }
46 
47     LBSLOGI(FENCE_EXTENSION, "start %{public}s begin.", __func__);
48     err = AAFwk::AbilityManagerClient::GetInstance()->StartExtensionAbility(
49         want, token_, accountId, AppExecFwk::ExtensionAbilityType::SERVICE);
50     if (err != ERR_OK) {
51         LBSLOGE(FENCE_EXTENSION, "FenceExtensionContext::StartServiceExtensionAbility is failed %{public}d", err);
52     }
53     return err;
54 }
55 
CheckCallerIsSystemApp() const56 bool FenceExtensionContext::CheckCallerIsSystemApp() const
57 {
58     auto selfToken = IPCSkeleton::GetSelfTokenID();
59     if (!Security::AccessToken::TokenIdKit::IsSystemAppByFullTokenID(selfToken)) {
60         return false;
61     }
62     return true;
63 }
64 }  // namespace Location
65 }  // namespace OHOS