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.
7 /** @suppress {duplicate} */
8 var remoting = remoting || {};
11 * @type {base.EventSourceImpl} An event source object for handling global
12 * events. This is an interim hack. Eventually, we should move
13 * functionalities away from the remoting namespace and into smaller objects.
18 * Initialization tasks that are common to all remoting apps.
20 remoting.initGlobalObjects = function() {
21 if (base.isAppsV2()) {
22 var htmlNode = /** @type {HTMLElement} */ (document.body.parentNode);
23 htmlNode.classList.add('apps-v2');
26 console.log(remoting.getExtensionInfo());
29 remoting.stats = new remoting.ConnectionStats(
30 document.getElementById('statistics'));
31 remoting.formatIq = new remoting.FormatIq();
33 remoting.clipboard = new remoting.Clipboard();
35 /** @type {HTMLIFrameElement} */ (document.getElementById('wcs-sandbox'));
36 remoting.wcsSandbox = new remoting.WcsSandboxContainer(sandbox.contentWindow);
38 // The plugin's onFocus handler sends a paste command to |window|, because
39 // it can't send one to the plugin element itself.
40 window.addEventListener('paste', pluginGotPaste_, false);
41 window.addEventListener('copy', pluginGotCopy_, false);
43 remoting.initModalDialogs();
45 remoting.testEvents = new base.EventSourceImpl();
47 remoting.testEvents.Names = {
48 uiModeChanged: 'uiModeChanged'
50 remoting.testEvents.defineEvents(base.values(remoting.testEvents.Names));
54 * Returns true if the current platform is fully supported. It's only used when
55 * we detect that host native messaging components are not installed. In that
56 * case the result of this function determines if the webapp should show the
57 * controls that allow to install and enable Me2Me host.
61 remoting.isMe2MeInstallable = function() {
62 // The chromoting host is currently not installable on ChromeOS.
63 // For Linux, we have a install package for Ubuntu but not other distros.
64 // Since we cannot tell from javascript alone the Linux distro the client is
65 // on, we don't show the daemon-control UI for Linux unless the host is
67 return remoting.platformIsWindows() || remoting.platformIsMac();
71 * @return {string} Information about the current extension.
73 remoting.getExtensionInfo = function() {
74 var v2OrLegacy = base.isAppsV2() ? " (v2)" : " (legacy)";
75 var manifest = chrome.runtime.getManifest();
76 if (manifest && manifest.version) {
77 var name = remoting.app.getApplicationName();
78 return name + ' version: ' + manifest.version + v2OrLegacy;
80 return 'Failed to get product version. Corrupt manifest?';
85 * If an IT2Me client or host is active then prompt the user before closing.
86 * If a Me2Me client is active then don't bother, since closing the window is
87 * the more intuitive way to end a Me2Me session, and re-connecting is easy.
89 remoting.promptClose = function() {
90 if (remoting.desktopConnectedView &&
91 remoting.desktopConnectedView.getMode() ==
92 remoting.DesktopConnectedView.Mode.IT2ME) {
93 switch (remoting.currentMode) {
94 case remoting.AppMode.CLIENT_CONNECTING:
95 case remoting.AppMode.HOST_WAITING_FOR_CODE:
96 case remoting.AppMode.HOST_WAITING_FOR_CONNECTION:
97 case remoting.AppMode.HOST_SHARED:
98 case remoting.AppMode.IN_SESSION:
99 return chrome.i18n.getMessage(/*i18n-content*/'CLOSE_PROMPT');
107 * Sign the user out of Chromoting by clearing (and revoking, if possible) the
108 * OAuth refresh token.
110 * Also clear all local storage, to avoid leaking information.
112 remoting.signOut = function() {
113 remoting.oauth2.removeCachedAuthToken().then(function(){
114 chrome.storage.local.clear();
115 remoting.setMode(remoting.AppMode.HOME);
116 window.location.reload();
121 * Callback function called when the browser window gets a paste operation.
123 * @param {Event} event
124 * @return {void} Nothing.
126 function pluginGotPaste_(event) {
127 if (event && event.clipboardData) {
128 remoting.clipboard.toHost(event.clipboardData);
133 * Callback function called when the browser window gets a copy operation.
135 * @param {Event} event
136 * @return {void} Nothing.
138 function pluginGotCopy_(event) {
139 if (event && event.clipboardData) {
140 if (remoting.clipboard.toOs(event.clipboardData)) {
141 // The default action may overwrite items that we added to clipboardData.
142 event.preventDefault();
148 * Return the current time as a formatted string suitable for logging.
150 * @return {string} The current time, formatted as [mmdd/hhmmss.xyz]
152 remoting.timestamp = function() {
154 * @param {number} num A number.
155 * @param {number} len The required length of the answer.
156 * @return {string} The number, formatted as a string of the specified length
157 * by prepending zeroes as necessary.
159 var pad = function(num, len) {
160 var result = num.toString();
161 if (result.length < len) {
162 result = new Array(len - result.length + 1).join('0') + result;
166 var now = new Date();
167 var timestamp = pad(now.getMonth() + 1, 2) + pad(now.getDate(), 2) + '/' +
168 pad(now.getHours(), 2) + pad(now.getMinutes(), 2) +
169 pad(now.getSeconds(), 2) + '.' + pad(now.getMilliseconds(), 3);
170 return '[' + timestamp + ']';
174 * Show an error message, optionally including a short-cut for signing in to
177 * @param {remoting.Error} error
178 * @return {void} Nothing.
180 remoting.showErrorMessage = function(error) {
181 l10n.localizeElementFromTag(
182 document.getElementById('token-refresh-error-message'),
184 var auth_failed = (error == remoting.Error.AUTHENTICATION_FAILED);
185 if (base.isAppsV2()) {
186 remoting.handleAuthFailureAndRelaunch();
188 document.getElementById('token-refresh-auth-failed').hidden = !auth_failed;
189 document.getElementById('token-refresh-other-error').hidden = auth_failed;
190 remoting.setMode(remoting.AppMode.TOKEN_REFRESH_FAILED);
195 * Determine whether or not the app is running in a window.
196 * @param {function(boolean):void} callback Callback to receive whether or not
197 * the current tab is running in windowed mode.
199 function isWindowed_(callback) {
200 /** @param {chrome.Window} win The current window. */
201 var windowCallback = function(win) {
202 callback(win.type == 'popup');
204 /** @param {chrome.Tab} tab The current tab. */
205 var tabCallback = function(tab) {
209 chrome.windows.get(tab.windowId, null, windowCallback);
213 chrome.tabs.getCurrent(tabCallback);
215 console.error('chome.tabs is not available.');