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 #include "message.h" 17 18 #include "global.h" 19 20 namespace OHOS { 21 namespace MiscServices { 22 /*! Constructor 23 * @param msgId a message Id 24 * @param msgContent the content of a message 25 */ Message(int32_t msgId,MessageParcel * msgContent)26Message::Message(int32_t msgId, MessageParcel *msgContent) 27 { 28 msgId_ = msgId; 29 msgContent_ = msgContent; 30 if (msgContent_ != nullptr) { 31 msgContent_->RewindRead(0); 32 } 33 } 34 // LCOV_EXCL_START 35 /*!Constructor 36 * @param msg a source message 37 */ Message(const Message & msg)38Message::Message(const Message &msg) 39 { 40 msgId_ = msg.msgId_; 41 if (msgContent_ != nullptr) { 42 delete msgContent_; 43 msgContent_ = nullptr; 44 } 45 MessageParcel *src = msg.msgContent_; 46 if (src != nullptr) { 47 msgContent_ = new (std::nothrow) MessageParcel(); 48 if (msgContent_ == nullptr) { 49 IMSA_HILOGE("new MessageParcel failed"); 50 return; 51 } 52 msgContent_->ParseFrom(src->GetData(), src->GetDataSize()); 53 } 54 } 55 operator =(const Message & msg)56Message &Message::operator=(const Message &msg) 57 { 58 if (this == &msg) { 59 return *this; 60 } 61 msgId_ = msg.msgId_; 62 if (msgContent_ != nullptr) { 63 delete msgContent_; 64 msgContent_ = nullptr; 65 } 66 if (msg.msgContent_ != nullptr) { 67 msgContent_ = new (std::nothrow) MessageParcel(); 68 if (msgContent_ == nullptr) { 69 IMSA_HILOGE("new MessageParcel failed"); 70 return *this; 71 } 72 msgContent_->ParseFrom(msg.msgContent_->GetData(), msg.msgContent_->GetDataSize()); 73 msgContent_->RewindRead(0); 74 } 75 return *this; 76 } 77 // LCOV_EXCL_STOP ~Message()78Message::~Message() 79 { 80 if (msgContent_ != nullptr) { 81 delete msgContent_; 82 msgContent_ = nullptr; 83 } 84 } 85 } // namespace MiscServices 86 } // namespace OHOS 87