• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 FOUNDATION_APPEXECFWK_SERVICES_APPMGR_INCLUDE_APP_SPAWN_CLIENT_H
17 #define FOUNDATION_APPEXECFWK_SERVICES_APPMGR_INCLUDE_APP_SPAWN_CLIENT_H
18 
19 #include "nocopyable.h"
20 #include "app_spawn_msg_wrapper.h"
21 #include "app_spawn_socket.h"
22 
23 namespace OHOS {
24 namespace AppExecFwk {
25 enum class SpawnConnectionState { STATE_NOT_CONNECT, STATE_CONNECTED, STATE_CONNECT_FAILED };
26 
27 class AppSpawnClient {
28 public:
29     /**
30      * Constructor.
31      */
32     AppSpawnClient();
33 
34     /**
35      * Destructor
36      */
37     virtual ~AppSpawnClient() = default;
38 
39     /**
40      * Disable copy.
41      */
42     DISALLOW_COPY_AND_MOVE(AppSpawnClient);
43 
44     /**
45      * Try connect to appspawn.
46      */
47     ErrCode OpenConnection();
48 
49     /**
50      * Close the connect of appspawn.
51      */
52     void CloseConnection();
53 
54     /**
55      * AppSpawnClient core function, Start request to appspawn.
56      *
57      * @param startMsg, request message.
58      * @param pid, pid of app process, get from appspawn.
59      */
60     virtual ErrCode StartProcess(const AppSpawnStartMsg &startMsg, pid_t &pid);
61 
62     /**
63      * Return the connect state.
64      */
65     SpawnConnectionState QueryConnectionState() const;
66 
67     /**
68      * Set function, unit test also use it.
69      */
70     void SetSocket(const std::shared_ptr<AppSpawnSocket> socket);
71 
72 private:
73     /**
74      * AppSpawnClient core function,
75      *
76      * @param startMsg, request message.
77      * @param pid, pid of app process, get it from appspawn.
78      */
79     ErrCode StartProcessImpl(const AppSpawnStartMsg &startMsg, pid_t &pid);
80 
81 private:
82     std::shared_ptr<AppSpawnSocket> socket_;
83     SpawnConnectionState state_ = SpawnConnectionState::STATE_NOT_CONNECT;
84 };
85 }  // namespace AppExecFwk
86 }  // namespace OHOS
87 #endif  // FOUNDATION_APPEXECFWK_SERVICES_APPMGR_INCLUDE_APP_SPAWN_CLIENT_H
88