• 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 
16 #include <memory>
17 #include <new>
18 #include <cstdint>
19 #include "skill.h"
20 #include "want.h"
21 #include "app_details_filter.h"
22 #include "app_details_meta_item.h"
23 #include "app_details_rdb_data_define.h"
24 #include "app_details_rdb_const_define.h"
25 #include "app_details_rdb_data_manager.h"
26 #include "app_domain_verify_error.h"
27 #include "domain_url_util.h"
28 #include "app_domain_verify_hilog.h"
29 #include "filter_define.h"
30 #include "lru_cache_util.h"
31 #include "app_details_data_mgr.h"
32 
33 
34 namespace OHOS {
35 namespace AppDomainVerify {
36 const static std::string OHOS_WANT_ACTION_APPDETAILS = "ohos.want.action.appdetail";
37 const static int64_t CACHE_TIME_S = 300;
38 const static int MAX_CACHE_CNT = 10;
AppDetailsDataMgr()39 AppDetailsDataMgr::AppDetailsDataMgr()
40 {
41     lruCache_ = std::make_shared<LruCacheUtil<std::string, std::string>>(MAX_CACHE_CNT);
42     rdbMgr_ = std::make_shared<AppDetailsRdbDataMgr>(false);
43 };
44 
~AppDetailsDataMgr()45 AppDetailsDataMgr::~AppDetailsDataMgr()
46 {};
47 
QueryAppDetailsWant(const std::string & url,AAFwk::Want & want,std::string & bundleName)48 int AppDetailsDataMgr::QueryAppDetailsWant(const std::string &url, AAFwk::Want& want, std::string& bundleName)
49 {
50     APP_DOMAIN_VERIFY_HILOGI(APP_DOMAIN_VERIFY_MGR_MODULE_SERVICE, "call, url:%{private}s", url.c_str());
51     std::string bundleNameQuery;
52     do {
53         if (QueryAppDetailsWantByCache(url, bundleNameQuery)) {
54             APP_DOMAIN_VERIFY_HILOGI(APP_DOMAIN_VERIFY_MGR_MODULE_SERVICE, "query success from cache");
55             break;
56         }
57         if (QueryAppDetailsWantByRdb(url, bundleNameQuery)) {
58             APP_DOMAIN_VERIFY_HILOGI(APP_DOMAIN_VERIFY_MGR_MODULE_SERVICE, "query success from rdb");
59             break;
60         }
61         APP_DOMAIN_VERIFY_HILOGI(APP_DOMAIN_VERIFY_MGR_MODULE_SERVICE, "query fail.");
62         return QUERY_FAIL;
63     } while (false);
64     AddInfoToWant(want, bundleNameQuery);
65     bundleName = bundleNameQuery;
66     return QUERY_SUCC;
67 };
68 
QueryAppDetailsWantByCache(const std::string & url,std::string & bundleName)69 bool AppDetailsDataMgr::QueryAppDetailsWantByCache(const std::string& url, std::string& bundleName)
70 {
71     int64_t currTime = std::chrono::time_point_cast<std::chrono::seconds>(std::chrono::system_clock::now())
72                           .time_since_epoch()
73                           .count();
74     if (currTime - cacheBeginTime_ > CACHE_TIME_S) {
75         cacheBeginTime_ = currTime;
76         lruCache_->Clear();
77         return false;
78     }
79     std::string value;
80     if (lruCache_->Get(url, value)) {
81         bundleName = value;
82         return true;
83     }
84     return false;
85 };
86 
QueryAppDetailsWantByRdb(const std::string & url,std::string & bundleName)87 bool AppDetailsDataMgr::QueryAppDetailsWantByRdb(const std::string& url, std::string& bundleName)
88 {
89     std::vector<AppDetailsRdbItem> rdbDetails;
90     std::string domain = UrlUtil::GetHost(url);
91     if (!rdbMgr_->QueryDataByDomain(APP_DETAILS_TABLE, domain, rdbDetails)) {
92         APP_DOMAIN_VERIFY_HILOGE(APP_DOMAIN_VERIFY_MODULE_EXTENSION, "query fail.");
93         return false;
94     }
95     std::vector<AppDetailInfo> appDetails;
96     std::vector<AppDetailInfo> appDetailsRet;
97     for (auto& detail : rdbDetails) {
98         AppDetailInfo info;
99         info.bundleName = detail.bundleName;
100         info.skillUri.host = detail.domain;
101         info.skillUri.scheme = detail.scheme;
102         if (detail.pathType == PATH) {
103             info.skillUri.path = detail.path;
104         }
105         if (detail.pathType == PATH_START_WITH) {
106             info.skillUri.pathStartWith = detail.path;
107         }
108         if (detail.pathType == PATH_REGEX) {
109             info.skillUri.pathRegex = detail.path;
110         }
111         appDetails.emplace_back(info);
112     }
113     if (!detailsFilter_->Filter(appDetails, appDetailsRet, url)) {
114         return false;
115     }
116     bundleName = appDetailsRet.front().bundleName;
117     lruCache_->Put(url, appDetailsRet.front().bundleName);
118     return true;
119 };
120 
AddInfoToWant(AAFwk::Want & want,const std::string & bundleName)121 void AppDetailsDataMgr::AddInfoToWant(AAFwk::Want& want, const std::string& bundleName)
122 {
123     MetaItem info;
124     if (agWantUrl_.empty()) {
125         rdbMgr_->QueryMetaData(APP_DETAILS_TABLE, info);
126         agWantUrl_ = info.tableExtInfo;
127     }
128     want.SetAction(OHOS_WANT_ACTION_APPDETAILS);
129     want.SetUri(agWantUrl_ + bundleName);
130     return;
131 };
132 
133 }
134 }