distribution.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. this.EXPORTED_SYMBOLS = [ "DistributionCustomizer" ];
  5. var Ci = Components.interfaces;
  6. var Cc = Components.classes;
  7. var Cr = Components.results;
  8. var Cu = Components.utils;
  9. const DISTRIBUTION_CUSTOMIZATION_COMPLETE_TOPIC =
  10. "distribution-customization-complete";
  11. Cu.import("resource://gre/modules/XPCOMUtils.jsm");
  12. XPCOMUtils.defineLazyModuleGetter(this, "PlacesUtils",
  13. "resource://gre/modules/PlacesUtils.jsm");
  14. this.DistributionCustomizer = function DistributionCustomizer() {
  15. let dirSvc = Cc["@mozilla.org/file/directory_service;1"]
  16. .getService(Ci.nsIProperties);
  17. let iniFile = dirSvc.get("XREExeF", Ci.nsIFile);
  18. iniFile.leafName = "distribution";
  19. iniFile.append("distribution.ini");
  20. if (iniFile.exists()) {
  21. this._iniFile = iniFile;
  22. }
  23. }
  24. DistributionCustomizer.prototype = {
  25. _iniFile: null,
  26. get _ini() {
  27. let ini = Cc["@mozilla.org/xpcom/ini-parser-factory;1"]
  28. .getService(Ci.nsIINIParserFactory)
  29. .createINIParser(this._iniFile);
  30. this.__defineGetter__("_ini", function() ini);
  31. return this._ini;
  32. },
  33. get _locale() {
  34. let locale = this._prefs.getCharPref("general.useragent.locale", "en-US");
  35. this.__defineGetter__("_locale", function() locale);
  36. return this._locale;
  37. },
  38. get _prefSvc() {
  39. let svc = Cc["@mozilla.org/preferences-service;1"]
  40. .getService(Ci.nsIPrefService);
  41. this.__defineGetter__("_prefSvc", function() svc);
  42. return this._prefSvc;
  43. },
  44. get _prefs() {
  45. let branch = this._prefSvc.getBranch(null);
  46. this.__defineGetter__("_prefs", function() branch);
  47. return this._prefs;
  48. },
  49. get _ioSvc() {
  50. let svc = Cc["@mozilla.org/network/io-service;1"]
  51. .getService(Ci.nsIIOService);
  52. this.__defineGetter__("_ioSvc", function() svc);
  53. return this._ioSvc;
  54. },
  55. _makeURI: function(spec) {
  56. return this._ioSvc.newURI(spec, null, null);
  57. },
  58. _parseBookmarksSection:
  59. function(parentId, section) {
  60. let keys = [];
  61. for (let i in enumerate(this._ini.getKeys(section))) {
  62. keys.push(i);
  63. }
  64. keys.sort();
  65. let items = {};
  66. let defaultItemId = -1;
  67. let maxItemId = -1;
  68. for (let i = 0; i < keys.length; i++) {
  69. let m = /^item\.(\d+)\.(\w+)\.?(\w*)/.exec(keys[i]);
  70. if (m) {
  71. let [foo, iid, iprop, ilocale] = m;
  72. iid = parseInt(iid);
  73. if (ilocale) {
  74. continue;
  75. }
  76. if (!items[iid]) {
  77. items[iid] = {};
  78. }
  79. if (keys.indexOf(keys[i] + "." + this._locale) >= 0) {
  80. items[iid][iprop] = this._ini.getString(section, keys[i] + "." +
  81. this._locale);
  82. } else {
  83. items[iid][iprop] = this._ini.getString(section, keys[i]);
  84. }
  85. if (iprop == "type" && items[iid]["type"] == "default") {
  86. defaultItemId = iid;
  87. }
  88. if (maxItemId < iid) {
  89. maxItemId = iid;
  90. }
  91. } else {
  92. dump("Key did not match: " + keys[i] + "\n");
  93. }
  94. }
  95. let prependIndex = 0;
  96. for (let iid = 0; iid <= maxItemId; iid++) {
  97. if (!items[iid]) {
  98. continue;
  99. }
  100. let index = PlacesUtils.bookmarks.DEFAULT_INDEX;
  101. let newId;
  102. switch (items[iid]["type"]) {
  103. case "default":
  104. break;
  105. case "folder":
  106. if (iid < defaultItemId) {
  107. index = prependIndex++;
  108. }
  109. newId = PlacesUtils.bookmarks.createFolder(parentId,
  110. items[iid]["title"],
  111. index);
  112. this._parseBookmarksSection(newId, "BookmarksFolder-" +
  113. items[iid]["folderId"]);
  114. if (items[iid]["description"])
  115. PlacesUtils.annotations.setItemAnnotation(newId,
  116. "bookmarkProperties/description",
  117. items[iid]["description"], 0,
  118. PlacesUtils.annotations.EXPIRE_NEVER);
  119. break;
  120. case "separator":
  121. if (iid < defaultItemId) {
  122. index = prependIndex++;
  123. }
  124. PlacesUtils.bookmarks.insertSeparator(parentId, index);
  125. break;
  126. case "livemark":
  127. if (iid < defaultItemId) {
  128. index = prependIndex++;
  129. }
  130. // Don't bother updating the livemark contents on creation.
  131. PlacesUtils.livemarks.addLivemark({ title: items[iid]["title"],
  132. parentId: parentId,
  133. index: index,
  134. feedURI: this._makeURI(items[iid]["feedLink"]),
  135. siteURI: this._makeURI(items[iid]["siteLink"])
  136. }).then(null, Cu.reportError);
  137. break;
  138. case "bookmark":
  139. // Fallthrough
  140. default:
  141. if (iid < defaultItemId) {
  142. index = prependIndex++;
  143. }
  144. newId = PlacesUtils.bookmarks.insertBookmark(parentId,
  145. this._makeURI(items[iid]["link"]),
  146. index, items[iid]["title"]);
  147. if (items[iid]["description"]) {
  148. PlacesUtils.annotations.setItemAnnotation(newId, "bookmarkProperties/description",
  149. items[iid]["description"], 0,
  150. PlacesUtils.annotations.EXPIRE_NEVER);
  151. }
  152. break;
  153. }
  154. }
  155. },
  156. _customizationsApplied: false,
  157. applyCustomizations: function() {
  158. this._customizationsApplied = true;
  159. if (!this._iniFile) {
  160. return this._checkCustomizationComplete();
  161. }
  162. // nsPrefService loads very early. Reload prefs so we can set
  163. // distribution defaults during the prefservice:after-app-defaults
  164. // notification (see applyPrefDefaults below)
  165. this._prefSvc.QueryInterface(Ci.nsIObserver);
  166. this._prefSvc.observe(null, "reload-default-prefs", null);
  167. },
  168. _bookmarksApplied: false,
  169. applyBookmarks: function() {
  170. this._bookmarksApplied = true;
  171. if (!this._iniFile) {
  172. return this._checkCustomizationComplete();
  173. }
  174. let sections = enumToObject(this._ini.getSections());
  175. // The global section, and several of its fields, is required
  176. // (we also check here to be consistent with applyPrefDefaults below)
  177. if (!sections["Global"]) {
  178. return this._checkCustomizationComplete();
  179. }
  180. let globalPrefs = enumToObject(this._ini.getKeys("Global"));
  181. if (!(globalPrefs["id"] && globalPrefs["version"] && globalPrefs["about"])) {
  182. return this._checkCustomizationComplete();
  183. }
  184. let bmProcessedPref;
  185. try {
  186. bmProcessedPref = this._ini.getString("Global",
  187. "bookmarks.initialized.pref");
  188. } catch(e) {
  189. bmProcessedPref = "distribution." +
  190. this._ini.getString("Global", "id") + ".bookmarksProcessed";
  191. }
  192. let bmProcessed = this._prefs.getBoolPref(bmProcessedPref, false);
  193. if (!bmProcessed) {
  194. if (sections["BookmarksMenu"]) {
  195. this._parseBookmarksSection(PlacesUtils.bookmarksMenuFolderId,
  196. "BookmarksMenu");
  197. }
  198. if (sections["BookmarksToolbar"]) {
  199. this._parseBookmarksSection(PlacesUtils.toolbarFolderId,
  200. "BookmarksToolbar");
  201. }
  202. this._prefs.setBoolPref(bmProcessedPref, true);
  203. }
  204. return this._checkCustomizationComplete();
  205. },
  206. _prefDefaultsApplied: false,
  207. applyPrefDefaults: function() {
  208. this._prefDefaultsApplied = true;
  209. if (!this._iniFile) {
  210. return this._checkCustomizationComplete();
  211. }
  212. let sections = enumToObject(this._ini.getSections());
  213. // The global section, and several of its fields, is required
  214. if (!sections["Global"]) {
  215. return this._checkCustomizationComplete();
  216. }
  217. let globalPrefs = enumToObject(this._ini.getKeys("Global"));
  218. if (!(globalPrefs["id"] && globalPrefs["version"] && globalPrefs["about"])) {
  219. return this._checkCustomizationComplete();
  220. }
  221. let defaults = this._prefSvc.getDefaultBranch(null);
  222. // Global really contains info we set as prefs. They're only
  223. // separate because they are "special" (read: required)
  224. defaults.setCharPref("distribution.id", this._ini.getString("Global", "id"));
  225. defaults.setCharPref("distribution.version",
  226. this._ini.getString("Global", "version"));
  227. let partnerAbout = Cc["@mozilla.org/supports-string;1"]
  228. .createInstance(Ci.nsISupportsString);
  229. try {
  230. if (globalPrefs["about." + this._locale]) {
  231. partnerAbout.data = this._ini.getString("Global", "about." + this._locale);
  232. } else {
  233. partnerAbout.data = this._ini.getString("Global", "about");
  234. }
  235. defaults.setComplexValue("distribution.about",
  236. Ci.nsISupportsString, partnerAbout);
  237. } catch(e) {
  238. /* ignore bad prefs due to bug 895473 and move on */
  239. Cu.reportError(e);
  240. }
  241. if (sections["Preferences"]) {
  242. for (let key in enumerate(this._ini.getKeys("Preferences"))) {
  243. try {
  244. let value = eval(this._ini.getString("Preferences", key));
  245. switch (typeof value) {
  246. case "boolean":
  247. defaults.setBoolPref(key, value);
  248. break;
  249. case "number":
  250. defaults.setIntPref(key, value);
  251. break;
  252. case "string":
  253. defaults.setCharPref(key, value);
  254. break;
  255. case "undefined":
  256. defaults.setCharPref(key, value);
  257. break;
  258. }
  259. } catch(e) {
  260. /* ignore bad prefs and move on */
  261. }
  262. }
  263. }
  264. // We eval() the localizable prefs as well (even though they'll
  265. // always get set as a string) to keep the INI format consistent:
  266. // string prefs always need to be in quotes
  267. let localizedStr = Cc["@mozilla.org/pref-localizedstring;1"]
  268. .createInstance(Ci.nsIPrefLocalizedString);
  269. if (sections["LocalizablePreferences"]) {
  270. for (let key in enumerate(this._ini.getKeys("LocalizablePreferences"))) {
  271. try {
  272. let value = eval(this._ini.getString("LocalizablePreferences", key));
  273. value = value.replace("%LOCALE%", this._locale, "g");
  274. localizedStr.data = "data:text/plain," + key + "=" + value;
  275. defaults.setComplexValue(key, Ci.nsIPrefLocalizedString, localizedStr);
  276. } catch(e) {
  277. /* ignore bad prefs and move on */
  278. }
  279. }
  280. }
  281. if (sections["LocalizablePreferences-" + this._locale]) {
  282. for (let key in enumerate(this._ini.getKeys("LocalizablePreferences-" + this._locale))) {
  283. try {
  284. let value = eval(this._ini.getString("LocalizablePreferences-" + this._locale, key));
  285. localizedStr.data = "data:text/plain," + key + "=" + value;
  286. defaults.setComplexValue(key, Ci.nsIPrefLocalizedString, localizedStr);
  287. } catch(e) {
  288. /* ignore bad prefs and move on */
  289. }
  290. }
  291. }
  292. return this._checkCustomizationComplete();
  293. },
  294. _checkCustomizationComplete: function() {
  295. let prefDefaultsApplied = this._prefDefaultsApplied || !this._iniFile;
  296. if (this._customizationsApplied && this._bookmarksApplied &&
  297. prefDefaultsApplied) {
  298. let os = Cc["@mozilla.org/observer-service;1"]
  299. .getService(Ci.nsIObserverService);
  300. os.notifyObservers(null, DISTRIBUTION_CUSTOMIZATION_COMPLETE_TOPIC, null);
  301. }
  302. }
  303. };
  304. function enumerate(UTF8Enumerator) {
  305. while (UTF8Enumerator.hasMore()) {
  306. yield UTF8Enumerator.getNext();
  307. }
  308. }
  309. function enumToObject(UTF8Enumerator) {
  310. let ret = {};
  311. for (let i in enumerate(UTF8Enumerator)) {
  312. ret[i] = 1;
  313. }
  314. return ret;
  315. }