• 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 #include "ndef_har_dispatch.h"
16 
17 #include "external_deps_proxy.h"
18 #include "iservice_registry.h"
19 #include "ndef_har_data_parser.h"
20 #include "tag_ability_dispatcher.h"
21 #include "ability_manager_client.h"
22 #include "loghelper.h"
23 #include "bundle_mgr_interface.h"
24 #include "if_system_ability_manager.h"
25 
26 namespace OHOS {
27 namespace NFC {
28 namespace TAG {
29 const int USER_ID = 100;
30 const int BUNDLE_MGR_SERVICE_SYS_ABILITY_ID = 401;
31 using namespace OHOS::NFC::KITS;
32 
33 std::string uri_ {};
34 std::string browserBundleName_ {};
35 
NdefHarDispatch()36 NdefHarDispatch::NdefHarDispatch()
37 {
38 }
39 
GetInstance()40 NdefHarDispatch& NdefHarDispatch::GetInstance()
41 {
42     static NdefHarDispatch instance;
43     return instance;
44 }
45 
GetBundleMgrProxy()46 sptr<AppExecFwk::IBundleMgr> NdefHarDispatch::GetBundleMgrProxy()
47 {
48     sptr<ISystemAbilityManager> systemAbilityManager =
49         SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
50     if (!systemAbilityManager) {
51         ErrorLog("GetBundleMgrProxy, systemAbilityManager is null");
52         return nullptr;
53     }
54     sptr<IRemoteObject> remoteObject = systemAbilityManager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
55     if (!remoteObject) {
56         ErrorLog("GetBundleMgrProxy, remoteObject is null");
57         return nullptr;
58     }
59     return iface_cast<AppExecFwk::IBundleMgr>(remoteObject);
60 }
61 
62 /* Implicit matching, using mimetype to pull up app */
DispatchMimeType(const std::string & type,std::shared_ptr<KITS::TagInfo> tagInfo)63 bool NdefHarDispatch::DispatchMimeType(const std::string &type, std::shared_ptr<KITS::TagInfo> tagInfo)
64 {
65     if (type.empty() || tagInfo == nullptr) {
66         ErrorLog("NdefHarDispatch::DispatchMimeType type is empty");
67         return false;
68     }
69     AAFwk::Want want;
70     want.SetType(type);
71     ExternalDepsProxy::GetInstance().SetWantExtraParam(tagInfo, want);
72     if (GetBundleMgrProxy() == nullptr) {
73         ErrorLog("NdefHarDispatch::DispatchMimeType GetBundleMgrProxy is nullptr");
74         return false;
75     }
76     bool withDefault = false;
77     auto abilityInfoFlag = AppExecFwk::AbilityInfoFlag::GET_ABILITY_INFO_DEFAULT
78         | AppExecFwk::AbilityInfoFlag::GET_ABILITY_INFO_WITH_SKILL_URI
79         | AppExecFwk::AbilityInfoFlag::GET_ABILITY_INFO_WITH_METADATA;
80     std::vector<AbilityInfo> abilityInfos;
81     std::vector<ExtensionAbilityInfo> extensionInfos;
82     bool findDefaultApp = false;
83     if (!GetBundleMgrProxy()->ImplicitQueryInfos(
84         want, abilityInfoFlag, USER_ID, withDefault, abilityInfos, extensionInfos, findDefaultApp)) {
85         ErrorLog("NdefHarDispatch::DispatchMimeType ImplicitQueryInfos false");
86         return false;
87     }
88     int32_t errCode = AAFwk::AbilityManagerClient::GetInstance()->StartAbility(want);
89     if (errCode) {
90         ErrorLog("NdefHarDispatch::DispatchMimeType call StartAbility fail. ret = %{public}d", errCode);
91         return false;
92     }
93     return true;
94 }
95 
96 /* Call GetLaunchWantForBundle through bundlename to obtain the want and pull up the app */
DispatchBundleAbility(const std::string & harPackage,std::shared_ptr<KITS::TagInfo> tagInfo,const std::string & mimeType,const std::string & uri)97 bool NdefHarDispatch::DispatchBundleAbility(const std::string &harPackage, std::shared_ptr<KITS::TagInfo> tagInfo,
98                                             const std::string &mimeType, const std::string &uri)
99 {
100     if (harPackage.empty()) {
101         ErrorLog("NdefHarDispatch::DispatchBundleAbility harPackage is empty");
102         return false;
103     }
104     std::string harPackageString = NfcSdkCommon::HexStringToAsciiString(harPackage);
105     AAFwk::Want want;
106     if (GetBundleMgrProxy() == nullptr) {
107         ErrorLog("NdefHarDispatch::GetBundleMgrProxy is nullptr");
108         return false;
109     }
110     int32_t errCode = GetBundleMgrProxy()->GetLaunchWantForBundle(harPackageString, want, USER_ID);
111     if (errCode) {
112         ErrorLog("NdefHarDispatch::GetLaunchWantForBundle fail. ret = %{public}d", errCode);
113         return false;
114     }
115     if (!mimeType.empty() && tagInfo != nullptr) {
116         want.SetType(mimeType);
117         ExternalDepsProxy::GetInstance().SetWantExtraParam(tagInfo, want);
118     }
119     if (uri.size() > 0) {
120         want.SetUri(uri);
121     }
122     errCode = AAFwk::AbilityManagerClient::GetInstance()->StartAbility(want);
123     if (errCode) {
124         ErrorLog("NdefHarDispatch::DispatchBundleAbility call StartAbility fail. ret = %{public}d", errCode);
125         return false;
126     }
127     return true;
128 }
129 
DispatchUriToBundleAbility(const std::string & uri)130 bool NdefHarDispatch::DispatchUriToBundleAbility(const std::string &uri)
131 {
132     if (uri.empty()) {
133         ErrorLog("NdefHarDispatch::DispatchUriToBundleAbility uri is empty");
134         return false;
135     }
136     bool canOpen = false;
137     if (GetBundleMgrProxy() == nullptr) {
138         ErrorLog("NdefHarDispatch::DispatchUriToBundleAbility GetBundleMgrProxy is nullptr");
139         return false;
140     }
141     int32_t errCode = GetBundleMgrProxy()->CanOpenLink(uri, canOpen);
142     if (!errCode && canOpen) {
143         InfoLog("NdefHarDispatch::DispatchUriToBundleAbility CanOpenLink");
144     }
145     ErrorLog("CanOpenLink fail. errCode = %{public}d, canOpen = %{public}d", errCode, canOpen);
146     return false;
147 }
148 
149 /* Pulling web page links through browser */
DispatchWebLink(const std::string & webAddress,const std::string & browserBundleName)150 bool NdefHarDispatch::DispatchWebLink(const std::string &webAddress, const std::string &browserBundleName)
151 {
152     std::unique_lock<std::shared_mutex> guard(mutex_);
153     InfoLog("NdefHarDispatch::DispatchWebLink enter");
154     if (webAddress.empty() || browserBundleName.empty()) {
155         ErrorLog("NdefHarDispatch::DispatchWebLink is empty");
156         return false;
157     }
158     uri_ = webAddress;
159     browserBundleName_ = browserBundleName;
160     ExternalDepsProxy::GetInstance().PublishNfcNotification(NFC_BROWSER_NOTIFICATION_ID, uri_, 0);
161     return true;
162 }
163 
OnBrowserOpenLink()164 void NdefHarDispatch::OnBrowserOpenLink()
165 {
166     std::unique_lock<std::shared_mutex> guard(mutex_);
167     InfoLog("NdefHarDispatch::OnBrowserOpenLink, %{public}s, %{public}s",
168         NfcSdkCommon::CodeMiddlePart(browserBundleName_).c_str(), NfcSdkCommon::CodeMiddlePart(uri_).c_str());
169     AAFwk::Want want;
170     const std::string ABILITY_NAME = "MainAbility";
171     const std::string ACTION_NAME = "ohos.want.action.viewData";
172     const std::string ENTITY_NAME = "entity.system.browsable";
173     want.SetElementName(browserBundleName_, ABILITY_NAME);
174     want.SetAction(ACTION_NAME);
175     want.SetUri(uri_);
176     want.AddEntity(ENTITY_NAME);
177     int32_t errCode = AAFwk::AbilityManagerClient::GetInstance()->StartAbility(want);
178     if (errCode) {
179         ErrorLog("NdefHarDispatch::DispatchWebLink call StartAbility fail. ret = %{public}d", errCode);
180     }
181 }
182 } // namespace TAG
183 } // namespace NFC
184 } // namespace OHOS