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