1// Copyright (c) 2012 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5cr.define('print_preview', function() {
6  'use strict';
7
8  /**
9   * Repository which stores information about the user. Events are dispatched
10   * when the information changes.
11   * @constructor
12   * @extends {cr.EventTarget}
13   */
14  function UserInfo() {
15    cr.EventTarget.call(this);
16
17    /**
18     * Email address of the logged in user or {@code null} if no user is logged
19     * in. In case of Google multilogin, can be changed by the user.
20     * @private {?string}
21     */
22    this.activeUser_ = null;
23
24    /**
25     * Email addresses of the logged in users or empty array if no user is
26     * logged in. {@code null} if not known yet.
27     * @private {?Array.<string>}
28     */
29    this.users_ = null;
30  };
31
32  /**
33   * Enumeration of event types dispatched by the user info.
34   * @enum {string}
35   */
36  UserInfo.EventType = {
37    ACTIVE_USER_CHANGED: 'print_preview.UserInfo.ACTIVE_USER_CHANGED',
38    USERS_CHANGED: 'print_preview.UserInfo.USERS_CHANGED'
39  };
40
41  UserInfo.prototype = {
42    __proto__: cr.EventTarget.prototype,
43
44    /** @return {boolean} Whether user accounts are already retrieved. */
45    get initialized() {
46      return this.users_ != null;
47    },
48
49    /** @return {boolean} Whether user is logged in or not. */
50    get loggedIn() {
51      return !!this.activeUser;
52    },
53
54    /**
55     * @return {?string} Email address of the logged in user or {@code null} if
56     *     no user is logged.
57     */
58    get activeUser() {
59      return this.activeUser_;
60    },
61
62    /** Changes active user. */
63    set activeUser(activeUser) {
64      if (this.activeUser_ != activeUser) {
65        this.activeUser_ = activeUser;
66        cr.dispatchSimpleEvent(this, UserInfo.EventType.ACTIVE_USER_CHANGED);
67      }
68    },
69
70    /**
71     * @return {?Array.<string>} Email addresses of the logged in users or
72     *     empty array if no user is logged in. {@code null} if not known yet.
73     */
74    get users() {
75      return this.users_;
76    },
77
78    /**
79     * Sets logged in user accounts info.
80     * @param {string} activeUser Active user account (email).
81     * @param {!Array.<string>} users List of currently logged in accounts.
82     */
83    setUsers: function(activeUser, users) {
84      this.activeUser_ = activeUser;
85      this.users_ = users || [];
86      cr.dispatchSimpleEvent(this, UserInfo.EventType.USERS_CHANGED);
87    },
88  };
89
90  return {
91    UserInfo: UserInfo
92  };
93});
94