Commit b48a6747 authored by Owen Min's avatar Owen Min Committed by Commit Bot

Refactor ChromeBrowserPolicyConnector.

Refactor the class to move most of MachineLevelUserCloudPolicy related code
into its own class.

Bug: 846742
Change-Id: I7d548a1d1e96de7ebbecf48a7e4e29d1cc033c12
Reviewed-on: https://chromium-review.googlesource.com/1089922Reviewed-by: default avatarScott Violet <sky@chromium.org>
Reviewed-by: default avatarMaksim Ivanov <emaxx@chromium.org>
Reviewed-by: default avatarRoger Tawa <rogerta@chromium.org>
Commit-Queue: Owen Min <zmin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#566583}
parent 93b8c817
......@@ -3263,6 +3263,8 @@ jumbo_split_static_library("browser") {
"policy/cloud/machine_level_user_cloud_policy_helper.h",
"policy/cloud/user_policy_signin_service.cc",
"policy/cloud/user_policy_signin_service.h",
"policy/machine_level_user_cloud_policy_controller.cc",
"policy/machine_level_user_cloud_policy_controller.h",
"policy/machine_level_user_cloud_policy_register_watcher.cc",
"policy/machine_level_user_cloud_policy_register_watcher.h",
"profiles/avatar_menu.cc",
......
......@@ -205,6 +205,7 @@
#if !defined(OS_ANDROID) && !defined(OS_CHROMEOS)
#include "chrome/browser/first_run/upgrade_util.h"
#include "chrome/browser/policy/machine_level_user_cloud_policy_controller.h"
#endif
#if defined(OS_CHROMEOS)
......@@ -725,18 +726,17 @@ bool IsSiteIsolationEnterprisePolicyApplicable() {
bool WaitUntilMachineLevelUserCloudPolicyEnrollmentFinished(
policy::ChromeBrowserPolicyConnector* connector) {
#if !defined(OS_CHROMEOS) && !defined(OS_ANDROID)
switch (connector->WaitUntilMachineLevelUserCloudPolicyEnrollmentFinished()) {
case policy::ChromeBrowserPolicyConnector::
MachineLevelUserCloudPolicyRegisterResult::kNoEnrollmentNeeded:
case policy::ChromeBrowserPolicyConnector::
MachineLevelUserCloudPolicyRegisterResult::kEnrollmentSuccess:
using RegisterResult =
policy::MachineLevelUserCloudPolicyController::RegisterResult;
switch (connector->machine_level_user_cloud_policy_controller()
->WaitUntilPolicyEnrollmentFinished()) {
case RegisterResult::kNoEnrollmentNeeded:
case RegisterResult::kEnrollmentSuccess:
return true;
case policy::ChromeBrowserPolicyConnector::
MachineLevelUserCloudPolicyRegisterResult::kRestartDueToFailure:
case RegisterResult::kRestartDueToFailure:
chrome::AttemptRestart();
return false;
case policy::ChromeBrowserPolicyConnector::
MachineLevelUserCloudPolicyRegisterResult::kQuitDueToFailure:
case RegisterResult::kQuitDueToFailure:
chrome::AttemptExit();
return false;
}
......
......@@ -13,6 +13,7 @@
#include "base/values.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/policy/chrome_browser_policy_connector.h"
#include "chrome/browser/policy/machine_level_user_cloud_policy_controller.h"
#include "chrome/browser/policy/policy_conversions.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/common/channel_info.h"
......@@ -68,7 +69,7 @@ std::string GetProfileId(const Profile* profile) {
int64_t GetMachineLevelUserCloudPolicyFetchTimestamp() {
policy::MachineLevelUserCloudPolicyManager* manager =
g_browser_process->browser_policy_connector()
->GetMachineLevelUserCloudPolicyManager();
->machine_level_user_cloud_policy_manager();
if (!manager || !manager->IsClientRegistered())
return 0;
return manager->core()->client()->last_policy_timestamp().ToJavaTime();
......
......@@ -10,7 +10,6 @@
#include <memory>
#include <vector>
#include "base/files/file_path.h"
#include "base/macros.h"
#include "base/memory/ref_counted.h"
#include "build/build_config.h"
......@@ -23,12 +22,12 @@ class URLRequestContextGetter;
}
namespace policy {
class ConfigurationPolicyProvider;
#if !defined(OS_ANDROID) && !defined(OS_CHROMEOS)
class MachineLevelUserCloudPolicyController;
class MachineLevelUserCloudPolicyManager;
class MachineLevelUserCloudPolicyFetcher;
class MachineLevelUserCloudPolicyRegisterWatcher;
class MachineLevelUserCloudPolicyRegistrar;
#endif
// Extends BrowserPolicyConnector with the setup shared among the desktop
// implementations and Android.
......@@ -38,21 +37,6 @@ class ChromeBrowserPolicyConnector : public BrowserPolicyConnector {
// displaying Chrome's GUI does not get delayed.)
static const int64_t kServiceInitializationStartupDelay = 5000;
// Directory name under the user-data-dir where machine level user cloud
// policy data is stored.
static const base::FilePath::CharType kPolicyDir[];
// Machine level user cloud dpolicy enrollment result.
enum class MachineLevelUserCloudPolicyRegisterResult {
kNoEnrollmentNeeded, // The device won't be enrolled without an enrollment
// token.
kEnrollmentSuccess, // The device has been enrolled successfully.
kQuitDueToFailure, // The enrollment has failed or aborted, user choose to
// quit Chrome.
kRestartDueToFailure, // The enrollment has failed, user choose to restart
// the Chrome to retry.
};
// Builds an uninitialized ChromeBrowserPolicyConnector, suitable for testing.
// Init() should be called to create and start the policy machinery.
ChromeBrowserPolicyConnector();
......@@ -73,24 +57,15 @@ class ChromeBrowserPolicyConnector : public BrowserPolicyConnector {
ConfigurationPolicyProvider* GetPlatformProvider();
class Observer {
public:
virtual ~Observer() {}
// Called when machine level user cloud policy enrollment is finished.
// |succeeded| is true if |dm_token| is returned from the server.
virtual void OnMachineLevelUserCloudPolicyRegisterFinished(bool succeeded) {
}
};
void AddObserver(Observer* observer);
void RemoveObserver(Observer* observer);
#if !defined(OS_ANDROID) && !defined(OS_CHROMEOS)
MachineLevelUserCloudPolicyRegisterResult
WaitUntilMachineLevelUserCloudPolicyEnrollmentFinished();
MachineLevelUserCloudPolicyManager* GetMachineLevelUserCloudPolicyManager();
MachineLevelUserCloudPolicyController*
machine_level_user_cloud_policy_controller() {
return machine_level_user_cloud_policy_controller_.get();
}
MachineLevelUserCloudPolicyManager*
machine_level_user_cloud_policy_manager() {
return machine_level_user_cloud_policy_manager_;
}
#endif
protected:
......@@ -98,39 +73,19 @@ class ChromeBrowserPolicyConnector : public BrowserPolicyConnector {
std::vector<std::unique_ptr<policy::ConfigurationPolicyProvider>>
CreatePolicyProviders() override;
#if !defined(OS_ANDROID) && !defined(OS_CHROMEOS)
void NotifyMachineLevelUserCloudPolicyRegisterFinished(bool succeeded);
#endif
private:
std::unique_ptr<ConfigurationPolicyProvider> CreatePlatformProvider();
#if !defined(OS_ANDROID) && !defined(OS_CHROMEOS)
void InitializeMachineLevelUserCloudPolicies(
PrefService* local_state,
scoped_refptr<net::URLRequestContextGetter> request_context);
bool GetEnrollmentTokenAndClientId(std::string* enrollment_token,
std::string* client_id);
void RegisterForPolicyWithEnrollmentTokenCallback(
const std::string& dm_token,
const std::string& client_id);
// Owned by base class.
ConfigurationPolicyProvider* platform_provider_ = nullptr;
#if !defined(OS_ANDROID) && !defined(OS_CHROMEOS)
std::unique_ptr<MachineLevelUserCloudPolicyController>
machine_level_user_cloud_policy_controller_;
// Owned by base class.
MachineLevelUserCloudPolicyManager* machine_level_user_cloud_policy_manager_ =
nullptr;
std::unique_ptr<MachineLevelUserCloudPolicyRegistrar>
machine_level_user_cloud_policy_registrar_;
std::unique_ptr<MachineLevelUserCloudPolicyFetcher>
machine_level_user_cloud_policy_fetcher_;
std::unique_ptr<MachineLevelUserCloudPolicyRegisterWatcher>
machine_level_user_cloud_policy_register_watcher_;
#endif
base::ObserverList<Observer, true> observers_;
// Owned by base class.
ConfigurationPolicyProvider* platform_provider_ = nullptr;
DISALLOW_COPY_AND_ASSIGN(ChromeBrowserPolicyConnector);
};
......
......@@ -22,6 +22,7 @@
#include "chrome/browser/chrome_browser_main_extra_parts.h"
#include "chrome/browser/policy/browser_dm_token_storage.h"
#include "chrome/browser/policy/chrome_browser_policy_connector.h"
#include "chrome/browser/policy/machine_level_user_cloud_policy_controller.h"
#include "chrome/browser/policy/test/local_policy_test_server.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/common/chrome_result_codes.h"
......@@ -64,10 +65,10 @@ const char kDMToken[] = "fake-dm-token";
const char kEnrollmentResultMetrics[] =
"Enterprise.MachineLevelUserCloudPolicyEnrollment.Result";
class ChromeBrowserPolicyConnectorObserver
: public ChromeBrowserPolicyConnector::Observer {
class MachineLevelUserCloudPolicyControllerObserver
: public MachineLevelUserCloudPolicyController::Observer {
public:
void OnMachineLevelUserCloudPolicyRegisterFinished(bool succeeded) override {
void OnPolicyRegisterFinished(bool succeeded) override {
if (!succeeded) {
// Close the error dialog.
ASSERT_EQ(1u, views::test::WidgetTest::GetAllWidgets().size());
......@@ -77,7 +78,9 @@ class ChromeBrowserPolicyConnectorObserver
is_finished_ = true;
if (run_loop_)
run_loop_->Quit();
g_browser_process->browser_policy_connector()->RemoveObserver(this);
g_browser_process->browser_policy_connector()
->machine_level_user_cloud_policy_controller()
->RemoveObserver(this);
}
void SetRunLoop(base::RunLoop* run_loop) { run_loop_ = run_loop; }
......@@ -132,14 +135,16 @@ class FakeBrowserDMTokenStorage : public BrowserDMTokenStorage {
class ChromeBrowserExtraSetUp : public ChromeBrowserMainExtraParts {
public:
explicit ChromeBrowserExtraSetUp(
ChromeBrowserPolicyConnectorObserver* observer)
MachineLevelUserCloudPolicyControllerObserver* observer)
: observer_(observer) {}
void PreMainMessageLoopStart() override {
g_browser_process->browser_policy_connector()->AddObserver(observer_);
g_browser_process->browser_policy_connector()
->machine_level_user_cloud_policy_controller()
->AddObserver(observer_);
}
private:
ChromeBrowserPolicyConnectorObserver* observer_;
MachineLevelUserCloudPolicyControllerObserver* observer_;
DISALLOW_COPY_AND_ASSIGN(ChromeBrowserExtraSetUp);
};
......@@ -324,7 +329,7 @@ class MachineLevelUserCloudPolicyManagerTest : public InProcessBrowserTest {
policy_store->AddObserver(&observer);
base::FilePath policy_dir =
user_data_dir.Append(ChromeBrowserPolicyConnector::kPolicyDir);
user_data_dir.Append(MachineLevelUserCloudPolicyController::kPolicyDir);
std::unique_ptr<MachineLevelUserCloudPolicyManager> manager =
std::make_unique<MachineLevelUserCloudPolicyManager>(
......@@ -398,7 +403,7 @@ class MachineLevelUserCloudPolicyEnrollmentTest
private:
LocalPolicyTestServer test_server_;
FakeBrowserDMTokenStorage storage_;
ChromeBrowserPolicyConnectorObserver observer_;
MachineLevelUserCloudPolicyControllerObserver observer_;
DISALLOW_COPY_AND_ASSIGN(MachineLevelUserCloudPolicyEnrollmentTest);
};
......
// 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 "chrome/browser/policy/machine_level_user_cloud_policy_controller.h"
#include <utility>
#include "base/bind.h"
#include "base/callback.h"
#include "base/command_line.h"
#include "base/logging.h"
#include "base/metrics/histogram_macros.h"
#include "base/path_service.h"
#include "base/task_scheduler/post_task.h"
#include "base/threading/thread_task_runner_handle.h"
#include "build/build_config.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/policy/browser_dm_token_storage.h"
#include "chrome/browser/policy/chrome_browser_policy_connector.h"
#include "chrome/browser/policy/cloud/machine_level_user_cloud_policy_helper.h"
#include "chrome/browser/policy/machine_level_user_cloud_policy_register_watcher.h"
#include "chrome/common/chrome_paths.h"
#include "components/policy/core/common/cloud/cloud_external_data_manager.h"
#include "components/policy/core/common/cloud/machine_level_user_cloud_policy_manager.h"
#include "components/policy/core/common/cloud/machine_level_user_cloud_policy_metrics.h"
#include "components/policy/core/common/cloud/machine_level_user_cloud_policy_store.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/common/content_switches.h"
#if defined(OS_WIN)
#include "chrome/install_static/install_util.h"
#endif
namespace policy {
namespace {
void RecordEnrollmentResult(
MachineLevelUserCloudPolicyEnrollmentResult result) {
UMA_HISTOGRAM_ENUMERATION(
"Enterprise.MachineLevelUserCloudPolicyEnrollment.Result", result);
}
} // namespace
const base::FilePath::CharType
MachineLevelUserCloudPolicyController::kPolicyDir[] =
FILE_PATH_LITERAL("Policy");
MachineLevelUserCloudPolicyController::MachineLevelUserCloudPolicyController() {
}
MachineLevelUserCloudPolicyController::
~MachineLevelUserCloudPolicyController() {}
// static
std::unique_ptr<MachineLevelUserCloudPolicyManager>
MachineLevelUserCloudPolicyController::CreatePolicyManager() {
std::string enrollment_token =
BrowserDMTokenStorage::Get()->RetrieveEnrollmentToken();
std::string dm_token = BrowserDMTokenStorage::Get()->RetrieveDMToken();
std::string client_id = BrowserDMTokenStorage::Get()->RetrieveClientId();
if (enrollment_token.empty() && dm_token.empty())
return nullptr;
base::FilePath user_data_dir;
if (!base::PathService::Get(chrome::DIR_USER_DATA, &user_data_dir))
return nullptr;
DVLOG(1) << "Creating machine level cloud policy manager";
base::FilePath policy_dir =
user_data_dir.Append(MachineLevelUserCloudPolicyController::kPolicyDir);
std::unique_ptr<MachineLevelUserCloudPolicyStore> policy_store =
MachineLevelUserCloudPolicyStore::Create(
dm_token, client_id, policy_dir,
base::CreateSequencedTaskRunnerWithTraits(
{base::MayBlock(), base::TaskPriority::BACKGROUND}));
return std::make_unique<MachineLevelUserCloudPolicyManager>(
std::move(policy_store), nullptr, policy_dir,
base::ThreadTaskRunnerHandle::Get(),
content::BrowserThread::GetTaskRunnerForThread(
content::BrowserThread::IO));
}
void MachineLevelUserCloudPolicyController::Init(
PrefService* local_state,
scoped_refptr<net::URLRequestContextGetter> request_context) {
MachineLevelUserCloudPolicyManager* policy_manager =
g_browser_process->browser_policy_connector()
->machine_level_user_cloud_policy_manager();
DeviceManagementService* device_management_service =
g_browser_process->browser_policy_connector()
->device_management_service();
if (!policy_manager)
return;
// If there exists an enrollment token, then there are two states:
// 1/ There also exists a DM token. This machine is already registeted, so
// the next step is to fetch policies.
// 2/ There is no DM token. In this case the machine is not already
// registered and needs to request a DM token.
std::string enrollment_token;
std::string client_id;
if (!GetEnrollmentTokenAndClientId(&enrollment_token, &client_id))
return;
DCHECK(!enrollment_token.empty());
DCHECK(!client_id.empty());
DVLOG(1) << "Enrollment token = " << enrollment_token;
DVLOG(1) << "Client ID = " << client_id;
policy_registrar_ = std::make_unique<MachineLevelUserCloudPolicyRegistrar>(
device_management_service, request_context);
policy_fetcher_ = std::make_unique<MachineLevelUserCloudPolicyFetcher>(
policy_manager, local_state, device_management_service, request_context);
policy_register_watcher_ =
std::make_unique<MachineLevelUserCloudPolicyRegisterWatcher>(this);
std::string dm_token = BrowserDMTokenStorage::Get()->RetrieveDMToken();
DVLOG(1) << "DM token = " << (dm_token.empty() ? "none" : "from persistence");
if (dm_token.empty()) {
// Not registered already, so do it now.
policy_registrar_->RegisterForPolicyWithEnrollmentToken(
enrollment_token, client_id,
base::Bind(&MachineLevelUserCloudPolicyController::
RegisterForPolicyWithEnrollmentTokenCallback,
base::Unretained(this)));
#if defined(OS_WIN)
// This metric is only published on Windows to indicate how many user level
// install Chrome try to enroll the policy which can't store the DM token
// in the Registry in the end of enrollment. Mac and Linux does not need
// this metric for now as they might use different token storage mechanism
// in the future.
UMA_HISTOGRAM_BOOLEAN(
"Enterprise.MachineLevelUserCloudPolicyEnrollment.InstallLevel_Win",
install_static::IsSystemInstall());
#endif
}
}
MachineLevelUserCloudPolicyController::RegisterResult
MachineLevelUserCloudPolicyController::WaitUntilPolicyEnrollmentFinished() {
if (policy_register_watcher_) {
return policy_register_watcher_->WaitUntilCloudPolicyEnrollmentFinished();
}
return RegisterResult::kNoEnrollmentNeeded;
}
void MachineLevelUserCloudPolicyController::AddObserver(Observer* observer) {
observers_.AddObserver(observer);
}
void MachineLevelUserCloudPolicyController::RemoveObserver(Observer* observer) {
observers_.RemoveObserver(observer);
}
void MachineLevelUserCloudPolicyController::NotifyPolicyRegisterFinished(
bool succeeded) {
for (auto& observer : observers_) {
observer.OnPolicyRegisterFinished(succeeded);
}
}
bool MachineLevelUserCloudPolicyController::GetEnrollmentTokenAndClientId(
std::string* enrollment_token,
std::string* client_id) {
*client_id = BrowserDMTokenStorage::Get()->RetrieveClientId();
if (client_id->empty())
return false;
*enrollment_token = BrowserDMTokenStorage::Get()->RetrieveEnrollmentToken();
return !enrollment_token->empty();
}
void MachineLevelUserCloudPolicyController::
RegisterForPolicyWithEnrollmentTokenCallback(const std::string& dm_token,
const std::string& client_id) {
if (dm_token.empty()) {
DVLOG(1) << "No DM token returned from browser registration";
RecordEnrollmentResult(
MachineLevelUserCloudPolicyEnrollmentResult::kFailedToFetch);
NotifyPolicyRegisterFinished(false);
return;
}
DVLOG(1) << "DM token = retrieved from server";
// TODO(alito): Log failures to store the DM token. Should we try again later?
BrowserDMTokenStorage::Get()->StoreDMToken(
dm_token, base::BindOnce([](bool success) {
if (!success) {
DVLOG(1) << "Failed to store the DM token";
RecordEnrollmentResult(
MachineLevelUserCloudPolicyEnrollmentResult::kFailedToStore);
} else {
DVLOG(1) << "Successfully stored the DM token";
RecordEnrollmentResult(
MachineLevelUserCloudPolicyEnrollmentResult::kSuccess);
}
}));
// Start fetching policies.
policy_fetcher_->SetupRegistrationAndFetchPolicy(dm_token, client_id);
NotifyPolicyRegisterFinished(true);
}
} // 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 CHROME_BROWSER_POLICY_MACHINE_LEVEL_USER_CLOUD_POLICY_CONTROLLER_H_
#define CHROME_BROWSER_POLICY_MACHINE_LEVEL_USER_CLOUD_POLICY_CONTROLLER_H_
#include <memory>
#include <string>
#include "base/files/file_path.h"
#include "base/macros.h"
#include "base/memory/ref_counted.h"
#include "base/observer_list.h"
#include "net/url_request/url_request_context_getter.h"
class PrefService;
namespace policy {
class MachineLevelUserCloudPolicyManager;
class MachineLevelUserCloudPolicyFetcher;
class MachineLevelUserCloudPolicyRegisterWatcher;
class MachineLevelUserCloudPolicyRegistrar;
// A class that setups and manages MachineLevelUserCloudPolicy.
class MachineLevelUserCloudPolicyController {
public:
// Machine level user cloud policy enrollment result.
enum class RegisterResult {
kNoEnrollmentNeeded, // The device won't be enrolled without an enrollment
// token.
kEnrollmentSuccess, // The device has been enrolled successfully.
kQuitDueToFailure, // The enrollment has failed or aborted, user choose to
// quit Chrome.
kRestartDueToFailure, // The enrollment has failed, user choose to restart
};
class Observer {
public:
virtual ~Observer() {}
// Called when policy enrollment is finished.
// |succeeded| is true if |dm_token| is returned from the server.
virtual void OnPolicyRegisterFinished(bool succeeded) {}
};
// Directory name under the user-data-dir where the policy data is stored.
static const base::FilePath::CharType kPolicyDir[];
MachineLevelUserCloudPolicyController();
virtual ~MachineLevelUserCloudPolicyController();
static std::unique_ptr<MachineLevelUserCloudPolicyManager>
CreatePolicyManager();
void Init(PrefService* local_state,
scoped_refptr<net::URLRequestContextGetter> request_context);
RegisterResult WaitUntilPolicyEnrollmentFinished();
void AddObserver(Observer* observer);
void RemoveObserver(Observer* observer);
protected:
void NotifyPolicyRegisterFinished(bool succeeded);
private:
bool GetEnrollmentTokenAndClientId(std::string* enrollment_token,
std::string* client_id);
void RegisterForPolicyWithEnrollmentTokenCallback(
const std::string& dm_token,
const std::string& client_id);
base::ObserverList<Observer, true> observers_;
std::unique_ptr<MachineLevelUserCloudPolicyRegistrar> policy_registrar_;
std::unique_ptr<MachineLevelUserCloudPolicyFetcher> policy_fetcher_;
// This is an observer of the controller and needs to be declared after the
// |observers_|.
std::unique_ptr<MachineLevelUserCloudPolicyRegisterWatcher>
policy_register_watcher_;
DISALLOW_COPY_AND_ASSIGN(MachineLevelUserCloudPolicyController);
};
} // namespace policy
#endif // CHROME_BROWSER_POLICY_MACHINE_LEVEL_USER_CLOUD_POLICY_CONTROLLER_H_
......@@ -16,18 +16,17 @@
namespace policy {
using RegisterResult =
ChromeBrowserPolicyConnector::MachineLevelUserCloudPolicyRegisterResult;
using RegisterResult = MachineLevelUserCloudPolicyController::RegisterResult;
MachineLevelUserCloudPolicyRegisterWatcher::
MachineLevelUserCloudPolicyRegisterWatcher(
ChromeBrowserPolicyConnector* connector)
: connector_(connector) {
connector_->AddObserver(this);
MachineLevelUserCloudPolicyController* controller)
: controller_(controller) {
controller_->AddObserver(this);
}
MachineLevelUserCloudPolicyRegisterWatcher::
~MachineLevelUserCloudPolicyRegisterWatcher() {
connector_->RemoveObserver(this);
controller_->RemoveObserver(this);
}
RegisterResult MachineLevelUserCloudPolicyRegisterWatcher::
......@@ -79,8 +78,8 @@ void MachineLevelUserCloudPolicyRegisterWatcher::
dialog_creation_callback_ = std::move(callback);
}
void MachineLevelUserCloudPolicyRegisterWatcher::
OnMachineLevelUserCloudPolicyRegisterFinished(bool succeeded) {
void MachineLevelUserCloudPolicyRegisterWatcher::OnPolicyRegisterFinished(
bool succeeded) {
register_result_ = succeeded;
// If dialog still exists, dismiss the dialog for a success enrollment or
......
......@@ -12,7 +12,7 @@
#include "base/macros.h"
#include "base/optional.h"
#include "base/run_loop.h"
#include "chrome/browser/policy/chrome_browser_policy_connector.h"
#include "chrome/browser/policy/machine_level_user_cloud_policy_controller.h"
#include "chrome/browser/ui/enterprise_startup_dialog.h"
namespace policy {
......@@ -20,33 +20,33 @@ namespace policy {
// Watches the status of machine level user cloud policy enrollment.
// Shows the blocking dialog for ongoing enrollment and failed enrollment.
class MachineLevelUserCloudPolicyRegisterWatcher
: public ChromeBrowserPolicyConnector::Observer {
: public MachineLevelUserCloudPolicyController::Observer {
public:
using DialogCreationCallback =
base::OnceCallback<std::unique_ptr<EnterpriseStartupDialog>(
EnterpriseStartupDialog::DialogResultCallback)>;
explicit MachineLevelUserCloudPolicyRegisterWatcher(
ChromeBrowserPolicyConnector* connector);
MachineLevelUserCloudPolicyController* controller);
~MachineLevelUserCloudPolicyRegisterWatcher() override;
// Blocks until the machine level user cloud policy enrollment process
// finishes. Returns the result of enrollment.
ChromeBrowserPolicyConnector::MachineLevelUserCloudPolicyRegisterResult
MachineLevelUserCloudPolicyController::RegisterResult
WaitUntilCloudPolicyEnrollmentFinished();
void SetDialogCreationCallbackForTesting(DialogCreationCallback callback);
private:
// ChromeBrowserPolicyConnector::Observer
void OnMachineLevelUserCloudPolicyRegisterFinished(bool succeeded) override;
// MachineLevelUserCloudPolicyController::Observer
void OnPolicyRegisterFinished(bool succeeded) override;
// EnterpriseStartupDialog callback.
void OnDialogClosed(bool is_accepted, bool can_show_browser_window);
void DisplayErrorMessage();
ChromeBrowserPolicyConnector* connector_;
MachineLevelUserCloudPolicyController* controller_;
base::RunLoop run_loop_;
std::unique_ptr<EnterpriseStartupDialog> dialog_;
......
......@@ -18,8 +18,8 @@
using ::testing::InvokeWithoutArgs;
using ::testing::Return;
using ::testing::_;
using RegisterResult = policy::ChromeBrowserPolicyConnector::
MachineLevelUserCloudPolicyRegisterResult;
using RegisterResult =
policy::MachineLevelUserCloudPolicyController::RegisterResult;
namespace policy {
......@@ -54,17 +54,18 @@ class FakeDMTokenStorage : public BrowserDMTokenStorage {
DISALLOW_COPY_AND_ASSIGN(FakeDMTokenStorage);
};
// A mock ChromeBrowserPolicyConnector that notifies all observers the machine
// level user cloud policy enrollment process has been finihsed.
class FakeChromeBrowserPolicyConnector : public ChromeBrowserPolicyConnector {
// A fake MachineLevelUserCloudPolicyController that notifies all observers the
// machine level user cloud policy enrollment process has been finished.
class FakeMachineLevelUserCloudPolicyController
: public MachineLevelUserCloudPolicyController {
public:
FakeChromeBrowserPolicyConnector() = default;
FakeMachineLevelUserCloudPolicyController() = default;
void FireNotification(bool succeeded) {
NotifyMachineLevelUserCloudPolicyRegisterFinished(succeeded);
NotifyPolicyRegisterFinished(succeeded);
}
private:
DISALLOW_COPY_AND_ASSIGN(FakeChromeBrowserPolicyConnector);
DISALLOW_COPY_AND_ASSIGN(FakeMachineLevelUserCloudPolicyController);
};
// A mock EnterpriseStartDialog to mimic the behavior of real dialog.
......@@ -108,7 +109,7 @@ class MockEnterpriseStartupDialog : public EnterpriseStartupDialog {
class MachineLevelUserCloudPolicyRegisterWatcherTest : public ::testing::Test {
public:
MachineLevelUserCloudPolicyRegisterWatcherTest()
: watcher_(&connector_),
: watcher_(&controller_),
dialog_(std::make_unique<MockEnterpriseStartupDialog>()),
dialog_ptr_(dialog_.get()) {
BrowserDMTokenStorage::SetForTesting(&storage_);
......@@ -122,7 +123,9 @@ class MachineLevelUserCloudPolicyRegisterWatcherTest : public ::testing::Test {
protected:
FakeDMTokenStorage* storage() { return &storage_; }
FakeChromeBrowserPolicyConnector* connector() { return &connector_; }
FakeMachineLevelUserCloudPolicyController* controller() {
return &controller_;
}
MachineLevelUserCloudPolicyRegisterWatcher* watcher() { return &watcher_; }
MockEnterpriseStartupDialog* dialog() { return dialog_ptr_; }
......@@ -135,7 +138,7 @@ class MachineLevelUserCloudPolicyRegisterWatcherTest : public ::testing::Test {
private:
content::TestBrowserThreadBundle browser_thread_bundle_;
FakeChromeBrowserPolicyConnector connector_;
FakeMachineLevelUserCloudPolicyController controller_;
MachineLevelUserCloudPolicyRegisterWatcher watcher_;
FakeDMTokenStorage storage_;
std::unique_ptr<MockEnterpriseStartupDialog> dialog_;
......@@ -164,8 +167,9 @@ TEST_F(MachineLevelUserCloudPolicyRegisterWatcherTest, EnrollmentSucceed) {
EXPECT_CALL(*dialog(), IsShowing()).WillOnce(Return(true));
base::ThreadTaskRunnerHandle::Get()->PostTask(
FROM_HERE,
base::BindOnce(&FakeChromeBrowserPolicyConnector::FireNotification,
base::Unretained(connector()), true));
base::BindOnce(
&FakeMachineLevelUserCloudPolicyController::FireNotification,
base::Unretained(controller()), true));
EXPECT_EQ(RegisterResult::kEnrollmentSuccess,
watcher()->WaitUntilCloudPolicyEnrollmentFinished());
}
......@@ -179,8 +183,9 @@ TEST_F(MachineLevelUserCloudPolicyRegisterWatcherTest,
EXPECT_CALL(*dialog(), IsShowing()).WillOnce(Return(true));
base::ThreadTaskRunnerHandle::Get()->PostTask(
FROM_HERE,
base::BindOnce(&FakeChromeBrowserPolicyConnector::FireNotification,
base::Unretained(connector()), false));
base::BindOnce(
&FakeMachineLevelUserCloudPolicyController::FireNotification,
base::Unretained(controller()), false));
EXPECT_EQ(RegisterResult::kQuitDueToFailure,
watcher()->WaitUntilCloudPolicyEnrollmentFinished());
}
......@@ -194,8 +199,9 @@ TEST_F(MachineLevelUserCloudPolicyRegisterWatcherTest,
EXPECT_CALL(*dialog(), IsShowing()).WillOnce(Return(true));
base::ThreadTaskRunnerHandle::Get()->PostTask(
FROM_HERE,
base::BindOnce(&FakeChromeBrowserPolicyConnector::FireNotification,
base::Unretained(connector()), false));
base::BindOnce(
&FakeMachineLevelUserCloudPolicyController::FireNotification,
base::Unretained(controller()), false));
EXPECT_EQ(RegisterResult::kRestartDueToFailure,
watcher()->WaitUntilCloudPolicyEnrollmentFinished());
}
......@@ -216,7 +222,7 @@ TEST_F(MachineLevelUserCloudPolicyRegisterWatcherTest,
EXPECT_CALL(*dialog(), DisplayErrorMessage(_, _))
.WillOnce(
InvokeWithoutArgs([this] { dialog()->UserClickedTheButton(false); }));
connector()->FireNotification(false);
controller()->FireNotification(false);
EXPECT_EQ(RegisterResult::kQuitDueToFailure,
watcher()->WaitUntilCloudPolicyEnrollmentFinished());
}
......
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