1 /* 2 * Copyright (C) 2024 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 #ifndef NDEF_HAR_DATA_PARSER_H 16 #define NDEF_HAR_DATA_PARSER_H 17 18 #include <string> 19 #include <vector> 20 #include "ndef_message.h" 21 #include "ndef_har_dispatch.h" 22 #include "inci_tag_interface.h" 23 #include "taginfo.h" 24 #include "nfc_service.h" 25 26 namespace OHOS { 27 namespace NFC { 28 namespace TAG { 29 using namespace OHOS::NFC::KITS; 30 31 enum RecordsType { 32 TYPE_RTP_UNKNOWN = 0, 33 TYPE_RTP_SCHEME_TEL, 34 TYPE_RTP_SCHEME_SMS, 35 TYPE_RTP_SCHEME_HTTP_WEB_URL, 36 TYPE_RTP_SCHEME_MAIL, 37 TYPE_RTP_SCHEME_OTHER, 38 TYPE_RTP_MIME_TEXT_PLAIN, 39 TYPE_RTP_MIME_TEXT_VCARD, 40 TYPE_RTP_MIME_OTHER 41 }; 42 43 const std::string HTTP_PREFIX = "http"; 44 const std::string TEL_PREFIX = "tel"; 45 const std::string SMS_PREFIX = "sms"; 46 const std::string SMSTO_PREFIX = "smsto"; 47 const std::string MAIL_PREFIX = "mailto"; 48 const std::string TEXT_PLAIN = "text/plain"; 49 const std::string TEXT_VCARD = "text/vcard"; 50 const int MIME_MAX_LENGTH = 128; 51 const int URI_MAX_LENGTH = 2048; 52 const int RECORD_LIST_MAX_SIZE = 20; 53 54 class NdefHarDataParser { 55 public: 56 static NdefHarDataParser &GetInstance(); 57 void Initialize(std::weak_ptr<NfcService> nfcService, std::weak_ptr<NCI::INciTagInterface> nciTagProxy, 58 std::weak_ptr<NCI::INciNfccInterface> nciNfccProxy); 59 bool TryNdef(const std::string &msg, const std::shared_ptr<KITS::TagInfo> &tagInfo); 60 61 private: 62 NdefHarDataParser(); ~NdefHarDataParser()63 ~NdefHarDataParser() {} 64 std::string GetUriPayload(const std::shared_ptr<NdefRecord> &record); 65 std::string GetUriPayload(const std::shared_ptr<NdefRecord> &record, bool isSmartPoster); 66 bool DispatchByHarBundleName( 67 const std::vector<std::shared_ptr<NdefRecord>> &records, const std::shared_ptr<KITS::TagInfo> &tagInfo); 68 bool ParseHarPackage(std::vector<std::string> harPackages, const std::shared_ptr<KITS::TagInfo> &tagInfo, 69 const std::string &mimeType, const std::string &uri); 70 bool DispatchAllHarPackage(const std::vector<std::string> &harPackages, 71 const std::shared_ptr<KITS::TagInfo> &tagInfo, const std::string &mimeType, const std::string &uri); 72 void ParseMimeTypeAndStr(const std::vector<std::shared_ptr<NdefRecord>> &records); 73 std::vector<std::string> ExtractHarPackages(const std::vector<std::shared_ptr<NdefRecord>> &records); 74 std::string CheckForHar(const std::shared_ptr<NdefRecord> &record); 75 bool IsOtherPlatformAppType(const std::string &appType); 76 bool StartsWith(const std::string &str, const std::string &prefix); 77 void ParseRecordsProperty(const std::vector<std::shared_ptr<NdefRecord>> &records); 78 bool DispatchByAppLinkMode(const std::shared_ptr<KITS::TagInfo> &tagInfo); 79 bool HandleUnsupportSchemeType(const std::vector<std::shared_ptr<NdefRecord>> &records); 80 bool DispatchMimeToBundleAbility(const std::shared_ptr<KITS::TagInfo> &tagInfo); 81 bool DispatchValidNdef( 82 const std::vector<std::shared_ptr<NdefRecord>> &records, const std::shared_ptr<KITS::TagInfo> &tagInfo); 83 void ClearNdefDispatchParam(); 84 85 std::shared_ptr<NdefHarDispatch> ndefHarDispatch_ {nullptr}; 86 std::weak_ptr<NCI::INciTagInterface> nciTagProxy_ {}; 87 std::weak_ptr<NCI::INciNfccInterface> nciNfccProxy_ {}; 88 RecordsType schemeType_ {RecordsType::TYPE_RTP_UNKNOWN}; 89 std::string uriAddress_ {}; 90 std::string uriSchemeValue_ {}; 91 std::vector<std::pair<RecordsType, std::string>> mimeTypeVec_ {}; 92 93 std::weak_ptr<NfcService> nfcService_ {}; 94 std::mutex mutex_ {}; 95 bool isInitialized_ = false; 96 }; 97 } // namespace TAG 98 } // namespace NFC 99 } // namespace OHOS 100 #endif // NDEF_HAR_DATA_PARSER_H