diff --git a/common/BUILD.gn b/common/BUILD.gn index f972803cba0e1ec0427bea9621ea700f74f6799e..5eb2ebcf5957b55b6d78a86d4bc852dd34c8609c 100644 --- a/common/BUILD.gn +++ b/common/BUILD.gn @@ -22,10 +22,12 @@ ohos_shared_library("distributed_screen_utils") { "include", "//base/hiviewdfx/hisysevent/interfaces/native/innerkits/hisysevent/include", "//base/hiviewdfx/hitrace/interfaces/native/innerkits/include/hitrace_meter", + "//third_party/json/include", ] sources = [ "src/dscreen_hisysevent.cpp", + "src/dscreen_json_util.cpp", "src/dscreen_log.cpp", "src/dscreen_sa_process_state.cpp", "src/dscreen_util.cpp", diff --git a/common/include/dscreen_constants.h b/common/include/dscreen_constants.h index e3f55a6608b67fe84411955437235520dc1967c7..629783d1e3c004b16292058e4b4b0c1c6c736e58 100644 --- a/common/include/dscreen_constants.h +++ b/common/include/dscreen_constants.h @@ -130,6 +130,7 @@ const std::string SINK_PROJ_SHOW_HEIGHT = "sinkProjShowHeight"; const std::string SINK_WIN_SHOW_X = "sinkWinShowX"; const std::string SINK_WIN_SHOW_Y = "sinkWinShowY"; +constexpr uint32_t MAX_MESSAGE_LEN = 40 * 1024 * 1024; constexpr float DEFAULT_DENSITY = 2.0; constexpr int32_t DEFAULT_SCREEN_FLAGS = 0; constexpr uint32_t DEFAULT_FPS = 30; diff --git a/common/include/dscreen_json_util.h b/common/include/dscreen_json_util.h new file mode 100644 index 0000000000000000000000000000000000000000..877b64f0be85115d117393009d4027f597ad1f6a --- /dev/null +++ b/common/include/dscreen_json_util.h @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_DSCREEN_JSON_UTIL_H +#define OHOS_DSCREEN_JSON_UTIL_H + +#include "nlohmann/json.hpp" + +namespace OHOS { +namespace DistributedHardware { +bool IsString(const nlohmann::json &jsonObj, const std::string &key); +bool IsUInt8(const nlohmann::json &jsonObj, const std::string &key); +bool IsInt32(const nlohmann::json &jsonObj, const std::string &key); +bool IsUInt32(const nlohmann::json &jsonObj, const std::string &key); +bool IsInt64(const nlohmann::json &jsonObj, const std::string &key); +bool IsUInt64(const nlohmann::json &jsonObj, const std::string &key); +bool IsArray(const nlohmann::json &jsonObj, const std::string &key); +bool IsBool(const nlohmann::json &jsonObj, const std::string &key); +} // namespace DistributedHardware +} // namespace OHOS +#endif \ No newline at end of file diff --git a/common/src/dscreen_json_util.cpp b/common/src/dscreen_json_util.cpp new file mode 100644 index 0000000000000000000000000000000000000000..408270e9a4f20280eb2f75dcd3598043b3f3a9b0 --- /dev/null +++ b/common/src/dscreen_json_util.cpp @@ -0,0 +1,97 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "dscreen_json_util.h" + +#include "dscreen_constants.h" +#include "dscreen_log.h" + +namespace OHOS { +namespace DistributedHardware { +bool IsString(const nlohmann::json &jsonObj, const std::string &key) +{ + bool res = jsonObj.contains(key) && jsonObj[key].is_string() && jsonObj[key].size() <= MAX_MESSAGE_LEN; + if (!res) { + DHLOGE("the key %s in jsonObj is invalid.", key.c_str()); + } + return res; +} + +bool IsUInt8(const nlohmann::json &jsonObj, const std::string &key) +{ + bool res = jsonObj.contains(key) && jsonObj[key].is_number_unsigned() && jsonObj[key] <= UINT8_MAX; + if (!res) { + DHLOGE("the key %s in jsonObj is invalid.", key.c_str()); + } + return res; +} + +bool IsInt32(const nlohmann::json &jsonObj, const std::string &key) +{ + bool res = jsonObj.contains(key) && jsonObj[key].is_number_integer() && jsonObj[key] >= INT32_MIN && + jsonObj[key] <= INT32_MAX; + if (!res) { + DHLOGE("the key %s in jsonObj is invalid.", key.c_str()); + } + return res; +} + +bool IsUInt32(const nlohmann::json &jsonObj, const std::string &key) +{ + bool res = jsonObj.contains(key) && jsonObj[key].is_number_unsigned() && jsonObj[key] <= UINT32_MAX; + if (!res) { + DHLOGE("the key %s in jsonObj is invalid.", key.c_str()); + } + return res; +} + +bool IsInt64(const nlohmann::json &jsonObj, const std::string &key) +{ + bool res = jsonObj.contains(key) && jsonObj[key].is_number_integer() && jsonObj[key] >= INT64_MIN && + jsonObj[key] <= INT64_MAX; + if (!res) { + DHLOGE("the key %s in jsonObj is invalid.", key.c_str()); + } + return res; +} + +bool IsUInt64(const nlohmann::json &jsonObj, const std::string &key) +{ + bool res = jsonObj.contains(key) && jsonObj[key].is_number_unsigned() && jsonObj[key] <= UINT64_MAX; + if (!res) { + DHLOGE("the key %s in jsonObj is invalid.", key.c_str()); + } + return res; +} + +bool IsArray(const nlohmann::json &jsonObj, const std::string &key) +{ + bool res = jsonObj.contains(key) && jsonObj[key].is_array(); + if (!res) { + DHLOGE("the key %s in jsonObj is invalid.", key.c_str()); + } + return res; +} + +bool IsBool(const nlohmann::json &jsonObj, const std::string &key) +{ + bool res = jsonObj.contains(key) && jsonObj[key].is_boolean(); + if (!res) { + DHLOGE("the key %s in jsonObj is invalid.", key.c_str()); + } + return res; +} +} // namespace DistributedHardware +} // namespace OHOS \ No newline at end of file diff --git a/services/common/utils/include/dscreen_maprelation.h b/services/common/utils/include/dscreen_maprelation.h index 25c440278e7b9d4f27327825afbc211d71ab9b29..e63ea17e3ba381850a3512f7ec9d21e2db54d39c 100644 --- a/services/common/utils/include/dscreen_maprelation.h +++ b/services/common/utils/include/dscreen_maprelation.h @@ -25,8 +25,8 @@ namespace DistributedHardware { struct ScreenRect { int32_t startX; int32_t startY; - uint16_t width; - uint16_t height; + uint32_t width; + uint32_t height; }; struct DisplayRect { int32_t startX; diff --git a/services/common/utils/src/dscreen_maprelation.cpp b/services/common/utils/src/dscreen_maprelation.cpp index 28db6e2e66522255389e7450d704f540c58c4a53..57d3db3e7efcebfe55992c039168bc373bd95a6e 100644 --- a/services/common/utils/src/dscreen_maprelation.cpp +++ b/services/common/utils/src/dscreen_maprelation.cpp @@ -17,6 +17,7 @@ #include "dscreen_constants.h" #include "dscreen_errcode.h" +#include "dscreen_json_util.h" using json = nlohmann::json; @@ -78,8 +79,12 @@ void to_json(json &j, const DScreenMapRelation &dScreenMapRelation) void from_json(const json &j, DScreenMapRelation &dScreenMapRelation) { - j.at(KEY_DISPLAY_ID).get_to(dScreenMapRelation.displayId_); - j.at(KEY_SCREEN_ID).get_to(dScreenMapRelation.screenId_); + if (!IsUInt64(j, KEY_DISPLAY_ID) || !IsUInt64(j, KEY_SCREEN_ID)) { + return; + } + dScreenMapRelation.displayId_ = j[KEY_DISPLAY_ID].get(); + dScreenMapRelation.screenId_ = j[KEY_SCREEN_ID].get(); + from_json(j.at(KEY_DISPLAY_RECT), dScreenMapRelation.displayRect_); from_json(j.at(KEY_SCREEN_RECT), dScreenMapRelation.screenRect_); } @@ -96,10 +101,14 @@ void to_json(json &j, const DisplayRect &rect) void from_json(const json &j, DisplayRect &rect) { - j.at(KEY_POINT_START_X).get_to(rect.startX); - j.at(KEY_POINT_START_Y).get_to(rect.startY); - j.at(KEY_WIDTH).get_to(rect.width); - j.at(KEY_HEIGHT).get_to(rect.height); + if (!IsInt32(j, KEY_POINT_START_X) || !IsInt32(j, KEY_POINT_START_Y) || + !IsInt32(j, KEY_WIDTH) || !IsInt32(j, KEY_HEIGHT)) { + return; + } + rect.startX = j[KEY_POINT_START_X].get(); + rect.startY = j[KEY_POINT_START_Y].get(); + rect.width = j[KEY_WIDTH].get(); + rect.height = j[KEY_HEIGHT].get(); } void to_json(json &j, const ScreenRect &rect) @@ -114,10 +123,14 @@ void to_json(json &j, const ScreenRect &rect) void from_json(const json &j, ScreenRect &rect) { - j.at(KEY_POINT_START_X).get_to(rect.startX); - j.at(KEY_POINT_START_Y).get_to(rect.startY); - j.at(KEY_WIDTH).get_to(rect.width); - j.at(KEY_HEIGHT).get_to(rect.height); + if (!IsInt32(j, KEY_POINT_START_X) || !IsInt32(j, KEY_POINT_START_Y) || + !IsUInt32(j, KEY_WIDTH) || !IsUInt32(j, KEY_HEIGHT)) { + return; + } + rect.startX = j[KEY_POINT_START_X].get(); + rect.startY = j[KEY_POINT_START_Y].get(); + rect.width = j[KEY_WIDTH].get(); + rect.height = j[KEY_HEIGHT].get(); } } // namespace DistributedHardware } // namespace OHOS \ No newline at end of file diff --git a/services/common/utils/src/video_param.cpp b/services/common/utils/src/video_param.cpp index 0b6d2b9f2454d359924de034d27fdf82b6698b79..561c7ac2b0dab2270ce4f94d8f88d480c9c13803 100644 --- a/services/common/utils/src/video_param.cpp +++ b/services/common/utils/src/video_param.cpp @@ -15,6 +15,7 @@ #include "video_param.h" #include "dscreen_constants.h" +#include "dscreen_json_util.h" using json = nlohmann::json; @@ -105,13 +106,19 @@ void to_json(json &j, const DistributedHardware::VideoParam &videoParam) void from_json(const json &j, DistributedHardware::VideoParam &videoParam) { - j.at(KEY_SCREEN_WIDTH).get_to(videoParam.screenWidth_); - j.at(KEY_SCREEN_HEIGHT).get_to(videoParam.screenHeight_); - j.at(KEY_VIDEO_WIDTH).get_to(videoParam.videoWidth_); - j.at(KEY_VIDEO_HEIGHT).get_to(videoParam.videoHeight_); - j.at(KEY_FPS).get_to(videoParam.fps_); - j.at(KEY_CODECTYPE).get_to(videoParam.codecType_); - j.at(KEY_COLOR_FORMAT).get_to(videoParam.videoFormat_); + if (!IsUInt32(j, KEY_SCREEN_WIDTH) || !IsUInt32(j, KEY_SCREEN_HEIGHT) || + !IsUInt32(j, KEY_VIDEO_WIDTH) || !IsUInt32(j, KEY_VIDEO_HEIGHT) || + !IsUInt32(j, KEY_FPS) || !IsUInt8(j, KEY_CODECTYPE) || !IsUInt8(j, KEY_COLOR_FORMAT)) { + return; + } + + videoParam.screenWidth_ = j[KEY_SCREEN_WIDTH].get(); + videoParam.screenHeight_ = j[KEY_SCREEN_HEIGHT].get(); + videoParam.videoWidth_ = j[KEY_VIDEO_WIDTH].get(); + videoParam.videoHeight_ = j[KEY_VIDEO_HEIGHT].get(); + videoParam.fps_ = j[KEY_FPS].get(); + videoParam.codecType_ = j[KEY_CODECTYPE].get(); + videoParam.videoFormat_ = j[KEY_COLOR_FORMAT].get(); } } // namespace DistributedHardware } // namespace OHOS diff --git a/services/screenclient/include/screen_client_common.h b/services/screenclient/include/screen_client_common.h index 431cc212f2b0704b0d36e529b164cb39fa115e85..2cc8e3f4f7e5b0a940157295371a5ff65d5e16c5 100644 --- a/services/screenclient/include/screen_client_common.h +++ b/services/screenclient/include/screen_client_common.h @@ -23,8 +23,8 @@ struct WindowProperty { uint64_t displayId; int32_t startX; int32_t startY; - uint16_t width; - uint16_t height; + uint32_t width; + uint32_t height; }; } // namespace DistributedHardware } // namespace OHOS diff --git a/services/screenservice/sinkservice/screenregionmgr/include/screenregionmgr.h b/services/screenservice/sinkservice/screenregionmgr/include/screenregionmgr.h index 0495174fcc940c55c90b19da20c8d2fc5354dc05..b3a1b5f15b09b214846b61cafb1f828c8ae51edf 100644 --- a/services/screenservice/sinkservice/screenregionmgr/include/screenregionmgr.h +++ b/services/screenservice/sinkservice/screenregionmgr/include/screenregionmgr.h @@ -46,6 +46,7 @@ private: sptr GetDScreenSourceSA(const std::string &devId); int32_t NotifyRemoteScreenService(const std::string &remoteDevId, const std::string &dhId, int32_t eventCode, const std::string &eventContent); + bool CheckContentJson(json &eventContentJson); void HandleNotifySetUp(const std::string &remoteDevId, const std::string &eventContent); void NotifyRemoteSourceSetUpResult(const std::string &remoteDevId, const std::string &dhId, int32_t errCode, const std::string &errContent); diff --git a/services/screenservice/sinkservice/screenregionmgr/src/screenregionmgr.cpp b/services/screenservice/sinkservice/screenregionmgr/src/screenregionmgr.cpp index 54bb00e196d303d2e85f6b7e82724c433129b9ca..9c67e4835f0e729c43e1d9e22582d4046f0b8086 100644 --- a/services/screenservice/sinkservice/screenregionmgr/src/screenregionmgr.cpp +++ b/services/screenservice/sinkservice/screenregionmgr/src/screenregionmgr.cpp @@ -23,6 +23,7 @@ #include "dscreen_constants.h" #include "dscreen_errcode.h" #include "dscreen_fwkkit.h" +#include "dscreen_json_util.h" #include "dscreen_log.h" #include "dscreen_maprelation.h" #include "dscreen_util.h" @@ -112,6 +113,17 @@ void ScreenRegionManager::GetScreenDumpInfo(std::string &result) result.append(" }\n]"); } +bool ScreenRegionManager::CheckContentJson(json &eventContentJson) +{ + if (!IsUInt64(eventContentJson, KEY_SCREEN_ID)) { + return false; + } + if (!IsString(eventContentJson, KEY_DH_ID)) { + return false; + } + return true; +} + void ScreenRegionManager::HandleNotifySetUp(const std::string &remoteDevId, const std::string &eventContent) { DHLOGI("HandleNotifySetUp, remoteDevId: %s", GetAnonyString(remoteDevId).c_str()); @@ -121,14 +133,15 @@ void ScreenRegionManager::HandleNotifySetUp(const std::string &remoteDevId, cons return; } - if (!eventContentJson.contains(KEY_SCREEN_ID) || !eventContentJson.contains(KEY_DH_ID) || - !eventContentJson.contains(KEY_VIDEO_PARAM) || !eventContentJson.contains(KEY_MAPRELATION)) { + if (!CheckContentJson(eventContentJson) || !eventContentJson.contains(KEY_VIDEO_PARAM) || + !eventContentJson.contains(KEY_MAPRELATION)) { NotifyRemoteSourceSetUpResult(remoteDevId, "", ERR_DH_SCREEN_SA_SCREENREGION_SETUP_FAIL, ""); return; } - uint64_t screenId = eventContentJson[KEY_SCREEN_ID]; - std::string dhId = eventContentJson[KEY_DH_ID]; + uint64_t screenId = eventContentJson[KEY_SCREEN_ID].get(); + std::string dhId = eventContentJson[KEY_DH_ID].get(); + std::shared_ptr videoParam = std::make_shared(eventContentJson[KEY_VIDEO_PARAM].get()); std::shared_ptr mapRelation = diff --git a/services/screenservice/sourceservice/dscreenmgr/include/dscreen_manager.h b/services/screenservice/sourceservice/dscreenmgr/include/dscreen_manager.h index df5589542c24cdb18fa37d166c4b9a8cb64eea9e..bc5db4dc86d522069c93064f5f235168f27c2d61 100644 --- a/services/screenservice/sourceservice/dscreenmgr/include/dscreen_manager.h +++ b/services/screenservice/sourceservice/dscreenmgr/include/dscreen_manager.h @@ -78,6 +78,7 @@ private: sptr GetDScreenSinkSA(const std::string &devId); int32_t NotifyRemoteScreenService(const std::string &devId, int32_t eventCode, const std::string &eventContent); void NotifyRemoteSinkSetUp(const std::shared_ptr &dScreen); + bool CheckContent(json &eventContent); void HandleNotifySetUpResult(const std::string &remoteDevId, const std::string &eventContent); }; } // namespace DistributedHardware diff --git a/services/screenservice/sourceservice/dscreenmgr/src/dscreen.cpp b/services/screenservice/sourceservice/dscreenmgr/src/dscreen.cpp index 3f0510dcb00054412b74bd6c6ad890ee69b4c419..eaef12e43472e929c827fe2e3b5701d453a2db40 100644 --- a/services/screenservice/sourceservice/dscreenmgr/src/dscreen.cpp +++ b/services/screenservice/sourceservice/dscreenmgr/src/dscreen.cpp @@ -21,6 +21,7 @@ #include "dscreen_constants.h" #include "dscreen_errcode.h" #include "dscreen_hisysevent.h" +#include "dscreen_json_util.h" #include "dscreen_log.h" #include "dscreen_util.h" #include "screen_manager_adapter.h" @@ -204,8 +205,8 @@ void DScreen::HandleEnable(const std::string ¶m, const std::string &taskId) return; } - videoParam_->SetScreenWidth(attrJson[KEY_SCREEN_WIDTH]); - videoParam_->SetScreenHeight(attrJson[KEY_SCREEN_HEIGHT]); + videoParam_->SetScreenWidth(attrJson[KEY_SCREEN_WIDTH].get()); + videoParam_->SetScreenHeight(attrJson[KEY_SCREEN_HEIGHT].get()); // negotiate codecType ret = NegotiateCodecType(attrJson[KEY_CODECTYPE]); @@ -241,8 +242,8 @@ int32_t DScreen::CheckJsonData(json &attrJson) return ERR_DH_SCREEN_SA_ENABLE_JSON_ERROR; } - if (!attrJson.contains(KEY_SCREEN_WIDTH) || !attrJson.contains(KEY_SCREEN_HEIGHT) || - !attrJson.contains(KEY_CODECTYPE)) { + if (!IsUInt32(attrJson, KEY_SCREEN_WIDTH) || !IsUInt32(attrJson, KEY_SCREEN_HEIGHT) || + !IsString(attrJson, KEY_CODECTYPE)) { DHLOGE("enable param is invalid."); return ERR_DH_SCREEN_SA_ENABLE_JSON_ERROR; } diff --git a/services/screenservice/sourceservice/dscreenmgr/src/dscreen_manager.cpp b/services/screenservice/sourceservice/dscreenmgr/src/dscreen_manager.cpp index e39c290dd97066ddbcee7b73ecf336e149e37eda..139a076e723fae68c4536b8cada7eb20568ae9ab 100644 --- a/services/screenservice/sourceservice/dscreenmgr/src/dscreen_manager.cpp +++ b/services/screenservice/sourceservice/dscreenmgr/src/dscreen_manager.cpp @@ -24,6 +24,7 @@ #include "dscreen_constants.h" #include "dscreen_errcode.h" #include "dscreen_fwkkit.h" +#include "dscreen_json_util.h" #include "dscreen_log.h" #include "dscreen_util.h" #include "idscreen_sink.h" @@ -484,6 +485,20 @@ void DScreenManager::NotifyRemoteSinkSetUp(const std::shared_ptr &dScre NotifyRemoteScreenService(devId, eventCode, eventContent); } +bool DScreenManager::CheckContent(json &eventContent) +{ + if (!IsString(eventContent, KEY_DH_ID)) { + return false; + } + if (!IsInt32(eventContent, KEY_ERR_CODE)) { + return false; + } + if (!IsString(eventContent, KEY_ERR_CONTENT)) { + return false; + } + return true; +} + void DScreenManager::HandleNotifySetUpResult(const std::string &remoteDevId, const std::string &eventContent) { DHLOGI("HandleNotifySetUpResult, remoteDevId:%s", GetAnonyString(remoteDevId).c_str()); @@ -493,16 +508,14 @@ void DScreenManager::HandleNotifySetUpResult(const std::string &remoteDevId, con return; } - if (!eventContentJson.contains(KEY_DH_ID) || - !eventContentJson.contains(KEY_ERR_CODE) || - !eventContentJson.contains(KEY_ERR_CONTENT)) { + if (!CheckContent(eventContentJson)) { DHLOGE("HandleNotifySetUpResult, eventContent is invalid"); return; } - std::string dhId = eventContentJson[KEY_DH_ID]; - int32_t errCode = eventContentJson[KEY_ERR_CODE]; - std::string errContent = eventContentJson[KEY_ERR_CONTENT]; + std::string dhId = eventContentJson[KEY_DH_ID].get(); + int32_t errCode = eventContentJson[KEY_ERR_CODE].get(); + std::string errContent = eventContentJson[KEY_ERR_CONTENT].get(); std::string dScreenIdx = remoteDevId + SEPERATOR + dhId; std::lock_guard lock(dScreenMapMtx_);