From 24d1e5a12f40dc29ac4849d50b3041689f2a1bfc Mon Sep 17 00:00:00 2001 From: mamingshuai Date: Wed, 2 Jun 2021 00:05:54 +0800 Subject: [PATCH] update OpenHarmony 2.0 Canary --- .gitattributes | 15 + LICENSE | 177 +++++++ README.en.md | 36 -- README.md | 45 +- README_zh.md | 32 ++ interfaces/innerkits/uri/BUILD.gn | 40 ++ ohos.build | 23 + services/dtbschedmgr/include/parcel_helper.h | 157 ++++++ services/dtbschedmgr/include/uri.h | 214 ++++++++ utils/native/include/single_instance.h | 46 ++ utils/native/include/test_log.h | 25 + utils/native/source/uri.cpp | 510 +++++++++++++++++++ 12 files changed, 1259 insertions(+), 61 deletions(-) create mode 100644 .gitattributes create mode 100755 LICENSE delete mode 100644 README.en.md mode change 100644 => 100755 README.md create mode 100755 README_zh.md create mode 100755 interfaces/innerkits/uri/BUILD.gn create mode 100755 ohos.build create mode 100755 services/dtbschedmgr/include/parcel_helper.h create mode 100755 services/dtbschedmgr/include/uri.h create mode 100755 utils/native/include/single_instance.h create mode 100755 utils/native/include/test_log.h create mode 100644 utils/native/source/uri.cpp diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 00000000..51c63e29 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,15 @@ +*.tgz filter=lfs diff=lfs merge=lfs -text +*.trp filter=lfs diff=lfs merge=lfs -text +*.apk filter=lfs diff=lfs merge=lfs -text +*.jar filter=lfs diff=lfs merge=lfs -text +*.mp4 filter=lfs diff=lfs merge=lfs -text +*.zip filter=lfs diff=lfs merge=lfs -text +*.asm filter=lfs diff=lfs merge=lfs -text +*.8svn filter=lfs diff=lfs merge=lfs -text +*.9svn filter=lfs diff=lfs merge=lfs -text +*.dylib filter=lfs diff=lfs merge=lfs -text +*.exe filter=lfs diff=lfs merge=lfs -text +*.a filter=lfs diff=lfs merge=lfs -text +*.so filter=lfs diff=lfs merge=lfs -text +*.bin filter=lfs diff=lfs merge=lfs -text +*.dll filter=lfs diff=lfs merge=lfs -text diff --git a/LICENSE b/LICENSE new file mode 100755 index 00000000..4a459866 --- /dev/null +++ b/LICENSE @@ -0,0 +1,177 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS \ No newline at end of file diff --git a/README.en.md b/README.en.md deleted file mode 100644 index 7849a1ed..00000000 --- a/README.en.md +++ /dev/null @@ -1,36 +0,0 @@ -# distributedschedule_dms_fwk - -#### Description -Framework of the distributed manager service (distributed scheduler) | 分布式任务调度执行框架 - -#### Software Architecture -Software architecture description - -#### Installation - -1. xxxx -2. xxxx -3. xxxx - -#### Instructions - -1. xxxx -2. xxxx -3. xxxx - -#### Contribution - -1. Fork the repository -2. Create Feat_xxx branch -3. Commit your code -4. Create Pull Request - - -#### Gitee Feature - -1. You can use Readme\_XXX.md to support different languages, such as Readme\_en.md, Readme\_zh.md -2. Gitee blog [blog.gitee.com](https://blog.gitee.com) -3. Explore open source project [https://gitee.com/explore](https://gitee.com/explore) -4. The most valuable open source project [GVP](https://gitee.com/gvp) -5. The manual of Gitee [https://gitee.com/help](https://gitee.com/help) -6. The most popular members [https://gitee.com/gitee-stars/](https://gitee.com/gitee-stars/) diff --git a/README.md b/README.md old mode 100644 new mode 100755 index 54221c69..8f74e869 --- a/README.md +++ b/README.md @@ -1,37 +1,32 @@ -# distributedschedule_dms_fwk +# dmsfwk -#### 介绍 -Framework of the distributed manager service (distributed scheduler) | 分布式任务调度执行框架 +- [Introduction](#section11660541593) +- [Directory Structure](#section161941989596) +- [Repositories Involved](#section1371113476307) -#### 软件架构 -软件架构说明 +## Introduction +The **dmsfwk** module provides URI capabilities for other OpenHarmony subsystems such as the application development framework \(including the ability framework and application framework\). -#### 安装教程 +## Directory Structure -1. xxxx -2. xxxx -3. xxxx +``` +/foundation/distributedschedule/dmsfwk/interfaces/innerkits/uri +``` -#### 使用说明 +## Repositories Involved -1. xxxx -2. xxxx -3. xxxx +Distributed Scheduler subsystem -#### 参与贡献 +**distributedschedule\_dms\_fwk** -1. Fork 本仓库 -2. 新建 Feat_xxx 分支 -3. 提交代码 -4. 新建 Pull Request +distributedschedule\_safwk +distributedschedule\_samgr -#### 特技 +distributedschedule\_safwk\_lite + +hdistributedschedule\_samgr\_lite + +distributedschedule\_dms\_fwk\_lite -1. 使用 Readme\_XXX.md 来支持不同的语言,例如 Readme\_en.md, Readme\_zh.md -2. Gitee 官方博客 [blog.gitee.com](https://blog.gitee.com) -3. 你可以 [https://gitee.com/explore](https://gitee.com/explore) 这个地址来了解 Gitee 上的优秀开源项目 -4. [GVP](https://gitee.com/gvp) 全称是 Gitee 最有价值开源项目,是综合评定出的优秀开源项目 -5. Gitee 官方提供的使用手册 [https://gitee.com/help](https://gitee.com/help) -6. Gitee 封面人物是一档用来展示 Gitee 会员风采的栏目 [https://gitee.com/gitee-stars/](https://gitee.com/gitee-stars/) diff --git a/README_zh.md b/README_zh.md new file mode 100755 index 00000000..2f4a7b9f --- /dev/null +++ b/README_zh.md @@ -0,0 +1,32 @@ +# dmsfwk组件 + +- [简介](#section11660541593) +- [目录](#section161941989596) +- [相关仓](#section1371113476307) + +## 简介 + +提供OpenHarmony uri组件的能力,当前提供给应用程序框架等子系统使用。 + +## 目录 + +``` +/foundation/distributedschedule/dmsfwk/interfaces/innerkits/uri +``` + +## 相关仓 + +分布式任务调度子系统 + +**distributedschedule\_dms\_fwk** + +distributedschedule\_safwk + +distributedschedule\_samgr + +distributedschedule\_safwk\_lite + +hdistributedschedule\_samgr\_lite + +distributedschedule\_dms\_fwk\_lite + diff --git a/interfaces/innerkits/uri/BUILD.gn b/interfaces/innerkits/uri/BUILD.gn new file mode 100755 index 00000000..c9cb9e62 --- /dev/null +++ b/interfaces/innerkits/uri/BUILD.gn @@ -0,0 +1,40 @@ +# Copyright (c) 2021 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. + +import("//build/ohos.gni") +import("//build/ohos_var.gni") + +config("zuri_config") { + include_dirs = [ + "//utils/native/base/include", + "//foundation/distributedschedule/dmsfwk/services/dtbschedmgr/include", + ] +} + +ohos_shared_library("zuri") { + sources = + [ "//foundation/distributedschedule/dmsfwk/utils/native/source/uri.cpp" ] + configs = [ ":zuri_config" ] + + public_configs = [ ":zuri_config" ] + + if (is_standard_system) { + external_deps = [ + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", + ] + part_name = "dmsfwk_L2" + } + deps = [ "//utils/native/base:utils" ] + subsystem_name = "distributedschedule" +} diff --git a/ohos.build b/ohos.build new file mode 100755 index 00000000..4169c3f7 --- /dev/null +++ b/ohos.build @@ -0,0 +1,23 @@ +{ + "parts": { + "dmsfwk_L2": { + "variants": ["phone", "wearable"], + "inner_kits": [ + { + "header": { + "header_base": "//foundation/distributedschedule/dmsfwk/services/dtbschedmgr/include", + "header_files": [ + "uri.h" + ] + }, + "name": "//foundation/distributedschedule/dmsfwk/interfaces/innerkits/uri:zuri" + } + ], + "module_list": [ + ], + "system_kits": [], + "test_list": [] + } + }, + "subsystem": "distributedschedule" +} diff --git a/services/dtbschedmgr/include/parcel_helper.h b/services/dtbschedmgr/include/parcel_helper.h new file mode 100755 index 00000000..f0e09e3f --- /dev/null +++ b/services/dtbschedmgr/include/parcel_helper.h @@ -0,0 +1,157 @@ +/* + * Copyright (c) 2021 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_DISTRIBUTED_SCHEDULE_PARCEL_HELPER_H +#define OHOS_DISTRIBUTED_SCHEDULE_PARCEL_HELPER_H + +#include + +namespace OHOS { +namespace DistributedSchedule { +#define PARCEL_WRITE_HELPER(parcel, type, value) \ + do { \ + bool ret = parcel.Write##type((value)); \ + if (!ret) { \ + HILOGE("%{public}s write value failed!", __func__); \ + return ERR_FLATTEN_OBJECT; \ + } \ + } while (0) + +#define PARCEL_WRITE_HELPER_NORET(parcel, type, value) \ + do { \ + bool ret = parcel.Write##type((value)); \ + if (!ret) { \ + HILOGE("%{public}s write value failed!", __func__); \ + return; \ + } \ + } while (0) + +#define PARCEL_WRITE_HELPER_RET(parcel, type, value, failRet) \ + do { \ + bool ret = parcel.Write##type((value)); \ + if (!ret) { \ + HILOGE("%{public}s write value failed!", __func__); \ + return failRet; \ + } \ + } while (0) + +#define PARCEL_READ_HELPER(parcel, type, out) \ + do { \ + bool ret = parcel.Read##type((out)); \ + if (!ret) { \ + HILOGE("%{public}s read value failed!", __func__); \ + return ERR_FLATTEN_OBJECT; \ + } \ + } while (0) + +#define PARCEL_READ_HELPER_RET(parcel, type, out, failRet) \ + do { \ + bool ret = parcel.Read##type((out)); \ + if (!ret) { \ + HILOGE("%{public}s read value failed!", __func__); \ + return failRet; \ + } \ + } while (0) + +#define PARCEL_READ_HELPER_NORET(parcel, type, out) \ + do { \ + bool ret = parcel.Read##type((out)); \ + if (!ret) { \ + HILOGW("%{public}s read value failed!", __func__); \ + } \ + } while (0) + +#define PARCEL_TRANSACT_SYNC_RET_INT(remote, code, data, reply) \ + do { \ + MessageOption option; \ + int32_t error = remote->SendRequest(code, data, reply, option); \ + if (error != ERR_NONE) { \ + HILOGE("%{public}s transact failed, error: %{public}d", __func__, error); \ + return error; \ + } \ + int32_t result = reply.ReadInt32(); \ + HILOGD("%{public}s get result from server data = %{public}d", __func__, result); \ + return result; \ + } while (0) + +#define PARCEL_TRANSACT_SYNC_DFX(remote, code, data, reply) \ + do { \ + MessageOption option; \ + int32_t error = remote->SendRequest(code, data, reply, option); \ + if (error != ERR_NONE) { \ + HILOGE("%{public}s transact failed, error: %{public}d", __func__, error); \ + return error; \ + } \ + int32_t result = 0; \ + PARCEL_READ_HELPER(reply, Int32, result); \ + int64_t timeTick = 0; \ + PARCEL_READ_HELPER(reply, Int64, timeTick); \ + std::string package; \ + PARCEL_READ_HELPER(reply, String, package); \ + std::string deviceId; \ + PARCEL_READ_HELPER_NORET(reply, String, deviceId); \ + std::string message = "#ARG1:" + package + "#ARG2:" + std::to_string(timeTick) + "#PDVID:" + deviceId; \ + OHOS::DevTools::JankLog::BetaDebug(JLID_LOCAL_DMS_START_ABILITY_RECV, message); \ + OHOS::DevTools::JankLog::Debug(JLID_LOCAL_DMS_CONTINUATION_RECV, "packageName=%s, recvTime=%" PRIu64, \ + package.c_str(), timeTick); \ + HILOGD("%{public}s get result from server data = %{public}d", __func__, result); \ + return result; \ + } while (0) + +#define PARCEL_TRANSACT_SYNC_NORET(remote, code, data, reply) \ + do { \ + MessageOption option; \ + int32_t error = remote->SendRequest(code, data, reply, option); \ + if (error != ERR_NONE) { \ + HILOGE("%{public}s transact failed, error: %{public}d", __func__, error); \ + return; \ + } \ + HILOGD("%{public}s transact success!", __func__); \ + } while (0) + +#define PARCEL_TRANSACT_SYNC_JANKLOG(remote, code, data, reply, type) \ + do { \ + MessageOption option; \ + int32_t error = remote->SendRequest(code, data, reply, option); \ + if (error != ERR_NONE) { \ + HILOGE("%{public}s transact failed, error: %{public}d", __func__, error); \ + return error; \ + } \ + int32_t result = 0; \ + PARCEL_READ_HELPER(reply, Int32, result); \ + int64_t timeTick = 0; \ + PARCEL_READ_HELPER_NORET(reply, Int64, timeTick); \ + std::string package; \ + PARCEL_READ_HELPER_NORET(reply, String, package); \ + std::string deviceId; \ + PARCEL_READ_HELPER_NORET(reply, String, deviceId); \ + std::string message = "#ARG1:" + package + "#ARG2:" + std::to_string(timeTick) + "#PDVID:" + deviceId; \ + OHOS::DevTools::JankLog::BetaDebug(type, message); \ + HILOGD("%{public}s get result from server data = %{public}d", __func__, result); \ + return result; \ + } while (0) + +#define PARCEL_WRITE_REPLY_NOERROR(reply, type, result) \ + do { \ + bool ret = reply.Write##type(result); \ + if (!ret) { \ + HILOGW("%{public}s write reply failed.", __func__); \ + } \ + return ERR_NONE; \ + } while (0) +} // namespace DistributedSchedule +} // namespace OHOS + +#endif /* OHOS_DISTRIBUTED_SCHEDULE_PARCEL_HELPER_H */ diff --git a/services/dtbschedmgr/include/uri.h b/services/dtbschedmgr/include/uri.h new file mode 100755 index 00000000..9c383364 --- /dev/null +++ b/services/dtbschedmgr/include/uri.h @@ -0,0 +1,214 @@ +/* + * Copyright (c) 2021 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 UTILS_NATIVE_INCLUDE_URI_H_ +#define UTILS_NATIVE_INCLUDE_URI_H_ + +#include +#include +#include "parcel.h" + +namespace OHOS { +class Uri : public Parcelable { +public: + explicit Uri(const std::string& uriString); + ~Uri() = default; + + /** + * Get the Scheme part. + * + * @return the scheme string. + */ + std::string GetScheme(); + + /** + * Get the Ssp part. + * + * @return the SchemeSpecificPart string. + */ + std::string GetSchemeSpecificPart(); + + /** + * Get the GetAuthority part. + * + * @return the authority string. + */ + std::string GetAuthority(); + + /** + * Get the Host part. + * + * @return the host string. + */ + std::string GetHost(); + + /** + * Get the Port part. + * + * @return the port number. + */ + int GetPort(); + + /** + * Get the User part. + * + * @return the user string. + */ + std::string GetUserInfo(); + + /** + * Get the Query part. + * + * @return the query string. + */ + std::string GetQuery(); + + /** + * Get the Path part. + * + * @return the path string. + */ + std::string GetPath(); + + /** + * Get the path segments. + * + * @param the path segments of Uri. + */ + void GetPathSegments(std::vector& segments); + + /** + * Get the Fragment part. + * + * @return the fragment string. + */ + std::string GetFragment(); + + /** + * Returns true if this URI is hierarchical like "http://www.example.com". + * Absolute URIs are hierarchical if the scheme-specific part starts with a '/'. + * Relative URIs are always hierarchical. + * + * @return true if this URI is hierarchical, false if it's opaque. + */ + bool IsHierarchical(); + + /** + * Returns true if this URI is opaque like "mailto:nobody@ohos.com". + * The scheme-specific part of an opaque URI cannot start with a '/'. + * + * @return true if this URI is opaque, false if it's hierarchical. + */ + bool IsOpaque(); + + /** + * Returns true if this URI is absolute, i.e. if it contains an explicit scheme. + * + * @return true if this URI is absolute, false if it's relative. + */ + bool IsAbsolute(); + + /** + * Returns true if this URI is relative, i.e. if it doesn't contain an explicit scheme. + * + * @return true if this URI is relative, false if it's absolute. + */ + bool IsRelative(); + + /** + * Check whether the other is the same as this. + * + * @return true if the same string. + */ + bool Equals(const Uri& other) const; + + /** + * Compare to other uri. + * + * @return the string compare result. + */ + int CompareTo(const Uri& other) const; + + /** + * Convert to a string object. + * + * @return a string object. + */ + std::string ToString() const; + + /** + * override the == method. + * + * @return true if the same content, false not the same content. + */ + bool operator==(const Uri& other) const; + + /** + * Override Parcelable' interface. + * + * @return true if parcel write success, false write fail. + */ + virtual bool Marshalling(Parcel& parcel) const override; + + /** + * Support the Ummarshlling method for construct object by Parcel read. + * + * @return the uri object address. + */ + static Uri* Unmarshalling(Parcel& parcel); + +private: + bool CheckScheme(); + std::string ParseScheme(); + std::string ParseSsp(); + std::string ParseAuthority(); + std::string ParseUserInfo(); + std::string ParseHost(); + int ParsePort(); + std::string ParsePath(size_t ssi); + std::string ParsePath(); + std::string ParseQuery(); + std::string ParseFragment(); + + /** + * Finds the first ':'. + * + * @return the pos of the ':', string::npos if none found. + */ + size_t FindSchemeSeparator(); + + /** + * Finds the first '#'. + * + * @return the pos of the '#', string::npos if none found. + */ + size_t FindFragmentSeparator(); + + std::string uriString_; + std::string scheme_; + std::string ssp_; + std::string authority_; + std::string host_; + int port_; + std::string userInfo_; + std::string query_; + std::string path_; + std::string fragment_; + size_t cachedSsi_; + size_t cachedFsi_; +}; +} // namespace OHOS + +#endif // #ifndef UTILS_NATIVE_INCLUDE_URI_H_ diff --git a/utils/native/include/single_instance.h b/utils/native/include/single_instance.h new file mode 100755 index 00000000..1cf64a13 --- /dev/null +++ b/utils/native/include/single_instance.h @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2021 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 FOUNDATION_DISTRIBUTEDSCHEDULE_UTILS_NATIVE_INCLUDE_SINGLE_INSTANCE_H +#define FOUNDATION_DISTRIBUTEDSCHEDULE_UTILS_NATIVE_INCLUDE_SINGLE_INSTANCE_H + +namespace OHOS { +namespace DistributedSchedule { +#define DECLARE_SINGLE_INSTANCE_BASE(className) \ +public: \ + static className& GetInstance(); \ +private: \ + className(const className&) = delete; \ + className& operator= (const className&) = delete;\ + className(className&&) = delete; \ + className& operator= (className&&) = delete; \ + + +#define DECLARE_SINGLE_INSTANCE(className) \ + DECLARE_SINGLE_INSTANCE_BASE(className)\ +private: \ + className() = default; \ + ~className() = default; \ + +#define IMPLEMENT_SINGLE_INSTANCE(className)\ +className& className::GetInstance() \ +{ \ + static auto instance = new className(); \ + return *instance; \ +} +}; // namespace DistributedSchedule +}; // namespace OHOS + +#endif // FOUNDATION_DISTRIBUTEDSCHEDULE_UTILS_NATIVE_INCLUDE_SINGLE_INSTANCE_H \ No newline at end of file diff --git a/utils/native/include/test_log.h b/utils/native/include/test_log.h new file mode 100755 index 00000000..fceb02b2 --- /dev/null +++ b/utils/native/include/test_log.h @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2021 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 FOUNDATION_DISTRIBUTEDSCHEDULE_UTILS_NATIVE_INCLUDE_TEST_LOG_H +#define FOUNDATION_DISTRIBUTEDSCHEDULE_UTILS_NATIVE_INCLUDE_TEST_LOG_H + +namespace OHOS { +namespace DistributedSchedule { +#define DTEST_LOG std::cout << __FILE__ << ":" << __LINE__ << ":" +} // namespace DistributedSchedule +} // namespace OHOS + +#endif // FOUNDATION_DISTRIBUTEDSCHEDULE_UTILS_NATIVE_INCLUDE_TEST_LOG_H \ No newline at end of file diff --git a/utils/native/source/uri.cpp b/utils/native/source/uri.cpp new file mode 100644 index 00000000..7fc3d1db --- /dev/null +++ b/utils/native/source/uri.cpp @@ -0,0 +1,510 @@ +/* + * Copyright (c) 2021 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 "uri.h" +#include +#include +#include "hilog/log.h" +#include "string_ex.h" + +using std::string; +using std::regex; +using OHOS::HiviewDFX::HiLog; + +namespace OHOS { +namespace { + const string NOT_CACHED = "NOT VALID"; + const string EMPTY = ""; + const size_t NOT_FOUND = string::npos; + const int NOT_CALCULATED = -2; + const int PORT_NONE = -1; + const char SCHEME_SEPARATOR = ':'; + const char SCHEME_FRAGMENT = '#'; + const char LEFT_SEPARATOR = '/'; + const char RIGHT_SEPARATOR = '\\'; + const char QUERY_FLAG = '?'; + const char USER_HOST_SEPARATOR = '@'; + const char PORT_SEPARATOR = ':'; + const size_t POS_INC = 1; + const size_t POS_INC_MORE = 2; + const size_t POS_INC_AGAIN = 3; + const regex SCHEME_REGEX("[a-zA-Z][a-zA-Z|\\d|+|-|.]*$"); + const HiviewDFX::HiLogLabel LABEL = {LOG_CORE, 0xD001800, "URI"}; +}; // namespace + +Uri::Uri(const string& uriString) +{ + cachedSsi_ = NOT_FOUND; + cachedFsi_ = NOT_FOUND; + port_ = NOT_CALCULATED; + + if (uriString.empty()) { + HiLog::Error(LABEL, "Input empty!"); + return; + } + + uriString_ = uriString; + scheme_ = NOT_CACHED; + ssp_ = NOT_CACHED; + authority_ = NOT_CACHED; + host_ = NOT_CACHED; + userInfo_ = NOT_CACHED; + query_ = NOT_CACHED; + path_ = NOT_CACHED; + fragment_ = NOT_CACHED; + + if (!CheckScheme()) { + uriString_ = EMPTY; + HiLog::Error(LABEL, "Scheme wrong!"); + } +} + +bool Uri::CheckScheme() +{ + scheme_ = ParseScheme(); + if (scheme_.empty()) { + return true; + } + return regex_match(scheme_, SCHEME_REGEX); +} + +string Uri::GetScheme() +{ + if (uriString_.empty()) { + return EMPTY; + } + + if (scheme_ == NOT_CACHED) { + scheme_ = ParseScheme(); + } + return scheme_; +} + +string Uri::ParseScheme() +{ + size_t ssi = FindSchemeSeparator(); + return (ssi == NOT_FOUND) ? EMPTY : uriString_.substr(0, ssi); +} + +string Uri::GetSchemeSpecificPart() +{ + if (uriString_.empty()) { + return EMPTY; + } + + return (ssp_ == NOT_CACHED) ? (ssp_ = ParseSsp()) : ssp_; +} + +string Uri::ParseSsp() +{ + size_t ssi = FindSchemeSeparator(); + size_t fsi = FindFragmentSeparator(); + + size_t start = (ssi == NOT_FOUND) ? 0 : (ssi + 1); + size_t end = (fsi == NOT_FOUND) ? uriString_.size() : fsi; + + // Return everything between ssi and fsi. + string ssp = EMPTY; + if (end > start) { + ssp = uriString_.substr(start, end - start); + } + + return ssp; +} + +string Uri::GetAuthority() +{ + if (uriString_.empty()) { + return EMPTY; + } + + if (authority_ == NOT_CACHED) { + authority_ = ParseAuthority(); + } + return authority_; +} + +string Uri::ParseAuthority() +{ + size_t ssi = FindSchemeSeparator(); + if (ssi == NOT_FOUND) { + return EMPTY; + } + + size_t length = uriString_.length(); + // If "//" follows the scheme separator, we have an authority. + if ((length > (ssi + POS_INC_MORE)) && (uriString_.at(ssi + POS_INC) == LEFT_SEPARATOR) && + (uriString_.at(ssi + POS_INC_MORE) == LEFT_SEPARATOR)) { + // Look for the start of the path, query, or fragment, or the end of the string. + size_t start = ssi + POS_INC_AGAIN; + size_t end = start; + + while (end < length) { + char ch = uriString_.at(end); + if ((ch == LEFT_SEPARATOR) || (ch == RIGHT_SEPARATOR) || (ch == QUERY_FLAG) || + (ch == SCHEME_FRAGMENT)) { + break; + } + + end++; + } + + return uriString_.substr(start, end - start); + } else { + return EMPTY; + } +} + +string Uri::GetUserInfo() +{ + if (uriString_.empty()) { + return EMPTY; + } + + if (userInfo_ == NOT_CACHED) { + userInfo_ = ParseUserInfo(); + } + return userInfo_; +} + +string Uri::ParseUserInfo() +{ + string authority = GetAuthority(); + if (authority.empty()) { + return EMPTY; + } + + size_t end = authority.find_last_of(USER_HOST_SEPARATOR); + return (end == NOT_FOUND) ? EMPTY : authority.substr(0, end); +} + +string Uri::GetHost() +{ + if (uriString_.empty()) { + return EMPTY; + } + + if (host_ == NOT_CACHED) { + host_ = ParseHost(); + } + return host_; +} + +string Uri::ParseHost() +{ + string authority = GetAuthority(); + if (authority.empty()) { + return EMPTY; + } + + // Parse out user info and then port. + size_t userInfoSeparator = authority.find_last_of(USER_HOST_SEPARATOR); + size_t start = (userInfoSeparator == NOT_FOUND) ? 0 : (userInfoSeparator + 1); + size_t portSeparator = authority.find_first_of(PORT_SEPARATOR, start); + size_t end = (portSeparator == NOT_FOUND) ? authority.size() : portSeparator; + + string host = EMPTY; + if (start < end) { + host = authority.substr(start, end - start); + } + + return host; +} + +int Uri::GetPort() +{ + if (uriString_.empty()) { + return PORT_NONE; + } + + if (port_ == NOT_CALCULATED) { + port_ = ParsePort(); + } + return port_; +} + +int Uri::ParsePort() +{ + string authority = GetAuthority(); + if (authority.empty()) { + return PORT_NONE; + } + + // Make sure we look for the port separtor *after* the user info separator. + size_t userInfoSeparator = authority.find_last_of(USER_HOST_SEPARATOR); + size_t start = (userInfoSeparator == NOT_FOUND) ? 0 : (userInfoSeparator + 1); + size_t portSeparator = authority.find_first_of(PORT_SEPARATOR, start); + if (portSeparator == NOT_FOUND) { + return PORT_NONE; + } + + start = portSeparator + 1; + string portString = authority.substr(start); + + int value = PORT_NONE; + return StrToInt(portString, value) ? value : PORT_NONE; +} + +string Uri::GetQuery() +{ + if (uriString_.empty()) { + return EMPTY; + } + + if (query_ == NOT_CACHED) { + query_ = ParseQuery(); + } + return query_; +} + +string Uri::ParseQuery() +{ + size_t ssi = FindSchemeSeparator(); + if (ssi == NOT_FOUND) { + ssi = 0; + } + size_t qsi = uriString_.find_first_of(QUERY_FLAG, ssi); + if (qsi == NOT_FOUND) { + return EMPTY; + } + + size_t start = qsi + 1; + size_t fsi = FindFragmentSeparator(); + if (fsi == NOT_FOUND) { + return uriString_.substr(start); + } + + if (fsi < qsi) { + // Invalid. + return EMPTY; + } + + return uriString_.substr(start, fsi - start); +} + +string Uri::GetPath() +{ + if (uriString_.empty()) { + return EMPTY; + } + + if (path_ == NOT_CACHED) { + path_ = ParsePath(); + } + return path_; +} + +void Uri::GetPathSegments(std::vector& segments) +{ + if (uriString_.empty()) { + return; + } + if (path_ == NOT_CACHED) { + path_ = ParsePath(); + } + + size_t previous = 0; + size_t current; + while ((current = path_.find(LEFT_SEPARATOR, previous)) != std::string::npos) { + if (previous < current) { + segments.emplace_back(path_.substr(previous, current - previous)); + } + previous = current + POS_INC; + } + // Add in the final path segment. + if (previous < path_.length()) { + segments.emplace_back(path_.substr(previous)); + } +} + +string Uri::ParsePath() +{ + size_t ssi = FindSchemeSeparator(); + // If the URI is absolute. + if (ssi != NOT_FOUND) { + // Is there anything after the ':'? + if ((ssi + 1) == uriString_.length()) { + // Opaque URI. + return EMPTY; + } + + // A '/' after the ':' means this is hierarchical. + if (uriString_.at(ssi + 1) != LEFT_SEPARATOR) { + // Opaque URI. + return EMPTY; + } + } else { + // All relative URIs are hierarchical. + } + + return ParsePath(ssi); +} + +string Uri::ParsePath(size_t ssi) +{ + size_t length = uriString_.length(); + + // Find start of path. + size_t pathStart = (ssi == NOT_FOUND) ? 0 : (ssi + POS_INC); + if ((length > (pathStart + POS_INC)) && (uriString_.at(pathStart) == LEFT_SEPARATOR) && + (uriString_.at(pathStart + POS_INC) == LEFT_SEPARATOR)) { + // Skip over authority to path. + pathStart += POS_INC_MORE; + + while (pathStart < length) { + char ch = uriString_.at(pathStart); + if ((ch == QUERY_FLAG) || (ch == SCHEME_FRAGMENT)) { + return EMPTY; + } + + if ((ch == LEFT_SEPARATOR) || (ch == RIGHT_SEPARATOR)) { + break; + } + + pathStart++; + } + } + + // Find end of path. + size_t pathEnd = pathStart; + while (pathEnd < length) { + char ch = uriString_.at(pathEnd); + if ((ch == QUERY_FLAG) || (ch == SCHEME_FRAGMENT)) { + break; + } + + pathEnd++; + } + + return uriString_.substr(pathStart, pathEnd - pathStart); +} + +string Uri::GetFragment() +{ + if (uriString_.empty()) { + return EMPTY; + } + + if (fragment_ == NOT_CACHED) { + fragment_ = ParseFragment(); + } + return fragment_; +} + +string Uri::ParseFragment() +{ + size_t fsi = FindFragmentSeparator(); + return (fsi == NOT_FOUND) ? EMPTY : uriString_.substr(fsi + 1); +} + +size_t Uri::FindSchemeSeparator() +{ + if (cachedSsi_ == NOT_FOUND) { + cachedSsi_ = uriString_.find_first_of(SCHEME_SEPARATOR); + } + return cachedSsi_; +} + +size_t Uri::FindFragmentSeparator() +{ + if (cachedFsi_ == NOT_FOUND) { + cachedFsi_ = uriString_.find_first_of(SCHEME_FRAGMENT, FindSchemeSeparator()); + } + return cachedFsi_; +} + +bool Uri::IsHierarchical() +{ + if (uriString_.empty()) { + return false; + } + + size_t ssi = FindSchemeSeparator(); + if (ssi == NOT_FOUND) { + // All relative URIs are hierarchical. + return true; + } + + if (uriString_.length() == (ssi + 1)) { + // No ssp. + return false; + } + + // If the ssp starts with a '/', this is hierarchical. + return (uriString_.at(ssi + 1) == LEFT_SEPARATOR); +} + +bool Uri::IsOpaque() +{ + if (uriString_.empty()) { + return false; + } + + return !IsHierarchical(); +} + +bool Uri::IsAbsolute() +{ + if (uriString_.empty()) { + return false; + } + + return !IsRelative(); +} + +bool Uri::IsRelative() +{ + if (uriString_.empty()) { + return false; + } + + // Note: We return true if the index is 0 + return FindSchemeSeparator() == NOT_FOUND; +} + +bool Uri::Equals(const Uri& other) const +{ + return ToString() == other.ToString(); +} + +int Uri::CompareTo(const Uri& other) const +{ + return ToString().compare(other.ToString()); +} + +string Uri::ToString() const +{ + return uriString_; +} + +bool Uri::operator==(const Uri& other) const +{ + return ToString() == other.ToString(); +} + +bool Uri::Marshalling(Parcel& parcel) const +{ + if (IsAsciiString(uriString_)) { + return parcel.WriteString16(Str8ToStr16(uriString_)); + } + + return false; +} + +Uri* Uri::Unmarshalling(Parcel& parcel) +{ + return new Uri(Str16ToStr8(parcel.ReadString16())); +} +} // namespace OHOS