1 /*
2  * Copyright (C) 2019 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_TRACED_SERVICE_LAZY_PRODUCER_H_
18 #define SRC_TRACED_SERVICE_LAZY_PRODUCER_H_
19 
20 #include <set>
21 #include <string>
22 
23 #include "perfetto/base/task_runner.h"
24 #include "perfetto/base/weak_ptr.h"
25 
26 #include "perfetto/tracing/core/basic_types.h"
27 #include "perfetto/tracing/core/producer.h"
28 #include "perfetto/tracing/core/tracing_service.h"
29 
30 namespace perfetto {
31 
32 class LazyProducer : public Producer {
33  public:
34   LazyProducer(base::TaskRunner* task_runner,
35                uint32_t delay_ms,
36                std::string data_source_name,
37                std::string property_name);
38 
39   ~LazyProducer() override;
40   // No-ops to satisfy the Producer implementation.
OnDisconnect()41   void OnDisconnect() override {}
OnTracingSetup()42   void OnTracingSetup() override {}
StartDataSource(DataSourceInstanceID,const DataSourceConfig &)43   void StartDataSource(DataSourceInstanceID, const DataSourceConfig&) override {
44   }
Flush(FlushRequestID flush_id,const DataSourceInstanceID *,size_t)45   void Flush(FlushRequestID flush_id,
46              const DataSourceInstanceID*,
47              size_t) override {
48     endpoint_->NotifyFlushComplete(flush_id);
49   }
ClearIncrementalState(const DataSourceInstanceID *,size_t)50   void ClearIncrementalState(const DataSourceInstanceID* /*data_source_ids*/,
51                              size_t /*num_data_sources*/) override {}
52 
53   void OnConnect() override;
54   void SetupDataSource(DataSourceInstanceID, const DataSourceConfig&) override;
55   void StopDataSource(DataSourceInstanceID) override;
56 
57   void ConnectInProcess(TracingService* svc);
58   virtual bool SetAndroidProperty(const std::string& name,
59                                   const std::string& value);
60 
61  private:
62   base::TaskRunner* task_runner_;
63   uint32_t delay_ms_;
64 
65   std::string data_source_name_;
66   std::string property_name_;
67 
68   std::unique_ptr<TracingService::ProducerEndpoint> endpoint_;
69   uint64_t active_sessions_ = 0;
70   uint64_t generation_ = 0;
71 
72   base::WeakPtrFactory<LazyProducer> weak_factory_;  // Keep last.
73 };
74 
75 }  // namespace perfetto
76 
77 #endif  // SRC_TRACED_SERVICE_LAZY_PRODUCER_H_
78