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 OHOS_MEDIA_DFX_CLOUD_MANAGER_H
17 #define OHOS_MEDIA_DFX_CLOUD_MANAGER_H
18 
19 #include <mutex>
20 #include <timer.h>
21 
22 namespace OHOS {
23 namespace Media {
24 
25 enum class SyncState : uint16_t {
26     INIT_STATE = 0,
27     START_STATE,
28     END_STATE,
29 };
30 
31 enum class CloudSyncStatus : int32_t {
32     BEGIN = 0,
33     FIRST_FIVE_HUNDRED,
34     INCREMENT_DOWNLOAD,
35     TOTAL_DOWNLOAD,
36     TOTAL_DOWNLOAD_FINISH,
37     SYNC_SWITCHED_OFF,
38 };
39 
40 class CloudSyncDfxManager;
41 
42 class InitState {
43 public:
44     static bool StateSwitch(CloudSyncDfxManager& manager);
45     static void Process(CloudSyncDfxManager& manager);
46 };
47 
48 class StartState {
49 public:
50     static bool StateSwitch(CloudSyncDfxManager& manager);
51     static void Process(CloudSyncDfxManager& manager);
52 };
53 
54 class EndState {
55 public:
56     static bool StateSwitch(CloudSyncDfxManager& manager);
57     static void Process(CloudSyncDfxManager& manager);
58 };
59 
60 struct StateProcessFunc {
61     bool (*StateSwitch)(CloudSyncDfxManager&);
62     void (*Process)(CloudSyncDfxManager& manager);
63 };
64 
65 class CloudSyncDfxManager {
66 public:
67     ~CloudSyncDfxManager();
68     static CloudSyncDfxManager& GetInstance();
69     void ShutDownTimer();
70     void RunDfx();
71 
72     friend class InitState;
73     friend class StartState;
74     friend class EndState;
75 private:
76     void InitSyncState();
77     CloudSyncDfxManager();
78     void StartTimer();
79     void SetStartTime();
80     void ResetStartTime();
81     std::mutex timerMutex_;
82     std::mutex endStateMutex_;
83     Utils::Timer timer_{ "CloudSyncTimer" };
84     uint32_t timerId_{ 0 };
85     SyncState syncState_{ SyncState::INIT_STATE };
86     std::vector<StateProcessFunc> stateProcessFuncs_ {
87         {
88             InitState::StateSwitch,
89             InitState::Process,
90         },
91         {
92             StartState::StateSwitch,
93             StartState::Process,
94         },
95         {
96             EndState::StateSwitch,
97             EndState::Process,
98         }
99     };
100 };
101 
102 } // Media
103 } // OHOS
104 
105 #endif // OHOS_MEDIA_DFX_CLOUD_MANAGER_H