/*
 * Copyright (c), Log-Normal, Inc.
 */

/**
\file memory.js
Plugin to collect memory metrics when available.
see: http://code.google.com/p/chromium/issues/detail?id=43281
*/

(function() {
var w, p={}, d, m, s, impl;
// First make sure BOOMR is actually defined.  It's possible that your plugin is loaded before boomerang, in which case
// you'll need this.
BOOMR = BOOMR || {};
BOOMR.plugins = BOOMR.plugins || {};
if (BOOMR.plugins.Memory) {
	return;
}

function nodeList(type) {
	return d.getElementsByTagName(type);
}

// A private object to encapsulate all your implementation details
impl = {
	done: function() {
		var res, doms={}, a;
		// If we have resource timing, get number of resources
		if(p && p.getEntriesByType && p.getEntriesByType("resource").length) {
			res = p.getEntriesByType("resource");
			BOOMR.addVar("dom.res", res.length);

			a = document.createElement("a");

			res.forEach(function(r) {
				a.href=r.name;
				doms[a.hostname] = true;
			});

			BOOMR.addVar("dom.doms", Object.keys(doms).length);
		}
		else {
			BOOMR.removeVar("dom.res");
		}

		if(m) {
			BOOMR.addVar({
				"mem.total": m.totalJSHeapSize,
				"mem.used" : m.usedJSHeapSize
			});
		}

		try {
			BOOMR.addVar({
				"dom.ln": nodeList("*").length,
				"dom.sz": nodeList("html")[0].innerHTML.length,
				"dom.img": nodeList("img").length,
				"dom.script": nodeList("script").length
			});
		}
		catch(err) {
			BOOMR.addError(err, "Memory.done.dom");
		}

		if(s) {
			try {
				BOOMR.addVar({
					"scr.xy": s.width + "x" + s.height,
					"scr.bpp": s.colorDepth + "/" + s.pixelDepth
				});
				if(s.orientation) {
					BOOMR.addVar("scr.orn", s.orientation.angle + "/" + s.orientation.type);
				}
				if(w.devicePixelRatio > 1) {
					BOOMR.addVar("scr.dpx", w.devicePixelRatio);
				}
			}
			catch(err) {
				BOOMR.addError(err, "Memory.done.screen");
			}
		}

		// no need of sendBeacon because we're called when the beacon is being sent
	}
};

BOOMR.plugins.Memory = {
	init: function() {
		var c;

		try {
			w = BOOMR.window;
			d = w.document;
			p = w.performance;
			c = w.console;
			s = w.screen;
		}
		catch(err) {
			BOOMR.addError(err, "Memory.init");
		}

		m = (p && p.memory ? p.memory : (c && c.memory ? c.memory : null));

		// we do this before sending a beacon to get the snapshot when the beacon is sent
		BOOMR.subscribe("before_beacon", impl.done, null, impl);
		return this;
	},

	is_complete: function() {
		// Always true since we run on before_beacon, which happens after the check
		return true;
	}
};

}());


@@ -206,16 +206,19 @@
 d, m, s,
+ n,
  impl;%0A/
@@ -1872,16 +1872,361 @@
 %09%7D%0A%09%09%7D%0A%0A
+%09%09if(n) %7B%0A%09%09%09try %7B%0A%09%09%09%09if(n.battery) %7B%0A%09%09%09%09%09BOOMR.addVar(%22bat.lvl%22, n.battery.level);%0A%09%09%09%09%7D%0A%09%09%09%09if(n.hardwareConcurrency) %7B%0A%09%09%09%09%09BOOMR.addVar(%22cpu.cnc%22, n.hardwareConcurrency);%0A%09%09%09%09%7D%0A%09%09%09%09if(n.maxTouchPoints) %7B%0A%09%09%09%09%09BOOMR.addVar(%22scr.mtp%22, n.maxTouchPoints);%0A%09%09%09%09%7D%0A%09%09%09%7D%0A%09%09%09catch(err) %7B%0A%09%09%09%09BOOMR.addError(err, %22Memory.done.navigator%22);%0A%09%09%09%7D%0A%09%09%7D%0A%0A
 %09%09// no 
@@ -2462,16 +2462,36 @@
 screen;%0A
+%09%09%09n = w.navigator;%0A
 %09%09%7D%0A%09%09ca

add battery level, cpu hardware concurrency and max touchpoints if available