OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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_H_ | 5 #ifndef CHROME_BROWSER_MEMORY_TAB_MANAGER_H_ |
6 #define CHROME_BROWSER_MEMORY_TAB_MANAGER_H_ | 6 #define CHROME_BROWSER_MEMORY_TAB_MANAGER_H_ |
7 | 7 |
8 #include <utility> | 8 #include <utility> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
43 // will be informed of each renderer's score, which is based on the status, so | 43 // will be informed of each renderer's score, which is based on the status, so |
44 // in case Chrome is not able to relieve the pressure quickly enough and the | 44 // in case Chrome is not able to relieve the pressure quickly enough and the |
45 // kernel is forced to kill processes, it will be able to do so using the same | 45 // kernel is forced to kill processes, it will be able to do so using the same |
46 // algorithm as the one used here. | 46 // algorithm as the one used here. |
47 // | 47 // |
48 // Note that the browser tests are only active for platforms that use | 48 // Note that the browser tests are only active for platforms that use |
49 // TabManager (CrOS only for now) and need to be adjusted accordingly if | 49 // TabManager (CrOS only for now) and need to be adjusted accordingly if |
50 // support for new platforms is added. | 50 // support for new platforms is added. |
51 class TabManager : public TabStripModelObserver { | 51 class TabManager : public TabStripModelObserver { |
52 public: | 52 public: |
53 // TODO(georgesak): Make this private once all external dependencies are | |
54 // removed. | |
55 class WebContentsData; | |
56 | |
57 TabManager(); | 53 TabManager(); |
58 ~TabManager() override; | 54 ~TabManager() override; |
59 | 55 |
60 // Number of discard events since Chrome started. | 56 // Number of discard events since Chrome started. |
61 int discard_count() const { return discard_count_; } | 57 int discard_count() const { return discard_count_; } |
62 | 58 |
63 // See member comment. | 59 // See member comment. |
64 bool recent_tab_discard() const { return recent_tab_discard_; } | 60 bool recent_tab_discard() const { return recent_tab_discard_; } |
65 | 61 |
66 // If |discard_once| is set, tabs that get discarded once will never get | 62 // If |discard_once| is set, tabs that get discarded once will never get |
67 // discarded again. | 63 // discarded again. |
68 void Start(bool discard_once); | 64 void Start(bool discard_once); |
69 void Stop(); | 65 void Stop(); |
70 | 66 |
71 // Returns the list of the stats for all renderers. Must be called on the UI | 67 // Returns the list of the stats for all renderers. Must be called on the UI |
72 // thread. | 68 // thread. |
73 TabStatsList GetTabStats(); | 69 TabStatsList GetTabStats(); |
74 | 70 |
| 71 // Returns true if |contents| is currently discarded. |
| 72 bool IsTabDiscarded(content::WebContents* contents) const; |
| 73 |
75 // Discards a tab to free the memory occupied by its renderer. The tab still | 74 // Discards a tab to free the memory occupied by its renderer. The tab still |
76 // exists in the tab-strip; clicking on it will reload it. Returns true if it | 75 // exists in the tab-strip; clicking on it will reload it. Returns true if it |
77 // successfully found a tab and discarded it. | 76 // successfully found a tab and discarded it. |
78 bool DiscardTab(); | 77 bool DiscardTab(); |
79 | 78 |
80 // Discards a tab with the given unique ID. The tab still exists in the | 79 // Discards a tab with the given unique ID. The tab still exists in the |
81 // tab-strip; clicking on it will reload it. Returns true if it successfully | 80 // tab-strip; clicking on it will reload it. Returns true if it successfully |
82 // found a tab and discarded it. | 81 // found a tab and discarded it. |
83 content::WebContents* DiscardTabById(int64 target_web_contents_id); | 82 content::WebContents* DiscardTabById(int64 target_web_contents_id); |
84 | 83 |
85 // Log memory statistics for the running processes, then discards a tab. | 84 // Log memory statistics for the running processes, then discards a tab. |
86 // Tab discard happens sometime later, as collecting the statistics touches | 85 // Tab discard happens sometime later, as collecting the statistics touches |
87 // multiple threads and takes time. | 86 // multiple threads and takes time. |
88 void LogMemoryAndDiscardTab(); | 87 void LogMemoryAndDiscardTab(); |
89 | 88 |
90 // Log memory statistics for the running processes, then call the callback. | 89 // Log memory statistics for the running processes, then call the callback. |
91 void LogMemory(const std::string& title, const base::Closure& callback); | 90 void LogMemory(const std::string& title, const base::Closure& callback); |
92 | 91 |
93 private: | 92 private: |
94 FRIEND_TEST_ALL_PREFIXES(TabManagerTest, Comparator); | 93 FRIEND_TEST_ALL_PREFIXES(TabManagerTest, Comparator); |
95 FRIEND_TEST_ALL_PREFIXES(TabManagerTest, DiscardedTabKeepsLastActiveTime); | 94 FRIEND_TEST_ALL_PREFIXES(TabManagerTest, DiscardedTabKeepsLastActiveTime); |
96 FRIEND_TEST_ALL_PREFIXES(TabManagerTest, DiscardWebContentsAt); | 95 FRIEND_TEST_ALL_PREFIXES(TabManagerTest, DiscardWebContentsAt); |
97 FRIEND_TEST_ALL_PREFIXES(TabManagerTest, IsInternalPage); | 96 FRIEND_TEST_ALL_PREFIXES(TabManagerTest, IsInternalPage); |
98 FRIEND_TEST_ALL_PREFIXES(TabManagerTest, ReloadDiscardedTabContextMenu); | 97 FRIEND_TEST_ALL_PREFIXES(TabManagerTest, ReloadDiscardedTabContextMenu); |
99 FRIEND_TEST_ALL_PREFIXES(TabManagerTest, TabManagerBasics); | 98 FRIEND_TEST_ALL_PREFIXES(TabManagerTest, TabManagerBasics); |
100 | 99 |
| 100 class WebContentsData; |
| 101 |
101 static void PurgeMemoryAndDiscardTab(); | 102 static void PurgeMemoryAndDiscardTab(); |
102 | 103 |
103 // Returns true if the |url| represents an internal Chrome web UI page that | 104 // Returns true if the |url| represents an internal Chrome web UI page that |
104 // can be easily reloaded and hence makes a good choice to discard. | 105 // can be easily reloaded and hence makes a good choice to discard. |
105 static bool IsInternalPage(const GURL& url); | 106 static bool IsInternalPage(const GURL& url); |
106 | 107 |
107 // Records UMA histogram statistics for a tab discard. Record statistics for | 108 // Records UMA histogram statistics for a tab discard. Record statistics for |
108 // user triggered discards via chrome://discards/ because that allows to | 109 // user triggered discards via chrome://discards/ because that allows to |
109 // manually test the system. | 110 // manually test the system. |
110 void RecordDiscardStatistics(); | 111 void RecordDiscardStatistics(); |
(...skipping 26 matching lines...) Expand all Loading... |
137 // Does the actual discard by destroying the WebContents in |model| at |index| | 138 // Does the actual discard by destroying the WebContents in |model| at |index| |
138 // and replacing it by an empty one. Returns the new WebContents or NULL if | 139 // and replacing it by an empty one. Returns the new WebContents or NULL if |
139 // the operation fails (return value used only in testing). | 140 // the operation fails (return value used only in testing). |
140 content::WebContents* DiscardWebContentsAt(int index, TabStripModel* model); | 141 content::WebContents* DiscardWebContentsAt(int index, TabStripModel* model); |
141 | 142 |
142 // Called by the memory pressure listener when the memory pressure rises. | 143 // Called by the memory pressure listener when the memory pressure rises. |
143 void OnMemoryPressure( | 144 void OnMemoryPressure( |
144 base::MemoryPressureListener::MemoryPressureLevel memory_pressure_level); | 145 base::MemoryPressureListener::MemoryPressureLevel memory_pressure_level); |
145 | 146 |
146 // TabStripModelObserver overrides. | 147 // TabStripModelObserver overrides. |
| 148 void TabInsertedAt(content::WebContents* contents, |
| 149 int index, |
| 150 bool foreground) override; |
147 void TabChangedAt(content::WebContents* contents, | 151 void TabChangedAt(content::WebContents* contents, |
148 int index, | 152 int index, |
149 TabChangeType change_type) override; | 153 TabChangeType change_type) override; |
| 154 void ActiveTabChanged(content::WebContents* old_contents, |
| 155 content::WebContents* new_contents, |
| 156 int index, |
| 157 int reason) override; |
150 | 158 |
151 // Returns true if the tab is currently playing audio or has played audio | 159 // Returns true if the tab is currently playing audio or has played audio |
152 // recently. | 160 // recently. |
153 bool IsAudioTab(content::WebContents* contents) const; | 161 bool IsAudioTab(content::WebContents* contents) const; |
154 | 162 |
| 163 // Returns the WebContentsData associated with |contents|. Also takes care of |
| 164 // creating one if needed. |
| 165 WebContentsData* GetWebContentsData(content::WebContents* contents) const; |
| 166 |
155 // Returns true if |first| is considered less desirable to be killed than | 167 // Returns true if |first| is considered less desirable to be killed than |
156 // |second|. | 168 // |second|. |
157 static bool CompareTabStats(TabStats first, TabStats second); | 169 static bool CompareTabStats(TabStats first, TabStats second); |
158 | 170 |
159 // Timer to periodically update the stats of the renderers. | 171 // Timer to periodically update the stats of the renderers. |
160 base::RepeatingTimer update_timer_; | 172 base::RepeatingTimer update_timer_; |
161 | 173 |
162 // Timer to periodically report whether a tab has been discarded since the | 174 // Timer to periodically report whether a tab has been discarded since the |
163 // last time the timer has fired. | 175 // last time the timer has fired. |
164 base::RepeatingTimer recent_tab_discard_timer_; | 176 base::RepeatingTimer recent_tab_discard_timer_; |
(...skipping 27 matching lines...) Expand all Loading... |
192 #endif | 204 #endif |
193 | 205 |
194 BrowserTabStripTracker browser_tab_strip_tracker_; | 206 BrowserTabStripTracker browser_tab_strip_tracker_; |
195 | 207 |
196 DISALLOW_COPY_AND_ASSIGN(TabManager); | 208 DISALLOW_COPY_AND_ASSIGN(TabManager); |
197 }; | 209 }; |
198 | 210 |
199 } // namespace memory | 211 } // namespace memory |
200 | 212 |
201 #endif // CHROME_BROWSER_MEMORY_TAB_MANAGER_H_ | 213 #endif // CHROME_BROWSER_MEMORY_TAB_MANAGER_H_ |
OLD | NEW |