Elim cr-checkbox
[chromium-blink-merge.git] / chrome / browser / chromeos / policy / remote_commands / device_command_screenshot_job.h
blob49032be5a7d5929910367c4e51da2f4360754a00
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef CHROME_BROWSER_CHROMEOS_POLICY_REMOTE_COMMANDS_DEVICE_COMMAND_SCREENSHOT_JOB_H_
6 #define CHROME_BROWSER_CHROMEOS_POLICY_REMOTE_COMMANDS_DEVICE_COMMAND_SCREENSHOT_JOB_H_
8 #include <list>
9 #include <map>
10 #include <string>
12 #include "base/macros.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/memory/ref_counted_memory.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/memory/weak_ptr.h"
17 #include "base/task_runner.h"
18 #include "base/timer/timer.h"
19 #include "chrome/browser/chromeos/policy/upload_job.h"
20 #include "components/policy/core/common/remote_commands/remote_command_job.h"
21 #include "google_apis/gaia/oauth2_token_service.h"
22 #include "net/url_request/url_request_context_getter.h"
23 #include "ui/snapshot/snapshot.h"
24 #include "url/gurl.h"
26 namespace policy {
28 // This class implementens a RemoteCommandJob that captures a screenshot from
29 // each attached screen and uploads the collected PNG data using UploadJob to
30 // the url specified in the "fileUploadUrl" field of the CommandPayload. Only
31 // one instance of this command will be running at a time. The
32 // RemoteCommandsQueue owns all instances of this class.
33 class DeviceCommandScreenshotJob : public RemoteCommandJob,
34 public UploadJob::Delegate {
35 public:
36 // When the screenshot command terminates, the result payload that gets sent
37 // to the server is populated with one of the following result codes. These
38 // are exposed publicly here since DeviceCommandScreenshotTest uses them.
39 enum ResultCode {
40 // Successfully uploaded screenshots.
41 SUCCESS = 0,
43 // Aborted screenshot acquisition due to user input having been entered.
44 FAILURE_USER_INPUT = 1,
46 // Failed to acquire screenshots, e.g. no attached screens.
47 FAILURE_SCREENSHOT_ACQUISITION = 2,
49 // Failed to authenticate to the remote server.
50 FAILURE_AUTHENTICATION = 3,
52 // Failed due to an internal server error.
53 FAILURE_SERVER = 4,
55 // Failed due to a client-side error.
56 FAILURE_CLIENT = 5,
58 // Failed due to an invalid upload url.
59 FAILURE_INVALID_URL = 6
62 // A delegate interface used by DeviceCommandScreenshotJob to retrieve its
63 // dependencies.
64 class Delegate {
65 public:
66 virtual ~Delegate() {}
68 // Returns true if screenshots are allowed in this session. Returns false
69 // if the current session is not an auto-launched kiosk session, or there
70 // have been certain types of user input that may result in leaking private
71 // information.
72 virtual bool IsScreenshotAllowed() = 0;
74 // Acquires a snapshot of |source_rect| in |window| and invokes |callback|
75 // with the PNG data. The passed-in callback will not be invoked after the
76 // delegate has been destroyed. See e.g. ScreenshotDelegate.
77 virtual void TakeSnapshot(
78 gfx::NativeWindow window,
79 const gfx::Rect& source_rect,
80 const ui::GrabWindowSnapshotAsyncPNGCallback& callback) = 0;
82 // Creates a new fully configured instance of an UploadJob. This method
83 // may be called any number of times.
84 virtual scoped_ptr<UploadJob> CreateUploadJob(const GURL&,
85 UploadJob::Delegate*) = 0;
88 explicit DeviceCommandScreenshotJob(scoped_ptr<Delegate> screenshot_delegate);
89 ~DeviceCommandScreenshotJob() override;
91 // RemoteCommandJob:
92 enterprise_management::RemoteCommand_Type GetType() const override;
94 private:
95 class Payload;
97 // UploadJob::Delegate:
98 void OnSuccess() override;
99 void OnFailure(UploadJob::ErrorCode error_code) override;
101 // RemoteCommandJob:
102 bool IsExpired(base::TimeTicks now) override;
103 bool ParseCommandPayload(const std::string& command_payload) override;
104 void RunImpl(const CallbackWithResult& succeeded_callback,
105 const CallbackWithResult& failed_callback) override;
106 void TerminateImpl() override;
108 void StoreScreenshot(size_t screen,
109 scoped_refptr<base::RefCountedBytes> png_data);
111 void StartScreenshotUpload();
113 // The URL to which the POST request should be directed.
114 GURL upload_url_;
116 // The callback that will be called when the screenshot was successfully
117 // uploaded.
118 CallbackWithResult succeeded_callback_;
120 // The callback that will be called when this command failed.
121 CallbackWithResult failed_callback_;
123 // Tracks the number of pending screenshots.
124 int num_pending_screenshots_;
126 // Caches the already completed screenshots for the different displays.
127 std::map<int, scoped_refptr<base::RefCountedBytes>> screenshots_;
129 // The Delegate is used to acquire screenshots and create UploadJobs.
130 scoped_ptr<Delegate> screenshot_delegate_;
132 // The upload job instance that will upload the screenshots.
133 scoped_ptr<UploadJob> upload_job_;
135 base::WeakPtrFactory<DeviceCommandScreenshotJob> weak_ptr_factory_;
137 DISALLOW_COPY_AND_ASSIGN(DeviceCommandScreenshotJob);
140 } // namespace policy
142 #endif // CHROME_BROWSER_CHROMEOS_POLICY_REMOTE_COMMANDS_DEVICE_COMMAND_SCREENSHOT_JOB_H_