not really known
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

104 lines
3.2 KiB

  1. define(function () {
  2. 'use strict';
  3. var env = {};
  4. env.getEnvironment = function (callback) {
  5. // FIXME: we assume this code runs on the same thread as the
  6. // javascript executed from sugar-toolkit-gtk3 (python)
  7. if (env.isSugarizer()) {
  8. var getUrlParameter = function(name) {
  9. var match = RegExp('[?&]' + name + '=([^&]*)').exec(window.location.search);
  10. return match && decodeURIComponent(match[1].replace(/\+/g, ' '));
  11. };
  12. window.top.sugar = {};
  13. window.top.sugar.environment = {
  14. activityId: getUrlParameter("aid"),
  15. activityName: getUrlParameter("n"),
  16. bundleId: getUrlParameter("a"),
  17. objectId: getUrlParameter("o"),
  18. sharedId: getUrlParameter("s")
  19. };
  20. if (typeof chrome != 'undefined' && chrome.app && chrome.app.runtime) {
  21. chrome.storage.local.get('sugar_settings', function(values) {
  22. window.top.sugar.environment.user = JSON.parse(values.sugar_settings);
  23. callback(null, window.top.sugar.environment);
  24. });
  25. } else {
  26. window.top.sugar.environment.user = JSON.parse(localStorage.sugar_settings);
  27. setTimeout(function () {
  28. callback(null, window.top.sugar.environment);
  29. }, 0);
  30. }
  31. } else if (env.isStandalone()) {
  32. setTimeout(function () {
  33. callback(null, {});
  34. }, 0);
  35. } else {
  36. var environmentCallback = function () {
  37. callback(null, window.top.sugar.environment);
  38. };
  39. if (window.top.sugar) {
  40. setTimeout(function () {
  41. environmentCallback();
  42. }, 0);
  43. } else {
  44. window.top.sugar = {};
  45. window.top.sugar.onEnvironmentSet = function () {
  46. environmentCallback();
  47. };
  48. }
  49. }
  50. };
  51. env.getObjectId = function (callback) {
  52. env.getEnvironment(function (error, environment) {
  53. callback(environment.objectId);
  54. });
  55. };
  56. env.getURLScheme = function () {
  57. return window.location.protocol;
  58. };
  59. env.getHost = function() {
  60. return window.location.hostname;
  61. };
  62. env.isStandalone = function () {
  63. var webActivityURLScheme = "activity:";
  64. var fileURLScheme = "file:";
  65. var currentURLScheme = env.getURLScheme();
  66. // the control of hostname !== '0.0.0.0' is used
  67. // for compatibility with F18 and webkit1
  68. return currentURLScheme !== webActivityURLScheme &&
  69. currentURLScheme !== fileURLScheme &&
  70. window.location.hostname !== '0.0.0.0';
  71. };
  72. env.isSugarizer = function() {
  73. // HACK: If in Chrome App automatic deduction that in Sugarizer
  74. if (typeof chrome != 'undefined' && chrome.app && chrome.app.runtime) {
  75. return true;
  76. } else if (typeof(Storage)!=="undefined" && typeof(window.localStorage)!=="undefined") {
  77. try {
  78. return (window.localStorage.getItem('sugar_settings') !== null);
  79. } catch(err) {
  80. return false;
  81. }
  82. }
  83. return false;
  84. };
  85. env.isSugarizerOS = function() {
  86. if (typeof window.sugarizerOS != 'undefined')
  87. return true;
  88. return false;
  89. }
  90. return env;
  91. });