• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 #include "migration_manager_adapter_impl.h"
16 
17 #include <cmath>
18 
19 #include "ability_manager_client.h"
20 #include "nweb_log.h"
21 
22 using namespace OHOS::NWeb;
23 
24 namespace OHOS::NWeb {
25 static const uint32_t CMD_CODE = 1;
26 static sptr<OHOS::IRemoteObject> remoteObject_ = nullptr;
27 static const std::string REPLY_CODE = "code";
28 static const std::string REPLY_ERROR_LIST = "errorList";
29 static const std::string REPLY_SUCCESS_COUNT = "successCount";
30 static const std::string REPLY_INDEX = "index";
31 std::atomic<bool> MigrationManagerAdapterImpl::isConnectSystemUI_(false);
32 
OnMigrationReply(int32_t errorCode,int32_t succussCount,const std::vector<int32_t> & errorIndex,const std::vector<int32_t> & codeList)33 void MigrationManagerAdapterImpl::MigrationListenerAdapterImpl::OnMigrationReply(int32_t errorCode,
34     int32_t succussCount, const std::vector<int32_t>& errorIndex, const std::vector<int32_t>& codeList)
35 {
36     if (listener_) {
37         WVLOG_I("on migration replay will be invoked");
38         listener_->OnMigrationReply(errorCode, succussCount, errorIndex, codeList);
39     }
40 }
41 
OnAbilityConnectDone(const AppExecFwk::ElementName & element,const sptr<IRemoteObject> & remoteObject,int resultCode)42 void MigrationManagerAdapterImpl::MigrationListenerAdapterImpl::OnAbilityConnectDone(
43     const AppExecFwk::ElementName& element, const sptr<IRemoteObject>& remoteObject, int resultCode)
44 {
45     WVLOG_I("send migration request begin");
46     if (!remoteObject) {
47         return;
48     }
49     if (remoteObject_ == nullptr) {
50         remoteObject_ = remoteObject;
51     }
52 
53     MessageParcel data;
54     MessageParcel reply;
55     MessageOption option;
56     isConnectSystemUI_.store(true);
57 
58     data.WriteInterfaceToken(Str8ToStr16(token_));
59     data.WriteString16(Str8ToStr16(jsonData_));
60     int32_t ret = remoteObject_->SendRequest(CMD_CODE, data, reply, option);
61     if (ret != ERR_OK) {
62         WVLOG_W("send request failed: %{public}d", ret);
63         return;
64     }
65     std::string replyData = Str16ToStr8(reply.ReadString16());
66     WVLOG_I("send migration request reply res: %{public}s", replyData.c_str());
67 
68     int errorCode = 0;
69     int successCount = 0;
70     std::vector<int> errorIndexList;
71     std::vector<int> codeList;
72     nlohmann::json jsonObj = nlohmann::json::parse(replyData);
73     if (jsonObj.contains(REPLY_CODE) && jsonObj[REPLY_CODE].is_number_integer()) {
74         errorCode = jsonObj[REPLY_CODE];
75     }
76     if (jsonObj.contains(REPLY_ERROR_LIST)) {
77         auto errorList = jsonObj[REPLY_ERROR_LIST];
78         for (size_t i = 0; i < errorList.size(); ++i) {
79             if (errorList[i].contains(REPLY_CODE) && errorList[i].contains(REPLY_INDEX)) {
80                 int errorCode = errorList[i][REPLY_CODE];
81                 int errorIndex = errorList[i][REPLY_INDEX];
82                 codeList.push_back(errorCode);
83                 errorIndexList.push_back(errorIndex);
84             }
85         }
86     }
87     if (jsonObj.contains(REPLY_SUCCESS_COUNT) && jsonObj[REPLY_SUCCESS_COUNT].is_number_integer()) {
88         successCount = jsonObj[REPLY_SUCCESS_COUNT];
89     }
90     WVLOG_I("send migration request successCount: %{public}d", successCount);
91     OnMigrationReply(errorCode, successCount, errorIndexList, codeList);
92     WVLOG_I("send migration request finish");
93 }
94 
SetJsonData(const std::string & jsonData)95 void MigrationManagerAdapterImpl::MigrationListenerAdapterImpl::SetJsonData(const std::string& jsonData)
96 {
97     WVLOG_D("set json data invoke");
98     jsonData_ = jsonData;
99 }
100 
SetInterfaceToken(const std::string & token)101 void MigrationManagerAdapterImpl::MigrationListenerAdapterImpl::SetInterfaceToken(const std::string& token)
102 {
103     WVLOG_I("SetInterfaceToken invoke");
104     token_ = token;
105 }
106 
OnAbilityDisconnectDone(const AppExecFwk::ElementName & element,int resultCode)107 void MigrationManagerAdapterImpl::MigrationListenerAdapterImpl::OnAbilityDisconnectDone(
108     const AppExecFwk::ElementName& element, int resultCode)
109 {
110     WVLOG_I("onAbilityDisconnectDone, resultCode is %{public}d", resultCode);
111     std::vector<int32_t> vec(0);
112     OnMigrationReply(0, 0, vec, vec);
113     isConnectSystemUI_.store(false);
114 }
115 
SetMigrationParam(const std::string & bundleName,const std::string & abilityName,const std::string & token)116 void MigrationManagerAdapterImpl::SetMigrationParam(
117     const std::string& bundleName, const std::string& abilityName, const std::string& token)
118 {
119     WVLOG_I("setMigrationParam start.");
120     bundleName_ = bundleName;
121     abilityName_ = abilityName;
122     if (callback_) {
123         callback_->SetInterfaceToken(token);
124     }
125 }
126 
SendMigrationRequest(std::shared_ptr<std::string> jsonData)127 bool MigrationManagerAdapterImpl::SendMigrationRequest(std::shared_ptr<std::string> jsonData)
128 {
129     WVLOG_I("send migration reqeust start.");
130     if (callback_) {
131         callback_->SetJsonData(*jsonData);
132     }
133     if (isConnectSystemUI_.load() && callback_ != nullptr) {
134         AppExecFwk::ElementName element;
135         callback_->OnAbilityConnectDone(element, remoteObject_, AppExecFwk::Constants::INVALID_USERID);
136         WVLOG_I("connectExtension has been invoked once.");
137         return true;
138     }
139 
140     AAFwk::Want want;
141     want.SetElementName(bundleName_, abilityName_);
142     auto abilityManager = AAFwk::AbilityManagerClient::GetInstance();
143     if (abilityManager == nullptr) {
144         WVLOG_W("ability Manager Client is nullptr");
145         return false;
146     }
147     auto ret = abilityManager->ConnectAbility(want, callback_, AppExecFwk::Constants::INVALID_USERID);
148     if (ret != ERR_OK) {
149         WVLOG_E("connectExtensionAbility failed.");
150         return false;
151     }
152     WVLOG_I("connectExtensionAbility end.");
153     return true;
154 }
155 
RegisterMigrationListener(std::shared_ptr<MigrationListenerAdapter> listener)156 uint32_t MigrationManagerAdapterImpl::RegisterMigrationListener(std::shared_ptr<MigrationListenerAdapter> listener)
157 {
158     if (!listener) {
159         WVLOG_E("registerMigrationListener make failed");
160     }
161     WVLOG_I("connectExtensionAbility RegisterMigrationListener.");
162     callback_ = new (std::nothrow) MigrationListenerAdapterImpl(listener);
163     if (!callback_) {
164         WVLOG_E("make_shared failed");
165         return -1;
166     }
167     return 0;
168 }
169 
170 } // namespace OHOS::NWeb
171