File: device_command_screenshot_job.h

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (143 lines) | stat: -rw-r--r-- 5,230 bytes parent folder | download | duplicates (7)
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
// Copyright 2015 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_POLICY_REMOTE_COMMANDS_DEVICE_COMMAND_SCREENSHOT_JOB_H_
#define CHROME_BROWSER_ASH_POLICY_REMOTE_COMMANDS_DEVICE_COMMAND_SCREENSHOT_JOB_H_

#include <stddef.h>

#include <map>
#include <memory>
#include <string>
#include <utility>

#include "base/functional/callback.h"
#include "base/memory/ref_counted_memory.h"
#include "base/memory/weak_ptr.h"
#include "base/task/task_runner.h"
#include "chrome/browser/ash/policy/uploading/upload_job.h"
#include "components/policy/core/common/remote_commands/remote_command_job.h"
#include "ui/snapshot/snapshot.h"
#include "url/gurl.h"

namespace policy {

// The first element represents screen index, the second presents the PNG data.
using ScreenshotData = std::pair<size_t, scoped_refptr<base::RefCountedMemory>>;

using OnScreenshotTakenCallback =
    base::OnceCallback<void(scoped_refptr<base::RefCountedMemory>)>;

// This class implements a RemoteCommandJob that captures a screenshot from
// each attached screen and uploads the collected PNG data using UploadJob to
// the url specified in the "fileUploadUrl" field of the CommandPayload. Only
// one instance of this command will be running at a time. The
// RemoteCommandsQueue owns all instances of this class.
class DeviceCommandScreenshotJob : public RemoteCommandJob,
                                   public UploadJob::Delegate {
 public:
  static const char kUploadUrlFieldName[];
  // When the screenshot command terminates, the result payload that gets sent
  // to the server is populated with one of the following result codes. These
  // are exposed publicly here since DeviceCommandScreenshotTest uses them.
  enum ResultCode {
    // Successfully uploaded screenshots.
    SUCCESS = 0,

    // Aborted screenshot acquisition due to user input having been entered.
    FAILURE_USER_INPUT = 1,

    // Failed to acquire screenshots, e.g. no attached screens.
    FAILURE_SCREENSHOT_ACQUISITION = 2,

    // Failed to authenticate to the remote server.
    FAILURE_AUTHENTICATION = 3,

    // Failed due to an internal server error.
    FAILURE_SERVER = 4,

    // Failed due to a client-side error.
    FAILURE_CLIENT = 5,

    // Failed due to an invalid upload url.
    FAILURE_INVALID_URL = 6
  };

  // A delegate interface used by DeviceCommandScreenshotJob to retrieve its
  // dependencies.
  class Delegate {
   public:
    virtual ~Delegate() = default;

    // Returns true if screenshots are allowed in this session. Returns false
    // if the current session is not an auto-launched kiosk session, or there
    // have been certain types of user input that may result in leaking private
    // information.
    virtual bool IsScreenshotAllowed() = 0;

    // Acquires a snapshot of |source_rect| in |window| and invokes |callback|
    // with the PNG data. The passed-in callback will not be invoked after the
    // delegate has been destroyed. See e.g. ScreenshotDelegate.
    virtual void TakeSnapshot(gfx::NativeWindow window,
                              const gfx::Rect& source_rect,
                              OnScreenshotTakenCallback callback) = 0;

    // Creates a new fully configured instance of an UploadJob. This method
    // may be called any number of times.
    virtual std::unique_ptr<UploadJob> CreateUploadJob(
        const GURL&,
        UploadJob::Delegate*) = 0;
  };

  explicit DeviceCommandScreenshotJob(
      std::unique_ptr<Delegate> screenshot_delegate);

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

  ~DeviceCommandScreenshotJob() override;

  // RemoteCommandJob:
  enterprise_management::RemoteCommand_Type GetType() const override;

 private:
  class Payload;

  // UploadJob::Delegate:
  void OnSuccess() override;
  void OnFailure(UploadJob::ErrorCode error_code) override;

  // RemoteCommandJob:
  bool ParseCommandPayload(const std::string& command_payload) override;
  void RunImpl(CallbackWithResult result_callback) override;
  void TerminateImpl() override;

  // Posts `StartScreenshotUpload` job on |task_runner|.
  void OnScreenshotsReady(scoped_refptr<base::TaskRunner> task_runner,
                          std::vector<ScreenshotData> upload_data);

  void StartScreenshotUpload(std::vector<ScreenshotData> upload_data);

  void ReportResult(ResultType result_type, ResultCode result_code);

  // The URL to which the POST request should be directed.
  GURL upload_url_;

  // The callback that will be called when the screenshot was successfully
  // uploaded or when the command has failed.
  CallbackWithResult result_callback_;

  // The Delegate is used to acquire screenshots and create UploadJobs.
  std::unique_ptr<Delegate> screenshot_delegate_;

  // The upload job instance that will upload the screenshots.
  std::unique_ptr<UploadJob> upload_job_;

  base::WeakPtrFactory<DeviceCommandScreenshotJob> weak_ptr_factory_{this};
};

}  // namespace policy

#endif  // CHROME_BROWSER_ASH_POLICY_REMOTE_COMMANDS_DEVICE_COMMAND_SCREENSHOT_JOB_H_