1 /*
2  * Copyright (c) 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 #ifndef RESOURCE_SCHEDULE_SERVICE_RESSCHED_COMMON_OOBE_MANAGER_H
17 #define RESOURCE_SCHEDULE_SERVICE_RESSCHED_COMMON_OOBE_MANAGER_H
18 
19 #include "data_ability_observer_stub.h"
20 #include "errors.h"
21 #include "mutex"
22 #include "ioobe_task.h"
23 #include <functional>
24 #include <vector>
25 
26 namespace OHOS {
27 namespace ResourceSchedule {
28 
29 class OOBEManager {
30 public:
31     static OOBEManager& GetInstance();
32     bool SubmitTask(const std::shared_ptr<IOOBETask>& task);
33     void StartListen();
34     ErrCode UnregisterObserver();
35     bool GetOOBValue();
36     void OnReceiveDataShareReadyCallBack();
37 
38 private:
39     class ResDataAbilityObserver : public AAFwk::DataAbilityObserverStub {
40     public:
41         ResDataAbilityObserver() = default;
42         ~ResDataAbilityObserver()  = default;
43         void OnChange() override;
44 
45         using UpdateFunc = std::function<void()>;
46         void SetUpdateFunc(const UpdateFunc& func);
47     private:
48         UpdateFunc update_ = nullptr;
49     };
50 
51     bool g_oobeValue = false;
52     static std::recursive_mutex mutex_;
53     static std::vector<std::shared_ptr<IOOBETask>> oobeTasks_;
54     static std::vector<std::function<void()>> dataShareFunctions_;
55     static sptr<OOBEManager::ResDataAbilityObserver> observer_;
56     OOBEManager();
57     ~OOBEManager();
58     void Initialize();
59     ErrCode RegisterObserver(const std::string& key, const ResDataAbilityObserver::UpdateFunc& func);
60     void ReRegisterObserver(const std::string& key, const ResDataAbilityObserver::UpdateFunc& func);
61 };
62 } // ResourceSchedule
63 } // OHOS
64 #endif //RESOURCE_SCHEDULE_SERVICE_RESSCHED_COMMON_OOBE_MANAGER_H