OLD | NEW |
(Empty) | |
| 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 |
| 5 #ifndef UI_SNAPSHOT_SCREENSHOT_TAKER_H_ |
| 6 #define UI_SNAPSHOT_SCREENSHOT_TAKER_H_ |
| 7 |
| 8 #include "base/basictypes.h" |
| 9 #include "base/compiler_specific.h" |
| 10 #include "base/files/file_path.h" |
| 11 #include "base/memory/ref_counted_memory.h" |
| 12 #include "base/memory/scoped_ptr.h" |
| 13 #include "base/memory/weak_ptr.h" |
| 14 #include "base/observer_list.h" |
| 15 #include "base/time/time.h" |
| 16 #include "ui/aura/window.h" |
| 17 #include "ui/gfx/rect.h" |
| 18 #include "ui/snapshot/snapshot_export.h" |
| 19 |
| 20 namespace base { |
| 21 class TaskRunner; |
| 22 } |
| 23 |
| 24 namespace ui { |
| 25 |
| 26 class SNAPSHOT_EXPORT ScreenshotTakerObserver { |
| 27 public: |
| 28 enum Result { |
| 29 SCREENSHOT_SUCCESS = 0, |
| 30 SCREENSHOT_GRABWINDOW_PARTIAL_FAILED, |
| 31 SCREENSHOT_GRABWINDOW_FULL_FAILED, |
| 32 SCREENSHOT_CREATE_DIR_FAILED, |
| 33 SCREENSHOT_GET_DIR_FAILED, |
| 34 SCREENSHOT_CHECK_DIR_FAILED, |
| 35 SCREENSHOT_CREATE_FILE_FAILED, |
| 36 SCREENSHOT_WRITE_FILE_FAILED, |
| 37 SCREENSHOTS_DISABLED, |
| 38 SCREENSHOT_RESULT_COUNT |
| 39 }; |
| 40 |
| 41 // Dispatched after attempting to take a screenshot with the |result| and |
| 42 // |screenshot_path| of the taken screenshot (if successful). |
| 43 virtual void OnScreenshotCompleted(Result screenshot_result, |
| 44 const base::FilePath& screenshot_path) = 0; |
| 45 |
| 46 protected: |
| 47 virtual ~ScreenshotTakerObserver() {} |
| 48 }; |
| 49 |
| 50 // TODO(flackr): Componentize google drive so that we don't need the |
| 51 // ScreenshotTakerClient. |
| 52 class SNAPSHOT_EXPORT ScreenshotTakerClient { |
| 53 public: |
| 54 enum WritableFileResult { |
| 55 WRITABLE_FILE_SUCCESS, |
| 56 WRITABLE_FILE_CHECK_DIR_FAILED, |
| 57 WRITABLE_FILE_CREATE_DIR_FAILED, |
| 58 WRITABLE_FILE_CREATE_FAILED |
| 59 }; |
| 60 |
| 61 // Callback called with the |result| of trying to create a local writable |
| 62 // |path| for the possibly remote path. |
| 63 typedef base::Callback<void(WritableFileResult result, |
| 64 const base::FilePath& path)> WritableFileCallback; |
| 65 |
| 66 ScreenshotTakerClient() {} |
| 67 virtual ~ScreenshotTakerClient() {} |
| 68 |
| 69 // Prepares a writable file for |path|. If |path| is a non-local path (i.e. |
| 70 // Google drive) and it is supported this will create a local cached copy of |
| 71 // the remote file and call the callback with the local path. |
| 72 virtual void PrepareWritableFileAndRunOnBlockingPool( |
| 73 const base::FilePath& path, |
| 74 scoped_refptr<base::TaskRunner> blocking_task_runner, |
| 75 WritableFileCallback callback_on_blocking_pool); |
| 76 }; |
| 77 |
| 78 class SNAPSHOT_EXPORT ScreenshotTaker { |
| 79 public: |
| 80 ScreenshotTaker(ScreenshotTakerClient* client, |
| 81 scoped_refptr<base::TaskRunner> blocking_task_runner); |
| 82 ~ScreenshotTaker(); |
| 83 |
| 84 void TakeScreenshot(gfx::NativeWindow window, |
| 85 const gfx::Rect& rect, |
| 86 const base::FilePath& screenshot_paths); |
| 87 bool CanTakeScreenshot(); |
| 88 |
| 89 void NotifyScreenshotCompleted( |
| 90 ScreenshotTakerObserver::Result screenshot_result, |
| 91 const base::FilePath& screenshot_path); |
| 92 |
| 93 void AddObserver(ScreenshotTakerObserver* observer); |
| 94 void RemoveObserver(ScreenshotTakerObserver* observer); |
| 95 bool HasObserver(const ScreenshotTakerObserver* observer) const; |
| 96 |
| 97 private: |
| 98 void GrabWindowSnapshotAsyncCallback( |
| 99 const std::string& window_identifier, |
| 100 base::FilePath screenshot_path, |
| 101 bool is_partial, |
| 102 scoped_refptr<base::RefCountedBytes> png_data); |
| 103 |
| 104 // A weak pointer to the screenshot taker client. |
| 105 ScreenshotTakerClient* client_; |
| 106 |
| 107 // The timestamp when the screenshot task was issued last time. |
| 108 base::Time last_screenshot_timestamp_; |
| 109 |
| 110 // Task runner for blocking tasks. |
| 111 scoped_refptr<base::TaskRunner> blocking_task_runner_; |
| 112 |
| 113 ObserverList<ScreenshotTakerObserver> observers_; |
| 114 base::WeakPtrFactory<ScreenshotTaker> factory_; |
| 115 |
| 116 DISALLOW_COPY_AND_ASSIGN(ScreenshotTaker); |
| 117 }; |
| 118 |
| 119 } // namespace ui |
| 120 |
| 121 #endif // UI_SNAPSHOT_SCREENSHOT_TAKER_H_ |
OLD | NEW |