Roll src/third_party/WebKit 3529d49:06e8485 (svn 202554:202555)
[chromium-blink-merge.git] / remoting / webapp / base / js / experiments.js
blobab6edc111b579e0e77c05c2946621b3c56d01303
1 // Copyright 2015 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.
5 /**
6  * @fileoverview
7  * Class for enabling experimental features.
8  */
10 'use strict';
12 /** @suppress {duplicate} */
13 var remoting = remoting || {};
15 (function() {
17 var kExperimentsStorageName = 'remoting-experiments';
18 var ACTIVE_FIELD_TRIALS = {'ChromotingQUIC': 'quic', 'ChromotingVP9': 'vp9'};
20 /**
21  * @param {Array.<string>} list
22  */
23 function save(list) {
24   var storageMap = {};
25   storageMap[kExperimentsStorageName] = list;
26   chrome.storage.local.set(storageMap);
29 /** @type {Object} */
30 remoting.experiments = {};
32 /**
33  * Enables an experiment.
34  *
35  * @param {string} experiment to enable.
36  */
37 remoting.experiments.enable = function(experiment) {
38   remoting.experiments.get().then(function(/** Array.<string> */list) {
39     if (list.indexOf(experiment) == -1) {
40       list.push(experiment);
41       save(list);
42     }
43   });
46 /**
47  * Disables an experiment.
48  *
49  * @param {string} experiment to disable.
50  */
51 remoting.experiments.disable = function(experiment) {
52   remoting.experiments.get().then(function(/** Array.<string> */list) {
53     list = list.filter(function(e) { return e !== experiment; });
54     save(list);
55   });
58 /**
59  * Determines if the field-trial is enabled for this session.
60  *
61  * @param {string} trialName The FieldTrial to check for enabling.
62  * @return {Promise}
63  */
64 function getTrialState(trialName) {
65   var deferred = new base.Deferred();
66   chrome.metricsPrivate.getFieldTrial(trialName,
67                                       function(/** string */ group) {
68                                         if (group == 'Enabled') {
69                                           deferred.resolve(true);
70                                         } else {
71                                           deferred.resolve(false);
72                                         }
73                                       });
74   return deferred.promise();
77 /**
78  * Returns list of all enabled experiments.
79  * @return {Promise}
80  */
81 remoting.experiments.get = function() {
83   var localStorageList = new Promise(function(resolve, reject) {
84     chrome.storage.local.get(kExperimentsStorageName, function(items) {
85       /** @type {Array<string>} */
86       var experiments = new Array();
87       if (items.hasOwnProperty(kExperimentsStorageName)) {
88         experiments = /** @type {Array<string>} */
89             (items[kExperimentsStorageName]);
90       }
91       resolve(experiments);
92     });
93   });
95   var trialNames = Object.keys(ACTIVE_FIELD_TRIALS);
97   return Promise.all([localStorageList].concat(
98                          trialNames.map(function(/** string */ trialName) {
99                            return getTrialState(trialName);
100                          })))
101       .then(function(results) {
102         /** @type {Array<string>} */
103         var list = results[0];
104         for (var i = 0; i < trialNames.length; ++i) {
105           if (results[i + 1]) {
106             list.push(ACTIVE_FIELD_TRIALS[trialNames[i]]);
107           }
108         }
109         return list;
110       });
113 })();