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 BASE_TRACE_EVENT_TRACE_CONFIG_CATEGORY_FILTER_H_
6 #define BASE_TRACE_EVENT_TRACE_CONFIG_CATEGORY_FILTER_H_
7 
8 #include <string>
9 #include <vector>
10 
11 #include "base/base_export.h"
12 #include "base/strings/string_piece.h"
13 #include "base/values.h"
14 
15 namespace base {
16 namespace trace_event {
17 
18 // Configuration of categories enabled and disabled in TraceConfig.
19 class BASE_EXPORT TraceConfigCategoryFilter {
20  public:
21   using StringList = std::vector<std::string>;
22 
23   TraceConfigCategoryFilter();
24   TraceConfigCategoryFilter(const TraceConfigCategoryFilter& other);
25   ~TraceConfigCategoryFilter();
26 
27   TraceConfigCategoryFilter& operator=(const TraceConfigCategoryFilter& rhs);
28 
29   // Initializes from category filter string. See TraceConfig constructor for
30   // description of how to write category filter string.
31   void InitializeFromString(const StringPiece& category_filter_string);
32 
33   // Initializes TraceConfigCategoryFilter object from the config dictionary.
34   void InitializeFromConfigDict(const Value& dict);
35 
36   // Merges this with category filter config.
37   void Merge(const TraceConfigCategoryFilter& config);
38   void Clear();
39 
40   // Returns true if at least one category in the list is enabled by this
41   // trace config. This is used to determine if the category filters are
42   // enabled in the TRACE_* macros.
43   bool IsCategoryGroupEnabled(const StringPiece& category_group_name) const;
44 
45   // Returns true if the category is enabled according to this trace config.
46   // This tells whether a category is enabled from the TraceConfig's
47   // perspective. Please refer to IsCategoryGroupEnabled() to determine if a
48   // category is enabled from the tracing runtime's perspective.
49   bool IsCategoryEnabled(const StringPiece& category_name) const;
50 
51   void ToDict(Value* dict) const;
52 
53   std::string ToFilterString() const;
54 
55   // Returns true if category name is a valid string.
56   static bool IsCategoryNameAllowed(StringPiece str);
57 
included_categories()58   const StringList& included_categories() const { return included_categories_; }
excluded_categories()59   const StringList& excluded_categories() const { return excluded_categories_; }
60 
61  private:
62   void SetCategoriesFromIncludedList(const Value& included_list);
63   void SetCategoriesFromExcludedList(const Value& excluded_list);
64 
65   void AddCategoriesToDict(const StringList& categories,
66                            const char* param,
67                            Value* dict) const;
68 
69   void WriteCategoryFilterString(const StringList& values,
70                                  std::string* out,
71                                  bool included) const;
72 
73   StringList included_categories_;
74   StringList disabled_categories_;
75   StringList excluded_categories_;
76 };
77 
78 }  // namespace trace_event
79 }  // namespace base
80 
81 #endif  // BASE_TRACE_EVENT_TRACE_CONFIG_CATEGORY_FILTER_H_
82