1 // Copyright 2013 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.
5 package org.chromium.chrome.browser;
7 import org.chromium.base.CalledByNative;
8 import org.chromium.chrome.browser.profiles.Profile;
10 import java.util.ArrayList;
11 import java.util.Collections;
12 import java.util.Comparator;
13 import java.util.List;
16 * This class exposes to Java information about sessions, windows, and tabs on the user's synced
19 public class ForeignSessionHelper {
20 private long mNativeForeignSessionHelper;
23 * Callback interface for getting notified when foreign session sync is updated.
25 public interface ForeignSessionCallback {
27 * This method will be called every time foreign session sync is updated.
29 * It's a good place to call {@link ForeignSessionHelper#getForeignSessions()} to get the
30 * updated information.
32 @CalledByNative("ForeignSessionCallback")
33 public void onUpdated();
37 * Represents synced foreign session.
39 public static class ForeignSession {
40 // Please keep in sync with synced_session.h
41 public static final int DEVICE_TYPE_UNSET = 0;
42 public static final int DEVICE_TYPE_WIN = 1;
43 public static final int DEVICE_TYPE_MACOSX = 2;
44 public static final int DEVICE_TYPE_LINUX = 3;
45 public static final int DEVICE_TYPE_CHROMEOS = 4;
46 public static final int DEVICE_TYPE_OTHER = 5;
47 public static final int DEVICE_TYPE_PHONE = 6;
48 public static final int DEVICE_TYPE_TABLET = 7;
50 public final String tag;
51 public final String name;
52 public final int deviceType;
53 public final long modifiedTime;
54 public final List<ForeignSessionWindow> windows = new ArrayList<ForeignSessionWindow>();
56 private ForeignSession(String tag, String name, int deviceType, long modifiedTime) {
59 this.deviceType = deviceType;
60 this.modifiedTime = modifiedTime;
65 * Represents synced foreign window. Note that desktop Chrome can have multiple windows in a
68 public static class ForeignSessionWindow {
69 public final long timestamp;
70 public final int sessionId;
71 public final List<ForeignSessionTab> tabs = new ArrayList<ForeignSessionTab>();
73 private ForeignSessionWindow(long timestamp, int sessionId) {
74 this.timestamp = timestamp;
75 this.sessionId = sessionId;
80 * Represents synced foreign tab.
82 public static class ForeignSessionTab {
83 public final String url;
84 public final String title;
85 public final long timestamp;
88 private ForeignSessionTab(String url, String title, long timestamp, int id) {
91 this.timestamp = timestamp;
97 private static ForeignSession pushSession(
98 List<ForeignSession> sessions, String tag, String name, int deviceType,
100 ForeignSession session = new ForeignSession(tag, name, deviceType, modifiedTime);
101 sessions.add(session);
106 private static ForeignSessionWindow pushWindow(
107 ForeignSession session, long timestamp, int sessionId) {
108 ForeignSessionWindow window = new ForeignSessionWindow(timestamp, sessionId);
109 session.windows.add(window);
114 private static void pushTab(
115 ForeignSessionWindow window, String url, String title, long timestamp, int sessionId) {
116 ForeignSessionTab tab = new ForeignSessionTab(url, title, timestamp, sessionId);
117 window.tabs.add(tab);
121 * Initialize this class with the given profile.
122 * @param profile Profile that will be used for syncing.
124 public ForeignSessionHelper(Profile profile) {
125 mNativeForeignSessionHelper = nativeInit(profile);
129 * Clean up the C++ side of this class. After the call, this class instance shouldn't be used.
131 public void destroy() {
132 assert mNativeForeignSessionHelper != 0;
133 nativeDestroy(mNativeForeignSessionHelper);
134 mNativeForeignSessionHelper = 0;
138 protected void finalize() {
139 // Just to make sure that we called destroy() before the java garbage collection picks up.
140 assert mNativeForeignSessionHelper == 0;
144 * @return {@code True} iff Tab sync is enabled.
146 public boolean isTabSyncEnabled() {
147 return nativeIsTabSyncEnabled(mNativeForeignSessionHelper);
151 * Force a sync for sessions.
153 public void triggerSessionSync() {
154 nativeTriggerSessionSync(mNativeForeignSessionHelper);
158 * Sets callback instance that will be called on every foreign session sync update.
159 * @param callback The callback to be invoked.
161 public void setOnForeignSessionCallback(ForeignSessionCallback callback) {
162 nativeSetOnForeignSessionCallback(mNativeForeignSessionHelper, callback);
166 * @return The list of synced foreign sessions. {@code null} iff it fails to get them for some
169 public List<ForeignSession> getForeignSessions() {
170 List<ForeignSession> result = new ArrayList<ForeignSession>();
171 boolean received = nativeGetForeignSessions(mNativeForeignSessionHelper, result);
173 // Sort sessions from most recent to least recent.
174 Collections.sort(result, new Comparator<ForeignSession>() {
176 public int compare(ForeignSession lhs, ForeignSession rhs) {
177 return lhs.modifiedTime < rhs.modifiedTime ? 1 :
178 (lhs.modifiedTime == rhs.modifiedTime ? 0 : -1);
189 * Opens the given foreign tab in a new tab.
190 * @param tab Tab to load the session into.
191 * @param session Session that the target tab belongs to.
192 * @param foreignTab Target tab to open.
193 * @param windowOpenDisposition The WindowOpenDisposition flag.
194 * @return {@code True} iff the tab is successfully opened.
196 public boolean openForeignSessionTab(Tab tab, ForeignSession session,
197 ForeignSessionTab foreignTab, int windowOpenDisposition) {
198 return nativeOpenForeignSessionTab(mNativeForeignSessionHelper, tab, session.tag,
199 foreignTab.id, windowOpenDisposition);
203 * Remove Foreign session to display. Note that it will be reappear on the next sync.
205 * This is mainly for when user wants to delete very old session that won't be used or syned in
207 * @param session Session to be deleted.
209 public void deleteForeignSession(ForeignSession session) {
210 nativeDeleteForeignSession(mNativeForeignSessionHelper, session.tag);
213 private static native long nativeInit(Profile profile);
214 private static native void nativeDestroy(long nativeForeignSessionHelper);
215 private static native boolean nativeIsTabSyncEnabled(long nativeForeignSessionHelper);
216 private static native void nativeTriggerSessionSync(long nativeForeignSessionHelper);
217 private static native void nativeSetOnForeignSessionCallback(
218 long nativeForeignSessionHelper, ForeignSessionCallback callback);
219 private static native boolean nativeGetForeignSessions(long nativeForeignSessionHelper,
220 List<ForeignSession> resultSessions);
221 private static native boolean nativeOpenForeignSessionTab(
222 long nativeForeignSessionHelper, Tab tab, String sessionTag, int tabId,
224 private static native void nativeDeleteForeignSession(
225 long nativeForeignSessionHelper, String sessionTag);