summaryrefslogtreecommitdiffstats
path: root/chrome/android/java/src/org/chromium/chrome/browser/ntp/RecentTabsManager.java
blob: 5f2749c02211638b11ec085e4294246f3c50e889 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
// Copyright 2015 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.

package org.chromium.chrome.browser.ntp;

import android.content.Context;
import android.graphics.Bitmap;

import org.chromium.base.ObserverList;
import org.chromium.base.ThreadUtils;
import org.chromium.chrome.browser.UrlConstants;
import org.chromium.chrome.browser.favicon.FaviconHelper;
import org.chromium.chrome.browser.favicon.FaviconHelper.FaviconImageCallback;
import org.chromium.chrome.browser.firstrun.ProfileDataCache;
import org.chromium.chrome.browser.invalidation.InvalidationController;
import org.chromium.chrome.browser.metrics.StartupMetrics;
import org.chromium.chrome.browser.ntp.ForeignSessionHelper.ForeignSession;
import org.chromium.chrome.browser.ntp.ForeignSessionHelper.ForeignSessionCallback;
import org.chromium.chrome.browser.ntp.ForeignSessionHelper.ForeignSessionTab;
import org.chromium.chrome.browser.ntp.RecentTabsPromoView.SyncPromoModel;
import org.chromium.chrome.browser.ntp.RecentlyClosedBridge.RecentlyClosedCallback;
import org.chromium.chrome.browser.ntp.RecentlyClosedBridge.RecentlyClosedTab;
import org.chromium.chrome.browser.profiles.Profile;
import org.chromium.chrome.browser.signin.SigninManager;
import org.chromium.chrome.browser.signin.SigninManager.SignInStateObserver;
import org.chromium.chrome.browser.sync.ProfileSyncService;
import org.chromium.chrome.browser.tab.Tab;
import org.chromium.content_public.browser.LoadUrlParams;
import org.chromium.sync.AndroidSyncSettings;
import org.chromium.sync.AndroidSyncSettings.AndroidSyncSettingsObserver;
import org.chromium.sync.signin.ChromeSigninController;

import java.util.Collections;
import java.util.List;

/**
 * Provides the domain logic and data for RecentTabsPage and RecentTabsRowAdapter.
 */
