1eace7efcSopenharmony_ci/* 2eace7efcSopenharmony_ci * Copyright (c) 2021-2024 Huawei Device Co., Ltd. 3eace7efcSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4eace7efcSopenharmony_ci * you may not use this file except in compliance with the License. 5eace7efcSopenharmony_ci * You may obtain a copy of the License at 6eace7efcSopenharmony_ci * 7eace7efcSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8eace7efcSopenharmony_ci * 9eace7efcSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10eace7efcSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11eace7efcSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12eace7efcSopenharmony_ci * See the License for the specific language governing permissions and 13eace7efcSopenharmony_ci * limitations under the License. 14eace7efcSopenharmony_ci */ 15eace7efcSopenharmony_ci 16eace7efcSopenharmony_ci#include "app_spawn_socket.h" 17eace7efcSopenharmony_ci 18eace7efcSopenharmony_ci#include "hilog_tag_wrapper.h" 19eace7efcSopenharmony_ci 20eace7efcSopenharmony_cinamespace OHOS { 21eace7efcSopenharmony_cinamespace AppExecFwk { 22eace7efcSopenharmony_ci// arg "AppSpawn" or "NWebSpawn" cannot be defined as string object since REGISTER_SYSTEM_ABILITY will 23eace7efcSopenharmony_ci// firstly start without init this string object, which leads to error. 24eace7efcSopenharmony_ci 25eace7efcSopenharmony_ciAppSpawnSocket::AppSpawnSocket(bool isNWebSpawn) 26eace7efcSopenharmony_ci{ 27eace7efcSopenharmony_ci clientSocket_ = isNWebSpawn ? 28eace7efcSopenharmony_ci std::make_unique<AppSpawn::ClientSocket>("/dev/unix/socket/NWebSpawn") : 29eace7efcSopenharmony_ci std::make_unique<AppSpawn::ClientSocket>("AppSpawn"); 30eace7efcSopenharmony_ci} 31eace7efcSopenharmony_ci 32eace7efcSopenharmony_ciAppSpawnSocket::~AppSpawnSocket() 33eace7efcSopenharmony_ci{} 34eace7efcSopenharmony_ci 35eace7efcSopenharmony_ciErrCode AppSpawnSocket::OpenAppSpawnConnection() 36eace7efcSopenharmony_ci{ 37eace7efcSopenharmony_ci TAG_LOGD(AAFwkTag::APPMGR, "ready to open connection"); 38eace7efcSopenharmony_ci if (clientSocket_) { 39eace7efcSopenharmony_ci if (clientSocket_->CreateClient() != ERR_OK) { 40eace7efcSopenharmony_ci TAG_LOGE(AAFwkTag::APPMGR, "create socketClient fail"); 41eace7efcSopenharmony_ci return ERR_APPEXECFWK_BAD_APPSPAWN_CLIENT; 42eace7efcSopenharmony_ci } 43eace7efcSopenharmony_ci if (clientSocket_->ConnectSocket() != ERR_OK) { 44eace7efcSopenharmony_ci TAG_LOGE(AAFwkTag::APPMGR, "connect socket fail"); 45eace7efcSopenharmony_ci clientSocket_->CloseClient(); 46eace7efcSopenharmony_ci return ERR_APPEXECFWK_CONNECT_APPSPAWN_FAILED; 47eace7efcSopenharmony_ci } 48eace7efcSopenharmony_ci TAG_LOGD(AAFwkTag::APPMGR, "connection has been opened"); 49eace7efcSopenharmony_ci return ERR_OK; 50eace7efcSopenharmony_ci } 51eace7efcSopenharmony_ci TAG_LOGE(AAFwkTag::APPMGR, "open connection fail"); 52eace7efcSopenharmony_ci return ERR_APPEXECFWK_BAD_APPSPAWN_SOCKET; 53eace7efcSopenharmony_ci} 54eace7efcSopenharmony_ci 55eace7efcSopenharmony_civoid AppSpawnSocket::CloseAppSpawnConnection() 56eace7efcSopenharmony_ci{ 57eace7efcSopenharmony_ci if (clientSocket_) { 58eace7efcSopenharmony_ci clientSocket_->CloseClient(); 59eace7efcSopenharmony_ci } 60eace7efcSopenharmony_ci} 61eace7efcSopenharmony_ci 62eace7efcSopenharmony_ciErrCode AppSpawnSocket::WriteMessage(const void *buf, const int32_t len) 63eace7efcSopenharmony_ci{ 64eace7efcSopenharmony_ci TAG_LOGD(AAFwkTag::APPMGR, "ready to write message"); 65eace7efcSopenharmony_ci if (len <= 0) { 66eace7efcSopenharmony_ci TAG_LOGE(AAFwkTag::APPMGR, "write message fail"); 67eace7efcSopenharmony_ci return ERR_INVALID_VALUE; 68eace7efcSopenharmony_ci } 69eace7efcSopenharmony_ci if (buf == nullptr) { 70eace7efcSopenharmony_ci TAG_LOGE(AAFwkTag::APPMGR, "write message fail"); 71eace7efcSopenharmony_ci return ERR_INVALID_VALUE; 72eace7efcSopenharmony_ci } 73eace7efcSopenharmony_ci if (clientSocket_) { 74eace7efcSopenharmony_ci if (clientSocket_->WriteSocketMessage(buf, len) != len) { 75eace7efcSopenharmony_ci TAG_LOGE(AAFwkTag::APPMGR, "write message fail"); 76eace7efcSopenharmony_ci return ERR_APPEXECFWK_SOCKET_WRITE_FAILED; 77eace7efcSopenharmony_ci } 78eace7efcSopenharmony_ci TAG_LOGD(AAFwkTag::APPMGR, "write message success"); 79eace7efcSopenharmony_ci return ERR_OK; 80eace7efcSopenharmony_ci } 81eace7efcSopenharmony_ci 82eace7efcSopenharmony_ci TAG_LOGE(AAFwkTag::APPMGR, "write message fail"); 83eace7efcSopenharmony_ci return ERR_APPEXECFWK_BAD_APPSPAWN_SOCKET; 84eace7efcSopenharmony_ci} 85eace7efcSopenharmony_ci 86eace7efcSopenharmony_ciErrCode AppSpawnSocket::ReadMessage(void *buf, const int32_t len) 87eace7efcSopenharmony_ci{ 88eace7efcSopenharmony_ci TAG_LOGD(AAFwkTag::APPMGR, "ready to read message"); 89eace7efcSopenharmony_ci if (len <= 0) { 90eace7efcSopenharmony_ci TAG_LOGE(AAFwkTag::APPMGR, "read message fail"); 91eace7efcSopenharmony_ci return ERR_INVALID_VALUE; 92eace7efcSopenharmony_ci } 93eace7efcSopenharmony_ci if (buf == nullptr) { 94eace7efcSopenharmony_ci TAG_LOGE(AAFwkTag::APPMGR, "read message fail"); 95eace7efcSopenharmony_ci return ERR_INVALID_VALUE; 96eace7efcSopenharmony_ci } 97eace7efcSopenharmony_ci if (clientSocket_) { 98eace7efcSopenharmony_ci if (clientSocket_->ReadSocketMessage(buf, len) != len) { 99eace7efcSopenharmony_ci TAG_LOGE(AAFwkTag::APPMGR, "read message fail"); 100eace7efcSopenharmony_ci return ERR_APPEXECFWK_SOCKET_READ_FAILED; 101eace7efcSopenharmony_ci } 102eace7efcSopenharmony_ci TAG_LOGD(AAFwkTag::APPMGR, "read message success"); 103eace7efcSopenharmony_ci return ERR_OK; 104eace7efcSopenharmony_ci } 105eace7efcSopenharmony_ci TAG_LOGE(AAFwkTag::APPMGR, "read message fail"); 106eace7efcSopenharmony_ci return ERR_APPEXECFWK_BAD_APPSPAWN_CLIENT; 107eace7efcSopenharmony_ci} 108eace7efcSopenharmony_ci 109eace7efcSopenharmony_civoid AppSpawnSocket::SetClientSocket(const std::shared_ptr<OHOS::AppSpawn::ClientSocket> clientSocket) 110eace7efcSopenharmony_ci{ 111eace7efcSopenharmony_ci clientSocket_ = clientSocket; 112eace7efcSopenharmony_ci} 113eace7efcSopenharmony_ci} // namespace AppExecFwk 114eace7efcSopenharmony_ci} // namespace OHOS 115