summaryrefslogtreecommitdiff
path: root/chromium/chrome/browser/resources/chromeos/login/oobe_screen_controller_pairing.js
blob: 9f0b233f60f3ec5be875885f2da2928a78041b1d (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
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

/**
 * @fileoverview controller pairing screen implementation.
 */

login.createScreen('ControllerPairingScreen', 'controller-pairing', function() {
  return {
    decorate: function() {
      this.children[0].decorate(this);
    }
  };
});

Polymer('pairing-device-list', (function() {
  /** @const */ var ICON_COLORS = ['#F0B9CB', '#F0ACC3', '#F098B6', '#F084A9',
                                   '#F06D99', '#F05287', '#F0467F', '#F03473',
                                   '#F01E65', '#F00051'];
  return {
    /* Returns pseudo-random color depending of hash of the |name|. */
    colorByName: function(name) {
      var hash = 0;
      for (var i = 0; i < name.length; ++i)
        hash = (name.charCodeAt(i) + 31 * hash) | 0;
      return ICON_COLORS[hash % ICON_COLORS.length];
    }
  };
})());

Polymer('controller-pairing-screen', (function() {
  'use strict';

  // Keep these constants synced with corresponding constants defined in
  // controller_pairing_screen_actor.{h,cc}.
  /** @const */ var CONTEXT_KEY_CONTROLS_DISABLED = 'controlsDisabled';
  /** @const */ var CONTEXT_KEY_SELECTED_DEVICE = 'selectedDevice';
  /** @const */ var CONTEXT_KEY_ACCOUNT_ID = 'accountId';

  /** @const */ var ACTION_ENROLL = 'enroll';

  /** @const */ var PAGE_AUTHENTICATION = 'authentication';

  return {
    gaiaHost_: null,
    selectedDevice: null,

    observe: {
      'C.devices': 'deviceListChanged',
      'C.page': 'pageChanged'
    },

    /** @override */
    initialize: function() {
      this.context.set(CONTEXT_KEY_CONTROLS_DISABLED, true);
      this.commitContextChanges();
      this.gaiaHost_ = new cr.login.GaiaAuthHost(this.$.gaiaFrame);
    },

    pageChanged: function(oldPage, newPage) {
      if (newPage == PAGE_AUTHENTICATION) {
        this.gaiaHost_.load(cr.login.GaiaAuthHost.AuthMode.DEFAULT,
                            {},
                            this.onAuthCompleted_.bind(this));
      }
    },

    deviceListChanged: function() {
      this.selectedDevice = this.context.get(CONTEXT_KEY_SELECTED_DEVICE);
    },

    selectedDeviceChanged: function() {
      this.context.set(CONTEXT_KEY_SELECTED_DEVICE,
          this.selectedDevice ? this.selectedDevice : '');
      this.commitContextChanges();
    },

    helpButtonClicked: function() {
      console.error('Help is not implemented yet.');
    },

    onAuthCompleted_: function(credentials) {
      this.context.set(CONTEXT_KEY_ACCOUNT_ID, credentials.email);
      this.commitContextChanges();
      this.send(login.Screen.CALLBACK_USER_ACTED, ACTION_ENROLL);
    }
  };
})());