• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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)32     SourceMapOperator(const std::string bundleName)
33         : bundleName_(bundleName), 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->hasFile_, 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 (hasFile_ && sourceMapObj_ != nullptr) {
61             return sourceMapObj_->TranslateBySourceMap(stackStr);
62         } else {
63             return NOT_FOUNDMAP + stackStr;
64         }
65     }
66 
TranslateUrlPositionBySourceMap(std::string & url,int & line,int & column,std::string & packageName)67     bool TranslateUrlPositionBySourceMap(std::string& url, int& line, int& column, std::string& packageName)
68     {
69         if (sourceMapObj_ == nullptr) {
70             return false;
71         }
72         return sourceMapObj_->TranslateUrlPositionBySourceMap(url, line, column, packageName);
73     }
74 
GetInitStatus()75     bool GetInitStatus() const
76     {
77         return (initStatus_ == InitStatus::EXECUTED_SUCCESSFULLY);
78     }
79 
SetInitStatus(InitStatus value)80     void SetInitStatus(InitStatus value)
81     {
82         initStatus_ = value;
83     }
84 
GetSourceMapObj()85     std::shared_ptr<SourceMap> GetSourceMapObj() const
86     {
87         return sourceMapObj_;
88     }
89 
GetHasFile()90     bool GetHasFile() const
91     {
92         return hasFile_;
93     }
94 private:
95     const std::string bundleName_;
96     bool hasFile_ = false;
97     std::shared_ptr<SourceMap> sourceMapObj_;
98     InitStatus initStatus_;
99 };
100 } // namespace JsEnv
101 } // namespace OHOS
102 #endif // OHOS_ABILITY_JS_ENVIRONMENT_SOURCE_MAP_OPERATOR_H
103