• 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_MSG_WRAPPER_H
17 #define FOUNDATION_APPEXECFWK_SERVICES_APPMGR_INCLUDE_APP_SPAWN_MSG_WRAPPER_H
18 
19 #include <string>
20 #include <vector>
21 #include <unistd.h>
22 
23 #include "nocopyable.h"
24 #include "client_socket.h"
25 
26 namespace OHOS {
27 namespace AppExecFwk {
28 struct AppSpawnStartMsg {
29     int32_t uid;
30     int32_t gid;
31     std::vector<int32_t> gids;
32     std::string procName;
33     std::string soPath;
34     uint32_t accessTokenId;
35     std::string apl;
36     std::string bundleName;
37     std::string renderParam; // only nweb spawn need this param.
38     int32_t pid;
39     int32_t code = 0; // 0: DEFAULT; 1: GET_RENDER_TERMINATION_STATUS
40     bool coldStart; // only nweb spawn need this param.
41 };
42 
43 using AppSpawnMsg = AppSpawn::ClientSocket::AppProperty;
44 
45 constexpr auto LEN_PID = sizeof(pid_t);
46 
47 union AppSpawnPidMsg {
48     pid_t pid = 0;
49     char pidBuf[LEN_PID];
50 };
51 
52 class AppSpawnMsgWrapper {
53 public:
54     /**
55      * Constructor.
56      */
57     AppSpawnMsgWrapper() = default;
58 
59     /**
60      * Destructor
61      */
62     ~AppSpawnMsgWrapper();
63 
64     /**
65      * Disable copy.
66      */
67     DISALLOW_COPY_AND_MOVE(AppSpawnMsgWrapper);
68 
69     /**
70      * Verify message and assign to member variable.
71      *
72      * @param startMsg, request message.
73      */
74     bool AssembleMsg(const AppSpawnStartMsg &startMsg);
75 
76     /**
77      * Get function, return isValid_.
78      */
IsValid()79     bool IsValid() const
80     {
81         return isValid_;
82     }
83 
84     /**
85      * Get function, return member variable message.
86      */
GetMsgBuf()87     const void *GetMsgBuf() const
88     {
89         return reinterpret_cast<void *>(msg_);
90     }
91 
92     /**
93      * Get function, return message length.
94      */
GetMsgLength()95     int32_t GetMsgLength() const
96     {
97         return isValid_ ? sizeof(AppSpawnMsg) : 0;
98     }
99 
100 private:
101     /**
102      * Verify message.
103      *
104      * @param startMsg, request message.
105      */
106     bool VerifyMsg(const AppSpawnStartMsg &startMsg) const;
107 
108     /**
109      * Print message.
110      *
111      * @param startMsg, request message.
112      */
113     void DumpMsg() const;
114 
115     /**
116      * Release message.
117      */
118     void FreeMsg();
119 
120 private:
121     bool isValid_ = false;
122     // because AppSpawnMsg's size is uncertain, so should use raw pointer.
123     AppSpawnMsg *msg_ = nullptr;
124 };
125 }  // namespace AppExecFwk
126 }  // namespace OHOS
127 #endif  // FOUNDATION_APPEXECFWK_SERVICES_APPMGR_INCLUDE_APP_SPAWN_MSG_WRAPPER_H
128