1 /*
2 * Copyright (c) 2022-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 "application_data_manager.h"
17
18 #include "app_recovery.h"
19 #include "hilog_tag_wrapper.h"
20
21 namespace OHOS {
22 namespace AppExecFwk {
ApplicationDataManager()23 ApplicationDataManager::ApplicationDataManager() {}
24
~ApplicationDataManager()25 ApplicationDataManager::~ApplicationDataManager() {}
26
GetInstance()27 ApplicationDataManager &ApplicationDataManager::GetInstance()
28 {
29 static ApplicationDataManager manager;
30 return manager;
31 }
32
AddErrorObserver(const std::shared_ptr<IErrorObserver> & observer)33 void ApplicationDataManager::AddErrorObserver(const std::shared_ptr<IErrorObserver> &observer)
34 {
35 errorObserver_ = observer;
36 }
37
NotifyUnhandledException(const std::string & errMsg)38 bool ApplicationDataManager::NotifyUnhandledException(const std::string &errMsg)
39 {
40 if (errorObserver_) {
41 errorObserver_->OnUnhandledException(errMsg);
42 return true;
43 }
44
45 // if apprecovery is enabled, we could callback to save current state
46 // and restart as developer wants
47 return AppRecovery::GetInstance().TryRecoverApp(StateReason::JS_ERROR);
48 }
49
NotifyCJUnhandledException(const std::string & errMsg)50 bool ApplicationDataManager::NotifyCJUnhandledException(const std::string &errMsg)
51 {
52 if (errorObserver_) {
53 errorObserver_->OnUnhandledException(errMsg);
54 return true;
55 }
56
57 // if apprecovery is enabled, we could callback to save current state
58 // and restart as developer wants
59 return AppRecovery::GetInstance().TryRecoverApp(StateReason::CJ_ERROR);
60 }
61
NotifyETSUnhandledException(const std::string & errMsg)62 bool ApplicationDataManager::NotifyETSUnhandledException(const std::string &errMsg)
63 {
64 if (errorObserver_) {
65 errorObserver_->OnUnhandledException(errMsg);
66 return true;
67 }
68 return AppRecovery::GetInstance().TryRecoverApp(StateReason::JS_ERROR);
69 }
70
RemoveErrorObserver()71 void ApplicationDataManager::RemoveErrorObserver()
72 {
73 errorObserver_ = nullptr;
74 }
75
NotifyExceptionObject(const AppExecFwk::ErrorObject & errorObj)76 bool ApplicationDataManager::NotifyExceptionObject(const AppExecFwk::ErrorObject &errorObj)
77 {
78 if (errorObserver_) {
79 errorObserver_->OnExceptionObject(errorObj);
80 return true;
81 }
82
83 // if apprecovery is enabled, we could callback to save current state
84 // and restart as developer wants
85 return AppRecovery::GetInstance().TryRecoverApp(StateReason::JS_ERROR);
86 }
87
NotifyCJExceptionObject(const AppExecFwk::ErrorObject & errorObj)88 bool ApplicationDataManager::NotifyCJExceptionObject(const AppExecFwk::ErrorObject &errorObj)
89 {
90 TAG_LOGD(AAFwkTag::APPKIT, "Notify Exception error observer come");
91 if (errorObserver_) {
92 errorObserver_->OnExceptionObject(errorObj);
93 return true;
94 }
95
96 // if apprecovery is enabled, we could callback to save current state
97 // and restart as developer wants
98 return AppRecovery::GetInstance().TryRecoverApp(StateReason::CJ_ERROR);
99 }
100
NotifyETSExceptionObject(const AppExecFwk::ErrorObject & errorObj)101 bool ApplicationDataManager::NotifyETSExceptionObject(const AppExecFwk::ErrorObject &errorObj)
102 {
103 TAG_LOGD(AAFwkTag::APPKIT, "Notify Exception error observer come");
104 if (errorObserver_) {
105 errorObserver_->OnExceptionObject(errorObj);
106 return true;
107 }
108 return AppRecovery::GetInstance().TryRecoverApp(StateReason::JS_ERROR);
109 }
110 } // namespace AppExecFwk
111 } // namespace OHOS
112