Chromium Code Reviews| Index: chrome/android/java/src/org/chromium/chrome/browser/ForeignSessionHelper.java |
| diff --git a/chrome/android/java/src/org/chromium/chrome/browser/ForeignSessionHelper.java b/chrome/android/java/src/org/chromium/chrome/browser/ForeignSessionHelper.java |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..aac1b2415cbff35b53b6e9aff7a47ccc34e0075a |
| --- /dev/null |
| +++ b/chrome/android/java/src/org/chromium/chrome/browser/ForeignSessionHelper.java |
| @@ -0,0 +1,215 @@ |
| +// Copyright 2013 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; |
| + |
| +import org.chromium.base.CalledByNative; |
| +import org.chromium.chrome.browser.profiles.Profile; |
| + |
| +import java.util.ArrayList; |
| +import java.util.Collections; |
| +import java.util.Comparator; |
| +import java.util.List; |
| + |
| +/** |
| + * This is a helper class that provides foreign session sync related functionalities. |
| + */ |
| +public class ForeignSessionHelper { |
| + private int mNativeForeignSessionHelper; |
| + |
| + /** |
| + * Callback interface for getting notified when foreign session sync is updated. |
| + */ |
| + public interface OnForeignSessionCallback { |
| + /** |
| + * This method will be called every time foreign session sync is updated. |
| + * |
| + * It's a good place to call {@link ForeignSessionHelper#getForeignSessions()} to get the |
| + * updated information. |
| + */ |
| + @CalledByNative("OnForeignSessionCallback") |
| + public void onUpdated(); |
| + } |
| + |
| + /** |
| + * Represents synced foreign session. |
| + */ |
| + public static class Session { |
|
Ted C
2013/07/24 23:48:20
for each of these, I would add a private construct
Kibeom Kim (inactive)
2013/07/25 21:18:23
Done.
|
| + public String tag; |
| + public String name; |
| + public String deviceType; |
| + public long modifiedTime; |
| + public List<Window> windows; |
| + } |
| + |
| + /** |
| + * Represents synced foreign window. Note that desktop Chrome can have multiple windows in a |
| + * session. |
| + */ |
| + public static class Window { |
|
Ted C
2013/07/24 23:48:20
purely for accessing these outside of this class,
Kibeom Kim (inactive)
2013/07/25 21:18:23
Done.
|
| + public double timestamp; |
| + public int sessionId; |
| + public List<Tab> tabs; |
| + } |
| + |
| + /** |
| + * Represents synced foreign tab. |
| + */ |
| + public static class Tab { |
| + public String url; |
| + public String title; |
| + public long timestamp; |
| + public int id; |
| + } |
| + |
| + @CalledByNative |
| + private static void pushSession( |
| + List<Session> sessions, String tag, String name, String deviceType, long modifiedTime) { |
| + Session session = new Session(); |
| + session.tag = tag; |
| + session.name = name; |
| + session.deviceType = deviceType; |
| + session.modifiedTime = modifiedTime; |
| + session.windows = new ArrayList<Window>(); |
| + |
| + assert sessions != null; |
| + if (sessions == null) return; |
| + |
| + sessions.add(session); |
| + } |
| + |
| + @CalledByNative |
| + private static void pushWindow( |
| + List<Session> sessions, long timestamp, int sessionId) { |
|
Ted C
2013/07/24 23:48:20
Instead of taking the list of sessions, can you ju
Kibeom Kim (inactive)
2013/07/25 21:18:23
Nice suggestion. Done.
|
| + Window window = new Window(); |
| + window.timestamp = timestamp; |
| + window.sessionId = sessionId; |
| + window.tabs = new ArrayList<Tab>(); |
| + |
| + assert sessions != null && !sessions.isEmpty(); |
| + if (sessions == null || sessions.isEmpty()) return; |
| + Session session = sessions.get(sessions.size() - 1); |
|
Ted C
2013/07/24 23:48:20
Yeah, with the above change, you could get rid of
Kibeom Kim (inactive)
2013/07/25 21:18:23
Done.
|
| + |
| + session.windows.add(window); |
| + } |
| + |
| + @CalledByNative |
| + private static void pushTab( |
| + List<Session> sessions, String url, String title, long timestamp, |
|
Ted C
2013/07/24 23:48:20
Same comment as above, pass in a Window instance i
Kibeom Kim (inactive)
2013/07/25 21:18:23
Done.
|
| + int sessionId) { |
| + assert !sessions.isEmpty(); |
| + if (sessions.isEmpty()) return; |
| + |
| + Tab tab = new Tab(); |
| + tab.url = url; |
| + tab.title = title; |
| + tab.timestamp = timestamp; |
| + tab.id = sessionId; |
| + |
| + assert sessions != null && !sessions.isEmpty(); |
| + if (sessions == null || sessions.isEmpty()) return; |
| + Session session = sessions.get(sessions.size() - 1); |
| + |
| + assert !session.windows.isEmpty(); |
| + if (session.windows.isEmpty()) return; |
| + Window window = session.windows.get(session.windows.size() - 1); |
| + |
| + window.tabs.add(tab); |
| + } |
| + |
| + /** |
| + * Initialize this class with the given profile. |
| + * @param profile Profile that will be used for syncing. |
| + */ |
| + public ForeignSessionHelper(Profile profile) { |
| + mNativeForeignSessionHelper = nativeInit(profile); |
| + } |
| + |
| + @Override |
| + protected void finalize() { |
|
Ted C
2013/07/24 23:48:20
we shouldn't rely on finalize being called, we sho
Kibeom Kim (inactive)
2013/07/25 21:18:23
Done.
|
| + assert mNativeForeignSessionHelper != 0; |
| + nativeDestroy(mNativeForeignSessionHelper); |
| + } |
| + |
| + /** |
| + * @return {@code True} iff Tab sync is enabled. |
| + */ |
| + public boolean isTabSyncEnabled() { |
| + return nativeIsTabSyncEnabled(mNativeForeignSessionHelper); |
| + } |
| + |
| + /** |
| + * Sets callback instance that will be called on every foreign session sync update. |
| + */ |
| + public void setOnForeignSessionCallback(OnForeignSessionCallback callback) { |
| + nativeSetOnForeignSessionCallback(mNativeForeignSessionHelper, callback); |
| + } |
| + |
| + /** |
| + * @return The list of synced foreign sessions. {@code null} iff it fails to get them for some |
| + * reason. |
|
Ted C
2013/07/24 23:48:20
align with "The list" above
Kibeom Kim (inactive)
2013/07/25 21:18:23
Done.
|
| + */ |
| + public List<Session> getForeignSessions() { |
| + List<Session> result = new ArrayList<Session>(); |
| + boolean received = nativeGetForeignSessions(mNativeForeignSessionHelper, result); |
| + if (received) { |
| + // Sort sessions from most recent to least recent. |
| + Collections.sort(result, new Comparator<Session>() { |
| + @Override |
| + public int compare(Session session1, Session session2) { |
| + long timeDiff = session2.modifiedTime - session1.modifiedTime; |
| + return (int) Math.max(Math.min(Integer.MAX_VALUE, timeDiff), Integer.MIN_VALUE); |
|
Ted C
2013/07/24 23:48:20
This is the way I've seen number compare's done...
Kibeom Kim (inactive)
2013/07/25 21:18:23
Done.
|
| + } |
| + }); |
| + } else { |
| + result = null; |
| + } |
| + |
| + return result; |
| + } |
| + |
| + /** |
| + * Opens the given foreign tab in a new tab. |
| + * @param session Session that the target tab belongs to. |
| + * @param tab Target tab to open. |
| + * @return {@code True} iff the tab is successfully opened. |
| + */ |
| + public boolean openForeignSessionTab(Session session, Tab tab) { |
| + return nativeOpenForeignSessionTab(mNativeForeignSessionHelper, session.tag, tab.id); |
| + } |
| + |
| + /** |
| + * Set the given session collapsed or uncollapsed in Pref. |
|
Ted C
2013/07/24 23:48:20
Pref = preferences.
Kibeom Kim (inactive)
2013/07/25 21:18:23
Done.
|
| + * @param session Session to set collapsed or uncollapsed. |
| + * @param isCollapsed {@code True} iff we want the session to be collapsed. |
| + */ |
| + public void setForeignSessionCollapsed(Session session, boolean isCollapsed) { |
| + nativeSetForeignSessionCollapsed(mNativeForeignSessionHelper, session.tag, isCollapsed); |
| + } |
| + |
| + /** |
| + * Remove Foreign session to display. Note that it will be reappear on the next sync. |
| + * |
| + * This is mainly for when user wants to delete very old session that won't be used or syned in |
| + * the future. |
| + * @param session Session to be deleted. |
| + */ |
| + public void deleteForeignSession(Session session) { |
| + nativeDeleteForeignSession(mNativeForeignSessionHelper, session.tag); |
| + } |
| + |
| + private static native int nativeInit(Profile profile); |
| + private static native void nativeDestroy(int nativeForeignSessionHelper); |
| + private static native boolean nativeIsTabSyncEnabled(int nativeForeignSessionHelper); |
| + private static native void nativeSetOnForeignSessionCallback( |
| + int nativeForeignSessionHelper, OnForeignSessionCallback callback); |
| + private static native boolean nativeGetForeignSessions(int nativeForeignSessionHelper, |
| + List<Session> resultSessions); |
| + private static native boolean nativeOpenForeignSessionTab( |
| + int nativeForeignSessionHelper, String sessionTag, int tabId); |
| + private static native void nativeSetForeignSessionCollapsed( |
| + int nativeForeignSessionHelper, String sessionTag, boolean isCollapsed); |
| + private static native void nativeDeleteForeignSession( |
| + int nativeForeignSessionHelper, String sessionTag); |
| +} |