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 #ifndef UPDATE_NOTIFY_H
17 #define UPDATE_NOTIFY_H
18 
19 #include <condition_variable>
20 #include <mutex>
21 #include <string>
22 #include "ability_connect_callback_stub.h"
23 #include "ability_connect_callback_interface.h"
24 #include "ability_manager_interface.h"
25 #include "ability_manager_client.h"
26 #include "if_system_ability_manager.h"
27 #include "i_update_notify.h"
28 #include "system_ability_definition.h"
29 #include "update_no_constructor.h"
30 #include "want.h"
31 
32 namespace OHOS {
33 namespace UpdateEngine {
34 class UpdateNotify : public IRemoteStub<IUpdateNotify> {
35 public:
36     DISALLOW_COPY_AND_MOVE(UpdateNotify);
37 
38     UpdateNotify();
39     ~UpdateNotify();
40     static sptr<UpdateNotify> GetInstance();
41     bool ConnectToAppService(const std::string &eventInfo, const std::string &subscribeInfo);
42     void HandleAbilityConnect(const sptr<IRemoteObject> &remoteObject);
43 
44 private:
45     bool HandleMessage(const std::string &message);
46     ErrCode ConnectAbility(const AAFwk::Want &want, const sptr<AAFwk::AbilityConnectionStub> &connect);
47     ErrCode DisconnectAbility(const sptr<AAFwk::AbilityConnectionStub> &connect);
48 
49 private:
50     static std::mutex instanceLock_;
51     static sptr<UpdateNotify> instance_;
52     sptr<IRemoteObject> remoteObject_ = nullptr;
53     std::mutex connectMutex_;
54     std::condition_variable  conditionVal_;
55 
56     enum class UpdateAppCode {
57         UNKNOWN = 0,
58         UPDATE_APP = 5
59     };
60 };
61 
62 class NotifyConnection : public AAFwk::AbilityConnectionStub {
63 public:
64     explicit NotifyConnection(const sptr<UpdateNotify> &instance);
65     ~NotifyConnection() = default;
66     void OnAbilityConnectDone(const AppExecFwk::ElementName &element, const sptr<IRemoteObject> &remoteObject,
67         int32_t resultCode) override;
68     void OnAbilityDisconnectDone(const AppExecFwk::ElementName &element, int resultCode) override;
69 
70 private:
71     sptr<UpdateNotify> instance_ = nullptr;
72 };
73 } // namespace UpdateEngine
74 } // namespace OHOS
75 #endif // UPDATE_NOTIFY_H