[Autofill]: Create a policy flag for AutofillProfileEnabled.

AutofillProfileEnabled was added as a preference setting in 1127344.
This CL creates a policy flag for AutofillProfileEnabled that can
overwrite the user preference.

Bug: 860526
Change-Id: I5d4848c26a53cf163fc4557d9b2da5f597bd98cf
Reviewed-on: https://chromium-review.googlesource.com/1127463
Reviewed-by: Lutz Justen <ljusten@chromium.org>
Reviewed-by: Drew Wilson <atwilson@chromium.org>
Reviewed-by: Sebastien Seguin-Gagnon <sebsg@chromium.org>
Commit-Queue: Etienne Pierre-Doray <etiennep@chromium.org>
Cr-Commit-Position: refs/heads/master@{#574601}
diff --git a/chrome/browser/policy/configuration_policy_handler_list_factory.cc b/chrome/browser/policy/configuration_policy_handler_list_factory.cc
index 17afce8..365276d 100644
--- a/chrome/browser/policy/configuration_policy_handler_list_factory.cc
+++ b/chrome/browser/policy/configuration_policy_handler_list_factory.cc
@@ -32,6 +32,7 @@
 #include "chrome/common/pref_names.h"
 #include "components/autofill/core/browser/autofill_credit_card_policy_handler.h"
 #include "components/autofill/core/browser/autofill_policy_handler.h"
+#include "components/autofill/core/browser/autofill_profile_policy_handler.h"
 #include "components/bookmarks/common/bookmark_pref_names.h"
 #include "components/browsing_data/core/pref_names.h"
 #include "components/certificate_transparency/pref_names.h"
@@ -990,6 +991,8 @@
   }
 
   handlers->AddHandler(
+      std::make_unique<autofill::AutofillProfilePolicyHandler>());
+  handlers->AddHandler(
       std::make_unique<autofill::AutofillCreditCardPolicyHandler>());
   handlers->AddHandler(std::make_unique<autofill::AutofillPolicyHandler>());
   handlers->AddHandler(std::make_unique<DefaultSearchPolicyHandler>());
diff --git a/chrome/test/data/policy/policy_test_cases.json b/chrome/test/data/policy/policy_test_cases.json
index 40a079fb..e5705a2d 100644
--- a/chrome/test/data/policy/policy_test_cases.json
+++ b/chrome/test/data/policy/policy_test_cases.json
@@ -457,6 +457,19 @@
     ]
   },
 
+  "AutofillProfileEnabled": {
+    "os": ["win", "linux", "mac", "chromeos"],
+    "can_be_recommended": true,
+    "test_policy": { "AutofillProfileEnabled": false },
+    "pref_mappings": [
+      { "pref": "autofill.profile_enabled",
+        "indicator_tests": [
+          { "policy": { "AutofillProfileEnabled": false } }
+        ]
+      }
+    ]
+  },
+
   "AutofillCreditCardEnabled": {
     "os": ["win", "linux", "mac", "chromeos"],
     "can_be_recommended": true,
diff --git a/components/autofill/core/browser/BUILD.gn b/components/autofill/core/browser/BUILD.gn
index 07007b4..e5c378ac 100644
--- a/components/autofill/core/browser/BUILD.gn
+++ b/components/autofill/core/browser/BUILD.gn
@@ -225,6 +225,8 @@
       "autofill_credit_card_policy_handler.h",
       "autofill_policy_handler.cc",
       "autofill_policy_handler.h",
+      "autofill_profile_policy_handler.cc",
+      "autofill_profile_policy_handler.h",
     ]
   }
 
@@ -463,6 +465,7 @@
     sources += [
       "autofill_credit_card_policy_handler_unittest.cc",
       "autofill_policy_handler_unittest.cc",
+      "autofill_profile_policy_handler_unittest.cc",
     ]
   }
 
