Andreea Costinas | 68cad91f | 2021-09-22 15:58:09 | [diff] [blame^] | 1 | // Copyright 2021 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 | #include "chrome/browser/lacros/net/proxy_config_service_lacros.h" |
| 6 | |
| 7 | #include <memory> |
| 8 | |
| 9 | #include "base/bind.h" |
| 10 | #include "base/callback.h" |
| 11 | #include "base/callback_helpers.h" |
| 12 | #include "base/logging.h" |
| 13 | #include "base/values.h" |
| 14 | #include "chrome/browser/lacros/net/network_settings_translation.h" |
| 15 | #include "chrome/browser/profiles/profile.h" |
| 16 | #include "chrome/browser/profiles/profile_manager.h" |
| 17 | #include "chrome/common/pref_names.h" |
| 18 | #include "chromeos/lacros/lacros_service.h" |
| 19 | #include "components/prefs/pref_registry_simple.h" |
| 20 | #include "components/prefs/pref_service.h" |
| 21 | #include "net/proxy_resolution/proxy_config.h" |
| 22 | #include "net/proxy_resolution/proxy_config_with_annotation.h" |
| 23 | |
| 24 | namespace { |
| 25 | |
| 26 | net::ProxyConfigWithAnnotation GetConfigOrDirect( |
| 27 | const absl::optional<net::ProxyConfigWithAnnotation>& optional_config, |
| 28 | PrefService* pref_service) { |
| 29 | if (!optional_config || !pref_service || |
| 30 | !pref_service->GetBoolean(prefs::kUseAshProxy)) { |
| 31 | net::ProxyConfigWithAnnotation config = |
| 32 | net::ProxyConfigWithAnnotation::CreateDirect(); |
| 33 | return config; |
| 34 | } |
| 35 | return optional_config.value(); |
| 36 | } |
| 37 | } // namespace |
| 38 | |
| 39 | namespace chromeos { |
| 40 | |
| 41 | ProxyConfigServiceLacros::ProxyConfigServiceLacros(Profile* profile) { |
| 42 | DCHECK(profile); |
| 43 | auto* lacros_service = chromeos::LacrosService::Get(); |
| 44 | // crosapi is disabled in browser_tests. |
| 45 | if (!lacros_service->IsAvailable<crosapi::mojom::NetworkSettingsService>()) { |
| 46 | LOG(ERROR) << "The NetworkSettingsService service is not available"; |
| 47 | return; |
| 48 | } |
| 49 | lacros_service->GetRemote<crosapi::mojom::NetworkSettingsService>() |
| 50 | ->AddNetworkSettingsObserver(receiver_.BindNewPipeAndPassRemote()); |
| 51 | |
| 52 | // `kUseAshProxy` is a user exposed setting whether to use the ash proxy (from |
| 53 | // the system) or whether to use profile specific proxy settings. This option |
| 54 | // is only given for secondary profiles. For the primary profile, the user has |
| 55 | // to use the system proxy settings, the value of kUseAshProxy is always true, |
| 56 | // and the setting is not exposed to the user. |
| 57 | // TODO(acostinas, b:192915915) Enable secondary profiles to configure |
| 58 | // `kUseAshProxy` from chrome://settings. |
| 59 | if (profile->IsMainProfile()) { |
| 60 | profile->GetPrefs()->SetBoolean(prefs::kUseAshProxy, true); |
| 61 | } |
| 62 | |
| 63 | profile_prefs_ = profile->GetPrefs(); |
| 64 | profile_pref_change_registrar_.Init(profile_prefs_); |
| 65 | profile_pref_change_registrar_.Add( |
| 66 | prefs::kUseAshProxy, |
| 67 | base::BindRepeating(&ProxyConfigServiceLacros::OnUseAshProxyPrefChanged, |
| 68 | base::Unretained(this))); |
| 69 | } |
| 70 | |
| 71 | ProxyConfigServiceLacros::~ProxyConfigServiceLacros() = default; |
| 72 | |
| 73 | void ProxyConfigServiceLacros::OnProxyChanged( |
| 74 | crosapi::mojom::ProxyConfigPtr proxy_config) { |
| 75 | cached_config_ = CrosapiProxyToNetProxy(std::move(proxy_config)); |
| 76 | NotifyObservers(); |
| 77 | } |
| 78 | |
| 79 | void ProxyConfigServiceLacros::OnUseAshProxyPrefChanged() { |
| 80 | NotifyObservers(); |
| 81 | } |
| 82 | |
| 83 | void ProxyConfigServiceLacros::NotifyObservers() { |
| 84 | for (auto& observer : observers_) { |
| 85 | observer.OnProxyConfigChanged( |
| 86 | GetConfigOrDirect(cached_config_, profile_prefs_), |
| 87 | ConfigAvailability::CONFIG_VALID); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | void ProxyConfigServiceLacros::AddObserver(Observer* observer) { |
| 92 | observers_.AddObserver(observer); |
| 93 | } |
| 94 | |
| 95 | void ProxyConfigServiceLacros::RemoveObserver(Observer* observer) { |
| 96 | observers_.RemoveObserver(observer); |
| 97 | } |
| 98 | |
| 99 | // static |
| 100 | void ProxyConfigServiceLacros::RegisterProfilePrefs( |
| 101 | PrefRegistrySimple* registry) { |
| 102 | registry->RegisterBooleanPref(prefs::kUseAshProxy, false); |
| 103 | } |
| 104 | |
| 105 | net::ProxyConfigService::ConfigAvailability |
| 106 | ProxyConfigServiceLacros::GetLatestProxyConfig( |
| 107 | net::ProxyConfigWithAnnotation* config) { |
| 108 | // Returns the last proxy configuration that the Ash |
| 109 | // NetworkSettingsService notified us, or the direct proxy if `cached_config_` |
| 110 | // is not set. |
| 111 | *config = GetConfigOrDirect(cached_config_, profile_prefs_); |
| 112 | return ConfigAvailability::CONFIG_VALID; |
| 113 | } |
| 114 | |
| 115 | } // namespace chromeos |