public class RecentTabsManager implements AndroidSyncSettingsObserver, SignInStateObserver,
        SyncPromoModel {

    /**
     * Implement this to receive updates when the page contents change.
     */
    interface UpdatedCallback {
        /**
         * Called when the list of recently closed tabs or foreign sessions changes.
         */
        void onUpdated();
    }

    private static final int RECENTLY_CLOSED_MAX_TAB_COUNT = 5;

    private final Profile mProfile;
    private final Tab mTab;
    private final Context mContext;
    private final ObserverList<AndroidSyncSettingsObserver> mObservers =
            new ObserverList<AndroidSyncSettingsObserver>();

    private FaviconHelper mFaviconHelper;
    private ForeignSessionHelper mForeignSessionHelper;
    private List<ForeignSession> mForeignSessions;
    private List<RecentlyClosedTab> mRecentlyClosedTabs;
    private NewTabPagePrefs mNewTabPagePrefs;
    private RecentlyClosedBridge mRecentlyClosedBridge;
    private SigninManager mSignInManager;
    private UpdatedCallback mUpdatedCallback;
    private ProfileDataCache mProfileDataCache;
    private boolean mIsDestroyed;

    /**
     * Create an RecentTabsManager to be used with RecentTabsPage and RecentTabsRowAdapter.
     *
     * @param tab The Tab that is showing this recent tabs page.
     * @param profile Profile that is associated with the current session.
     * @param context the Android context this manager will work in.
     */
    public RecentTabsManager(Tab tab, Profile profile, Context context) {
        mProfile = profile;
        mTab = tab;
        mForeignSessionHelper = buildForeignSessionHelper(mProfile);
        mNewTabPagePrefs = buildNewTabPagePrefs(mProfile);
        mFaviconHelper = buildFaviconHelper();
        mRecentlyClosedBridge = buildRecentlyClosedBridge(mProfile);
        mSignInManager = SigninManager.get(context);
        mContext = context;

        updateRecentlyClosedTabs();
        registerForForeignSessionUpdates();
        updateForeignSessions();
        mForeignSessionHelper.triggerSessionSync();
        registerForSignInAndSyncNotifications();

        InvalidationController.get(mContext).onRecentTabsPageOpened();
    }

    /**
     * Should be called when this object is no longer needed. Performs necessary listener tear down.
     */
    public void destroy() {
        mIsDestroyed = true;
        AndroidSyncSettings.unregisterObserver(mContext, this);

        mSignInManager.removeSignInStateObserver(this);
        mSignInManager = null;

        mFaviconHelper.destroy();
        mFaviconHelper = null;

        mRecentlyClosedBridge.destroy();
        mRecentlyClosedBridge = null;

        mForeignSessionHelper.destroy();
        mForeignSessionHelper = null;

        mUpdatedCallback = null;

        mNewTabPagePrefs.destroy();
        mNewTabPagePrefs = null;

        if (mProfileDataCache != null) {
            mProfileDataCache.destroy();
            mProfileDataCache = null;
        }

        InvalidationController.get(mContext).onRecentTabsPageClosed();
    }

    /**
     * Returns true if destroy() has been called.
     */
    public boolean isDestroyed() {
        return mIsDestroyed;
    }

    private static ForeignSessionHelper buildForeignSessionHelper(Profile profile) {
        return new ForeignSessionHelper(profile);
    }

    private static NewTabPagePrefs buildNewTabPagePrefs(Profile profile) {
        return new NewTabPagePrefs(profile);
    }

    private static FaviconHelper buildFaviconHelper() {
        return new FaviconHelper();
    }

    private RecentlyClosedBridge buildRecentlyClosedBridge(Profile profile) {
        RecentlyClosedBridge bridge = new RecentlyClosedBridge(profile);
        bridge.setRecentlyClosedCallback(new RecentlyClosedCallback() {
            @Override
            public void onUpdated() {
                updateRecentlyClosedTabs();
                postUpdate();
            }
        });
        return bridge;
    }

    private void registerForForeignSessionUpdates() {
        mForeignSessionHelper.setOnForeignSessionCallback(new ForeignSessionCallback() {
            @Override
            public void onUpdated() {
                updateForeignSessions();
                postUpdate();
            }
        });
    }

    private void registerForSignInAndSyncNotifications() {
        AndroidSyncSettings.registerObserver(mContext, this);
        mSignInManager.addSignInStateObserver(this);
    }

    protected void updateCurrentlyOpenTabs() {
    }

    private void updateRecentlyClosedTabs() {
        mRecentlyClosedTabs = mRecentlyClosedBridge.getRecentlyClosedTabs(
                RECENTLY_CLOSED_MAX_TAB_COUNT);
    }

    private void updateForeignSessions() {
        mForeignSessions = mForeignSessionHelper.getForeignSessions();
        if (mForeignSessions == null) {
            mForeignSessions = Collections.emptyList();
        }
    }

    /**
     * @return Most up-to-date list of currently open tabs.
     */
    public List<CurrentlyOpenTab> getCurrentlyOpenTabs() {
        return null;
    }

    /**
     * @return Most up-to-date list of foreign sessions.
     */
    public List<ForeignSession> getForeignSessions() {
        return mForeignSessions;
    }

    /**
     * @return Most up-to-date list of recently closed tabs.
     */
    public List<RecentlyClosedTab> getRecentlyClosedTabs() {
        return mRecentlyClosedTabs;
    }

    /**
     * Opens a new tab navigating to ForeignSessionTab.
     *
     * @param session The foreign session that the tab belongs to.
     * @param tab The tab to open.
     * @param windowDisposition The WindowOpenDisposition flag.
     */
    public void openForeignSessionTab(ForeignSession session, ForeignSessionTab tab,
            int windowDisposition) {
        NewTabPageUma.recordAction(NewTabPageUma.ACTION_OPENED_FOREIGN_SESSION);
        mForeignSessionHelper.openForeignSessionTab(mTab, session, tab, windowDisposition);
    }

    /**
     * Restores a recently closed tab.
     *
     * @param tab The tab to open.
     * @param windowDisposition The WindowOpenDisposition value specifying whether the tab should
     *         be restored into the current tab or a new tab.
     */
    public void openRecentlyClosedTab(RecentlyClosedTab tab, int windowDisposition) {
        NewTabPageUma.recordAction(NewTabPageUma.ACTION_OPENED_RECENTLY_CLOSED_ENTRY);
        mRecentlyClosedBridge.openRecentlyClosedTab(mTab, tab, windowDisposition);
    }

    /**
     * Opens the history page.
     */
    public void openHistoryPage() {
        mTab.loadUrl(new LoadUrlParams(UrlConstants.HISTORY_URL));
        StartupMetrics.getInstance().recordOpenedHistory();
    }

    /**
     * Returns a 16x16 favicon for a given synced url.
     *
     * @param url The url to fetch the favicon for.
     * @return 16x16 favicon or null if favicon unavailable.
     */
    public Bitmap getSyncedFaviconImageForURL(String url) {
        return mFaviconHelper.getSyncedFaviconImageForURL(mProfile, url);
    }

    /**
     * Fetches a favicon for snapshot document url which is returned via callback.
     *
     * @param url The url to fetch a favicon for.
     * @param size the desired favicon size.
     * @param faviconCallback the callback to be invoked when the favicon is available.
     *
     * @return may return false if we could not fetch the favicon.
     */
    public boolean getLocalFaviconForUrl(String url, int size,
            FaviconImageCallback faviconCallback) {
        return mFaviconHelper.getLocalFaviconImageForURL(mProfile, url, size, faviconCallback);
    }

    /**
     * Sets a callback to be invoked when recently closed tabs or foreign sessions documents have
     * been updated.
     *
     * @param updatedCallback the listener to be invoked.
     */
    public void setUpdatedCallback(UpdatedCallback updatedCallback) {
        mUpdatedCallback = updatedCallback;
    }

    /**
     * Sets the persistent expanded/collapsed state of the currently open tabs list.
     *
     * @param isCollapsed Whether the currently open tabs list is collapsed.
     */
    public void setCurrentlyOpenTabsCollapsed(boolean isCollapsed) {
        mNewTabPagePrefs.setCurrentlyOpenTabsCollapsed(isCollapsed);
    }

    /**
     * Determine the expanded/collapsed state of the currently open tabs list.
     *
     * @return Whether the currently open tabs list is collapsed.
     */
    public boolean isCurrentlyOpenTabsCollapsed() {
        return mNewTabPagePrefs.getCurrentlyOpenTabsCollapsed();
    }

    /**
     * Sets the state for showing all tabs in the currently open tabs list. This is intended to
     * be overridden in extending classes and set to true when the user clicks the "More" button
     * at the end of the list.
     * @param showingAll Whether the currently open tabs list should start to show all.
     */
    public void setCurrentlyOpenTabsShowAll(boolean showingAll) {
    }

    /**
     * @return Whether the currently open tabs group shows all tabs. If it is not, only a limited
     * number of tabs is shown with a "More" button at the end of the list to show all.
     */
    public boolean isCurrentlyOpenTabsShowingAll() {
        return false;
    }

    /**
     * Closes the specified currently open tab.
     * @param tab Information about the tab that should be closed.
     */
    public void closeTab(CurrentlyOpenTab tab) {
    }

    /**
     * Sets the persistent expanded/collapsed state of a foreign session list.
     *
     * @param session foreign session to collapsed.
     * @param isCollapsed Whether the session is collapsed or expanded.
     */
    public void setForeignSessionCollapsed(ForeignSession session, boolean isCollapsed) {
        mNewTabPagePrefs.setForeignSessionCollapsed(session, isCollapsed);
    }

    /**
     * Determine the expanded/collapsed state of a foreign session list.
     *
     * @param session foreign session whose state to obtain.
     *
     * @return Whether the session is collapsed.
     */
    public boolean getForeignSessionCollapsed(ForeignSession session) {
        return mNewTabPagePrefs.getForeignSessionCollapsed(session);
    }

    /**
     * Sets the persistent expanded/collapsed state of the recently closed tabs list.
     *
     * @param isCollapsed Whether the recently closed tabs list is collapsed.
     */
    public void setRecentlyClosedTabsCollapsed(boolean isCollapsed) {
        mNewTabPagePrefs.setRecentlyClosedTabsCollapsed(isCollapsed);
    }

    /**
     * Determine the expanded/collapsed state of the recently closed tabs list.
     *
     * @return Whether the recently closed tabs list is collapsed.
     */
    public boolean isRecentlyClosedTabsCollapsed() {
        return mNewTabPagePrefs.getRecentlyClosedTabsCollapsed();
    }

   /**
     * Remove Foreign session to display. Note that it might reappear during the next sync if the
     * session is not orphaned.
     *
     * This is mainly for when user wants to delete an orphaned session.
     * @param session Session to be deleted.
     */
    public void deleteForeignSession(ForeignSession session) {
        mForeignSessionHelper.deleteForeignSession(session);
    }

    /**
     * Clears the list of recently closed tabs.
     */
    public void clearRecentlyClosedTabs() {
        mRecentlyClosedBridge.clearRecentlyClosedTabs();
    }

    /**
     * Determine whether the sync promo needs to be displayed.
     *
     * @return Whether sync promo should be displayed.
     */
    public boolean shouldDisplaySyncPromo() {
        if (SigninManager.get(mContext).isSigninDisabledByPolicy()) {
            return false;
        }

        return !AndroidSyncSettings.isSyncEnabled(mContext) || mForeignSessions.isEmpty();
    }

    /**
     * Collapse the sync promo.
     *
     * @param isCollapsed Whether the sync promo is collapsed.
     */
    public void setSyncPromoCollapsed(boolean isCollapsed) {
        mNewTabPagePrefs.setSyncPromoCollapsed(isCollapsed);
    }

    /**
     * Determine whether the sync promo is collapsed.
     *
     * @return Whether the sync promo is collapsed.
     */
    public boolean isSyncPromoCollapsed() {
        return mNewTabPagePrefs.getSyncPromoCollapsed();
    }

    protected void postUpdate() {
        if (mUpdatedCallback != null) {
            mUpdatedCallback.onUpdated();
        }
    }

    // SignInStateObserver
    @Override
    public void onSignedIn() {
        androidSyncSettingsChanged();
    }

    @Override
    public void onSignedOut() {
        androidSyncSettingsChanged();
    }

    // AndroidSyncSettingsObserver
    @Override
    public void androidSyncSettingsChanged() {
        ThreadUtils.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (mIsDestroyed) return;
                updateForeignSessions();
                postUpdate();
                for (AndroidSyncSettingsObserver observer : mObservers) {
                    observer.androidSyncSettingsChanged();
                }
            }
        });
    }

    // SyncPromoModel
    @Override
    public boolean isSyncEnabled() {
        return AndroidSyncSettings.isSyncEnabled(mContext);
    }

    @Override
    public boolean isSignedIn() {
        return ChromeSigninController.get(mContext).isSignedIn();
    }

    @Override
    public void enableSync() {
        ProfileSyncService syncService = ProfileSyncService.get();
        if (syncService != null) {
            syncService.requestStart();
        }
    }

    @Override
    public void registerForSyncUpdates(AndroidSyncSettingsObserver changeListener) {
        mObservers.addObserver(changeListener);
    }

    @Override
    public void unregisterForSyncUpdates(AndroidSyncSettingsObserver changeListener) {
        mObservers.removeObserver(changeListener);
    }

    @Override
    public ProfileDataCache getProfileDataCache() {
        if (mProfileDataCache == null) {
            mProfileDataCache = new ProfileDataCache(mContext, Profile.getLastUsedProfile());
        }
        return mProfileDataCache;
    }
}