forked from mirrors/gecko-dev
		
	***
Bug 1514594: Part 3a - Change ChromeUtils.import to return an exports object; not pollute global. r=mccr8
This changes the behavior of ChromeUtils.import() to return an exports object,
rather than a module global, in all cases except when `null` is passed as a
second argument, and changes the default behavior not to pollute the global
scope with the module's exports. Thus, the following code written for the old
model:
  ChromeUtils.import("resource://gre/modules/Services.jsm");
is approximately the same as the following, in the new model:
  var {Services} = ChromeUtils.import("resource://gre/modules/Services.jsm");
Since the two behaviors are mutually incompatible, this patch will land with a
scripted rewrite to update all existing callers to use the new model rather
than the old.
***
Bug 1514594: Part 3b - Mass rewrite all JS code to use the new ChromeUtils.import API. rs=Gijs
This was done using the followng script:
https://bitbucket.org/kmaglione/m-c-rewrites/src/tip/processors/cu-import-exports.jsm
***
Bug 1514594: Part 3c - Update ESLint plugin for ChromeUtils.import API changes. r=Standard8
Differential Revision: https://phabricator.services.mozilla.com/D16747
***
Bug 1514594: Part 3d - Remove/fix hundreds of duplicate imports from sync tests. r=Gijs
Differential Revision: https://phabricator.services.mozilla.com/D16748
***
Bug 1514594: Part 3e - Remove no-op ChromeUtils.import() calls. r=Gijs
Differential Revision: https://phabricator.services.mozilla.com/D16749
***
Bug 1514594: Part 3f.1 - Cleanup various test corner cases after mass rewrite. r=Gijs
***
Bug 1514594: Part 3f.2 - Cleanup various non-test corner cases after mass rewrite. r=Gijs
Differential Revision: https://phabricator.services.mozilla.com/D16750
--HG--
extra : rebase_source : 359574ee3064c90f33bf36c2ebe3159a24cc8895
extra : histedit_source : b93c8f42808b1599f9122d7842d2c0b3e656a594%2C64a3a4e3359dc889e2ab2b49461bab9e27fc10a7
		
	
			
		
			
				
	
	
		
			108 lines
		
	
	
	
		
			4.2 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
			
		
		
	
	
			108 lines
		
	
	
	
		
			4.2 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
/* Any copyright is dedicated to the Public Domain.
 | 
						|
 * http://creativecommons.org/publicdomain/zero/1.0/
 | 
						|
 */
 | 
						|
"use strict";
 | 
						|
 | 
						|
const {AppConstants} = ChromeUtils.import("resource://gre/modules/AppConstants.jsm");
 | 
						|
const {AttributionCode} = ChromeUtils.import("resource:///modules/AttributionCode.jsm");
 | 
						|
const {OS} = ChromeUtils.import("resource://gre/modules/osfile.jsm");
 | 
						|
const {Services} = ChromeUtils.import("resource://gre/modules/Services.jsm");
 | 
						|
 | 
						|
let validAttrCodes = [
 | 
						|
  {code: "source%3Dgoogle.com%26medium%3Dorganic%26campaign%3D(not%20set)%26content%3D(not%20set)",
 | 
						|
   parsed: {"source": "google.com", "medium": "organic",
 | 
						|
            "campaign": "(not%20set)", "content": "(not%20set)"}},
 | 
						|
  {code: "source%3Dgoogle.com%26medium%3Dorganic%26campaign%3D%26content%3D",
 | 
						|
   parsed: {"source": "google.com", "medium": "organic"}},
 | 
						|
  {code: "source%3Dgoogle.com%26medium%3Dorganic%26campaign%3D(not%20set)",
 | 
						|
   parsed: {"source": "google.com", "medium": "organic", "campaign": "(not%20set)"}},
 | 
						|
  {code: "source%3Dgoogle.com%26medium%3Dorganic",
 | 
						|
   parsed: {"source": "google.com", "medium": "organic"}},
 | 
						|
  {code: "source%3Dgoogle.com",
 | 
						|
   parsed: {"source": "google.com"}},
 | 
						|
  {code: "medium%3Dgoogle.com",
 | 
						|
   parsed: {"medium": "google.com"}},
 | 
						|
  {code: "campaign%3Dgoogle.com",
 | 
						|
   parsed: {"campaign": "google.com"}},
 | 
						|
  {code: "content%3Dgoogle.com",
 | 
						|
   parsed: {"content": "google.com"}},
 | 
						|
];
 | 
						|
 | 
						|
