• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 "frameworks/bridge/js_frontend/frontend_delegate.h"
17 
18 #include <string>
19 
20 #include "core/common/ace_application_info.h"
21 #include "frameworks/bridge/common/utils/utils.h"
22 
23 namespace OHOS::Ace::Framework {
24 namespace {
25 
26 const char SLASH = '/';
27 const char SLASHSTR[] = "/";
28 
ParseFileUri(const RefPtr<AssetManager> & assetManager,const std::string & fileUri,std::string & assetsFilePath)29 bool ParseFileUri(const RefPtr<AssetManager>& assetManager, const std::string& fileUri, std::string& assetsFilePath)
30 {
31     if (!assetManager || fileUri.empty() || (fileUri.length() > PATH_MAX)) {
32         return false;
33     }
34 
35     std::string fileName;
36     std::string filePath;
37     size_t slashPos = fileUri.find_last_of(SLASH);
38     if (slashPos == std::string::npos) {
39         fileName = fileUri;
40     } else {
41         fileName = fileUri.substr(slashPos + 1);
42         filePath = fileUri.substr(0, slashPos + 1);
43     }
44 
45     if (StartWith(filePath, SLASHSTR)) {
46         filePath = filePath.substr(1);
47     }
48 
49     std::vector<std::string> files;
50     assetManager->GetAssetList(filePath, files);
51 
52     bool fileExist = false;
53     for (const auto& file : files) {
54         bool startWithSlash = StartWith(file, SLASHSTR);
55         if ((startWithSlash && (file.substr(1) == fileName)) || (!startWithSlash && (file == fileName))) {
56             assetsFilePath = filePath + file;
57             fileExist = true;
58             break;
59         }
60     }
61 
62     return fileExist;
63 }
64 
65 } // namespace
66 
67 template<typename T>
GetResourceData(const std::string & fileUri,T & content)68 bool FrontendDelegate::GetResourceData(const std::string& fileUri, T& content)
69 {
70     std::string targetFilePath;
71     if (!ParseFileUri(assetManager_, fileUri, targetFilePath)) {
72         LOGE("GetResourceData parse file uri failed.");
73         return false;
74     }
75     if (!GetAssetContentAllowEmpty(assetManager_, targetFilePath, content)) {
76         LOGE("GetResourceData GetAssetContent failed.");
77         return false;
78     }
79 
80     return true;
81 }
82 
83 template<typename T>
GetResourceData(const std::string & fileUri,T & content,std::string & ami)84 bool FrontendDelegate::GetResourceData(const std::string& fileUri, T& content, std::string& ami)
85 {
86     std::string targetFilePath;
87     if (!ParseFileUri(assetManager_, fileUri, targetFilePath)) {
88         LOGE("GetResourceData parse file uri failed.");
89         return false;
90     }
91     ami = assetManager_->GetAssetPath(targetFilePath) + targetFilePath;
92     if (!GetAssetContentAllowEmpty(assetManager_, targetFilePath, content)) {
93         LOGE("GetResourceData GetAssetContent failed.");
94         return false;
95     }
96 
97     return true;
98 }
99 
100 template<typename T>
GetResourceData(const std::string & fileUri,const RefPtr<AssetManager> & assetManager,T & content)101 bool FrontendDelegate::GetResourceData(const std::string& fileUri, const RefPtr<AssetManager>& assetManager,
102     T& content)
103 {
104     std::string targetFilePath;
105     if (!ParseFileUri(assetManager, fileUri, targetFilePath)) {
106         LOGE("GetResourceData parse file uri failed.");
107         return false;
108     }
109     if (!GetAssetContentAllowEmpty(assetManager, targetFilePath, content)) {
110         LOGE("GetResourceData GetAssetContent failed.");
111         return false;
112     }
113 
114     return true;
115 }
116 
117 template bool FrontendDelegate::GetResourceData(const std::string& fileUri, std::string& content);
118 template bool FrontendDelegate::GetResourceData(const std::string& fileUri, std::vector<uint8_t>& content);
119 template bool FrontendDelegate::GetResourceData(const std::string& fileUri, std::vector<uint8_t>& content,
120                                                 std::string& ami);
121 template bool FrontendDelegate::GetResourceData(const std::string& fileUri, const RefPtr<AssetManager>& assetManager,
122     std::vector<uint8_t>& content);
123 
124 } // namespace OHOS::Ace::Framework
125