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
RemoveErrorObserver()62 void ApplicationDataManager::RemoveErrorObserver()
63 {
64 errorObserver_ = nullptr;
65 }
66
NotifyExceptionObject(const AppExecFwk::ErrorObject & errorObj)67 bool ApplicationDataManager::NotifyExceptionObject(const AppExecFwk::ErrorObject &errorObj)
68 {
69 if (errorObserver_) {
70 errorObserver_->OnExceptionObject(errorObj);
71 return true;
72 }
73
74 // if apprecovery is enabled, we could callback to save current state
75 // and restart as developer wants
76 return AppRecovery::GetInstance().TryRecoverApp(StateReason::JS_ERROR);
77 }
78
NotifyCJExceptionObject(const AppExecFwk::ErrorObject & errorObj)79 bool ApplicationDataManager::NotifyCJExceptionObject(const AppExecFwk::ErrorObject &errorObj)
80 {
81 TAG_LOGD(AAFwkTag::APPKIT, "Notify Exception error observer come");
82 if (errorObserver_) {
83 errorObserver_->OnExceptionObject(errorObj);
84 return true;
85 }
86
87 // if apprecovery is enabled, we could callback to save current state
88 // and restart as developer wants
89 return AppRecovery::GetInstance().TryRecoverApp(StateReason::CJ_ERROR);
90 }
91 } // namespace AppExecFwk
92 } // namespace OHOS
93