| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ui/snapshot/screenshot_grabber.h" | 5 #include "ui/snapshot/screenshot_grabber.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <climits> | 9 #include <climits> |
| 10 #include <string> | 10 #include <string> |
| 11 | 11 |
| 12 #include "base/bind.h" | 12 #include "base/bind.h" |
| 13 #include "base/callback.h" | 13 #include "base/callback.h" |
| 14 #include "base/files/file_util.h" | 14 #include "base/files/file_util.h" |
| 15 #include "base/location.h" | 15 #include "base/location.h" |
| 16 #include "base/logging.h" | 16 #include "base/logging.h" |
| 17 #include "base/message_loop/message_loop.h" | 17 #include "base/message_loop/message_loop.h" |
| 18 #include "base/single_thread_task_runner.h" | 18 #include "base/single_thread_task_runner.h" |
| 19 #include "base/task_runner.h" | 19 #include "base/task_runner.h" |
| 20 #include "base/threading/sequenced_worker_pool.h" | 20 #include "base/threading/sequenced_worker_pool.h" |
| 21 #include "base/threading/thread_task_runner_handle.h" | 21 #include "base/threading/thread_task_runner_handle.h" |
| 22 #include "base/time/time.h" | 22 #include "base/time/time.h" |
| 23 #include "ui/gfx/image/image.h" | 23 #include "ui/gfx/image/image.h" |
| 24 #include "ui/snapshot/snapshot.h" | 24 #include "ui/snapshot/snapshot.h" |
| 25 | 25 |
| 26 #if defined(USE_AURA) | 26 #if defined(USE_AURA) |
| 27 #include "ui/aura/client/cursor_client.h" |
| 27 #include "ui/aura/window.h" | 28 #include "ui/aura/window.h" |
| 28 #endif | 29 #endif |
| 29 | 30 |
| 30 namespace ui { | 31 namespace ui { |
| 31 | 32 |
| 32 namespace { | 33 namespace { |
| 33 // The minimum interval between two screenshot commands. It has to be | 34 // The minimum interval between two screenshot commands. It has to be |
| 34 // more than 1000 to prevent the conflict of filenames. | 35 // more than 1000 to prevent the conflict of filenames. |
| 35 const int kScreenshotMinimumIntervalInMS = 1000; | 36 const int kScreenshotMinimumIntervalInMS = 1000; |
| 36 | 37 |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 101 | 102 |
| 102 void ScreenshotGrabberDelegate::PrepareFileAndRunOnBlockingPool( | 103 void ScreenshotGrabberDelegate::PrepareFileAndRunOnBlockingPool( |
| 103 const base::FilePath& path, | 104 const base::FilePath& path, |
| 104 scoped_refptr<base::TaskRunner> blocking_task_runner, | 105 scoped_refptr<base::TaskRunner> blocking_task_runner, |
| 105 const FileCallback& callback_on_blocking_pool) { | 106 const FileCallback& callback_on_blocking_pool) { |
| 106 blocking_task_runner->PostTask( | 107 blocking_task_runner->PostTask( |
| 107 FROM_HERE, | 108 FROM_HERE, |
| 108 base::Bind(EnsureLocalDirectoryExists, path, callback_on_blocking_pool)); | 109 base::Bind(EnsureLocalDirectoryExists, path, callback_on_blocking_pool)); |
| 109 } | 110 } |
| 110 | 111 |
| 112 #if defined(USE_AURA) |
| 113 class ScreenshotGrabber::ScopedCursorHider { |
| 114 public: |
| 115 // The nullptr might be returned when GetCursorClient is nullptr. |
| 116 static std::unique_ptr<ScopedCursorHider> Create(aura::Window* window) { |
| 117 DCHECK(window->IsRootWindow()); |
| 118 aura::client::CursorClient* cursor_client = |
| 119 aura::client::GetCursorClient(window); |
| 120 if (!cursor_client) |
| 121 return nullptr; |
| 122 cursor_client->HideCursor(); |
| 123 return std::unique_ptr<ScopedCursorHider>( |
| 124 base::WrapUnique(new ScopedCursorHider(window))); |
| 125 } |
| 126 |
| 127 ~ScopedCursorHider() { |
| 128 aura::client::CursorClient* cursor_client = |
| 129 aura::client::GetCursorClient(window_); |
| 130 cursor_client->ShowCursor(); |
| 131 } |
| 132 |
| 133 private: |
| 134 explicit ScopedCursorHider(aura::Window* window) : window_(window) {} |
| 135 aura::Window* window_; |
| 136 |
| 137 DISALLOW_COPY_AND_ASSIGN(ScopedCursorHider); |
| 138 }; |
| 139 #endif |
| 140 |
| 111 ScreenshotGrabber::ScreenshotGrabber( | 141 ScreenshotGrabber::ScreenshotGrabber( |
| 112 ScreenshotGrabberDelegate* client, | 142 ScreenshotGrabberDelegate* client, |
| 113 scoped_refptr<base::TaskRunner> blocking_task_runner) | 143 scoped_refptr<base::TaskRunner> blocking_task_runner) |
| 114 : client_(client), | 144 : client_(client), |
| 115 blocking_task_runner_(blocking_task_runner), | 145 blocking_task_runner_(blocking_task_runner), |
| 116 factory_(this) { | 146 factory_(this) { |
| 117 } | 147 } |
| 118 | 148 |
| 119 ScreenshotGrabber::~ScreenshotGrabber() { | 149 ScreenshotGrabber::~ScreenshotGrabber() { |
| 120 } | 150 } |
| 121 | 151 |
| 122 void ScreenshotGrabber::TakeScreenshot(gfx::NativeWindow window, | 152 void ScreenshotGrabber::TakeScreenshot(gfx::NativeWindow window, |
| 123 const gfx::Rect& rect, | 153 const gfx::Rect& rect, |
| 124 const base::FilePath& screenshot_path) { | 154 const base::FilePath& screenshot_path) { |
| 125 DCHECK(base::MessageLoopForUI::IsCurrent()); | 155 DCHECK(base::MessageLoopForUI::IsCurrent()); |
| 126 last_screenshot_timestamp_ = base::TimeTicks::Now(); | 156 last_screenshot_timestamp_ = base::TimeTicks::Now(); |
| 127 | 157 |
| 128 bool is_partial = true; | 158 bool is_partial = true; |
| 129 // Window identifier is used to log a message on failure to capture a full | 159 // Window identifier is used to log a message on failure to capture a full |
| 130 // screen (i.e. non partial) screenshot. The only time is_partial can be | 160 // screen (i.e. non partial) screenshot. The only time is_partial can be |
| 131 // false, we will also have an identification string for the window. | 161 // false, we will also have an identification string for the window. |
| 132 std::string window_identifier; | 162 std::string window_identifier; |
| 133 #if defined(USE_AURA) | 163 #if defined(USE_AURA) |
| 134 aura::Window* aura_window = static_cast<aura::Window*>(window); | 164 aura::Window* aura_window = static_cast<aura::Window*>(window); |
| 135 is_partial = rect.size() != aura_window->bounds().size(); | 165 is_partial = rect.size() != aura_window->bounds().size(); |
| 136 window_identifier = aura_window->GetBoundsInScreen().ToString(); | 166 window_identifier = aura_window->GetBoundsInScreen().ToString(); |
| 167 |
| 168 cursor_hider_ = ScopedCursorHider::Create(aura_window->GetRootWindow()); |
| 137 #endif | 169 #endif |
| 138 ui::GrabWindowSnapshotAsync( | 170 ui::GrabWindowSnapshotAsync( |
| 139 window, rect, blocking_task_runner_, | 171 window, rect, blocking_task_runner_, |
| 140 base::Bind(&ScreenshotGrabber::GrabWindowSnapshotAsyncCallback, | 172 base::Bind(&ScreenshotGrabber::GrabWindowSnapshotAsyncCallback, |
| 141 factory_.GetWeakPtr(), window_identifier, screenshot_path, | 173 factory_.GetWeakPtr(), window_identifier, screenshot_path, |
| 142 is_partial)); | 174 is_partial)); |
| 143 } | 175 } |
| 144 | 176 |
| 145 bool ScreenshotGrabber::CanTakeScreenshot() { | 177 bool ScreenshotGrabber::CanTakeScreenshot() { |
| 146 return last_screenshot_timestamp_.is_null() || | 178 return last_screenshot_timestamp_.is_null() || |
| 147 base::TimeTicks::Now() - last_screenshot_timestamp_ > | 179 base::TimeTicks::Now() - last_screenshot_timestamp_ > |
| 148 base::TimeDelta::FromMilliseconds(kScreenshotMinimumIntervalInMS); | 180 base::TimeDelta::FromMilliseconds(kScreenshotMinimumIntervalInMS); |
| 149 } | 181 } |
| 150 | 182 |
| 151 void ScreenshotGrabber::NotifyScreenshotCompleted( | 183 void ScreenshotGrabber::NotifyScreenshotCompleted( |
| 152 ScreenshotGrabberObserver::Result screenshot_result, | 184 ScreenshotGrabberObserver::Result screenshot_result, |
| 153 const base::FilePath& screenshot_path) { | 185 const base::FilePath& screenshot_path) { |
| 154 DCHECK(base::MessageLoopForUI::IsCurrent()); | 186 DCHECK(base::MessageLoopForUI::IsCurrent()); |
| 187 #if defined(USE_AURA) |
| 188 cursor_hider_.reset(); |
| 189 #endif |
| 155 FOR_EACH_OBSERVER(ScreenshotGrabberObserver, observers_, | 190 FOR_EACH_OBSERVER(ScreenshotGrabberObserver, observers_, |
| 156 OnScreenshotCompleted(screenshot_result, screenshot_path)); | 191 OnScreenshotCompleted(screenshot_result, screenshot_path)); |
| 157 } | 192 } |
| 158 | 193 |
| 159 void ScreenshotGrabber::AddObserver(ScreenshotGrabberObserver* observer) { | 194 void ScreenshotGrabber::AddObserver(ScreenshotGrabberObserver* observer) { |
| 160 observers_.AddObserver(observer); | 195 observers_.AddObserver(observer); |
| 161 } | 196 } |
| 162 | 197 |
| 163 void ScreenshotGrabber::RemoveObserver(ScreenshotGrabberObserver* observer) { | 198 void ScreenshotGrabber::RemoveObserver(ScreenshotGrabberObserver* observer) { |
| 164 observers_.RemoveObserver(observer); | 199 observers_.RemoveObserver(observer); |
| (...skipping 28 matching lines...) Expand all Loading... |
| 193 | 228 |
| 194 ShowNotificationCallback notification_callback(base::Bind( | 229 ShowNotificationCallback notification_callback(base::Bind( |
| 195 &ScreenshotGrabber::NotifyScreenshotCompleted, factory_.GetWeakPtr())); | 230 &ScreenshotGrabber::NotifyScreenshotCompleted, factory_.GetWeakPtr())); |
| 196 client_->PrepareFileAndRunOnBlockingPool( | 231 client_->PrepareFileAndRunOnBlockingPool( |
| 197 screenshot_path, blocking_task_runner_, | 232 screenshot_path, blocking_task_runner_, |
| 198 base::Bind(&SaveScreenshot, base::ThreadTaskRunnerHandle::Get(), | 233 base::Bind(&SaveScreenshot, base::ThreadTaskRunnerHandle::Get(), |
| 199 notification_callback, screenshot_path, png_data)); | 234 notification_callback, screenshot_path, png_data)); |
| 200 } | 235 } |
| 201 | 236 |
| 202 } // namespace ui | 237 } // namespace ui |
| OLD | NEW |