| 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> | 
| (...skipping 25 matching lines...) Expand all  Loading... | 
| 36 // more than 1000 to prevent the conflict of filenames. | 36 // more than 1000 to prevent the conflict of filenames. | 
| 37 const int kScreenshotMinimumIntervalInMS = 1000; | 37 const int kScreenshotMinimumIntervalInMS = 1000; | 
| 38 | 38 | 
| 39 using ShowNotificationCallback = | 39 using ShowNotificationCallback = | 
| 40     base::Callback<void(ScreenshotGrabberObserver::Result screenshot_result, | 40     base::Callback<void(ScreenshotGrabberObserver::Result screenshot_result, | 
| 41                         const base::FilePath& screenshot_path)>; | 41                         const base::FilePath& screenshot_path)>; | 
| 42 | 42 | 
| 43 void SaveScreenshot(scoped_refptr<base::TaskRunner> ui_task_runner, | 43 void SaveScreenshot(scoped_refptr<base::TaskRunner> ui_task_runner, | 
| 44                     const ShowNotificationCallback& callback, | 44                     const ShowNotificationCallback& callback, | 
| 45                     const base::FilePath& screenshot_path, | 45                     const base::FilePath& screenshot_path, | 
| 46                     scoped_refptr<base::RefCountedBytes> png_data, | 46                     scoped_refptr<base::RefCountedMemory> png_data, | 
| 47                     ScreenshotGrabberDelegate::FileResult result, | 47                     ScreenshotGrabberDelegate::FileResult result, | 
| 48                     const base::FilePath& local_path) { | 48                     const base::FilePath& local_path) { | 
| 49   DCHECK(!base::MessageLoopForUI::IsCurrent()); | 49   DCHECK(!base::MessageLoopForUI::IsCurrent()); | 
| 50   DCHECK(!screenshot_path.empty()); | 50   DCHECK(!screenshot_path.empty()); | 
| 51 | 51 | 
| 52   // Convert FileResult into ScreenshotGrabberObserver::Result. | 52   // Convert FileResult into ScreenshotGrabberObserver::Result. | 
| 53   ScreenshotGrabberObserver::Result screenshot_result = | 53   ScreenshotGrabberObserver::Result screenshot_result = | 
| 54       ScreenshotGrabberObserver::SCREENSHOT_SUCCESS; | 54       ScreenshotGrabberObserver::SCREENSHOT_SUCCESS; | 
| 55   switch (result) { | 55   switch (result) { | 
| 56     case ScreenshotGrabberDelegate::FILE_SUCCESS: | 56     case ScreenshotGrabberDelegate::FILE_SUCCESS: | 
| 57       // Successfully got a local file to write to, write png data. | 57       // Successfully got a local file to write to, write png data. | 
| 58       DCHECK_GT(static_cast<int>(png_data->size()), 0); | 58       DCHECK_GT(static_cast<int>(png_data->size()), 0); | 
| 59       if (static_cast<size_t>(base::WriteFile( | 59       if (static_cast<size_t>(base::WriteFile( | 
| 60               local_path, reinterpret_cast<char*>(&(png_data->data()[0])), | 60               local_path, reinterpret_cast<const char*>(png_data->front()), | 
| 61               static_cast<int>(png_data->size()))) != png_data->size()) { | 61               static_cast<int>(png_data->size()))) != png_data->size()) { | 
| 62         LOG(ERROR) << "Failed to save to " << local_path.value(); | 62         LOG(ERROR) << "Failed to save to " << local_path.value(); | 
| 63         screenshot_result = | 63         screenshot_result = | 
| 64             ScreenshotGrabberObserver::SCREENSHOT_WRITE_FILE_FAILED; | 64             ScreenshotGrabberObserver::SCREENSHOT_WRITE_FILE_FAILED; | 
| 65       } | 65       } | 
| 66       break; | 66       break; | 
| 67     case ScreenshotGrabberDelegate::FILE_CHECK_DIR_FAILED: | 67     case ScreenshotGrabberDelegate::FILE_CHECK_DIR_FAILED: | 
| 68       screenshot_result = | 68       screenshot_result = | 
| 69           ScreenshotGrabberObserver::SCREENSHOT_CHECK_DIR_FAILED; | 69           ScreenshotGrabberObserver::SCREENSHOT_CHECK_DIR_FAILED; | 
| 70       break; | 70       break; | 
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 161   // screen (i.e. non partial) screenshot. The only time is_partial can be | 161   // screen (i.e. non partial) screenshot. The only time is_partial can be | 
| 162   // false, we will also have an identification string for the window. | 162   // false, we will also have an identification string for the window. | 
| 163   std::string window_identifier; | 163   std::string window_identifier; | 
| 164 #if defined(USE_AURA) | 164 #if defined(USE_AURA) | 
| 165   aura::Window* aura_window = static_cast<aura::Window*>(window); | 165   aura::Window* aura_window = static_cast<aura::Window*>(window); | 
| 166   is_partial = rect.size() != aura_window->bounds().size(); | 166   is_partial = rect.size() != aura_window->bounds().size(); | 
| 167   window_identifier = aura_window->GetBoundsInScreen().ToString(); | 167   window_identifier = aura_window->GetBoundsInScreen().ToString(); | 
| 168 | 168 | 
| 169   cursor_hider_ = ScopedCursorHider::Create(aura_window->GetRootWindow()); | 169   cursor_hider_ = ScopedCursorHider::Create(aura_window->GetRootWindow()); | 
| 170 #endif | 170 #endif | 
| 171   ui::GrabWindowSnapshotAsync( | 171   ui::GrabWindowSnapshotAsyncPNG( | 
| 172       window, rect, blocking_task_runner_, | 172       window, rect, blocking_task_runner_, | 
| 173       base::Bind(&ScreenshotGrabber::GrabWindowSnapshotAsyncCallback, | 173       base::Bind(&ScreenshotGrabber::GrabWindowSnapshotAsyncCallback, | 
| 174                  factory_.GetWeakPtr(), window_identifier, screenshot_path, | 174                  factory_.GetWeakPtr(), window_identifier, screenshot_path, | 
| 175                  is_partial)); | 175                  is_partial)); | 
| 176 } | 176 } | 
| 177 | 177 | 
| 178 bool ScreenshotGrabber::CanTakeScreenshot() { | 178 bool ScreenshotGrabber::CanTakeScreenshot() { | 
| 179   return last_screenshot_timestamp_.is_null() || | 179   return last_screenshot_timestamp_.is_null() || | 
| 180          base::TimeTicks::Now() - last_screenshot_timestamp_ > | 180          base::TimeTicks::Now() - last_screenshot_timestamp_ > | 
| 181              base::TimeDelta::FromMilliseconds(kScreenshotMinimumIntervalInMS); | 181              base::TimeDelta::FromMilliseconds(kScreenshotMinimumIntervalInMS); | 
| (...skipping 20 matching lines...) Expand all  Loading... | 
| 202 | 202 | 
| 203 bool ScreenshotGrabber::HasObserver( | 203 bool ScreenshotGrabber::HasObserver( | 
| 204     const ScreenshotGrabberObserver* observer) const { | 204     const ScreenshotGrabberObserver* observer) const { | 
| 205   return observers_.HasObserver(observer); | 205   return observers_.HasObserver(observer); | 
| 206 } | 206 } | 
| 207 | 207 | 
| 208 void ScreenshotGrabber::GrabWindowSnapshotAsyncCallback( | 208 void ScreenshotGrabber::GrabWindowSnapshotAsyncCallback( | 
| 209     const std::string& window_identifier, | 209     const std::string& window_identifier, | 
| 210     base::FilePath screenshot_path, | 210     base::FilePath screenshot_path, | 
| 211     bool is_partial, | 211     bool is_partial, | 
| 212     scoped_refptr<base::RefCountedBytes> png_data) { | 212     scoped_refptr<base::RefCountedMemory> png_data) { | 
| 213   DCHECK(base::MessageLoopForUI::IsCurrent()); | 213   DCHECK(base::MessageLoopForUI::IsCurrent()); | 
| 214   if (!png_data.get()) { | 214   if (!png_data.get()) { | 
| 215     if (is_partial) { | 215     if (is_partial) { | 
| 216       LOG(ERROR) << "Failed to grab the window screenshot"; | 216       LOG(ERROR) << "Failed to grab the window screenshot"; | 
| 217       NotifyScreenshotCompleted( | 217       NotifyScreenshotCompleted( | 
| 218           ScreenshotGrabberObserver::SCREENSHOT_GRABWINDOW_PARTIAL_FAILED, | 218           ScreenshotGrabberObserver::SCREENSHOT_GRABWINDOW_PARTIAL_FAILED, | 
| 219           screenshot_path); | 219           screenshot_path); | 
| 220     } else { | 220     } else { | 
| 221       LOG(ERROR) << "Failed to grab the window screenshot for " | 221       LOG(ERROR) << "Failed to grab the window screenshot for " | 
| 222                  << window_identifier; | 222                  << window_identifier; | 
| 223       NotifyScreenshotCompleted( | 223       NotifyScreenshotCompleted( | 
| 224           ScreenshotGrabberObserver::SCREENSHOT_GRABWINDOW_FULL_FAILED, | 224           ScreenshotGrabberObserver::SCREENSHOT_GRABWINDOW_FULL_FAILED, | 
| 225           screenshot_path); | 225           screenshot_path); | 
| 226     } | 226     } | 
| 227     return; | 227     return; | 
| 228   } | 228   } | 
| 229 | 229 | 
| 230   ShowNotificationCallback notification_callback(base::Bind( | 230   ShowNotificationCallback notification_callback(base::Bind( | 
| 231       &ScreenshotGrabber::NotifyScreenshotCompleted, factory_.GetWeakPtr())); | 231       &ScreenshotGrabber::NotifyScreenshotCompleted, factory_.GetWeakPtr())); | 
| 232   client_->PrepareFileAndRunOnBlockingPool( | 232   client_->PrepareFileAndRunOnBlockingPool( | 
| 233       screenshot_path, blocking_task_runner_, | 233       screenshot_path, blocking_task_runner_, | 
| 234       base::Bind(&SaveScreenshot, base::ThreadTaskRunnerHandle::Get(), | 234       base::Bind(&SaveScreenshot, base::ThreadTaskRunnerHandle::Get(), | 
| 235                  notification_callback, screenshot_path, png_data)); | 235                  notification_callback, screenshot_path, png_data)); | 
| 236 } | 236 } | 
| 237 | 237 | 
| 238 }  // namespace ui | 238 }  // namespace ui | 
| OLD | NEW | 
|---|