File: reporting_server_connector.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 (123 lines) | stat: -rw-r--r-- 5,300 bytes parent folder | download | duplicates (6)
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
// Copyright 2022 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_POLICY_MESSAGING_LAYER_UTIL_REPORTING_SERVER_CONNECTOR_H_
#define CHROME_BROWSER_POLICY_MESSAGING_LAYER_UTIL_REPORTING_SERVER_CONNECTOR_H_

#include <memory>
#include <vector>

#include "base/feature_list.h"
#include "base/functional/callback.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/singleton.h"
#include "chrome/browser/policy/messaging_layer/upload/encrypted_reporting_client.h"
#include "chrome/browser/policy/messaging_layer/util/upload_declarations.h"
#include "chrome/browser/policy/messaging_layer/util/upload_response_parser.h"
#include "components/policy/core/common/cloud/cloud_policy_client.h"
#include "components/policy/core/common/cloud/cloud_policy_core.h"
#include "components/policy/core/common/cloud/cloud_policy_manager.h"
#include "components/reporting/proto/synced/record.pb.h"
#include "components/reporting/proto/synced/record_constants.pb.h"
#include "components/reporting/resources/resource_manager.h"
#include "components/reporting/util/status.h"
#include "components/reporting/util/statusor.h"

namespace reporting {

BASE_DECLARE_FEATURE(kEnableReportingFromUnmanagedDevices);

// Singleton wrapper of a reporting server client used when uploading events
// to the reporting server. Enables safe access to the cloud policy client with
// an ability to detect when it is disconnected. Actual upload is implemented
// with a dedicated reporting client.
class ReportingServerConnector : public ::policy::CloudPolicyCore::Observer {
 public:
  using ResponseCallback = EncryptedReportingClient::ResponseCallback;

  class Observer {
   public:
    virtual ~Observer() = default;
    virtual void OnConnected() = 0;
    virtual void OnDisconnected() = 0;

   protected:
    Observer() = default;
  };

  // RAII class for testing ReportingServerConnector - substitutes cloud policy
  // client instead of getting it from the cloud policy core. Resets client when
  // destructed.
  class TestEnvironment;

  ReportingServerConnector(const ReportingServerConnector& other) = delete;
  ReportingServerConnector& operator=(const ReportingServerConnector& other) =
      delete;
  ~ReportingServerConnector() override;

  // Accesses singleton `ReportingServerConnector` instance.
  static ReportingServerConnector* GetInstance();

  // Uploads a report containing `merging_payload` (merged into the default
  // payload of the job). The client must be in a registered state (otherwise
  // the upload fails). The `callback` will be called when the operation
  // completes or fails.
  static void UploadEncryptedReport(bool need_encryption_key,
                                    int config_file_version,
                                    std::vector<EncryptedRecord> records,
                                    ScopedReservation scoped_reservation,
                                    UploadEnqueuedCallback enqueued_cb,
                                    ResponseCallback callback);

  // Adds/removes observer to the Connector.
  void AddObserver(Observer* observer);
  void RemoveObserver(Observer* observer);

 private:
  friend struct base::DefaultSingletonTraits<ReportingServerConnector>;

  // Constructor to be used by singleton only.
  ReportingServerConnector();

  // Returns cloud policy manager or Status in case of error.
  StatusOr<::policy::CloudPolicyManager*> GetUserCloudPolicyManager();

  // Returns OK if `CloudPolicyCore` instance is usable, or error status
  // otherwise. If successful, caches the core and registers `this` as its
  // observer.
  Status EnsureUsableCore();

  // Returns OK if `CloudPolicyClient` is already usable or made usable, or
  // error status otherwise. If successful, caches the client.
  Status EnsureUsableClient();

  // ::policy::CloudPolicyCore::Observer implementation.
  void OnCoreConnected(::policy::CloudPolicyCore* core) override;
  void OnRefreshSchedulerStarted(::policy::CloudPolicyCore* core) override;
  void OnCoreDisconnecting(::policy::CloudPolicyCore* core) override;
  void OnCoreDestruction(::policy::CloudPolicyCore* core) override;

  // Presets uploads and forwards the data.
  void UploadEncryptedReportInternal(bool need_encryption_key,
                                     int config_file_version,
                                     std::vector<EncryptedRecord> records,
                                     ScopedReservation scoped_reservation,
                                     UploadEnqueuedCallback enqueued_cb,
                                     ResponseCallback callback);

  // Onwed by CloudPolicyManager. Cached here (only on UI task runner).
  raw_ptr<::policy::CloudPolicyCore> core_ = nullptr;

  // Onwed by CloudPolicyCore. Used by `UploadEncryptedReport` - must be
  // non-null by then. Cached here (only on UI task runner).
  raw_ptr<::policy::CloudPolicyClient> client_ = nullptr;

  std::unique_ptr<EncryptedReportingClient> encrypted_reporting_client_;

  // Active observers list (to be updated on UI task runner only).
  std::vector<raw_ptr<Observer>> observers_;
};
}  // namespace reporting

#endif  // CHROME_BROWSER_POLICY_MESSAGING_LAYER_UTIL_REPORTING_SERVER_CONNECTOR_H_