session.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  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 file,
  3. * You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. define(["require", "util", "channels", "jquery", "storage"], function (require, util, channels, $, storage) {
  5. var DEBUG = true;
  6. // This is the amount of time in which a hello-back must be received after a hello
  7. // for us to respect a URL change:
  8. var HELLO_BACK_CUTOFF = 1500;
  9. var session = util.mixinEvents(util.Module("session"));
  10. var assert = util.assert;
  11. // We will load this module later (there's a circular import):
  12. var peers;
  13. // This is the hub we connect to:
  14. session.shareId = null;
  15. // This is the ID that identifies this client:
  16. session.clientId = null;
  17. session.router = channels.Router();
  18. // Indicates if TogetherJS has just started (not continuing from a saved session):
  19. session.firstRun = false;
  20. // This is the key we use for localStorage:
  21. var localStoragePrefix = "togetherjs.";
  22. // This is the channel to the hub:
  23. var channel = null;
  24. // Setting, essentially global:
  25. session.AVATAR_SIZE = 90;
  26. var MAX_SESSION_AGE = 30*24*60*60*1000; // 30 days
  27. /****************************************
  28. * URLs
  29. */
  30. var includeHashInUrl = TogetherJS.config.get("includeHashInUrl");
  31. TogetherJS.config.close("includeHashInUrl");
  32. var currentUrl = (location.href + "").replace(/\#.*$/, "");
  33. if (includeHashInUrl) {
  34. currentUrl = location.href;
  35. }
  36. session.hubUrl = function (id) {
  37. id = id || session.shareId;
  38. assert(id, "URL cannot be resolved before TogetherJS.shareId has been initialized");
  39. TogetherJS.config.close("hubBase");
  40. var hubBase = TogetherJS.config.get("hubBase");
  41. return hubBase.replace(/\/*$/, "") + "/hub/" + id;
  42. };
  43. session.shareUrl = function () {
  44. assert(session.shareId, "Attempted to access shareUrl() before shareId is set");
  45. var hash = location.hash;
  46. var m = /\?[^#]*/.exec(location.href);
  47. var query = "";
  48. if (m) {
  49. query = m[0];
  50. }
  51. hash = hash.replace(/&?togetherjs-[a-zA-Z0-9]+/, "");
  52. hash = hash || "#";
  53. return location.protocol + "//" + location.host + location.pathname + query +
  54. hash + "&togetherjs=" + session.shareId;
  55. };
  56. session.recordUrl = function () {
  57. assert(session.shareId);
  58. var url = TogetherJS.baseUrl.replace(/\/*$/, "") + "/togetherjs/recorder.html";
  59. url += "#&togetherjs=" + session.shareId + "&hubBase=" + TogetherJS.config.get("hubBase");
  60. return url;
  61. };
  62. /* location.href without the hash */
  63. session.currentUrl = function () {
  64. if (includeHashInUrl) {
  65. return location.href;
  66. } else {
  67. return location.href.replace(/#.*/, "");
  68. }
  69. };
  70. /****************************************
  71. * Message handling/dispatching
  72. */
  73. session.hub = util.mixinEvents({});
  74. var IGNORE_MESSAGES = TogetherJS.config.get("ignoreMessages");
  75. if (IGNORE_MESSAGES === true) {
  76. DEBUG = false;
  77. IGNORE_MESSAGES = [];
  78. }
  79. // These are messages sent by clients who aren't "part" of the TogetherJS session:
  80. var MESSAGES_WITHOUT_CLIENTID = ["who", "invite", "init-connection"];
  81. // We ignore incoming messages from the channel until this is true:
  82. var readyForMessages = false;
  83. function openChannel() {
  84. assert(! channel, "Attempt to re-open channel");
  85. console.info("Connecting to", session.hubUrl(), location.href);
  86. var c = channels.WebSocketChannel(session.hubUrl());
  87. c.onmessage = function (msg) {
  88. if (! readyForMessages) {
  89. if (DEBUG) {
  90. console.info("In (but ignored for being early):", msg);
  91. }
  92. return;
  93. }
  94. if (DEBUG && IGNORE_MESSAGES.indexOf(msg.type) == -1) {
  95. console.info("In:", msg);
  96. }
  97. if (! peers) {
  98. // We're getting messages before everything is fully initialized
  99. console.warn("Message received before all modules loaded (ignoring):", msg);
  100. return;
  101. }
  102. if ((! msg.clientId) && MESSAGES_WITHOUT_CLIENTID.indexOf(msg.type) == -1) {
  103. console.warn("Got message without clientId, where clientId is required", msg);
  104. return;
  105. }
  106. if (msg.clientId) {
  107. msg.peer = peers.getPeer(msg.clientId, msg);
  108. }
  109. if (msg.type == "hello" || msg.type == "hello-back" || msg.type == "peer-update") {
  110. // We do this here to make sure this is run before any other
  111. // hello handlers:
  112. msg.peer.updateFromHello(msg);
  113. }
  114. if (msg.peer) {
  115. msg.sameUrl = msg.peer.url == currentUrl;
  116. if (!msg.peer.isSelf) {
  117. msg.peer.updateMessageDate(msg);
  118. }
  119. }
  120. session.hub.emit(msg.type, msg);
  121. TogetherJS._onmessage(msg);
  122. };
  123. channel = c;
  124. session.router.bindChannel(channel);
  125. }
  126. session.send = function (msg) {
  127. if (DEBUG && IGNORE_MESSAGES.indexOf(msg.type) == -1) {
  128. console.info("Send:", msg);
  129. }
  130. msg.clientId = session.clientId;
  131. channel.send(msg);
  132. };
  133. session.appSend = function (msg) {
  134. var type = msg.type;
  135. if (type.search(/^togetherjs\./) === 0) {
  136. type = type.substr("togetherjs.".length);
  137. } else if (type.search(/^app\./) === -1) {
  138. type = "app." + type;
  139. }
  140. msg.type = type;
  141. session.send(msg);
  142. };
  143. /****************************************
  144. * Standard message responses
  145. */
  146. /* Always say hello back, and keep track of peers: */
  147. session.hub.on("hello hello-back", function (msg) {
  148. if (msg.type == "hello") {
  149. sendHello(true);
  150. }
  151. if (session.isClient && (! msg.isClient) &&
  152. session.firstRun && session.timeHelloSent &&
  153. Date.now() - session.timeHelloSent < HELLO_BACK_CUTOFF) {
  154. processFirstHello(msg);
  155. }
  156. });
  157. session.hub.on("who", function (msg) {
  158. sendHello(true);
  159. });
  160. function processFirstHello(msg) {
  161. if (! msg.sameUrl) {
  162. var url = msg.url;
  163. if (msg.urlHash) {
  164. url += msg.urlHash;
  165. }
  166. require("ui").showUrlChangeMessage(msg.peer, url);
  167. location.href = url;
  168. }
  169. }
  170. session.timeHelloSent = null;
  171. function sendHello(helloBack) {
  172. var msg = session.makeHelloMessage(helloBack);
  173. if (! helloBack) {
  174. session.timeHelloSent = Date.now();
  175. peers.Self.url = msg.url;
  176. }
  177. session.send(msg);
  178. }
  179. session.makeHelloMessage = function (helloBack) {
  180. var msg = {
  181. name: peers.Self.name || peers.Self.defaultName,
  182. avatar: peers.Self.avatar,
  183. color: peers.Self.color,
  184. url: session.currentUrl(),
  185. urlHash: location.hash,
  186. // FIXME: titles update, we should track those changes:
  187. title: document.title,
  188. rtcSupported: session.RTCSupported,
  189. isClient: session.isClient
  190. };
  191. if (helloBack) {
  192. msg.type = "hello-back";
  193. } else {
  194. msg.type = "hello";
  195. msg.clientVersion = TogetherJS.version;
  196. }
  197. if (! TogetherJS.startup.continued) {
  198. msg.starting = true;
  199. }
  200. // This is a chance for other modules to effect the hello message:
  201. session.emit("prepare-hello", msg);
  202. return msg;
  203. };
  204. /****************************************
  205. * Lifecycle (start and end)
  206. */
  207. // These are Javascript files that implement features, and so must
  208. // be injected at runtime because they aren't pulled in naturally
  209. // via define().
  210. // ui must be the first item:
  211. var features = ["peers", "ui", "chat", "webrtc", "cursor", "startup", "videos", "forms", "visibilityApi", "youtubeVideos"];
  212. function getRoomName(prefix, maxSize) {
  213. var findRoom = TogetherJS.config.get("hubBase").replace(/\/*$/, "") + "/findroom";
  214. return $.ajax({
  215. url: findRoom,
  216. dataType: "json",
  217. data: {prefix: prefix, max: maxSize}
  218. }).then(function (resp) {
  219. return resp.name;
  220. });
  221. }
  222. function initIdentityId() {
  223. return util.Deferred(function (def) {
  224. if (session.identityId) {
  225. def.resolve();
  226. return;
  227. }
  228. storage.get("identityId").then(function (identityId) {
  229. if (! identityId) {
  230. identityId = util.generateId();
  231. storage.set("identityId", identityId);
  232. }
  233. session.identityId = identityId;
  234. // We don't actually have to wait for the set to succede, so
  235. // long as session.identityId is set
  236. def.resolve();
  237. });
  238. });
  239. }
  240. initIdentityId.done = initIdentityId();
  241. function initShareId() {
  242. return util.Deferred(function (def) {
  243. var hash = location.hash;
  244. var shareId = session.shareId;
  245. var isClient = true;
  246. var set = true;
  247. var sessionId;
  248. session.firstRun = ! TogetherJS.startup.continued;
  249. if (! shareId) {
  250. if (TogetherJS.startup._joinShareId) {
  251. // Like, below, this *also* means we got the shareId from the hash
  252. // (in togetherjs.js):
  253. shareId = TogetherJS.startup._joinShareId;
  254. }
  255. }
  256. if (! shareId) {
  257. // FIXME: I'm not sure if this will ever happen, because togetherjs.js should
  258. // handle it
  259. var m = /&?togetherjs=([^&]*)/.exec(hash);
  260. if (m) {
  261. isClient = ! m[1];
  262. shareId = m[2];
  263. var newHash = hash.substr(0, m.index) + hash.substr(m.index + m[0].length);
  264. location.hash = newHash;
  265. }
  266. }
  267. return storage.tab.get("status").then(function (saved) {
  268. var findRoom = TogetherJS.config.get("findRoom");
  269. TogetherJS.config.close("findRoom");
  270. if (findRoom && saved && findRoom != saved.shareId) {
  271. console.info("Ignoring findRoom in lieu of continued session");
  272. } else if (findRoom && TogetherJS.startup._joinShareId) {
  273. console.info("Ignoring findRoom in lieu of explicit invite to session");
  274. }
  275. if (findRoom && typeof findRoom == "string" && (! saved) && (! TogetherJS.startup._joinShareId)) {
  276. isClient = true;
  277. shareId = findRoom;
  278. sessionId = util.generateId();
  279. } else if (findRoom && (! saved) && (! TogetherJS.startup._joinShareId)) {
  280. assert(findRoom.prefix && typeof findRoom.prefix == "string", "Bad findRoom.prefix", findRoom);
  281. assert(findRoom.max && typeof findRoom.max == "number" && findRoom.max > 0,
  282. "Bad findRoom.max", findRoom);
  283. sessionId = util.generateId();
  284. if (findRoom.prefix.search(/[^a-zA-Z0-9]/) != -1) {
  285. console.warn("Bad value for findRoom.prefix:", JSON.stringify(findRoom.prefix));
  286. }
  287. getRoomName(findRoom.prefix, findRoom.max).then(function (shareId) {
  288. // FIXME: duplicates code below:
  289. session.clientId = session.identityId + "." + sessionId;
  290. storage.tab.set("status", {reason: "joined", shareId: shareId, running: true, date: Date.now(), sessionId: sessionId});
  291. session.isClient = true;
  292. session.shareId = shareId;
  293. session.emit("shareId");
  294. def.resolve(session.shareId);
  295. });
  296. return;
  297. } else if (TogetherJS.startup._launch) {
  298. if (saved) {
  299. isClient = saved.reason == "joined";
  300. if (! shareId) {
  301. shareId = saved.shareId;
  302. }
  303. sessionId = saved.sessionId;
  304. } else {
  305. isClient = TogetherJS.startup.reason == "joined";
  306. assert(! sessionId);
  307. sessionId = util.generateId();
  308. }
  309. if (! shareId) {
  310. shareId = util.generateId();
  311. }
  312. } else if (saved) {
  313. isClient = saved.reason == "joined";
  314. TogetherJS.startup.reason = saved.reason;
  315. TogetherJS.startup.continued = true;
  316. shareId = saved.shareId;
  317. sessionId = saved.sessionId;
  318. // The only case when we don't need to set the storage status again is when
  319. // we're already set to be running
  320. set = ! saved.running;
  321. } else {
  322. throw new util.AssertionError("No saved status, and no startup._launch request; why did TogetherJS start?");
  323. }
  324. assert(session.identityId);
  325. session.clientId = session.identityId + "." + sessionId;
  326. if (set) {
  327. storage.tab.set("status", {reason: TogetherJS.startup.reason, shareId: shareId, running: true, date: Date.now(), sessionId: sessionId});
  328. }
  329. session.isClient = isClient;
  330. session.shareId = shareId;
  331. session.emit("shareId");
  332. def.resolve(session.shareId);
  333. });
  334. });
  335. }
  336. function initStartTarget() {
  337. var id;
  338. if (TogetherJS.startup.button) {
  339. id = TogetherJS.startup.button.id;
  340. if (id) {
  341. storage.set("startTarget", id);
  342. }
  343. return;
  344. }
  345. storage.get("startTarget").then(function (id) {
  346. var el = document.getElementById(id);
  347. if (el) {
  348. TogetherJS.startup.button = el;
  349. }
  350. });
  351. }
  352. session.start = function () {
  353. initStartTarget();
  354. initIdentityId().then(function () {
  355. initShareId().then(function () {
  356. readyForMessages = false;
  357. openChannel();
  358. require(["ui"], function (ui) {
  359. TogetherJS.running = true;
  360. ui.prepareUI();
  361. require(features, function () {
  362. $(function () {
  363. peers = require("peers");
  364. var startup = require("startup");
  365. session.emit("start");
  366. session.once("ui-ready", function () {
  367. readyForMessages = true;
  368. startup.start();
  369. });
  370. ui.activateUI();
  371. TogetherJS.config.close("enableAnalytics");
  372. if (TogetherJS.config.get("enableAnalytics")) {
  373. require(["analytics"], function (analytics) {
  374. analytics.activate();
  375. });
  376. }
  377. peers._SelfLoaded.then(function () {
  378. sendHello(false);
  379. });
  380. TogetherJS.emit("ready");
  381. });
  382. });
  383. });
  384. });
  385. });
  386. };
  387. session.close = function (reason) {
  388. TogetherJS.running = false;
  389. var msg = {type: "bye"};
  390. if (reason) {
  391. msg.reason = reason;
  392. }
  393. session.send(msg);
  394. session.emit("close");
  395. var name = window.name;
  396. storage.tab.get("status").then(function (saved) {
  397. if (! saved) {
  398. console.warn("No session information saved in", "status." + name);
  399. } else {
  400. saved.running = false;
  401. saved.date = Date.now();
  402. storage.tab.set("status", saved);
  403. }
  404. channel.close();
  405. channel = null;
  406. session.shareId = null;
  407. session.emit("shareId");
  408. TogetherJS.emit("close");
  409. TogetherJS._teardown();
  410. });
  411. };
  412. session.on("start", function () {
  413. $(window).on("resize", resizeEvent);
  414. if (includeHashInUrl) {
  415. $(window).on("hashchange", hashchangeEvent);
  416. }
  417. });
  418. session.on("close", function () {
  419. $(window).off("resize", resizeEvent);
  420. if (includeHashInUrl) {
  421. $(window).off("hashchange", hashchangeEvent);
  422. }
  423. });
  424. function hashchangeEvent() {
  425. // needed because when message arives from peer this variable will be checked to
  426. // decide weather to show actions or not
  427. sendHello(false);
  428. }
  429. function resizeEvent() {
  430. session.emit("resize");
  431. }
  432. if (TogetherJS.startup._launch) {
  433. setTimeout(session.start);
  434. }
  435. util.testExpose({
  436. getChannel: function () {
  437. return channel;
  438. }
  439. });
  440. return session;
  441. });