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 |
| 47 const char kNotificationId[] = "screenshot"; | |
| 48 | |
| 46 const char kNotificationOriginUrl[] = "chrome://screenshot"; | 49 const char kNotificationOriginUrl[] = "chrome://screenshot"; |
| 47 | 50 |
| 48 // Delegate for a notification. This class has two roles: to implement callback | 51 // Delegate for a notification. This class has two roles: to implement callback |
| 49 // methods for notification, and to provide an identity of the associated | 52 // methods for notification, and to provide an identity of the associated |
| 50 // notification. | 53 // notification. |
| 51 class ScreenshotTakerNotificationDelegate : public NotificationDelegate { | 54 class ScreenshotTakerNotificationDelegate : public NotificationDelegate { |
| 52 public: | 55 public: |
| 53 ScreenshotTakerNotificationDelegate(const std::string& id_text, | 56 ScreenshotTakerNotificationDelegate(bool success, |
| 54 bool success, | |
| 55 const base::FilePath& screenshot_path) | 57 const base::FilePath& screenshot_path) |
| 56 : id_text_(id_text), | 58 : success_(success), |
| 57 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 { |
| 76 return std::string(kNotificationId); | |
| 77 } | |
| 75 virtual content::RenderViewHost* GetRenderViewHost() const OVERRIDE { | 78 virtual content::RenderViewHost* GetRenderViewHost() const OVERRIDE { |
| 76 return NULL; | 79 return NULL; |
| 77 } | 80 } |
| 78 | 81 |
| 79 private: | 82 private: |
| 80 virtual ~ScreenshotTakerNotificationDelegate() {} | 83 virtual ~ScreenshotTakerNotificationDelegate() {} |
| 81 | 84 |
| 82 const std::string id_text_; | |
| 83 const bool success_; | 85 const bool success_; |
| 84 const base::FilePath screenshot_path_; | 86 const base::FilePath screenshot_path_; |
| 85 | 87 |
| 86 DISALLOW_COPY_AND_ASSIGN(ScreenshotTakerNotificationDelegate); | 88 DISALLOW_COPY_AND_ASSIGN(ScreenshotTakerNotificationDelegate); |
| 87 }; | 89 }; |
| 88 | 90 |
| 89 typedef base::Callback< | 91 typedef base::Callback< |
| 90 void(ScreenshotTakerObserver::Result screenshot_result, | 92 void(ScreenshotTakerObserver::Result screenshot_result, |
| 91 const base::FilePath& screenshot_path)> ShowNotificationCallback; | 93 const base::FilePath& screenshot_path)> ShowNotificationCallback; |
| 92 | 94 |
| 93 void SaveScreenshotInternal(const ShowNotificationCallback& callback, | 95 void SaveScreenshotInternal(const ShowNotificationCallback& callback, |
| 94 const base::FilePath& screenshot_path, | 96 const base::FilePath& screenshot_path, |
| 97 const base::FilePath& local_path, | |
| 95 scoped_refptr<base::RefCountedBytes> png_data) { | 98 scoped_refptr<base::RefCountedBytes> png_data) { |
| 96 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); | 99 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); |
| 97 DCHECK(!screenshot_path.empty()); | 100 DCHECK(!local_path.empty()); |
| 98 ScreenshotTakerObserver::Result result = | 101 ScreenshotTakerObserver::Result result = |
| 99 ScreenshotTakerObserver::SCREENSHOT_SUCCESS; | 102 ScreenshotTakerObserver::SCREENSHOT_SUCCESS; |
| 100 if (static_cast<size_t>(file_util::WriteFile( | 103 if (static_cast<size_t>(file_util::WriteFile( |
| 101 screenshot_path, | 104 local_path, |
| 102 reinterpret_cast<char*>(&(png_data->data()[0])), | 105 reinterpret_cast<char*>(&(png_data->data()[0])), |
| 103 png_data->size())) != png_data->size()) { | 106 png_data->size())) != png_data->size()) { |
| 104 LOG(ERROR) << "Failed to save to " << screenshot_path.value(); | 107 LOG(ERROR) << "Failed to save to " << local_path.value(); |
| 105 result = ScreenshotTakerObserver::SCREENSHOT_WRITE_FILE_FAILED; | 108 result = ScreenshotTakerObserver::SCREENSHOT_WRITE_FILE_FAILED; |
| 106 } | 109 } |
| 107 content::BrowserThread::PostTask( | 110 content::BrowserThread::PostTask( |
| 108 content::BrowserThread::UI, FROM_HERE, | 111 content::BrowserThread::UI, FROM_HERE, |
| 109 base::Bind(callback, result, screenshot_path)); | 112 base::Bind(callback, result, screenshot_path)); |
| 110 } | 113 } |
| 111 | 114 |
| 112 void SaveScreenshot(const ShowNotificationCallback& callback, | 115 void SaveScreenshot(const ShowNotificationCallback& callback, |
| 113 const base::FilePath& screenshot_path, | 116 const base::FilePath& screenshot_path, |
| 114 scoped_refptr<base::RefCountedBytes> png_data) { | 117 scoped_refptr<base::RefCountedBytes> png_data) { |
| 115 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); | 118 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); |
| 116 DCHECK(!screenshot_path.empty()); | 119 DCHECK(!screenshot_path.empty()); |
| 117 | 120 |
| 118 if (!file_util::CreateDirectory(screenshot_path.DirName())) { | 121 if (!file_util::CreateDirectory(screenshot_path.DirName())) { |
| 119 LOG(ERROR) << "Failed to ensure the existence of " | 122 LOG(ERROR) << "Failed to ensure the existence of " |
| 120 << screenshot_path.DirName().value(); | 123 << screenshot_path.DirName().value(); |
| 121 content::BrowserThread::PostTask( | 124 content::BrowserThread::PostTask( |
| 122 content::BrowserThread::UI, FROM_HERE, | 125 content::BrowserThread::UI, FROM_HERE, |
| 123 base::Bind(callback, | 126 base::Bind(callback, |
| 124 ScreenshotTakerObserver::SCREENSHOT_CREATE_DIR_FAILED, | 127 ScreenshotTakerObserver::SCREENSHOT_CREATE_DIR_FAILED, |
| 125 screenshot_path)); | 128 screenshot_path)); |
| 126 return; | 129 return; |
| 127 } | 130 } |
| 128 SaveScreenshotInternal(callback, screenshot_path, png_data); | 131 SaveScreenshotInternal(callback, screenshot_path, screenshot_path, png_data); |
| 129 } | 132 } |
| 130 | 133 |
| 131 // TODO(kinaba): crbug.com/140425, remove this ungly #ifdef dispatch. | 134 // TODO(kinaba): crbug.com/140425, remove this ungly #ifdef dispatch. |
| 132 #if defined(OS_CHROMEOS) | 135 #if defined(OS_CHROMEOS) |
| 133 void SaveScreenshotToDrive(const ShowNotificationCallback& callback, | 136 void SaveScreenshotToDrive(const ShowNotificationCallback& callback, |
| 137 const base::FilePath& screenshot_path, | |
| 134 scoped_refptr<base::RefCountedBytes> png_data, | 138 scoped_refptr<base::RefCountedBytes> png_data, |
| 135 drive::DriveFileError error, | 139 drive::DriveFileError error, |
| 136 const base::FilePath& local_path) { | 140 const base::FilePath& local_path) { |
| 141 // |screenshot_path| is used in the notification callback. | |
| 142 // |local_path| is a temporary file in a hidden cache directory used for | |
| 143 // internal work generated by drive::util::PrepareWritableFileAndRun. | |
| 137 if (error != drive::DRIVE_FILE_OK) { | 144 if (error != drive::DRIVE_FILE_OK) { |
| 138 LOG(ERROR) << "Failed to write screenshot image to Google Drive: " << error; | 145 LOG(ERROR) << "Failed to write screenshot image to Google Drive: " << error; |
| 139 content::BrowserThread::PostTask( | 146 content::BrowserThread::PostTask( |
| 140 content::BrowserThread::UI, FROM_HERE, | 147 content::BrowserThread::UI, FROM_HERE, |
| 141 base::Bind(callback, | 148 base::Bind(callback, |
| 142 ScreenshotTakerObserver::SCREENSHOT_CREATE_FILE_FAILED, | 149 ScreenshotTakerObserver::SCREENSHOT_CREATE_FILE_FAILED, |
| 143 local_path)); | 150 screenshot_path)); |
| 144 return; | 151 return; |
| 145 } | 152 } |
| 146 SaveScreenshotInternal(callback, local_path, png_data); | 153 SaveScreenshotInternal(callback, screenshot_path, local_path, png_data); |
| 147 } | 154 } |
| 148 | 155 |
| 149 void EnsureDirectoryExistsCallback( | 156 void EnsureDirectoryExistsCallback( |
| 150 const ShowNotificationCallback& callback, | 157 const ShowNotificationCallback& callback, |
| 151 Profile* profile, | 158 Profile* profile, |
| 152 const base::FilePath& screenshot_path, | 159 const base::FilePath& screenshot_path, |
| 153 scoped_refptr<base::RefCountedBytes> png_data, | 160 scoped_refptr<base::RefCountedBytes> png_data, |
| 154 drive::DriveFileError error) { | 161 drive::DriveFileError error) { |
| 155 // It is okay to fail with DRIVE_FILE_ERROR_EXISTS since anyway the directory | 162 // It is okay to fail with DRIVE_FILE_ERROR_EXISTS since anyway the directory |
| 156 // of the target file exists. | 163 // of the target file exists. |
| 157 if (error == drive::DRIVE_FILE_OK || | 164 if (error == drive::DRIVE_FILE_OK || |
| 158 error == drive::DRIVE_FILE_ERROR_EXISTS) { | 165 error == drive::DRIVE_FILE_ERROR_EXISTS) { |
| 159 drive::util::PrepareWritableFileAndRun( | 166 drive::util::PrepareWritableFileAndRun( |
| 160 profile, | 167 profile, |
| 161 screenshot_path, | 168 screenshot_path, |
| 162 base::Bind(&SaveScreenshotToDrive, callback, png_data)); | 169 base::Bind(&SaveScreenshotToDrive, |
| 170 callback, | |
| 171 screenshot_path, | |
| 172 png_data)); | |
| 163 } else { | 173 } else { |
| 164 LOG(ERROR) << "Failed to ensure the existence of the specified directory " | 174 LOG(ERROR) << "Failed to ensure the existence of the specified directory " |
| 165 << "in Google Drive: " << error; | 175 << "in Google Drive: " << error; |
| 166 callback.Run(ScreenshotTakerObserver::SCREENSHOT_CHECK_DIR_FAILED, | 176 callback.Run(ScreenshotTakerObserver::SCREENSHOT_CHECK_DIR_FAILED, |
| 167 screenshot_path); | 177 screenshot_path); |
| 168 } | 178 } |
| 169 } | 179 } |
| 170 | 180 |
| 171 void PostSaveScreenshotTask(const ShowNotificationCallback& callback, | 181 void PostSaveScreenshotTask(const ShowNotificationCallback& callback, |
| 172 Profile* profile, | 182 Profile* profile, |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 216 // TODO(mukai): remove this when the performance issue has been fixed. | 226 // TODO(mukai): remove this when the performance issue has been fixed. |
| 217 if (window->GetRootWindow()->GrabSnapshot(snapshot_bounds, png_data)) | 227 if (window->GetRootWindow()->GrabSnapshot(snapshot_bounds, png_data)) |
| 218 return true; | 228 return true; |
| 219 #endif // OS_LINUX | 229 #endif // OS_LINUX |
| 220 | 230 |
| 221 return chrome::GrabWindowSnapshotForUser(window, png_data, snapshot_bounds); | 231 return chrome::GrabWindowSnapshotForUser(window, png_data, snapshot_bounds); |
| 222 } | 232 } |
| 223 | 233 |
| 224 } // namespace | 234 } // namespace |
| 225 | 235 |
| 226 ScreenshotTaker::ScreenshotTaker(Profile* profile) | 236 ScreenshotTaker::ScreenshotTaker() |
| 227 : profile_(profile), | 237 : ALLOW_THIS_IN_INITIALIZER_LIST(factory_(this)), |
| 228 ALLOW_THIS_IN_INITIALIZER_LIST(factory_(this)) { | 238 profile_for_test_(NULL) { |
| 229 DCHECK(profile_); | |
| 230 } | 239 } |
| 231 | 240 |
| 232 ScreenshotTaker::~ScreenshotTaker() { | 241 ScreenshotTaker::~ScreenshotTaker() { |
| 233 } | 242 } |
| 234 | 243 |
| 235 void ScreenshotTaker::HandleTakeScreenshotForAllRootWindows() { | 244 void ScreenshotTaker::HandleTakeScreenshotForAllRootWindows() { |
| 236 base::FilePath screenshot_directory; | 245 base::FilePath screenshot_directory; |
| 237 if (!screenshot_directory_for_test_.empty()) { | 246 if (!screenshot_directory_for_test_.empty()) { |
| 238 screenshot_directory = screenshot_directory_for_test_; | 247 screenshot_directory = screenshot_directory_for_test_; |
| 239 } else if (!ScreenshotSource::GetScreenshotDirectory(&screenshot_directory)) { | 248 } else if (!ScreenshotSource::GetScreenshotDirectory(&screenshot_directory)) { |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 258 scoped_refptr<base::RefCountedBytes> png_data(new base::RefCountedBytes); | 267 scoped_refptr<base::RefCountedBytes> png_data(new base::RefCountedBytes); |
| 259 std::string basename = screenshot_basename; | 268 std::string basename = screenshot_basename; |
| 260 gfx::Rect rect = root_window->bounds(); | 269 gfx::Rect rect = root_window->bounds(); |
| 261 if (root_windows.size() > 1) | 270 if (root_windows.size() > 1) |
| 262 basename += base::StringPrintf(" - Display %d", static_cast<int>(i + 1)); | 271 basename += base::StringPrintf(" - Display %d", static_cast<int>(i + 1)); |
| 263 base::FilePath screenshot_path = | 272 base::FilePath screenshot_path = |
| 264 screenshot_directory.AppendASCII(basename + ".png"); | 273 screenshot_directory.AppendASCII(basename + ".png"); |
| 265 if (GrabWindowSnapshot(root_window, rect, &png_data->data())) { | 274 if (GrabWindowSnapshot(root_window, rect, &png_data->data())) { |
| 266 PostSaveScreenshotTask( | 275 PostSaveScreenshotTask( |
| 267 base::Bind(&ScreenshotTaker::ShowNotification, factory_.GetWeakPtr()), | 276 base::Bind(&ScreenshotTaker::ShowNotification, factory_.GetWeakPtr()), |
| 268 profile_, | 277 GetProfile(), |
| 269 screenshot_path, | 278 screenshot_path, |
| 270 png_data); | 279 png_data); |
| 271 } else { | 280 } else { |
| 272 LOG(ERROR) << "Failed to grab the window screenshot for " << i; | 281 LOG(ERROR) << "Failed to grab the window screenshot for " << i; |
| 273 ShowNotification( | 282 ShowNotification( |
| 274 ScreenshotTakerObserver::SCREENSHOT_GRABWINDOW_FULL_FAILED, | 283 ScreenshotTakerObserver::SCREENSHOT_GRABWINDOW_FULL_FAILED, |
| 275 screenshot_path); | 284 screenshot_path); |
| 276 } | 285 } |
| 277 } | 286 } |
| 278 last_screenshot_timestamp_ = base::Time::Now(); | 287 last_screenshot_timestamp_ = base::Time::Now(); |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 295 | 304 |
| 296 std::string screenshot_basename = !screenshot_basename_for_test_.empty() ? | 305 std::string screenshot_basename = !screenshot_basename_for_test_.empty() ? |
| 297 screenshot_basename_for_test_ : | 306 screenshot_basename_for_test_ : |
| 298 ScreenshotSource::GetScreenshotBaseFilename(); | 307 ScreenshotSource::GetScreenshotBaseFilename(); |
| 299 base::FilePath screenshot_path = | 308 base::FilePath screenshot_path = |
| 300 screenshot_directory.AppendASCII(screenshot_basename + ".png"); | 309 screenshot_directory.AppendASCII(screenshot_basename + ".png"); |
| 301 if (GrabWindowSnapshot(window, rect, &png_data->data())) { | 310 if (GrabWindowSnapshot(window, rect, &png_data->data())) { |
| 302 last_screenshot_timestamp_ = base::Time::Now(); | 311 last_screenshot_timestamp_ = base::Time::Now(); |
| 303 PostSaveScreenshotTask( | 312 PostSaveScreenshotTask( |
| 304 base::Bind(&ScreenshotTaker::ShowNotification, factory_.GetWeakPtr()), | 313 base::Bind(&ScreenshotTaker::ShowNotification, factory_.GetWeakPtr()), |
| 305 profile_, | 314 GetProfile(), |
| 306 screenshot_path, | 315 screenshot_path, |
| 307 png_data); | 316 png_data); |
| 308 } else { | 317 } else { |
| 309 LOG(ERROR) << "Failed to grab the window screenshot"; | 318 LOG(ERROR) << "Failed to grab the window screenshot"; |
| 310 ShowNotification( | 319 ShowNotification( |
| 311 ScreenshotTakerObserver::SCREENSHOT_GRABWINDOW_PARTIAL_FAILED, | 320 ScreenshotTakerObserver::SCREENSHOT_GRABWINDOW_PARTIAL_FAILED, |
| 312 screenshot_path); | 321 screenshot_path); |
| 313 } | 322 } |
| 314 } | 323 } |
| 315 | 324 |
| 316 bool ScreenshotTaker::CanTakeScreenshot() { | 325 bool ScreenshotTaker::CanTakeScreenshot() { |
| 317 return last_screenshot_timestamp_.is_null() || | 326 return last_screenshot_timestamp_.is_null() || |
| 318 base::Time::Now() - last_screenshot_timestamp_ > | 327 base::Time::Now() - last_screenshot_timestamp_ > |
| 319 base::TimeDelta::FromMilliseconds( | 328 base::TimeDelta::FromMilliseconds( |
| 320 kScreenshotMinimumIntervalInMS); | 329 kScreenshotMinimumIntervalInMS); |
| 321 } | 330 } |
| 322 | 331 |
| 323 void ScreenshotTaker::ShowNotification( | 332 void ScreenshotTaker::ShowNotification( |
| 324 ScreenshotTakerObserver::Result screenshot_result, | 333 ScreenshotTakerObserver::Result screenshot_result, |
| 325 const base::FilePath& screenshot_path) { | 334 const base::FilePath& screenshot_path) { |
| 326 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 335 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 327 #if defined(OS_CHROMEOS) | 336 #if defined(OS_CHROMEOS) |
| 328 // TODO(sschmitz): make this work for Windows. | 337 // TODO(sschmitz): make this work for Windows. |
| 329 static int id = 0; | 338 const std::string notification_id(kNotificationId); |
| 330 std::string id_text = base::StringPrintf("screenshot_%3.3d", ++id); | 339 // We cancel a previous screenshot notification, if any, to ensure we get |
| 331 string16 replace_id = UTF8ToUTF16(id_text); | 340 // a fresh notification pop-up. |
| 341 g_browser_process->notification_ui_manager()->CancelById(notification_id); | |
| 342 const string16 replace_id(UTF8ToUTF16(notification_id)); | |
| 332 bool success = | 343 bool success = |
| 333 (screenshot_result == ScreenshotTakerObserver::SCREENSHOT_SUCCESS); | 344 (screenshot_result == ScreenshotTakerObserver::SCREENSHOT_SUCCESS); |
| 334 Notification notification( | 345 Notification notification( |
| 335 GURL(kNotificationOriginUrl), | 346 GURL(kNotificationOriginUrl), |
| 336 ui::ResourceBundle::GetSharedInstance().GetImageNamed( | 347 ui::ResourceBundle::GetSharedInstance().GetImageNamed( |
| 337 IDR_SCREENSHOT_NOTIFICATION_ICON), | 348 IDR_SCREENSHOT_NOTIFICATION_ICON), |
| 338 l10n_util::GetStringUTF16( | 349 l10n_util::GetStringUTF16( |
| 339 success ? | 350 success ? |
| 340 IDS_ASH_SCREENSHOT_NOTIFICATION_TITLE_SUCCESS : | 351 IDS_ASH_SCREENSHOT_NOTIFICATION_TITLE_SUCCESS : |
| 341 IDS_ASH_SCREENSHOT_NOTIFICATION_TITLE_FAIL), | 352 IDS_ASH_SCREENSHOT_NOTIFICATION_TITLE_FAIL), |
| 342 l10n_util::GetStringUTF16( | 353 l10n_util::GetStringUTF16( |
| 343 success ? | 354 success ? |
| 344 IDS_ASH_SCREENSHOT_NOTIFICATION_TEXT_SUCCESS : | 355 IDS_ASH_SCREENSHOT_NOTIFICATION_TEXT_SUCCESS : |
| 345 IDS_ASH_SCREENSHOT_NOTIFICATION_TEXT_FAIL), | 356 IDS_ASH_SCREENSHOT_NOTIFICATION_TEXT_FAIL), |
| 346 WebKit::WebTextDirectionDefault, | 357 WebKit::WebTextDirectionDefault, |
| 347 string16(), | 358 string16(), |
| 348 replace_id, | 359 replace_id, |
| 349 new ScreenshotTakerNotificationDelegate(id_text, | 360 new ScreenshotTakerNotificationDelegate(success, screenshot_path)); |
| 350 success, | 361 g_browser_process->notification_ui_manager()->Add(notification, |
| 351 screenshot_path)); | 362 GetProfile()); |
| 352 g_browser_process->notification_ui_manager()->Add(notification, profile_); | |
| 353 #endif | 363 #endif |
| 354 | 364 |
| 355 FOR_EACH_OBSERVER(ScreenshotTakerObserver, observers_, | 365 FOR_EACH_OBSERVER(ScreenshotTakerObserver, observers_, |
| 356 OnScreenshotCompleted(screenshot_result, screenshot_path)); | 366 OnScreenshotCompleted(screenshot_result, screenshot_path)); |
| 357 } | 367 } |
| 358 | 368 |
| 359 void ScreenshotTaker::AddObserver(ScreenshotTakerObserver* observer) { | 369 void ScreenshotTaker::AddObserver(ScreenshotTakerObserver* observer) { |
| 360 observers_.AddObserver(observer); | 370 observers_.AddObserver(observer); |
| 361 } | 371 } |
| 362 | 372 |
| 363 void ScreenshotTaker::RemoveObserver(ScreenshotTakerObserver* observer) { | 373 void ScreenshotTaker::RemoveObserver(ScreenshotTakerObserver* observer) { |
| 364 observers_.RemoveObserver(observer); | 374 observers_.RemoveObserver(observer); |
| 365 } | 375 } |
| 366 | 376 |
| 367 bool ScreenshotTaker::HasObserver(ScreenshotTakerObserver* observer) const { | 377 bool ScreenshotTaker::HasObserver(ScreenshotTakerObserver* observer) const { |
| 368 return observers_.HasObserver(observer); | 378 return observers_.HasObserver(observer); |
| 369 } | 379 } |
| 370 | 380 |
| 381 Profile* ScreenshotTaker::GetProfile() { | |
| 382 if (profile_for_test_) | |
| 383 return profile_for_test_; | |
| 384 return ProfileManager::GetDefaultProfileOrOffTheRecord(); | |
|
sky
2013/04/22 15:01:19
The reason I wanted this code to take a Profile is
| |
| 385 } | |
| 386 | |
| 371 void ScreenshotTaker::SetScreenshotDirectoryForTest( | 387 void ScreenshotTaker::SetScreenshotDirectoryForTest( |
| 372 const base::FilePath& directory) { | 388 const base::FilePath& directory) { |
| 373 screenshot_directory_for_test_ = directory; | 389 screenshot_directory_for_test_ = directory; |
| 374 } | 390 } |
| 375 | 391 |
| 376 void ScreenshotTaker::SetScreenshotBasenameForTest( | 392 void ScreenshotTaker::SetScreenshotBasenameForTest( |
| 377 const std::string& basename){ | 393 const std::string& basename) { |
| 378 screenshot_basename_for_test_ = basename; | 394 screenshot_basename_for_test_ = basename; |
| 379 } | 395 } |
| 396 | |
| 397 void ScreenshotTaker::SetScreenshotProfileForTest(Profile* profile) { | |
| 398 profile_for_test_ = profile; | |
| 399 } | |
| OLD | NEW |