 Chromium Code Reviews
 Chromium Code Reviews Issue 13105002:
  Screenshot effect non-obvious  (Closed) 
  Base URL: http://git.chromium.org/chromium/src.git@master
    
  
    Issue 13105002:
  Screenshot effect non-obvious  (Closed) 
  Base URL: http://git.chromium.org/chromium/src.git@master| OLD | NEW | 
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "chrome/browser/ui/ash/screenshot_taker.h" | 5 #include "chrome/browser/ui/ash/screenshot_taker.h" | 
| 6 | 6 | 
| 7 #include <climits> | 7 #include <climits> | 
| 8 #include <string> | 8 #include <string> | 
| 9 | 9 | 
| 10 #include "ash/shell.h" | 10 #include "ash/shell.h" | 
| 11 #include "ash/shell_delegate.h" | 11 #include "ash/shell_delegate.h" | 
| 12 #include "ash/shell_window_ids.h" | 12 #include "ash/shell_window_ids.h" | 
| 13 #include "base/bind.h" | 13 #include "base/bind.h" | 
| 14 #include "base/file_util.h" | 14 #include "base/file_util.h" | 
| 15 #include "base/files/file_path.h" | |
| 16 #include "base/logging.h" | 15 #include "base/logging.h" | 
| 17 #include "base/memory/ref_counted_memory.h" | 16 #include "base/memory/ref_counted_memory.h" | 
| 18 #include "base/stringprintf.h" | 17 #include "base/stringprintf.h" | 
| 19 #include "base/threading/sequenced_worker_pool.h" | 18 #include "base/threading/sequenced_worker_pool.h" | 
| 20 #include "base/time.h" | 19 #include "base/time.h" | 
| 20 #include "base/utf_string_conversions.h" | |
| 21 #include "chrome/browser/browser_process.h" | |
| 22 #include "chrome/browser/chromeos/extensions/file_manager_util.h" | |
| 21 #include "chrome/browser/download/download_prefs.h" | 23 #include "chrome/browser/download/download_prefs.h" | 
| 24 #include "chrome/browser/notifications/notification.h" | |
| 25 #include "chrome/browser/notifications/notification_ui_manager.h" | |
| 22 #include "chrome/browser/profiles/profile.h" | 26 #include "chrome/browser/profiles/profile.h" | 
| 23 #include "chrome/browser/profiles/profile_manager.h" | 27 #include "chrome/browser/profiles/profile_manager.h" | 
| 24 #include "chrome/browser/ui/webui/screenshot_source.h" | 28 #include "chrome/browser/ui/webui/screenshot_source.h" | 
| 25 #include "chrome/browser/ui/window_snapshot/window_snapshot.h" | 29 #include "chrome/browser/ui/window_snapshot/window_snapshot.h" | 
| 26 #include "content/public/browser/browser_thread.h" | 30 #include "content/public/browser/browser_thread.h" | 
| 31 #include "grit/ash_strings.h" | |
| 32 #include "grit/theme_resources.h" | |
| 27 #include "ui/aura/root_window.h" | 33 #include "ui/aura/root_window.h" | 
| 28 #include "ui/aura/window.h" | 34 #include "ui/aura/window.h" | 
| 35 #include "ui/base/l10n/l10n_util.h" | |
| 36 #include "ui/base/resource/resource_bundle.h" | |
| 37 #include "ui/gfx/image/image.h" | |
| 29 | 38 | 
| 30 #if defined(OS_CHROMEOS) | 39 #if defined(OS_CHROMEOS) | 
| 31 #include "chrome/browser/chromeos/drive/drive_file_system_util.h" | 40 #include "chrome/browser/chromeos/drive/drive_file_system_util.h" | 
| 32 #include "chrome/browser/chromeos/login/user_manager.h" | 41 #include "chrome/browser/chromeos/login/user_manager.h" | 
| 33 #endif | 42 #endif | 
| 34 | 43 | 
| 35 namespace { | 44 namespace { | 
| 36 // How opaque should the layer that we flash onscreen to provide visual | |
| 37 // feedback after the screenshot is taken be? | |
| 38 const float kVisualFeedbackLayerOpacity = 0.25f; | |
| 39 | |
| 40 // How long should the visual feedback layer be displayed? | |
| 41 const int64 kVisualFeedbackLayerDisplayTimeMs = 100; | |
| 42 | |
| 43 // The minimum interval between two screenshot commands. It has to be | 45 // The minimum interval between two screenshot commands. It has to be | 
| 44 // more than 1000 to prevent the conflict of filenames. | 46 // more than 1000 to prevent the conflict of filenames. | 
| 45 const int kScreenshotMinimumIntervalInMS = 1000; | 47 const int kScreenshotMinimumIntervalInMS = 1000; | 
| 46 | 48 | 
| 49 const char kNotificationOriginUrl[] = "chrome://screenshot"; | |
| 50 | |
| 51 class ScreenshotTakerNotificationDelegate : public NotificationDelegate { | |
| 52 public: | |
| 53 ScreenshotTakerNotificationDelegate(const std::string& id_text, | |
| 54 bool success, | |
| 55 const base::FilePath& screenshot_path) | |
| 56 : id_text_(id_text), | |
| 57 success_(success), | |
| 58 screenshot_path_(screenshot_path) { | |
| 59 } | |
| 60 | |
| 61 virtual void Display() OVERRIDE {} | |
| 62 | |
| 63 virtual void Error() OVERRIDE {} | |
| 64 | |
| 65 virtual void Close(bool by_user) OVERRIDE {} | |
| 66 | |
| 67 virtual void Click() OVERRIDE { | |
| 68 if (success_) | |
| 69 file_manager_util::ShowFileInFolder(screenshot_path_); | |
| 70 } | |
| 71 | |
| 72 virtual std::string id() const OVERRIDE { return id_text_; } | |
| 73 | |
| 74 virtual content::RenderViewHost* GetRenderViewHost() const OVERRIDE { | |
| 75 return NULL; | |
| 76 } | |
| 77 | |
| 78 private: | |
| 79 virtual ~ScreenshotTakerNotificationDelegate() {} | |
| 80 | |
| 81 std::string id_text_; | |
| 
sky
2013/03/31 01:16:03
Make all of these const if they can be.
 
sschmitz
2013/04/02 21:23:25
Done.
 | |
| 82 bool success_; | |
| 83 base::FilePath screenshot_path_; | |
| 84 | |
| 85 DISALLOW_COPY_AND_ASSIGN(ScreenshotTakerNotificationDelegate); | |
| 86 }; | |
| 87 | |
| 88 void ShowNotification(ScreenshotTakerObserver::Result screenshot_result, | |
| 89 const base::FilePath& screenshot_path) { | |
| 90 #if defined(OS_CHROMEOS) | |
| 91 // Notifications only on Chrome OS. | |
| 
sky
2013/03/31 01:16:03
Why is the notification only for chromeos?
 
sschmitz
2013/04/02 21:23:25
Removed.
 | |
| 92 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
| 93 static int id = 0; | |
| 94 std::string id_text = base::StringPrintf("screenshot_%3.3d", ++id); | |
| 95 string16 replace_id = UTF8ToUTF16(id_text); | |
| 96 bool success = | |
| 97 (screenshot_result == ScreenshotTakerObserver::SCREENSHOT_SUCCESS); | |
| 98 Notification notification( | |
| 99 GURL(kNotificationOriginUrl), | |
| 100 ui::ResourceBundle::GetSharedInstance().GetImageNamed( | |
| 101 IDR_SCREENSHOT_NOTIFICATION_ICON), | |
| 102 l10n_util::GetStringUTF16( | |
| 103 success ? | |
| 104 IDS_ASH_SCREENSHOT_NOTIFICATION_TITLE_SUCCESS : | |
| 105 IDS_ASH_SCREENSHOT_NOTIFICATION_TITLE_FAIL), | |
| 106 l10n_util::GetStringUTF16( | |
| 107 success ? | |
| 108 IDS_ASH_SCREENSHOT_NOTIFICATION_TEXT_SUCCESS : | |
| 109 IDS_ASH_SCREENSHOT_NOTIFICATION_TEXT_FAIL), | |
| 110 WebKit::WebTextDirectionDefault, | |
| 111 string16(), | |
| 112 replace_id, | |
| 113 new ScreenshotTakerNotificationDelegate(id_text, | |
| 114 success, | |
| 115 screenshot_path)); | |
| 116 Profile* profile = ProfileManager::GetDefaultProfileOrOffTheRecord(); | |
| 
sky
2013/03/31 01:16:03
Can ScreenshotTaker so that we minimize uses of Ge
 
sschmitz
2013/04/02 21:23:25
Changed: ScreenshotTake takes profile in ctor.
 | |
| 117 DCHECK(profile); | |
| 118 g_browser_process->notification_ui_manager()->Add(notification, profile); | |
| 119 | |
| 120 if (ScreenshotTaker::GetObserverForTest()) { | |
| 
sky
2013/03/31 01:16:03
Why is this in the ifdef?
 
sschmitz
2013/04/02 21:23:25
Removed.
 | |
| 121 ScreenshotTaker::GetObserverForTest()->OnScreenshotCompleted( | |
| 122 screenshot_result, screenshot_path); | |
| 123 } | |
| 124 #endif | |
| 125 } | |
| 126 | |
| 127 void PostShowNotification(ScreenshotTakerObserver::Result screenshot_result, | |
| 128 const base::FilePath& screenshot_path) { | |
| 129 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); | |
| 130 content::BrowserThread::PostTask( | |
| 131 content::BrowserThread::UI, FROM_HERE, | |
| 132 base::Bind(&ShowNotification, screenshot_result, screenshot_path)); | |
| 133 } | |
| 47 | 134 | 
| 48 void SaveScreenshotInternal(const base::FilePath& screenshot_path, | 135 void SaveScreenshotInternal(const base::FilePath& screenshot_path, | 
| 49 scoped_refptr<base::RefCountedBytes> png_data) { | 136 scoped_refptr<base::RefCountedBytes> png_data) { | 
| 50 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); | 137 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); | 
| 51 DCHECK(!screenshot_path.empty()); | 138 DCHECK(!screenshot_path.empty()); | 
| 52 if (static_cast<size_t>(file_util::WriteFile( | 139 if (static_cast<size_t>(file_util::WriteFile( | 
| 53 screenshot_path, | 140 screenshot_path, | 
| 54 reinterpret_cast<char*>(&(png_data->data()[0])), | 141 reinterpret_cast<char*>(&(png_data->data()[0])), | 
| 55 png_data->size())) != png_data->size()) { | 142 png_data->size())) != png_data->size()) { | 
| 56 LOG(ERROR) << "Failed to save to " << screenshot_path.value(); | 143 LOG(ERROR) << "Failed to save to " << screenshot_path.value(); | 
| 144 PostShowNotification(ScreenshotTakerObserver::SCREENSHOT_WRITE_FILE_FAILED, | |
| 145 screenshot_path); | |
| 146 return; | |
| 57 } | 147 } | 
| 148 PostShowNotification(ScreenshotTakerObserver::SCREENSHOT_SUCCESS, | |
| 149 screenshot_path); | |
| 58 } | 150 } | 
| 59 | 151 | 
| 60 void SaveScreenshot(const base::FilePath& screenshot_path, | 152 void SaveScreenshot(const base::FilePath& screenshot_path, | 
| 61 scoped_refptr<base::RefCountedBytes> png_data) { | 153 scoped_refptr<base::RefCountedBytes> png_data) { | 
| 62 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); | 154 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); | 
| 63 DCHECK(!screenshot_path.empty()); | 155 DCHECK(!screenshot_path.empty()); | 
| 64 | 156 | 
| 65 if (!file_util::CreateDirectory(screenshot_path.DirName())) { | 157 if (!file_util::CreateDirectory(screenshot_path.DirName())) { | 
| 66 LOG(ERROR) << "Failed to ensure the existence of " | 158 LOG(ERROR) << "Failed to ensure the existence of " | 
| 67 << screenshot_path.DirName().value(); | 159 << screenshot_path.DirName().value(); | 
| 160 PostShowNotification(ScreenshotTakerObserver::SCREENSHOT_CREATE_DIR_FAILED, | |
| 161 screenshot_path); | |
| 68 return; | 162 return; | 
| 69 } | 163 } | 
| 70 SaveScreenshotInternal(screenshot_path, png_data); | 164 SaveScreenshotInternal(screenshot_path, png_data); | 
| 71 } | 165 } | 
| 72 | 166 | 
| 73 // TODO(kinaba): crbug.com/140425, remove this ungly #ifdef dispatch. | 167 // TODO(kinaba): crbug.com/140425, remove this ungly #ifdef dispatch. | 
| 74 #if defined(OS_CHROMEOS) | 168 #if defined(OS_CHROMEOS) | 
| 75 void SaveScreenshotToDrive(scoped_refptr<base::RefCountedBytes> png_data, | 169 void SaveScreenshotToDrive(scoped_refptr<base::RefCountedBytes> png_data, | 
| 76 drive::DriveFileError error, | 170 drive::DriveFileError error, | 
| 77 const base::FilePath& local_path) { | 171 const base::FilePath& local_path) { | 
| 78 if (error != drive::DRIVE_FILE_OK) { | 172 if (error != drive::DRIVE_FILE_OK) { | 
| 79 LOG(ERROR) << "Failed to write screenshot image to Google Drive: " << error; | 173 LOG(ERROR) << "Failed to write screenshot image to Google Drive: " << error; | 
| 174 PostShowNotification( | |
| 175 ScreenshotTakerObserver::SCREENSHOT_CREATE_FILE_FAILED, local_path); | |
| 80 return; | 176 return; | 
| 81 } | 177 } | 
| 82 SaveScreenshotInternal(local_path, png_data); | 178 SaveScreenshotInternal(local_path, png_data); | 
| 83 } | 179 } | 
| 84 | 180 | 
| 85 void EnsureDirectoryExistsCallback( | 181 void EnsureDirectoryExistsCallback( | 
| 86 Profile* profile, | 182 Profile* profile, | 
| 87 const base::FilePath& screenshot_path, | 183 const base::FilePath& screenshot_path, | 
| 88 scoped_refptr<base::RefCountedBytes> png_data, | 184 scoped_refptr<base::RefCountedBytes> png_data, | 
| 89 drive::DriveFileError error) { | 185 drive::DriveFileError error) { | 
| 90 // It is okay to fail with DRIVE_FILE_ERROR_EXISTS since anyway the directory | 186 // It is okay to fail with DRIVE_FILE_ERROR_EXISTS since anyway the directory | 
| 91 // of the target file exists. | 187 // of the target file exists. | 
| 92 if (error == drive::DRIVE_FILE_OK || | 188 if (error == drive::DRIVE_FILE_OK || | 
| 93 error == drive::DRIVE_FILE_ERROR_EXISTS) { | 189 error == drive::DRIVE_FILE_ERROR_EXISTS) { | 
| 190 // Note: The last two arguments of SaveScreenshotToDrive are appended by | |
| 191 // PrepareWritableFileAndRun. | |
| 94 drive::util::PrepareWritableFileAndRun( | 192 drive::util::PrepareWritableFileAndRun( | 
| 95 profile, | 193 profile, | 
| 96 screenshot_path, | 194 screenshot_path, | 
| 97 base::Bind(&SaveScreenshotToDrive, png_data)); | 195 base::Bind(&SaveScreenshotToDrive, png_data)); | 
| 98 } else { | 196 } else { | 
| 99 LOG(ERROR) << "Failed to ensure the existence of the specified directory " | 197 LOG(ERROR) << "Failed to ensure the existence of the specified directory " | 
| 100 << "in Google Drive: " << error; | 198 << "in Google Drive: " << error; | 
| 199 ShowNotification(ScreenshotTakerObserver::SCREENSHOT_CHECK_DIR_FAILED, | |
| 200 screenshot_path); | |
| 101 } | 201 } | 
| 102 } | 202 } | 
| 103 | 203 | 
| 104 void PostSaveScreenshotTask(const base::FilePath& screenshot_path, | 204 void PostSaveScreenshotTask(const base::FilePath& screenshot_path, | 
| 105 scoped_refptr<base::RefCountedBytes> png_data) { | 205 scoped_refptr<base::RefCountedBytes> png_data) { | 
| 106 if (drive::util::IsUnderDriveMountPoint(screenshot_path)) { | 206 if (drive::util::IsUnderDriveMountPoint(screenshot_path)) { | 
| 107 Profile* profile = ProfileManager::GetDefaultProfileOrOffTheRecord(); | 207 Profile* profile = ProfileManager::GetDefaultProfileOrOffTheRecord(); | 
| 108 if (profile) { | 208 if (profile) { | 
| 209 // Note: The last argument of EnsureDirectoryExistsCallback is | |
| 210 // appended by EnsureDirectoryExists. | |
| 109 drive::util::EnsureDirectoryExists( | 211 drive::util::EnsureDirectoryExists( | 
| 110 profile, | 212 profile, | 
| 111 screenshot_path.DirName(), | 213 screenshot_path.DirName(), | 
| 112 base::Bind(&EnsureDirectoryExistsCallback, | 214 base::Bind(&EnsureDirectoryExistsCallback, | 
| 113 profile, | 215 profile, | 
| 114 screenshot_path, | 216 screenshot_path, | 
| 115 png_data)); | 217 png_data)); | 
| 116 } | 218 } | 
| 117 } else { | 219 } else { | 
| 118 content::BrowserThread::GetBlockingPool()->PostTask( | 220 content::BrowserThread::GetBlockingPool()->PostTask( | 
| (...skipping 22 matching lines...) Expand all Loading... | |
| 141 // TODO(mukai): remove this when the performance issue has been fixed. | 243 // TODO(mukai): remove this when the performance issue has been fixed. | 
| 142 if (window->GetRootWindow()->GrabSnapshot(snapshot_bounds, png_data)) | 244 if (window->GetRootWindow()->GrabSnapshot(snapshot_bounds, png_data)) | 
| 143 return true; | 245 return true; | 
| 144 #endif // OS_LINUX | 246 #endif // OS_LINUX | 
| 145 | 247 | 
| 146 return chrome::GrabWindowSnapshotForUser(window, png_data, snapshot_bounds); | 248 return chrome::GrabWindowSnapshotForUser(window, png_data, snapshot_bounds); | 
| 147 } | 249 } | 
| 148 | 250 | 
| 149 } // namespace | 251 } // namespace | 
| 150 | 252 | 
| 253 ScreenshotTakerObserver* ScreenshotTaker::observer_for_test_ = NULL; | |
| 254 | |
| 151 ScreenshotTaker::ScreenshotTaker() { | 255 ScreenshotTaker::ScreenshotTaker() { | 
| 152 } | 256 } | 
| 153 | 257 | 
| 154 ScreenshotTaker::~ScreenshotTaker() { | 258 ScreenshotTaker::~ScreenshotTaker() { | 
| 155 } | 259 } | 
| 156 | 260 | 
| 157 void ScreenshotTaker::HandleTakeScreenshotForAllRootWindows() { | 261 void ScreenshotTaker::HandleTakeScreenshotForAllRootWindows() { | 
| 158 base::FilePath screenshot_directory; | 262 base::FilePath screenshot_directory; | 
| 159 if (!ScreenshotSource::GetScreenshotDirectory(&screenshot_directory)) | 263 if (!screenshot_directory_for_test_.empty()) { | 
| 264 screenshot_directory = screenshot_directory_for_test_; | |
| 265 } else if (!ScreenshotSource::GetScreenshotDirectory(&screenshot_directory)) { | |
| 266 ShowNotification(ScreenshotTakerObserver::SCREENSHOT_GET_DIR_FAILED, | |
| 267 base::FilePath()); | |
| 160 return; | 268 return; | 
| 269 } | |
| 161 | 270 | 
| 162 std::string screenshot_basename = | 271 std::string screenshot_basename = | 
| 163 ScreenshotSource::GetScreenshotBaseFilename(); | 272 ScreenshotSource::GetScreenshotBaseFilename(); | 
| 164 ash::Shell::RootWindowList root_windows = ash::Shell::GetAllRootWindows(); | 273 ash::Shell::RootWindowList root_windows = ash::Shell::GetAllRootWindows(); | 
| 165 // Reorder root_windows to take the primary root window's snapshot at first. | 274 // Reorder root_windows to take the primary root window's snapshot at first. | 
| 166 aura::RootWindow* primary_root = ash::Shell::GetPrimaryRootWindow(); | 275 aura::RootWindow* primary_root = ash::Shell::GetPrimaryRootWindow(); | 
| 167 if (*(root_windows.begin()) != primary_root) { | 276 if (*(root_windows.begin()) != primary_root) { | 
| 168 root_windows.erase(std::find( | 277 root_windows.erase(std::find( | 
| 169 root_windows.begin(), root_windows.end(), primary_root)); | 278 root_windows.begin(), root_windows.end(), primary_root)); | 
| 170 root_windows.insert(root_windows.begin(), primary_root); | 279 root_windows.insert(root_windows.begin(), primary_root); | 
| 171 } | 280 } | 
| 172 for (size_t i = 0; i < root_windows.size(); ++i) { | 281 for (size_t i = 0; i < root_windows.size(); ++i) { | 
| 173 aura::RootWindow* root_window = root_windows[i]; | 282 aura::RootWindow* root_window = root_windows[i]; | 
| 174 scoped_refptr<base::RefCountedBytes> png_data(new base::RefCountedBytes); | 283 scoped_refptr<base::RefCountedBytes> png_data(new base::RefCountedBytes); | 
| 175 std::string basename = screenshot_basename; | 284 std::string basename = screenshot_basename; | 
| 176 gfx::Rect rect = root_window->bounds(); | 285 gfx::Rect rect = root_window->bounds(); | 
| 177 if (root_windows.size() > 1) | 286 if (root_windows.size() > 1) | 
| 178 basename += base::StringPrintf(" - Display %d", static_cast<int>(i + 1)); | 287 basename += base::StringPrintf(" - Display %d", static_cast<int>(i + 1)); | 
| 288 base::FilePath screenshot_path = | |
| 289 screenshot_directory.AppendASCII(basename + ".png"); | |
| 179 if (GrabWindowSnapshot(root_window, rect, &png_data->data())) { | 290 if (GrabWindowSnapshot(root_window, rect, &png_data->data())) { | 
| 180 DisplayVisualFeedback(rect); | 291 PostSaveScreenshotTask(screenshot_path, png_data); | 
| 181 PostSaveScreenshotTask( | |
| 182 screenshot_directory.AppendASCII(basename + ".png"), png_data); | |
| 183 } else { | 292 } else { | 
| 184 LOG(ERROR) << "Failed to grab the window screenshot for " << i; | 293 LOG(ERROR) << "Failed to grab the window screenshot for " << i; | 
| 294 ShowNotification( | |
| 295 ScreenshotTakerObserver::SCREENSHOT_GRABWINDOW_FULL_FAILED, | |
| 296 screenshot_path); | |
| 185 } | 297 } | 
| 186 } | 298 } | 
| 187 last_screenshot_timestamp_ = base::Time::Now(); | 299 last_screenshot_timestamp_ = base::Time::Now(); | 
| 188 } | 300 } | 
| 189 | 301 | 
| 190 void ScreenshotTaker::HandleTakePartialScreenshot( | 302 void ScreenshotTaker::HandleTakePartialScreenshot( | 
| 191 aura::Window* window, const gfx::Rect& rect) { | 303 aura::Window* window, const gfx::Rect& rect) { | 
| 192 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 304 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 
| 193 | 305 | 
| 194 base::FilePath screenshot_directory; | 306 base::FilePath screenshot_directory; | 
| 195 if (!ScreenshotSource::GetScreenshotDirectory(&screenshot_directory)) | 307 if (!screenshot_directory_for_test_.empty()) { | 
| 308 screenshot_directory = screenshot_directory_for_test_; | |
| 309 } else if (!ScreenshotSource::GetScreenshotDirectory(&screenshot_directory)) { | |
| 310 ShowNotification(ScreenshotTakerObserver::SCREENSHOT_GET_DIR_FAILED, | |
| 311 base::FilePath()); | |
| 196 return; | 312 return; | 
| 313 } | |
| 197 | 314 | 
| 198 scoped_refptr<base::RefCountedBytes> png_data(new base::RefCountedBytes); | 315 scoped_refptr<base::RefCountedBytes> png_data(new base::RefCountedBytes); | 
| 199 | 316 | 
| 317 base::FilePath screenshot_path = | |
| 318 screenshot_directory.AppendASCII( | |
| 319 ScreenshotSource::GetScreenshotBaseFilename() + ".png"); | |
| 200 if (GrabWindowSnapshot(window, rect, &png_data->data())) { | 320 if (GrabWindowSnapshot(window, rect, &png_data->data())) { | 
| 201 last_screenshot_timestamp_ = base::Time::Now(); | 321 last_screenshot_timestamp_ = base::Time::Now(); | 
| 202 DisplayVisualFeedback(rect); | 322 PostSaveScreenshotTask(screenshot_path, png_data); | 
| 203 PostSaveScreenshotTask( | |
| 204 screenshot_directory.AppendASCII( | |
| 205 ScreenshotSource::GetScreenshotBaseFilename() + ".png"), | |
| 206 png_data); | |
| 207 } else { | 323 } else { | 
| 208 LOG(ERROR) << "Failed to grab the window screenshot"; | 324 LOG(ERROR) << "Failed to grab the window screenshot"; | 
| 325 ShowNotification( | |
| 326 ScreenshotTakerObserver::SCREENSHOT_GRABWINDOW_PARTIAL_FAILED, | |
| 327 screenshot_path); | |
| 209 } | 328 } | 
| 210 } | 329 } | 
| 211 | 330 | 
| 212 bool ScreenshotTaker::CanTakeScreenshot() { | 331 bool ScreenshotTaker::CanTakeScreenshot() { | 
| 213 return last_screenshot_timestamp_.is_null() || | 332 return last_screenshot_timestamp_.is_null() || | 
| 214 base::Time::Now() - last_screenshot_timestamp_ > | 333 base::Time::Now() - last_screenshot_timestamp_ > | 
| 215 base::TimeDelta::FromMilliseconds( | 334 base::TimeDelta::FromMilliseconds( | 
| 216 kScreenshotMinimumIntervalInMS); | 335 kScreenshotMinimumIntervalInMS); | 
| 217 } | 336 } | 
| 218 | 337 | 
| 219 void ScreenshotTaker::CloseVisualFeedbackLayer() { | 338 void ScreenshotTaker::SetScreenshotDirectoryForTest( | 
| 220 visual_feedback_layer_.reset(); | 339 const base::FilePath& directory) { | 
| 340 screenshot_directory_for_test_ = directory; | |
| 221 } | 341 } | 
| 222 | 342 | 
| 223 void ScreenshotTaker::DisplayVisualFeedback(const gfx::Rect& rect) { | 343 // static | 
| 224 visual_feedback_layer_.reset(new ui::Layer(ui::LAYER_SOLID_COLOR)); | 344 void ScreenshotTaker::SetObserverForTest(ScreenshotTakerObserver* observer) { | 
| 225 visual_feedback_layer_->SetColor(SK_ColorWHITE); | 345 observer_for_test_ = observer; | 
| 226 visual_feedback_layer_->SetOpacity(kVisualFeedbackLayerOpacity); | 346 } | 
| 227 visual_feedback_layer_->SetBounds(rect); | |
| 228 | 347 | 
| 229 ui::Layer* parent = ash::Shell::GetContainer( | 348 // static | 
| 230 ash::Shell::GetActiveRootWindow(), | 349 ScreenshotTakerObserver* ScreenshotTaker::GetObserverForTest() { | 
| 231 ash::internal::kShellWindowId_OverlayContainer)->layer(); | 350 return observer_for_test_; | 
| 232 parent->Add(visual_feedback_layer_.get()); | |
| 233 visual_feedback_layer_->SetVisible(true); | |
| 234 | |
| 235 MessageLoopForUI::current()->PostDelayedTask( | |
| 236 FROM_HERE, | |
| 237 base::Bind(&ScreenshotTaker::CloseVisualFeedbackLayer, | |
| 238 base::Unretained(this)), | |
| 239 base::TimeDelta::FromMilliseconds(kVisualFeedbackLayerDisplayTimeMs)); | |
| 240 } | 351 } | 
| OLD | NEW |