Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/apps/system/js/identity.js
blob: 803037786e6e625b0a0b5fcf885178331d0448b3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- /
/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */

// When bug 794999 is resolved, switch to use the abstract Trusted UI Component

'use strict';

const kIdentityScreen = 'https://login.native-persona.org/sign_in#NATIVE';
const kIdentityFrame =
    'https://login.native-persona.org/communication_iframe';

var Identity = (function() {
  var iframe;

  return {
    trustedUILayers: {},

    init: function() {
      window.addEventListener('mozChromeEvent', this);
    },

    handleEvent: function onMozChromeEvent(e) {
      var chromeEventId = e.detail.id;
      var requestId = e.detail.requestId;
      switch (e.detail.type) {
        // Chrome asks Gaia to show the identity dialog.
        case 'open-id-dialog':
          if (!chromeEventId)
            return;

          // When opening the dialog, we record the chrome event id, which
          // we will need to send back to the TrustedUIManager when asking
          // to close.
          this.trustedUILayers[requestId] = chromeEventId;

          if (!e.detail.showUI && iframe) {
            this._dispatchEvent({
              id: chromeEventId,
              frame: iframe
            });
            return;
          }
          var frame = document.createElement('iframe');
          frame.setAttribute('mozbrowser', 'true');
          frame.setAttribute('remote', true);
          frame.classList.add('screen');
          frame.src = e.detail.showUI ? kIdentityScreen : kIdentityFrame;
          frame.addEventListener('mozbrowserloadstart',
              function loadStart(evt) {
            // After creating the new frame containing the identity flow, we
            // send it back to chrome so the identity callbacks can be injected.
            this._dispatchEvent({
              id: chromeEventId,
              frame: evt.target
            });
          }.bind(this));


          if (e.detail.showUI) {
            // The identity flow is shown within the trusted UI.
            TrustedUIManager.open(navigator.mozL10n.get('persona-signin'),
                                  frame,
                                  this.trustedUILayers[requestId]);
          } else {
            var container = document.getElementById('screen');
            container.appendChild(frame);
            frame.classList.add('communication-frame');
            iframe = frame;
          }
          break;

        case 'received-id-assertion':
          if (e.detail.showUI) {
            TrustedUIManager.close(this.trustedUILayers[requestId],
                                   (function dialogClosed() {
              delete this.trustedUILayers[requestId];
            }).bind(this));
          }
          this._dispatchEvent({ id: chromeEventId });
          break;
      }
    },
    _dispatchEvent: function su_dispatchEvent(obj) {
      var event = document.createEvent('CustomEvent');
      event.initCustomEvent('mozContentEvent', true, true, obj);
      window.dispatchEvent(event);
    }
  };
})();

// Make sure L10n is ready before init
if (navigator.mozL10n.readyState == 'complete' ||
    navigator.mozL10n.readyState == 'interactive') {
  Identity.init();
} else {
  window.addEventListener('localized', Identity.init.bind(Identity));
}