1 // Copyright (c) 2024 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef TOOLS_GN_OHOS_COMPONENTS_MGR_H_ 6 #define TOOLS_GN_OHOS_COMPONENTS_MGR_H_ 7 8 #include <map> 9 #include <mutex> 10 11 #include "base/files/file_path.h" 12 #include "base/values.h" 13 #include "gn/err.h" 14 #include "gn/value.h" 15 16 class OhosComponent; 17 18 struct OhosComponentTree { 19 const char *dirName; 20 struct OhosComponentTree *next; 21 struct OhosComponentTree *child; 22 const OhosComponent *component; 23 24 public: 25 OhosComponentTree(const char *dirName, const OhosComponent *component = nullptr) 26 { 27 this->dirName = strdup(dirName); 28 this->component = component; 29 this->next = nullptr; 30 this->child = nullptr; 31 } 32 33 OhosComponentTree(const char *dirName, size_t len, const OhosComponent *component = nullptr) 34 { 35 this->dirName = (char *)malloc(len + 1); 36 if (this->dirName) { 37 strncpy((char *)this->dirName, dirName, len); 38 ((char *)this->dirName)[len] = '\0'; 39 } 40 this->component = component; 41 this->next = nullptr; 42 this->child = nullptr; 43 } 44 ~OhosComponentTreeOhosComponentTree45 ~OhosComponentTree() 46 { 47 if (!this->dirName) { 48 free((void *)this->dirName); 49 } 50 } 51 }; 52 53 class OhosComponentsImpl { 54 public: 55 OhosComponentsImpl(); 56 57 bool LoadOhosComponents(const std::string &build_dir, 58 const Value *enable, Err *err); 59 60 bool GetExternalDepsLabel(const Value &external_dep, std::string &label, Err *err) const; 61 bool GetSubsystemName(const Value &component_name, std::string &subsystem_name, Err *err) const; 62 63 const OhosComponent *GetComponentByName(const std::string &component_name) const; 64 65 private: 66 bool ReadBuildConfigFile(const std::string &build_dir, 67 const char *subfile, std::string &content); 68 69 std::map<std::string, OhosComponent *> components_; 70 71 struct OhosComponentTree *pathTree = nullptr; 72 void setupComponentsTree(); 73 const struct OhosComponentTree *findChildByPath(const struct OhosComponentTree *current, 74 const char *path, size_t len); 75 void addComponentToTree(struct OhosComponentTree *current, OhosComponent *component); 76 77 void LoadInnerApi(const base::DictionaryValue *innerapis); 78 79 // For unittest 80 public: 81 const OhosComponent *matchComponentByLabel(const char *label); 82 83 bool LoadComponentSubsystemAndPaths(const std::string &paths, 84 const std::string &override_map, 85 const std::string &subsystems, 86 std::string &err_msg_out); 87 88 bool LoadOhosInnerApis_(const std::string innerapi_content, std::string &err_msg_out); 89 }; 90 91 #endif // TOOLS_GN_OHOS_COMPONENTS_MGR_H_