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.

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 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
/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*-
 * 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/. */
ChromeUtils.import("resource://gre/modules/XPCOMUtils.jsm", this);

XPCOMUtils.defineLazyModuleGetters(this, {
  SessionStore: "resource:///modules/sessionstore/SessionStore.jsm",
});

function UpdateSessionStore(
  aBrowser,
  aFlushId,
  aIsFinal,
  aEpoch,
  aData,
  aCollectSHistory
) {
  return SessionStoreFuncInternal.updateSessionStore(
    aBrowser,
    aFlushId,
    aIsFinal,
    aEpoch,
    aData,
    aCollectSHistory
  );
}

var EXPORTED_SYMBOLS = ["UpdateSessionStore"];

var SessionStoreFuncInternal = {
  // form data which is waiting to be updated
  _formDataId: [],
  _formDataIdValue: [],
  _formDataXPath: [],
  _formDataXPathValue: [],

  /**
   * The data will be stored in the arrays:
   *   "_formDataId, _formDataIdValue" for the elements with id.
   *   "_formDataXPath, _formDataXPathValue" for the elements with XPath.
   */
  updateFormData: function SSF_updateFormData(aType, aData) {
    let idArray = this._formDataId;
    let valueArray = this._formDataIdValue;

    if (aType == "XPath") {
      idArray = this._formDataXPath;
      valueArray = this._formDataXPathValue;
    }

    let valueIdx = aData.valueIdx;
    for (let i = 0; i < aData.id.length; i++) {
      idArray.push(aData.id[i]);

      if (aData.type[i] == "singleSelect") {
        valueArray.push({
          selectedIndex: aData.selectedIndex[valueIdx[i]],
          value: aData.selectVal[valueIdx[i]],
        });
      } else if (aData.type[i] == "file") {
        valueArray.push({
          type: "file",
          fileList: aData.strVal.slice(valueIdx[i], valueIdx[++i]),
        });
      } else if (aData.type[i] == "multipleSelect") {
        valueArray.push(aData.strVal.slice(valueIdx[i], valueIdx[++i]));
      } else if (aData.type[i] == "string") {
        valueArray.push(aData.strVal[valueIdx[i]]);
      } else if (aData.type[i] == "bool") {
        valueArray.push(aData.boolVal[valueIdx[i]]);
      }
    }
  },

  /**
   * Return the array of formdata for this._sessionData.formdata.children
   *
   * aStartIndex:       Current index for aInnerHTML/aUrl/aNumId/aNumXPath/aDescendants.
   *                    (aStartIndex means the index of current root frame)
   * aInnerHTML:        Array for innerHTML.
   * aUrl:              Array for url.
   * aNumId:            Array for number of containing elements with id
   * aNumXPath:         Array for number of containing elements with XPath
   * aDescendants:      Array for number of descendants.
   *
   * aCurrentIdIdx:     Current index for this._formDataId and this._formDataIdValue
   * aCurrentXPathIdx:  Current index for this._formDataXPath and this._formDataXPathValue
   * aNumberOfDescendants: The number of descendants for current frame
   *
   * The returned array includes "aNumberOfDescendants" formdata objects.
   */
  composeInputChildren: function SSF_composeInputChildren(
    aInnerHTML,
    aUrl,
    aCurrentIdIdx,
    aNumId,
    aCurrentXpathIdx,
    aNumXPath,
    aDescendants,
    aStartIndex,
    aNumberOfDescendants
  ) {
    let children = [];
    let lastIndexOfNonNullbject = -1;
    for (let i = 0; i < aNumberOfDescendants; i++) {
      let currentIndex = aStartIndex + i;
      let obj = {};
      let objWithData = false;

      // set url/id/xpath
      if (aUrl[currentIndex]) {
        obj.url = aUrl[currentIndex];
        objWithData = true;

        if (aInnerHTML[currentIndex]) {
          // eslint-disable-next-line no-unsanitized/property
          obj.innerHTML = aInnerHTML[currentIndex];
        }
        if (aNumId[currentIndex]) {
          let idObj = {};
          for (let idx = 0; idx < aNumId[currentIndex]; idx++) {
            idObj[
              this._formDataId[aCurrentIdIdx + idx]
            ] = this._formDataIdValue[aCurrentIdIdx + idx];
          }
          obj.id = idObj;
        }

        // We want to avoid saving data for about:sessionrestore as a string.
        // Since it's stored in the form as stringified JSON, stringifying further
        // causes an explosion of escape characters. cf. bug 467409
        if (
          obj.url == "about:sessionrestore" ||
          obj.url == "about:welcomeback"
        ) {
          obj.id.sessionData = JSON.parse(obj.id.sessionData);
        }

        if (aNumXPath[currentIndex]) {
          let xpathObj = {};
          for (let idx = 0; idx < aNumXPath[currentIndex]; idx++) {
            xpathObj[
              this._formDataXPath[aCurrentXpathIdx + idx]
            ] = this._formDataXPathValue[aCurrentXpathIdx + idx];
          }
          obj.xpath = xpathObj;
        }
      }

      // compose the descendantsTree which will be pushed into children array
      if (aDescendants[currentIndex]) {
        let descendantsTree = this.composeInputChildren(
          aInnerHTML,
          aUrl,
          aCurrentIdIdx + aNumId[currentIndex],
          aNumId,
          aCurrentXpathIdx + aNumXPath[currentIndex],
          aNumXPath,
          aDescendants,
          currentIndex + 1,
          aDescendants[currentIndex]
        );
        i += aDescendants[currentIndex];
        if (descendantsTree) {
          obj.children = descendantsTree;
        }
      }

      if (objWithData) {
        lastIndexOfNonNullbject = children.length;
        children.push(obj);
      } else {
        children.push(null);
      }
    }

    if (lastIndexOfNonNullbject == -1) {
      return null;
    }

    return children.slice(0, lastIndexOfNonNullbject + 1);
  },

  /**
   * Update the object for this._sessionData.formdata.
   * The object contains the formdata for all reachable frames.
   *
   * "object.children" is an array with one entry per frame,
   * containing formdata as a nested data structure according
   * to the layout of the frame tree, or null if no formdata.
   *
   * Example:
   *   {
   *     url: "http://mozilla.org/",
   *     id: {input_id: "input value"},
   *     xpath: {input_xpath: "input value"},
   *     children: [
   *       null,
   *       {url: "http://sub.mozilla.org/", id: {input_id: "input value 2"}}
   *     ]
   *   }
   *
   * Each index of the following array is corresponging to each frame.
   * aDescendants: Array for number of descendants
   * aInnerHTML:   Array for innerHTML
   * aUrl:         Array for url
   * aNumId:       Array for number of containing elements with id
   * aNumXPath:    Array for number of containing elements with XPath
   *
   * Here we use [index 0] to compose the formdata object of root frame.
   * Besides, we use composeInputChildren() to get array of "object.children".
   */
  updateInput: function SSF_updateInput(
    aSessionData,
    aDescendants,
    aInnerHTML,
    aUrl,
    aNumId,
    aNumXPath
  ) {
    let obj = {};
    let objWithData = false;

    if (aUrl[0]) {
      obj.url = aUrl[0];

      if (aInnerHTML[0]) {
        // eslint-disable-next-line no-unsanitized/property
        obj.innerHTML = aInnerHTML[0];
        objWithData = true;
      }

      if (aNumId[0]) {
        let idObj = {};
        for (let i = 0; i < aNumId[0]; i++) {
          idObj[this._formDataId[i]] = this._formDataIdValue[i];
        }
        obj.id = idObj;
        objWithData = true;
      }

      // We want to avoid saving data for about:sessionrestore as a string.
      // Since it's stored in the form as stringified JSON, stringifying further
      // causes an explosion of escape characters. cf. bug 467409
      if (obj.url == "about:sessionrestore" || obj.url == "about:welcomeback") {
        obj.id.sessionData = JSON.parse(obj.id.sessionData);
      }

      if (aNumXPath[0]) {
        let xpathObj = {};
        for (let i = 0; i < aNumXPath[0]; i++) {
          xpathObj[this._formDataXPath[i]] = this._formDataXPathValue[i];
        }
        obj.xpath = xpathObj;
        objWithData = true;
      }
    }

    if (aDescendants.length > 1) {
      let descendantsTree = this.composeInputChildren(
        aInnerHTML,
        aUrl,
        aNumId[0],
        aNumId,
        aNumXPath[0],
        aNumXPath,
        aDescendants,
        1,
        aDescendants[0]
      );
      if (descendantsTree) {
        obj.children = descendantsTree;
        objWithData = true;
      }
    }

    if (objWithData) {
      aSessionData.formdata = obj;
    } else {
      aSessionData.formdata = null;
    }
  },

  composeChildren: function SSF_composeScrollPositionsData(
    aPositions,
    aDescendants,
    aStartIndex,
    aNumberOfDescendants
  ) {
    let children = [];
    let lastIndexOfNonNullbject = -1;
    for (let i = 0; i < aNumberOfDescendants; i++) {
      let currentIndex = aStartIndex + i;
      let obj = {};
      let objWithData = false;
      if (aPositions[currentIndex]) {
        obj.scroll = aPositions[currentIndex];
        objWithData = true;
      }
      if (aDescendants[currentIndex]) {
        let descendantsTree = this.composeChildren(
          aPositions,
          aDescendants,
          currentIndex + 1,
          aDescendants[currentIndex]
        );
        i += aDescendants[currentIndex];
        if (descendantsTree) {
          obj.children = descendantsTree;
          objWithData = true;
        }
      }

      if (objWithData) {
        lastIndexOfNonNullbject = children.length;
        children.push(obj);
      } else {
        children.push(null);
      }
    }

    if (lastIndexOfNonNullbject == -1) {
      return null;
    }

    return children.slice(0, lastIndexOfNonNullbject + 1);
  },

  updateScrollPositions: function SSF_updateScrollPositions(
    aPositions,
    aDescendants
  ) {
    let obj = {};
    let objWithData = false;

    if (aPositions[0]) {
      obj.scroll = aPositions[0];
      objWithData = true;
    }

    if (aPositions.length > 1) {
      let children = this.composeChildren(
        aPositions,
        aDescendants,
        1,
        aDescendants[0]
      );
      if (children) {
        obj.children = children;
        objWithData = true;
      }
    }
    if (objWithData) {
      return obj;
    }
    return null;
  },

  updateStorage: function SSF_updateStorage(aOrigins, aKeys, aValues) {
    let data = {};
    for (let i = 0; i < aOrigins.length; i++) {
      // If the key isn't defined, then .clear() was called, and we send
      // up null for this domain to indicate that storage has been cleared
      // for it.
      if (aKeys[i] == "") {
        while (aOrigins[i + 1] == aOrigins[i]) {
          i++;
        }
        data[aOrigins[i]] = null;
      } else {
        let hostData = {};
        hostData[aKeys[i]] = aValues[i];
        while (aOrigins[i + 1] == aOrigins[i]) {
          i++;
          hostData[aKeys[i]] = aValues[i];
        }
        data[aOrigins[i]] = hostData;
      }
    }
    if (aOrigins.length) {
      return data;
    }

    return null;
  },

  updateSessionStore: function SSF_updateSessionStore(
    aBrowser,
    aFlushId,
    aIsFinal,
    aEpoch,
    aData,
    aCollectSHistory
  ) {
    let currentData = {};
    if (aData.docShellCaps != undefined) {
      currentData.disallow = aData.docShellCaps ? aData.docShellCaps : null;
    }
    if (aData.isPrivate != undefined) {
      currentData.isPrivate = aData.isPrivate;
    }
    if (
      aData.positions != undefined &&
      aData.positionDescendants != undefined
    ) {
      currentData.scroll = this.updateScrollPositions(
        aData.positions,
        aData.positionDescendants
      );
    }
    if (aData.id != undefined) {
      this.updateFormData("id", aData.id);
    }
    if (aData.xpath != undefined) {
      this.updateFormData("XPath", aData.xpath);
    }
    if (aData.inputDescendants != undefined) {
      this.updateInput(
        currentData,
        aData.inputDescendants,
        aData.innerHTML,
        aData.url,
        aData.numId,
        aData.numXPath
      );
    }
    if (aData.isFullStorage != undefined) {
      let storage = this.updateStorage(
        aData.storageOrigins,
        aData.storageKeys,
        aData.storageValues
      );
      if (aData.isFullStorage) {
        currentData.storage = storage;
      } else {
        currentData.storagechange = storage;
      }
    }

    SessionStore.updateSessionStoreFromTablistener(aBrowser, {
      data: currentData,
      flushID: aFlushId,
      isFinal: aIsFinal,
      epoch: aEpoch,
      sHistoryNeeded: aCollectSHistory,
    });
    this._formDataId = [];
    this._formDataIdValue = [];
    this._formDataXPath = [];
    this._formDataXPathValue = [];
  },
};