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..456d884b57d24d3235c2d5647b9941e15d07fb76 |
--- /dev/null |
+++ b/chrome/android/java/src/org/chromium/chrome/browser/ForeignSessionHelper.java |
@@ -0,0 +1,214 @@ |
+// 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 ForeignSession { |
+ private ForeignSession(String tag, String name, String deviceType, long modifiedTime, |
+ List<ForeignSessionWindow> windows) { |
+ this.tag = tag; |
+ this.name = name; |
+ this.deviceType = deviceType; |
+ this.modifiedTime = modifiedTime; |
+ this.windows = windows; |
+ } |
+ |
+ public final String tag; |
+ public final String name; |
Ted C
2013/07/26 22:29:47
I would put the params above the constructor in al
Kibeom Kim (inactive)
2013/07/26 23:32:52
Done.
|
+ public final String deviceType; |
+ public final long modifiedTime; |
+ public final List<ForeignSessionWindow> windows; |
+ } |
+ |
+ /** |
+ * Represents synced foreign window. Note that desktop Chrome can have multiple windows in a |
+ * session. |
+ */ |
+ public static class ForeignSessionWindow { |
+ private ForeignSessionWindow(long timestamp, int sessionId, List<ForeignSessionTab> tabs) { |
+ this.timestamp = timestamp; |
+ this.sessionId = sessionId; |
+ this.tabs = tabs; |
+ } |
+ |
+ public final long timestamp; |
+ public final int sessionId; |
+ public final List<ForeignSessionTab> tabs; |
+ } |
+ |
+ /** |
+ * Represents synced foreign tab. |
+ */ |
+ public static class ForeignSessionTab { |
+ private ForeignSessionTab(String url, String title, long timestamp, int id) { |
+ this.url = url; |
+ this.title = title; |
+ this.timestamp = timestamp; |
+ this.id = id; |
+ } |
+ |
+ public final String url; |
+ public final String title; |
+ public final long timestamp; |
+ public final int id; |
+ } |
+ |
+ @CalledByNative |
+ private static ForeignSession pushSession( |
+ List<ForeignSession> sessions, String tag, String name, String deviceType, |
+ long modifiedTime) { |
+ ForeignSession session = new ForeignSession( |
+ tag, name, deviceType, modifiedTime, new ArrayList<ForeignSessionWindow>()); |
+ sessions.add(session); |
+ return session; |
+ } |
+ |
+ @CalledByNative |
+ private static ForeignSessionWindow pushWindow( |
+ ForeignSession session, long timestamp, int sessionId) { |
+ ForeignSessionWindow window = new ForeignSessionWindow( |
+ timestamp, sessionId, new ArrayList<ForeignSessionTab>()); |
+ session.windows.add(window); |
+ return window; |
+ } |
+ |
+ @CalledByNative |
+ private static void pushTab( |
+ ForeignSessionWindow window, String url, String title, long timestamp, int sessionId) { |
+ ForeignSessionTab tab = new ForeignSessionTab(url, title, timestamp, sessionId); |
+ 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); |
+ } |
+ |
+ public void destroy() { |
Ted C
2013/07/26 22:29:47
javadoc :-)
Kibeom Kim (inactive)
2013/07/26 23:32:52
Done.
|
+ assert mNativeForeignSessionHelper != 0; |
+ nativeDestroy(mNativeForeignSessionHelper); |
+ } |
+ |
+ @Override |
+ protected void finalize() { |
+ // Just to make sure that we called destroy() before the java garbage collection picks up. |
+ assert mNativeForeignSessionHelper == 0; |
+ } |
+ |
+ /** |
+ * @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. |
+ */ |
+ public List<ForeignSession> getForeignSessions() { |
+ List<ForeignSession> result = new ArrayList<ForeignSession>(); |
+ boolean received = nativeGetForeignSessions(mNativeForeignSessionHelper, result); |
+ if (received) { |
+ // Sort sessions from most recent to least recent. |
+ Collections.sort(result, new Comparator<ForeignSession>() { |
+ @Override |
+ public int compare(ForeignSession session1, ForeignSession session2) { |
+ long timeDiff = session2.modifiedTime - session1.modifiedTime; |
+ return session1.modifiedTime < session2.modifiedTime ? 1 : |
+ (session1.modifiedTime == session2.modifiedTime ? 0 : -1); |
+ } |
+ }); |
+ } 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(ForeignSession session, ForeignSessionTab tab) { |
+ return nativeOpenForeignSessionTab(mNativeForeignSessionHelper, session.tag, tab.id); |
+ } |
+ |
+ /** |
+ * Set the given session collapsed or uncollapsed in preferences. |
+ * @param session Session to set collapsed or uncollapsed. |
+ * @param isCollapsed {@code True} iff we want the session to be collapsed. |
+ */ |
+ public void setForeignSessionCollapsed(ForeignSession 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(ForeignSession 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<ForeignSession> 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); |
+} |