let invalidAttrCodes = [
 | 
						|
  // Empty string
 | 
						|
  "",
 | 
						|
  // Not escaped
 | 
						|
  "source=google.com&medium=organic&campaign=(not set)&content=(not set)",
 | 
						|
  // Too long
 | 
						|
  "source%3Dreallyreallyreallyreallyreallyreallyreallyreallyreallylongdomain.com%26medium%3Dorganic%26campaign%3D(not%20set)%26content%3Dalmostexactlyenoughcontenttomakethisstringlongerthanthe200characterlimit",
 | 
						|
  // Unknown key name
 | 
						|
  "source%3Dgoogle.com%26medium%3Dorganic%26large%3Dgeneticallymodified",
 | 
						|
  // Empty key name
 | 
						|
  "source%3Dgoogle.com%26medium%3Dorganic%26%3Dgeneticallymodified",
 | 
						|
];
 | 
						|
 | 
						|
async function writeAttributionFile(data) {
 | 
						|
  let appDir = Services.dirsvc.get("LocalAppData", Ci.nsIFile);
 | 
						|
  let file = appDir.clone();
 | 
						|
  file.append(Services.appinfo.vendor || "mozilla");
 | 
						|
  file.append(AppConstants.MOZ_APP_NAME);
 | 
						|
 | 
						|
  await OS.File.makeDir(file.path,
 | 
						|
    {from: appDir.path, ignoreExisting: true});
 | 
						|
 | 
						|
  file.append("postSigningData");
 | 
						|
  await OS.File.writeAtomic(file.path, data);
 | 
						|
}
 | 
						|
 | 
						|
/**
 | 
						|
 * Test validation of attribution codes,
 | 
						|
 * to make sure we reject bad ones and accept good ones.
 | 
						|
 */
 | 
						|
add_task(async function testValidAttrCodes() {
 | 
						|
  for (let entry of validAttrCodes) {
 | 
						|
    AttributionCode._clearCache();
 | 
						|
    await writeAttributionFile(entry.code);
 | 
						|
    let result = await AttributionCode.getAttrDataAsync();
 | 
						|
    Assert.deepEqual(result, entry.parsed,
 | 
						|
      "Parsed code should match expected value, code was: " + entry.code);
 | 
						|
  }
 | 
						|
  AttributionCode._clearCache();
 | 
						|
});
 | 
						|
 | 
						|
/**
 | 
						|
 * Make sure codes with various formatting errors are not seen as valid.
 | 
						|
 */
 | 
						|
add_task(async function testInvalidAttrCodes() {
 | 
						|
  for (let code of invalidAttrCodes) {
 | 
						|
    AttributionCode._clearCache();
 | 
						|
    await writeAttributionFile(code);
 | 
						|
    let result = await AttributionCode.getAttrDataAsync();
 | 
						|
    Assert.deepEqual(result, {},
 | 
						|
      "Code should have failed to parse: " + code);
 | 
						|
  }
 | 
						|
  AttributionCode._clearCache();
 | 
						|
});
 | 
						|
 | 
						|
/**
 | 
						|
 * Test the cache by deleting the attribution data file
 | 
						|
 * and making sure we still get the expected code.
 | 
						|
 */
 | 
						|
add_task(async function testDeletedFile() {
 | 
						|
  // Set up the test by clearing the cache and writing a valid file.
 | 
						|
  await writeAttributionFile(validAttrCodes[0].code);
 | 
						|
  let result = await AttributionCode.getAttrDataAsync();
 | 
						|
  Assert.deepEqual(result, validAttrCodes[0].parsed,
 | 
						|
    "The code should be readable directly from the file");
 | 
						|
 | 
						|
  // Delete the file and make sure we can still read the value back from cache.
 | 
						|
  await AttributionCode.deleteFileAsync();
 | 
						|
  result = await AttributionCode.getAttrDataAsync();
 | 
						|
  Assert.deepEqual(result, validAttrCodes[0].parsed,
 | 
						|
    "The code should be readable from the cache");
 | 
						|
 | 
						|
  // Clear the cache and check we can't read anything.
 | 
						|
  AttributionCode._clearCache();
 | 
						|
  result = await AttributionCode.getAttrDataAsync();
 | 
						|
  Assert.deepEqual(result, {},
 | 
						|
    "Shouldn't be able to get a code after file is deleted and cache is cleared");
 | 
						|
});
 |