screenshot_grabber.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // Copyright 2014 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.
  4. #ifndef UI_SNAPSHOT_SCREENSHOT_GRABBER_H_
  5. #define UI_SNAPSHOT_SCREENSHOT_GRABBER_H_
  6. #include <memory>
  7. #include <string>
  8. #include "base/callback.h"
  9. #include "base/memory/ref_counted_memory.h"
  10. #include "base/memory/weak_ptr.h"
  11. #include "base/time/time.h"
  12. #include "ui/gfx/geometry/rect.h"
  13. #include "ui/gfx/native_widget_types.h"
  14. #include "ui/snapshot/snapshot_export.h"
  15. namespace ui {
  16. // Result of the entire screenshotting attempt. This enum is fat for various
  17. // file operations which could happen in the chrome layer.
  18. enum class ScreenshotResult {
  19. SUCCESS,
  20. GRABWINDOW_PARTIAL_FAILED,
  21. GRABWINDOW_FULL_FAILED,
  22. CREATE_DIR_FAILED,
  23. GET_DIR_FAILED,
  24. CHECK_DIR_FAILED,
  25. CREATE_FILE_FAILED,
  26. WRITE_FILE_FAILED,
  27. // Disabled by an enterprise policy or special modes.
  28. DISABLED,
  29. // Disabled by Data Leak Prevention feature.
  30. DISABLED_BY_DLP
  31. };
  32. class SNAPSHOT_EXPORT ScreenshotGrabber {
  33. public:
  34. ScreenshotGrabber();
  35. ScreenshotGrabber(const ScreenshotGrabber&) = delete;
  36. ScreenshotGrabber& operator=(const ScreenshotGrabber&) = delete;
  37. ~ScreenshotGrabber();
  38. // Callback for the new system, which ignores the observer crud.
  39. using ScreenshotCallback =
  40. base::OnceCallback<void(ui::ScreenshotResult screenshot_result,
  41. scoped_refptr<base::RefCountedMemory> png_data)>;
  42. // Takes a screenshot of |rect| in |window| in that window's coordinate space
  43. // and return it to |callback|.
  44. void TakeScreenshot(gfx::NativeWindow window,
  45. const gfx::Rect& rect,
  46. ScreenshotCallback callback);
  47. bool CanTakeScreenshot();
  48. private:
  49. #if defined(USE_AURA)
  50. class ScopedCursorHider;
  51. #endif
  52. void GrabWindowSnapshotAsyncCallback(
  53. const std::string& window_identifier,
  54. bool is_partial,
  55. ScreenshotCallback callback,
  56. scoped_refptr<base::RefCountedMemory> png_data);
  57. // The timestamp when the screenshot task was issued last time.
  58. base::TimeTicks last_screenshot_timestamp_;
  59. #if defined(USE_AURA)
  60. // The object to hide cursor when taking screenshot.
  61. std::unique_ptr<ScopedCursorHider> cursor_hider_;
  62. #endif
  63. base::WeakPtrFactory<ScreenshotGrabber> factory_{this};
  64. };
  65. } // namespace ui
  66. #endif // UI_SNAPSHOT_SCREENSHOT_GRABBER_H_