1 /* 2 * Copyright (c) 2020 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 OHOS_ABILITYMS_STATUS_H 17 #define OHOS_ABILITYMS_STATUS_H 18 19 #include <string> 20 21 namespace OHOS { 22 class AbilityMsStatus { 23 public: AbilityMsStatus()24 AbilityMsStatus() : code_(OK) {} 25 ~AbilityMsStatus() = default; Ok()26 static AbilityMsStatus Ok() 27 { 28 return AbilityMsStatus(); 29 } 30 BmsQueryStatus(const char * msg)31 static AbilityMsStatus BmsQueryStatus(const char *msg) 32 { 33 return AbilityMsStatus(BMS_QUERY_NOT_FOUND, nullptr, msg); 34 } 35 AppTransanctStatus(const char * msg)36 static AbilityMsStatus AppTransanctStatus(const char *msg) 37 { 38 return AbilityMsStatus(APP_TRANSACT_ERROR, nullptr, msg); 39 } 40 TaskStatus(const char * key,const char * msg)41 static AbilityMsStatus TaskStatus(const char *key, const char *msg) 42 { 43 return AbilityMsStatus(ABILITY_TASK_ERROR, key, msg); 44 } 45 LifeCycleStatus(const char * msg)46 static AbilityMsStatus LifeCycleStatus(const char *msg) 47 { 48 return AbilityMsStatus(LIFE_CYCLE_ILLEGAL, nullptr, msg); 49 } 50 ProcessStatus(const char * msg)51 static AbilityMsStatus ProcessStatus(const char *msg) 52 { 53 return AbilityMsStatus(PROCESS_ERROR, nullptr, msg); 54 } 55 NoActiveAbilityStatus(const char * key,const char * msg)56 static AbilityMsStatus NoActiveAbilityStatus(const char *key, const char *msg) 57 { 58 return AbilityMsStatus(NO_ACTIVE_ABILITY, key, msg); 59 } 60 HelpStatus(const char * msg)61 static AbilityMsStatus HelpStatus(const char *msg) 62 { 63 return AbilityMsStatus(ABILITY_HELP_ERROR, nullptr, msg); 64 } 65 DumpStatus(const char * msg)66 static AbilityMsStatus DumpStatus(const char *msg) 67 { 68 return AbilityMsStatus(ABILITY_DUMP, nullptr, msg); 69 } 70 PermissionStatus(const char * msg)71 static AbilityMsStatus PermissionStatus(const char *msg) 72 { 73 return AbilityMsStatus(PERMISSION_DENIED, nullptr, msg); 74 } 75 AppCapabilitiesStatus(const char * msg)76 static AbilityMsStatus AppCapabilitiesStatus(const char *msg) 77 { 78 return AbilityMsStatus(QUERY_APP_CAPS, nullptr, msg); 79 } 80 DumpAppend(const AbilityMsStatus & status)81 void DumpAppend(const AbilityMsStatus &status) 82 { 83 if (code_ == ABILITY_DUMP) { 84 state_ += status.state_; 85 } 86 } 87 DumpAppend(const char * msg)88 void DumpAppend(const char *msg) 89 { 90 if (code_ == ABILITY_DUMP) { 91 state_ += msg; 92 } 93 } 94 Dump()95 const char *Dump() const 96 { 97 if (code_ == ABILITY_DUMP) { 98 return state_.c_str(); 99 } 100 return ""; 101 } 102 IsOk()103 bool IsOk() const 104 { 105 return code_ == OK; 106 } 107 IsNoActiveAbility()108 bool IsNoActiveAbility() const 109 { 110 return code_ == NO_ACTIVE_ABILITY; 111 } 112 IsProcessError()113 bool IsProcessError() const 114 { 115 return code_ == PROCESS_ERROR; 116 } 117 IsTransactError()118 bool IsTransactError() const 119 { 120 return code_ == APP_TRANSACT_ERROR; 121 } 122 123 void LogStatus() const; 124 private: 125 enum StatusCode { 126 OK = 0, 127 BMS_QUERY_NOT_FOUND, 128 PERMISSION_DENIED, 129 ABILITY_TASK_ERROR, 130 APP_TRANSACT_ERROR, 131 LIFE_CYCLE_ILLEGAL, 132 PROCESS_ERROR, 133 NO_ACTIVE_ABILITY, 134 ABILITY_HELP_ERROR, 135 ABILITY_DUMP, 136 QUERY_APP_CAPS, 137 }; 138 AbilityMsStatus(StatusCode code, const char *key, const char *msg); 139 StatusCode code_ = OK; 140 std::string state_; 141 std::string key_; 142 }; 143 144 #define CHECK_RESULT_LOG_CODE(status, code) \ 145 do { \ 146 if (!((status).IsOk())) { \ 147 status.LogStatus(); \ 148 return code; \ 149 } \ 150 } while (0) 151 152 #define CHECK_RESULT_LOG(status) \ 153 do { \ 154 if (!((status).IsOk())) { \ 155 status.LogStatus(); \ 156 return; \ 157 } \ 158 } while (0) 159 160 #define CHECK_RESULT(status) \ 161 do { \ 162 if (!((status).IsOk())) { \ 163 return status; \ 164 } \ 165 } while (0) 166 } 167 #endif // OHOS_ABILITY_CONST_H 168