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.
5 #include "chrome/browser/ui/ash/screenshot_taker.h"
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"
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"
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;
58 const char kNotificationId[] = "screenshot";
60 #if defined(OS_CHROMEOS)
61 const char kNotificationOriginUrl[] = "chrome://screenshot";
64 const char kImageClipboardFormatPrefix[] = "<img src='data:image/png;base64,";
65 const char kImageClipboardFormatSuffix[] = "'>";
67 void CopyScreenshotToClipboard(scoped_refptr<base::RefCountedString> png_data) {
68 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
71 base::Base64Encode(png_data->data(), &encoded);
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?
77 ui::ScopedClipboardWriter scw(ui::CLIPBOARD_TYPE_COPY_PASTE);
78 std::string html(kImageClipboardFormatPrefix);
80 html += kImageClipboardFormatSuffix;
81 scw.WriteHTML(base::UTF8ToUTF16(html), std::string());
83 content::RecordAction(base::UserMetricsAction("Screenshot_CopyClipboard"));
86 void ReadFileAndCopyToClipboardLocal(const base::FilePath& screenshot_path) {
87 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread());
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();
96 content::BrowserThread::PostTask(
97 content::BrowserThread::UI, FROM_HERE,
98 base::Bind(CopyScreenshotToClipboard, png_data));
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);
110 content::BrowserThread::GetBlockingPool()->PostTask(
112 base::Bind(&ReadFileAndCopyToClipboardLocal, file_path));
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
119 class ScreenshotTakerNotificationDelegate : public NotificationDelegate {
121 ScreenshotTakerNotificationDelegate(bool success,
123 const base::FilePath& screenshot_path)
126 screenshot_path_(screenshot_path) {
129 // Overridden from NotificationDelegate:
130 virtual void Display() OVERRIDE {}
131 virtual void Error() OVERRIDE {}
132 virtual void Close(bool by_user) OVERRIDE {}
133 virtual void Click() OVERRIDE {
136 #if defined(OS_CHROMEOS)
137 file_manager::util::ShowItemInFolder(profile_, screenshot_path_);
139 // TODO(sschmitz): perhaps add similar action for Windows.
142 virtual void ButtonClick(int button_index) OVERRIDE {
143 DCHECK(success_ && button_index == 0);
145 // To avoid keeping the screenshot image on memory, it will re-read the
146 // screenshot file and copy it to the clipboard.
147 #if defined(OS_CHROMEOS)
148 if (drive::util::IsUnderDriveMountPoint(screenshot_path_)) {
149 drive::FileSystemInterface* file_system =
150 drive::util::GetFileSystemByProfile(profile_);
151 file_system->GetFile(
152 drive::util::ExtractDrivePath(screenshot_path_),
153 base::Bind(&ReadFileAndCopyToClipboardDrive));
157 content::BrowserThread::GetBlockingPool()->PostTask(
158 FROM_HERE, base::Bind(
159 &ReadFileAndCopyToClipboardLocal, screenshot_path_));
161 virtual bool HasClickedListener() OVERRIDE { return success_; }
162 virtual std::string id() const OVERRIDE {
163 return std::string(kNotificationId);
165 virtual content::WebContents* GetWebContents() const OVERRIDE {
170 virtual ~ScreenshotTakerNotificationDelegate() {}
174 const base::FilePath screenshot_path_;
176 DISALLOW_COPY_AND_ASSIGN(ScreenshotTakerNotificationDelegate);
179 typedef base::Callback<
180 void(ScreenshotTakerObserver::Result screenshot_result,
181 const base::FilePath& screenshot_path)> ShowNotificationCallback;
183 void SaveScreenshotInternal(const ShowNotificationCallback& callback,
184 const base::FilePath& screenshot_path,
185 const base::FilePath& local_path,
186 scoped_refptr<base::RefCountedBytes> png_data) {
187 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread());
188 DCHECK(!local_path.empty());
189 ScreenshotTakerObserver::Result result =
190 ScreenshotTakerObserver::SCREENSHOT_SUCCESS;
191 if (static_cast<size_t>(base::WriteFile(
193 reinterpret_cast<char*>(&(png_data->data()[0])),
194 png_data->size())) != png_data->size()) {
195 LOG(ERROR) << "Failed to save to " << local_path.value();
196 result = ScreenshotTakerObserver::SCREENSHOT_WRITE_FILE_FAILED;
198 content::BrowserThread::PostTask(
199 content::BrowserThread::UI, FROM_HERE,
200 base::Bind(callback, result, screenshot_path));
203 void SaveScreenshot(const ShowNotificationCallback& callback,
204 const base::FilePath& screenshot_path,
205 scoped_refptr<base::RefCountedBytes> png_data) {
206 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread());
207 DCHECK(!screenshot_path.empty());
209 if (!base::CreateDirectory(screenshot_path.DirName())) {
210 LOG(ERROR) << "Failed to ensure the existence of "
211 << screenshot_path.DirName().value();
212 content::BrowserThread::PostTask(
213 content::BrowserThread::UI, FROM_HERE,
215 ScreenshotTakerObserver::SCREENSHOT_CREATE_DIR_FAILED,
219 SaveScreenshotInternal(callback, screenshot_path, screenshot_path, png_data);
222 // TODO(kinaba): crbug.com/140425, remove this ungly #ifdef dispatch.
223 #if defined(OS_CHROMEOS)
224 void SaveScreenshotToDrive(const ShowNotificationCallback& callback,
225 const base::FilePath& screenshot_path,
226 scoped_refptr<base::RefCountedBytes> png_data,
227 drive::FileError error,
228 const base::FilePath& local_path) {
229 // |screenshot_path| is used in the notification callback.
230 // |local_path| is a temporary file in a hidden cache directory used for
231 // internal work generated by drive::util::PrepareWritableFileAndRun.
232 if (error != drive::FILE_ERROR_OK) {
233 LOG(ERROR) << "Failed to write screenshot image to Google Drive: " << error;
234 content::BrowserThread::PostTask(
235 content::BrowserThread::UI, FROM_HERE,
237 ScreenshotTakerObserver::SCREENSHOT_CREATE_FILE_FAILED,
241 SaveScreenshotInternal(callback, screenshot_path, local_path, png_data);
244 void EnsureDirectoryExistsCallback(
245 const ShowNotificationCallback& callback,
247 const base::FilePath& screenshot_path,
248 scoped_refptr<base::RefCountedBytes> png_data,
249 drive::FileError error) {
250 // It is okay to fail with FILE_ERROR_EXISTS since anyway the directory
251 // of the target file exists.
252 if (error == drive::FILE_ERROR_OK ||
253 error == drive::FILE_ERROR_EXISTS) {
254 drive::util::PrepareWritableFileAndRun(
257 base::Bind(&SaveScreenshotToDrive,
262 LOG(ERROR) << "Failed to ensure the existence of the specified directory "
263 << "in Google Drive: " << error;
264 callback.Run(ScreenshotTakerObserver::SCREENSHOT_CHECK_DIR_FAILED,
269 void PostSaveScreenshotTask(const ShowNotificationCallback& callback,
271 const base::FilePath& screenshot_path,
272 scoped_refptr<base::RefCountedBytes> png_data) {
273 if (drive::util::IsUnderDriveMountPoint(screenshot_path)) {
274 drive::util::EnsureDirectoryExists(
276 screenshot_path.DirName(),
277 base::Bind(&EnsureDirectoryExistsCallback,
283 content::BrowserThread::GetBlockingPool()->PostTask(
284 FROM_HERE, base::Bind(&SaveScreenshot,
291 void PostSaveScreenshotTask(const ShowNotificationCallback& callback,
293 const base::FilePath& screenshot_path,
294 scoped_refptr<base::RefCountedBytes> png_data) {
295 content::BrowserThread::GetBlockingPool()->PostTask(
296 FROM_HERE, base::Bind(&SaveScreenshot,
303 bool ShouldUse24HourClock() {
304 #if defined(OS_CHROMEOS)
305 Profile* profile = ProfileManager::GetActiveUserProfile();
307 return profile->GetPrefs()->GetBoolean(prefs::kUse24HourClock);
310 return base::GetHourClockType() == base::k24HourClock;
313 std::string GetScreenshotBaseFilename() {
314 base::Time::Exploded now;
315 base::Time::Now().LocalExplode(&now);
317 // We don't use base/i18n/time_formatting.h here because it doesn't
318 // support our format. Don't use ICU either to avoid i18n file names
319 // for non-English locales.
320 // TODO(mukai): integrate this logic somewhere time_formatting.h
321 std::string file_name = base::StringPrintf(
322 "Screenshot %d-%02d-%02d at ", now.year, now.month, now.day_of_month);
324 if (ShouldUse24HourClock()) {
325 file_name.append(base::StringPrintf(
326 "%02d.%02d.%02d", now.hour, now.minute, now.second));
331 } else if (hour == 0) {
334 file_name.append(base::StringPrintf(
335 "%d.%02d.%02d ", hour, now.minute, now.second));
336 file_name.append((now.hour >= 12) ? "PM" : "AM");
342 bool GetScreenshotDirectory(base::FilePath* directory) {
343 bool is_logged_in = true;
345 #if defined(OS_CHROMEOS)
346 is_logged_in = chromeos::LoginState::Get()->IsUserLoggedIn();
350 DownloadPrefs* download_prefs = DownloadPrefs::FromBrowserContext(
351 ProfileManager::GetActiveUserProfile());
352 *directory = download_prefs->DownloadPath();
354 if (!base::GetTempDir(directory)) {
355 LOG(ERROR) << "Failed to find temporary directory.";
362 #if defined(OS_CHROMEOS)
363 const int GetScreenshotNotificationTitle(
364 ScreenshotTakerObserver::Result screenshot_result) {
365 switch (screenshot_result) {
366 case ScreenshotTakerObserver::SCREENSHOTS_DISABLED:
367 return IDS_ASH_SCREENSHOT_NOTIFICATION_TITLE_DISABLED;
368 case ScreenshotTakerObserver::SCREENSHOT_SUCCESS:
369 return IDS_ASH_SCREENSHOT_NOTIFICATION_TITLE_SUCCESS;
371 return IDS_ASH_SCREENSHOT_NOTIFICATION_TITLE_FAIL;
375 const int GetScreenshotNotificationText(
376 ScreenshotTakerObserver::Result screenshot_result) {
377 switch (screenshot_result) {
378 case ScreenshotTakerObserver::SCREENSHOTS_DISABLED:
379 return IDS_ASH_SCREENSHOT_NOTIFICATION_TEXT_DISABLED;
380 case ScreenshotTakerObserver::SCREENSHOT_SUCCESS:
381 return IDS_ASH_SCREENSHOT_NOTIFICATION_TEXT_SUCCESS;
383 return IDS_ASH_SCREENSHOT_NOTIFICATION_TEXT_FAIL;
390 ScreenshotTaker::ScreenshotTaker()
391 : profile_for_test_(NULL),
395 ScreenshotTaker::~ScreenshotTaker() {
398 void ScreenshotTaker::HandleTakeScreenshotForAllRootWindows() {
399 if (g_browser_process->local_state()->
400 GetBoolean(prefs::kDisableScreenshots)) {
401 ShowNotification(ScreenshotTakerObserver::SCREENSHOTS_DISABLED,
405 base::FilePath screenshot_directory;
406 if (!screenshot_directory_for_test_.empty()) {
407 screenshot_directory = screenshot_directory_for_test_;
408 } else if (!GetScreenshotDirectory(&screenshot_directory)) {
409 ShowNotification(ScreenshotTakerObserver::SCREENSHOT_GET_DIR_FAILED,
413 std::string screenshot_basename = !screenshot_basename_for_test_.empty() ?
414 screenshot_basename_for_test_ : GetScreenshotBaseFilename();
416 aura::Window::Windows root_windows = ash::Shell::GetAllRootWindows();
417 // Reorder root_windows to take the primary root window's snapshot at first.
418 aura::Window* primary_root = ash::Shell::GetPrimaryRootWindow();
419 if (*(root_windows.begin()) != primary_root) {
420 root_windows.erase(std::find(
421 root_windows.begin(), root_windows.end(), primary_root));
422 root_windows.insert(root_windows.begin(), primary_root);
424 for (size_t i = 0; i < root_windows.size(); ++i) {
425 aura::Window* root_window = root_windows[i];
426 std::string basename = screenshot_basename;
427 gfx::Rect rect = root_window->bounds();
428 if (root_windows.size() > 1)
429 basename += base::StringPrintf(" - Display %d", static_cast<int>(i + 1));
430 base::FilePath screenshot_path =
431 screenshot_directory.AppendASCII(basename + ".png");
432 GrabFullWindowSnapshotAsync(
433 root_window, rect, GetProfile(), screenshot_path, i);
435 content::RecordAction(base::UserMetricsAction("Screenshot_TakeFull"));
438 void ScreenshotTaker::HandleTakePartialScreenshot(
439 aura::Window* window, const gfx::Rect& rect) {
440 if (g_browser_process->local_state()->
441 GetBoolean(prefs::kDisableScreenshots)) {
442 ShowNotification(ScreenshotTakerObserver::SCREENSHOTS_DISABLED,
446 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
448 base::FilePath screenshot_directory;
449 if (!screenshot_directory_for_test_.empty()) {
450 screenshot_directory = screenshot_directory_for_test_;
451 } else if (!GetScreenshotDirectory(&screenshot_directory)) {
452 ShowNotification(ScreenshotTakerObserver::SCREENSHOT_GET_DIR_FAILED,
457 std::string screenshot_basename = !screenshot_basename_for_test_.empty() ?
458 screenshot_basename_for_test_ : GetScreenshotBaseFilename();
459 base::FilePath screenshot_path =
460 screenshot_directory.AppendASCII(screenshot_basename + ".png");
461 GrabPartialWindowSnapshotAsync(window, rect, GetProfile(), screenshot_path);
462 content::RecordAction(base::UserMetricsAction("Screenshot_TakePartial"));
465 bool ScreenshotTaker::CanTakeScreenshot() {
466 return last_screenshot_timestamp_.is_null() ||
467 base::Time::Now() - last_screenshot_timestamp_ >
468 base::TimeDelta::FromMilliseconds(
469 kScreenshotMinimumIntervalInMS);
472 #if defined(OS_CHROMEOS)
473 Notification* ScreenshotTaker::CreateNotification(
474 ScreenshotTakerObserver::Result screenshot_result,
475 const base::FilePath& screenshot_path) {
476 const std::string notification_id(kNotificationId);
477 // We cancel a previous screenshot notification, if any, to ensure we get
478 // a fresh notification pop-up.
479 g_browser_process->notification_ui_manager()->CancelById(notification_id);
480 const base::string16 replace_id(base::UTF8ToUTF16(notification_id));
482 (screenshot_result == ScreenshotTakerObserver::SCREENSHOT_SUCCESS);
483 message_center::RichNotificationData optional_field;
485 const base::string16 label = l10n_util::GetStringUTF16(
486 IDS_MESSAGE_CENTER_NOTIFICATION_BUTTON_COPY_SCREENSHOT_TO_CLIPBOARD);
487 optional_field.buttons.push_back(message_center::ButtonInfo(label));
489 return new Notification(
490 message_center::NOTIFICATION_TYPE_SIMPLE,
491 GURL(kNotificationOriginUrl),
492 l10n_util::GetStringUTF16(
493 GetScreenshotNotificationTitle(screenshot_result)),
494 l10n_util::GetStringUTF16(
495 GetScreenshotNotificationText(screenshot_result)),
496 ui::ResourceBundle::GetSharedInstance().GetImageNamed(
497 IDR_SCREENSHOT_NOTIFICATION_ICON),
498 blink::WebTextDirectionDefault,
499 message_center::NotifierId(
500 message_center::NotifierId::SYSTEM_COMPONENT,
501 ash::system_notifier::kNotifierScreenshot),
502 l10n_util::GetStringUTF16(IDS_MESSAGE_CENTER_NOTIFIER_SCREENSHOT_NAME),
505 new ScreenshotTakerNotificationDelegate(
506 success, GetProfile(), screenshot_path));
510 void ScreenshotTaker::ShowNotification(
511 ScreenshotTakerObserver::Result screenshot_result,
512 const base::FilePath& screenshot_path) {
513 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
514 #if defined(OS_CHROMEOS)
515 // Do not show a notification that a screenshot was taken while no user is
516 // logged in, since it is confusing for the user to get a message about it
517 // after he logs in (crbug.com/235217).
518 if (!chromeos::LoginState::Get()->IsUserLoggedIn())
521 // TODO(sschmitz): make this work for Windows.
522 DesktopNotificationService* const service =
523 DesktopNotificationServiceFactory::GetForProfile(GetProfile());
524 if (service->IsNotifierEnabled(message_center::NotifierId(
525 message_center::NotifierId::SYSTEM_COMPONENT,
526 ash::system_notifier::kNotifierScreenshot))) {
527 scoped_ptr<Notification> notification(
528 CreateNotification(screenshot_result, screenshot_path));
529 g_browser_process->notification_ui_manager()->Add(*notification,
533 FOR_EACH_OBSERVER(ScreenshotTakerObserver, observers_,
534 OnScreenshotCompleted(screenshot_result, screenshot_path));
537 void ScreenshotTaker::AddObserver(ScreenshotTakerObserver* observer) {
538 observers_.AddObserver(observer);
541 void ScreenshotTaker::RemoveObserver(ScreenshotTakerObserver* observer) {
542 observers_.RemoveObserver(observer);
545 bool ScreenshotTaker::HasObserver(ScreenshotTakerObserver* observer) const {
546 return observers_.HasObserver(observer);
549 void ScreenshotTaker::GrabWindowSnapshotAsyncCallback(
550 base::FilePath screenshot_path,
553 scoped_refptr<base::RefCountedBytes> png_data) {
554 if (!png_data.get()) {
556 LOG(ERROR) << "Failed to grab the window screenshot";
558 ScreenshotTakerObserver::SCREENSHOT_GRABWINDOW_PARTIAL_FAILED,
561 LOG(ERROR) << "Failed to grab the window screenshot for " << window_idx;
563 ScreenshotTakerObserver::SCREENSHOT_GRABWINDOW_FULL_FAILED,
569 PostSaveScreenshotTask(
570 base::Bind(&ScreenshotTaker::ShowNotification, factory_.GetWeakPtr()),
576 void ScreenshotTaker::GrabPartialWindowSnapshotAsync(
577 aura::Window* window,
578 const gfx::Rect& snapshot_bounds,
580 base::FilePath screenshot_path) {
581 last_screenshot_timestamp_ = base::Time::Now();
583 bool is_partial = true;
584 int window_idx = -1; // unused
585 ui::GrabWindowSnapshotAsync(
588 content::BrowserThread::GetBlockingPool(),
589 base::Bind(&ScreenshotTaker::GrabWindowSnapshotAsyncCallback,
590 factory_.GetWeakPtr(),
596 void ScreenshotTaker::GrabFullWindowSnapshotAsync(
597 aura::Window* window,
598 const gfx::Rect& snapshot_bounds,
600 base::FilePath screenshot_path,
602 last_screenshot_timestamp_ = base::Time::Now();
604 bool is_partial = false;
605 ui::GrabWindowSnapshotAsync(
608 content::BrowserThread::GetBlockingPool(),
609 base::Bind(&ScreenshotTaker::GrabWindowSnapshotAsyncCallback,
610 factory_.GetWeakPtr(),
616 Profile* ScreenshotTaker::GetProfile() {
617 if (profile_for_test_)
618 return profile_for_test_;
619 return ProfileManager::GetActiveUserProfile();
622 void ScreenshotTaker::SetScreenshotDirectoryForTest(
623 const base::FilePath& directory) {
624 screenshot_directory_for_test_ = directory;
627 void ScreenshotTaker::SetScreenshotBasenameForTest(
628 const std::string& basename) {
629 screenshot_basename_for_test_ = basename;
632 void ScreenshotTaker::SetScreenshotProfileForTest(Profile* profile) {
633 profile_for_test_ = profile;