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.
6 * @fileoverview A singleton that keeps track of the VPN providers enabled in
7 * the primary user's profile.
10 cr.define('options', function() {
14 function VPNProviders() {
17 cr.addSingletonGetter(VPNProviders);
19 VPNProviders.prototype = {
21 * The VPN providers enabled in the primary user's profile. Each provider
22 * has a name. Third-party VPN providers additionally have an extension ID.
23 * @type {!Array<{name: string, extensionID: ?string}>}
29 * Observers who will be called when the list of VPN providers changes.
30 * @type {!Array<!function()>}
35 * The VPN providers enabled in the primary user's profile.
36 * @type {!Array<{name: string, extensionID: ?string}>}
39 return this.providers_;
41 set providers(providers) {
42 this.providers_ = providers;
43 for (var i = 0; i < this.observers_.length; ++i)
48 * Adds an observer to be called when the list of VPN providers changes.
49 * @param {!function()} observer The observer to add.
52 addObserver_: function(observer) {
53 this.observers_.push(observer);
57 * Formats a network name for display purposes. If the network belongs to
58 * a third-party VPN provider, the provider name is added to the network
60 * @param {cr.onc.OncData} onc ONC data describing this network.
61 * @return {string} The resulting display name.
64 formatNetworkName_: function(onc) {
65 var networkName = onc.getTranslatedValue('Name');
66 if (onc.getActiveValue('VPN.Type') != 'ThirdPartyVPN')
68 var extensionID = onc.getActiveValue('VPN.ThirdPartyVPN.ExtensionID');
69 for (var i = 0; i < this.providers_.length; ++i) {
70 if (extensionID == this.providers_[i].extensionID) {
71 return loadTimeData.getStringF('vpnNameTemplate',
72 this.providers_[i].name,
81 * Adds an observer to be called when the list of VPN providers changes. Note
82 * that an observer may in turn call setProviders() but should be careful not
83 * to get stuck in an infinite loop as every change to the list of VPN
84 * providers will cause the observers to be called again.
85 * @param {!function()} observer The observer to add.
87 VPNProviders.addObserver = function(observer) {
88 VPNProviders.getInstance().addObserver_(observer);
92 * Returns the list of VPN providers enabled in the primary user's profile.
93 * @return {!Array<{name: string, extensionID: ?string}>} The list of VPN
94 * providers enabled in the primary user's profile.
96 VPNProviders.getProviders = function() {
97 return VPNProviders.getInstance().providers;
101 * Replaces the list of VPN providers enabled in the primary user's profile.
102 * @param {!Array<{name: string, extensionID: ?string}>} providers The list
103 * of VPN providers enabled in the primary user's profile.
105 VPNProviders.setProviders = function(providers) {
106 VPNProviders.getInstance().providers = providers;
110 * Formats a network name for display purposes. If the network belongs to a
111 * third-party VPN provider, the provider name is added to the network name.
112 * @param {cr.onc.OncData} onc ONC data describing this network.
113 * @return {string} The resulting display name.
115 VPNProviders.formatNetworkName = function(onc) {
116 return VPNProviders.getInstance().formatNetworkName_(onc);
121 VPNProviders: VPNProviders