• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 OHOS_ABILITY_RUNTIME_APP_SPAWN_CLIENT_H
17 #define OHOS_ABILITY_RUNTIME_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     explicit AppSpawnClient(bool isNWebSpawn = false);
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      * Start request to nwebspawn process.
56      *
57      */
58     virtual ErrCode PreStartNWebSpawnProcess();
59 
60     /**
61      * AppSpawnClient core function, Start request to appSpawn.
62      *
63      * @param startMsg, request message.
64      * @param pid, pid of app process, get from appSpawn.
65      */
66     virtual ErrCode StartProcess(const AppSpawnStartMsg &startMsg, pid_t &pid);
67 
68     /**
69      * Get render process termination status.
70      *
71      * @param startMsg, request message.
72      * @param status, termination status of render process, get from appSpawn.
73      */
74     virtual ErrCode GetRenderProcessTerminationStatus(const AppSpawnStartMsg &startMsg, int &status);
75 
76     /**
77      * Return the connect state.
78      */
79     SpawnConnectionState QueryConnectionState() const;
80 
81     /**
82      * Set function, unit test also use it.
83      */
84     void SetSocket(const std::shared_ptr<AppSpawnSocket> socket);
85 
86 private:
87     /**
88      * AppSpawnClient core function,
89      *
90      * @param startMsg, request message.
91      * @param pid, pid of app process, get it from appSpawn.
92      */
93     ErrCode StartProcessImpl(const AppSpawnStartMsg &startMsg, pid_t &pid);
94 
95     /**
96      * Start request to nwebspawn process.
97      *
98      */
99     ErrCode PreStartNWebSpawnProcessImpl();
100 
101     /**
102      * write string info to appSpawn.
103      *
104      * @param strInfo, request message wrapper.
105      */
106     ErrCode WriteStrInfoMessage(const std::string &strInfo);
107 
108     /**
109      * @brief write message to appSpawn.
110      *
111      * @param startMsg, request message.
112      */
113     ErrCode StartProcessForWriteMsg(const AppSpawnMsgWrapper &msgWrapper);
114 
115 private:
116     std::shared_ptr<AppSpawnSocket> socket_;
117     SpawnConnectionState state_ = SpawnConnectionState::STATE_NOT_CONNECT;
118 };
119 }  // namespace AppExecFwk
120 }  // namespace OHOS
121 #endif  // OHOS_ABILITY_RUNTIME_APP_SPAWN_CLIENT_H
122