1 /*
2  * Copyright (c) 2020 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 OHOS_BUNLDE_DAEMON_CLIENT_H
17 #define OHOS_BUNLDE_DAEMON_CLIENT_H
18 
19 #include <semaphore.h>
20 
21 #include "ohos_errno.h"
22 #include "iproxy_client.h"
23 #include "ipc_skeleton.h"
24 #include "mutex_lock.h"
25 #include "nocopyable.h"
26 
27 namespace OHOS {
28 class BundleDaemonClient : public NoCopyable {
29 public:
GetInstance()30     static BundleDaemonClient &GetInstance()
31     {
32         static BundleDaemonClient instance;
33         return instance;
34     }
35     bool Initialize();
36     int32_t ExtractHap(const char *hapFile, const char *codePath);
37     int32_t RenameFile(const char *oldFile, const char *newFile);
38     int32_t CreatePermissionDir();
39     int32_t CreateDataDirectory(const char *dataPath, int32_t uid, int32_t gid, bool isChown);
40     int32_t StoreContentToFile(const char *file, const void *buffer, uint32_t size);
41     int32_t MoveFile(const char *oldFile, const char *newFile);
42     int32_t RemoveFile(const char *file);
43     int32_t RemoveInstallDirectory(const char *codePath, const char *dataPath, bool keepData);
44     int32_t CallClientInvoke(int32_t funcId, const char *firstPath, const char *secondPath, bool keepData = false);
45     static int32_t BundleDaemonCallback(uint32_t code, IpcIo* data, IpcIo* reply, MessageOption option);
46     static void DeathCallback(void* arg);
47     static int Notify(IOwner owner, int code, IpcIo *reply);
48 
49 private:
50     BundleDaemonClient() = default;
51     ~BundleDaemonClient();
52 
53     IClientProxy *bdsClient_ { nullptr };
54     SvcIdentity svcIdentity_ {};
55     SvcIdentity bdsSvcIdentity_ {};
56     IpcObjectStub objectStub_ {};
57     uint32_t cbid_ = INVALID_INDEX;
58     sem_t sem_;
59     Mutex mutex_;
60     int32_t result_ = EC_FAILURE;
61     bool initialized_ = false;
62 
63     static void *RegisterDeathCallback(void *);
64     int32_t RegisterCallback();
65     int32_t WaitResultSync(int32_t result);
66 };
67 } // namespace OHOS
68 #endif // OHOS_BUNLDE_DAEMON_CLIENT_H
69