1  /*
2   * Copyright (c) 2021-2024 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  #include "app_spawn_socket.h"
17  
18  #include "hilog_tag_wrapper.h"
19  
20  namespace OHOS {
21  namespace AppExecFwk {
22  // arg "AppSpawn" or "NWebSpawn" cannot be defined as string object since REGISTER_SYSTEM_ABILITY will
23  // firstly start without init this string object, which leads to error.
24  
AppSpawnSocket(bool isNWebSpawn)25  AppSpawnSocket::AppSpawnSocket(bool isNWebSpawn)
26  {
27      clientSocket_ = isNWebSpawn ?
28          std::make_unique<AppSpawn::ClientSocket>("/dev/unix/socket/NWebSpawn") :
29          std::make_unique<AppSpawn::ClientSocket>("AppSpawn");
30  }
31  
~AppSpawnSocket()32  AppSpawnSocket::~AppSpawnSocket()
33  {}
34  
OpenAppSpawnConnection()35  ErrCode AppSpawnSocket::OpenAppSpawnConnection()
36  {
37      TAG_LOGD(AAFwkTag::APPMGR, "ready to open connection");
38      if (clientSocket_) {
39          if (clientSocket_->CreateClient() != ERR_OK) {
40              TAG_LOGE(AAFwkTag::APPMGR, "failed to create socketClient");
41              return ERR_APPEXECFWK_BAD_APPSPAWN_CLIENT;
42          }
43          if (clientSocket_->ConnectSocket() != ERR_OK) {
44              TAG_LOGE(AAFwkTag::APPMGR, "failed to connect socket");
45              clientSocket_->CloseClient();
46              return ERR_APPEXECFWK_CONNECT_APPSPAWN_FAILED;
47          }
48          TAG_LOGD(AAFwkTag::APPMGR, "connection has been opened");
49          return ERR_OK;
50      }
51      TAG_LOGE(AAFwkTag::APPMGR, "failed to open connection without socket");
52      return ERR_APPEXECFWK_BAD_APPSPAWN_SOCKET;
53  }
54  
CloseAppSpawnConnection()55  void AppSpawnSocket::CloseAppSpawnConnection()
56  {
57      if (clientSocket_) {
58          clientSocket_->CloseClient();
59      }
60  }
61  
WriteMessage(const void * buf,const int32_t len)62  ErrCode AppSpawnSocket::WriteMessage(const void *buf, const int32_t len)
63  {
64      TAG_LOGD(AAFwkTag::APPMGR, "ready to write message");
65      if (len <= 0) {
66          TAG_LOGE(AAFwkTag::APPMGR, "failed to write message due to invalid length of message");
67          return ERR_INVALID_VALUE;
68      }
69      if (buf == nullptr) {
70          TAG_LOGE(AAFwkTag::APPMGR, "failed to write message due to null buf");
71          return ERR_INVALID_VALUE;
72      }
73      if (clientSocket_) {
74          if (clientSocket_->WriteSocketMessage(buf, len) != len) {
75              TAG_LOGE(AAFwkTag::APPMGR, "failed to write message due to invalid write length");
76              return ERR_APPEXECFWK_SOCKET_WRITE_FAILED;
77          }
78          TAG_LOGD(AAFwkTag::APPMGR, "write message success");
79          return ERR_OK;
80      }
81  
82      TAG_LOGE(AAFwkTag::APPMGR, "failed to write message without socket");
83      return ERR_APPEXECFWK_BAD_APPSPAWN_SOCKET;
84  }
85  
ReadMessage(void * buf,const int32_t len)86  ErrCode AppSpawnSocket::ReadMessage(void *buf, const int32_t len)
87  {
88      TAG_LOGD(AAFwkTag::APPMGR, "ready to read message");
89      if (len <= 0) {
90          TAG_LOGE(AAFwkTag::APPMGR, "failed to read message due to invalid length of cache");
91          return ERR_INVALID_VALUE;
92      }
93      if (buf == nullptr) {
94          TAG_LOGE(AAFwkTag::APPMGR, "failed to read message due to null buf");
95          return ERR_INVALID_VALUE;
96      }
97      if (clientSocket_) {
98          if (clientSocket_->ReadSocketMessage(buf, len) != len) {
99              TAG_LOGE(AAFwkTag::APPMGR, "failed to read message due to invalid read length");
100              return ERR_APPEXECFWK_SOCKET_READ_FAILED;
101          }
102          TAG_LOGD(AAFwkTag::APPMGR, "read message success");
103          return ERR_OK;
104      }
105      TAG_LOGE(AAFwkTag::APPMGR, "failed to read message without socket");
106      return ERR_APPEXECFWK_BAD_APPSPAWN_CLIENT;
107  }
108  
SetClientSocket(const std::shared_ptr<OHOS::AppSpawn::ClientSocket> clientSocket)109  void AppSpawnSocket::SetClientSocket(const std::shared_ptr<OHOS::AppSpawn::ClientSocket> clientSocket)
110  {
111      clientSocket_ = clientSocket;
112  }
113  }  // namespace AppExecFwk
114  }  // namespace OHOS
115