Upstream version 11.39.266.0
[platform/framework/web/crosswalk.git] / src / remoting / webapp / background / background.js
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.
4
5 /** @suppress {duplicate} */
6 var remoting = remoting || {};
7
8 (function(){
9
10 /** @param {remoting.AppLauncher} appLauncher */
11 function initializeAppV2(appLauncher) {
12   /** @type {string} */
13   var kNewWindowId = 'new-window';
14
15   /** @param {OnClickData} info */
16   function onContextMenu(info) {
17     if (info.menuItemId == kNewWindowId) {
18       appLauncher.launch();
19     }
20   }
21
22   function initializeContextMenu() {
23     chrome.contextMenus.create({
24        id: kNewWindowId,
25        contexts: ['launcher'],
26        title: chrome.i18n.getMessage(/*i18n-content*/'NEW_WINDOW')
27     });
28     chrome.contextMenus.onClicked.addListener(onContextMenu);
29   }
30
31   initializeContextMenu();
32   chrome.app.runtime.onLaunched.addListener(
33       appLauncher.launch.bind(appLauncher)
34   );
35 }
36
37 /**
38  * The background service is responsible for listening to incoming connection
39  * requests from Hangouts and the webapp.
40  *
41  * @param {remoting.AppLauncher} appLauncher
42  */
43 function initializeBackgroundService(appLauncher) {
44   function initializeIt2MeService() {
45     /** @type {remoting.It2MeService} */
46     remoting.it2meService = new remoting.It2MeService(appLauncher);
47     remoting.it2meService.init();
48   }
49
50   chrome.runtime.onSuspend.addListener(function() {
51     base.debug.assert(remoting.it2meService != null);
52     remoting.it2meService.dispose();
53     remoting.it2meService = null;
54   });
55
56   remoting.settings = new remoting.Settings();
57
58   chrome.runtime.onSuspendCanceled.addListener(initializeIt2MeService);
59   initializeIt2MeService();
60 }
61
62 function main() {
63   if (base.isAppsV2()) {
64     var appLauncher = new remoting.V2AppLauncher();
65     initializeAppV2(appLauncher);
66   }
67 }
68
69 remoting.enableHangoutsRemoteAssistance = function() {
70   /** @type {remoting.AppLauncher} */
71   var appLauncher = base.isAppsV2() ? new remoting.V1AppLauncher():
72                                       new remoting.V2AppLauncher();
73   initializeBackgroundService(appLauncher);
74 };
75
76 window.addEventListener('load', main, false);
77
78 }());