Chromium Code Reviews| Index: media/base/android/media_service_throttler.h |
| diff --git a/media/base/android/media_service_throttler.h b/media/base/android/media_service_throttler.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..f129e9ec7a14250c11db91543b0779a50ef930e1 |
| --- /dev/null |
| +++ b/media/base/android/media_service_throttler.h |
| @@ -0,0 +1,98 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#ifndef MEDIA_BASE_ANDROID_MEDIA_SERVICE_THROTTLER_H_ |
| +#define MEDIA_BASE_ANDROID_MEDIA_SERVICE_THROTTLER_H_ |
| + |
| +#include <memory> |
| + |
| +#include "base/callback_forward.h" |
| +#include "base/cancelable_callback.h" |
| +#include "base/macros.h" |
| +#include "base/lazy_instance.h" |
| +#include "base/single_thread_task_runner.h" |
| +#include "base/time/tick_clock.h" |
| +#include "base/time/time.h" |
| +#include "media/base/media_export.h" |
| + |
| +namespace media { |
| +class MediaServerCrashListener; |
| + |
| +class MEDIA_EXPORT MediaServiceThrottler { |
| + public: |
| + // Called to get the singleton MediaServiceThrottler instance. |
| + // The first thread on which GetInstance() is called is the thread on which |
| + // calls to OnMediaServerCrash() will be signaled. |
| + static MediaServiceThrottler* GetInstance(); |
| + |
| + // Returns the delay to wait until a new client is allowed to be created. |
| + base::TimeDelta GetDelayForClientCreation(); |
| + |
| + void SetTickClockForTesting(base::TickClock* clock); |
|
DaleCurtis
2016/11/11 23:32:16
Usually I'd put these as a single block with a com
tguilbert
2016/11/14 23:18:08
Done.
|
| + |
| + void ResetInternalStateForTesting(); |
| + |
| + base::TimeDelta GetBaseThrottlingRateForTesting(); |
| + |
| + bool IsCrashListenerAliveForTesting(); |
| + |
| + void SetCrashListenerTaskRunnerForTesting( |
| + scoped_refptr<base::SingleThreadTaskRunner> crash_listener_task_runner); |
| + |
| + private: |
| + friend struct base::DefaultLazyInstanceTraits<MediaServiceThrottler>; |
| + friend class MediaServiceThrottlerTest; |
|
tguilbert
2016/11/11 03:50:29
Is adding tests as a friend class frowned upon? sa
sandersd (OOO until July 31)
2016/11/11 18:55:14
Friend classes are fine for testing, and common in
|
| + |
| + MediaServiceThrottler(); |
| + virtual ~MediaServiceThrottler(); |
| + |
| + // Called by the |crash_listener_| whenever a crash is detected. |
| + void OnMediaServerCrash(); |
| + |
| + // Updates |current_craches_| according to a linear decay function. |
| + void UpdateServerCrashes(); |
| + |
| + // Ensures that the MediaServerCrashListener was properly started (can lead |
| + // to OnMediaServerCrash() being called in the case it hasn't). |
| + void EnsureCrashListenerStarted(); |
| + |
| + // Frees up the resources used by |crash_listener_|; |
| + void ReleaseCrashListener(); |
| + |
| + // Gets the delay for ScheduleClientCreation(), which grows exponentially |
| + // based on |current_crashes_|. |
| + base::TimeDelta GetThrottlingDelayFromServerCrashes(); |
| + |
| + std::unique_ptr<base::TickClock> clock_; |
| + |
| + // Effective number of media server crashes. |
|
DaleCurtis
2016/11/11 23:32:16
Explain why it's a double.
tguilbert
2016/11/14 23:18:08
Done.
|
| + double current_crashes_; |
| + |
| + // Next time at which a client creation can be scheduled. |
| + base::TimeTicks next_schedulable_slot_; |
| + |
| + // Last media server crash time. |
| + base::TimeTicks last_server_crash_; |
| + |
| + // Last time UpdateServerCrashes() was called. |
| + base::TimeTicks last_current_crash_update_time_; |
| + |
| + // Last time ScheduleClientCreation() was called. |
| + base::TimeTicks last_schedule_call_; |
| + |
| + // Callbacks used to release |crash_listener_| after 60s of inactivity. |
| + base::Closure release_crash_listener_cb_; |
| + base::CancelableClosure cancelable_release_crash_listener_cb_; |
| + |
| + // Listener that verifies |
| + std::unique_ptr<MediaServerCrashListener> crash_listener_; |
| + |
| + scoped_refptr<base::SingleThreadTaskRunner> crash_listener_task_runner_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(MediaServiceThrottler); |
| +}; |
| + |
| +} // namespace media |
| + |
| +#endif // MEDIA_BASE_ANDROID_MEDIA_SERVICE_THROTTLER_H_ |