diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..bd25702 --- /dev/null +++ b/.gitignore @@ -0,0 +1,7 @@ +## Ignore Visual Studio temporary files, build results, and +## files generated by popular Visual Studio add-ons. +## Picked and pieced from here: https://github.com/github/gitignore/blob/master/VisualStudio.gitignore + +# Don't want any of our prebuilt files getting checked in +IL/ + diff --git a/README.md b/README.md index 26945e6..06b3993 100644 --- a/README.md +++ b/README.md @@ -1,119 +1,30 @@ -![Try it now in CWS](https://raw.github.com/GoogleChrome/chrome-app-samples/master/tryitnowbutton.png "Click here to install this sample from the Chrome Web Store") - -# Chrome Web Server - an HTTP web server for Chrome (chrome.sockets) - -Get it in the chrome web store: -https://chrome.google.com/webstore/detail/web-server-for-chrome/ofhbbkphhbklhfoeikjpcbhemlocgigb - -Many people have found the webstore version useful for doing some basic web development as an alternative to python -m SimpleHTTPServer. But it can also be used for quick file sharing over a local network. - -Features -- serve local files -- configure listening port -- configure listening interface (e.g. localhost or all interfaces) -- custom http handlers possible -- websocket support available -- works nice with chrome.runtime.onSuspend -- options for autostart, start in background, etc etc. -- handles range requests, HEAD, etc -- options for CORS -- optional PUT, DELETE request (for upload files) -- sets MIME types -- can render directory listing -- See relevant options: https://github.com/kzahel/web-server-chrome/blob/master/polymer-ui/options.js - - -How to include into your own chrome app === - -run minimize.sh to concatenate all the required files together and then include the resulting wsc-chrome.min.js in your project. Here is an example of another project's usage: https://github.com/zebradog/kiosk/blob/f7a398f697edc1c22b90c14f959779f1e850012a/src/js/main.js#L124 - +This is based on an existing extension in the web store called Chrome Web Server === -Basic usage: - -``` -var app = new WSC.WebApplication(options) -app.start( callback ) -``` - -options: object, with keys -- handlers: array of handlers, -- renderIndex: boolean (whether to render index.html if in directory) -- optBackground: whether to run even if the window is closed -- optAutoStart: whether to auto start when chrome starts -- port: int (port to listen on) -- See relevant options: https://github.com/kzahel/web-server-chrome/blob/master/polymer-ui/options.js - -``` -Handlers - var handlers = [ - ['/favicon.ico',FavIconHandler], - ['/stream.*',StreamHandler], - ['/static/(.*)',StaticHandler], - ['.*', DefaultHandler] - ] -``` - -handlers is an array of 2 element arrays where the first item is a regular expression for the URL and the second is the handler class, which should extend WSC.BaseHandler - -``` - function StaticHandler() { - this.disk = null - chrome.runtime.getPackageDirectoryEntry( function(entry) { this.disk = entry }.bind(this) ) - WSC.BaseHandler.prototype.constructor.call(this) - } - var FavIconHandlerprototype = { - get: function(path) { - // USE HTML5 filesystem operations to read file - - }, - onReadFile: function(evt) { - if (evt.error) { - this.write('disk access error') - } else { - this.write(evt) - } - } - } - _.extend(StaticHandler.prototype, - StaticHandlerprototype, - WSC.BaseHandler.prototype - ) -``` - - -==== -Building -==== -Unfortunately there is a build process if you want to run this from source directly because I am using a Polymer (polymer-project.org) user interface. There is a bower.json in the polymer-ui folder and you will need to install node+npm+bower and then run bower install from that folder. Oh, and then you will need to "Refactor for CSP" (chrome apps do not allow inline scripts), one way of doing this is using https://chrome.google.com/webstore/detail/chrome-dev-editor-develop/pnoffddplpippgcfjdhbmhkofpnaalpg (open the folder and right click and select refactor for CSP) - -I'm now using a script that can do this (look in polymer-ui/build.sh. You'll need to npm install -g vulcanize crisper) - -==== Get it in the chrome web store: -https://chrome.google.com/webstore/detail/web-server-for-chrome/ofhbbkphhbklhfoeikjpcbhemlocgigb - -The default behavior right now is very simple. You choose a directory -to serve static content. It is now able to stream large files and -handle range requests. It also sets mime types correctly. -Here is an example project based on it: -https://chrome.google.com/webstore/detail/flv-player/dhogabmliblgpadclikpkjfnnipeebjm - -==== +https://chrome.google.com/webstore/detail/web-server-for-chrome/ofhbbkphhbklhfoeikjpcbhemlocgigb -MIT license +== +General modifications made +== -I wrote this because the example app provided by google would lock and -hang and had all sorts of nasty race conditions. Plus it would not -stream large files or do range requests, HEAD requests, etc, etc. +* No static directory or configuration required because we server up IL's WebGL build from the IL folder directly +* The buffer sizes increased to server our pages up quicker +* We force the app to server the jsgz files to make it transfer less data +* Manifest modified to accomodate recordings -The design of this is inspired heavily by to the Python Tornado Web -library. In this as well as that, you create an "app" which registers -handlers. Then under the hood it will accept connections, create an -HTTPConnection object, and that has an associated IOStream object -which handles the nonblocking read/write events for you. +== +Build instructions +== +To make something that can go to the Chrome Web Store is pretty simple but here are the steps: -See CREDITS file +* Copy the WebGL build from Q:\Tablet\PotentialRelease\Sprint##\1.##.#####\bin into the IL folder +* Open the uniquely named folder and delete the release directory (files aren't used) +* Open the templatedata directory and rename Booster.png to booster.png +* In the compressed directory you can delete WebGL.jsgz since it isn't used (10 mb-ish) +* Open the manifest.json from the root and update the version number to match whatever you've just added + +At that point you can send the package to the Chrome Web Store diff --git a/TODO b/TODO deleted file mode 100644 index 8da3d88..0000000 --- a/TODO +++ /dev/null @@ -1,2 +0,0 @@ -- if webserver turned off and close it, dont start even if bg mode on -- clean up server socket when fail on listen or other failure \ No newline at end of file diff --git a/background.js b/background.js index 9220f58..f9e9bee 100644 --- a/background.js +++ b/background.js @@ -5,135 +5,137 @@ var HADEVENT = false var OS var localOptions if (navigator.userAgent.match('OS X')) { - OS = 'Mac' + OS = 'Mac' } else if (navigator.userAgent.match("Windows")) { - OS = "Win" + OS = "Win" } else { - OS = "Chrome" + OS = "Chrome" } function onchoosefolder(entry) { - if (entry) { - var retainstr = chrome.fileSystem.retainEntry(entry) - var d = {'retainstr':retainstr} - chrome.storage.local.set(d) - console.log('set retainstr!') - var webapp = get_webapp() - if (webapp) { - var fs = new WSC.FileSystem(entry) - webapp.fs = fs - webapp.handlers = [] - webapp.add_handler(['.*',WSC.DirectoryEntryHandler.bind(null,fs)]) - webapp.init_handlers() - webapp.change() - } - // reload UI, restart server... etc - } + if (entry) { + var retainstr = chrome.fileSystem.retainEntry(entry) + var d = { 'retainstr': retainstr } + chrome.storage.local.set(d) + console.log('set retainstr!') + var webapp = get_webapp() + if (webapp) { + var fs = new WSC.FileSystem(entry) + webapp.fs = fs + webapp.handlers = [] + webapp.add_handler(['.*', WSC.DirectoryEntryHandler.bind(null, fs)]) + webapp.init_handlers() + webapp.change() + } + // reload UI, restart server... etc + } } function settings_ready(d) { - localOptions = d - console.log('settings:',d) - setTimeout( maybeStartup, 2000 ) // give background accept handler some time to trigger - //chrome.alarms.getAll( onAllAlarms ) + localOptions = d + console.log('settings:', d) + setTimeout(maybeStartup, 2000) // give background accept handler some time to trigger + //chrome.alarms.getAll( onAllAlarms ) } chrome.storage.local.get(null, settings_ready) function maybeStartup() { if (getting_settings) { return } // accept handler if (had_backgroundaccept) { return } - if (localOptions.optBackground && localOptions.optAutoStart) { - console.log('background && autostart. wake up!') - get_webapp(localOptions) - if (app.started || app.starting || app.starting_interfaces) { - console.log('actually, dont wake up, im already started/starting') - } else { - app.start() - } - } + if (localOptions.optBackground && localOptions.optAutoStart) { + console.log('background && autostart. wake up!') + get_webapp(localOptions) + if (app.started || app.starting || app.starting_interfaces) { + console.log('actually, dont wake up, im already started/starting') + } else { + app.start() + } + } } -function onAlarm( alarm ) { - console.log('alarm fired',alarm) - if (alarm.name == ALARMID) { - //sendWSCAwakeMessage() - } +function onAlarm(alarm) { + console.log('alarm fired', alarm) + if (alarm.name == ALARMID) { + //sendWSCAwakeMessage() + } } //chrome.alarms.onAlarm.addListener( onAlarm ) -function backgroundSettingChange( opts ) { - if (opts.optBackground !== undefined) { - localOptions.optBackground = opts.optBackground - } - if (opts.optPreventSleep !== undefined) { - localOptions.optPreventSleep = opts.optPreventSleep - } - if (opts.optBackground !== undefined) { - localOptions.optAutoStart = opts.optAutoStart - } +function backgroundSettingChange(opts) { + if (opts.optBackground !== undefined) { + localOptions.optBackground = opts.optBackground + } + if (opts.optPreventSleep !== undefined) { + localOptions.optPreventSleep = opts.optPreventSleep + } + if (opts.optBackground !== undefined) { + localOptions.optAutoStart = opts.optAutoStart + } /* - if (localOptions.optBackground && localOptions.optAutoStart) { - chrome.alarms.getAll( onAllAlarms ) - } else { - chrome.alarms.clearAll() - }*/ + if (localOptions.optBackground && localOptions.optAutoStart) { + chrome.alarms.getAll( onAllAlarms ) + } else { + chrome.alarms.clearAll() + }*/ } -function onAllAlarms( alarms ) { +function onAllAlarms(alarms) { return - if (! localOptions.optBackground) { - return - } - if (! localOptions.optAutoStart) { - return - } - var found = false - - console.log('got alarms',alarms) - for (var i=0; i