summaryrefslogtreecommitdiffstats
path: root/remoting/webapp/base/js/application.js
blob: 4346ffe69eabfac1df596717e3dd75a689f4cf93 (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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
// 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
 * Interface abstracting the Application functionality.
 */

'use strict';

/** @suppress {duplicate} */
var remoting = remoting || {};

/**
 * @type {base.EventSourceImpl} An event source object for handling global
 *    events. This is an interim hack.  Eventually, we should move
 *    functionalities away from the remoting namespace and into smaller objects.
 */
remoting.testEvents;

/**
 * @constructor
 * @implements {base.Disposable}
 */
remoting.Application = function() {
  // Create global factories.
  remoting.ClientPlugin.factory = new remoting.DefaultClientPluginFactory();

  /** @private {base.WindowMessageDispatcher} */
  this.windowMessageDispatcher_ = new base.WindowMessageDispatcher();
};

remoting.Application.prototype.dispose = function() {
  base.dispose(this.windowMessageDispatcher_);
  this.windowMessageDispatcher_ = null;
};

/* Public method to exit the application. */
remoting.Application.prototype.quit = function() {
  this.exitApplication_();
};

/**
 * Close the main window when quitting the application. This should be called
 * by exitApplication() in the subclass.
 * @protected
 */
remoting.Application.prototype.closeMainWindow_ = function() {
  chrome.app.window.current().close();
};

/**
 * Initialize the application and register all event handlers. After this
 * is called, the app is running and waiting for user events.
 */
remoting.Application.prototype.start = function() {
  // TODO(garykac): This should be owned properly rather than living in the
  // global 'remoting' namespace.
  remoting.settings = new remoting.Settings();

  this.initGlobalObjects_();
  remoting.initIdentity();

  this.initApplication_();

  var that = this;
  remoting.identity.getToken().then(
    this.startApplication_.bind(this)
  ).catch(function(/** !remoting.Error */ error) {
    if (error.hasTag(remoting.Error.Tag.CANCELLED)) {
      that.exitApplication_();
    } else {
      that.signInFailed_(error);
    }
  });
};

/** @private */
remoting.Application.prototype.initGlobalObjects_ = function() {
  if (base.isAppsV2()) {
    var htmlNode = /** @type {HTMLElement} */ (document.body.parentNode);
    htmlNode.classList.add('apps-v2');
  }

  console.log(this.getExtensionInfo());
  l10n.localize();

  var sandbox =
      /** @type {HTMLIFrameElement} */ (document.getElementById('wcs-sandbox'));
  remoting.wcsSandbox = new remoting.WcsSandboxContainer(
      sandbox.contentWindow, this.windowMessageDispatcher_);
  remoting.initModalDialogs();

  remoting.testEvents = new base.EventSourceImpl();
  /** @enum {string} */
  remoting.testEvents.Names = {
    uiModeChanged: 'uiModeChanged'
  };
  remoting.testEvents.defineEvents(base.values(remoting.testEvents.Names));
};

/**
 * @return {string} Information about the current extension.
 */
remoting.Application.prototype.getExtensionInfo = function() {
  var v2OrLegacy = base.isAppsV2() ? " (v2)" : " (legacy)";
  var manifest = chrome.runtime.getManifest();
  if (manifest && manifest.version) {
    var name = this.getApplicationName();
    return name + ' version: ' + manifest.version + v2OrLegacy;
  } else {
    return 'Failed to get product version. Corrupt manifest?';
  }
};

/**
 * These functions must be overridden in the subclass.
 */

/** @return {string} */
remoting.Application.prototype.getApplicationName = function() {
  base.debug.assert(false, 'Subclass must override');
};

/**
 * @return {remoting.Activity}  The Current activity.
 */
remoting.Application.prototype.getActivity = function() {
  base.debug.assert(false, 'Subclass must override');
};

/**
 * @param {!remoting.Error} error
 * @protected
 */
remoting.Application.prototype.signInFailed_ = function(error) {
  base.debug.assert(false, 'Subclass must override');
};

/** @protected */
remoting.Application.prototype.initApplication_ = function() {
  base.debug.assert(false, 'Subclass must override');
};

/**
 * @param {string} token
 * @protected
 */
remoting.Application.prototype.startApplication_ = function(token) {
  base.debug.assert(false, 'Subclass must override');
};

/** @protected */
remoting.Application.prototype.exitApplication_ = function() {
  base.debug.assert(false, 'Subclass must override');
};

/**
 * The interface specifies the methods that a subclass of remoting.Application
 * is required implement to override the default behavior.
 *
 * @interface
 */
remoting.ApplicationInterface = function() {};

/**
 * @return {string} Application product name to be used in UI.
 */
remoting.ApplicationInterface.prototype.getApplicationName = function() {};

/**
 * Report an authentication error to the user. This is called in lieu of
 * startApplication() if the user cannot be authenticated or if they decline
 * the app permissions.
 *
 * @param {!remoting.Error} error The failure reason.
 */
remoting.ApplicationInterface.prototype.signInFailed_ = function(error) {};

/**
 * Initialize the application. This is called before an OAuth token is requested
 * and should be used for tasks such as initializing the DOM, registering event
 * handlers, etc. After this is called, the app is running and waiting for
 * user events.
 */
remoting.ApplicationInterface.prototype.initApplication_ = function() {};

/**
 * Start the application. Once startApplication() is called, we can assume that
 * the user has consented to all permissions specified in the manifest.
 *
 * @param {string} token An OAuth access token. The app should not cache
 *     this token, but can assume that it will remain valid during application
 *     start-up.
 */
remoting.ApplicationInterface.prototype.startApplication_ = function(token) {};

/**
 * Close down the application before exiting.
 */
remoting.ApplicationInterface.prototype.exitApplication_ = function() {};

/** @type {remoting.Application} */
remoting.app = null;