Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(163)

Side by Side Diff: chrome/android/java_staging/src/org/chromium/chrome/browser/ntp/NativePageAssassin.java

Issue 1141283003: Upstream oodles of Chrome for Android code into Chromium. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: final patch? Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 package org.chromium.chrome.browser.ntp;
6
7 import org.chromium.chrome.browser.FrozenNativePage;
8 import org.chromium.chrome.browser.NativePage;
9 import org.chromium.chrome.browser.Tab;
10
11 import java.lang.ref.WeakReference;
12 import java.util.ArrayList;
13
14 /**
15 * NativePageAssassin tracks recent tabs and freezes each native page when it ha sn't been visible
16 * for a while. This keeps hidden NativePages from using up precious memory.
17 *
18 * The NativePageAssassin is a singleton since having full knowledge of the user 's hidden tabs --
19 * across all local instances of Chrome and all TabModels -- allows the NativePa geAssassin to better
20 * estimate which hidden tabs the user is likely to return to.
21 *
22 * Thread safety: this should only be accessed on the UI thread.
23 */
24 public class NativePageAssassin {
25
26 private static final NativePageAssassin sInstance = new NativePageAssassin() ;
27
28 /**
29 * The number of hidden tabs to consider "recent". Any non-recent native pag e will be frozen.
30 */
31 private static final int MAX_RECENT_TABS = 3;
32
33 /**
34 * The most recently hidden tabs, limited to MAX_RECENT_TABS elements, order ed from oldest to
35 * newest. Visible tabs are not included in this list.
36 */
37 private ArrayList<WeakReference<Tab>> mRecentTabs = new ArrayList<WeakRefere nce<Tab>>(
38 MAX_RECENT_TABS + 1);
39
40 private NativePageAssassin() {}
41
42 /**
43 * @return The one and only NativePageAssassin.
44 */
45 public static NativePageAssassin getInstance() {
46 return sInstance;
47 }
48
49 /**
50 * Call this whenever a tab is shown.
51 *
52 * @param tab The tab being shown.
53 */
54 public void tabShown(Tab tab) {
55 // Remove the tab from the list of recently hidden tabs.
56 for (int i = 0; i < mRecentTabs.size(); i++) {
57 Tab t = mRecentTabs.get(i).get();
58 if (t == tab) {
59 mRecentTabs.remove(i);
60 }
61 }
62 }
63
64 /**
65 * Call this whenever a tab is hidden.
66 *
67 * @param tab The tab being hidden.
68 */
69 public void tabHidden(Tab tab) {
70 mRecentTabs.add(new WeakReference<Tab>(tab));
71
72 // If a tab has just passed the threshold from "recent" to "not recent" and it's displaying
73 // a native page, freeze the native page.
74 if (mRecentTabs.size() <= MAX_RECENT_TABS) return;
75 freeze(mRecentTabs.remove(0).get());
76 }
77
78 /**
79 * Freezes all hidden NativePages that aren't already frozen.
80 */
81 public void freezeAllHiddenPages() {
82 for (int i = 0; i < mRecentTabs.size(); i++) {
83 freeze(mRecentTabs.get(i).get());
84 }
85 mRecentTabs.clear();
86 }
87
88 private void freeze(Tab tab) {
89 if (tab == null) return;
90 NativePage pageToFreeze = tab.getNativePage();
91 if (pageToFreeze == null || pageToFreeze instanceof FrozenNativePage
92 || pageToFreeze.getView().getParent() != null) {
93 return;
94 }
95 tab.freezeNativePage();
96 }
97 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698