1 /*
2  * Copyright 2020 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "dumpsys/filter.h"
18 
19 #include <bluetooth/log.h>
20 
21 #include <memory>
22 
23 using namespace bluetooth;
24 using namespace dumpsys;
25 
26 class Filter {
27  public:
Filter(const dumpsys::ReflectionSchema & reflection_schema)28   Filter(const dumpsys::ReflectionSchema& reflection_schema) : reflection_schema_(reflection_schema) {}
29 
30   virtual ~Filter() = default;
31 
32   virtual void FilterInPlace(char* dumpsys_data) = 0;
33 
34   static std::unique_ptr<Filter> Factory(const dumpsys::ReflectionSchema& reflection_schema);
35 
36  protected:
37   /**
38    * Given both reflection field data and the populated flatbuffer table data, if any,
39    * filter the contents of the field based upon the filtering privacy level.
40    *
41    * Primitives and composite strings may be successfully processed at this point.
42    * Other composite types (e.g. structs or tables) must be expanded into the
43    * respective grouping of subfields.
44    *
45    * @param field The reflection field information from the bundled schema
46    * @param table The populated field data, if any
47    *
48    * @return true if field was filtered successfully, false otherwise.
49    */
FilterField(const reflection::Field *,flatbuffers::Table *)50   virtual bool FilterField(const reflection::Field* /* field */, flatbuffers::Table* /* table */) {
51     return false;
52   }
53 
54   /**
55    * Given both reflection object data and the populated flatbuffer table data, if any,
56    * filter the object fields based upon the filtering privacy level.
57    *
58    * @param object The reflection object information from the bundled schema
59    * @param table The populated field data, if any
60    *
61    */
FilterObject(const reflection::Object *,flatbuffers::Table *)62   virtual void FilterObject(
63       const reflection::Object* /* object */, flatbuffers::Table* /* table */){};
64 
65   /**
66    * Given both reflection field data and the populated table data, if any,
67    * filter the contents of the table based upon the filtering privacy level.
68    *
69    * @param schema The reflection schema information from the bundled schema
70    * @param table The populated field data, if any
71    *
72    */
FilterTable(const reflection::Schema *,flatbuffers::Table *)73   virtual void FilterTable(
74       const reflection::Schema* /* schema */, flatbuffers::Table* /* table */){};
75 
76   const dumpsys::ReflectionSchema& reflection_schema_;
77 };
78 
79 class DeveloperPrivacyFilter : public Filter {
80  public:
DeveloperPrivacyFilter(const dumpsys::ReflectionSchema & reflection_schema)81   DeveloperPrivacyFilter(const dumpsys::ReflectionSchema& reflection_schema) : Filter(reflection_schema) {}
FilterInPlace(char *)82   void FilterInPlace(char* /* dumpsys_data */) override { /* Nothing to do in this mode */
83   }
84 };
85 
Factory(const dumpsys::ReflectionSchema & reflection_schema)86 std::unique_ptr<Filter> Filter::Factory(const dumpsys::ReflectionSchema& reflection_schema) {
87   return std::make_unique<DeveloperPrivacyFilter>(reflection_schema);
88 }
89 
FilterSchema(const ReflectionSchema & reflection_schema,std::string * dumpsys_data)90 void bluetooth::dumpsys::FilterSchema(
91     const ReflectionSchema& reflection_schema, std::string* dumpsys_data) {
92   auto filter = Filter::Factory(reflection_schema);
93   filter->FilterInPlace(dumpsys_data->data());
94 }
95