From a81f502084b97321d434a4efba996747559158a8 Mon Sep 17 00:00:00 2001 From: limasdf Date: Wed, 1 Apr 2015 20:11:37 -0700 Subject: [PATCH] Policy UI shows extension policy when extension is installed PolicyUIHandler gets callback when schema registry is updated, and then update policy names. BUG=471561 R=dbeam@chromium.org TEST=browser_test --gtest_filter=PolicyUITest.ExtensionLoadAndSendPolicy Review URL: https://codereview.chromium.org/1046663002 Cr-Commit-Position: refs/heads/master@{#323416} --- chrome/browser/ui/webui/policy_ui.cc | 27 ++++++++++- chrome/browser/ui/webui/policy_ui_browsertest.cc | 62 +++++++++++++++++++++++- 2 files changed, 87 insertions(+), 2 deletions(-) diff --git a/chrome/browser/ui/webui/policy_ui.cc b/chrome/browser/ui/webui/policy_ui.cc index 904724a85439..4077d2d327e0 100644 --- a/chrome/browser/ui/webui/policy_ui.cc +++ b/chrome/browser/ui/webui/policy_ui.cc @@ -342,7 +342,8 @@ class DeviceLocalAccountPolicyStatusProvider // The JavaScript message handler for the chrome://policy page. class PolicyUIHandler : public content::NotificationObserver, public content::WebUIMessageHandler, - public policy::PolicyService::Observer { + public policy::PolicyService::Observer, + public policy::SchemaRegistry::Observer { public: PolicyUIHandler(); ~PolicyUIHandler() override; @@ -360,6 +361,10 @@ class PolicyUIHandler : public content::NotificationObserver, const policy::PolicyMap& previous, const policy::PolicyMap& current) override; + // policy::SchemaRegistry::Observer implementation. + void OnSchemaRegistryReady() override; + void OnSchemaRegistryUpdated(bool has_new_schemas) override; + private: // Send a dictionary containing the names of all known policies to the UI. void SendPolicyNames() const; @@ -528,6 +533,10 @@ PolicyUIHandler::PolicyUIHandler() PolicyUIHandler::~PolicyUIHandler() { GetPolicyService()->RemoveObserver(policy::POLICY_DOMAIN_CHROME, this); GetPolicyService()->RemoveObserver(policy::POLICY_DOMAIN_EXTENSIONS, this); + policy::SchemaRegistry* registry = + policy::SchemaRegistryServiceFactory::GetForContext( + Profile::FromWebUI(web_ui())->GetOriginalProfile())->registry(); + registry->RemoveObserver(this); } void PolicyUIHandler::RegisterMessages() { @@ -586,6 +595,10 @@ void PolicyUIHandler::RegisterMessages() { extensions::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED, content::NotificationService::AllSources()); #endif + policy::SchemaRegistry* registry = + policy::SchemaRegistryServiceFactory::GetForContext( + Profile::FromWebUI(web_ui())->GetOriginalProfile())->registry(); + registry->AddObserver(this); web_ui()->RegisterMessageCallback( "initialized", @@ -607,6 +620,18 @@ void PolicyUIHandler::Observe(int type, #endif } +// TODO(limasdf): Add default implementation and remove this override. +void PolicyUIHandler::OnSchemaRegistryReady() { +} + +void PolicyUIHandler::OnSchemaRegistryUpdated(bool has_new_schemas) { + // Update UI when new schema is added. + if (has_new_schemas) { + SendPolicyNames(); + SendPolicyValues(); + } +} + void PolicyUIHandler::OnPolicyUpdated(const policy::PolicyNamespace& ns, const policy::PolicyMap& previous, const policy::PolicyMap& current) { diff --git a/chrome/browser/ui/webui/policy_ui_browsertest.cc b/chrome/browser/ui/webui/policy_ui_browsertest.cc index 10aa48db8523..bb0b94d3e8d8 100644 --- a/chrome/browser/ui/webui/policy_ui_browsertest.cc +++ b/chrome/browser/ui/webui/policy_ui_browsertest.cc @@ -5,11 +5,16 @@ #include #include "base/callback.h" +#include "base/files/scoped_temp_dir.h" #include "base/json/json_reader.h" #include "base/run_loop.h" #include "base/values.h" +#include "chrome/browser/extensions/extension_service.h" +#include "chrome/browser/extensions/test_extension_system.h" +#include "chrome/browser/profiles/profile.h" #include "chrome/browser/ui/browser.h" #include "chrome/browser/ui/tabs/tab_strip_model.h" +#include "chrome/common/url_constants.h" #include "chrome/test/base/in_process_browser_test.h" #include "chrome/test/base/ui_test_utils.h" #include "components/policy/core/browser/browser_policy_connector.h" @@ -20,6 +25,7 @@ #include "components/policy/core/common/schema.h" #include "content/public/browser/web_contents.h" #include "content/public/test/browser_test_utils.h" +#include "extensions/common/extension_builder.h" #include "grit/components_strings.h" #include "policy/policy_constants.h" #include "testing/gmock/include/gmock/gmock.h" @@ -92,9 +98,10 @@ class PolicyUITest : public InProcessBrowserTest { void VerifyPolicies(const std::vector >& expected); - private: + protected: policy::MockConfigurationPolicyProvider provider_; + private: DISALLOW_COPY_AND_ASSIGN(PolicyUITest); }; @@ -267,3 +274,56 @@ IN_PROC_BROWSER_TEST_F(PolicyUITest, SendPolicyValues) { // matches the expectation. VerifyPolicies(expected_policies); } + +IN_PROC_BROWSER_TEST_F(PolicyUITest, ExtensionLoadAndSendPolicy) { + ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUIPolicyURL)); + base::ScopedTempDir temp_dir_; + ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); + + const std::string newly_added_policy_name = "new_policy"; + std::string json_data = "{\"type\": \"object\",\"properties\": {\"" + + newly_added_policy_name + + "\": { \"type\": \"string\"}}}"; + + const std::string schema_file = "schema.json"; + base::FilePath schema_path = temp_dir_.path().AppendASCII(schema_file); + base::WriteFile(schema_path, json_data.data(), json_data.size()); + + // Build extension that contains the policy schema. + extensions::DictionaryBuilder storage; + storage.Set("managed_schema", schema_file); + + extensions::DictionaryBuilder manifest; + manifest.Set("name", "test") + .Set("version", "1") + .Set("manifest_version", 2) + .Set("storage", storage); + + extensions::ExtensionBuilder builder; + builder.SetPath(temp_dir_.path()); + builder.SetManifest(manifest); + + // Install extension. + ExtensionService* service = extensions::ExtensionSystem::Get( + browser()->profile())->extension_service(); + EXPECT_CALL(provider_, RefreshPolicies()); + service->OnExtensionInstalled(builder.Build().get(), syncer::StringOrdinal(), + 0); + + std::vector> expected_policies; + policy::Schema chrome_schema = + policy::Schema::Wrap(policy::GetChromeSchemaData()); + ASSERT_TRUE(chrome_schema.valid()); + + for (policy::Schema::Iterator it = chrome_schema.GetPropertiesIterator(); + !it.IsAtEnd(); it.Advance()) { + expected_policies.push_back( + PopulateExpectedPolicy(it.key(), std::string(), NULL, false)); + } + // Add newly added policy to expected policy list. + expected_policies.push_back(PopulateExpectedPolicy( + newly_added_policy_name, std::string(), NULL, false)); + + // Verify if policy UI includes policy that extension have. + VerifyPolicies(expected_policies); +} -- 2.11.4.GIT