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