1 /*
2  * Copyright (c) 2023 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 "extension_record.h"
17 
18 #include "ability_manager_service.h"
19 #include "ability_util.h"
20 
21 namespace OHOS {
22 namespace AbilityRuntime {
ExtensionRecord(const std::shared_ptr<AAFwk::AbilityRecord> & abilityRecord)23 ExtensionRecord::ExtensionRecord(const std::shared_ptr<AAFwk::AbilityRecord> &abilityRecord)
24     : abilityRecord_(abilityRecord)
25 {}
26 
27 ExtensionRecord::~ExtensionRecord() = default;
28 
GetCallToken() const29 sptr<IRemoteObject> ExtensionRecord::GetCallToken() const
30 {
31     CHECK_POINTER_AND_RETURN(abilityRecord_, nullptr);
32     auto sessionInfo = abilityRecord_->GetSessionInfo();
33     CHECK_POINTER_AND_RETURN(sessionInfo, nullptr);
34     return sessionInfo->callerToken;
35 }
36 
GetRootCallerToken() const37 sptr<IRemoteObject> ExtensionRecord::GetRootCallerToken() const
38 {
39     return rootCallerToken_;
40 }
41 
SetRootCallerToken(sptr<IRemoteObject> & rootCallerToken)42 void ExtensionRecord::SetRootCallerToken(sptr<IRemoteObject> &rootCallerToken)
43 {
44     rootCallerToken_ = rootCallerToken;
45 }
46 
GetFocusedCallerToken() const47 sptr<IRemoteObject> ExtensionRecord::GetFocusedCallerToken() const
48 {
49     return focusedCallerToken_;
50 }
51 
SetFocusedCallerToken(sptr<IRemoteObject> & focusedCallerToken)52 void ExtensionRecord::SetFocusedCallerToken(sptr<IRemoteObject> &focusedCallerToken)
53 {
54     focusedCallerToken_ = focusedCallerToken;
55 }
56 
UnloadUIExtensionAbility()57 void ExtensionRecord::UnloadUIExtensionAbility()
58 {
59     auto ret = DelayedSingleton<AppExecFwk::AppMgrClient>::GetInstance()->UnregisterApplicationStateObserver(
60         preLoadUIExtStateObserver_);
61     if (ret != ERR_OK) {
62         TAG_LOGE(AAFwkTag::ABILITYMGR, "Unregister application state observer error.");
63     }
64     auto result = DelayedSingleton<AAFwk::AbilityManagerService>::GetInstance()->UnloadUIExtensionAbility(
65         abilityRecord_, hostBundleName_);
66     if (result != ERR_OK) {
67         TAG_LOGE(AAFwkTag::ABILITYMGR, "Unload UIExtension error.");
68     }
69 }
70 
RegisterStateObserver(const std::string & hostBundleName)71 int32_t ExtensionRecord::RegisterStateObserver(const std::string &hostBundleName)
72 {
73     preLoadUIExtStateObserver_ = sptr<AAFwk::PreLoadUIExtStateObserver>::MakeSptr(weak_from_this());
74     auto ret = IN_PROCESS_CALL(
75         DelayedSingleton<AppExecFwk::AppMgrClient>::GetInstance()->RegisterApplicationStateObserver(
76             preLoadUIExtStateObserver_, {hostBundleName}));
77     return ret;
78 }
79 
ContinueToGetCallerToken()80 bool ExtensionRecord::ContinueToGetCallerToken()
81 {
82     return false;
83 }
84 
Update(const AAFwk::AbilityRequest & abilityRequest)85 void ExtensionRecord::Update(const AAFwk::AbilityRequest &abilityRequest)
86 {
87 }
88 } // namespace AbilityRuntime
89 } // namespace OHOS
90