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 #include "ui/snapshot/screenshot_taker.h" |
| 6 |
| 7 #include <climits> |
| 8 #include <string> |
| 9 |
| 10 #include "base/bind.h" |
| 11 #include "base/callback.h" |
| 12 #include "base/files/file_util.h" |
| 13 #include "base/logging.h" |
| 14 #include "base/task_runner.h" |
| 15 #include "base/threading/sequenced_worker_pool.h" |
| 16 #include "base/time/time.h" |
| 17 #include "ui/gfx/image/image.h" |
| 18 #include "ui/snapshot/snapshot.h" |
| 19 |
| 20 #if defined(USE_AURA) |
| 21 #include "ui/aura/window.h" |
| 22 #endif |
| 23 |
| 24 namespace ui { |
| 25 |
| 26 namespace { |
| 27 // The minimum interval between two screenshot commands. It has to be |
| 28 // more than 1000 to prevent the conflict of filenames. |
| 29 const int kScreenshotMinimumIntervalInMS = 1000; |
| 30 |
| 31 typedef base::Callback<void(ScreenshotTakerObserver::Result screenshot_result, |
| 32 const base::FilePath& screenshot_path)> |
| 33 ShowNotificationCallback; |
| 34 |
| 35 void SaveScreenshot(scoped_refptr<base::TaskRunner> ui_task_runner, |
| 36 const ShowNotificationCallback& callback, |
| 37 const base::FilePath& screenshot_path, |
| 38 scoped_refptr<base::RefCountedBytes> png_data, |
| 39 ScreenshotTakerClient::WritableFileResult result, |
| 40 const base::FilePath& local_path) { |
| 41 DCHECK(!screenshot_path.empty()); |
| 42 |
| 43 // Convert WritableFileResult into ScreenshotTakerObserver::Result. |
| 44 ScreenshotTakerObserver::Result screenshot_result = |
| 45 ScreenshotTakerObserver::SCREENSHOT_SUCCESS; |
| 46 switch (result) { |
| 47 case ScreenshotTakerClient::WRITABLE_FILE_SUCCESS: |
| 48 // Successful so far, continue to attempt to take screenshot. |
| 49 break; |
| 50 case ScreenshotTakerClient::WRITABLE_FILE_CHECK_DIR_FAILED: |
| 51 screenshot_result = ScreenshotTakerObserver::SCREENSHOT_CHECK_DIR_FAILED; |
| 52 break; |
| 53 case ScreenshotTakerClient::WRITABLE_FILE_CREATE_DIR_FAILED: |
| 54 screenshot_result = ScreenshotTakerObserver::SCREENSHOT_CREATE_DIR_FAILED; |
| 55 break; |
| 56 case ScreenshotTakerClient::WRITABLE_FILE_CREATE_FAILED: |
| 57 screenshot_result = |
| 58 ScreenshotTakerObserver::SCREENSHOT_CREATE_FILE_FAILED; |
| 59 break; |
| 60 } |
| 61 |
| 62 if (screenshot_result == ScreenshotTakerObserver::SCREENSHOT_SUCCESS && |
| 63 static_cast<size_t>(base::WriteFile( |
| 64 local_path, reinterpret_cast<char*>(&(png_data->data()[0])), |
| 65 static_cast<int>(png_data->size()))) != png_data->size()) { |
| 66 LOG(ERROR) << "Failed to save to " << local_path.value(); |
| 67 screenshot_result = ScreenshotTakerObserver::SCREENSHOT_WRITE_FILE_FAILED; |
| 68 } |
| 69 |
| 70 // Report the result on the UI thread. |
| 71 ui_task_runner->PostTask( |
| 72 FROM_HERE, base::Bind(callback, screenshot_result, screenshot_path)); |
| 73 } |
| 74 |
| 75 void EnsureLocalDirectoryExists( |
| 76 const base::FilePath& path, |
| 77 scoped_refptr<base::TaskRunner> ui_task_runner, |
| 78 ScreenshotTakerClient::WritableFileCallback callback) { |
| 79 DCHECK(!path.empty()); |
| 80 |
| 81 if (!base::CreateDirectory(path.DirName())) { |
| 82 LOG(ERROR) << "Failed to ensure the existence of " |
| 83 << path.DirName().value(); |
| 84 ui_task_runner->PostTask( |
| 85 FROM_HERE, |
| 86 base::Bind(callback, |
| 87 ScreenshotTakerClient::WRITABLE_FILE_CREATE_DIR_FAILED, |
| 88 path)); |
| 89 return; |
| 90 } |
| 91 |
| 92 callback.Run(ScreenshotTakerClient::WRITABLE_FILE_SUCCESS, path); |
| 93 } |
| 94 |
| 95 } // namespace |
| 96 |
| 97 void ScreenshotTakerClient::PrepareWritableFileAndRunOnBlockingPool( |
| 98 const base::FilePath& path, |
| 99 scoped_refptr<base::TaskRunner> blocking_task_runner, |
| 100 WritableFileCallback callback_on_blocking_pool) { |
| 101 blocking_task_runner->PostTask( |
| 102 FROM_HERE, base::Bind(EnsureLocalDirectoryExists, path, |
| 103 blocking_task_runner, callback_on_blocking_pool)); |
| 104 } |
| 105 |
| 106 ScreenshotTaker::ScreenshotTaker( |
| 107 ScreenshotTakerClient* client, |
| 108 scoped_refptr<base::TaskRunner> blocking_task_runner) |
| 109 : client_(client), |
| 110 blocking_task_runner_(blocking_task_runner), |
| 111 factory_(this) { |
| 112 } |
| 113 |
| 114 ScreenshotTaker::~ScreenshotTaker() { |
| 115 } |
| 116 |
| 117 void ScreenshotTaker::TakeScreenshot(gfx::NativeWindow window, |
| 118 const gfx::Rect& rect, |
| 119 const base::FilePath& screenshot_path) { |
| 120 DCHECK(base::MessageLoopForUI::IsCurrent()); |
| 121 last_screenshot_timestamp_ = base::Time::Now(); |
| 122 |
| 123 bool is_partial = true; |
| 124 // Window identifier is used to log a message on failure to capture a full |
| 125 // screen (i.e. non partial) screenshot. The only time is_partial can be |
| 126 // false, we will also have an identification string for the window. |
| 127 std::string window_identifier; |
| 128 #if defined(USE_AURA) |
| 129 aura::Window* aura_window = static_cast<aura::Window*>(window); |
| 130 is_partial = rect == aura_window->bounds(); |
| 131 window_identifier = aura_window->GetBoundsInScreen().ToString(); |
| 132 #endif |
| 133 ui::GrabWindowSnapshotAsync( |
| 134 window, rect, blocking_task_runner_, |
| 135 base::Bind(&ScreenshotTaker::GrabWindowSnapshotAsyncCallback, |
| 136 factory_.GetWeakPtr(), window_identifier, screenshot_path, |
| 137 is_partial)); |
| 138 } |
| 139 |
| 140 bool ScreenshotTaker::CanTakeScreenshot() { |
| 141 return last_screenshot_timestamp_.is_null() || |
| 142 base::Time::Now() - last_screenshot_timestamp_ > |
| 143 base::TimeDelta::FromMilliseconds(kScreenshotMinimumIntervalInMS); |
| 144 } |
| 145 |
| 146 void ScreenshotTaker::NotifyScreenshotCompleted( |
| 147 ScreenshotTakerObserver::Result screenshot_result, |
| 148 const base::FilePath& screenshot_path) { |
| 149 DCHECK(base::MessageLoopForUI::IsCurrent()); |
| 150 FOR_EACH_OBSERVER(ScreenshotTakerObserver, observers_, |
| 151 OnScreenshotCompleted(screenshot_result, screenshot_path)); |
| 152 } |
| 153 |
| 154 void ScreenshotTaker::AddObserver(ScreenshotTakerObserver* observer) { |
| 155 observers_.AddObserver(observer); |
| 156 } |
| 157 |
| 158 void ScreenshotTaker::RemoveObserver(ScreenshotTakerObserver* observer) { |
| 159 observers_.RemoveObserver(observer); |
| 160 } |
| 161 |
| 162 bool ScreenshotTaker::HasObserver( |
| 163 const ScreenshotTakerObserver* observer) const { |
| 164 return observers_.HasObserver(observer); |
| 165 } |
| 166 |
| 167 void ScreenshotTaker::GrabWindowSnapshotAsyncCallback( |
| 168 const std::string& window_identifier, |
| 169 base::FilePath screenshot_path, |
| 170 bool is_partial, |
| 171 scoped_refptr<base::RefCountedBytes> png_data) { |
| 172 if (!png_data.get()) { |
| 173 if (is_partial) { |
| 174 LOG(ERROR) << "Failed to grab the window screenshot"; |
| 175 NotifyScreenshotCompleted( |
| 176 ScreenshotTakerObserver::SCREENSHOT_GRABWINDOW_PARTIAL_FAILED, |
| 177 screenshot_path); |
| 178 } else { |
| 179 LOG(ERROR) << "Failed to grab the window screenshot for " |
| 180 << window_identifier; |
| 181 NotifyScreenshotCompleted( |
| 182 ScreenshotTakerObserver::SCREENSHOT_GRABWINDOW_FULL_FAILED, |
| 183 screenshot_path); |
| 184 } |
| 185 return; |
| 186 } |
| 187 |
| 188 client_->PrepareWritableFileAndRunOnBlockingPool( |
| 189 screenshot_path, blocking_task_runner_, |
| 190 base::Bind(&SaveScreenshot, base::MessageLoop::current()->task_runner(), |
| 191 base::Bind(&ScreenshotTaker::NotifyScreenshotCompleted, |
| 192 factory_.GetWeakPtr()), |
| 193 screenshot_path, png_data)); |
| 194 } |
| 195 |
| 196 } // namespace ui |
OLD | NEW |