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.getApplicationName = function() {
123 console
.assert(false, 'Subclass must override getApplicationName().');
127 * @return {remoting.Activity} The Current activity.
129 remoting
.Application
.prototype.getActivity = function() {
130 console
.assert(false, 'Subclass must override getActivity().');
134 * @param {!remoting.Error} error
137 remoting
.Application
.prototype.signInFailed_ = function(error
) {
138 console
.assert(false, 'Subclass must override signInFailed().');
142 remoting
.Application
.prototype.initApplication_ = function() {
143 console
.assert(false, 'Subclass must override initApplication().');
147 * @param {string} token
150 remoting
.Application
.prototype.startApplication_ = function(token
) {
151 console
.assert(false, 'Subclass must override startApplication().');
155 remoting
.Application
.prototype.exitApplication_ = function() {
156 console
.assert(false, 'Subclass must override exitApplication().');
160 * The interface specifies the methods that a subclass of remoting.Application
161 * is required implement to override the default behavior.
165 remoting
.ApplicationInterface = function() {};
168 * @return {string} Application product name to be used in UI.
170 remoting
.ApplicationInterface
.prototype.getApplicationName = function() {};
173 * Report an authentication error to the user. This is called in lieu of
174 * startApplication() if the user cannot be authenticated or if they decline
175 * the app permissions.
177 * @param {!remoting.Error} error The failure reason.
179 remoting
.ApplicationInterface
.prototype.signInFailed_ = function(error
) {};
182 * Initialize the application. This is called before an OAuth token is requested
183 * and should be used for tasks such as initializing the DOM, registering event
184 * handlers, etc. After this is called, the app is running and waiting for
187 remoting
.ApplicationInterface
.prototype.initApplication_ = function() {};
190 * Start the application. Once startApplication() is called, we can assume that
191 * the user has consented to all permissions specified in the manifest.
193 * @param {string} token An OAuth access token. The app should not cache
194 * this token, but can assume that it will remain valid during application
197 remoting
.ApplicationInterface
.prototype.startApplication_ = function(token
) {};
200 * Close down the application before exiting.
202 remoting
.ApplicationInterface
.prototype.exitApplication_ = function() {};
204 /** @type {remoting.Application} */