Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / tools / chrome_proxy / integration_tests / chrome_proxy_metrics.js
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 // This file contains common utilities to find chrome proxy related elements on
6 // a page and collect info from them.
7
8 (function() {
9   var PROXY_VIEW_ID = 'proxy-view-tab-content';
10   var PROXY_VIEW_EFFECTIVE_SETTINGS_ID = 'proxy-view-effective-settings';
11   var PROXY_VIEW_BAD_PROXIES_ID = 'proxy-view-bad-proxies-div';
12   var PROXY_VIEW_BAD_PROXIES_TBODY = 'proxy-view-bad-proxies-tbody';
13   var PRXOY_SETTINGS_PREFIX = 'Proxy server for HTTP: ['
14   var PROXY_SETTINGS_SIGNATURE = 'proxy.googlezip.net:443, ' +
15     'compress.googlezip.net:80, direct://';
16
17   // Returns the effective proxy in an array from settings.
18   // An example of the settings is:
19   // "Proxy server for HTTP: [proxy.googlezip.net:443, " +
20   // "compress.googlezip.net:80, direct://]"
21   function getEffectiveProxies(doc) {
22     var settings = doc.getElementById(PROXY_VIEW_EFFECTIVE_SETTINGS_ID);
23     if (settings && settings.innerHTML &&
24       settings.innerHTML.indexOf(PRXOY_SETTINGS_PREFIX) == 0) {
25       var left = settings.innerHTML.indexOf('[');
26       var right = settings.innerHTML.indexOf(']');
27       if (left >= 0 && right > left) {
28         return settings.innerHTML.substring(left + 1, right).split(/[ ,]+/);
29       }
30     }
31     return [];
32   }
33
34   // Returns an array of bad proxies. Each element is a bad proxy with
35   // attribute 'proxy' as the proxy name and attribute 'retry' as the
36   // next retry time.
37   function getBadProxyList(doc) {
38     var bad_proxies = doc.getElementById(PROXY_VIEW_BAD_PROXIES_ID);
39     if (bad_proxies.hasAttribute('style') &&
40         ('cssText' in bad_proxies.style) &&
41         bad_proxies.style.cssText == 'display: none;') {
42           return null;
43         }
44     var tbody = doc.getElementById(PROXY_VIEW_BAD_PROXIES_TBODY);
45     results = [];
46     for (var r = 0, n = tbody.rows.length; r < n; r++) {
47       results[r] = {};
48       results[r].proxy = tbody.rows[r].cells[0].innerHTML;
49       timeSpan = tbody.rows[r].cells[1].getElementsByTagName('span')[0];
50       if (timeSpan.hasAttribute('title') && timeSpan.title.indexOf('t=') == 0) {
51         results[r].retry = timeSpan.title.substr(2);
52       } else {
53         results[r].retry = '-1';
54       }
55     }
56     return results;
57   }
58
59   function getChromeProxyInfo() {
60     if (!document.getElementById(PROXY_VIEW_ID)) {
61       return null;
62     }
63     info = {};
64     info.proxies = getEffectiveProxies(document);
65     info.enabled = (info.proxies.length > 1 &&
66         info.proxies[info.proxies.length - 1] == 'direct://' &&
67         info.proxies[info.proxies.length - 2] != 'direct://');
68     info.badProxies = getBadProxyList(document);
69     return info;
70   };
71   window.__getChromeProxyInfo = getChromeProxyInfo;
72 })();