| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/ui/ash/screenshot_taker.h" | |
| 6 | |
| 7 #include <climits> | |
| 8 #include <string> | |
| 9 | |
| 10 #include "ash/shell.h" | |
| 11 #include "ash/shell_delegate.h" | |
| 12 #include "ash/system/system_notifier.h" | |
| 13 #include "base/base64.h" | |
| 14 #include "base/bind.h" | |
| 15 #include "base/files/file_util.h" | |
| 16 #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" | |
| 20 #include "base/strings/stringprintf.h" | |
| 21 #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" | |
| 25 #include "chrome/browser/download/download_prefs.h" | |
| 26 #include "chrome/browser/notifications/notification_ui_manager.h" | |
| 27 #include "chrome/browser/profiles/profile.h" | |
| 28 #include "chrome/browser/profiles/profile_manager.h" | |
| 29 #include "chrome/common/pref_names.h" | |
| 30 #include "content/public/browser/browser_thread.h" | |
| 31 #include "content/public/browser/user_metrics.h" | |
| 32 #include "grit/ash_strings.h" | |
| 33 #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" | |
| 37 #include "ui/base/clipboard/scoped_clipboard_writer.h" | |
| 38 #include "ui/base/l10n/l10n_util.h" | |
| 39 #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" | |
| 43 | |
| 44 #if defined(OS_CHROMEOS) | |
| 45 #include "chrome/browser/chromeos/drive/file_system_interface.h" | |
| 46 #include "chrome/browser/chromeos/drive/file_system_util.h" | |
| 47 #include "chrome/browser/chromeos/file_manager/open_util.h" | |
| 48 #include "chrome/browser/notifications/desktop_notification_service.h" | |
| 49 #include "chrome/browser/notifications/desktop_notification_service_factory.h" | |
| 50 #include "chromeos/login/login_state.h" | |
| 51 #endif | |
| 52 | |
| 53 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 | |
| 58 const char kNotificationId[] = "screenshot"; | |
| 59 | |
| 60 #if defined(OS_CHROMEOS) | |
| 61 const char kNotificationOriginUrl[] = "chrome://screenshot"; | |
| 62 #endif | |
| 63 | |
| 64 const char kImageClipboardFormatPrefix[] = "<img src='data:image/png;base64,"; | |
| 65 const char kImageClipboardFormatSuffix[] = "'>"; | |
| 66 | |
| 67 void CopyScreenshotToClipboard(scoped_refptr<base::RefCountedString> png_data) { | |
| 68 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 69 | |
| 70 std::string encoded; | |
| 71 base::Base64Encode(png_data->data(), &encoded); | |
| 72 | |
| 73 // Only cares about HTML because ChromeOS doesn't need other formats. | |
| 74 // TODO(dcheng): Why don't we take advantage of the ability to write bitmaps | |
| 75 // to the clipboard here? | |
| 76 { | |
| 77 ui::ScopedClipboardWriter scw(ui::CLIPBOARD_TYPE_COPY_PASTE); | |
| 78 std::string html(kImageClipboardFormatPrefix); | |
| 79 html += encoded; | |
| 80 html += kImageClipboardFormatSuffix; | |
| 81 scw.WriteHTML(base::UTF8ToUTF16(html), std::string()); | |
| 82 } | |
| 83 content::RecordAction(base::UserMetricsAction("Screenshot_CopyClipboard")); | |
| 84 } | |
| 85 | |
| 86 void ReadFileAndCopyToClipboardLocal(const base::FilePath& screenshot_path) { | |
| 87 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); | |
| 88 | |
| 89 scoped_refptr<base::RefCountedString> png_data(new base::RefCountedString()); | |
| 90 if (!base::ReadFileToString(screenshot_path, &(png_data->data()))) { | |
| 91 LOG(ERROR) << "Failed to read the screenshot file: " | |
| 92 << screenshot_path.value(); | |
| 93 return; | |
| 94 } | |
| 95 | |
| 96 content::BrowserThread::PostTask( | |
| 97 content::BrowserThread::UI, FROM_HERE, | |
| 98 base::Bind(CopyScreenshotToClipboard, png_data)); | |
| 99 } | |
| 100 | |
| 101 #if defined(OS_CHROMEOS) | |
| 102 void ReadFileAndCopyToClipboardDrive(drive::FileError error, | |
| 103 const base::FilePath& file_path, | |
| 104 scoped_ptr<drive::ResourceEntry> entry) { | |
| 105 if (error != drive::FILE_ERROR_OK) { | |
| 106 LOG(ERROR) << "Failed to read the screenshot path on drive: " | |
| 107 << drive::FileErrorToString(error); | |
| 108 return; | |
| 109 } | |
| 110 content::BrowserThread::GetBlockingPool()->PostTask( | |
| 111 FROM_HERE, | |
| 112 base::Bind(&ReadFileAndCopyToClipboardLocal, file_path)); | |
| 113 } | |
| 114 #endif | |
| 115 | |
| 116 // Delegate for a notification. This class has two roles: to implement callback | |
| 117 // methods for notification, and to provide an identity of the associated | |
| 118 // notification. | |
| 119 class ScreenshotTakerNotificationDelegate : public NotificationDelegate { | |
| 120 public: | |
| 121 ScreenshotTakerNotificationDelegate(bool success, | |
| 122 Profile* profile, | |
| 123 const base::FilePath& screenshot_path) | |
| 124 : success_(success), | |
| 125 profile_(profile), | |
| 126 screenshot_path_(screenshot_path) { | |
| 127 } | |
| 128 | |
| 129 // Overridden from NotificationDelegate: | |
| 130 void Click() override { | |
| 131 if (!success_) | |
| 132 return; | |
| 133 #if defined(OS_CHROMEOS) | |
| 134 file_manager::util::ShowItemInFolder(profile_, screenshot_path_); | |
| 135 #else | |
| 136 // TODO(sschmitz): perhaps add similar action for Windows. | |
| 137 #endif | |
| 138 } | |
| 139 void ButtonClick(int button_index) override { | |
| 140 DCHECK(success_ && button_index == 0); | |
| 141 | |
| 142 // To avoid keeping the screenshot image on memory, it will re-read the | |
| 143 // screenshot file and copy it to the clipboard. | |
| 144 #if defined(OS_CHROMEOS) | |
| 145 if (drive::util::IsUnderDriveMountPoint(screenshot_path_)) { | |
| 146 drive::FileSystemInterface* file_system = | |
| 147 drive::util::GetFileSystemByProfile(profile_); | |
| 148 file_system->GetFile( | |
| 149 drive::util::ExtractDrivePath(screenshot_path_), | |
| 150 base::Bind(&ReadFileAndCopyToClipboardDrive)); | |
| 151 return; | |
| 152 } | |
| 153 #endif | |
| 154 content::BrowserThread::GetBlockingPool()->PostTask( | |
| 155 FROM_HERE, base::Bind( | |
| 156 &ReadFileAndCopyToClipboardLocal, screenshot_path_)); | |
| 157 } | |
| 158 bool HasClickedListener() override { return success_; } | |
| 159 std::string id() const override { return std::string(kNotificationId); } | |
| 160 | |
| 161 private: | |
| 162 ~ScreenshotTakerNotificationDelegate() override {} | |
| 163 | |
| 164 const bool success_; | |
| 165 Profile* profile_; | |
| 166 const base::FilePath screenshot_path_; | |
| 167 | |
| 168 DISALLOW_COPY_AND_ASSIGN(ScreenshotTakerNotificationDelegate); | |
| 169 }; | |
| 170 | |
| 171 typedef base::Callback< | |
| 172 void(ScreenshotTakerObserver::Result screenshot_result, | |
| 173 const base::FilePath& screenshot_path)> ShowNotificationCallback; | |
| 174 | |
| 175 void SaveScreenshotInternal(const ShowNotificationCallback& callback, | |
| 176 const base::FilePath& screenshot_path, | |
| 177 const base::FilePath& local_path, | |
| 178 scoped_refptr<base::RefCountedBytes> png_data) { | |
| 179 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); | |
| 180 DCHECK(!local_path.empty()); | |
| 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 } | |
| 190 content::BrowserThread::PostTask( | |
| 191 content::BrowserThread::UI, FROM_HERE, | |
| 192 base::Bind(callback, result, screenshot_path)); | |
| 193 } | |
| 194 | |
| 195 void SaveScreenshot(const ShowNotificationCallback& callback, | |
| 196 const base::FilePath& screenshot_path, | |
| 197 scoped_refptr<base::RefCountedBytes> png_data) { | |
| 198 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); | |
| 199 DCHECK(!screenshot_path.empty()); | |
| 200 | |
| 201 if (!base::CreateDirectory(screenshot_path.DirName())) { | |
| 202 LOG(ERROR) << "Failed to ensure the existence of " | |
| 203 << screenshot_path.DirName().value(); | |
| 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 } | |
| 211 SaveScreenshotInternal(callback, screenshot_path, screenshot_path, png_data); | |
| 212 } | |
| 213 | |
| 214 // TODO(kinaba): crbug.com/140425, remove this ungly #ifdef dispatch. | |
| 215 #if defined(OS_CHROMEOS) | |
| 216 void SaveScreenshotToDrive(const ShowNotificationCallback& callback, | |
| 217 const base::FilePath& screenshot_path, | |
| 218 scoped_refptr<base::RefCountedBytes> png_data, | |
| 219 drive::FileError error, | |
| 220 const base::FilePath& local_path) { | |
| 221 // |screenshot_path| is used in the notification callback. | |
| 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 } | |
| 235 | |
| 236 void EnsureDirectoryExistsCallback( | |
| 237 const ShowNotificationCallback& callback, | |
| 238 Profile* profile, | |
| 239 const base::FilePath& screenshot_path, | |
| 240 scoped_refptr<base::RefCountedBytes> png_data, | |
| 241 drive::FileError error) { | |
| 242 // It is okay to fail with FILE_ERROR_EXISTS since anyway the directory | |
| 243 // of the target file exists. | |
| 244 if (error == drive::FILE_ERROR_OK || | |
| 245 error == drive::FILE_ERROR_EXISTS) { | |
| 246 drive::util::PrepareWritableFileAndRun( | |
| 247 profile, | |
| 248 screenshot_path, | |
| 249 base::Bind(&SaveScreenshotToDrive, | |
| 250 callback, | |
| 251 screenshot_path, | |
| 252 png_data)); | |
| 253 } else { | |
| 254 LOG(ERROR) << "Failed to ensure the existence of the specified directory " | |
| 255 << "in Google Drive: " << error; | |
| 256 callback.Run(ScreenshotTakerObserver::SCREENSHOT_CHECK_DIR_FAILED, | |
| 257 screenshot_path); | |
| 258 } | |
| 259 } | |
| 260 | |
| 261 void PostSaveScreenshotTask(const ShowNotificationCallback& callback, | |
| 262 Profile* profile, | |
| 263 const base::FilePath& screenshot_path, | |
| 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 } | |
| 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 | |
| 295 bool ShouldUse24HourClock() { | |
| 296 #if defined(OS_CHROMEOS) | |
| 297 Profile* profile = ProfileManager::GetActiveUserProfile(); | |
| 298 if (profile) { | |
| 299 return profile->GetPrefs()->GetBoolean(prefs::kUse24HourClock); | |
| 300 } | |
| 301 #endif | |
| 302 return base::GetHourClockType() == base::k24HourClock; | |
| 303 } | |
| 304 | |
| 305 std::string GetScreenshotBaseFilename() { | |
| 306 base::Time::Exploded now; | |
| 307 base::Time::Now().LocalExplode(&now); | |
| 308 | |
| 309 // 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 | |
| 311 // for non-English locales. | |
| 312 // TODO(mukai): integrate this logic somewhere time_formatting.h | |
| 313 std::string file_name = base::StringPrintf( | |
| 314 "Screenshot %d-%02d-%02d at ", now.year, now.month, now.day_of_month); | |
| 315 | |
| 316 if (ShouldUse24HourClock()) { | |
| 317 file_name.append(base::StringPrintf( | |
| 318 "%02d.%02d.%02d", now.hour, now.minute, now.second)); | |
| 319 } else { | |
| 320 int hour = now.hour; | |
| 321 if (hour > 12) { | |
| 322 hour -= 12; | |
| 323 } else if (hour == 0) { | |
| 324 hour = 12; | |
| 325 } | |
| 326 file_name.append(base::StringPrintf( | |
| 327 "%d.%02d.%02d ", hour, now.minute, now.second)); | |
| 328 file_name.append((now.hour >= 12) ? "PM" : "AM"); | |
| 329 } | |
| 330 | |
| 331 return file_name; | |
| 332 } | |
| 333 | |
| 334 bool GetScreenshotDirectory(base::FilePath* directory) { | |
| 335 bool is_logged_in = true; | |
| 336 | |
| 337 #if defined(OS_CHROMEOS) | |
| 338 is_logged_in = chromeos::LoginState::Get()->IsUserLoggedIn(); | |
| 339 #endif | |
| 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 } | |
| 353 | |
| 354 #if defined(OS_CHROMEOS) | |
| 355 int GetScreenshotNotificationTitle( | |
| 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 } | |
| 366 | |
| 367 int GetScreenshotNotificationText( | |
| 368 ScreenshotTakerObserver::Result screenshot_result) { | |
| 369 switch (screenshot_result) { | |
| 370 case ScreenshotTakerObserver::SCREENSHOTS_DISABLED: | |
| 371 return IDS_ASH_SCREENSHOT_NOTIFICATION_TEXT_DISABLED; | |
| 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; | |
| 396 } | |
| 397 base::FilePath screenshot_directory; | |
| 398 if (!screenshot_directory_for_test_.empty()) { | |
| 399 screenshot_directory = screenshot_directory_for_test_; | |
| 400 } else if (!GetScreenshotDirectory(&screenshot_directory)) { | |
| 401 ShowNotification(ScreenshotTakerObserver::SCREENSHOT_GET_DIR_FAILED, | |
| 402 base::FilePath()); | |
| 403 return; | |
| 404 } | |
| 405 std::string screenshot_basename = !screenshot_basename_for_test_.empty() ? | |
| 406 screenshot_basename_for_test_ : GetScreenshotBaseFilename(); | |
| 407 | |
| 408 aura::Window::Windows root_windows = ash::Shell::GetAllRootWindows(); | |
| 409 // Reorder root_windows to take the primary root window's snapshot at first. | |
| 410 aura::Window* primary_root = ash::Shell::GetPrimaryRootWindow(); | |
| 411 if (*(root_windows.begin()) != primary_root) { | |
| 412 root_windows.erase(std::find( | |
| 413 root_windows.begin(), root_windows.end(), primary_root)); | |
| 414 root_windows.insert(root_windows.begin(), primary_root); | |
| 415 } | |
| 416 for (size_t i = 0; i < root_windows.size(); ++i) { | |
| 417 aura::Window* root_window = root_windows[i]; | |
| 418 std::string basename = screenshot_basename; | |
| 419 gfx::Rect rect = root_window->bounds(); | |
| 420 if (root_windows.size() > 1) | |
| 421 basename += base::StringPrintf(" - Display %d", static_cast<int>(i + 1)); | |
| 422 base::FilePath screenshot_path = | |
| 423 screenshot_directory.AppendASCII(basename + ".png"); | |
| 424 GrabFullWindowSnapshotAsync( | |
| 425 root_window, rect, GetProfile(), screenshot_path, i); | |
| 426 } | |
| 427 content::RecordAction(base::UserMetricsAction("Screenshot_TakeFull")); | |
| 428 } | |
| 429 | |
| 430 void ScreenshotTaker::HandleTakePartialScreenshot( | |
| 431 aura::Window* window, const gfx::Rect& rect) { | |
| 432 if (g_browser_process->local_state()-> | |
| 433 GetBoolean(prefs::kDisableScreenshots)) { | |
| 434 ShowNotification(ScreenshotTakerObserver::SCREENSHOTS_DISABLED, | |
| 435 base::FilePath()); | |
| 436 return; | |
| 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; | |
| 447 } | |
| 448 | |
| 449 std::string screenshot_basename = !screenshot_basename_for_test_.empty() ? | |
| 450 screenshot_basename_for_test_ : GetScreenshotBaseFilename(); | |
| 451 base::FilePath screenshot_path = | |
| 452 screenshot_directory.AppendASCII(screenshot_basename + ".png"); | |
| 453 GrabPartialWindowSnapshotAsync(window, rect, GetProfile(), screenshot_path); | |
| 454 content::RecordAction(base::UserMetricsAction("Screenshot_TakePartial")); | |
| 455 } | |
| 456 | |
| 457 bool ScreenshotTaker::CanTakeScreenshot() { | |
| 458 return last_screenshot_timestamp_.is_null() || | |
| 459 base::Time::Now() - last_screenshot_timestamp_ > | |
| 460 base::TimeDelta::FromMilliseconds( | |
| 461 kScreenshotMinimumIntervalInMS); | |
| 462 } | |
| 463 | |
| 464 #if defined(OS_CHROMEOS) | |
| 465 Notification* ScreenshotTaker::CreateNotification( | |
| 466 ScreenshotTakerObserver::Result screenshot_result, | |
| 467 const base::FilePath& screenshot_path) { | |
| 468 const std::string notification_id(kNotificationId); | |
| 469 // We cancel a previous screenshot notification, if any, to ensure we get | |
| 470 // a fresh notification pop-up. | |
| 471 g_browser_process->notification_ui_manager()->CancelById( | |
| 472 notification_id, NotificationUIManager::GetProfileID(GetProfile())); | |
| 473 const base::string16 replace_id(base::UTF8ToUTF16(notification_id)); | |
| 474 bool success = | |
| 475 (screenshot_result == ScreenshotTakerObserver::SCREENSHOT_SUCCESS); | |
| 476 message_center::RichNotificationData optional_field; | |
| 477 if (success) { | |
| 478 const base::string16 label = l10n_util::GetStringUTF16( | |
| 479 IDS_MESSAGE_CENTER_NOTIFICATION_BUTTON_COPY_SCREENSHOT_TO_CLIPBOARD); | |
| 480 optional_field.buttons.push_back(message_center::ButtonInfo(label)); | |
| 481 } | |
| 482 return new Notification( | |
| 483 message_center::NOTIFICATION_TYPE_SIMPLE, | |
| 484 GURL(kNotificationOriginUrl), | |
| 485 l10n_util::GetStringUTF16( | |
| 486 GetScreenshotNotificationTitle(screenshot_result)), | |
| 487 l10n_util::GetStringUTF16( | |
| 488 GetScreenshotNotificationText(screenshot_result)), | |
| 489 ui::ResourceBundle::GetSharedInstance().GetImageNamed( | |
| 490 IDR_SCREENSHOT_NOTIFICATION_ICON), | |
| 491 blink::WebTextDirectionDefault, | |
| 492 message_center::NotifierId( | |
| 493 message_center::NotifierId::SYSTEM_COMPONENT, | |
| 494 ash::system_notifier::kNotifierScreenshot), | |
| 495 l10n_util::GetStringUTF16(IDS_MESSAGE_CENTER_NOTIFIER_SCREENSHOT_NAME), | |
| 496 replace_id, | |
| 497 optional_field, | |
| 498 new ScreenshotTakerNotificationDelegate( | |
| 499 success, GetProfile(), screenshot_path)); | |
| 500 } | |
| 501 #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 |