OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_BROWSER_MEMORY_TAB_MANAGER_WEB_CONTENTS_DATA_H_ | 5 #ifndef CHROME_BROWSER_MEMORY_TAB_MANAGER_WEB_CONTENTS_DATA_H_ |
6 #define CHROME_BROWSER_MEMORY_TAB_MANAGER_WEB_CONTENTS_DATA_H_ | 6 #define CHROME_BROWSER_MEMORY_TAB_MANAGER_WEB_CONTENTS_DATA_H_ |
7 | 7 |
8 #include "base/supports_user_data.h" | 8 #include "base/macros.h" |
9 #include "base/time/time.h" | 9 #include "base/time/time.h" |
10 #include "chrome/browser/memory/tab_manager.h" | 10 #include "chrome/browser/memory/tab_manager.h" |
| 11 #include "content/public/browser/web_contents_observer.h" |
| 12 #include "content/public/browser/web_contents_user_data.h" |
11 | 13 |
12 namespace content { | 14 namespace content { |
13 class WebContents; | 15 class WebContents; |
14 } | 16 } |
15 | 17 |
16 namespace memory { | 18 namespace memory { |
17 | 19 |
18 // Manages the information about the discarding state of a tab. This data is | 20 // Internal class used by TabManager to record the needed data for |
19 // stored in WebContents. | 21 // WebContentses. |
20 class TabManager::WebContentsData : public base::SupportsUserData::Data { | 22 class TabManager::WebContentsData |
| 23 : public content::WebContentsObserver, |
| 24 public content::WebContentsUserData<TabManager::WebContentsData> { |
21 public: | 25 public: |
22 // Returns the WebContentsData associated with |web_contents|. It will create | 26 explicit WebContentsData(content::WebContents* web_contents); |
23 // one if none exists. | 27 ~WebContentsData() override; |
24 static WebContentsData* Get(content::WebContents* web_contents); | |
25 | 28 |
26 // Sets the WebContentsData of a |web_contents|. The object referenced by | 29 // WebContentsObserver implementation: |
27 // |state| is now owned by |web_contents| | 30 void DidStartLoading() override; |
28 static void Set(content::WebContents* web_contents, WebContentsData* state); | 31 |
| 32 // Returns true if |web_contents| has been discarded to save memory. |
| 33 bool IsDiscarded(); |
| 34 |
| 35 // Sets/clears the discard state of |web_contents|. |
| 36 void SetDiscardState(bool state); |
| 37 |
| 38 // Returns the number of times |web_contents| has been discarded. |
| 39 int DiscardCount(); |
| 40 |
| 41 // Increments the number of times |web_contents| has been discarded. |
| 42 void IncrementDiscardCount(); |
| 43 |
| 44 // Returns true if audio has recently been audible from the WebContents. |
| 45 bool IsRecentlyAudible(); |
| 46 |
| 47 // Set/clears the state of whether audio has recently been audible from the |
| 48 // WebContents. |
| 49 void SetRecentlyAudible(bool state); |
| 50 |
| 51 // Returns the timestamp of the last time |web_contents| changed its audio |
| 52 // state. |
| 53 base::TimeTicks LastAudioChangeTime(); |
| 54 |
| 55 // Sets the timestamp of the last time |web_contents| changed its audio state. |
| 56 void SetLastAudioChangeTime(base::TimeTicks timestamp); |
29 | 57 |
30 // Copies the discard state from |old_contents| to |new_contents|. | 58 // Copies the discard state from |old_contents| to |new_contents|. |
31 static void CopyState(content::WebContents* old_contents, | 59 static void CopyState(content::WebContents* old_contents, |
32 content::WebContents* new_contents); | 60 content::WebContents* new_contents); |
33 | 61 |
34 // Returns true if |web_contents| has been discarded to save memory. | 62 private: |
35 static bool IsDiscarded(content::WebContents* web_contents); | 63 struct Data { |
| 64 Data(); |
| 65 // Is the tab currently discarded? |
| 66 bool is_discarded_; |
| 67 // Number of times the tab has been discarded. |
| 68 int discard_count_; |
| 69 // Is the tab playing audio? |
| 70 bool is_recently_audible_; |
| 71 // Last time the tab started or stopped playing audio (we record the |
| 72 // transition time). |
| 73 base::TimeTicks last_audio_change_time_; |
| 74 // The last time the tab was discarded. |
| 75 base::TimeTicks last_discard_time_; |
| 76 }; |
36 | 77 |
37 // Sets/clears the discard state of |web_contents|. | 78 Data tab_data_; |
38 static void SetDiscardState(content::WebContents* web_contents, bool state); | |
39 | 79 |
40 // Returns the number of times |web_contents| has been discarded. | 80 DISALLOW_COPY_AND_ASSIGN(WebContentsData); |
41 static int DiscardCount(content::WebContents* web_contents); | |
42 | |
43 // Increments the number of times |web_contents| has been discarded. | |
44 static void IncrementDiscardCount(content::WebContents* web_contents); | |
45 | |
46 // Returns true if audio has recently been audible from the WebContents. | |
47 static bool IsRecentlyAudible(content::WebContents* web_contents); | |
48 | |
49 // Set/clears the state of whether audio has recently been audible from the | |
50 // WebContents. | |
51 static void SetRecentlyAudible(content::WebContents* web_contents, | |
52 bool state); | |
53 | |
54 // Returns the timestamp of the last time |web_contents| changed its audio | |
55 // state. | |
56 static base::TimeTicks LastAudioChangeTime( | |
57 content::WebContents* web_contents); | |
58 | |
59 // Sets the timestamp of the last time |web_contents| changed its audio state. | |
60 static void SetLastAudioChangeTime(content::WebContents* web_contents, | |
61 base::TimeTicks timestamp); | |
62 | |
63 private: | |
64 WebContentsData(); | |
65 | |
66 // Is the tab currently discarded? | |
67 bool is_discarded_; | |
68 | |
69 // Number of times the tab has been discarded. | |
70 int discard_count_; | |
71 | |
72 // Is the tab playing audio? | |
73 bool is_recently_audible_; | |
74 | |
75 // Last time the tab started or stopped playing audio (we record the | |
76 // transition time). | |
77 base::TimeTicks last_audio_change_time_; | |
78 | |
79 // The last time the tab was discarded. | |
80 base::TimeTicks last_discard_time_; | |
81 }; | 81 }; |
82 | 82 |
83 } // namespace memory | 83 } // namespace memory |
84 | 84 |
85 #endif // CHROME_BROWSER_MEMORY_TAB_MANAGER_WEB_CONTENTS_DATA_H_ | 85 #endif // CHROME_BROWSER_MEMORY_TAB_MANAGER_WEB_CONTENTS_DATA_H_ |
OLD | NEW |