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 "js_free_install_observer.h"
17
18 #include "hilog_wrapper.h"
19 #include "hitrace_meter.h"
20 #include "js_error_utils.h"
21 #include "js_runtime.h"
22 #include "js_runtime_utils.h"
23 #include "napi/native_api.h"
24
25 namespace OHOS {
26 namespace AbilityRuntime {
27 constexpr size_t ARGC_ONE = 1;
28
JsFreeInstallObserver(NativeEngine & engine)29 JsFreeInstallObserver::JsFreeInstallObserver(NativeEngine& engine) : engine_(engine) {}
30
31 JsFreeInstallObserver::~JsFreeInstallObserver() = default;
32
OnInstallFinished(const std::string & bundleName,const std::string & abilityName,const std::string & startTime,const int & resultCode)33 void JsFreeInstallObserver::OnInstallFinished(const std::string &bundleName, const std::string &abilityName,
34 const std::string &startTime, const int &resultCode)
35 {
36 HILOG_DEBUG("OnInstallFinished come.");
37 wptr<JsFreeInstallObserver> jsObserver = this;
38 std::unique_ptr<AsyncTask::CompleteCallback> complete = std::make_unique<AsyncTask::CompleteCallback>
39 ([jsObserver, bundleName, abilityName, startTime, resultCode](NativeEngine &engine, AsyncTask &task,
40 int32_t status) {
41 sptr<JsFreeInstallObserver> jsObserverSptr = jsObserver.promote();
42 if (jsObserverSptr) {
43 jsObserverSptr->HandleOnInstallFinished(bundleName, abilityName, startTime, resultCode);
44 }
45 });
46 NativeReference* callback = nullptr;
47 std::unique_ptr<AsyncTask::ExecuteCallback> execute = nullptr;
48 AsyncTask::Schedule("JsFreeInstallObserver::OnInstallFinished", engine_, std::make_unique<AsyncTask>(callback,
49 std::move(execute), std::move(complete)));
50 }
51
HandleOnInstallFinished(const std::string & bundleName,const std::string & abilityName,const std::string & startTime,const int & resultCode)52 void JsFreeInstallObserver::HandleOnInstallFinished(const std::string &bundleName, const std::string &abilityName,
53 const std::string &startTime, const int &resultCode)
54 {
55 HILOG_DEBUG("HandleOnInstallFinished begin.");
56 for (auto it = jsObserverObjectList_.begin(); it != jsObserverObjectList_.end();) {
57 if ((it->bundleName == bundleName) && (it->abilityName == abilityName) && (it->startTime == startTime)) {
58 if (it->callback == nullptr) {
59 continue;
60 }
61 if (it->isAbilityResult && resultCode == ERR_OK) {
62 it = jsObserverObjectList_.erase(it);
63 continue;
64 }
65 FinishAsyncTrace(HITRACE_TAG_ABILITY_MANAGER, "StartFreeInstall", atoi(startTime.c_str()));
66 NativeValue* value = (it->callback)->Get();
67 NativeValue* argv[] = { CreateJsErrorByNativeErr(engine_, resultCode) };
68 CallJsFunction(value, argv, ARGC_ONE);
69 it = jsObserverObjectList_.erase(it);
70 HILOG_DEBUG("the size of jsObserverObjectList_:%{public}zu", jsObserverObjectList_.size());
71 } else {
72 it++;
73 }
74 }
75 }
76
CallJsFunction(NativeValue * value,NativeValue * const * argv,size_t argc)77 void JsFreeInstallObserver::CallJsFunction(NativeValue* value, NativeValue* const* argv, size_t argc)
78 {
79 HILOG_INFO("CallJsFunction begin");
80 if (value == nullptr) {
81 HILOG_ERROR("value is nullptr.");
82 return;
83 }
84 engine_.CallFunction(value, value, argv, argc);
85 }
86
AddJsObserverObject(const std::string & bundleName,const std::string & abilityName,const std::string & startTime,NativeValue * jsObserverObject,bool isAbilityResult)87 void JsFreeInstallObserver::AddJsObserverObject(const std::string &bundleName, const std::string &abilityName,
88 const std::string &startTime, NativeValue* jsObserverObject, bool isAbilityResult)
89 {
90 HILOG_INFO("AddJsObserverObject begin.");
91 if (jsObserverObject == nullptr) {
92 HILOG_ERROR("jsObserverObject is nullptr.");
93 return;
94 }
95
96 for (auto it = jsObserverObjectList_.begin(); it != jsObserverObjectList_.end(); ++it) {
97 if (it->bundleName == bundleName && it->abilityName == abilityName && it->startTime == startTime) {
98 HILOG_WARN("The jsObject has been added.");
99 return;
100 }
101 }
102
103 StartAsyncTrace(HITRACE_TAG_ABILITY_MANAGER, "StartFreeInstall", atoi(startTime.c_str()));
104 JsFreeInstallObserverObject object;
105 object.bundleName = bundleName;
106 object.abilityName = abilityName;
107 object.startTime = startTime;
108 object.callback = std::shared_ptr<NativeReference>(engine_.CreateReference(jsObserverObject, 1));
109 object.isAbilityResult = isAbilityResult;
110 jsObserverObjectList_.emplace_back(object);
111 }
112 } // namespace AbilityRuntime
113 } // namespace OHOS