• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "session_manager/include/extension_session_manager.h"
17 
18 #include <ability_manager_client.h>
19 #include <session_info.h>
20 #include <start_options.h>
21 
22 #include "session/host/include/extension_session.h"
23 #include "window_manager_hilog.h"
24 
25 namespace OHOS::Rosen {
26 namespace {
27 constexpr HiviewDFX::HiLogLabel LABEL = { LOG_CORE, HILOG_DOMAIN_WINDOW, "ExtensionSessionManager" };
28 const std::string EXTENSION_SESSION_MANAGER_THREAD = "ExtensionSessionManager";
29 } // namespace
30 
GetInstance()31 ExtensionSessionManager& ExtensionSessionManager::GetInstance()
32 {
33     static ExtensionSessionManager* instance = nullptr;
34     if (instance == nullptr) {
35         instance = new ExtensionSessionManager();
36         instance->Init();
37     }
38     return *instance;
39 }
40 
Init()41 void ExtensionSessionManager::Init()
42 {
43     taskScheduler_ = std::make_shared<TaskScheduler>(EXTENSION_SESSION_MANAGER_THREAD);
44 }
45 
SetAbilitySessionInfo(const sptr<ExtensionSession> & extSession)46 sptr<AAFwk::SessionInfo> ExtensionSessionManager::SetAbilitySessionInfo(const sptr<ExtensionSession>& extSession)
47 {
48     sptr<AAFwk::SessionInfo> abilitySessionInfo = new (std::nothrow) AAFwk::SessionInfo();
49     if (!abilitySessionInfo) {
50         WLOGFE("abilitySessionInfo is nullptr");
51         return nullptr;
52     }
53     auto sessionInfo = extSession->GetSessionInfo();
54     sptr<ISession> iSession(extSession);
55     abilitySessionInfo->sessionToken = iSession->AsObject();
56     abilitySessionInfo->callerToken = sessionInfo.callerToken_;
57     abilitySessionInfo->persistentId = extSession->GetPersistentId();
58     if (sessionInfo.want != nullptr) {
59         abilitySessionInfo->want = *sessionInfo.want;
60     }
61     return abilitySessionInfo;
62 }
63 
RequestExtensionSession(const SessionInfo & sessionInfo)64 sptr<ExtensionSession> ExtensionSessionManager::RequestExtensionSession(const SessionInfo& sessionInfo)
65 {
66     auto task = [this, sessionInfo]() {
67         sptr<ExtensionSession> extensionSession = new (std::nothrow) ExtensionSession(sessionInfo);
68         if (extensionSession == nullptr) {
69             WLOGFE("extensionSession is nullptr!");
70             return extensionSession;
71         }
72         auto persistentId = extensionSession->GetPersistentId();
73         WLOGFI("create session persistentId: %{public}d, bundleName: %{public}s, abilityName: %{public}s",
74             persistentId, sessionInfo.bundleName_.c_str(), sessionInfo.abilityName_.c_str());
75         extensionSessionMap_.insert({ persistentId, extensionSession });
76         return extensionSession;
77     };
78 
79     return taskScheduler_->PostSyncTask(task);
80 }
81 
RequestExtensionSessionActivation(const sptr<ExtensionSession> & extensionSession,uint32_t hostWindowId)82 WSError ExtensionSessionManager::RequestExtensionSessionActivation(
83     const sptr<ExtensionSession>& extensionSession, uint32_t hostWindowId)
84 {
85     wptr<ExtensionSession> weakExtSession(extensionSession);
86     WSError ret = WSError::WS_OK;
87     auto task = [this, weakExtSession, &ret, hostWindowId]() {
88         auto extSession = weakExtSession.promote();
89         if (extSession == nullptr) {
90             WLOGFE("session is nullptr");
91             return WSError::WS_ERROR_NULLPTR;
92         }
93         auto persistentId = extSession->GetPersistentId();
94         WLOGFI("Activate session with persistentId: %{public}d", persistentId);
95         if (extensionSessionMap_.count(persistentId) == 0) {
96             WLOGFE("Session is invalid!");
97             return WSError::WS_ERROR_INVALID_SESSION;
98         }
99         auto extSessionInfo = SetAbilitySessionInfo(extSession);
100         if (extSessionInfo == nullptr) {
101             return WSError::WS_ERROR_NULLPTR;
102         }
103         extSessionInfo->hostWindowId = hostWindowId;
104         auto errorCode = AAFwk::AbilityManagerClient::GetInstance()->StartUIExtensionAbility(extSessionInfo,
105             AAFwk::DEFAULT_INVAL_VALUE);
106         ret = (errorCode == ERR_OK) ? WSError::WS_OK : WSError::WS_ERROR_START_UI_EXTENSION_ABILITY_FAILED;
107         return ret;
108     };
109     taskScheduler_->PostSyncTask(task);
110     return ret;
111 }
112 
RequestExtensionSessionBackground(const sptr<ExtensionSession> & extensionSession)113 WSError ExtensionSessionManager::RequestExtensionSessionBackground(const sptr<ExtensionSession>& extensionSession)
114 {
115     wptr<ExtensionSession> weakExtSession(extensionSession);
116     WSError ret = WSError::WS_OK;
117     auto task = [this, weakExtSession, &ret]() {
118         auto extSession = weakExtSession.promote();
119         if (extSession == nullptr) {
120             WLOGFE("session is nullptr");
121             return WSError::WS_ERROR_NULLPTR;
122         }
123         auto persistentId = extSession->GetPersistentId();
124         WLOGFI("Background session with persistentId: %{public}d", persistentId);
125         extSession->SetActive(false);
126         extSession->Background();
127         if (extensionSessionMap_.count(persistentId) == 0) {
128             WLOGFE("Session is invalid!");
129             return WSError::WS_ERROR_INVALID_SESSION;
130         }
131         auto extSessionInfo = SetAbilitySessionInfo(extSession);
132         if (!extSessionInfo) {
133             return WSError::WS_ERROR_NULLPTR;
134         }
135         auto errorCode = AAFwk::AbilityManagerClient::GetInstance()->MinimizeUIExtensionAbility(extSessionInfo);
136         ret = (errorCode == ERR_OK) ? WSError::WS_OK : WSError::WS_ERROR_MIN_UI_EXTENSION_ABILITY_FAILED;
137         return ret;
138     };
139     taskScheduler_->PostSyncTask(task);
140     return ret;
141 }
142 
RequestExtensionSessionDestruction(const sptr<ExtensionSession> & extensionSession)143 WSError ExtensionSessionManager::RequestExtensionSessionDestruction(const sptr<ExtensionSession>& extensionSession)
144 {
145     wptr<ExtensionSession> weakExtSession(extensionSession);
146     WSError ret = WSError::WS_OK;
147     auto task = [this, weakExtSession, &ret]() {
148         auto extSession = weakExtSession.promote();
149         if (extSession == nullptr) {
150             WLOGFE("session is nullptr");
151             return WSError::WS_ERROR_NULLPTR;
152         }
153         auto persistentId = extSession->GetPersistentId();
154         WLOGFI("Destroy session with persistentId: %{public}d", persistentId);
155         extSession->Disconnect();
156         if (extensionSessionMap_.count(persistentId) == 0) {
157             WLOGFE("Session is invalid!");
158             return WSError::WS_ERROR_INVALID_SESSION;
159         }
160         auto extSessionInfo = SetAbilitySessionInfo(extSession);
161         if (!extSessionInfo) {
162             return WSError::WS_ERROR_NULLPTR;
163         }
164         auto errorCode = AAFwk::AbilityManagerClient::GetInstance()->TerminateUIExtensionAbility(extSessionInfo);
165         extensionSessionMap_.erase(persistentId);
166         ret = (errorCode == ERR_OK) ? WSError::WS_OK : WSError::WS_ERROR_TERMINATE_UI_EXTENSION_ABILITY_FAILED;
167         return ret;
168     };
169     taskScheduler_->PostSyncTask(task);
170     return ret;
171 }
172 } // namespace OHOS::Rosen
173