Commit f281a6c8 authored by pastarmovj's avatar pastarmovj Committed by Commit Bot

Add a path parsing policy handler for the RoamingProfileLocation policy.

This policy should like most other FilePath policies parse variables
in its content before storing its value in its backing pref.

BUG=726007
TEST=unit_tests

Review-Url: https://codereview.chromium.org/2912353003
Cr-Commit-Position: refs/heads/master@{#476272}
parent ff30a0e4
...@@ -3520,6 +3520,8 @@ split_static_library("browser") { ...@@ -3520,6 +3520,8 @@ split_static_library("browser") {
"obsolete_system/obsolete_system_win.cc", "obsolete_system/obsolete_system_win.cc",
"pdf/pdf_extension_util.cc", "pdf/pdf_extension_util.cc",
"pdf/pdf_extension_util.h", "pdf/pdf_extension_util.h",
"policy/local_sync_policy_handler.cc",
"policy/local_sync_policy_handler.h",
"process_singleton_modal_dialog_lock.cc", "process_singleton_modal_dialog_lock.cc",
"process_singleton_modal_dialog_lock.h", "process_singleton_modal_dialog_lock.h",
"process_singleton_posix.cc", "process_singleton_posix.cc",
......
...@@ -78,6 +78,7 @@ ...@@ -78,6 +78,7 @@
#if !defined(OS_ANDROID) #if !defined(OS_ANDROID)
#include "chrome/browser/download/download_dir_policy_handler.h" #include "chrome/browser/download/download_dir_policy_handler.h"
#include "chrome/browser/policy/local_sync_policy_handler.h"
#endif #endif
#if BUILDFLAG(ENABLE_EXTENSIONS) #if BUILDFLAG(ENABLE_EXTENSIONS)
...@@ -669,9 +670,6 @@ const PolicyToPreferenceMapEntry kSimplePolicyMap[] = { ...@@ -669,9 +670,6 @@ const PolicyToPreferenceMapEntry kSimplePolicyMap[] = {
{ key::kRoamingProfileSupportEnabled, { key::kRoamingProfileSupportEnabled,
syncer::prefs::kEnableLocalSyncBackend, syncer::prefs::kEnableLocalSyncBackend,
base::Value::Type::BOOLEAN }, base::Value::Type::BOOLEAN },
{ key::kRoamingProfileLocation,
syncer::prefs::kLocalSyncBackendDir,
base::Value::Type::STRING },
{ key::kNetworkTimeQueriesEnabled, { key::kNetworkTimeQueriesEnabled,
network_time::prefs::kNetworkTimeQueriesEnabled, network_time::prefs::kNetworkTimeQueriesEnabled,
...@@ -922,6 +920,7 @@ std::unique_ptr<ConfigurationPolicyHandlerList> BuildHandlerList( ...@@ -922,6 +920,7 @@ std::unique_ptr<ConfigurationPolicyHandlerList> BuildHandlerList(
#if !defined(OS_ANDROID) #if !defined(OS_ANDROID)
handlers->AddHandler(base::WrapUnique(new DownloadDirPolicyHandler)); handlers->AddHandler(base::WrapUnique(new DownloadDirPolicyHandler));
handlers->AddHandler(base::MakeUnique<LocalSyncPolicyHandler>());
handlers->AddHandler(base::MakeUnique<SimpleSchemaValidatingPolicyHandler>( handlers->AddHandler(base::MakeUnique<SimpleSchemaValidatingPolicyHandler>(
key::kRegisteredProtocolHandlers, key::kRegisteredProtocolHandlers,
......
// Copyright 2017 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 "chrome/browser/policy/local_sync_policy_handler.h"
#include "base/files/file_path.h"
#include "base/memory/ptr_util.h"
#include "base/values.h"
#include "chrome/browser/policy/policy_path_parser.h"
#include "components/policy/core/common/policy_map.h"
#include "components/policy/policy_constants.h"
#include "components/prefs/pref_value_map.h"
#include "components/sync/base/pref_names.h"
namespace policy {
LocalSyncPolicyHandler::LocalSyncPolicyHandler()
: TypeCheckingPolicyHandler(key::kRoamingProfileLocation,
base::Value::Type::STRING) {}
LocalSyncPolicyHandler::~LocalSyncPolicyHandler() {}
void LocalSyncPolicyHandler::ApplyPolicySettings(const PolicyMap& policies,
PrefValueMap* prefs) {
const base::Value* value = policies.GetValue(policy_name());
base::FilePath::StringType string_value;
if (value && value->GetAsString(&string_value)) {
base::FilePath::StringType expanded_value =
policy::path_parser::ExpandPathVariables(string_value);
prefs->SetValue(syncer::prefs::kLocalSyncBackendDir,
base::MakeUnique<base::Value>(expanded_value));
}
}
} // namespace policy
// Copyright 2017 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 CHROME_BROWSER_POLICY_LOCAL_SYNC_POLICY_HANDLER_H_
#define CHROME_BROWSER_POLICY_LOCAL_SYNC_POLICY_HANDLER_H_
#include "base/compiler_specific.h"
#include "base/macros.h"
#include "components/policy/core/browser/configuration_policy_handler.h"
namespace policy {
// ConfigurationPolicyHandler for the RoamingProfileLocation policy.
class LocalSyncPolicyHandler : public TypeCheckingPolicyHandler {
public:
LocalSyncPolicyHandler();
~LocalSyncPolicyHandler() override;
// ConfigurationPolicyHandler methods:
void ApplyPolicySettings(const PolicyMap& policies,
PrefValueMap* prefs) override;
private:
DISALLOW_COPY_AND_ASSIGN(LocalSyncPolicyHandler);
};
} // namespace policy
#endif // CHROME_BROWSER_POLICY_LOCAL_SYNC_POLICY_HANDLER_H_
// Copyright 2017 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 <string>
#include "base/memory/ptr_util.h"
#include "base/values.h"
#include "chrome/browser/policy/local_sync_policy_handler.h"
#include "components/policy/core/common/policy_map.h"
#include "components/policy/core/common/policy_types.h"
#include "components/policy/policy_constants.h"
#include "components/prefs/pref_value_map.h"
#include "components/sync/base/pref_names.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace policy {
class LocalSyncPolicyTest : public testing::Test {
protected:
PolicyMap policy_;
LocalSyncPolicyHandler handler_;
PrefValueMap prefs_;
};
TEST_F(LocalSyncPolicyTest, Default) {
handler_.ApplyPolicySettings(policy_, &prefs_);
EXPECT_FALSE(prefs_.GetValue(syncer::prefs::kLocalSyncBackendDir, nullptr));
}
// RoamingProfileLocation policy expects a string; give it a boolean.
TEST_F(LocalSyncPolicyTest, SetPolicyInvalid) {
policy_.Set(key::kRoamingProfileLocation, POLICY_LEVEL_MANDATORY,
POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD,
base::MakeUnique<base::Value>(false), nullptr);
handler_.ApplyPolicySettings(policy_, &prefs_);
EXPECT_FALSE(prefs_.GetValue(syncer::prefs::kLocalSyncBackendDir, nullptr));
}
// Use a variable in the value. It should be expanded by the handler.
TEST_F(LocalSyncPolicyTest, SetPolicyValid) {
const std::string in = "${user_name}/foo";
policy_.Set(key::kRoamingProfileLocation, POLICY_LEVEL_MANDATORY,
POLICY_SCOPE_USER, POLICY_SOURCE_CLOUD,
base::MakeUnique<base::Value>(in), nullptr);
handler_.ApplyPolicySettings(policy_, &prefs_);
const base::Value* value;
ASSERT_TRUE(prefs_.GetValue(syncer::prefs::kLocalSyncBackendDir, &value));
std::string out;
ASSERT_TRUE(value->GetAsString(&out));
EXPECT_NE(std::string::npos, out.find("foo"));
EXPECT_EQ(std::string::npos, out.find("${user_name}"));
}
} // namespace policy
...@@ -3724,6 +3724,7 @@ test("unit_tests") { ...@@ -3724,6 +3724,7 @@ test("unit_tests") {
"../browser/media/router/mojo/media_route_controller_unittest.cc", "../browser/media/router/mojo/media_route_controller_unittest.cc",
"../browser/media/router/mojo/media_router_mojo_impl_unittest.cc", "../browser/media/router/mojo/media_router_mojo_impl_unittest.cc",
"../browser/media/router/mojo/media_router_mojo_metrics_unittest.cc", "../browser/media/router/mojo/media_router_mojo_metrics_unittest.cc",
"../browser/policy/local_sync_policy_handler_unittest.cc",
"../browser/renderer_context_menu/render_view_context_menu_test_util.cc", "../browser/renderer_context_menu/render_view_context_menu_test_util.cc",
"../browser/renderer_context_menu/render_view_context_menu_test_util.h", "../browser/renderer_context_menu/render_view_context_menu_test_util.h",
"../browser/ui/autofill/save_card_bubble_controller_impl_unittest.cc", "../browser/ui/autofill/save_card_bubble_controller_impl_unittest.cc",
......
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