Chromium Code Reviews| 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 "base/bind.h" | 11 #include "base/bind.h" |
| 12 #include "base/file_util.h" | 12 #include "base/file_util.h" |
| 13 #include "base/logging.h" | 13 #include "base/logging.h" |
| 14 #include "base/memory/ref_counted_memory.h" | 14 #include "base/memory/ref_counted_memory.h" |
| 15 #include "base/stringprintf.h" | 15 #include "base/stringprintf.h" |
| 16 #include "base/threading/sequenced_worker_pool.h" | 16 #include "base/threading/sequenced_worker_pool.h" |
| 17 #include "base/time.h" | 17 #include "base/time.h" |
| 18 #include "base/utf_string_conversions.h" | 18 #include "base/utf_string_conversions.h" |
| 19 #include "chrome/browser/browser_process.h" | 19 #include "chrome/browser/browser_process.h" |
| 20 #include "chrome/browser/download/download_prefs.h" | 20 #include "chrome/browser/download/download_prefs.h" |
| 21 #include "chrome/browser/notifications/notification.h" | 21 #include "chrome/browser/notifications/notification.h" |
| 22 #include "chrome/browser/notifications/notification_ui_manager.h" | 22 #include "chrome/browser/notifications/notification_ui_manager.h" |
| 23 #include "chrome/browser/profiles/profile.h" | 23 #include "chrome/browser/profiles/profile.h" |
| 24 #include "chrome/browser/profiles/profile_manager.h" | |
| 24 #include "chrome/browser/ui/webui/screenshot_source.h" | 25 #include "chrome/browser/ui/webui/screenshot_source.h" |
| 25 #include "chrome/browser/ui/window_snapshot/window_snapshot.h" | 26 #include "chrome/browser/ui/window_snapshot/window_snapshot.h" |
| 26 #include "content/public/browser/browser_thread.h" | 27 #include "content/public/browser/browser_thread.h" |
| 27 #include "grit/ash_strings.h" | 28 #include "grit/ash_strings.h" |
| 28 #include "grit/theme_resources.h" | 29 #include "grit/theme_resources.h" |
| 29 #include "ui/aura/root_window.h" | 30 #include "ui/aura/root_window.h" |
| 30 #include "ui/aura/window.h" | 31 #include "ui/aura/window.h" |
| 31 #include "ui/base/l10n/l10n_util.h" | 32 #include "ui/base/l10n/l10n_util.h" |
| 32 #include "ui/base/resource/resource_bundle.h" | 33 #include "ui/base/resource/resource_bundle.h" |
| 33 #include "ui/gfx/image/image.h" | 34 #include "ui/gfx/image/image.h" |
| 34 | 35 |
| 35 #if defined(OS_CHROMEOS) | 36 #if defined(OS_CHROMEOS) |
| 36 #include "chrome/browser/chromeos/drive/drive_file_system_util.h" | 37 #include "chrome/browser/chromeos/drive/drive_file_system_util.h" |
| 37 #include "chrome/browser/chromeos/extensions/file_manager/file_manager_util.h" | 38 #include "chrome/browser/chromeos/extensions/file_manager/file_manager_util.h" |
| 38 #include "chrome/browser/chromeos/login/user_manager.h" | 39 #include "chrome/browser/chromeos/login/user_manager.h" |
| 39 #endif | 40 #endif |
| 40 | 41 |
| 41 namespace { | 42 namespace { |
| 42 // The minimum interval between two screenshot commands. It has to be | 43 // The minimum interval between two screenshot commands. It has to be |
| 43 // more than 1000 to prevent the conflict of filenames. | 44 // more than 1000 to prevent the conflict of filenames. |
| 44 const int kScreenshotMinimumIntervalInMS = 1000; | 45 const int kScreenshotMinimumIntervalInMS = 1000; |
| 45 | 46 |
| 46 const char kNotificationOriginUrl[] = "chrome://screenshot"; | 47 const char kNotificationOriginUrl[] = "chrome://screenshot"; |
| 47 | 48 |
| 48 // Delegate for a notification. This class has two roles: to implement callback | 49 // Delegate for a notification. This class has two roles: to implement callback |
| 49 // methods for notification, and to provide an identity of the associated | 50 // methods for notification, and to provide an identity of the associated |
| 50 // notification. | 51 // notification. |
| 51 class ScreenshotTakerNotificationDelegate : public NotificationDelegate { | 52 class ScreenshotTakerNotificationDelegate : public NotificationDelegate { |
| 52 public: | 53 public: |
| 53 ScreenshotTakerNotificationDelegate(const std::string& id_text, | 54 ScreenshotTakerNotificationDelegate(const std::string& notification_id, |
| 54 bool success, | 55 bool success, |
| 55 const base::FilePath& screenshot_path) | 56 const base::FilePath& screenshot_path) |
| 56 : id_text_(id_text), | 57 : notification_id_(notification_id), |
| 57 success_(success), | 58 success_(success), |
| 58 screenshot_path_(screenshot_path) { | 59 screenshot_path_(screenshot_path) { |
| 59 } | 60 } |
| 60 | 61 |
| 61 // Overridden from NotificationDelegate: | 62 // Overridden from NotificationDelegate: |
| 62 virtual void Display() OVERRIDE {} | 63 virtual void Display() OVERRIDE {} |
| 63 virtual void Error() OVERRIDE {} | 64 virtual void Error() OVERRIDE {} |
| 64 virtual void Close(bool by_user) OVERRIDE {} | 65 virtual void Close(bool by_user) OVERRIDE {} |
| 65 virtual void Click() OVERRIDE { | 66 virtual void Click() OVERRIDE { |
| 66 if (!success_) | 67 if (!success_) |
| 67 return; | 68 return; |
| 68 #if defined(OS_CHROMEOS) | 69 #if defined(OS_CHROMEOS) |
| 69 file_manager_util::ShowFileInFolder(screenshot_path_); | 70 file_manager_util::ShowFileInFolder(screenshot_path_); |
| 70 #else | 71 #else |
| 71 // TODO(sschmitz): perhaps add similar action for Windows. | 72 // TODO(sschmitz): perhaps add similar action for Windows. |
| 72 #endif | 73 #endif |
| 73 } | 74 } |
| 74 virtual std::string id() const OVERRIDE { return id_text_; } | 75 virtual std::string id() const OVERRIDE { return notification_id_; } |
| 75 virtual content::RenderViewHost* GetRenderViewHost() const OVERRIDE { | 76 virtual content::RenderViewHost* GetRenderViewHost() const OVERRIDE { |
| 76 return NULL; | 77 return NULL; |
| 77 } | 78 } |
| 78 | 79 |
| 79 private: | 80 private: |
| 80 virtual ~ScreenshotTakerNotificationDelegate() {} | 81 virtual ~ScreenshotTakerNotificationDelegate() {} |
| 81 | 82 |
| 82 const std::string id_text_; | 83 const std::string notification_id_; |
| 83 const bool success_; | 84 const bool success_; |
| 84 const base::FilePath screenshot_path_; | 85 const base::FilePath screenshot_path_; |
| 85 | 86 |
| 86 DISALLOW_COPY_AND_ASSIGN(ScreenshotTakerNotificationDelegate); | 87 DISALLOW_COPY_AND_ASSIGN(ScreenshotTakerNotificationDelegate); |
| 87 }; | 88 }; |
| 88 | 89 |
| 89 typedef base::Callback< | 90 typedef base::Callback< |
| 90 void(ScreenshotTakerObserver::Result screenshot_result, | 91 void(ScreenshotTakerObserver::Result screenshot_result, |
| 91 const base::FilePath& screenshot_path)> ShowNotificationCallback; | 92 const base::FilePath& screenshot_path)> ShowNotificationCallback; |
| 92 | 93 |
| (...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 216 // TODO(mukai): remove this when the performance issue has been fixed. | 217 // TODO(mukai): remove this when the performance issue has been fixed. |
| 217 if (window->GetRootWindow()->GrabSnapshot(snapshot_bounds, png_data)) | 218 if (window->GetRootWindow()->GrabSnapshot(snapshot_bounds, png_data)) |
| 218 return true; | 219 return true; |
| 219 #endif // OS_LINUX | 220 #endif // OS_LINUX |
| 220 | 221 |
| 221 return chrome::GrabWindowSnapshotForUser(window, png_data, snapshot_bounds); | 222 return chrome::GrabWindowSnapshotForUser(window, png_data, snapshot_bounds); |
| 222 } | 223 } |
| 223 | 224 |
| 224 } // namespace | 225 } // namespace |
| 225 | 226 |
| 226 ScreenshotTaker::ScreenshotTaker(Profile* profile) | 227 ScreenshotTaker::ScreenshotTaker() |
| 227 : profile_(profile), | 228 : ALLOW_THIS_IN_INITIALIZER_LIST(factory_(this)), |
| 228 ALLOW_THIS_IN_INITIALIZER_LIST(factory_(this)) { | 229 profile_for_test_(NULL) { |
| 229 DCHECK(profile_); | |
| 230 } | 230 } |
| 231 | 231 |
| 232 ScreenshotTaker::~ScreenshotTaker() { | 232 ScreenshotTaker::~ScreenshotTaker() { |
| 233 } | 233 } |
| 234 | 234 |
| 235 void ScreenshotTaker::HandleTakeScreenshotForAllRootWindows() { | 235 void ScreenshotTaker::HandleTakeScreenshotForAllRootWindows() { |
| 236 base::FilePath screenshot_directory; | 236 base::FilePath screenshot_directory; |
| 237 if (!screenshot_directory_for_test_.empty()) { | 237 if (!screenshot_directory_for_test_.empty()) { |
| 238 screenshot_directory = screenshot_directory_for_test_; | 238 screenshot_directory = screenshot_directory_for_test_; |
| 239 } else if (!ScreenshotSource::GetScreenshotDirectory(&screenshot_directory)) { | 239 } else if (!ScreenshotSource::GetScreenshotDirectory(&screenshot_directory)) { |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 258 scoped_refptr<base::RefCountedBytes> png_data(new base::RefCountedBytes); | 258 scoped_refptr<base::RefCountedBytes> png_data(new base::RefCountedBytes); |
| 259 std::string basename = screenshot_basename; | 259 std::string basename = screenshot_basename; |
| 260 gfx::Rect rect = root_window->bounds(); | 260 gfx::Rect rect = root_window->bounds(); |
| 261 if (root_windows.size() > 1) | 261 if (root_windows.size() > 1) |
| 262 basename += base::StringPrintf(" - Display %d", static_cast<int>(i + 1)); | 262 basename += base::StringPrintf(" - Display %d", static_cast<int>(i + 1)); |
| 263 base::FilePath screenshot_path = | 263 base::FilePath screenshot_path = |
| 264 screenshot_directory.AppendASCII(basename + ".png"); | 264 screenshot_directory.AppendASCII(basename + ".png"); |
| 265 if (GrabWindowSnapshot(root_window, rect, &png_data->data())) { | 265 if (GrabWindowSnapshot(root_window, rect, &png_data->data())) { |
| 266 PostSaveScreenshotTask( | 266 PostSaveScreenshotTask( |
| 267 base::Bind(&ScreenshotTaker::ShowNotification, factory_.GetWeakPtr()), | 267 base::Bind(&ScreenshotTaker::ShowNotification, factory_.GetWeakPtr()), |
| 268 profile_, | 268 GetProfile(), |
| 269 screenshot_path, | 269 screenshot_path, |
| 270 png_data); | 270 png_data); |
| 271 } else { | 271 } else { |
| 272 LOG(ERROR) << "Failed to grab the window screenshot for " << i; | 272 LOG(ERROR) << "Failed to grab the window screenshot for " << i; |
| 273 ShowNotification( | 273 ShowNotification( |
| 274 ScreenshotTakerObserver::SCREENSHOT_GRABWINDOW_FULL_FAILED, | 274 ScreenshotTakerObserver::SCREENSHOT_GRABWINDOW_FULL_FAILED, |
| 275 screenshot_path); | 275 screenshot_path); |
| 276 } | 276 } |
| 277 } | 277 } |
| 278 last_screenshot_timestamp_ = base::Time::Now(); | 278 last_screenshot_timestamp_ = base::Time::Now(); |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 295 | 295 |
| 296 std::string screenshot_basename = !screenshot_basename_for_test_.empty() ? | 296 std::string screenshot_basename = !screenshot_basename_for_test_.empty() ? |
| 297 screenshot_basename_for_test_ : | 297 screenshot_basename_for_test_ : |
| 298 ScreenshotSource::GetScreenshotBaseFilename(); | 298 ScreenshotSource::GetScreenshotBaseFilename(); |
| 299 base::FilePath screenshot_path = | 299 base::FilePath screenshot_path = |
| 300 screenshot_directory.AppendASCII(screenshot_basename + ".png"); | 300 screenshot_directory.AppendASCII(screenshot_basename + ".png"); |
| 301 if (GrabWindowSnapshot(window, rect, &png_data->data())) { | 301 if (GrabWindowSnapshot(window, rect, &png_data->data())) { |
| 302 last_screenshot_timestamp_ = base::Time::Now(); | 302 last_screenshot_timestamp_ = base::Time::Now(); |
| 303 PostSaveScreenshotTask( | 303 PostSaveScreenshotTask( |
| 304 base::Bind(&ScreenshotTaker::ShowNotification, factory_.GetWeakPtr()), | 304 base::Bind(&ScreenshotTaker::ShowNotification, factory_.GetWeakPtr()), |
| 305 profile_, | 305 GetProfile(), |
| 306 screenshot_path, | 306 screenshot_path, |
| 307 png_data); | 307 png_data); |
| 308 } else { | 308 } else { |
| 309 LOG(ERROR) << "Failed to grab the window screenshot"; | 309 LOG(ERROR) << "Failed to grab the window screenshot"; |
| 310 ShowNotification( | 310 ShowNotification( |
| 311 ScreenshotTakerObserver::SCREENSHOT_GRABWINDOW_PARTIAL_FAILED, | 311 ScreenshotTakerObserver::SCREENSHOT_GRABWINDOW_PARTIAL_FAILED, |
| 312 screenshot_path); | 312 screenshot_path); |
| 313 } | 313 } |
| 314 } | 314 } |
| 315 | 315 |
| 316 bool ScreenshotTaker::CanTakeScreenshot() { | 316 bool ScreenshotTaker::CanTakeScreenshot() { |
| 317 return last_screenshot_timestamp_.is_null() || | 317 return last_screenshot_timestamp_.is_null() || |
| 318 base::Time::Now() - last_screenshot_timestamp_ > | 318 base::Time::Now() - last_screenshot_timestamp_ > |
| 319 base::TimeDelta::FromMilliseconds( | 319 base::TimeDelta::FromMilliseconds( |
| 320 kScreenshotMinimumIntervalInMS); | 320 kScreenshotMinimumIntervalInMS); |
| 321 } | 321 } |
| 322 | 322 |
| 323 void ScreenshotTaker::ShowNotification( | 323 void ScreenshotTaker::ShowNotification( |
| 324 ScreenshotTakerObserver::Result screenshot_result, | 324 ScreenshotTakerObserver::Result screenshot_result, |
| 325 const base::FilePath& screenshot_path) { | 325 const base::FilePath& screenshot_path) { |
| 326 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 326 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 327 #if defined(OS_CHROMEOS) | 327 #if defined(OS_CHROMEOS) |
| 328 // TODO(sschmitz): make this work for Windows. | 328 // TODO(sschmitz): make this work for Windows. |
| 329 if (!notification_id_.empty()) | |
| 330 g_browser_process->notification_ui_manager()->CancelById(notification_id_); | |
|
James Cook
2013/04/18 23:46:09
Why not just always give screenshots the same ID,
sschmitz
2013/04/19 00:51:28
I talked to mukai@. And, yes, its ok and better to
| |
| 329 static int id = 0; | 331 static int id = 0; |
| 330 std::string id_text = base::StringPrintf("screenshot_%3.3d", ++id); | 332 notification_id_ = base::StringPrintf("screenshot_%3.3d", ++id); |
| 331 string16 replace_id = UTF8ToUTF16(id_text); | 333 string16 replace_id = UTF8ToUTF16("screenshot"); |
| 332 bool success = | 334 bool success = |
| 333 (screenshot_result == ScreenshotTakerObserver::SCREENSHOT_SUCCESS); | 335 (screenshot_result == ScreenshotTakerObserver::SCREENSHOT_SUCCESS); |
| 334 Notification notification( | 336 Notification notification( |
| 335 GURL(kNotificationOriginUrl), | 337 GURL(kNotificationOriginUrl), |
| 336 ui::ResourceBundle::GetSharedInstance().GetImageNamed( | 338 ui::ResourceBundle::GetSharedInstance().GetImageNamed( |
| 337 IDR_SCREENSHOT_NOTIFICATION_ICON), | 339 IDR_SCREENSHOT_NOTIFICATION_ICON), |
| 338 l10n_util::GetStringUTF16( | 340 l10n_util::GetStringUTF16( |
| 339 success ? | 341 success ? |
| 340 IDS_ASH_SCREENSHOT_NOTIFICATION_TITLE_SUCCESS : | 342 IDS_ASH_SCREENSHOT_NOTIFICATION_TITLE_SUCCESS : |
| 341 IDS_ASH_SCREENSHOT_NOTIFICATION_TITLE_FAIL), | 343 IDS_ASH_SCREENSHOT_NOTIFICATION_TITLE_FAIL), |
| 342 l10n_util::GetStringUTF16( | 344 l10n_util::GetStringUTF16( |
| 343 success ? | 345 success ? |
| 344 IDS_ASH_SCREENSHOT_NOTIFICATION_TEXT_SUCCESS : | 346 IDS_ASH_SCREENSHOT_NOTIFICATION_TEXT_SUCCESS : |
| 345 IDS_ASH_SCREENSHOT_NOTIFICATION_TEXT_FAIL), | 347 IDS_ASH_SCREENSHOT_NOTIFICATION_TEXT_FAIL), |
| 346 WebKit::WebTextDirectionDefault, | 348 WebKit::WebTextDirectionDefault, |
| 347 string16(), | 349 string16(), |
| 348 replace_id, | 350 replace_id, |
| 349 new ScreenshotTakerNotificationDelegate(id_text, | 351 new ScreenshotTakerNotificationDelegate(notification_id_, |
| 350 success, | 352 success, |
| 351 screenshot_path)); | 353 screenshot_path)); |
| 352 g_browser_process->notification_ui_manager()->Add(notification, profile_); | 354 g_browser_process->notification_ui_manager()->Add(notification, |
| 355 GetProfile()); | |
| 353 #endif | 356 #endif |
| 354 | 357 |
| 355 FOR_EACH_OBSERVER(ScreenshotTakerObserver, observers_, | 358 FOR_EACH_OBSERVER(ScreenshotTakerObserver, observers_, |
| 356 OnScreenshotCompleted(screenshot_result, screenshot_path)); | 359 OnScreenshotCompleted(screenshot_result, screenshot_path)); |
| 357 } | 360 } |
| 358 | 361 |
| 359 void ScreenshotTaker::AddObserver(ScreenshotTakerObserver* observer) { | 362 void ScreenshotTaker::AddObserver(ScreenshotTakerObserver* observer) { |
| 360 observers_.AddObserver(observer); | 363 observers_.AddObserver(observer); |
| 361 } | 364 } |
| 362 | 365 |
| 363 void ScreenshotTaker::RemoveObserver(ScreenshotTakerObserver* observer) { | 366 void ScreenshotTaker::RemoveObserver(ScreenshotTakerObserver* observer) { |
| 364 observers_.RemoveObserver(observer); | 367 observers_.RemoveObserver(observer); |
| 365 } | 368 } |
| 366 | 369 |
| 367 bool ScreenshotTaker::HasObserver(ScreenshotTakerObserver* observer) const { | 370 bool ScreenshotTaker::HasObserver(ScreenshotTakerObserver* observer) const { |
| 368 return observers_.HasObserver(observer); | 371 return observers_.HasObserver(observer); |
| 369 } | 372 } |
| 370 | 373 |
| 374 Profile* ScreenshotTaker::GetProfile() { | |
| 375 if (profile_for_test_) | |
| 376 return profile_for_test_; | |
| 377 return ProfileManager::GetDefaultProfileOrOffTheRecord(); | |
| 378 } | |
| 379 | |
| 371 void ScreenshotTaker::SetScreenshotDirectoryForTest( | 380 void ScreenshotTaker::SetScreenshotDirectoryForTest( |
| 372 const base::FilePath& directory) { | 381 const base::FilePath& directory) { |
| 373 screenshot_directory_for_test_ = directory; | 382 screenshot_directory_for_test_ = directory; |
| 374 } | 383 } |
| 375 | 384 |
| 376 void ScreenshotTaker::SetScreenshotBasenameForTest( | 385 void ScreenshotTaker::SetScreenshotBasenameForTest( |
| 377 const std::string& basename){ | 386 const std::string& basename) { |
| 378 screenshot_basename_for_test_ = basename; | 387 screenshot_basename_for_test_ = basename; |
| 379 } | 388 } |
| 389 | |
| 390 void ScreenshotTaker::SetScreenshotProfileForTest(Profile* profile) { | |
| 391 profile_for_test_ = profile; | |
| 392 } | |
| OLD | NEW |