File: dmserver_job_configurations.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (181 lines) | stat: -rw-r--r-- 6,248 bytes parent folder | download | duplicates (5)
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// Copyright 2019 The Chromium Authors
// 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_DMSERVER_JOB_CONFIGURATIONS_H_
#define COMPONENTS_POLICY_CORE_COMMON_CLOUD_DMSERVER_JOB_CONFIGURATIONS_H_

#include <memory>
#include <optional>
#include <string>

#include "base/functional/callback.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/scoped_refptr.h"
#include "components/policy/core/common/cloud/device_management_service.h"
#include "components/policy/core/common/cloud/dm_auth.h"
#include "components/policy/policy_export.h"
#include "components/policy/proto/cloud_policy.pb.h"
#include "components/policy/proto/device_management_backend.pb.h"
#include "url/gurl.h"

namespace network {
class SharedURLLoaderFactory;
}

namespace policy {

class CloudPolicyClient;

// Struct containing the result data for a given job.
struct DMServerJobResult {
  // Unowned pointer the return value of `DeviceManagementService::CreateJob`.
  raw_ptr<const DeviceManagementService::Job, DanglingUntriaged> job = nullptr;

  // net::Error value cast to int.
  int net_error = 0;

  // Status code combining
  //   - `net_error`
  //   - HTTP response code received from DMServer
  //   - potential error from parsing `response`
  DeviceManagementStatus dm_status =
      DeviceManagementStatus::DM_STATUS_REQUEST_INVALID;

  // HTTP response codes of the DMServer.
  int response_code = 0;

  // The parsed response proto received from DMServer. This could be empty
  // in case of errors.
  enterprise_management::DeviceManagementResponse response;
};

// A configuration for sending enterprise_management::DeviceManagementRequest to
// the DM server.
class POLICY_EXPORT DMServerJobConfiguration : public JobConfigurationBase {
 public:
  typedef base::OnceCallback<void(DMServerJobResult)> Callback;

  struct POLICY_EXPORT CreateParams {
   public:
    static CreateParams WithClient(JobType type, CloudPolicyClient* client);
    static CreateParams WithoutClient(
        JobType type,
        DeviceManagementService* service,
        const std::string& client_id,
        scoped_refptr<network::SharedURLLoaderFactory> factory);

    // Used by old `DMServerJobConfiguration` constructor. Please avoid adding
    // new parameter to it or using it outside DMServerJobConfiguration
    // constructor.
    static CreateParams WithParams(
        DeviceManagementService* service,
        JobType type,
        const std::string& client_id,
        bool critical,
        DMAuth auth_data,
        std::optional<std::string> oauth_token,
        scoped_refptr<network::SharedURLLoaderFactory> factory,
        Callback callback);

    CreateParams();

    CreateParams(const CreateParams&) = delete;
    CreateParams& operator=(const CreateParams&) = delete;

    CreateParams(CreateParams&&);
    CreateParams& operator=(CreateParams&&);

    ~CreateParams();

    raw_ptr<DeviceManagementService> service = nullptr;
    JobType type = JobType::TYPE_INVALID;
    std::string client_id;
    bool critical = false;
    DMAuth auth_data = DMAuth::NoAuth();
    std::optional<std::string> profile_id = std::nullopt;
    std::optional<std::string> oauth_token = std::nullopt;
    bool use_cookies = false;
    scoped_refptr<network::SharedURLLoaderFactory> factory;
    DMServerJobConfiguration::Callback callback;
  };

  explicit DMServerJobConfiguration(CreateParams params);

  // Deprecated. Please use the `CreateParams` instead.
  DMServerJobConfiguration(
      DeviceManagementService* service,
      JobType type,
      const std::string& client_id,
      bool critical,
      DMAuth auth_data,
      std::optional<std::string>&& oauth_token,
      scoped_refptr<network::SharedURLLoaderFactory> factory,
      Callback callback);

  // Deprecated. Please use the `CreateParams` instead.
  DMServerJobConfiguration(JobType type,
                           CloudPolicyClient* client,
                           bool critical,
                           DMAuth auth_data,
                           std::optional<std::string>&& oauth_token,
                           Callback callback);

  DMServerJobConfiguration(const DMServerJobConfiguration&) = delete;
  DMServerJobConfiguration& operator=(const DMServerJobConfiguration&) = delete;

  ~DMServerJobConfiguration() override;

  enterprise_management::DeviceManagementRequest* request() {
    return &request_;
  }

 protected:
  DeviceManagementStatus MapNetErrorAndResponseToDMStatus(
      int net_error,
      int response_code,
      const std::string& response_body);

 private:
  // JobConfiguration interface.
  std::string GetPayload() override;
  std::string GetUmaName() override;
  void OnBeforeRetry(int response_code,
                     const std::string& response_body) override {}
  void OnURLLoadComplete(DeviceManagementService::Job* job,
                         int net_error,
                         int response_code,
                         const std::string& response_body) override;

  // JobConfigurationBase overrides.
  GURL GetURL(int last_error) const override;

  std::string server_url_;
  enterprise_management::DeviceManagementRequest request_;
  Callback callback_;
};

// A configuration for sending registration requests to the DM server.  These
// are requests of type TYPE_REGISTRATION, TYPE_TOKEN_ENROLLMENT, and
// TYPE_CERT_BASED_REGISTRATION.  This class extends DMServerJobConfiguration
// by adjusting the enterprise_management::DeviceManagementRequest message in
// registration retry requests.
class POLICY_EXPORT RegistrationJobConfiguration
    : public DMServerJobConfiguration {
 public:
  explicit RegistrationJobConfiguration(CreateParams params);
  RegistrationJobConfiguration(const RegistrationJobConfiguration&) = delete;
  RegistrationJobConfiguration& operator=(const RegistrationJobConfiguration&) =
      delete;

  void SetTimeoutDuration(base::TimeDelta timeout);

 private:
  // JobConfiguration interface.
  void OnBeforeRetry(int response_code,
                     const std::string& response_body) override;
};

}  // namespace policy

#endif  // COMPONENTS_POLICY_CORE_COMMON_CLOUD_DMSERVER_JOB_CONFIGURATIONS_H_