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 APPSPAWN_SOCKET_CLIENT_H 17 #define APPSPAWN_SOCKET_CLIENT_H 18 19 #include "appspawn_socket.h" 20 #include "nocopyable.h" 21 22 namespace OHOS { 23 namespace AppSpawn { 24 class ClientSocket : public AppSpawnSocket { 25 public: 26 /** 27 * Constructor used to create a ClientSocket 28 */ 29 explicit ClientSocket(const std::string &client); 30 31 /** 32 * Destructor used to destory a ClientSocket 33 */ 34 virtual ~ClientSocket() = default; 35 36 /** 37 * Disables copying and moving for the ClientSocket. 38 */ 39 DISALLOW_COPY_AND_MOVE(ClientSocket); 40 41 /** 42 * Creates a local client socket. 43 */ 44 virtual int CreateClient(); 45 46 /** 47 * Closes a client socket. 48 */ 49 virtual void CloseClient(); 50 51 /** 52 * Connects a client socket. 53 */ 54 virtual int ConnectSocket(); 55 56 /** 57 * Writes messages to a client socket. 58 * 59 * @param buf Indicates the pointer to the message buffer. 60 * @param len Indicates the message length. 61 */ 62 virtual int WriteSocketMessage(const void *buf, int len); 63 64 /** 65 * Reads messages from a client socket. 66 * 67 * @param buf Indicates the pointer to the message buffer. 68 * @param len Indicates the message length. 69 */ 70 virtual int ReadSocketMessage(void *buf, int len); 71 72 /** 73 * Uses functions of the parent class. 74 */ 75 using AppSpawnSocket::CloseSocket; 76 using AppSpawnSocket::CreateSocket; 77 using AppSpawnSocket::GetSocketFd; 78 using AppSpawnSocket::ReadSocketMessage; 79 using AppSpawnSocket::WriteSocketMessage; 80 81 enum AppType { 82 APP_TYPE_DEFAULT = 0, // JavaScript app 83 APP_TYPE_NATIVE // Native C++ app 84 }; 85 86 static constexpr int APPSPAWN_MSG_MAX_SIZE = 4096; // appspawn message max size 87 static constexpr int LEN_PROC_NAME = 256; // process name length 88 static constexpr int LEN_BUNDLE_NAME = 256; // bundle name length 89 static constexpr int LEN_SO_PATH = 256; // load so lib 90 static constexpr int MAX_GIDS = 64; 91 static constexpr int APL_MAX_LEN = 32; 92 static constexpr int RENDER_CMD_MAX_LEN = 1024; 93 static constexpr int APPSPAWN_COLD_BOOT = 0x01; 94 95 enum AppOperateCode { 96 DEFAULT = 0, 97 GET_RENDER_TERMINATION_STATUS, 98 }; 99 100 struct AppProperty { 101 uint32_t uid; // the UNIX uid that the child process setuid() to after fork() 102 uint32_t gid; // the UNIX gid that the child process setgid() to after fork() 103 uint32_t gidTable[MAX_GIDS]; // a list of UNIX gids that the child process setgroups() to after fork() 104 uint32_t gidCount; // the size of gidTable 105 char processName[LEN_PROC_NAME]; // process name 106 char bundleName[LEN_BUNDLE_NAME]; // bundle name 107 char soPath[LEN_SO_PATH]; // so lib path 108 uint32_t accessTokenId; 109 char apl[APL_MAX_LEN]; 110 char renderCmd[RENDER_CMD_MAX_LEN]; 111 uint32_t flags; 112 int32_t pid; // query render process exited status by render process pid 113 AppOperateCode code; 114 }; 115 116 private: 117 /** 118 * Connects a client socket. 119 * 120 * @param connectFd Indicates the connection's FD. 121 */ 122 int ConnectSocket(int connectFd); 123 }; 124 } // namespace AppSpawn 125 } // namespace OHOS 126 #endif 127