// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/**
 * @fileoverview Settings that affect how Chrome interacts with the underlying
 * operating system (i.e. network, background processes, hardware).
 */
import 'chrome://resources/cr_elements/cr_button/cr_button.js';
import 'chrome://resources/cr_elements/cr_icon_button/cr_icon_button.js';
import 'chrome://resources/cr_elements/policy/cr_policy_indicator.js';
import '/shared/settings/controls/cr_policy_pref_indicator.js';
import '/shared/settings/controls/extension_controlled_indicator.js';
import '../controls/settings_toggle_button.js';
import '../relaunch_confirmation_dialog.js';
import '../settings_page/settings_section.js';
import '../settings_shared.css.js';
import { PrefsMixin } from '/shared/settings/prefs/prefs_mixin.js';
import { WebUiListenerMixin } from 'chrome://resources/cr_elements/web_ui_listener_mixin.js';
import { PolymerElement } from 'chrome://resources/polymer/v3_0/polymer/polymer_bundled.min.js';
// 
import { RelaunchMixin, RestartType } from '../relaunch_mixin.js';
import { getSearchManager } from '../search_settings.js';
// 
import { getTemplate } from './system_page.html.js';
import { SystemPageBrowserProxyImpl } from './system_page_browser_proxy.js';
const SettingsSystemPageElementBase = WebUiListenerMixin(PrefsMixin(RelaunchMixin(PolymerElement)));
export class SettingsSystemPageElement extends SettingsSystemPageElementBase {
    static get is() {
        return 'settings-system-page';
    }
    static get template() {
        return getTemplate();
    }
    static get properties() {
        return {
            // 
            isProxyEnforcedByPolicy_: Boolean,
            isProxyDefault_: Boolean,
            isProxyEnforcedByMultipleSources_: Boolean,
            // 
            // 
        };
    }
    static get observers() {
        return [
            'observeProxyPrefChanged_(prefs.proxy.*)',
            'observeProxyPrefChanged_(prefs.proxy_override_rules.*)',
        ];
    }
    // 
    // 
    // 
    // 
    observeProxyPrefChanged_() {
        const pref = this.getPref('proxy');
        // TODO(dbeam): do types of policy other than USER apply on ChromeOS?
        this.isProxyEnforcedByPolicy_ =
            pref.enforcement === chrome.settingsPrivate.Enforcement.ENFORCED &&
                pref.controlledBy === chrome.settingsPrivate.ControlledBy.USER_POLICY;
        this.isProxyDefault_ = !this.isProxyEnforcedByPolicy_ && !pref.extensionId;
        const rulesPref = this.getPref('proxy_override_rules');
        // Don't need to consider multiple source display when
        // `ProxyOverrideRules` preference is not set
        if (!rulesPref.value || rulesPref.value.length === 0) {
            this.isProxyEnforcedByMultipleSources_ = false;
            return;
        }
        // Don't need to consider multiple source display when proxy setting is not
        // set
        if (this.isProxyDefault_) {
            this.isProxyEnforcedByMultipleSources_ = true;
            return;
        }
        // When proxy settings and `ProxyOverrideRules` are from different levels of
        // sources
        if (pref.controlledBy !== rulesPref.controlledBy) {
            this.isProxyEnforcedByMultipleSources_ = true;
            return;
        }
        // When proxy settings and `ProxyOverrideRules` are both from policies, the
        // sources are considered to be the same
        if (pref.controlledBy === chrome.settingsPrivate.ControlledBy.USER_POLICY) {
            this.isProxyEnforcedByMultipleSources_ = false;
            return;
        }
        // When proxy settings and `ProxyOverrideRules` are both from extension(s),
        // the sources are considered to be the same only if they are set by the
        // same extension
        this.isProxyEnforcedByMultipleSources_ =
            (pref.extensionId !== rulesPref.extensionId);
    }
    onDisableExtensionClick_() {
        // TODO(dbeam): this is a pretty huge bummer. It means there are things
        // (inputs) that our prefs system is not observing. And that changes from
        // other sources (i.e. disabling/enabling an extension from
        // chrome://extensions or from the omnibox directly) will not update
        // |this.getPref('proxy')| directly (nor the UI). We should fix this
        // eventually.
        this.dispatchEvent(new CustomEvent('refresh-pref', { bubbles: true, composed: true, detail: 'proxy' }));
    }
    onProxyClick_() {
        if (this.isProxyDefault_) {
            SystemPageBrowserProxyImpl.getInstance().showProxySettings();
        }
    }
    onRestartClick_(e) {
        // Prevent event from bubbling up to the toggle button.
        e.stopPropagation();
        this.performRestart(RestartType.RESTART);
    }
    // 
    /**
     * @param enabled Whether hardware acceleration is currently enabled.
     */
    shouldShowRestart_(enabled) {
        const proxy = SystemPageBrowserProxyImpl.getInstance();
        return enabled !== proxy.wasHardwareAccelerationEnabledAtStartup();
    }
    // 
    // 
    // SettingsPlugin implementation
    async searchContents(query) {
        const searchRequest = await getSearchManager().search(query, this);
        return searchRequest.getSearchResult();
    }
}
customElements.define(SettingsSystemPageElement.is, SettingsSystemPageElement);
