1/*
2Copyright 2012 Google Inc.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8     http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15
16Author: Renato Mangini (mangini@chromium.org)
17*/
18
19(function(exports) {
20
21  function Commands() {
22  	this.commands={};
23  }
24
25  Commands.prototype.addCommand=function(name, help, runnable) {
26  	if (name in this.commands) {
27  		console.log("WARNING: ignoring duplicate command "+name);
28  		return;
29  	}
30  	this.commands[name] = {help: help, runnable: runnable};
31  }
32
33  Commands.prototype.help=function(name, args) {
34    var result='';
35    for (var command in this.commands) {
36      result+=command+'\t'+this.commands[command].help+"\n";
37    }
38    return result;
39  	/*if (! (name in this.commands)) {
40  		return "Unknown command "+name;
41  	}
42  	var context={out: out};
43  	return this.commands[name].help.apply(context, args);*/
44  }
45
46  Commands.prototype.run=function(name, args) {
47    if (name === 'help') {
48      return this.help(name, args);
49    }
50  	if (! (name in this.commands)) {
51  		throw 'Unknown command '+name+'. Try "help"';
52  	}
53  	var context={};
54  	return this.commands[name].runnable.call(context, args);
55  }
56
57  exports.Commands=new Commands();
58
59})(window);
60
61
62Commands.addCommand("echo",
63	"Echo the arguments",
64	function(args) {
65		return args.join(' ');
66	});
67
68Commands.addCommand("open",
69  "Open the given URL",
70  function(args) {
71    chrome.app.window.create('commands/webview.html', {innerBounds: {width: 600, height: 400}},
72      function(w) {
73        w.contentWindow.addEventListener("DOMContentLoaded", function() {
74          var doc=w.contentWindow.document;
75          var el=doc.querySelector("webview");
76          el.src=args[0];
77        });
78      });
79    return "ok, url "+args[0]+" open";
80  });
81