Reilly Grant | 0e21968 | 2020-09-16 21:05:31 | [diff] [blame] | 1 | // Copyright 2020 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 | |
| 5 | #ifndef CHROME_BROWSER_VISIBILITY_TIMER_TAB_HELPER_H_ |
| 6 | #define CHROME_BROWSER_VISIBILITY_TIMER_TAB_HELPER_H_ |
| 7 | |
Dan Harrington | 92ddbc2 | 2021-06-04 17:22:49 | [diff] [blame] | 8 | #include "base/callback_forward.h" |
Reilly Grant | 0e21968 | 2020-09-16 21:05:31 | [diff] [blame] | 9 | #include "base/containers/circular_deque.h" |
Dan Harrington | 92ddbc2 | 2021-06-04 17:22:49 | [diff] [blame] | 10 | #include "base/timer/timer.h" |
Reilly Grant | 0e21968 | 2020-09-16 21:05:31 | [diff] [blame] | 11 | #include "content/public/browser/web_contents_observer.h" |
| 12 | #include "content/public/browser/web_contents_user_data.h" |
| 13 | |
Reilly Grant | 0e21968 | 2020-09-16 21:05:31 | [diff] [blame] | 14 | // At most one of these is attached to each WebContents. It allows posting |
| 15 | // delayed tasks whose timer only counts down whilst the WebContents is visible |
| 16 | // (and whose timer is reset whenever the WebContents stops being visible). |
Dan Harrington | 92ddbc2 | 2021-06-04 17:22:49 | [diff] [blame] | 17 | // If multiple tasks are added, they are queued in a dormant state -- their |
| 18 | // timer will not elapse until earlier tasks are completed. |
Reilly Grant | 0e21968 | 2020-09-16 21:05:31 | [diff] [blame] | 19 | class VisibilityTimerTabHelper |
| 20 | : public content::WebContentsObserver, |
| 21 | public content::WebContentsUserData<VisibilityTimerTabHelper> { |
| 22 | public: |
| 23 | VisibilityTimerTabHelper(const VisibilityTimerTabHelper&&) = delete; |
| 24 | VisibilityTimerTabHelper& operator=(const VisibilityTimerTabHelper&&) = |
| 25 | delete; |
| 26 | |
| 27 | ~VisibilityTimerTabHelper() override; |
| 28 | |
| 29 | // Runs |task| after the WebContents has been visible for a consecutive |
| 30 | // duration of at least |visible_delay|. |
| 31 | void PostTaskAfterVisibleDelay(const base::Location& from_here, |
| 32 | base::OnceClosure task, |
| 33 | base::TimeDelta visible_delay); |
| 34 | |
| 35 | // WebContentsObserver: |
| 36 | void OnVisibilityChanged(content::Visibility visibility) override; |
| 37 | |
| 38 | private: |
Dan Harrington | 92ddbc2 | 2021-06-04 17:22:49 | [diff] [blame] | 39 | struct Task; |
Reilly Grant | 0e21968 | 2020-09-16 21:05:31 | [diff] [blame] | 40 | friend class content::WebContentsUserData<VisibilityTimerTabHelper>; |
| 41 | explicit VisibilityTimerTabHelper(content::WebContents* contents); |
| 42 | |
| 43 | void RunTask(base::OnceClosure task); |
Dan Harrington | 92ddbc2 | 2021-06-04 17:22:49 | [diff] [blame] | 44 | void StartNextTaskTimer(); |
Reilly Grant | 0e21968 | 2020-09-16 21:05:31 | [diff] [blame] | 45 | |
Dan Harrington | 92ddbc2 | 2021-06-04 17:22:49 | [diff] [blame] | 46 | base::OneShotTimer timer_; |
| 47 | base::circular_deque<Task> task_queue_; |
Reilly Grant | 0e21968 | 2020-09-16 21:05:31 | [diff] [blame] | 48 | |
| 49 | WEB_CONTENTS_USER_DATA_KEY_DECL(); |
| 50 | }; |
| 51 | |
| 52 | #endif // CHROME_BROWSER_VISIBILITY_TIMER_TAB_HELPER_H_ |