| // Copyright 2022 The Chromium Authors |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| |
| #ifndef MEDIA_CAPABILITIES_PENDING_OPERATIONS_H_ |
| #define MEDIA_CAPABILITIES_PENDING_OPERATIONS_H_ |
| |
| #include "base/cancelable_callback.h" |
| #include "base/containers/flat_map.h" |
| #include "base/memory/raw_ref.h" |
| #include "base/memory/weak_ptr.h" |
| #include "base/time/time.h" |
| #include "media/base/media_export.h" |
| |
| namespace media { |
| |
| class MEDIA_EXPORT PendingOperations { |
| public: |
| using Id = int; |
| |
| // Helper to report timing information for DB operations, including when they |
| // hang indefinitely. |
| class PendingOperation { |
| public: |
| PendingOperation( |
| const std::string& uma_prefix, |
| std::string uma_str, |
| std::unique_ptr<base::CancelableOnceClosure> timeout_closure); |
| // Records task timing UMA if it hasn't already timed out. |
| virtual ~PendingOperation(); |
| |
| // Copies disallowed. Incompatible with move-only members and UMA logging in |
| // the destructor. |
| PendingOperation(const PendingOperation&) = delete; |
| PendingOperation& operator=(const PendingOperation&) = delete; |
| |
| void UmaHistogramOpTime(const std::string& op_name, |
| base::TimeDelta duration); |
| |
| // Trigger UMA recording for timeout. |
| void OnTimeout(); |
| |
| private: |
| friend class VideoDecodeStatsDBImplTest; |
| friend class WebrtcVideoStatsDBImplTest; |
| const raw_ref<const std::string> uma_prefix_; |
| const std::string uma_str_; |
| std::unique_ptr<base::CancelableOnceClosure> timeout_closure_; |
| const base::TimeTicks start_ticks_; |
| }; |
| |
| explicit PendingOperations(std::string uma_prefix); |
| ~PendingOperations(); |
| |
| // Creates a PendingOperation using `uma_str` and adds it to `pending_ops_` |
| // map. Returns Id for newly started operation. Callers must later call |
| // Complete() with this id to destroy the PendingOperation and finalize timing |
| // UMA. |
| Id Start(std::string uma_str); |
| |
| // Removes PendingOperation from `pending_ops_` using `op_id_` as a key. This |
| // destroys the object and triggers timing UMA. |
| void Complete(Id op_id); |
| |
| // Unified handler for timeouts of pending DB operations. PendingOperation |
| // will be notified that it timed out (to trigger timing UMA) and removed from |
| // `pending_ops_`. |
| void OnTimeout(Id id); |
| |
| const base::flat_map<Id, std::unique_ptr<PendingOperation>>& |
| get_pending_ops_for_test() const { |
| return pending_ops_; |
| } |
| |
| private: |
| // UMA prefix that is used for pending operations histograms. |
| const std::string uma_prefix_; |
| |
| // Next Id for use in `pending_ops_` map. See Start(). |
| Id next_op_id_ = 0; |
| |
| // Map of operation id -> outstanding PendingOperations. |
| base::flat_map<Id, std::unique_ptr<PendingOperation>> pending_ops_; |
| |
| // Ensures all access to class members come on the same sequence. |
| SEQUENCE_CHECKER(sequence_checker_); |
| |
| base::WeakPtrFactory<PendingOperations> weak_ptr_factory_{this}; |
| }; |
| |
| } // namespace media |
| |
| #endif // MEDIA_CAPABILITIES_PENDING_OPERATIONS_H_ |