Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
michael@0 | 3 | * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | const { classes: Cc, interfaces: Ci, utils: Cu, results: Cr } = Components; |
michael@0 | 6 | Cu.import("resource://gre/modules/Services.jsm"); |
michael@0 | 7 | Cu.import("resource://gre/modules/XPCOMUtils.jsm"); |
michael@0 | 8 | |
michael@0 | 9 | var Social, SocialService; |
michael@0 | 10 | |
michael@0 | 11 | let manifests = [ |
michael@0 | 12 | { |
michael@0 | 13 | name: "provider 1", |
michael@0 | 14 | origin: "https://example1.com", |
michael@0 | 15 | sidebarURL: "https://example1.com/sidebar/", |
michael@0 | 16 | }, |
michael@0 | 17 | { |
michael@0 | 18 | name: "provider 2", |
michael@0 | 19 | origin: "https://example2.com", |
michael@0 | 20 | sidebarURL: "https://example1.com/sidebar/", |
michael@0 | 21 | } |
michael@0 | 22 | ]; |
michael@0 | 23 | |
michael@0 | 24 | const MANIFEST_PREFS = Services.prefs.getBranch("social.manifest."); |
michael@0 | 25 | |
michael@0 | 26 | // SocialProvider class relies on blocklisting being enabled. To enable |
michael@0 | 27 | // blocklisting, we have to setup an app and initialize the blocklist (see |
michael@0 | 28 | // initApp below). |
michael@0 | 29 | const gProfD = do_get_profile(); |
michael@0 | 30 | |
michael@0 | 31 | const XULAPPINFO_CONTRACTID = "@mozilla.org/xre/app-info;1"; |
michael@0 | 32 | const XULAPPINFO_CID = Components.ID("{c763b610-9d49-455a-bbd2-ede71682a1ac}"); |
michael@0 | 33 | |
michael@0 | 34 | function createAppInfo(id, name, version, platformVersion) { |
michael@0 | 35 | gAppInfo = { |
michael@0 | 36 | // nsIXULAppInfo |
michael@0 | 37 | vendor: "Mozilla", |
michael@0 | 38 | name: name, |
michael@0 | 39 | ID: id, |
michael@0 | 40 | version: version, |
michael@0 | 41 | appBuildID: "2007010101", |
michael@0 | 42 | platformVersion: platformVersion ? platformVersion : "1.0", |
michael@0 | 43 | platformBuildID: "2007010101", |
michael@0 | 44 | |
michael@0 | 45 | // nsIXULRuntime |
michael@0 | 46 | inSafeMode: false, |
michael@0 | 47 | logConsoleErrors: true, |
michael@0 | 48 | OS: "XPCShell", |
michael@0 | 49 | XPCOMABI: "noarch-spidermonkey", |
michael@0 | 50 | invalidateCachesOnRestart: function invalidateCachesOnRestart() { |
michael@0 | 51 | // Do nothing |
michael@0 | 52 | }, |
michael@0 | 53 | |
michael@0 | 54 | // nsICrashReporter |
michael@0 | 55 | annotations: {}, |
michael@0 | 56 | |
michael@0 | 57 | annotateCrashReport: function(key, data) { |
michael@0 | 58 | this.annotations[key] = data; |
michael@0 | 59 | }, |
michael@0 | 60 | |
michael@0 | 61 | QueryInterface: XPCOMUtils.generateQI([Ci.nsIXULAppInfo, |
michael@0 | 62 | Ci.nsIXULRuntime, |
michael@0 | 63 | Ci.nsICrashReporter, |
michael@0 | 64 | Ci.nsISupports]) |
michael@0 | 65 | }; |
michael@0 | 66 | |
michael@0 | 67 | var XULAppInfoFactory = { |
michael@0 | 68 | createInstance: function (outer, iid) { |
michael@0 | 69 | if (outer != null) |
michael@0 | 70 | throw Components.results.NS_ERROR_NO_AGGREGATION; |
michael@0 | 71 | return gAppInfo.QueryInterface(iid); |
michael@0 | 72 | } |
michael@0 | 73 | }; |
michael@0 | 74 | var registrar = Components.manager.QueryInterface(Ci.nsIComponentRegistrar); |
michael@0 | 75 | registrar.registerFactory(XULAPPINFO_CID, "XULAppInfo", |
michael@0 | 76 | XULAPPINFO_CONTRACTID, XULAppInfoFactory); |
michael@0 | 77 | } |
michael@0 | 78 | |
michael@0 | 79 | function initApp() { |
michael@0 | 80 | createAppInfo("xpcshell@tests.mozilla.org", "XPCShell", "1", "1.9"); |
michael@0 | 81 | // prepare a blocklist file for the blocklist service |
michael@0 | 82 | var blocklistFile = gProfD.clone(); |
michael@0 | 83 | blocklistFile.append("blocklist.xml"); |
michael@0 | 84 | if (blocklistFile.exists()) |
michael@0 | 85 | blocklistFile.remove(false); |
michael@0 | 86 | var source = do_get_file("blocklist.xml"); |
michael@0 | 87 | source.copyTo(gProfD, "blocklist.xml"); |
michael@0 | 88 | blocklistFile.lastModifiedTime = Date.now(); |
michael@0 | 89 | |
michael@0 | 90 | |
michael@0 | 91 | let internalManager = Cc["@mozilla.org/addons/integration;1"]. |
michael@0 | 92 | getService(Ci.nsIObserver). |
michael@0 | 93 | QueryInterface(Ci.nsITimerCallback); |
michael@0 | 94 | |
michael@0 | 95 | internalManager.observe(null, "addons-startup", null); |
michael@0 | 96 | } |
michael@0 | 97 | |
michael@0 | 98 | function setManifestPref(manifest) { |
michael@0 | 99 | let string = Cc["@mozilla.org/supports-string;1"]. |
michael@0 | 100 | createInstance(Ci.nsISupportsString); |
michael@0 | 101 | string.data = JSON.stringify(manifest); |
michael@0 | 102 | Services.prefs.setComplexValue("social.manifest." + manifest.origin, Ci.nsISupportsString, string); |
michael@0 | 103 | } |
michael@0 | 104 | |
michael@0 | 105 | function do_wait_observer(topic, cb) { |
michael@0 | 106 | function observer(subject, topic, data) { |
michael@0 | 107 | Services.obs.removeObserver(observer, topic); |
michael@0 | 108 | cb(); |
michael@0 | 109 | } |
michael@0 | 110 | Services.obs.addObserver(observer, topic, false); |
michael@0 | 111 | } |
michael@0 | 112 | |
michael@0 | 113 | function do_add_providers(cb) { |
michael@0 | 114 | // run only after social is already initialized |
michael@0 | 115 | SocialService.addProvider(manifests[0], function() { |
michael@0 | 116 | do_wait_observer("social:providers-changed", function() { |
michael@0 | 117 | do_check_eq(Social.providers.length, 2, "2 providers installed"); |
michael@0 | 118 | do_execute_soon(cb); |
michael@0 | 119 | }); |
michael@0 | 120 | SocialService.addProvider(manifests[1]); |
michael@0 | 121 | }); |
michael@0 | 122 | } |
michael@0 | 123 | |
michael@0 | 124 | function do_initialize_social(enabledOnStartup, cb) { |
michael@0 | 125 | initApp(); |
michael@0 | 126 | |
michael@0 | 127 | if (enabledOnStartup) { |
michael@0 | 128 | // set prefs before initializing social |
michael@0 | 129 | manifests.forEach(function (manifest) { |
michael@0 | 130 | setManifestPref(manifest); |
michael@0 | 131 | }); |
michael@0 | 132 | // Set both providers active and flag the first one as "current" |
michael@0 | 133 | let activeVal = Cc["@mozilla.org/supports-string;1"]. |
michael@0 | 134 | createInstance(Ci.nsISupportsString); |
michael@0 | 135 | let active = {}; |
michael@0 | 136 | for (let m of manifests) |
michael@0 | 137 | active[m.origin] = 1; |
michael@0 | 138 | activeVal.data = JSON.stringify(active); |
michael@0 | 139 | Services.prefs.setComplexValue("social.activeProviders", |
michael@0 | 140 | Ci.nsISupportsString, activeVal); |
michael@0 | 141 | |
michael@0 | 142 | do_register_cleanup(function() { |
michael@0 | 143 | manifests.forEach(function (manifest) { |
michael@0 | 144 | Services.prefs.clearUserPref("social.manifest." + manifest.origin); |
michael@0 | 145 | }); |
michael@0 | 146 | Services.prefs.clearUserPref("social.activeProviders"); |
michael@0 | 147 | }); |
michael@0 | 148 | |
michael@0 | 149 | // expecting 2 providers installed |
michael@0 | 150 | do_wait_observer("social:providers-changed", function() { |
michael@0 | 151 | do_check_eq(Social.providers.length, 2, "2 providers installed"); |
michael@0 | 152 | do_execute_soon(cb); |
michael@0 | 153 | }); |
michael@0 | 154 | } |
michael@0 | 155 | |
michael@0 | 156 | // import and initialize everything |
michael@0 | 157 | SocialService = Cu.import("resource://gre/modules/SocialService.jsm", {}).SocialService; |
michael@0 | 158 | do_check_eq(enabledOnStartup, SocialService.hasEnabledProviders, "Service has enabled providers"); |
michael@0 | 159 | Social = Cu.import("resource:///modules/Social.jsm", {}).Social; |
michael@0 | 160 | do_check_false(Social.initialized, "Social is not initialized"); |
michael@0 | 161 | Social.init(); |
michael@0 | 162 | do_check_true(Social.initialized, "Social is initialized"); |
michael@0 | 163 | if (!enabledOnStartup) |
michael@0 | 164 | do_execute_soon(cb); |
michael@0 | 165 | } |