Commit eaa283bc authored by pbond's avatar pbond Committed by Commit bot

arc: update proxy settings from UI and ONC policy.

BUG=643239
TEST=manual(UI),autotest(extension API),browsertest(policy)

Review-Url: https://codereview.chromium.org/2306673002
Cr-Commit-Position: refs/heads/master@{#418665}
parent bd442c23
......@@ -10,15 +10,23 @@
#include "base/json/json_writer.h"
#include "base/strings/stringprintf.h"
#include "base/values.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/chromeos/arc/arc_auth_service.h"
#include "chrome/browser/chromeos/net/onc_utils.h"
#include "chrome/browser/chromeos/proxy_config_service_impl.h"
#include "chrome/browser/chromeos/settings/cros_settings.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/common/pref_names.h"
#include "chromeos/network/network_handler.h"
#include "chromeos/network/network_state.h"
#include "chromeos/network/network_state_handler.h"
#include "chromeos/network/network_state_handler_observer.h"
#include "chromeos/settings/cros_settings_names.h"
#include "chromeos/settings/timezone_settings.h"
#include "components/arc/intent_helper/font_size_util.h"
#include "components/prefs/pref_change_registrar.h"
#include "components/prefs/pref_service.h"
#include "components/proxy_config/pref_proxy_config_tracker_impl.h"
#include "components/proxy_config/proxy_config_dictionary.h"
#include "components/proxy_config/proxy_config_pref_names.h"
#include "device/bluetooth/bluetooth_adapter.h"
......@@ -30,11 +38,11 @@ using ::chromeos::system::TimezoneSettings;
namespace {
bool GetHttpProxyServer(const ProxyConfigDictionary& proxy_config_dict,
bool GetHttpProxyServer(const ProxyConfigDictionary* proxy_config_dict,
std::string* host,
int* port) {
std::string proxy_rules_string;
if (!proxy_config_dict.GetProxyServer(&proxy_rules_string))
if (!proxy_config_dict->GetProxyServer(&proxy_rules_string))
return false;
net::ProxyConfig::ProxyRules proxy_rules;
......@@ -56,6 +64,14 @@ bool GetHttpProxyServer(const ProxyConfigDictionary& proxy_config_dict,
return !host->empty() && *port;
}
// Returns whether kProxy pref proxy config is applied.
bool IsPrefProxyConfigApplied() {
net::ProxyConfig config;
Profile* profile = ProfileManager::GetActiveUserProfile();
return PrefProxyConfigTrackerImpl::PrefPrecedes(
PrefProxyConfigTrackerImpl::ReadPrefConfig(profile->GetPrefs(), &config));
}
} // namespace
namespace arc {
......@@ -65,7 +81,8 @@ namespace arc {
class ArcSettingsServiceImpl
: public chromeos::system::TimezoneSettings::Observer,
public device::BluetoothAdapter::Observer,
public ArcAuthService::Observer {
public ArcAuthService::Observer,
public chromeos::NetworkStateHandlerObserver {
public:
explicit ArcSettingsServiceImpl(ArcBridgeService* arc_bridge_service);
~ArcSettingsServiceImpl() override;
......@@ -84,6 +101,9 @@ class ArcSettingsServiceImpl
// ArcAuthService::Observer:
void OnInitialStart() override;
// NetworkStateHandlerObserver:
void DefaultNetworkChanged(const chromeos::NetworkState* network) override;
private:
// Registers to observe changes for Chrome settings we care about.
void StartObservingSettingsChanges();
......@@ -168,8 +188,10 @@ void ArcSettingsServiceImpl::StartObservingSettingsChanges() {
AddPrefToObserve(prefs::kWebKitMinimumFontSize);
AddPrefToObserve(prefs::kAccessibilitySpokenFeedbackEnabled);
AddPrefToObserve(prefs::kUse24HourClock);
AddPrefToObserve(proxy_config::prefs::kProxy);
AddPrefToObserve(prefs::kArcBackupRestoreEnabled);
AddPrefToObserve(proxy_config::prefs::kProxy);
AddPrefToObserve(prefs::kDeviceOpenNetworkConfiguration);
AddPrefToObserve(prefs::kOpenNetworkConfiguration);
reporting_consent_subscription_ = CrosSettings::Get()->AddSettingsObserver(
chromeos::kStatsReportingPref,
......@@ -183,6 +205,9 @@ void ArcSettingsServiceImpl::StartObservingSettingsChanges() {
base::Bind(&ArcSettingsServiceImpl::OnBluetoothAdapterInitialized,
weak_factory_.GetWeakPtr()));
}
chromeos::NetworkHandler::Get()->network_state_handler()->AddObserver(
this, FROM_HERE);
}
void ArcSettingsServiceImpl::OnBluetoothAdapterInitialized(
......@@ -225,6 +250,8 @@ void ArcSettingsServiceImpl::StopObservingSettingsChanges() {
reporting_consent_subscription_.reset();
TimezoneSettings::GetInstance()->RemoveObserver(this);
chromeos::NetworkHandler::Get()->network_state_handler()->RemoveObserver(
this, FROM_HERE);
}
void ArcSettingsServiceImpl::AddPrefToObserve(const std::string& pref_name) {
......@@ -252,6 +279,15 @@ void ArcSettingsServiceImpl::OnPrefChanged(const std::string& pref_name) const {
SyncUse24HourClock();
} else if (pref_name == proxy_config::prefs::kProxy) {
SyncProxySettings();
} else if (pref_name == prefs::kDeviceOpenNetworkConfiguration ||
pref_name == prefs::kOpenNetworkConfiguration) {
// Only update proxy settings if kProxy pref is not applied.
if (IsPrefProxyConfigApplied()) {
LOG(ERROR) << "Open Network Configuration proxy settings are not applied,"
<< " because kProxy preference is configured.";
return;
}
SyncProxySettings();
} else {
LOG(ERROR) << "Unknown pref changed.";
}
......@@ -349,16 +385,14 @@ void ArcSettingsServiceImpl::SyncUse24HourClock() const {
}
void ArcSettingsServiceImpl::SyncProxySettings() const {
const PrefService::Preference* const pref =
registrar_.prefs()->FindPreference(proxy_config::prefs::kProxy);
const base::DictionaryValue* proxy_config_value;
bool value_exists = pref->GetValue()->GetAsDictionary(&proxy_config_value);
DCHECK(value_exists);
ProxyConfigDictionary proxy_config_dict(proxy_config_value);
std::unique_ptr<ProxyConfigDictionary> proxy_config_dict =
chromeos::ProxyConfigServiceImpl::GetActiveProxyConfigDictionary(
ProfileManager::GetActiveUserProfile()->GetPrefs());
if (!proxy_config_dict)
return;
ProxyPrefs::ProxyMode mode;
if (!proxy_config_dict.GetMode(&mode))
if (!proxy_config_dict || !proxy_config_dict->GetMode(&mode))
mode = ProxyPrefs::MODE_DIRECT;
base::DictionaryValue extras;
......@@ -375,7 +409,7 @@ void ArcSettingsServiceImpl::SyncProxySettings() const {
break;
case ProxyPrefs::MODE_PAC_SCRIPT: {
std::string pac_url;
if (!proxy_config_dict.GetPacUrl(&pac_url)) {
if (!proxy_config_dict->GetPacUrl(&pac_url)) {
LOG(ERROR) << "No pac URL for pac_script proxy mode.";
return;
}
......@@ -385,7 +419,7 @@ void ArcSettingsServiceImpl::SyncProxySettings() const {
case ProxyPrefs::MODE_FIXED_SERVERS: {
std::string host;
int port = 0;
if (!GetHttpProxyServer(proxy_config_dict, &host, &port)) {
if (!GetHttpProxyServer(proxy_config_dict.get(), &host, &port)) {
LOG(ERROR) << "No Http proxy server is sent.";
return;
}
......@@ -393,7 +427,7 @@ void ArcSettingsServiceImpl::SyncProxySettings() const {
extras.SetInteger("port", port);
std::string bypass_list;
if (proxy_config_dict.GetBypassList(&bypass_list) &&
if (proxy_config_dict->GetBypassList(&bypass_list) &&
!bypass_list.empty()) {
extras.SetString("bypassList", bypass_list);
}
......@@ -438,6 +472,18 @@ void ArcSettingsServiceImpl::SendSettingsBroadcast(
}
}
void ArcSettingsServiceImpl::DefaultNetworkChanged(
const chromeos::NetworkState* network) {
// kProxy pref and ONC policy have more priority than the default network
// update.
Profile* profile = ProfileManager::GetActiveUserProfile();
if (!chromeos::onc::HasPolicyForNetwork(
profile->GetPrefs(), g_browser_process->local_state(), *network) &&
!IsPrefProxyConfigApplied()) {
SyncProxySettings();
}
}
ArcSettingsService::ArcSettingsService(ArcBridgeService* bridge_service)
: ArcService(bridge_service) {
arc_bridge_service()->intent_helper()->AddObserver(this);
......
......@@ -31,7 +31,7 @@ std::unique_ptr<ProxyConfigDictionary> GetProxyConfigForNetwork(
const PrefService* profile_prefs,
const PrefService* local_state_prefs,
const NetworkState& network,
onc::ONCSource* onc_source);
::onc::ONCSource* onc_source);
void SetProxyConfigForNetwork(const ProxyConfigDictionary& proxy_config,
const NetworkState& network);
......
......@@ -9,6 +9,7 @@
#include "base/callback.h"
#include "base/location.h"
#include "base/logging.h"
#include "base/memory/ptr_util.h"
#include "base/values.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/chromeos/net/proxy_config_handler.h"
......@@ -22,6 +23,7 @@
#include "components/prefs/pref_service.h"
#include "components/proxy_config/pref_proxy_config_tracker_impl.h"
#include "components/proxy_config/proxy_config_dictionary.h"
#include "components/proxy_config/proxy_config_pref_names.h"
#include "components/proxy_config/proxy_prefs.h"
#include "components/user_manager/user_manager.h"
#include "content/public/browser/browser_thread.h"
......@@ -173,6 +175,46 @@ bool ProxyConfigServiceImpl::IgnoreProxy(const PrefService* profile_prefs,
return !use_shared_proxies;
}
// static
std::unique_ptr<ProxyConfigDictionary>
ProxyConfigServiceImpl::GetActiveProxyConfigDictionary(
const PrefService* profile_prefs) {
// Apply Pref Proxy configuration if available.
net::ProxyConfig pref_proxy_config;
ProxyPrefs::ConfigState pref_state =
PrefProxyConfigTrackerImpl::ReadPrefConfig(profile_prefs,
&pref_proxy_config);
if (PrefProxyConfigTrackerImpl::PrefPrecedes(pref_state)) {
const PrefService::Preference* const pref =
profile_prefs->FindPreference(::proxy_config::prefs::kProxy);
const base::DictionaryValue* proxy_config_value;
bool value_exists = pref->GetValue()->GetAsDictionary(&proxy_config_value);
DCHECK(value_exists);
return base::MakeUnique<ProxyConfigDictionary>(proxy_config_value);
}
const chromeos::NetworkState* network = chromeos::NetworkHandler::Get()
->network_state_handler()
->DefaultNetwork();
// No connected network.
if (!network)
return nullptr;
// Apply network proxy configuration.
::onc::ONCSource onc_source;
std::unique_ptr<ProxyConfigDictionary> proxy_config =
chromeos::proxy_config::GetProxyConfigForNetwork(
profile_prefs, g_browser_process->local_state(), *network,
&onc_source);
if (!chromeos::ProxyConfigServiceImpl::IgnoreProxy(
profile_prefs, network->profile_path(), onc_source))
return proxy_config;
return base::MakeUnique<ProxyConfigDictionary>(
ProxyConfigDictionary::CreateDirect());
}
void ProxyConfigServiceImpl::DetermineEffectiveConfigFromDefaultNetwork() {
if (!NetworkHandler::IsInitialized())
return;
......
......@@ -54,15 +54,19 @@ class ProxyConfigServiceImpl : public PrefProxyConfigTrackerImpl,
void DefaultNetworkChanged(const NetworkState* network) override;
void OnShuttingDown() override;
protected:
friend class UIProxyConfigService;
// Returns true if proxy is to be ignored for this network profile and
// |onc_source|, e.g. this happens if the network is shared and
// use-shared-proxies is turned off. |profile_prefs| may be NULL.
static bool IgnoreProxy(const PrefService* profile_prefs,
const std::string network_profile_path,
onc::ONCSource onc_source);
::onc::ONCSource onc_source);
// Returns Pref Proxy configuration if available or a proxy config dictionary
// applied to the default network.
// Returns NULL if no Pref Proxy configuration and no active network.
// |profile_prefs| must be not NULL.
static std::unique_ptr<ProxyConfigDictionary> GetActiveProxyConfigDictionary(
const PrefService* profile_prefs);
private:
// Called when any proxy preference changes.
......
......@@ -2116,6 +2116,7 @@ test("browser_tests") {
"../browser/chromeos/app_mode/kiosk_app_update_service_browsertest.cc",
"../browser/chromeos/app_mode/kiosk_crash_restore_browsertest.cc",
"../browser/chromeos/arc/arc_auth_service_browsertest.cc",
"../browser/chromeos/arc/arc_settings_service_browsertest.cc",
"../browser/chromeos/attestation/attestation_policy_browsertest.cc",
"../browser/chromeos/customization/customization_document_browsertest.cc",
"../browser/chromeos/customization/customization_wallpaper_downloader_browsertest.cc",
......
......@@ -185,6 +185,8 @@ static_library("arc_test_support") {
"test/fake_arc_bridge_service.h",
"test/fake_bluetooth_instance.cc",
"test/fake_bluetooth_instance.h",
"test/fake_intent_helper_instance.cc",
"test/fake_intent_helper_instance.h",
"test/fake_notifications_instance.cc",
"test/fake_notifications_instance.h",
"test/fake_policy_instance.cc",
......
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/arc/test/fake_intent_helper_instance.h"
namespace arc {
FakeIntentHelperInstance::FakeIntentHelperInstance() {}
FakeIntentHelperInstance::Broadcast::Broadcast(const mojo::String& action,
const mojo::String& package_name,
const mojo::String& cls,
const mojo::String& extras)
: action(action), package_name(package_name), cls(cls), extras(extras) {}
FakeIntentHelperInstance::Broadcast::Broadcast(const Broadcast& broadcast)
: action(broadcast.action),
package_name(broadcast.package_name),
cls(broadcast.cls),
extras(broadcast.extras) {}
FakeIntentHelperInstance::Broadcast::~Broadcast() {}
FakeIntentHelperInstance::~FakeIntentHelperInstance() {}
void FakeIntentHelperInstance::AddPreferredPackage(
const mojo::String& package_name) {}
void FakeIntentHelperInstance::HandleUrl(const mojo::String& url,
const mojo::String& package_name) {}
void FakeIntentHelperInstance::HandleUrlList(
mojo::Array<mojom::UrlWithMimeTypePtr> urls,
mojom::ActivityNamePtr activity,
mojom::ActionType action) {}
void FakeIntentHelperInstance::HandleUrlListDeprecated(
mojo::Array<mojom::UrlWithMimeTypePtr> urls,
const mojo::String& package_name,
mojom::ActionType action) {}
void FakeIntentHelperInstance::Init(mojom::IntentHelperHostPtr host_ptr) {}
void FakeIntentHelperInstance::RequestActivityIcons(
mojo::Array<mojom::ActivityNamePtr> activities,
::arc::mojom::ScaleFactor scale_factor,
const RequestActivityIconsCallback& callback) {}
void FakeIntentHelperInstance::RequestUrlHandlerList(
const mojo::String& url,
const RequestUrlHandlerListCallback& callback) {}
void FakeIntentHelperInstance::RequestUrlListHandlerList(
mojo::Array<mojom::UrlWithMimeTypePtr> urls,
const RequestUrlListHandlerListCallback& callback) {}
void FakeIntentHelperInstance::SendBroadcast(const mojo::String& action,
const mojo::String& package_name,
const mojo::String& cls,
const mojo::String& extras) {
broadcasts_.emplace_back(action, package_name, cls, extras);
}
} // namespace arc
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef COMPONENTS_ARC_TEST_FAKE_INTENT_HELPER_INSTANCE_H_
#define COMPONENTS_ARC_TEST_FAKE_INTENT_HELPER_INSTANCE_H_
#include <string>
#include <vector>
#include "base/callback.h"
#include "components/arc/common/intent_helper.mojom.h"
#include "components/arc/test/fake_arc_bridge_instance.h"
#include "mojo/public/cpp/bindings/binding.h"
namespace arc {
class FakeIntentHelperInstance : public mojom::IntentHelperInstance {
public:
FakeIntentHelperInstance();
class Broadcast {
public:
Broadcast(const mojo::String& action,
const mojo::String& package_name,
const mojo::String& cls,
const mojo::String& extras);
~Broadcast();
Broadcast(const Broadcast& broadcast);
std::string action;
std::string package_name;
std::string cls;
std::string extras;
};
void clear_broadcasts() { broadcasts_.clear(); }
const std::vector<Broadcast>& broadcasts() const { return broadcasts_; }
// mojom::HelpIntentInstance:
~FakeIntentHelperInstance() override;
void AddPreferredPackage(const mojo::String& package_name) override;
void HandleUrl(const mojo::String& url,
const mojo::String& package_name) override;
void HandleUrlList(mojo::Array<mojom::UrlWithMimeTypePtr> urls,
mojom::ActivityNamePtr activity,
mojom::ActionType action) override;
void HandleUrlListDeprecated(mojo::Array<mojom::UrlWithMimeTypePtr> urls,
const mojo::String& package_name,
mojom::ActionType action) override;
void Init(mojom::IntentHelperHostPtr host_ptr) override;
void RequestActivityIcons(
mojo::Array<mojom::ActivityNamePtr> activities,
::arc::mojom::ScaleFactor scale_factor,
const RequestActivityIconsCallback& callback) override;
void RequestUrlHandlerList(
const mojo::String& url,
const RequestUrlHandlerListCallback& callback) override;
void RequestUrlListHandlerList(
mojo::Array<mojom::UrlWithMimeTypePtr> urls,
const RequestUrlListHandlerListCallback& callback) override;
void SendBroadcast(const mojo::String& action,
const mojo::String& package_name,
const mojo::String& cls,
const mojo::String& extras) override;
private:
std::vector<Broadcast> broadcasts_;
DISALLOW_COPY_AND_ASSIGN(FakeIntentHelperInstance);
};
} // namespace arc
#endif // COMPONENTS_ARC_TEST_FAKE_POLICY_INSTANCE_H_
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment