1 // Copyright 2017 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_TRAFFIC_ANNOTATION_AUDITOR_TRAFFIC_ANNOTATION_EXPORTER_H_ 6 #define TOOLS_TRAFFIC_ANNOTATION_AUDITOR_TRAFFIC_ANNOTATION_EXPORTER_H_ 7 8 #include <map> 9 #include <set> 10 #include <vector> 11 12 #include "base/files/file_path.h" 13 #include "base/stl_util.h" 14 #include "tools/traffic_annotation/auditor/instance.h" 15 16 class TrafficAnnotationExporter { 17 public: 18 struct ArchivedAnnotation { 19 ArchivedAnnotation(); 20 ArchivedAnnotation(const ArchivedAnnotation& other); 21 ~ArchivedAnnotation(); 22 23 AnnotationInstance::Type type; 24 25 int unique_id_hash_code; 26 int second_id_hash_code; 27 int content_hash_code; 28 29 std::string deprecation_date; 30 std::vector<std::string> os_list; 31 32 std::set<int> semantics_fields; 33 std::set<int> policy_fields; 34 std::string file_path; 35 }; 36 37 TrafficAnnotationExporter(const base::FilePath& source_path); 38 ~TrafficAnnotationExporter(); 39 TrafficAnnotationExporter(const TrafficAnnotationExporter&) = delete; 40 TrafficAnnotationExporter(TrafficAnnotationExporter&&) = delete; 41 42 // Loads annotations from annotations.xml file into |archive_|. 43 bool LoadAnnotationsXML(); 44 45 // Updates |archive_| with current set of extracted annotations and reserved 46 // ids. Sets the |modified_| flag if any item is updated. Appends errors to 47 // |errors|. 48 void UpdateAnnotations(const std::vector<AnnotationInstance>& annotations, 49 const std::map<int, std::string>& reserved_ids, 50 std::vector<AuditorResult>* errors); 51 52 // Saves |archive_| into annotations.xml. 53 bool SaveAnnotationsXML() const; 54 55 // Returns the required updates for annotations.xml. 56 std::string GetRequiredUpdates(); 57 58 // Produces the list of deprecated hash codes. Requires annotations.xml to be 59 // loaded. 60 void GetDeprecatedHashCodes(std::set<int>* hash_codes); 61 modified()62 bool modified() const { return modified_; } 63 64 // Runs tests on content of |archive_|. 65 void CheckArchivedAnnotations(std::vector<AuditorResult>* errors); 66 GetArchivedAnnotations()67 const std::map<std::string, ArchivedAnnotation>& GetArchivedAnnotations() 68 const { 69 return archive_; 70 } 71 72 // Checks if the current platform is in the os list of archived annotation. MatchesCurrentPlatform(const ArchivedAnnotation & annotation)73 bool MatchesCurrentPlatform(const ArchivedAnnotation& annotation) const { 74 return base::Contains(annotation.os_list, current_platform_); 75 } 76 77 // Produces the list of annotations that are not defined in this platform. 78 // Returns false if annotations.xml is not loaded. 79 bool GetOtherPlatformsAnnotationIDs(std::vector<std::string>* ids) const; 80 81 // Returns the number of items in annotations.xml for testing. 82 unsigned GetXMLItemsCountForTesting(); 83 GetXMLDifferencesForTesting(const std::string & old_xml,const std::string & new_xml)84 std::string GetXMLDifferencesForTesting(const std::string& old_xml, 85 const std::string& new_xml) { 86 return GetXMLDifferences(old_xml, new_xml); 87 } 88 89 private: 90 // Generates a text serialized XML for current report items. 91 std::string GenerateSerializedXML() const; 92 93 // Returns the required updates to convert one serialized XML to another. 94 std::string GetXMLDifferences(const std::string& old_xml, 95 const std::string& new_xml); 96 97 std::vector<std::string> all_supported_platforms_; 98 std::map<std::string, ArchivedAnnotation> archive_; 99 const base::FilePath source_path_; 100 std::string current_platform_; 101 bool modified_; 102 }; 103 104 #endif // TOOLS_TRAFFIC_ANNOTATION_AUDITOR_TRAFFIC_ANNOTATION_EXPORTER_H_ 105