1 /* 2 * Copyright (c) 2023 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 #ifndef OHOS_ABILITY_JS_ENVIRONMENT_SOURCE_MAP_OPERATOR_H 17 #define OHOS_ABILITY_JS_ENVIRONMENT_SOURCE_MAP_OPERATOR_H 18 #include <memory> 19 #include <set> 20 #include <string> 21 22 #include "ffrt.h" 23 #include "source_map.h" 24 25 namespace OHOS { 26 namespace JsEnv { 27 namespace { 28 enum InitStatus { NOT_EXECUTED, EXECUTED_SUCCESSFULLY }; 29 } 30 class SourceMapOperator : public std::enable_shared_from_this<SourceMapOperator> { 31 public: SourceMapOperator(const std::string bundleName,bool isModular,bool hasFile)32 SourceMapOperator(const std::string bundleName, bool isModular, bool hasFile) 33 : bundleName_(bundleName), isModular_(isModular), hasFile_(hasFile), initStatus_(NOT_EXECUTED) {} 34 35 ~SourceMapOperator() = default; 36 InitSourceMap()37 void InitSourceMap() 38 { 39 sourceMapObj_ = std::make_shared<SourceMap>(); 40 41 auto init = [weak = weak_from_this()]() { 42 auto sourceMapOperator = weak.lock(); 43 if (sourceMapOperator != nullptr && sourceMapOperator->sourceMapObj_ != nullptr) { 44 std::vector<std::string> hapList; 45 sourceMapOperator->sourceMapObj_->GetHapPath(sourceMapOperator->bundleName_, hapList); 46 for (auto &hapInfo : hapList) { 47 if (!hapInfo.empty()) { 48 sourceMapOperator->sourceMapObj_->Init(sourceMapOperator->isModular_, hapInfo); 49 } 50 } 51 sourceMapOperator->initStatus_ = EXECUTED_SUCCESSFULLY; 52 } 53 }; 54 55 ffrt::submit(init, {}, {}, ffrt::task_attr().qos(ffrt::qos_user_initiated)); 56 } 57 TranslateBySourceMap(const std::string & stackStr)58 std::string TranslateBySourceMap(const std::string& stackStr) 59 { 60 if (sourceMapObj_ == nullptr) { 61 return ""; 62 } 63 if (hasFile_) { 64 return sourceMapObj_->TranslateBySourceMap(stackStr); 65 } else { 66 return NOT_FOUNDMAP + stackStr; 67 } 68 } 69 TranslateUrlPositionBySourceMap(std::string & url,int & line,int & column)70 bool TranslateUrlPositionBySourceMap(std::string& url, int& line, int& column) 71 { 72 if (sourceMapObj_ == nullptr) { 73 return false; 74 } 75 return sourceMapObj_->TranslateUrlPositionBySourceMap(url, line, column); 76 } 77 GetInitStatus()78 bool GetInitStatus() const 79 { 80 return (initStatus_ == InitStatus::EXECUTED_SUCCESSFULLY); 81 } 82 GetSourceMapObj()83 std::shared_ptr<SourceMap> GetSourceMapObj() const 84 { 85 return sourceMapObj_; 86 } 87 88 private: 89 const std::string bundleName_; 90 bool isModular_ = false; 91 bool hasFile_ = false; 92 std::shared_ptr<SourceMap> sourceMapObj_; 93 InitStatus initStatus_; 94 }; 95 } // namespace JsEnv 96 } // namespace OHOS 97 #endif // OHOS_ABILITY_JS_ENVIRONMENT_SOURCE_MAP_OPERATOR_H 98