1 // Copyright 2014 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.
7 * Interface abstracting the Application functionality.
12 /** @suppress {duplicate} */
13 var remoting = remoting || {};
16 * @type {base.EventSourceImpl} An event source object for handling global
17 * events. This is an interim hack. Eventually, we should move
18 * functionalities away from the remoting namespace and into smaller objects.
24 * @implements {base.Disposable}
26 remoting.Application = function() {
27 // Create global factories.
28 remoting.ClientPlugin.factory = new remoting.DefaultClientPluginFactory();
30 /** @protected {base.WindowMessageDispatcher} */
31 this.windowMessageDispatcher_ = new base.WindowMessageDispatcher();
34 remoting.Application.prototype.dispose = function() {
35 base.dispose(this.windowMessageDispatcher_);
36 this.windowMessageDispatcher_ = null;
39 /* Public method to exit the application. */
40 remoting.Application.prototype.quit = function() {
41 this.exitApplication_();
45 * Close the main window when quitting the application. This should be called
46 * by exitApplication() in the subclass.
49 remoting.Application.prototype.closeMainWindow_ = function() {
50 chrome.app.window.current().close();
54 * Initialize the application and register all event handlers. After this
55 * is called, the app is running and waiting for user events.
57 remoting.Application.prototype.start = function() {
58 // TODO(garykac): This should be owned properly rather than living in the
59 // global 'remoting' namespace.
60 remoting.settings = new remoting.Settings();
62 this.initGlobalObjects_();
63 remoting.initIdentity();
65 this.initApplication_();
68 remoting.identity.getToken().then(
69 this.startApplication_.bind(this)
70 ).catch(function(/** !remoting.Error */ error) {
71 if (error.hasTag(remoting.Error.Tag.CANCELLED)) {
72 that.exitApplication_();
74 that.signInFailed_(error);
80 remoting.Application.prototype.initGlobalObjects_ = function() {
81 if (base.isAppsV2()) {
82 var htmlNode = /** @type {HTMLElement} */ (document.body.parentNode);
83 htmlNode.classList.add('apps-v2');
86 console.log(this.getExtensionInfo());
90 /** @type {HTMLIFrameElement} */ (document.getElementById('wcs-sandbox'));
91 remoting.wcsSandbox = new remoting.WcsSandboxContainer(
92 sandbox.contentWindow, this.windowMessageDispatcher_);
93 remoting.initModalDialogs();
95 remoting.testEvents = new base.EventSourceImpl();
97 remoting.testEvents.Names = {
98 uiModeChanged: 'uiModeChanged'
100 remoting.testEvents.defineEvents(base.values(remoting.testEvents.Names));
104 * @return {string} Information about the current extension.
106 remoting.Application.prototype.getExtensionInfo = function() {
107 var v2OrLegacy = base.isAppsV2() ? " (v2)" : " (legacy)";
108 var manifest = chrome.runtime.getManifest();
109 if (manifest && manifest.version) {
110 var name = this.getApplicationName();
111 return name + ' version: ' + manifest.version + v2OrLegacy;
113 return 'Failed to get product version. Corrupt manifest?';
118 * These functions must be overridden in the subclass.
121 /** @return {string} */
122 remoting.Application.prototype.getApplicationId = function() {
123 console.assert(false, 'Subclass must override');
126 /** @return {string} */
127 remoting.Application.prototype.getApplicationName = function() {
128 console.assert(false, 'Subclass must override getApplicationName().');
132 * @return {remoting.Activity} The Current activity.
134 remoting.Application.prototype.getActivity = function() {
135 console.assert(false, 'Subclass must override getActivity().');
139 * @param {!remoting.Error} error
142 remoting.Application.prototype.signInFailed_ = function(error) {
143 console.assert(false, 'Subclass must override signInFailed().');
147 remoting.Application.prototype.initApplication_ = function() {
148 console.assert(false, 'Subclass must override initApplication().');
152 * @param {string} token
155 remoting.Application.prototype.startApplication_ = function(token) {
156 console.assert(false, 'Subclass must override startApplication().');
160 remoting.Application.prototype.exitApplication_ = function() {
161 console.assert(false, 'Subclass must override exitApplication().');
165 * The interface specifies the methods that a subclass of remoting.Application
166 * is required implement to override the default behavior.
170 remoting.ApplicationInterface = function() {};
173 * @return {string} Application Id.
175 remoting.ApplicationInterface.prototype.getApplicationId = function() {};
178 * @return {string} Application product name to be used in UI.
180 remoting.ApplicationInterface.prototype.getApplicationName = function() {};
183 * Report an authentication error to the user. This is called in lieu of
184 * startApplication() if the user cannot be authenticated or if they decline
185 * the app permissions.
187 * @param {!remoting.Error} error The failure reason.
189 remoting.ApplicationInterface.prototype.signInFailed_ = function(error) {};
192 * Initialize the application. This is called before an OAuth token is requested
193 * and should be used for tasks such as initializing the DOM, registering event
194 * handlers, etc. After this is called, the app is running and waiting for
197 remoting.ApplicationInterface.prototype.initApplication_ = function() {};
200 * Start the application. Once startApplication() is called, we can assume that
201 * the user has consented to all permissions specified in the manifest.
203 * @param {string} token An OAuth access token. The app should not cache
204 * this token, but can assume that it will remain valid during application
207 remoting.ApplicationInterface.prototype.startApplication_ = function(token) {};
210 * Close down the application before exiting.
212 remoting.ApplicationInterface.prototype.exitApplication_ = function() {};
214 /** @type {remoting.Application} */