1 /*
2  * Copyright (c) 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_LAZY_LOAD_MANAGER_H
17 #define OHOS_ACELITE_LAZY_LOAD_MANAGER_H
18 
19 #include "gfx_utils/list.h"
20 #include "lazy_load_watcher.h"
21 #include "non_copyable.h"
22 
23 namespace OHOS {
24 namespace ACELite {
25 /**
26  * lazy load state
27  */
28 enum class LazyLoadState : uint8_t {
29     INIT,
30     READY,
31     DONE,
32     ABANDON
33 };
34 
35 class LazyLoadManager final : public MemoryHeap {
36 public:
37     ACE_DISALLOW_COPY_AND_MOVE(LazyLoadManager);
38     LazyLoadManager();
39 
40     ~LazyLoadManager();
41 
42     void ResetWatchers();
43 
44     /**
45      * @brief Cache watcher, need to calculate the name key by self
46      */
47     void AddLazyLoadWatcher(jerry_value_t nativeElement, jerry_value_t attrName, jerry_value_t getter);
48 
49     /**
50      * @brief Cache watcher
51      */
52     void AddLazyLoadWatcher(jerry_value_t nativeElement, jerry_value_t attrName, jerry_value_t getter, uint16_t keyId);
53 
54     /**
55      * @brief Remove one lazy watcher from pending list by native element value
56      */
57     void RemoveLazyWatcher(jerry_value_t nativeElement);
58 
59     /**
60      * @brief Render watcher at next TE task
61      */
62     void RenderLazyLoadWatcher();
63 
SetState(LazyLoadState state)64     void SetState(LazyLoadState state)
65     {
66         state_ = state;
67     }
68 
GetState()69     LazyLoadState GetState() const
70     {
71         return state_;
72     }
73 private:
74     void RenderSingleLazyWatcher(const LazyLoadWatcher &watcher) const;
75     List<LazyLoadWatcher *> lazyWatchersList_;
76     LazyLoadState state_;
77 };
78 } // namespace ACELite
79 } // namespace OHOS
80 #endif // OHOS_ACELITE_LAZY_LOAD_MANAGER_H
81