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 "continue_scene_session_handler.h"
17 
18 #include <chrono>
19 #include <thread>
20 
21 #include "dtbschedmgr_log.h"
22 #include "scene_board_judgement.h"
23 #include "session_manager_lite.h"
24 
25 namespace OHOS {
26 namespace DistributedSchedule {
27 namespace {
28 const std::string TAG = "ContinueSceneSessionHandler";
29 constexpr int32_t RETRY_TIMES = 60;
30 constexpr int32_t SLEEP_TIME = 50;
31 }
32 
33 IMPLEMENT_SINGLE_INSTANCE(ContinueSceneSessionHandler);
34 
35 using OHOS::Rosen::SessionManagerLite;
36 using OHOS::Rosen::WSError;
37 
UpdateContinueSessionId(const std::string & bundleName,const std::string & abilityName)38 void ContinueSceneSessionHandler::UpdateContinueSessionId(const std::string& bundleName, const std::string& abilityName)
39 {
40     HILOGD("Update continueSessionId, bundleName: %{public}s, abilityName: %{public}s",
41         bundleName.c_str(), abilityName.c_str());
42     auto now = std::chrono::system_clock::now();
43     int64_t timeStamp = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()).count();
44     std::size_t hash = std::hash<std::string>()(bundleName + abilityName + std::to_string(timeStamp));
45     continueSessionId_ = std::to_string(hash);
46 }
47 
GetContinueSessionId() const48 std::string ContinueSceneSessionHandler::GetContinueSessionId() const
49 {
50     return continueSessionId_;
51 }
52 
ClearContinueSessionId()53 void ContinueSceneSessionHandler::ClearContinueSessionId()
54 {
55     HILOGI("%{public}s called", __func__);
56     continueSessionId_.clear();
57 }
58 
GetPersistentId(int32_t & persistentId)59 int32_t ContinueSceneSessionHandler::GetPersistentId(int32_t& persistentId)
60 {
61     HILOGI("%{public}s called", __func__);
62     if (continueSessionId_.empty()) {
63         HILOGE("continueSessionId is empty.");
64         return INVALID_PARAMETERS_ERR;
65     }
66     if (!Rosen::SceneBoardJudgement::IsSceneBoardEnabled()) {
67         HILOGE("sceneBoard not available.");
68         return INVALID_PARAMETERS_ERR;
69     }
70 
71     auto sceneSessionManager = SessionManagerLite::GetInstance().GetSceneSessionManagerLiteProxy();
72     if (!sceneSessionManager) {
73         HILOGE("proxy is nullptr.");
74         return INVALID_PARAMETERS_ERR;
75     }
76 
77     int32_t retryTimeout = RETRY_TIMES;
78     AAFwk::MissionInfo missionInfo;
79     do {
80         auto err = sceneSessionManager->GetSessionInfoByContinueSessionId(continueSessionId_, missionInfo);
81         if (err == WSError::WS_OK) {
82             persistentId = missionInfo.id;
83             return ERR_OK;
84         }
85         HILOGE("Get sessionInfo failed, continueSessionId: %{public}s, errorCode: %{public}d",
86             continueSessionId_.c_str(), err);
87         std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_TIME));
88     } while (--retryTimeout >= 0);
89 
90     return INVALID_PARAMETERS_ERR;
91 }
92 } // namespace DistributedSchedule
93 } // namespace OHOS