|
1 "use strict"; |
|
2 |
|
3 // runapp.js: |
|
4 // Provide a --runapp APPNAME command-line option. |
|
5 |
|
6 let runAppObj; |
|
7 window.addEventListener('load', function() { |
|
8 if (!window.arguments) { |
|
9 return; |
|
10 } |
|
11 |
|
12 // Get the command line arguments that were passed to the b2g client |
|
13 let args = window.arguments[0].QueryInterface(Ci.nsICommandLine); |
|
14 let appname; |
|
15 |
|
16 // - Check if the argument is present before doing any work. |
|
17 try { |
|
18 // Returns null if the argument was not specified. Throws |
|
19 // NS_ERROR_INVALID_ARG if there is no parameter specified (because |
|
20 // it was the last argument or the next argument starts with '-'). |
|
21 // However, someone could still explicitly pass an empty argument! |
|
22 appname = args.handleFlagWithParam('runapp', false); |
|
23 } catch(e) { |
|
24 // treat a missing parameter like an empty parameter (=> show usage) |
|
25 appname = ''; |
|
26 } |
|
27 |
|
28 // not specified, bail. |
|
29 if (appname === null) { |
|
30 return; |
|
31 } |
|
32 |
|
33 runAppObj = new AppRunner(appname); |
|
34 Services.obs.addObserver(runAppObj, 'browser-ui-startup-complete', false); |
|
35 }); |
|
36 |
|
37 window.addEventListener('unload', function() { |
|
38 if (runAppObj) { |
|
39 Services.obs.removeObserver(runAppObj, 'browser-ui-startup-complete'); |
|
40 } |
|
41 }); |
|
42 |
|
43 function AppRunner(aName) { |
|
44 this._req = null; |
|
45 this._appName = aName; |
|
46 } |
|
47 AppRunner.prototype = { |
|
48 observe: function(aSubject, aTopic, aData) { |
|
49 if (aTopic == 'browser-ui-startup-complete') { |
|
50 this.doRunApp(); |
|
51 } |
|
52 }, |
|
53 |
|
54 doRunApp: function() { |
|
55 // - Get the list of apps since the parameter was specified |
|
56 this._req = navigator.mozApps.mgmt.getAll(); |
|
57 this._req.onsuccess = this.getAllSuccess.bind(this); |
|
58 this._req.onerror = this.getAllError.bind(this); |
|
59 }, |
|
60 |
|
61 getAllSuccess: function() { |
|
62 let apps = this._req.result; |
|
63 |
|
64 function findAppWithName(name) { |
|
65 let normalizedSearchName = name.replace(/[- ]+/g, '').toLowerCase(); |
|
66 |
|
67 for (let i = 0; i < apps.length; i++) { |
|
68 let app = apps[i]; |
|
69 let normalizedAppName = |
|
70 app.manifest.name.replace(/[- ]+/g, '').toLowerCase(); |
|
71 if (normalizedSearchName === normalizedAppName) { |
|
72 return app; |
|
73 } |
|
74 } |
|
75 return null; |
|
76 } |
|
77 |
|
78 function usageAndDie(justApps) { |
|
79 if (!justApps) |
|
80 dump( |
|
81 'The --runapp argument specifies an app to automatically run at\n'+ |
|
82 'startup. We match against app names per their manifest and \n' + |
|
83 'ignoring capitalization, dashes, and whitespace.\n' + |
|
84 '\nThe system will load as usual except the lock screen will be ' + |
|
85 'automatically be disabled.\n\n' + |
|
86 'Known apps:\n'); |
|
87 |
|
88 for (let i = 0; i < apps.length; i++) { |
|
89 dump(' ' + apps[i].manifest.name + '\n'); |
|
90 } |
|
91 |
|
92 // Exit the b2g client |
|
93 Services.startup.quit(Ci.nsIAppStartup.eAttemptQuit); |
|
94 } |
|
95 |
|
96 if (this._appName === '') { |
|
97 usageAndDie(); |
|
98 return; |
|
99 } |
|
100 |
|
101 let app = findAppWithName(this._appName); |
|
102 if (!app) { |
|
103 dump('Could not find app: "' + this._appName + '". Maybe you meant one of:\n'); |
|
104 usageAndDie(true); |
|
105 return; |
|
106 } |
|
107 |
|
108 let setReq = |
|
109 navigator.mozSettings.createLock().set({'lockscreen.enabled': false}); |
|
110 setReq.onsuccess = function() { |
|
111 // give the event loop 100ms to disable the lock screen |
|
112 window.setTimeout(function() { |
|
113 dump('--runapp launching app: ' + app.manifest.name + '\n'); |
|
114 app.launch(); |
|
115 }, 100); |
|
116 }; |
|
117 setReq.onerror = function() { |
|
118 dump('--runapp failed to disable lock-screen. Giving up.\n'); |
|
119 }; |
|
120 |
|
121 dump('--runapp found app: ' + app.manifest.name + |
|
122 ', disabling lock screen...\n'); |
|
123 }, |
|
124 |
|
125 getAllError: function() { |
|
126 dump('Problem getting the list of all apps!'); |
|
127 } |
|
128 }; |