1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
|
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_ASH_CERT_PROVISIONING_CERT_PROVISIONING_WORKER_H_
#define CHROME_BROWSER_ASH_CERT_PROVISIONING_CERT_PROVISIONING_WORKER_H_
#include <stddef.h>
#include <memory>
#include <optional>
#include <string>
#include <vector>
#include "base/functional/callback_forward.h"
#include "base/time/time.h"
#include "chrome/browser/ash/cert_provisioning/cert_provisioning_client.h"
#include "chrome/browser/ash/cert_provisioning/cert_provisioning_common.h"
#include "components/policy/proto/device_management_backend.pb.h"
class Profile;
class PrefService;
namespace ash {
namespace cert_provisioning {
class CertProvisioningInvalidator;
// A OnceCallback that is invoked when the CertProvisioningWorker is done and
// has a result (which could be success or failure).
using CertProvisioningWorkerCallback =
base::OnceCallback<void(CertProfile profile,
std::string process_id,
CertProvisioningWorkerState state)>;
class CertProvisioningWorker;
struct BackendServerError {
// info on the last failed DMServer call attempt.
BackendServerError(policy::DeviceManagementStatus dm_status,
base::Time error_time)
: time(error_time), status(dm_status) {}
base::Time time;
policy::DeviceManagementStatus status;
};
class CertProvisioningWorkerFactory {
public:
virtual ~CertProvisioningWorkerFactory() = default;
static CertProvisioningWorkerFactory* Get();
virtual std::unique_ptr<CertProvisioningWorker> Create(
std::string process_id,
CertScope cert_scope,
Profile* profile,
PrefService* pref_service,
const CertProfile& cert_profile,
CertProvisioningClient* cert_provisioning_client,
std::unique_ptr<CertProvisioningInvalidator> invalidator,
base::RepeatingClosure state_change_callback,
CertProvisioningWorkerCallback result_callback);
virtual std::unique_ptr<CertProvisioningWorker> Deserialize(
CertScope cert_scope,
Profile* profile,
PrefService* pref_service,
const base::Value::Dict& saved_worker,
CertProvisioningClient* cert_provisioning_client,
std::unique_ptr<CertProvisioningInvalidator> invalidator,
base::RepeatingClosure state_change_callback,
CertProvisioningWorkerCallback result_callback);
// Doesn't take ownership.
static void SetFactoryForTesting(CertProvisioningWorkerFactory* test_factory);
private:
static CertProvisioningWorkerFactory* test_factory_;
};
// This class is a part of certificate provisioning feature. It takes a
// certificate profile, generates a key pair, communicates with DM Server to
// create a CSR for it and request a certificate, and imports it.
class CertProvisioningWorker {
public:
CertProvisioningWorker() = default;
CertProvisioningWorker(const CertProvisioningWorker&) = delete;
CertProvisioningWorker& operator=(const CertProvisioningWorker&) = delete;
virtual ~CertProvisioningWorker() = default;
// Continue provisioning a certificate.
virtual void DoStep() = 0;
// Sets worker's state to one of final ones. That triggers corresponding
// clean ups (deletes serialized state, keys, and so on) and returns |state|
// via callback.
virtual void Stop(CertProvisioningWorkerState state) = 0;
// Make worker pause all activity and wait for DoStep.
virtual void Pause() = 0;
// Mark worker that it is undergoing a reset process.
virtual void MarkWorkerForReset() = 0;
// Returns true, if the worker is waiting for some future event. |DoStep| can
// be called to try continue right now.
virtual bool IsWaiting() const = 0;
// Returns true if the worker is to be recreated due to a user-initiated
// "reset" action.
virtual bool IsWorkerMarkedForReset() const = 0;
// Returns the certificate provisioning process id.
virtual const std::string& GetProcessId() const = 0;
// Returns CertProfile that this worker is working on.
virtual const CertProfile& GetCertProfile() const = 0;
// Returns public key or an empty string if the key is not created yet.
virtual const std::vector<uint8_t>& GetPublicKey() const = 0;
// Returns current state.
virtual CertProvisioningWorkerState GetState() const = 0;
// Returns state that was before the current one. Especially helpful on failed
// workers.
virtual CertProvisioningWorkerState GetPreviousState() const = 0;
// Returns the time when this worker has been last updated.
virtual base::Time GetLastUpdateTime() const = 0;
// Return the info of when this worker has last faced an unsuccessful attempt.
virtual const std::optional<BackendServerError>& GetLastBackendServerError()
const = 0;
// Return a message describing the reason for failure when the worker fails.
// In case the worker did not fail, the message is empty. It can be shown in
// the UI, but should not be logged.
virtual std::string GetFailureMessageWithPii() const = 0;
};
} // namespace cert_provisioning
} // namespace ash
#endif // CHROME_BROWSER_ASH_CERT_PROVISIONING_CERT_PROVISIONING_WORKER_H_
|