1 /*
2  * Copyright (c) 2020-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 #ifndef OHOS_ACELITE_FATAL_HANDLER_H
17 #define OHOS_ACELITE_FATAL_HANDLER_H
18 
19 #include "component.h"
20 #include "gfx_utils/list.h"
21 #include "js_ability.h"
22 #include "non_copyable.h"
23 #include "ui_view.h"
24 
25 namespace OHOS {
26 namespace ACELite {
27 class FatalHandler final : public MemoryHeap {
28 public:
29     ACE_DISALLOW_COPY_AND_MOVE(FatalHandler);
30     static FatalHandler& GetInstance();
31     static bool IsErrorHittedWrapper();
32     static bool IsAppExitingWrapper();
33     const char* GetErrorStr(int errorCode) const;
34     void RegisterFatalHandler(JSAbility *ability);
35     void SetFatalError(int errorCode);
36     void HandleFatalError(int errorCode);
37     void HandleFatalInternal();
38     void CleanUpFatalResource();
39     bool IsJSRuntimeFatal() const;
40     bool IsFatalErrorHitted() const;
41     bool IsFatalErrorHandling() const;
42     bool IsFatalErrorHandleDone() const;
43     void AttachComponentNode(Component* component);
44     void DetachComponentNode(const Component* component);
45     void SetTEHandlingFlag(bool flag);
46     bool IsTEHandling() const;
47     void ResetRendering();
48     void SetExitingFlag(bool flag);
49     bool IsAppExiting() const;
50     // when the page is attached or destroyed by state machine, the fatal handler will be got notified
51     void SetCurrentPageRootView(UIView *pageRoot);
52     void DumpFatalTrace(int errorCode) const;
53     uint16_t GetComponentCount() const;
54     void NotifyVisibleStatusChange(bool isVisible) const;
55     // define all fatal error below, please note the jerry fatal defines, avoid conflicts
56     static const int ERR_INVALID = 0;
57     static const int ERR_NATIVE_OUT_OF_MEMORY = 200;
58     static const int ERR_READ_FWK_FILE_FAILED = 201;
59     static const int ERR_EVAL_FWK_FAILED = 202;
60     static const int ERR_READ_JS_FILE_FAILED = 203;
61     static const int ERR_EVAL_JS_FAILED = 204;
62 
63 private:
FatalHandler()64     FatalHandler()
65         : jsAbility_(nullptr),
66           pageRootView_(nullptr),
67           componentNodes_(),
68           fatalErrorCode_(0),
69           isRecycling_(false),
70           isFatalHandled_(false),
71           isTEHandling_(false),
72           isExiting_(false) {}
~FatalHandler()73     ~FatalHandler() {}
74     bool IsErrorSupported(int errorCode) const;
75     void RecycleComponents();
76     JSAbility *jsAbility_;
77     UIView *pageRootView_;
78     List<Component *> componentNodes_;
79     int fatalErrorCode_;
80     bool isRecycling_;
81     bool isFatalHandled_;
82     bool isTEHandling_;
83     bool isExiting_;
84 };
85 } // namespace ACELite
86 } // namespace OHOS
87 #endif // OHOS_ACELITE_FATAL_HANDLER_H
88