instance.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. #ifndef TOOLS_TRAFFIC_ANNOTATION_AUDITOR_INSTANCE_H_
  5. #define TOOLS_TRAFFIC_ANNOTATION_AUDITOR_INSTANCE_H_
  6. #include <vector>
  7. #include "base/version.h"
  8. #include "tools/traffic_annotation/auditor/auditor_result.h"
  9. #include "tools/traffic_annotation/traffic_annotation.pb.h"
  10. // Base class for Annotation and Call instances.
  11. class InstanceBase {
  12. public:
  13. InstanceBase() {}
  14. virtual ~InstanceBase() {}
  15. virtual AuditorResult Deserialize(
  16. const std::vector<std::string>& serialized_lines,
  17. int start_line,
  18. int end_line) = 0;
  19. };
  20. // Holds an instance of network traffic annotation.
  21. class AnnotationInstance : public InstanceBase {
  22. public:
  23. // Annotation Type.
  24. enum class Type {
  25. ANNOTATION_COMPLETE,
  26. ANNOTATION_PARTIAL,
  27. ANNOTATION_COMPLETING,
  28. ANNOTATION_BRANCHED_COMPLETING,
  29. ANNOTATION_INSTANCE_TYPE_LAST = ANNOTATION_BRANCHED_COMPLETING
  30. };
  31. AnnotationInstance();
  32. AnnotationInstance(const AnnotationInstance& other);
  33. AnnotationInstance& operator=(const AnnotationInstance& other);
  34. ~AnnotationInstance() override;
  35. // Deserializes an instance from serialized lines of the text provided by the
  36. // extractor.
  37. // |serialized_lines| are read from |start_line| to |end_line| and should
  38. // contain the following lines:
  39. // 1- File path.
  40. // 2- Name of the function including this annotation.
  41. // 3- Line number.
  42. // 4- Annotation function Type.
  43. // 5- Unique id of annotation.
  44. // 6- Completing id or group id, when applicable, empty otherwise.
  45. // 7- Serialization of annotation text (several lines).
  46. AuditorResult Deserialize(const std::vector<std::string>& serialized_lines,
  47. int start_line,
  48. int end_line) override;
  49. // Returns the proto field numbers of TrafficSemantics fields that are
  50. // included in this annotation.
  51. void GetSemanticsFieldNumbers(std::set<int>* field_numbers) const;
  52. // Returns the proto field numbers of TrafficPolicy fields that are included
  53. // in this annotation.
  54. // For the cookies_allowed field, if the value is YES,
  55. // kCookiesAllowedFieldNumber is added to the set and if it is NO,
  56. // -kCookiesAllowedFieldNumber is added.
  57. void GetPolicyFieldNumbers(std::set<int>* field_numbers) const;
  58. // Loads annotation based on the data from archive in annotations.xml.
  59. static AnnotationInstance LoadFromArchive(
  60. AnnotationInstance::Type type,
  61. const std::string& unique_id,
  62. int unique_id_hash_code,
  63. int second_id_hash_code,
  64. int content_hash_code,
  65. const std::set<int>& semantics_fields,
  66. const std::set<int>& policy_fields,
  67. const std::string& file_path,
  68. int added_in_milestone);
  69. // Checks if an annotation has all required fields.
  70. AuditorResult IsComplete() const;
  71. // Checks if annotation fields are consistent.
  72. AuditorResult IsConsistent() const;
  73. // Checks if annotation appears in summary/grouping.xml
  74. AuditorResult InGroupingXML(
  75. const std::set<std::string>& grouping_annotation_unique_ids) const;
  76. // Checks to see if this annotation can be completed with the |other|
  77. // annotation, based on their unique ids, types, and extra ids. |*this| should
  78. // be of partial type and the |other| either COMPLETING or BRANCHED_COMPLETING
  79. // type.
  80. bool IsCompletableWith(const AnnotationInstance& other) const;
  81. // Tells if annotation requires two ids. All annotations have the unique id,
  82. // but partial annotations also require the completing id, and branched
  83. // completing annotations require the group id.
  84. bool NeedsTwoIDs() const {
  85. return type == Type::ANNOTATION_PARTIAL ||
  86. type == Type::ANNOTATION_BRANCHED_COMPLETING;
  87. }
  88. // If annotation is loaded from archive, returns |archive_content_hash_code|.
  89. // Otherwise computes a hashcode for the annotation content. Source field is
  90. // not used in this computation as we don't need sensitivity to changes in
  91. // source location, i.e. filepath, line number and function.
  92. int GetContentHashCode() const;
  93. // Combines |*this| partial annotation with a completing/branched_completing
  94. // annotation and returns the combined complete annotation.
  95. AuditorResult CreateCompleteAnnotation(
  96. AnnotationInstance& completing_annotation,
  97. AnnotationInstance* combination) const;
  98. // Serializes to text for debugging and visualization.
  99. std::string Serialize() const;
  100. // Protobuf of the annotation.
  101. traffic_annotation::NetworkTrafficAnnotation proto;
  102. // Same message as |proto|, but with the schema loaded at runtime
  103. // (using reflection) based on chrome_settings_full_runtime.proto.
  104. std::unique_ptr<google::protobuf::Message> runtime_proto = nullptr;
  105. // Type of the annotation.
  106. Type type;
  107. // Extra id of the annotation (if available). This can be the completing id
  108. // for partial annotations, or group id for branched completing annotations.
  109. std::string second_id;
  110. // Hash codes of unique id and extra id (if available).
  111. int unique_id_hash_code;
  112. int second_id_hash_code;
  113. // The hash code of annotation content for archived annotations.
  114. int archive_content_hash_code;
  115. // The milestone (Chrome version) where this annotation was first added to
  116. // Chromium code.
  117. int archive_added_in_milestone;
  118. // Flag stating if annotation is loaded from annotations.xml.
  119. bool is_loaded_from_archive;
  120. // This annotation is generated from merging two other incomplete annotations.
  121. bool is_merged;
  122. };
  123. std::ostream& operator<<(std::ostream& out, const AnnotationInstance& instance);
  124. // Holds an instance of calling a function that might have a network traffic
  125. // annotation argument.
  126. class CallInstance : public InstanceBase {
  127. public:
  128. CallInstance();
  129. CallInstance(const CallInstance& other);
  130. // Deserializes an instance from serialized lines of text provided by the
  131. // extractor.
  132. // |serialized_lines| are read from |start_line| to |end_line| and should
  133. // contain the following lines:
  134. // 1- File path.
  135. // 2- Name of the function in which the call is made.
  136. // 3- Name of the called function.
  137. // 4- Does the call have an annotation?
  138. AuditorResult Deserialize(const std::vector<std::string>& serialized_lines,
  139. int start_line,
  140. int end_line) override;
  141. std::string file_path;
  142. uint32_t line_number;
  143. // Name of the function that may need annotation.
  144. std::string function_name;
  145. // Is function |function_name| annotated?
  146. bool is_annotated;
  147. };
  148. // Holds an instance of initializing a traffic annotation tag with list
  149. // expressions or assignment of a value to |unique_id_hash_code| of the mutable
  150. // ones, outside traffic annotation API functions.
  151. class AssignmentInstance : public InstanceBase {
  152. public:
  153. AssignmentInstance();
  154. AssignmentInstance(const AssignmentInstance& other);
  155. // Deserializes an instance from serialized lines of text provided by the
  156. // extractor.
  157. // |serialized_lines| are read from |start_line| to |end_line| and should
  158. // contain the following lines:
  159. // 1- File path.
  160. // 2- Name of the function in which the assignment is made.
  161. // 3- Line number.
  162. AuditorResult Deserialize(const std::vector<std::string>& serialized_lines,
  163. int start_line,
  164. int end_line) override;
  165. std::string file_path;
  166. uint32_t line_number;
  167. };
  168. #endif // TOOLS_TRAFFIC_ANNOTATION_AUDITOR_INSTANCE_H_