content_tracing_manager.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. // Copyright 2021 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 COMPONENTS_FEEDBACK_CONTENT_CONTENT_TRACING_MANAGER_H_
  5. #define COMPONENTS_FEEDBACK_CONTENT_CONTENT_TRACING_MANAGER_H_
  6. #include "components/feedback/tracing_manager.h"
  7. #include <map>
  8. #include <memory>
  9. #include <string>
  10. #include "base/memory/weak_ptr.h"
  11. #include "base/sequence_checker.h"
  12. // This class is used to manage performance metrics that can be attached to
  13. // feedback reports. This class is a Singleton that is owned by the preference
  14. // system. It should only be created when it is enabled, and should only be
  15. // accessed elsewhere via Get().
  16. //
  17. // When a performance trace is desired, TracingManager::Get()->RequestTrace()
  18. // should be invoked. The TracingManager will then start preparing a zipped
  19. // version of the performance data. That data can then be requested via
  20. // GetTraceData(). When the data is no longer needed, it should be discarded
  21. // via DiscardTraceData().
  22. class ContentTracingManager : public TracingManager {
  23. public:
  24. ~ContentTracingManager() override;
  25. // Create a ContentTracingManager. Can only be called when none exists.
  26. static std::unique_ptr<ContentTracingManager> Create();
  27. // Get the current ContentTracingManager. Returns NULL if one doesn't exist.
  28. static ContentTracingManager* Get();
  29. // Request a trace ending at the current time. If a trace is already being
  30. // collected, the id for that trace is returned.
  31. int RequestTrace() override;
  32. // Get the trace data for |id|. On success, true is returned, and the data is
  33. // returned via |callback|. Returns false on failure.
  34. bool GetTraceData(int id, TraceDataCallback callback) override;
  35. // Discard the data for trace |id|.
  36. void DiscardTraceData(int id) override;
  37. private:
  38. ContentTracingManager();
  39. void StartTracing();
  40. void OnTraceDataCollected(std::unique_ptr<std::string> data);
  41. // ID of the trace that is being collected.
  42. int current_trace_id_ = 0;
  43. // Mapping of trace ID to trace data.
  44. std::map<int, scoped_refptr<base::RefCountedString>> trace_data_;
  45. // Callback for the current trace request.
  46. TraceDataCallback trace_callback_;
  47. base::WeakPtrFactory<ContentTracingManager> weak_ptr_factory_{this};
  48. };
  49. #endif // COMPONENTS_FEEDBACK_CONTENT_CONTENT_TRACING_MANAGER_H_