1 /*
2  * Copyright (C) 2018 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 #ifndef SRC_PERFETTO_CMD_RATE_LIMITER_H_
18 #define SRC_PERFETTO_CMD_RATE_LIMITER_H_
19 
20 #include "perfetto/base/time.h"
21 #include "src/perfetto_cmd/perfetto_cmd_state.gen.h"
22 
23 namespace perfetto {
24 
25 class RateLimiter {
26  public:
27   struct Args {
28     bool is_user_build = false;
29     bool is_uploading = false;
30     bool ignore_guardrails = false;
31     bool allow_user_build_tracing = false;
32     base::TimeSeconds current_time = base::TimeSeconds(0);
33     uint64_t max_upload_bytes_override = 0;
34     std::string unique_session_name = "";
35   };
36   enum ShouldTraceResponse {
37     kOkToTrace,
38     kNotAllowedOnUserBuild,
39     kFailedToInitState,
40     kInvalidState,
41     kHitUploadLimit,
42   };
43 
44   RateLimiter();
45   virtual ~RateLimiter();
46 
47   ShouldTraceResponse ShouldTrace(const Args& args);
48   bool OnTraceDone(const Args& args, bool success, uint64_t bytes);
49 
50   bool ClearState();
51 
52   // virtual for testing.
53   virtual bool LoadState(gen::PerfettoCmdState* state);
54 
55   // virtual for testing.
56   virtual bool SaveState(const gen::PerfettoCmdState& state);
57 
58   bool StateFileExists();
59   virtual std::string GetStateFilePath() const;
60 
61  private:
62   gen::PerfettoCmdState state_{};
63 };
64 
65 }  // namespace perfetto
66 
67 #endif  // SRC_PERFETTO_CMD_RATE_LIMITER_H_
68