Upstream version 7.35.144.0
[platform/framework/web/crosswalk.git] / src / chrome / test / functional / perf / endure_graphs / js / common.js
1 /*
2   Copyright (c) 2012 The Chromium Authors. All rights reserved.
3   Use of this source code is governed by a BSD-style license that can be
4   found in the LICENSE file.
5 */
6
7 /**
8  * @fileoverview Common methods for performance-plotting Javascript.
9  */
10
11 /**
12  * Fetches a URL asynchronously and invokes a callback when complete.
13  *
14  * @param {string} url URL to fetch.
15  * @param {Function(string, string)} callback The function to invoke when the
16  *     results of the URL fetch are complete.  The function should accept two
17  *     strings representing the URL data, and any errors, respectively.
18  */
19 function Fetch(url, callback) {
20   var r = new XMLHttpRequest();
21   r.open('GET', url, true);
22   r.setRequestHeader('pragma', 'no-cache');
23   r.setRequestHeader('cache-control', 'no-cache');
24
25   r.onreadystatechange = function() {
26     if (r.readyState == 4) {
27       var text = r.responseText;
28       var error;
29       if (r.status != 200)
30         error = url + ': ' + r.status + ': ' + r.statusText;
31       else if (!text)
32         error = url + ': null response';
33       callback(text, error);
34     }
35   }
36
37   r.send(null);
38 }
39
40 /**
41  * Parses the parameters of the current page's URL.
42  *
43  * @return {Object} An object with properties given by the parameters specified
44  *     in the URL's query string.
45  */
46 function ParseParams() {
47   var result = new Object();
48
49   var query = window.location.search.substring(1)
50   if (query.charAt(query.length - 1) == '/')
51     query = query.substring(0, query.length - 1)  // Strip trailing slash.
52   var s = query.split('&');
53
54   for (i = 0; i < s.length; ++i) {
55     var v = s[i].split('=');
56     var key = v[0];
57     var value = unescape(v[1]);
58     result[key] = value;
59   }
60
61   if ('history' in result) {
62     result['history'] = parseInt(result['history']);
63     result['history'] = Math.max(result['history'], 2);
64   }
65   if ('rev' in result) {
66     result['rev'] = parseInt(result['rev']);
67     result['rev'] = Math.max(result['rev'], -1);
68   }
69
70   return result;
71 }
72
73 /**
74  * Creates the URL constructed from the current pathname and the given params.
75  *
76  * @param {Object} An object containing parameters for a URL query string.
77  * @return {string} The URL constructed from the given params.
78  */
79 function MakeURL(params) {
80   var url = window.location.pathname;
81   var sep = '?';
82   for (p in params) {
83     if (!p)
84       continue;
85     url += sep + p + '=' + params[p];
86     sep = '&';
87   }
88   return url;
89 }