Commit 86ce4187 authored by Owen Min's avatar Owen Min Committed by Commit Bot

Create MachineLevelUserCloudPolicyManager

This is a new implmentation of CloudPolicyManager.
It will initialize the policy and always try to load store immediately if
possible.

Bug: 812641
Change-Id: I0c23fdeda4093123f7859682cd43669fefe37cfd
Reviewed-on: https://chromium-review.googlesource.com/957817
Commit-Queue: Owen Min <zmin@chromium.org>
Reviewed-by: default avatarMaksim Ivanov <emaxx@chromium.org>
Cr-Commit-Position: refs/heads/master@{#544060}
parent 4fdd012a
......@@ -62,6 +62,8 @@ source_set("internal") {
"cloud/external_policy_data_fetcher.h",
"cloud/external_policy_data_updater.cc",
"cloud/external_policy_data_updater.h",
"cloud/machine_level_user_cloud_policy_manager.cc",
"cloud/machine_level_user_cloud_policy_manager.h",
"cloud/machine_level_user_cloud_policy_store.cc",
"cloud/machine_level_user_cloud_policy_store.h",
"cloud/policy_header_io_helper.cc",
......@@ -202,6 +204,8 @@ source_set("internal") {
sources -= [
"cloud/cloud_policy_client_registration_helper.cc",
"cloud/cloud_policy_client_registration_helper.h",
"cloud/machine_level_user_cloud_policy_manager.cc",
"cloud/machine_level_user_cloud_policy_manager.h",
"cloud/machine_level_user_cloud_policy_store.cc",
"cloud/machine_level_user_cloud_policy_store.h",
"cloud/user_cloud_policy_manager.cc",
......@@ -322,6 +326,7 @@ source_set("unit_tests") {
]
} else {
sources += [
"cloud/machine_level_user_cloud_policy_manager_unittest.cc",
"cloud/machine_level_user_cloud_policy_store_unittest.cc",
"cloud/user_cloud_policy_manager_unittest.cc",
"cloud/user_cloud_policy_store_unittest.cc",
......
// 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/policy/core/common/cloud/machine_level_user_cloud_policy_manager.h"
#include <string>
#include <utility>
#include "base/threading/thread_task_runner_handle.h"
#include "components/policy/core/common/cloud/cloud_external_data_manager.h"
#include "components/policy/core/common/cloud/cloud_policy_constants.h"
#include "components/policy/core/common/cloud/machine_level_user_cloud_policy_store.h"
#include "components/policy/core/common/policy_pref_names.h"
#include "components/prefs/pref_service.h"
#include "net/url_request/url_request_context_getter.h"
namespace policy {
namespace {
const base::FilePath::CharType kComponentPolicyCache[] =
FILE_PATH_LITERAL("Machine Level User Cloud Component Policy");
} // namespace
MachineLevelUserCloudPolicyManager::MachineLevelUserCloudPolicyManager(
std::unique_ptr<MachineLevelUserCloudPolicyStore> store,
std::unique_ptr<CloudExternalDataManager> external_data_manager,
const base::FilePath& policy_dir,
const scoped_refptr<base::SequencedTaskRunner>& task_runner,
const scoped_refptr<base::SequencedTaskRunner>& io_task_runner)
: CloudPolicyManager(dm_protocol::kChromeMachineLevelUserCloudPolicyType,
std::string(),
store.get(),
task_runner,
io_task_runner),
store_(std::move(store)),
external_data_manager_(std::move(external_data_manager)),
policy_dir_(policy_dir) {}
MachineLevelUserCloudPolicyManager::~MachineLevelUserCloudPolicyManager() {}
void MachineLevelUserCloudPolicyManager::Connect(
PrefService* local_state,
scoped_refptr<net::URLRequestContextGetter> request_context,
std::unique_ptr<CloudPolicyClient> client) {
CHECK(!core()->client());
CreateComponentCloudPolicyService(
dm_protocol::kChromeMachineLevelExtensionCloudPolicyType,
policy_dir_.Append(kComponentPolicyCache), request_context, client.get(),
schema_registry());
core()->Connect(std::move(client));
core()->StartRefreshScheduler();
core()->TrackRefreshDelayPref(local_state,
policy_prefs::kUserPolicyRefreshRate);
if (external_data_manager_)
external_data_manager_->Connect(request_context);
}
bool MachineLevelUserCloudPolicyManager::IsClientRegistered() {
return client() && client()->is_registered();
}
void MachineLevelUserCloudPolicyManager::Init(SchemaRegistry* registry) {
DVLOG(1) << "Machine level cloud policy manager initialized";
ConfigurationPolicyProvider::Init(registry);
store()->AddObserver(this);
// Load the policy from disk synchronously once the manager is initalized
// during Chrome launch if the cache and the global dm token exist.
store()->LoadImmediately();
}
void MachineLevelUserCloudPolicyManager::Shutdown() {
if (external_data_manager_)
external_data_manager_->Disconnect();
CloudPolicyManager::Shutdown();
}
} // namespace policy
// 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_POLICY_CORE_COMMON_CLOUD_MACHINE_LEVEL_USER_CLOUD_POLICY_MANAGER_H_
#define COMPONENTS_POLICY_CORE_COMMON_CLOUD_MACHINE_LEVEL_USER_CLOUD_POLICY_MANAGER_H_
#include <memory>
#include "base/macros.h"
#include "components/policy/core/common/cloud/cloud_policy_manager.h"
class PrefService;
namespace policy {
class MachineLevelUserCloudPolicyStore;
// Implements a cloud policy manager that initializes the machine level user
// cloud policy.
class POLICY_EXPORT MachineLevelUserCloudPolicyManager
: public CloudPolicyManager {
public:
MachineLevelUserCloudPolicyManager(
std::unique_ptr<MachineLevelUserCloudPolicyStore> store,
std::unique_ptr<CloudExternalDataManager> external_data_manager,
const base::FilePath& policy_dir,
const scoped_refptr<base::SequencedTaskRunner>& task_runner,
const scoped_refptr<base::SequencedTaskRunner>& io_task_runner);
~MachineLevelUserCloudPolicyManager() override;
// Initializes the cloud connection. |local_state| must stay valid until this
// object is deleted.
void Connect(PrefService* local_state,
scoped_refptr<net::URLRequestContextGetter> request_context,
std::unique_ptr<CloudPolicyClient> client);
// Returns true if the underlying CloudPolicyClient is already registered.
bool IsClientRegistered();
MachineLevelUserCloudPolicyStore* store() { return store_.get(); }
// ConfigurationPolicyProvider:
void Init(SchemaRegistry* registry) override;
void Shutdown() override;
private:
std::unique_ptr<MachineLevelUserCloudPolicyStore> store_;
std::unique_ptr<CloudExternalDataManager> external_data_manager_;
const base::FilePath policy_dir_;
DISALLOW_COPY_AND_ASSIGN(MachineLevelUserCloudPolicyManager);
};
} // namespace policy
#endif // COMPONENTS_POLICY_CORE_COMMON_CLOUD_MACHINE_LEVEL_USER_CLOUD_POLICY_MANAGER_H_
// 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/policy/core/common/cloud/machine_level_user_cloud_policy_manager.h"
#include <string>
#include <utility>
#include "base/macros.h"
#include "components/policy/core/common/cloud/cloud_external_data_manager.h"
#include "components/policy/core/common/cloud/machine_level_user_cloud_policy_store.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace policy {
class MockMachineLevelUserCloudPolicyStore
: public MachineLevelUserCloudPolicyStore {
public:
MockMachineLevelUserCloudPolicyStore()
: MachineLevelUserCloudPolicyStore(
std::string(),
std::string(),
base::FilePath(),
base::FilePath(),
scoped_refptr<base::SequencedTaskRunner>()) {}
MOCK_METHOD0(LoadImmediately, void(void));
};
class MachineLevelUserCloudPolicyManagerTest : public ::testing::Test {
public:
MachineLevelUserCloudPolicyManagerTest() {}
~MachineLevelUserCloudPolicyManagerTest() override { manager_->Shutdown(); }
void SetUp() override {
auto store = std::make_unique<MockMachineLevelUserCloudPolicyStore>();
store_ = store.get();
manager_ = std::make_unique<MachineLevelUserCloudPolicyManager>(
std::move(store), std::unique_ptr<CloudExternalDataManager>(),
base::FilePath(), scoped_refptr<base::SequencedTaskRunner>(),
scoped_refptr<base::SequencedTaskRunner>());
}
SchemaRegistry schema_registry_;
MockMachineLevelUserCloudPolicyStore* store_ = nullptr;
std::unique_ptr<MachineLevelUserCloudPolicyManager> manager_;
private:
DISALLOW_COPY_AND_ASSIGN(MachineLevelUserCloudPolicyManagerTest);
};
TEST_F(MachineLevelUserCloudPolicyManagerTest, InitManager) {
EXPECT_CALL(*store_, LoadImmediately());
manager_->Init(&schema_registry_);
::testing::Mock::VerifyAndClearExpectations(store_);
}
} // namespace policy
......@@ -12,7 +12,6 @@
namespace policy {
namespace {
const base::FilePath::CharType kPolicyDir[] = FILE_PATH_LITERAL("Policy");
const base::FilePath::CharType kPolicyCache[] =
FILE_PATH_LITERAL("Machine Level User Cloud Policy");
const base::FilePath::CharType kKeyCache[] =
......@@ -40,9 +39,8 @@ std::unique_ptr<MachineLevelUserCloudPolicyStore>
MachineLevelUserCloudPolicyStore::Create(
const std::string& machine_dm_token,
const std::string& machine_client_id,
const base::FilePath& user_data_dir,
const base::FilePath& policy_dir,
scoped_refptr<base::SequencedTaskRunner> background_task_runner) {
base::FilePath policy_dir = user_data_dir.Append(kPolicyDir);
base::FilePath policy_cache_file = policy_dir.Append(kPolicyCache);
base::FilePath key_cache_file = policy_dir.Append(kKeyCache);
return std::make_unique<MachineLevelUserCloudPolicyStore>(
......
......@@ -30,7 +30,7 @@ class POLICY_EXPORT MachineLevelUserCloudPolicyStore
static std::unique_ptr<MachineLevelUserCloudPolicyStore> Create(
const std::string& machine_dm_token,
const std::string& machine_client_id,
const base::FilePath& user_data_dir,
const base::FilePath& policy_dir,
scoped_refptr<base::SequencedTaskRunner> background_task_runner);
// override DesktopCloudPolicyStore
......
......@@ -38,7 +38,7 @@ class MachineLevelUserCloudPolicyStoreTest : public ::testing::Test {
~MachineLevelUserCloudPolicyStoreTest() override {}
void SetUp() override {
ASSERT_TRUE(tmp_user_data_dir_.CreateUniqueTempDir());
ASSERT_TRUE(tmp_policy_dir_.CreateUniqueTempDir());
store_ = CreateStore();
}
......@@ -46,7 +46,7 @@ class MachineLevelUserCloudPolicyStoreTest : public ::testing::Test {
std::unique_ptr<MachineLevelUserCloudPolicyStore> store =
MachineLevelUserCloudPolicyStore::Create(
PolicyBuilder::kFakeToken, PolicyBuilder::kFakeDeviceId,
tmp_user_data_dir_.GetPath(), base::ThreadTaskRunnerHandle::Get());
tmp_policy_dir_.GetPath(), base::ThreadTaskRunnerHandle::Get());
store->AddObserver(&observer_);
return store;
}
......@@ -59,7 +59,7 @@ class MachineLevelUserCloudPolicyStoreTest : public ::testing::Test {
std::unique_ptr<MachineLevelUserCloudPolicyStore> store_;
base::ScopedTempDir tmp_user_data_dir_;
base::ScopedTempDir tmp_policy_dir_;
UserPolicyBuilder policy_;
MockCloudPolicyStoreObserver observer_;
......@@ -119,15 +119,10 @@ TEST_F(MachineLevelUserCloudPolicyStoreTest, LoadWithNoFile) {
TEST_F(MachineLevelUserCloudPolicyStoreTest, StorePolicy) {
EXPECT_FALSE(store_->policy());
EXPECT_TRUE(store_->policy_map().empty());
const base::FilePath policy_path =
tmp_user_data_dir_.GetPath()
.Append(FILE_PATH_LITERAL("Policy"))
.Append(FILE_PATH_LITERAL("Machine Level User Cloud Policy"));
const base::FilePath signing_key_path =
tmp_user_data_dir_.GetPath()
.Append(FILE_PATH_LITERAL("Policy"))
.Append(
FILE_PATH_LITERAL("Machine Level User Cloud Policy Signing Key"));
const base::FilePath policy_path = tmp_policy_dir_.GetPath().Append(
FILE_PATH_LITERAL("Machine Level User Cloud Policy"));
const base::FilePath signing_key_path = tmp_policy_dir_.GetPath().Append(
FILE_PATH_LITERAL("Machine Level User Cloud Policy Signing Key"));
EXPECT_FALSE(base::PathExists(policy_path));
EXPECT_FALSE(base::PathExists(signing_key_path));
......
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