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/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 "grit/ui_strings.h"
35 #include "ui/aura/window.h"
36 #include "ui/aura/window_event_dispatcher.h"
37 #include "ui/base/clipboard/clipboard.h"
38 #include "ui/base/clipboard/scoped_clipboard_writer.h"
39 #include "ui/base/l10n/l10n_util.h"
40 #include "ui/base/resource/resource_bundle.h"
41 #include "ui/gfx/image/image.h"
42 #include "ui/snapshot/snapshot.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/chromeos/login/user_manager.h"
49 #include "chrome/browser/notifications/desktop_notification_service.h"
50 #include "chrome/browser/notifications/desktop_notification_service_factory.h"
51 #include "chromeos/login/login_state.h"
55 // The minimum interval between two screenshot commands. It has to be
56 // more than 1000 to prevent the conflict of filenames.
57 const int kScreenshotMinimumIntervalInMS
= 1000;
59 const char kNotificationId
[] = "screenshot";
61 const char kNotificationOriginUrl
[] = "chrome://screenshot";
63 const char kImageClipboardFormatPrefix
[] = "<img src='data:image/png;base64,";
64 const char kImageClipboardFormatSuffix
[] = "'>";
66 void CopyScreenshotToClipboard(scoped_refptr
<base::RefCountedString
> png_data
) {
67 DCHECK_CURRENTLY_ON(content::BrowserThread::UI
);
70 base::Base64Encode(png_data
->data(), &encoded
);
72 // Only cares about HTML because ChromeOS doesn't need other formats.
73 // TODO(dcheng): Why don't we take advantage of the ability to write bitmaps
74 // to the clipboard here?
76 ui::ScopedClipboardWriter
scw(ui::Clipboard::GetForCurrentThread(),
77 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 const int GetScreenshotNotificationTitle(
363 ScreenshotTakerObserver::Result screenshot_result
) {
364 switch (screenshot_result
) {
365 case ScreenshotTakerObserver::SCREENSHOTS_DISABLED
:
366 return IDS_ASH_SCREENSHOT_NOTIFICATION_TITLE_DISABLED
;
367 case ScreenshotTakerObserver::SCREENSHOT_SUCCESS
:
368 return IDS_ASH_SCREENSHOT_NOTIFICATION_TITLE_SUCCESS
;
370 return IDS_ASH_SCREENSHOT_NOTIFICATION_TITLE_FAIL
;
374 const int GetScreenshotNotificationText(
375 ScreenshotTakerObserver::Result screenshot_result
) {
376 switch (screenshot_result
) {
377 case ScreenshotTakerObserver::SCREENSHOTS_DISABLED
:
378 return IDS_ASH_SCREENSHOT_NOTIFICATION_TEXT_DISABLED
;
379 case ScreenshotTakerObserver::SCREENSHOT_SUCCESS
:
380 return IDS_ASH_SCREENSHOT_NOTIFICATION_TEXT_SUCCESS
;
382 return IDS_ASH_SCREENSHOT_NOTIFICATION_TEXT_FAIL
;
388 ScreenshotTaker::ScreenshotTaker()
390 profile_for_test_(NULL
) {
393 ScreenshotTaker::~ScreenshotTaker() {
396 void ScreenshotTaker::HandleTakeScreenshotForAllRootWindows() {
397 if (g_browser_process
->local_state()->
398 GetBoolean(prefs::kDisableScreenshots
)) {
399 ShowNotification(ScreenshotTakerObserver::SCREENSHOTS_DISABLED
,
403 base::FilePath screenshot_directory
;
404 if (!screenshot_directory_for_test_
.empty()) {
405 screenshot_directory
= screenshot_directory_for_test_
;
406 } else if (!GetScreenshotDirectory(&screenshot_directory
)) {
407 ShowNotification(ScreenshotTakerObserver::SCREENSHOT_GET_DIR_FAILED
,
411 std::string screenshot_basename
= !screenshot_basename_for_test_
.empty() ?
412 screenshot_basename_for_test_
: GetScreenshotBaseFilename();
414 aura::Window::Windows root_windows
= ash::Shell::GetAllRootWindows();
415 // Reorder root_windows to take the primary root window's snapshot at first.
416 aura::Window
* primary_root
= ash::Shell::GetPrimaryRootWindow();
417 if (*(root_windows
.begin()) != primary_root
) {
418 root_windows
.erase(std::find(
419 root_windows
.begin(), root_windows
.end(), primary_root
));
420 root_windows
.insert(root_windows
.begin(), primary_root
);
422 for (size_t i
= 0; i
< root_windows
.size(); ++i
) {
423 aura::Window
* root_window
= root_windows
[i
];
424 std::string basename
= screenshot_basename
;
425 gfx::Rect rect
= root_window
->bounds();
426 if (root_windows
.size() > 1)
427 basename
+= base::StringPrintf(" - Display %d", static_cast<int>(i
+ 1));
428 base::FilePath screenshot_path
=
429 screenshot_directory
.AppendASCII(basename
+ ".png");
430 GrabFullWindowSnapshotAsync(
431 root_window
, rect
, GetProfile(), screenshot_path
, i
);
433 content::RecordAction(base::UserMetricsAction("Screenshot_TakeFull"));
436 void ScreenshotTaker::HandleTakePartialScreenshot(
437 aura::Window
* window
, const gfx::Rect
& rect
) {
438 if (g_browser_process
->local_state()->
439 GetBoolean(prefs::kDisableScreenshots
)) {
440 ShowNotification(ScreenshotTakerObserver::SCREENSHOTS_DISABLED
,
444 DCHECK_CURRENTLY_ON(content::BrowserThread::UI
);
446 base::FilePath screenshot_directory
;
447 if (!screenshot_directory_for_test_
.empty()) {
448 screenshot_directory
= screenshot_directory_for_test_
;
449 } else if (!GetScreenshotDirectory(&screenshot_directory
)) {
450 ShowNotification(ScreenshotTakerObserver::SCREENSHOT_GET_DIR_FAILED
,
455 std::string screenshot_basename
= !screenshot_basename_for_test_
.empty() ?
456 screenshot_basename_for_test_
: GetScreenshotBaseFilename();
457 base::FilePath screenshot_path
=
458 screenshot_directory
.AppendASCII(screenshot_basename
+ ".png");
459 GrabPartialWindowSnapshotAsync(window
, rect
, GetProfile(), screenshot_path
);
460 content::RecordAction(base::UserMetricsAction("Screenshot_TakePartial"));
463 bool ScreenshotTaker::CanTakeScreenshot() {
464 return last_screenshot_timestamp_
.is_null() ||
465 base::Time::Now() - last_screenshot_timestamp_
>
466 base::TimeDelta::FromMilliseconds(
467 kScreenshotMinimumIntervalInMS
);
470 #if defined(OS_CHROMEOS)
471 Notification
* ScreenshotTaker::CreateNotification(
472 ScreenshotTakerObserver::Result screenshot_result
,
473 const base::FilePath
& screenshot_path
) {
474 const std::string
notification_id(kNotificationId
);
475 // We cancel a previous screenshot notification, if any, to ensure we get
476 // a fresh notification pop-up.
477 g_browser_process
->notification_ui_manager()->CancelById(notification_id
);
478 const base::string16
replace_id(base::UTF8ToUTF16(notification_id
));
480 (screenshot_result
== ScreenshotTakerObserver::SCREENSHOT_SUCCESS
);
481 message_center::RichNotificationData optional_field
;
483 const base::string16 label
= l10n_util::GetStringUTF16(
484 IDS_MESSAGE_CENTER_NOTIFICATION_BUTTON_COPY_SCREENSHOT_TO_CLIPBOARD
);
485 optional_field
.buttons
.push_back(message_center::ButtonInfo(label
));
487 return new Notification(
488 message_center::NOTIFICATION_TYPE_SIMPLE
,
489 GURL(kNotificationOriginUrl
),
490 l10n_util::GetStringUTF16(
491 GetScreenshotNotificationTitle(screenshot_result
)),
492 l10n_util::GetStringUTF16(
493 GetScreenshotNotificationText(screenshot_result
)),
494 ui::ResourceBundle::GetSharedInstance().GetImageNamed(
495 IDR_SCREENSHOT_NOTIFICATION_ICON
),
496 blink::WebTextDirectionDefault
,
497 message_center::NotifierId(
498 message_center::NotifierId::SYSTEM_COMPONENT
,
499 ash::system_notifier::kNotifierScreenshot
),
500 l10n_util::GetStringUTF16(IDS_MESSAGE_CENTER_NOTIFIER_SCREENSHOT_NAME
),
503 new ScreenshotTakerNotificationDelegate(
504 success
, GetProfile(), screenshot_path
));
508 void ScreenshotTaker::ShowNotification(
509 ScreenshotTakerObserver::Result screenshot_result
,
510 const base::FilePath
& screenshot_path
) {
511 DCHECK_CURRENTLY_ON(content::BrowserThread::UI
);
512 #if defined(OS_CHROMEOS)
513 // Do not show a notification that a screenshot was taken while no user is
514 // logged in, since it is confusing for the user to get a message about it
515 // after he logs in (crbug.com/235217).
516 if (!chromeos::LoginState::Get()->IsUserLoggedIn())
519 // TODO(sschmitz): make this work for Windows.
520 DesktopNotificationService
* const service
=
521 DesktopNotificationServiceFactory::GetForProfile(GetProfile());
522 if (service
->IsNotifierEnabled(message_center::NotifierId(
523 message_center::NotifierId::SYSTEM_COMPONENT
,
524 ash::system_notifier::kNotifierScreenshot
))) {
525 scoped_ptr
<Notification
> notification(
526 CreateNotification(screenshot_result
, screenshot_path
));
527 g_browser_process
->notification_ui_manager()->Add(*notification
,
531 FOR_EACH_OBSERVER(ScreenshotTakerObserver
, observers_
,
532 OnScreenshotCompleted(screenshot_result
, screenshot_path
));
535 void ScreenshotTaker::AddObserver(ScreenshotTakerObserver
* observer
) {
536 observers_
.AddObserver(observer
);
539 void ScreenshotTaker::RemoveObserver(ScreenshotTakerObserver
* observer
) {
540 observers_
.RemoveObserver(observer
);
543 bool ScreenshotTaker::HasObserver(ScreenshotTakerObserver
* observer
) const {
544 return observers_
.HasObserver(observer
);
547 void ScreenshotTaker::GrabWindowSnapshotAsyncCallback(
548 base::FilePath screenshot_path
,
551 scoped_refptr
<base::RefCountedBytes
> png_data
) {
554 LOG(ERROR
) << "Failed to grab the window screenshot";
556 ScreenshotTakerObserver::SCREENSHOT_GRABWINDOW_PARTIAL_FAILED
,
559 LOG(ERROR
) << "Failed to grab the window screenshot for " << window_idx
;
561 ScreenshotTakerObserver::SCREENSHOT_GRABWINDOW_FULL_FAILED
,
567 PostSaveScreenshotTask(
568 base::Bind(&ScreenshotTaker::ShowNotification
, factory_
.GetWeakPtr()),
574 void ScreenshotTaker::GrabPartialWindowSnapshotAsync(
575 aura::Window
* window
,
576 const gfx::Rect
& snapshot_bounds
,
578 base::FilePath screenshot_path
) {
579 last_screenshot_timestamp_
= base::Time::Now();
581 bool is_partial
= true;
582 int window_idx
= -1; // unused
583 ui::GrabWindowSnapshotAsync(
586 content::BrowserThread::GetBlockingPool(),
587 base::Bind(&ScreenshotTaker::GrabWindowSnapshotAsyncCallback
,
588 factory_
.GetWeakPtr(),
594 void ScreenshotTaker::GrabFullWindowSnapshotAsync(
595 aura::Window
* window
,
596 const gfx::Rect
& snapshot_bounds
,
598 base::FilePath screenshot_path
,
600 last_screenshot_timestamp_
= base::Time::Now();
602 bool is_partial
= false;
603 ui::GrabWindowSnapshotAsync(
606 content::BrowserThread::GetBlockingPool(),
607 base::Bind(&ScreenshotTaker::GrabWindowSnapshotAsyncCallback
,
608 factory_
.GetWeakPtr(),
614 Profile
* ScreenshotTaker::GetProfile() {
615 if (profile_for_test_
)
616 return profile_for_test_
;
617 return ProfileManager::GetActiveUserProfile();
620 void ScreenshotTaker::SetScreenshotDirectoryForTest(
621 const base::FilePath
& directory
) {
622 screenshot_directory_for_test_
= directory
;
625 void ScreenshotTaker::SetScreenshotBasenameForTest(
626 const std::string
& basename
) {
627 screenshot_basename_for_test_
= basename
;
630 void ScreenshotTaker::SetScreenshotProfileForTest(Profile
* profile
) {
631 profile_for_test_
= profile
;