| OLD | NEW |
| 1 // Copyright (c) 2012 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 "chrome/browser/ui/ash/screenshot_taker.h" | 5 #include "chrome/browser/ui/ash/chrome_screenshot_taker.h" |
| 6 | |
| 7 #include <climits> | |
| 8 #include <string> | |
| 9 | 6 |
| 10 #include "ash/shell.h" | 7 #include "ash/shell.h" |
| 11 #include "ash/shell_delegate.h" | |
| 12 #include "ash/system/system_notifier.h" | 8 #include "ash/system/system_notifier.h" |
| 13 #include "base/base64.h" | 9 #include "base/base64.h" |
| 14 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/callback.h" |
| 15 #include "base/files/file_util.h" | 12 #include "base/files/file_util.h" |
| 16 #include "base/i18n/time_formatting.h" | 13 #include "base/i18n/time_formatting.h" |
| 17 #include "base/logging.h" | |
| 18 #include "base/memory/ref_counted_memory.h" | |
| 19 #include "base/prefs/pref_service.h" | 14 #include "base/prefs/pref_service.h" |
| 20 #include "base/strings/stringprintf.h" | 15 #include "base/strings/stringprintf.h" |
| 21 #include "base/strings/utf_string_conversions.h" | 16 #include "base/strings/utf_string_conversions.h" |
| 22 #include "base/threading/sequenced_worker_pool.h" | |
| 23 #include "base/time/time.h" | |
| 24 #include "chrome/browser/browser_process.h" | 17 #include "chrome/browser/browser_process.h" |
| 25 #include "chrome/browser/download/download_prefs.h" | 18 #include "chrome/browser/download/download_prefs.h" |
| 26 #include "chrome/browser/notifications/notification_ui_manager.h" | 19 #include "chrome/browser/notifications/notification_ui_manager.h" |
| 27 #include "chrome/browser/profiles/profile.h" | 20 #include "chrome/browser/profiles/profile.h" |
| 28 #include "chrome/browser/profiles/profile_manager.h" | 21 #include "chrome/browser/profiles/profile_manager.h" |
| 29 #include "chrome/common/pref_names.h" | 22 #include "chrome/common/pref_names.h" |
| 30 #include "content/public/browser/browser_thread.h" | 23 #include "content/public/browser/browser_thread.h" |
| 31 #include "content/public/browser/user_metrics.h" | 24 #include "content/public/browser/user_metrics.h" |
| 32 #include "grit/ash_strings.h" | 25 #include "grit/ash_strings.h" |
| 33 #include "grit/theme_resources.h" | 26 #include "grit/theme_resources.h" |
| 34 #include "ui/aura/window.h" | |
| 35 #include "ui/aura/window_event_dispatcher.h" | |
| 36 #include "ui/base/clipboard/clipboard.h" | 27 #include "ui/base/clipboard/clipboard.h" |
| 37 #include "ui/base/clipboard/scoped_clipboard_writer.h" | 28 #include "ui/base/clipboard/scoped_clipboard_writer.h" |
| 38 #include "ui/base/l10n/l10n_util.h" | 29 #include "ui/base/l10n/l10n_util.h" |
| 39 #include "ui/base/resource/resource_bundle.h" | 30 #include "ui/base/resource/resource_bundle.h" |
| 40 #include "ui/gfx/image/image.h" | |
| 41 #include "ui/snapshot/snapshot.h" | |
| 42 #include "ui/strings/grit/ui_strings.h" | 31 #include "ui/strings/grit/ui_strings.h" |
| 43 | 32 |
| 44 #if defined(OS_CHROMEOS) | 33 #if defined(OS_CHROMEOS) |
| 45 #include "chrome/browser/chromeos/drive/file_system_interface.h" | 34 #include "chrome/browser/chromeos/drive/file_system_interface.h" |
| 46 #include "chrome/browser/chromeos/drive/file_system_util.h" | 35 #include "chrome/browser/chromeos/drive/file_system_util.h" |
| 47 #include "chrome/browser/chromeos/file_manager/open_util.h" | 36 #include "chrome/browser/chromeos/file_manager/open_util.h" |
| 48 #include "chrome/browser/notifications/desktop_notification_service.h" | 37 #include "chrome/browser/notifications/desktop_notification_service.h" |
| 49 #include "chrome/browser/notifications/desktop_notification_service_factory.h" | 38 #include "chrome/browser/notifications/desktop_notification_service_factory.h" |
| 50 #include "chromeos/login/login_state.h" | 39 #include "chromeos/login/login_state.h" |
| 51 #endif | 40 #endif |
| 52 | 41 |
| 53 namespace { | 42 namespace { |
| 54 // The minimum interval between two screenshot commands. It has to be | |
| 55 // more than 1000 to prevent the conflict of filenames. | |
| 56 const int kScreenshotMinimumIntervalInMS = 1000; | |
| 57 | 43 |
| 58 const char kNotificationId[] = "screenshot"; | 44 const char kNotificationId[] = "screenshot"; |
| 59 | 45 |
| 60 #if defined(OS_CHROMEOS) | 46 #if defined(OS_CHROMEOS) |
| 61 const char kNotificationOriginUrl[] = "chrome://screenshot"; | 47 const char kNotificationOriginUrl[] = "chrome://screenshot"; |
| 62 #endif | 48 #endif |
| 63 | 49 |
| 64 const char kImageClipboardFormatPrefix[] = "<img src='data:image/png;base64,"; | 50 const char kImageClipboardFormatPrefix[] = "<img src='data:image/png;base64,"; |
| 65 const char kImageClipboardFormatSuffix[] = "'>"; | 51 const char kImageClipboardFormatSuffix[] = "'>"; |
| 66 | 52 |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 161 private: | 147 private: |
| 162 ~ScreenshotTakerNotificationDelegate() override {} | 148 ~ScreenshotTakerNotificationDelegate() override {} |
| 163 | 149 |
| 164 const bool success_; | 150 const bool success_; |
| 165 Profile* profile_; | 151 Profile* profile_; |
| 166 const base::FilePath screenshot_path_; | 152 const base::FilePath screenshot_path_; |
| 167 | 153 |
| 168 DISALLOW_COPY_AND_ASSIGN(ScreenshotTakerNotificationDelegate); | 154 DISALLOW_COPY_AND_ASSIGN(ScreenshotTakerNotificationDelegate); |
| 169 }; | 155 }; |
| 170 | 156 |
| 171 typedef base::Callback< | 157 #if defined(OS_CHROMEOS) |
| 172 void(ScreenshotTakerObserver::Result screenshot_result, | 158 int GetScreenshotNotificationTitle( |
| 173 const base::FilePath& screenshot_path)> ShowNotificationCallback; | 159 ScreenshotTakerObserver::Result screenshot_result) { |
| 174 | 160 switch (screenshot_result) { |
| 175 void SaveScreenshotInternal(const ShowNotificationCallback& callback, | 161 case ScreenshotTakerObserver::SCREENSHOTS_DISABLED: |
| 176 const base::FilePath& screenshot_path, | 162 return IDS_ASH_SCREENSHOT_NOTIFICATION_TITLE_DISABLED; |
| 177 const base::FilePath& local_path, | 163 case ScreenshotTakerObserver::SCREENSHOT_SUCCESS: |
| 178 scoped_refptr<base::RefCountedBytes> png_data) { | 164 return IDS_ASH_SCREENSHOT_NOTIFICATION_TITLE_SUCCESS; |
| 179 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); | 165 default: |
| 180 DCHECK(!local_path.empty()); | 166 return IDS_ASH_SCREENSHOT_NOTIFICATION_TITLE_FAIL; |
| 181 ScreenshotTakerObserver::Result result = | |
| 182 ScreenshotTakerObserver::SCREENSHOT_SUCCESS; | |
| 183 if (static_cast<size_t>(base::WriteFile( | |
| 184 local_path, | |
| 185 reinterpret_cast<char*>(&(png_data->data()[0])), | |
| 186 png_data->size())) != png_data->size()) { | |
| 187 LOG(ERROR) << "Failed to save to " << local_path.value(); | |
| 188 result = ScreenshotTakerObserver::SCREENSHOT_WRITE_FILE_FAILED; | |
| 189 } | 167 } |
| 190 content::BrowserThread::PostTask( | |
| 191 content::BrowserThread::UI, FROM_HERE, | |
| 192 base::Bind(callback, result, screenshot_path)); | |
| 193 } | 168 } |
| 194 | 169 |
| 195 void SaveScreenshot(const ShowNotificationCallback& callback, | 170 int GetScreenshotNotificationText( |
| 196 const base::FilePath& screenshot_path, | 171 ScreenshotTakerObserver::Result screenshot_result) { |
| 197 scoped_refptr<base::RefCountedBytes> png_data) { | 172 switch (screenshot_result) { |
| 198 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); | 173 case ScreenshotTakerObserver::SCREENSHOTS_DISABLED: |
| 199 DCHECK(!screenshot_path.empty()); | 174 return IDS_ASH_SCREENSHOT_NOTIFICATION_TEXT_DISABLED; |
| 200 | 175 case ScreenshotTakerObserver::SCREENSHOT_SUCCESS: |
| 201 if (!base::CreateDirectory(screenshot_path.DirName())) { | 176 return IDS_ASH_SCREENSHOT_NOTIFICATION_TEXT_SUCCESS; |
| 202 LOG(ERROR) << "Failed to ensure the existence of " | 177 default: |
| 203 << screenshot_path.DirName().value(); | 178 return IDS_ASH_SCREENSHOT_NOTIFICATION_TEXT_FAIL; |
| 204 content::BrowserThread::PostTask( | |
| 205 content::BrowserThread::UI, FROM_HERE, | |
| 206 base::Bind(callback, | |
| 207 ScreenshotTakerObserver::SCREENSHOT_CREATE_DIR_FAILED, | |
| 208 screenshot_path)); | |
| 209 return; | |
| 210 } | 179 } |
| 211 SaveScreenshotInternal(callback, screenshot_path, screenshot_path, png_data); | |
| 212 } | 180 } |
| 213 | 181 |
| 214 // TODO(kinaba): crbug.com/140425, remove this ungly #ifdef dispatch. | 182 void PrepareWritableFileCallback( |
| 215 #if defined(OS_CHROMEOS) | 183 const ChromeScreenshotTaker::WritableFileCallback& callback, |
| 216 void SaveScreenshotToDrive(const ShowNotificationCallback& callback, | 184 drive::FileError error, |
| 217 const base::FilePath& screenshot_path, | 185 const base::FilePath& local_path) { |
| 218 scoped_refptr<base::RefCountedBytes> png_data, | 186 callback.Run(error == drive::FILE_ERROR_OK ? |
| 219 drive::FileError error, | 187 ScreenshotTakerClient::WRITABLE_FILE_SUCCESS : |
| 220 const base::FilePath& local_path) { | 188 ScreenshotTakerClient::WRITABLE_FILE_CREATE_FAILED, |
| 221 // |screenshot_path| is used in the notification callback. | 189 local_path); |
| 222 // |local_path| is a temporary file in a hidden cache directory used for | |
| 223 // internal work generated by drive::util::PrepareWritableFileAndRun. | |
| 224 if (error != drive::FILE_ERROR_OK) { | |
| 225 LOG(ERROR) << "Failed to write screenshot image to Google Drive: " << error; | |
| 226 content::BrowserThread::PostTask( | |
| 227 content::BrowserThread::UI, FROM_HERE, | |
| 228 base::Bind(callback, | |
| 229 ScreenshotTakerObserver::SCREENSHOT_CREATE_FILE_FAILED, | |
| 230 screenshot_path)); | |
| 231 return; | |
| 232 } | |
| 233 SaveScreenshotInternal(callback, screenshot_path, local_path, png_data); | |
| 234 } | 190 } |
| 235 | 191 |
| 236 void EnsureDirectoryExistsCallback( | 192 void EnsureDirectoryExistsCallback( |
| 237 const ShowNotificationCallback& callback, | 193 const ChromeScreenshotTaker::WritableFileCallback& callback, |
| 238 Profile* profile, | 194 Profile* profile, |
| 239 const base::FilePath& screenshot_path, | 195 const base::FilePath& path, |
| 240 scoped_refptr<base::RefCountedBytes> png_data, | |
| 241 drive::FileError error) { | 196 drive::FileError error) { |
| 242 // It is okay to fail with FILE_ERROR_EXISTS since anyway the directory | 197 // It is okay to fail with FILE_ERROR_EXISTS since anyway the directory |
| 243 // of the target file exists. | 198 // of the target file exists. |
| 244 if (error == drive::FILE_ERROR_OK || | 199 if (error == drive::FILE_ERROR_OK || |
| 245 error == drive::FILE_ERROR_EXISTS) { | 200 error == drive::FILE_ERROR_EXISTS) { |
| 246 drive::util::PrepareWritableFileAndRun( | 201 drive::util::PrepareWritableFileAndRun( |
| 247 profile, | 202 profile, |
| 248 screenshot_path, | 203 path, |
| 249 base::Bind(&SaveScreenshotToDrive, | 204 base::Bind(&PrepareWritableFileCallback, |
| 250 callback, | 205 callback)); |
| 251 screenshot_path, | |
| 252 png_data)); | |
| 253 } else { | 206 } else { |
| 254 LOG(ERROR) << "Failed to ensure the existence of the specified directory " | 207 LOG(ERROR) << "Failed to ensure the existence of the specified directory " |
| 255 << "in Google Drive: " << error; | 208 << "in Google Drive: " << error; |
| 256 callback.Run(ScreenshotTakerObserver::SCREENSHOT_CHECK_DIR_FAILED, | 209 content::BrowserThread::GetBlockingPool()->PostTask( |
| 257 screenshot_path); | 210 FROM_HERE, base::Bind( |
| 211 callback, |
| 212 ScreenshotTakerClient::WRITABLE_FILE_CHECK_DIR_FAILED, |
| 213 base::FilePath())); |
| 258 } | 214 } |
| 259 } | 215 } |
| 216 #endif |
| 260 | 217 |
| 261 void PostSaveScreenshotTask(const ShowNotificationCallback& callback, | 218 bool ScreenshotsDisabled() { |
| 262 Profile* profile, | 219 return g_browser_process->local_state()-> |
| 263 const base::FilePath& screenshot_path, | 220 GetBoolean(prefs::kDisableScreenshots); |
| 264 scoped_refptr<base::RefCountedBytes> png_data) { | |
| 265 if (drive::util::IsUnderDriveMountPoint(screenshot_path)) { | |
| 266 drive::util::EnsureDirectoryExists( | |
| 267 profile, | |
| 268 screenshot_path.DirName(), | |
| 269 base::Bind(&EnsureDirectoryExistsCallback, | |
| 270 callback, | |
| 271 profile, | |
| 272 screenshot_path, | |
| 273 png_data)); | |
| 274 } else { | |
| 275 content::BrowserThread::GetBlockingPool()->PostTask( | |
| 276 FROM_HERE, base::Bind(&SaveScreenshot, | |
| 277 callback, | |
| 278 screenshot_path, | |
| 279 png_data)); | |
| 280 } | |
| 281 } | 221 } |
| 282 #else | |
| 283 void PostSaveScreenshotTask(const ShowNotificationCallback& callback, | |
| 284 Profile* profile, | |
| 285 const base::FilePath& screenshot_path, | |
| 286 scoped_refptr<base::RefCountedBytes> png_data) { | |
| 287 content::BrowserThread::GetBlockingPool()->PostTask( | |
| 288 FROM_HERE, base::Bind(&SaveScreenshot, | |
| 289 callback, | |
| 290 screenshot_path, | |
| 291 png_data)); | |
| 292 } | |
| 293 #endif | |
| 294 | 222 |
| 295 bool ShouldUse24HourClock() { | 223 bool ShouldUse24HourClock() { |
| 296 #if defined(OS_CHROMEOS) | 224 #if defined(OS_CHROMEOS) |
| 297 Profile* profile = ProfileManager::GetActiveUserProfile(); | 225 Profile* profile = ProfileManager::GetActiveUserProfile(); |
| 298 if (profile) { | 226 if (profile) { |
| 299 return profile->GetPrefs()->GetBoolean(prefs::kUse24HourClock); | 227 return profile->GetPrefs()->GetBoolean(prefs::kUse24HourClock); |
| 300 } | 228 } |
| 301 #endif | 229 #endif |
| 302 return base::GetHourClockType() == base::k24HourClock; | 230 return base::GetHourClockType() == base::k24HourClock; |
| 303 } | 231 } |
| 304 | 232 |
| 233 bool GetScreenshotDirectory(base::FilePath* directory) { |
| 234 bool is_logged_in = true; |
| 235 |
| 236 #if defined(OS_CHROMEOS) |
| 237 is_logged_in = chromeos::LoginState::Get()->IsUserLoggedIn(); |
| 238 #endif |
| 239 |
| 240 if (is_logged_in) { |
| 241 DownloadPrefs* download_prefs = DownloadPrefs::FromBrowserContext( |
| 242 ProfileManager::GetActiveUserProfile()); |
| 243 *directory = download_prefs->DownloadPath(); |
| 244 } else { |
| 245 if (!base::GetTempDir(directory)) { |
| 246 LOG(ERROR) << "Failed to find temporary directory."; |
| 247 return false; |
| 248 } |
| 249 } |
| 250 return true; |
| 251 } |
| 252 |
| 305 std::string GetScreenshotBaseFilename() { | 253 std::string GetScreenshotBaseFilename() { |
| 306 base::Time::Exploded now; | 254 base::Time::Exploded now; |
| 307 base::Time::Now().LocalExplode(&now); | 255 base::Time::Now().LocalExplode(&now); |
| 308 | 256 |
| 309 // We don't use base/i18n/time_formatting.h here because it doesn't | 257 // We don't use base/i18n/time_formatting.h here because it doesn't |
| 310 // support our format. Don't use ICU either to avoid i18n file names | 258 // support our format. Don't use ICU either to avoid i18n file names |
| 311 // for non-English locales. | 259 // for non-English locales. |
| 312 // TODO(mukai): integrate this logic somewhere time_formatting.h | 260 // TODO(mukai): integrate this logic somewhere time_formatting.h |
| 313 std::string file_name = base::StringPrintf( | 261 std::string file_name = base::StringPrintf( |
| 314 "Screenshot %d-%02d-%02d at ", now.year, now.month, now.day_of_month); | 262 "Screenshot %d-%02d-%02d at ", now.year, now.month, now.day_of_month); |
| 315 | 263 |
| 316 if (ShouldUse24HourClock()) { | 264 if (ShouldUse24HourClock()) { |
| 317 file_name.append(base::StringPrintf( | 265 file_name.append(base::StringPrintf( |
| 318 "%02d.%02d.%02d", now.hour, now.minute, now.second)); | 266 "%02d.%02d.%02d", now.hour, now.minute, now.second)); |
| 319 } else { | 267 } else { |
| 320 int hour = now.hour; | 268 int hour = now.hour; |
| 321 if (hour > 12) { | 269 if (hour > 12) { |
| 322 hour -= 12; | 270 hour -= 12; |
| 323 } else if (hour == 0) { | 271 } else if (hour == 0) { |
| 324 hour = 12; | 272 hour = 12; |
| 325 } | 273 } |
| 326 file_name.append(base::StringPrintf( | 274 file_name.append(base::StringPrintf( |
| 327 "%d.%02d.%02d ", hour, now.minute, now.second)); | 275 "%d.%02d.%02d ", hour, now.minute, now.second)); |
| 328 file_name.append((now.hour >= 12) ? "PM" : "AM"); | 276 file_name.append((now.hour >= 12) ? "PM" : "AM"); |
| 329 } | 277 } |
| 330 | 278 |
| 331 return file_name; | 279 return file_name; |
| 332 } | 280 } |
| 333 | 281 |
| 334 bool GetScreenshotDirectory(base::FilePath* directory) { | 282 } // namespace |
| 335 bool is_logged_in = true; | |
| 336 | 283 |
| 337 #if defined(OS_CHROMEOS) | 284 ChromeScreenshotTaker::ChromeScreenshotTaker() |
| 338 is_logged_in = chromeos::LoginState::Get()->IsUserLoggedIn(); | 285 : screenshot_taker_(new ScreenshotTaker(this)) { |
| 339 #endif | 286 screenshot_taker_->AddObserver(this); |
| 340 | |
| 341 if (is_logged_in) { | |
| 342 DownloadPrefs* download_prefs = DownloadPrefs::FromBrowserContext( | |
| 343 ProfileManager::GetActiveUserProfile()); | |
| 344 *directory = download_prefs->DownloadPath(); | |
| 345 } else { | |
| 346 if (!base::GetTempDir(directory)) { | |
| 347 LOG(ERROR) << "Failed to find temporary directory."; | |
| 348 return false; | |
| 349 } | |
| 350 } | |
| 351 return true; | |
| 352 } | 287 } |
| 353 | 288 |
| 354 #if defined(OS_CHROMEOS) | 289 ChromeScreenshotTaker::~ChromeScreenshotTaker() { |
| 355 int GetScreenshotNotificationTitle( | 290 screenshot_taker_->RemoveObserver(this); |
| 356 ScreenshotTakerObserver::Result screenshot_result) { | |
| 357 switch (screenshot_result) { | |
| 358 case ScreenshotTakerObserver::SCREENSHOTS_DISABLED: | |
| 359 return IDS_ASH_SCREENSHOT_NOTIFICATION_TITLE_DISABLED; | |
| 360 case ScreenshotTakerObserver::SCREENSHOT_SUCCESS: | |
| 361 return IDS_ASH_SCREENSHOT_NOTIFICATION_TITLE_SUCCESS; | |
| 362 default: | |
| 363 return IDS_ASH_SCREENSHOT_NOTIFICATION_TITLE_FAIL; | |
| 364 } | |
| 365 } | 291 } |
| 366 | 292 |
| 367 int GetScreenshotNotificationText( | 293 void ChromeScreenshotTaker::HandleTakeScreenshotForAllRootWindows() { |
| 368 ScreenshotTakerObserver::Result screenshot_result) { | 294 if (ScreenshotsDisabled()) { |
| 369 switch (screenshot_result) { | 295 screenshot_taker_->NotifyScreenshotCompleted( |
| 370 case ScreenshotTakerObserver::SCREENSHOTS_DISABLED: | 296 ScreenshotTakerObserver::SCREENSHOTS_DISABLED, |
| 371 return IDS_ASH_SCREENSHOT_NOTIFICATION_TEXT_DISABLED; | 297 base::FilePath()); |
| 372 case ScreenshotTakerObserver::SCREENSHOT_SUCCESS: | |
| 373 return IDS_ASH_SCREENSHOT_NOTIFICATION_TEXT_SUCCESS; | |
| 374 default: | |
| 375 return IDS_ASH_SCREENSHOT_NOTIFICATION_TEXT_FAIL; | |
| 376 } | |
| 377 } | |
| 378 #endif | |
| 379 | |
| 380 } // namespace | |
| 381 | |
| 382 ScreenshotTaker::ScreenshotTaker() | |
| 383 : profile_for_test_(NULL), | |
| 384 factory_(this) { | |
| 385 } | |
| 386 | |
| 387 ScreenshotTaker::~ScreenshotTaker() { | |
| 388 } | |
| 389 | |
| 390 void ScreenshotTaker::HandleTakeScreenshotForAllRootWindows() { | |
| 391 if (g_browser_process->local_state()-> | |
| 392 GetBoolean(prefs::kDisableScreenshots)) { | |
| 393 ShowNotification(ScreenshotTakerObserver::SCREENSHOTS_DISABLED, | |
| 394 base::FilePath()); | |
| 395 return; | 298 return; |
| 396 } | 299 } |
| 300 |
| 397 base::FilePath screenshot_directory; | 301 base::FilePath screenshot_directory; |
| 398 if (!screenshot_directory_for_test_.empty()) { | 302 if (!GetScreenshotDirectory(&screenshot_directory)) { |
| 399 screenshot_directory = screenshot_directory_for_test_; | 303 screenshot_taker_->NotifyScreenshotCompleted( |
| 400 } else if (!GetScreenshotDirectory(&screenshot_directory)) { | 304 ScreenshotTakerObserver::SCREENSHOT_GET_DIR_FAILED, |
| 401 ShowNotification(ScreenshotTakerObserver::SCREENSHOT_GET_DIR_FAILED, | 305 base::FilePath()); |
| 402 base::FilePath()); | |
| 403 return; | 306 return; |
| 404 } | 307 } |
| 405 std::string screenshot_basename = !screenshot_basename_for_test_.empty() ? | |
| 406 screenshot_basename_for_test_ : GetScreenshotBaseFilename(); | |
| 407 | 308 |
| 309 std::string screenshot_basename = GetScreenshotBaseFilename(); |
| 408 aura::Window::Windows root_windows = ash::Shell::GetAllRootWindows(); | 310 aura::Window::Windows root_windows = ash::Shell::GetAllRootWindows(); |
| 409 // Reorder root_windows to take the primary root window's snapshot at first. | 311 // Reorder root_windows to take the primary root window's snapshot at first. |
| 410 aura::Window* primary_root = ash::Shell::GetPrimaryRootWindow(); | 312 aura::Window* primary_root = ash::Shell::GetPrimaryRootWindow(); |
| 411 if (*(root_windows.begin()) != primary_root) { | 313 if (*(root_windows.begin()) != primary_root) { |
| 412 root_windows.erase(std::find( | 314 root_windows.erase(std::find( |
| 413 root_windows.begin(), root_windows.end(), primary_root)); | 315 root_windows.begin(), root_windows.end(), primary_root)); |
| 414 root_windows.insert(root_windows.begin(), primary_root); | 316 root_windows.insert(root_windows.begin(), primary_root); |
| 415 } | 317 } |
| 318 std::vector<base::FilePath> filenames; |
| 416 for (size_t i = 0; i < root_windows.size(); ++i) { | 319 for (size_t i = 0; i < root_windows.size(); ++i) { |
| 417 aura::Window* root_window = root_windows[i]; | 320 aura::Window* root_window = root_windows[i]; |
| 418 std::string basename = screenshot_basename; | 321 std::string basename = screenshot_basename; |
| 419 gfx::Rect rect = root_window->bounds(); | 322 gfx::Rect rect = root_window->bounds(); |
| 420 if (root_windows.size() > 1) | 323 if (root_windows.size() > 1) |
| 421 basename += base::StringPrintf(" - Display %d", static_cast<int>(i + 1)); | 324 basename += base::StringPrintf(" - Display %d", static_cast<int>(i + 1)); |
| 422 base::FilePath screenshot_path = | 325 base::FilePath screenshot_path = |
| 423 screenshot_directory.AppendASCII(basename + ".png"); | 326 screenshot_directory.AppendASCII(basename + ".png"); |
| 424 GrabFullWindowSnapshotAsync( | 327 screenshot_taker_->TakeScreenshot(root_window, |
| 425 root_window, rect, GetProfile(), screenshot_path, i); | 328 rect, |
| 329 screenshot_path); |
| 426 } | 330 } |
| 427 content::RecordAction(base::UserMetricsAction("Screenshot_TakeFull")); | 331 content::RecordAction(base::UserMetricsAction("Screenshot_TakeFull")); |
| 428 } | 332 } |
| 429 | 333 |
| 430 void ScreenshotTaker::HandleTakePartialScreenshot( | 334 void ChromeScreenshotTaker::HandleTakePartialScreenshot( |
| 431 aura::Window* window, const gfx::Rect& rect) { | 335 aura::Window* window, |
| 432 if (g_browser_process->local_state()-> | 336 const gfx::Rect& rect) { |
| 433 GetBoolean(prefs::kDisableScreenshots)) { | 337 if (ScreenshotsDisabled()) { |
| 434 ShowNotification(ScreenshotTakerObserver::SCREENSHOTS_DISABLED, | 338 screenshot_taker_->NotifyScreenshotCompleted( |
| 435 base::FilePath()); | 339 ScreenshotTakerObserver::SCREENSHOTS_DISABLED, |
| 436 return; | 340 base::FilePath()); |
| 437 } | |
| 438 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 439 | |
| 440 base::FilePath screenshot_directory; | |
| 441 if (!screenshot_directory_for_test_.empty()) { | |
| 442 screenshot_directory = screenshot_directory_for_test_; | |
| 443 } else if (!GetScreenshotDirectory(&screenshot_directory)) { | |
| 444 ShowNotification(ScreenshotTakerObserver::SCREENSHOT_GET_DIR_FAILED, | |
| 445 base::FilePath()); | |
| 446 return; | 341 return; |
| 447 } | 342 } |
| 448 | 343 |
| 449 std::string screenshot_basename = !screenshot_basename_for_test_.empty() ? | 344 base::FilePath screenshot_directory; |
| 450 screenshot_basename_for_test_ : GetScreenshotBaseFilename(); | 345 if (!GetScreenshotDirectory(&screenshot_directory)) { |
| 346 screenshot_taker_->NotifyScreenshotCompleted( |
| 347 ScreenshotTakerObserver::SCREENSHOT_GET_DIR_FAILED, |
| 348 base::FilePath()); |
| 349 return; |
| 350 } |
| 351 |
| 451 base::FilePath screenshot_path = | 352 base::FilePath screenshot_path = |
| 452 screenshot_directory.AppendASCII(screenshot_basename + ".png"); | 353 screenshot_directory.AppendASCII(GetScreenshotBaseFilename() + ".png"); |
| 453 GrabPartialWindowSnapshotAsync(window, rect, GetProfile(), screenshot_path); | 354 screenshot_taker_->TakeScreenshot(window, rect, screenshot_path); |
| 454 content::RecordAction(base::UserMetricsAction("Screenshot_TakePartial")); | 355 content::RecordAction(base::UserMetricsAction("Screenshot_TakePartial")); |
| 455 } | 356 } |
| 456 | 357 |
| 457 bool ScreenshotTaker::CanTakeScreenshot() { | 358 bool ChromeScreenshotTaker::CanTakeScreenshot() { |
| 458 return last_screenshot_timestamp_.is_null() || | 359 return screenshot_taker_->CanTakeScreenshot(); |
| 459 base::Time::Now() - last_screenshot_timestamp_ > | 360 } |
| 460 base::TimeDelta::FromMilliseconds( | 361 |
| 461 kScreenshotMinimumIntervalInMS); | 362 void ChromeScreenshotTaker::PrepareWritableFileAndRunOnBlockingPool( |
| 363 const base::FilePath& path, |
| 364 WritableFileCallback callback) { |
| 365 #if defined(OS_CHROMEOS) |
| 366 Profile* profile = ProfileManager::GetActiveUserProfile(); |
| 367 if (drive::util::IsUnderDriveMountPoint(path)) { |
| 368 drive::util::EnsureDirectoryExists( |
| 369 profile, |
| 370 path.DirName(), |
| 371 base::Bind(&EnsureDirectoryExistsCallback, |
| 372 callback, |
| 373 profile, |
| 374 path)); |
| 375 return; |
| 376 } |
| 377 #endif |
| 378 ScreenshotTakerClient::PrepareWritableFileAndRunOnBlockingPool( |
| 379 path, callback); |
| 380 } |
| 381 |
| 382 void ChromeScreenshotTaker::OnScreenshotCompleted( |
| 383 ScreenshotTakerObserver::Result result, |
| 384 const base::FilePath& screenshot_path) { |
| 385 #if defined(OS_CHROMEOS) |
| 386 // Do not show a notification that a screenshot was taken while no user is |
| 387 // logged in, since it is confusing for the user to get a message about it |
| 388 // after he logs in (crbug.com/235217). |
| 389 if (!chromeos::LoginState::Get()->IsUserLoggedIn()) |
| 390 return; |
| 391 |
| 392 // TODO(sschmitz): make this work for Windows. |
| 393 Profile* profile = ProfileManager::GetActiveUserProfile(); |
| 394 DesktopNotificationService* const service = |
| 395 DesktopNotificationServiceFactory::GetForProfile(profile); |
| 396 if (service->IsNotifierEnabled(message_center::NotifierId( |
| 397 message_center::NotifierId::SYSTEM_COMPONENT, |
| 398 ash::system_notifier::kNotifierScreenshot))) { |
| 399 scoped_ptr<Notification> notification( |
| 400 CreateNotification(result, screenshot_path)); |
| 401 g_browser_process->notification_ui_manager()->Add(*notification, profile); |
| 402 } |
| 403 #endif |
| 462 } | 404 } |
| 463 | 405 |
| 464 #if defined(OS_CHROMEOS) | 406 #if defined(OS_CHROMEOS) |
| 465 Notification* ScreenshotTaker::CreateNotification( | 407 Notification* ChromeScreenshotTaker::CreateNotification( |
| 466 ScreenshotTakerObserver::Result screenshot_result, | 408 ScreenshotTakerObserver::Result screenshot_result, |
| 467 const base::FilePath& screenshot_path) { | 409 const base::FilePath& screenshot_path) { |
| 468 const std::string notification_id(kNotificationId); | 410 const std::string notification_id(kNotificationId); |
| 469 // We cancel a previous screenshot notification, if any, to ensure we get | 411 // We cancel a previous screenshot notification, if any, to ensure we get |
| 470 // a fresh notification pop-up. | 412 // a fresh notification pop-up. |
| 413 Profile* profile = ProfileManager::GetActiveUserProfile(); |
| 471 g_browser_process->notification_ui_manager()->CancelById( | 414 g_browser_process->notification_ui_manager()->CancelById( |
| 472 notification_id, NotificationUIManager::GetProfileID(GetProfile())); | 415 notification_id, NotificationUIManager::GetProfileID(profile)); |
| 473 const base::string16 replace_id(base::UTF8ToUTF16(notification_id)); | 416 const base::string16 replace_id(base::UTF8ToUTF16(notification_id)); |
| 474 bool success = | 417 bool success = |
| 475 (screenshot_result == ScreenshotTakerObserver::SCREENSHOT_SUCCESS); | 418 (screenshot_result == ScreenshotTakerObserver::SCREENSHOT_SUCCESS); |
| 476 message_center::RichNotificationData optional_field; | 419 message_center::RichNotificationData optional_field; |
| 477 if (success) { | 420 if (success) { |
| 478 const base::string16 label = l10n_util::GetStringUTF16( | 421 const base::string16 label = l10n_util::GetStringUTF16( |
| 479 IDS_MESSAGE_CENTER_NOTIFICATION_BUTTON_COPY_SCREENSHOT_TO_CLIPBOARD); | 422 IDS_MESSAGE_CENTER_NOTIFICATION_BUTTON_COPY_SCREENSHOT_TO_CLIPBOARD); |
| 480 optional_field.buttons.push_back(message_center::ButtonInfo(label)); | 423 optional_field.buttons.push_back(message_center::ButtonInfo(label)); |
| 481 } | 424 } |
| 482 return new Notification( | 425 return new Notification( |
| 483 message_center::NOTIFICATION_TYPE_SIMPLE, | 426 message_center::NOTIFICATION_TYPE_SIMPLE, |
| 484 GURL(kNotificationOriginUrl), | 427 GURL(kNotificationOriginUrl), |
| 485 l10n_util::GetStringUTF16( | 428 l10n_util::GetStringUTF16( |
| 486 GetScreenshotNotificationTitle(screenshot_result)), | 429 GetScreenshotNotificationTitle(screenshot_result)), |
| 487 l10n_util::GetStringUTF16( | 430 l10n_util::GetStringUTF16( |
| 488 GetScreenshotNotificationText(screenshot_result)), | 431 GetScreenshotNotificationText(screenshot_result)), |
| 489 ui::ResourceBundle::GetSharedInstance().GetImageNamed( | 432 ui::ResourceBundle::GetSharedInstance().GetImageNamed( |
| 490 IDR_SCREENSHOT_NOTIFICATION_ICON), | 433 IDR_SCREENSHOT_NOTIFICATION_ICON), |
| 491 blink::WebTextDirectionDefault, | 434 blink::WebTextDirectionDefault, |
| 492 message_center::NotifierId( | 435 message_center::NotifierId( |
| 493 message_center::NotifierId::SYSTEM_COMPONENT, | 436 message_center::NotifierId::SYSTEM_COMPONENT, |
| 494 ash::system_notifier::kNotifierScreenshot), | 437 ash::system_notifier::kNotifierScreenshot), |
| 495 l10n_util::GetStringUTF16(IDS_MESSAGE_CENTER_NOTIFIER_SCREENSHOT_NAME), | 438 l10n_util::GetStringUTF16(IDS_MESSAGE_CENTER_NOTIFIER_SCREENSHOT_NAME), |
| 496 replace_id, | 439 replace_id, |
| 497 optional_field, | 440 optional_field, |
| 498 new ScreenshotTakerNotificationDelegate( | 441 new ScreenshotTakerNotificationDelegate( |
| 499 success, GetProfile(), screenshot_path)); | 442 success, profile, screenshot_path)); |
| 500 } | 443 } |
| 501 #endif | 444 #endif |
| 502 | |
| 503 void ScreenshotTaker::ShowNotification( | |
| 504 ScreenshotTakerObserver::Result screenshot_result, | |
| 505 const base::FilePath& screenshot_path) { | |
| 506 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 507 #if defined(OS_CHROMEOS) | |
| 508 // Do not show a notification that a screenshot was taken while no user is | |
| 509 // logged in, since it is confusing for the user to get a message about it | |
| 510 // after he logs in (crbug.com/235217). | |
| 511 if (!chromeos::LoginState::Get()->IsUserLoggedIn()) | |
| 512 return; | |
| 513 | |
| 514 // TODO(sschmitz): make this work for Windows. | |
| 515 DesktopNotificationService* const service = | |
| 516 DesktopNotificationServiceFactory::GetForProfile(GetProfile()); | |
| 517 if (service->IsNotifierEnabled(message_center::NotifierId( | |
| 518 message_center::NotifierId::SYSTEM_COMPONENT, | |
| 519 ash::system_notifier::kNotifierScreenshot))) { | |
| 520 scoped_ptr<Notification> notification( | |
| 521 CreateNotification(screenshot_result, screenshot_path)); | |
| 522 g_browser_process->notification_ui_manager()->Add(*notification, | |
| 523 GetProfile()); | |
| 524 } | |
| 525 #endif | |
| 526 FOR_EACH_OBSERVER(ScreenshotTakerObserver, observers_, | |
| 527 OnScreenshotCompleted(screenshot_result, screenshot_path)); | |
| 528 } | |
| 529 | |
| 530 void ScreenshotTaker::AddObserver(ScreenshotTakerObserver* observer) { | |
| 531 observers_.AddObserver(observer); | |
| 532 } | |
| 533 | |
| 534 void ScreenshotTaker::RemoveObserver(ScreenshotTakerObserver* observer) { | |
| 535 observers_.RemoveObserver(observer); | |
| 536 } | |
| 537 | |
| 538 bool ScreenshotTaker::HasObserver( | |
| 539 const ScreenshotTakerObserver* observer) const { | |
| 540 return observers_.HasObserver(observer); | |
| 541 } | |
| 542 | |
| 543 void ScreenshotTaker::GrabWindowSnapshotAsyncCallback( | |
| 544 base::FilePath screenshot_path, | |
| 545 bool is_partial, | |
| 546 int window_idx, | |
| 547 scoped_refptr<base::RefCountedBytes> png_data) { | |
| 548 if (!png_data.get()) { | |
| 549 if (is_partial) { | |
| 550 LOG(ERROR) << "Failed to grab the window screenshot"; | |
| 551 ShowNotification( | |
| 552 ScreenshotTakerObserver::SCREENSHOT_GRABWINDOW_PARTIAL_FAILED, | |
| 553 screenshot_path); | |
| 554 } else { | |
| 555 LOG(ERROR) << "Failed to grab the window screenshot for " << window_idx; | |
| 556 ShowNotification( | |
| 557 ScreenshotTakerObserver::SCREENSHOT_GRABWINDOW_FULL_FAILED, | |
| 558 screenshot_path); | |
| 559 } | |
| 560 return; | |
| 561 } | |
| 562 | |
| 563 PostSaveScreenshotTask( | |
| 564 base::Bind(&ScreenshotTaker::ShowNotification, factory_.GetWeakPtr()), | |
| 565 GetProfile(), | |
| 566 screenshot_path, | |
| 567 png_data); | |
| 568 } | |
| 569 | |
| 570 void ScreenshotTaker::GrabPartialWindowSnapshotAsync( | |
| 571 aura::Window* window, | |
| 572 const gfx::Rect& snapshot_bounds, | |
| 573 Profile* profile, | |
| 574 base::FilePath screenshot_path) { | |
| 575 last_screenshot_timestamp_ = base::Time::Now(); | |
| 576 | |
| 577 bool is_partial = true; | |
| 578 int window_idx = -1; // unused | |
| 579 ui::GrabWindowSnapshotAsync( | |
| 580 window, | |
| 581 snapshot_bounds, | |
| 582 content::BrowserThread::GetBlockingPool(), | |
| 583 base::Bind(&ScreenshotTaker::GrabWindowSnapshotAsyncCallback, | |
| 584 factory_.GetWeakPtr(), | |
| 585 screenshot_path, | |
| 586 is_partial, | |
| 587 window_idx)); | |
| 588 } | |
| 589 | |
| 590 void ScreenshotTaker::GrabFullWindowSnapshotAsync( | |
| 591 aura::Window* window, | |
| 592 const gfx::Rect& snapshot_bounds, | |
| 593 Profile* profile, | |
| 594 base::FilePath screenshot_path, | |
| 595 int window_idx) { | |
| 596 last_screenshot_timestamp_ = base::Time::Now(); | |
| 597 | |
| 598 bool is_partial = false; | |
| 599 ui::GrabWindowSnapshotAsync( | |
| 600 window, | |
| 601 snapshot_bounds, | |
| 602 content::BrowserThread::GetBlockingPool(), | |
| 603 base::Bind(&ScreenshotTaker::GrabWindowSnapshotAsyncCallback, | |
| 604 factory_.GetWeakPtr(), | |
| 605 screenshot_path, | |
| 606 is_partial, | |
| 607 window_idx)); | |
| 608 } | |
| 609 | |
| 610 Profile* ScreenshotTaker::GetProfile() { | |
| 611 if (profile_for_test_) | |
| 612 return profile_for_test_; | |
| 613 return ProfileManager::GetActiveUserProfile(); | |
| 614 } | |
| 615 | |
| 616 void ScreenshotTaker::SetScreenshotDirectoryForTest( | |
| 617 const base::FilePath& directory) { | |
| 618 screenshot_directory_for_test_ = directory; | |
| 619 } | |
| 620 | |
| 621 void ScreenshotTaker::SetScreenshotBasenameForTest( | |
| 622 const std::string& basename) { | |
| 623 screenshot_basename_for_test_ = basename; | |
| 624 } | |
| 625 | |
| 626 void ScreenshotTaker::SetScreenshotProfileForTest(Profile* profile) { | |
| 627 profile_for_test_ = profile; | |
| 628 } | |
| OLD | NEW |