1 /* 2 * Copyright (c) 2022 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 FOUNDATION_ACE_ADAPTER_OHOS_OSAL_RESOURCE_THEME_STYLE_H 17 #define FOUNDATION_ACE_ADAPTER_OHOS_OSAL_RESOURCE_THEME_STYLE_H 18 19 #include <map> 20 #include <future> 21 #include <mutex> 22 #include <shared_mutex> 23 24 #include "core/components/theme/theme_style.h" 25 #include "core/components/theme/resource_adapter.h" 26 27 namespace OHOS::Ace { 28 class ResourceThemeStyle : public ThemeStyle { 29 DECLARE_ACE_TYPE(ResourceThemeStyle, ThemeStyle); 30 31 public: 32 friend class ResourceAdapterImpl; 33 friend class ResourceAdapterImplV2; 34 using RawAttrMap = std::map<std::string, std::string>; 35 using RawPatternMap = std::map<std::string, RawAttrMap>; 36 ResourceThemeStyle(RefPtr<ResourceAdapter> resAdapter)37 explicit ResourceThemeStyle(RefPtr<ResourceAdapter> resAdapter) : resAdapter_(resAdapter) {}; 38 ~ResourceThemeStyle() override = default; 39 40 void ParseContent() override; 41 void CheckThemeStyleLoaded(const std::string& patternName) override; SetPromiseValue()42 void SetPromiseValue() 43 { 44 promise_.set_value(); 45 } PushBackCheckThemeStyleVector(const std::string & patternName)46 void PushBackCheckThemeStyleVector(const std::string& patternName) 47 { 48 std::unique_lock<std::shared_mutex> lock(checkThemeStyleVectorMutex_); 49 checkThemeStyleVector_.push_back(patternName); 50 } CheckThemeStyle(const std::string & patternName)51 bool CheckThemeStyle(const std::string& patternName) 52 { 53 std::shared_lock<std::shared_mutex> lock(checkThemeStyleVectorMutex_); 54 auto it = std::find(checkThemeStyleVector_.begin(), checkThemeStyleVector_.end(), patternName.c_str()); 55 if (it == checkThemeStyleVector_.end()) { 56 return false; 57 } 58 return true; 59 } 60 protected: 61 void OnParseStyle(); 62 void OnParseResourceMedia(const std::string& attrName, const std::string& attrValue); 63 64 private: 65 RawAttrMap rawAttrs_; // key and value read from global resource api. 66 RawPatternMap patternAttrs_; 67 RefPtr<ResourceAdapter> resAdapter_; 68 std::promise<void> promise_; 69 std::shared_future<void> future_ = promise_.get_future(); 70 std::vector<std::string> checkThemeStyleVector_; // theme pattern name list for checking the preloaded theme style 71 std::shared_mutex checkThemeStyleVectorMutex_; 72 }; 73 } // namespace OHOS::Ace 74 75 #endif // FOUNDATION_ACE_ADAPTER_OHOS_OSAL_RESOURCE_THEME_STYLE_H 76