1 #include "dynamic_depth/profiles.h"
2 
3 #include "android-base/logging.h"
4 #include "dynamic_depth/const.h"
5 
6 using ::dynamic_depth::xmpmeta::xml::Deserializer;
7 using ::dynamic_depth::xmpmeta::xml::Serializer;
8 
9 namespace dynamic_depth {
10 
GetNamespaces(std::unordered_map<string,string> * ns_name_href_map)11 void Profiles::GetNamespaces(
12     std::unordered_map<string, string>* ns_name_href_map) {
13   if (ns_name_href_map == nullptr || profile_list_.empty()) {
14     LOG(ERROR) << "Namespace list is null or profile list is empty";
15     return;
16   }
17   for (const auto& profile : profile_list_) {
18     profile->GetNamespaces(ns_name_href_map);
19   }
20 }
21 
FromProfileArray(std::vector<std::unique_ptr<Profile>> * profile_list)22 std::unique_ptr<Profiles> Profiles::FromProfileArray(
23     std::vector<std::unique_ptr<Profile>>* profile_list) {
24   if (profile_list->empty()) {
25     LOG(ERROR) << "Profile list is empty";
26     return nullptr;
27   }
28   std::unique_ptr<Profiles> profiles(new Profiles());
29   profiles->profile_list_ = std::move(*profile_list);
30   return profiles;
31 }
32 
FromDeserializer(const Deserializer & parent_deserializer)33 std::unique_ptr<Profiles> Profiles::FromDeserializer(
34     const Deserializer& parent_deserializer) {
35   std::unique_ptr<Profiles> profiles(new Profiles());
36   int i = 0;
37   for (std::unique_ptr<Deserializer> deserializer =
38            parent_deserializer.CreateDeserializerFromListElementAt(
39                DynamicDepthConst::Namespace(DynamicDepthConst::Profiles()),
40                DynamicDepthConst::Profiles(), i);
41        deserializer != nullptr;
42        deserializer = parent_deserializer.CreateDeserializerFromListElementAt(
43            DynamicDepthConst::Namespace(DynamicDepthConst::Profiles()),
44            DynamicDepthConst::Profiles(), ++i)) {
45     std::unique_ptr<Profile> profile = Profile::FromDeserializer(*deserializer);
46     if (profile != nullptr) {
47       profiles->profile_list_.emplace_back(std::move(profile));
48     }
49   }
50 
51   if (profiles->profile_list_.empty()) {
52     return nullptr;
53   }
54   return profiles;
55 }
56 
GetProfiles() const57 const std::vector<const Profile*> Profiles::GetProfiles() const {
58   std::vector<const Profile*> profile_list;
59   for (const auto& profile : profile_list_) {
60     profile_list.push_back(profile.get());
61   }
62   return profile_list;
63 }
64 
Serialize(Serializer * serializer) const65 bool Profiles::Serialize(Serializer* serializer) const {
66   if (profile_list_.empty()) {
67     LOG(ERROR) << "Profile list is empty";
68     return false;
69   }
70   bool success = true;
71   int i = 0;
72   std::unique_ptr<Serializer> profiles_serializer =
73       serializer->CreateListSerializer(
74           DynamicDepthConst::Namespace(DynamicDepthConst::Profiles()),
75           DynamicDepthConst::Profiles());
76   if (profiles_serializer == nullptr) {
77     // Error is logged in Serializer.
78     return false;
79   }
80   for (const auto& profile : profile_list_) {
81     std::unique_ptr<Serializer> profile_serializer =
82         profiles_serializer->CreateItemSerializer(
83             DynamicDepthConst::Namespace(DynamicDepthConst::Profile()),
84             DynamicDepthConst::Profile());
85     if (profile_serializer == nullptr) {
86       continue;
87     }
88     success &= profile->Serialize(profile_serializer.get());
89     if (!success) {
90       LOG(ERROR) << "Could not serialize profile " << i;
91     }
92     ++i;
93   }
94   return success;
95 }
96 
97 }  // namespace dynamic_depth
98