DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Mercurial (dcc6d7a0dc00)

VCS Links

Line Code
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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

"use strict";

this.EXPORTED_SYMBOLS = ["TabGroupsMigrator"];

const { classes: Cc, interfaces: Ci, results: Cr, utils: Cu } = Components;

Cu.import("resource://gre/modules/XPCOMUtils.jsm");

XPCOMUtils.defineLazyModuleGetter(this, "Services", "resource://gre/modules/Services.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "PlacesUtils", "resource://gre/modules/PlacesUtils.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "Task", "resource://gre/modules/Task.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "OS", "resource://gre/modules/osfile.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "AsyncShutdown", "resource://gre/modules/AsyncShutdown.jsm");

XPCOMUtils.defineLazyGetter(this, "gBrowserBundle", function() {
  return Services.strings.createBundle('chrome://browser/locale/browser.properties');
});

const RECOVERY_URL = "chrome://browser/content/aboutTabGroupsMigration.xhtml";


this.TabGroupsMigrator = {
  bookmarkedGroupsPromise: null,

  /**
   * If this state contains tab groups, migrate the user's data. This means:
   * - make a backup of the user's data.
   * - create bookmarks of all the user's tab groups in a single folder
   * - append a tab to the active window that lets the user restore background
   *   groups.
   * - remove all the tabs hidden through tab groups from the state data.
   */
  migrate(stateAsSupportsString) {
    stateAsSupportsString.QueryInterface(Ci.nsISupportsString);
    let stateStr = stateAsSupportsString.data;
    // If this is the very first startup of this profile, this is going to be empty:
    if (!stateStr) {
      return;
    }
    let state;
    try {
      state = JSON.parse(stateStr);
    } catch (ex) {
      Cu.reportError("Failed to parse sessionstore state JSON to migrate tab groups: " + ex);
      return; // can't recover from invalid JSON
    }

    let groupData = this._gatherGroupData(state);

    // This strips out the hidden tab groups and puts them in a different object.
    // It also removes all tabview metadata.
    // We always do this, and always reassign the new state back into the
    // nsISupportsString for use by sessionstore, in order to tidy up the state
    // object.
    let hiddenTabState = this._removeHiddenTabGroupsFromState(state, groupData);

    // However, we will only create a backup file, bookmarks and a new tab to
    // restore hidden tabs if tabs were actually removed from |state| by
    // _removeHiddenTabGroupsFromState.
    if (hiddenTabState.windows.length) {
      // We create the backup with the original string, from before all our
      // changes:
      this._createBackup(stateStr);

      this._createBackgroundTabGroupRestorationPage(state, hiddenTabState);

      // Bookmark creation is async. We need to return synchronously,
      // so we purposefully don't wait for this to be finished here. We do
      // store the promise it creates and use that in the session restore page
      // to be able to link to the bookmarks folder...
      let bookmarksFinishedPromise = this._bookmarkAllGroupsFromState(groupData);
      // ... and we make sure we finish before shutting down:
      AsyncShutdown.profileBeforeChange.addBlocker(
        "Tab groups migration bookmarks",
        bookmarksFinishedPromise
      );
    }

    // We always write this back to ensure that any spurious tab groups data is
    // removed:
    stateAsSupportsString.data = JSON.stringify(state);
  },

  /**
   * Returns a Map from window state objects to per-window group data.
   * Specifically, the values in the Map are themselves Maps from group IDs to
   * JS Objects which have these properties:
   *  - tabGroupsMigrationTitle: the title of the group (or empty string)
   *  - tabs: an array of the tabs objects in this group.
   */
  _gatherGroupData(state) {
    let allGroupData = new Map();
    let globalAnonGroupID = 0;
    for (let win of state.windows) {
      if (win.extData && win.extData["tabview-group"]) {
        let groupInfo = {};
        try {
          groupInfo = JSON.parse(win.extData["tabview-group"]);
        } catch (ex) {
          // This is annoying, but we'll try to deal with this.
        }

        let windowGroupData = new Map();
        let activeGroupID = null;
        let tabsWithoutGroup = [];
        for (let tab of win.tabs) {
          let group;
          // Get a string group ID:
          try {
            let tabViewData = tab.extData && tab.extData["tabview-tab"] &&
                              JSON.parse(tab.extData["tabview-tab"]);
            if (tabViewData && ("groupID" in tabViewData)) {
              group = tabViewData.groupID + "";
            }
          } catch (ex) {
            // Ignore errors reading group info, treat as active group
          }
          if (!group) {
            // We didn't find group info. If we already have an active group,
            // pretend this is part of that group:
            if (activeGroupID) {
              group = activeGroupID;
            } else {
              if (!tabsWithoutGroup) {
                Cu.reportError("ERROR: the list of tabs without groups was " +
                               "nulled out, but there's no active group ID? " +
                               "This should never happen!");
                tabsWithoutGroup = [];
              }
              // Otherwise, add to the list of tabs with no group and move to
              // the next tab immediately. We'll add all these tabs at the
              // beginning of the active group as soon as we find a tab in it,
              // so as to preserve their order.
              tabsWithoutGroup.push(tab);
              continue;
            }
          }
          let groupData = windowGroupData.get(group);
          if (!groupData) {
            let title = (groupInfo[group] && groupInfo[group].title) || "";
            groupData = {
              tabs: [],
              tabGroupsMigrationTitle: title,
            };
            if (!title) {
              groupData.anonGroupID = ++globalAnonGroupID;
              groupData.tabGroupsMigrationTitle =
                gBrowserBundle.formatStringFromName("tabgroups.migration.anonGroup",
                                                    [groupData.anonGroupID], 1);
            }
            // If this is the active group, set the active group ID and add
            // all the already-known tabs (that didn't list a group ID), if any.
            if (!activeGroupID && !tab.hidden) {
              activeGroupID = group;
              groupData.tabs = tabsWithoutGroup;
              tabsWithoutGroup = null;
            }
            windowGroupData.set(group, groupData);
          }
          groupData.tabs.push(tab);
        }

        // If we found tabs but no active group, assume there's just 1 group:
        if (tabsWithoutGroup && tabsWithoutGroup.length) {
          windowGroupData.set("active group", {
            tabs: tabsWithoutGroup,
            anonGroupID: ++globalAnonGroupID,
          });
        }

        allGroupData.set(win, windowGroupData);
      }
    }
    return allGroupData;
  },

  _createBackup(stateStr) {
    let dest = Services.dirsvc.get("ProfD", Ci.nsIFile);
    dest.append("tabgroups-session-backup.json");
    let promise = OS.File.writeAtomic(dest.path, stateStr, {encoding: "utf-8"});
    AsyncShutdown.webWorkersShutdown.addBlocker("TabGroupsMigrator", promise);
    return promise;
  },

  _groupSorter(a, b) {
    if (!a.anonGroupID) {
      return -1;
    }
    if (!b.anonGroupID) {
      return 1;
    }
    return a.anonGroupID - b.anonGroupID;
  },

  _bookmarkAllGroupsFromState: Task.async(function*(groupData) {
    // First create a folder in which to put all these bookmarks:
    this.bookmarkedGroupsPromise = PlacesUtils.bookmarks.insert({
      parentGuid: PlacesUtils.bookmarks.menuGuid,
      type: PlacesUtils.bookmarks.TYPE_FOLDER,
      index: 0,
      title: gBrowserBundle.GetStringFromName("tabgroups.migration.tabGroupBookmarkFolderName"),
    }).catch(Cu.reportError);
    let tabgroupsFolder = yield this.bookmarkedGroupsPromise;

    for (let [, windowGroupMap] of groupData) {
      let windowGroups = [... windowGroupMap.values()].sort(this._groupSorter);
      for (let group of windowGroups) {
        let groupFolder = yield PlacesUtils.bookmarks.insert({
          parentGuid: tabgroupsFolder.guid,
          type: PlacesUtils.bookmarks.TYPE_FOLDER,
          title: group.tabGroupsMigrationTitle
        }).catch(Cu.reportError);

        for (let tab of group.tabs) {
          let entry = tab.entries[tab.index - 1];
          yield PlacesUtils.bookmarks.insert({
            parentGuid: groupFolder.guid,
            title: tab.title || entry.title,
            url: entry.url,
          }).catch(Cu.reportError);
        }
      }
    }
  }),

  _removeHiddenTabGroupsFromState(state, groups) {
    let stateToReturn = {windows: []};
    for (let win of state.windows) {
      let groupInfoForWindow = groups.get(win);
      let hiddenGroupIDs = new Set();
      for (let i = win.tabs.length - 1; i >= 0; i--) {
        let tab =  win.tabs[i];
        // Determine whether the tab is grouped:
        let tabGroupInfo = null;
        try {
          tabGroupInfo = tab.extData && tab.extData["tabview-tab"] &&
                         JSON.parse(tab.extData["tabview-tab"]);
        } catch (ex) {}

        // Then remove this data.
        if (tab.extData) {
          delete tab.extData["tabview-tab"];
          if (Object.keys(tab.extData).length == 0) {
            delete tab.extData;
          }
        }

        // If the tab was grouped and hidden, remove it:
        if (tabGroupInfo && tab.hidden) {
          hiddenGroupIDs.add(tabGroupInfo.groupID);
          win.tabs.splice(i, 1);
          // Make sure we unhide it, or it won't get restored correctly
          tab.hidden = false;
        }
      }

      // We then convert any hidden groups into windows for the state object
      // we show in about:tabgroupsdata
      if (groupInfoForWindow) {
        let windowsToReturn = [];
        for (let groupID of hiddenGroupIDs) {
          let group = groupInfoForWindow.get("" + groupID);
          if (group) {
            windowsToReturn.push(group);
          }
        }
        windowsToReturn.sort(this._groupSorter);
        stateToReturn.windows = stateToReturn.windows.concat(windowsToReturn);
      }

      // Finally we remove tab groups data from the window:
      if (win.extData) {
        delete win.extData["tabview-group"];
        delete win.extData["tabview-groups"];
        delete win.extData["tabview-ui"];
        delete win.extData["tabview-visibility"];
        if (Object.keys(win.extData).length == 0) {
          delete win.extData;
        }
      }
    }
    return stateToReturn;
  },

  _createBackgroundTabGroupRestorationPage(state, backgroundData) {
    let win = state.windows[(state.selectedWindow || 1) - 1];
    let formdata = {id: {sessionData: JSON.stringify(backgroundData)}, url: RECOVERY_URL};
    let newTab = { entries: [{url: RECOVERY_URL}], formdata, index: 1 };
    // Add tab and mark it as selected:
    win.selected = win.tabs.push(newTab);
  },
};