Chromium Code Reviews| 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 static std::unique_ptr<ScopedCursorHider> Get(aura::Window* window) { | |
|
sky
2016/07/22 23:35:04
nit: Get->Create, with a suitable description that
Qiang(Joe) Xu
2016/07/22 23:48:09
Done.
| |
| 116 DCHECK(window->IsRootWindow()); | |
| 117 aura::client::CursorClient* cursor_client = | |
| 118 aura::client::GetCursorClient(window); | |
| 119 if (!cursor_client) | |
| 120 return nullptr; | |
| 121 cursor_client->HideCursor(); | |
| 122 return std::unique_ptr<ScopedCursorHider>( | |
| 123 base::WrapUnique(new ScopedCursorHider(window))); | |
| 124 } | |
| 125 | |
| 126 ~ScopedCursorHider() { | |
| 127 aura::client::CursorClient* cursor_client = | |
| 128 aura::client::GetCursorClient(window_); | |
| 129 cursor_client->ShowCursor(); | |
| 130 } | |
| 131 | |
| 132 private: | |
| 133 explicit ScopedCursorHider(aura::Window* window) : window_(window) {} | |
| 134 aura::Window* window_; | |
| 135 | |
| 136 DISALLOW_COPY_AND_ASSIGN(ScopedCursorHider); | |
| 137 }; | |
| 138 #endif | |
| 139 | |
| 111 ScreenshotGrabber::ScreenshotGrabber( | 140 ScreenshotGrabber::ScreenshotGrabber( |
| 112 ScreenshotGrabberDelegate* client, | 141 ScreenshotGrabberDelegate* client, |
| 113 scoped_refptr<base::TaskRunner> blocking_task_runner) | 142 scoped_refptr<base::TaskRunner> blocking_task_runner) |
| 114 : client_(client), | 143 : client_(client), |
| 115 blocking_task_runner_(blocking_task_runner), | 144 blocking_task_runner_(blocking_task_runner), |
| 116 factory_(this) { | 145 factory_(this) { |
| 117 } | 146 } |
| 118 | 147 |
| 119 ScreenshotGrabber::~ScreenshotGrabber() { | 148 ScreenshotGrabber::~ScreenshotGrabber() { |
| 120 } | 149 } |
| 121 | 150 |
| 122 void ScreenshotGrabber::TakeScreenshot(gfx::NativeWindow window, | 151 void ScreenshotGrabber::TakeScreenshot(gfx::NativeWindow window, |
| 123 const gfx::Rect& rect, | 152 const gfx::Rect& rect, |
| 124 const base::FilePath& screenshot_path) { | 153 const base::FilePath& screenshot_path) { |
| 125 DCHECK(base::MessageLoopForUI::IsCurrent()); | 154 DCHECK(base::MessageLoopForUI::IsCurrent()); |
| 126 last_screenshot_timestamp_ = base::TimeTicks::Now(); | 155 last_screenshot_timestamp_ = base::TimeTicks::Now(); |
| 127 | 156 |
| 128 bool is_partial = true; | 157 bool is_partial = true; |
| 129 // Window identifier is used to log a message on failure to capture a full | 158 // 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 | 159 // 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. | 160 // false, we will also have an identification string for the window. |
| 132 std::string window_identifier; | 161 std::string window_identifier; |
| 133 #if defined(USE_AURA) | 162 #if defined(USE_AURA) |
| 134 aura::Window* aura_window = static_cast<aura::Window*>(window); | 163 aura::Window* aura_window = static_cast<aura::Window*>(window); |
| 135 is_partial = rect.size() != aura_window->bounds().size(); | 164 is_partial = rect.size() != aura_window->bounds().size(); |
| 136 window_identifier = aura_window->GetBoundsInScreen().ToString(); | 165 window_identifier = aura_window->GetBoundsInScreen().ToString(); |
| 166 | |
| 167 cursor_hider_ = ScopedCursorHider::Get(aura_window->GetRootWindow()); | |
| 137 #endif | 168 #endif |
| 138 ui::GrabWindowSnapshotAsync( | 169 ui::GrabWindowSnapshotAsync( |
| 139 window, rect, blocking_task_runner_, | 170 window, rect, blocking_task_runner_, |
| 140 base::Bind(&ScreenshotGrabber::GrabWindowSnapshotAsyncCallback, | 171 base::Bind(&ScreenshotGrabber::GrabWindowSnapshotAsyncCallback, |
| 141 factory_.GetWeakPtr(), window_identifier, screenshot_path, | 172 factory_.GetWeakPtr(), window_identifier, screenshot_path, |
| 142 is_partial)); | 173 is_partial)); |
| 143 } | 174 } |
| 144 | 175 |
| 145 bool ScreenshotGrabber::CanTakeScreenshot() { | 176 bool ScreenshotGrabber::CanTakeScreenshot() { |
| 146 return last_screenshot_timestamp_.is_null() || | 177 return last_screenshot_timestamp_.is_null() || |
| 147 base::TimeTicks::Now() - last_screenshot_timestamp_ > | 178 base::TimeTicks::Now() - last_screenshot_timestamp_ > |
| 148 base::TimeDelta::FromMilliseconds(kScreenshotMinimumIntervalInMS); | 179 base::TimeDelta::FromMilliseconds(kScreenshotMinimumIntervalInMS); |
| 149 } | 180 } |
| 150 | 181 |
| 151 void ScreenshotGrabber::NotifyScreenshotCompleted( | 182 void ScreenshotGrabber::NotifyScreenshotCompleted( |
| 152 ScreenshotGrabberObserver::Result screenshot_result, | 183 ScreenshotGrabberObserver::Result screenshot_result, |
| 153 const base::FilePath& screenshot_path) { | 184 const base::FilePath& screenshot_path) { |
| 154 DCHECK(base::MessageLoopForUI::IsCurrent()); | 185 DCHECK(base::MessageLoopForUI::IsCurrent()); |
| 186 #if defined(USE_AURA) | |
| 187 if (cursor_hider_) | |
|
sky
2016/07/22 23:35:04
You don't need the conditional here, cursor_hider_
Qiang(Joe) Xu
2016/07/22 23:48:09
Done.
| |
| 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 |