diff --git a/components/autofill/core/browser/autofill_profile_policy_handler.cc b/components/autofill/core/browser/autofill_profile_policy_handler.cc
new file mode 100644
index 0000000..295bfcb
--- /dev/null
+++ b/components/autofill/core/browser/autofill_profile_policy_handler.cc
@@ -0,0 +1,32 @@
+// Copyright 2018 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/autofill/core/browser/autofill_profile_policy_handler.h"
+
+#include "base/values.h"
+#include "components/autofill/core/common/autofill_pref_names.h"
+#include "components/policy/core/common/policy_map.h"
+#include "components/policy/policy_constants.h"
+#include "components/prefs/pref_value_map.h"
+
+namespace autofill {
+
+AutofillProfilePolicyHandler::AutofillProfilePolicyHandler()
+    : TypeCheckingPolicyHandler(policy::key::kAutofillProfileEnabled,
+                                base::Value::Type::BOOLEAN) {}
+
+AutofillProfilePolicyHandler::~AutofillProfilePolicyHandler() {}
+
+void AutofillProfilePolicyHandler::ApplyPolicySettings(
+    const policy::PolicyMap& policies,
+    PrefValueMap* prefs) {
+  const base::Value* value = policies.GetValue(policy_name());
+  bool autofill_profile_enabled;
+  if (value && value->GetAsBoolean(&autofill_profile_enabled) &&
+      !autofill_profile_enabled) {
+    prefs->SetBoolean(autofill::prefs::kAutofillProfileEnabled, false);
+  }
+}
+
+}  // namespace autofill
diff --git a/components/autofill/core/browser/autofill_profile_policy_handler.h b/components/autofill/core/browser/autofill_profile_policy_handler.h
new file mode 100644
index 0000000..7784b92
--- /dev/null
+++ b/components/autofill/core/browser/autofill_profile_policy_handler.h
@@ -0,0 +1,30 @@
+// Copyright 2018 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_AUTOFILL_CORE_BROWSER_AUTOFILL_PROFILE_POLICY_HANDLER_H_
+#define COMPONENTS_AUTOFILL_CORE_BROWSER_AUTOFILL_PROFILE_POLICY_HANDLER_H_
+
+#include "base/macros.h"
+#include "components/policy/core/browser/configuration_policy_handler.h"
+#include "components/policy/policy_export.h"
+
+namespace autofill {
+
+// ConfigurationPolicyHandler for the AutofillPolicyEnabled policy.
+class AutofillProfilePolicyHandler : public policy::TypeCheckingPolicyHandler {
+ public:
+  AutofillProfilePolicyHandler();
+  ~AutofillProfilePolicyHandler() override;
+
+  // ConfigurationPolicyHandler methods:
+  void ApplyPolicySettings(const policy::PolicyMap& policies,
+                           PrefValueMap* prefs) override;
+
+ private:
+  DISALLOW_COPY_AND_ASSIGN(AutofillProfilePolicyHandler);
+};
+
+}  // namespace autofill
+
+#endif  // COMPONENTS_AUTOFILL_CORE_BROWSER_AUTOFILL_PROFILE_POLICY_HANDLER_H_
diff --git a/components/autofill/core/browser/autofill_profile_policy_handler_unittest.cc b/components/autofill/core/browser/autofill_profile_policy_handler_unittest.cc
new file mode 100644
index 0000000..b6d14fa
--- /dev/null
+++ b/components/autofill/core/browser/autofill_profile_policy_handler_unittest.cc
@@ -0,0 +1,66 @@
+// Copyright 2018 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/autofill/core/browser/autofill_profile_policy_handler.h"
+
+#include <memory>
+
+#include "base/values.h"
+#include "components/autofill/core/common/autofill_pref_names.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 "testing/gtest/include/gtest/gtest.h"
+
+namespace autofill {
+
+// Test cases for the Autofill profiles policy setting.
+class AutofillProfilePolicyHandlerTest : public testing::Test {};
+
+TEST_F(AutofillProfilePolicyHandlerTest, Default) {
+  policy::PolicyMap policy;
+  PrefValueMap prefs;
+  AutofillProfilePolicyHandler handler;
+  handler.ApplyPolicySettings(policy, &prefs);
+  EXPECT_FALSE(
+      prefs.GetValue(autofill::prefs::kAutofillProfileEnabled, nullptr));
+}
+
+TEST_F(AutofillProfilePolicyHandlerTest, Enabled) {
+  policy::PolicyMap policy;
+  policy.Set(policy::key::kAutofillProfileEnabled,
+             policy::POLICY_LEVEL_MANDATORY, policy::POLICY_SCOPE_USER,
+             policy::POLICY_SOURCE_CLOUD, std::make_unique<base::Value>(true),
+             nullptr);
+  PrefValueMap prefs;
+  AutofillProfilePolicyHandler handler;
+  handler.ApplyPolicySettings(policy, &prefs);
+
+  // Enabling Autofill for profiles should not set the prefs.
+  EXPECT_FALSE(
+      prefs.GetValue(autofill::prefs::kAutofillProfileEnabled, nullptr));
+}
+
+TEST_F(AutofillProfilePolicyHandlerTest, Disabled) {
+  policy::PolicyMap policy;
+  policy.Set(policy::key::kAutofillProfileEnabled,
+             policy::POLICY_LEVEL_MANDATORY, policy::POLICY_SCOPE_USER,
+             policy::POLICY_SOURCE_CLOUD, std::make_unique<base::Value>(false),
+             nullptr);
+  PrefValueMap prefs;
+  AutofillProfilePolicyHandler handler;
+  handler.ApplyPolicySettings(policy, &prefs);
+
+  // Disabling Autofill for profiles should switch the prefs to managed.
+  const base::Value* value = nullptr;
+  EXPECT_TRUE(prefs.GetValue(autofill::prefs::kAutofillProfileEnabled, &value));
+  ASSERT_TRUE(value);
+  bool autofill_profile_enabled = true;
+  bool result = value->GetAsBoolean(&autofill_profile_enabled);
+  ASSERT_TRUE(result);
+  EXPECT_FALSE(autofill_profile_enabled);
+}
+
+}  // namespace autofill
diff --git a/components/policy/resources/policy_templates.json b/components/policy/resources/policy_templates.json
index 0cbfea89..35d8858 100644
--- a/components/policy/resources/policy_templates.json
+++ b/components/policy/resources/policy_templates.json
@@ -1768,6 +1768,30 @@
       If you enable this setting or do not set a value, AutoFill will remain under the control of the user. This will allow them to configure AutoFill profiles and to switch AutoFill on or off at their own discretion.''',
     },
     {
+      'name': 'AutofillProfileEnabled',
+      'type': 'main',
+      'schema': { 'type': 'boolean' },
+      'supported_on': [
+        'chrome.*:69-',
+        'chrome_os:69-',
+        'android:69-',
+      ],
+      'features': {
+        'can_be_recommended': True,
+        'dynamic_refresh': True,
+        'per_profile': True,
+      },
+      'example_value': False,
+      'id': 459,
+      'caption': '''Enable AutoFill for profiles''',
+      'tags': [],
+      'desc': '''Enables <ph name="PRODUCT_NAME">$1<ex>Google Chrome</ex></ph>'s AutoFill feature and allows users to auto complete profile and address information in web forms using previously stored information.
+
+      If this setting is disabled, Autofill will never suggest, or fill address information, nor will it save additional address information that the user might submit while browsing the web.
+
+      If this setting is enabled or has no value, the user will be able to control Autofill for addresses in the UI.''',
+    },
+    {
       'name': 'AutofillCreditCardEnabled',
       'type': 'main',
       'schema': { 'type': 'boolean' },
@@ -1785,11 +1809,11 @@
       'id': 392,
       'caption': '''Enable AutoFill for credit cards''',
       'tags': [],
-      'desc': '''Enables <ph name="PRODUCT_NAME">$1<ex>Google Chrome</ex></ph>'s AutoFill feature and allows users to auto complete credit card and address information in web forms using previously stored information.
+      'desc': '''Enables <ph name="PRODUCT_NAME">$1<ex>Google Chrome</ex></ph>'s AutoFill feature and allows users to auto complete credit card information in web forms using previously stored information.
 
-      If you disable this setting, Autofill will never suggest, or fill credit card information, nor will it save additional credit card information that the user might submit while browsing the web.
+      If this setting is disabled, Autofill will never suggest, or fill credit card information, nor will it save additional credit card information that the user might submit while browsing the web.
 
-      If you enable this setting or do not set a value, then user will be able to control the overall autofill feature (incuding credit cards) in the UI.''',
+      If this setting is enabled or has no value, the user will be able to control Autofill for credit cards in the UI.''',
     },
     {
       'name': 'DisabledPlugins',
@@ -12615,5 +12639,5 @@
   },
   'placeholders': [],
   'deleted_policy_ids': [412],
-  'highest_id_currently_used': 458
+  'highest_id_currently_used': 459
 }
diff --git a/tools/metrics/histograms/enums.xml b/tools/metrics/histograms/enums.xml
index c770f9e..7301ae5 100644
--- a/tools/metrics/histograms/enums.xml
+++ b/tools/metrics/histograms/enums.xml
@@ -13795,6 +13795,7 @@
   <int value="456" label="AllowedInputMethods"/>
   <int value="457" label="OverrideSecurityRestrictionsOnInsecureOrigin"/>
   <int value="458" label="DeviceUpdateStagingPercentOfFleetPerWeek"/>
+  <int value="459" label="AutofillProfileEnabled"/>
 </enum>
 
 <enum name="EnterprisePolicyInvalidations">