1 // Copyright 2014 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/chrome_screenshot_grabber.h"
8 #include "ash/system/system_notifier.h"
9 #include "base/base64.h"
10 #include "base/bind.h"
11 #include "base/callback.h"
12 #include "base/files/file_util.h"
13 #include "base/i18n/time_formatting.h"
14 #include "base/prefs/pref_service.h"
15 #include "base/strings/stringprintf.h"
16 #include "base/strings/utf_string_conversions.h"
17 #include "chrome/browser/browser_process.h"
18 #include "chrome/browser/download/download_prefs.h"
19 #include "chrome/browser/notifications/notification_ui_manager.h"
20 #include "chrome/browser/platform_util.h"
21 #include "chrome/browser/profiles/profile.h"
22 #include "chrome/browser/profiles/profile_manager.h"
23 #include "chrome/common/pref_names.h"
24 #include "content/public/browser/browser_thread.h"
25 #include "content/public/browser/user_metrics.h"
26 #include "grit/ash_strings.h"
27 #include "grit/theme_resources.h"
28 #include "ui/base/clipboard/clipboard.h"
29 #include "ui/base/clipboard/scoped_clipboard_writer.h"
30 #include "ui/base/l10n/l10n_util.h"
31 #include "ui/base/resource/resource_bundle.h"
32 #include "ui/strings/grit/ui_strings.h"
34 #if defined(OS_CHROMEOS)
35 #include "chrome/browser/chromeos/drive/file_system_interface.h"
36 #include "chrome/browser/chromeos/drive/file_system_util.h"
37 #include "chrome/browser/chromeos/file_manager/open_util.h"
38 #include "chrome/browser/notifications/desktop_notification_service.h"
39 #include "chrome/browser/notifications/desktop_notification_service_factory.h"
40 #include "chromeos/login/login_state.h"
45 const char kNotificationId
[] = "screenshot";
47 #if defined(OS_CHROMEOS)
48 const char kNotificationOriginUrl
[] = "chrome://screenshot";
51 const char kImageClipboardFormatPrefix
[] = "<img src='data:image/png;base64,";
52 const char kImageClipboardFormatSuffix
[] = "'>";
54 void CopyScreenshotToClipboard(scoped_refptr
<base::RefCountedString
> png_data
) {
55 DCHECK_CURRENTLY_ON(content::BrowserThread::UI
);
58 base::Base64Encode(png_data
->data(), &encoded
);
60 // Only cares about HTML because ChromeOS doesn't need other formats.
61 // TODO(dcheng): Why don't we take advantage of the ability to write bitmaps
62 // to the clipboard here?
64 ui::ScopedClipboardWriter
scw(ui::CLIPBOARD_TYPE_COPY_PASTE
);
65 std::string
html(kImageClipboardFormatPrefix
);
67 html
+= kImageClipboardFormatSuffix
;
68 scw
.WriteHTML(base::UTF8ToUTF16(html
), std::string());
70 content::RecordAction(base::UserMetricsAction("Screenshot_CopyClipboard"));
73 void ReadFileAndCopyToClipboardLocal(const base::FilePath
& screenshot_path
) {
74 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread());
76 scoped_refptr
<base::RefCountedString
> png_data(new base::RefCountedString());
77 if (!base::ReadFileToString(screenshot_path
, &(png_data
->data()))) {
78 LOG(ERROR
) << "Failed to read the screenshot file: "
79 << screenshot_path
.value();
83 content::BrowserThread::PostTask(
84 content::BrowserThread::UI
, FROM_HERE
,
85 base::Bind(CopyScreenshotToClipboard
, png_data
));
88 #if defined(OS_CHROMEOS)
89 void ReadFileAndCopyToClipboardDrive(drive::FileError error
,
90 const base::FilePath
& file_path
,
91 scoped_ptr
<drive::ResourceEntry
> entry
) {
92 if (error
!= drive::FILE_ERROR_OK
) {
93 LOG(ERROR
) << "Failed to read the screenshot path on drive: "
94 << drive::FileErrorToString(error
);
97 content::BrowserThread::GetBlockingPool()->PostTask(
98 FROM_HERE
, base::Bind(&ReadFileAndCopyToClipboardLocal
, file_path
));
102 // Delegate for a notification. This class has two roles: to implement callback
103 // methods for notification, and to provide an identity of the associated
105 class ScreenshotGrabberNotificationDelegate
: public NotificationDelegate
{
107 ScreenshotGrabberNotificationDelegate(bool success
,
109 const base::FilePath
& screenshot_path
)
112 screenshot_path_(screenshot_path
) {}
114 // Overridden from NotificationDelegate:
115 void Click() override
{
118 platform_util::ShowItemInFolder(profile_
, screenshot_path_
);
120 void ButtonClick(int button_index
) override
{
121 DCHECK(success_
&& button_index
== 0);
123 // To avoid keeping the screenshot image on memory, it will re-read the
124 // screenshot file and copy it to the clipboard.
125 #if defined(OS_CHROMEOS)
126 if (drive::util::IsUnderDriveMountPoint(screenshot_path_
)) {
127 drive::FileSystemInterface
* file_system
=
128 drive::util::GetFileSystemByProfile(profile_
);
129 file_system
->GetFile(drive::util::ExtractDrivePath(screenshot_path_
),
130 base::Bind(&ReadFileAndCopyToClipboardDrive
));
134 content::BrowserThread::GetBlockingPool()->PostTask(
136 base::Bind(&ReadFileAndCopyToClipboardLocal
, screenshot_path_
));
138 bool HasClickedListener() override
{ return success_
; }
139 std::string
id() const override
{ return std::string(kNotificationId
); }
142 ~ScreenshotGrabberNotificationDelegate() override
{}
146 const base::FilePath screenshot_path_
;
148 DISALLOW_COPY_AND_ASSIGN(ScreenshotGrabberNotificationDelegate
);
151 #if defined(OS_CHROMEOS)
152 int GetScreenshotNotificationTitle(
153 ui::ScreenshotGrabberObserver::Result screenshot_result
) {
154 switch (screenshot_result
) {
155 case ui::ScreenshotGrabberObserver::SCREENSHOTS_DISABLED
:
156 return IDS_ASH_SCREENSHOT_NOTIFICATION_TITLE_DISABLED
;
157 case ui::ScreenshotGrabberObserver::SCREENSHOT_SUCCESS
:
158 return IDS_ASH_SCREENSHOT_NOTIFICATION_TITLE_SUCCESS
;
160 return IDS_ASH_SCREENSHOT_NOTIFICATION_TITLE_FAIL
;
164 int GetScreenshotNotificationText(
165 ui::ScreenshotGrabberObserver::Result screenshot_result
) {
166 switch (screenshot_result
) {
167 case ui::ScreenshotGrabberObserver::SCREENSHOTS_DISABLED
:
168 return IDS_ASH_SCREENSHOT_NOTIFICATION_TEXT_DISABLED
;
169 case ui::ScreenshotGrabberObserver::SCREENSHOT_SUCCESS
:
170 return IDS_ASH_SCREENSHOT_NOTIFICATION_TEXT_SUCCESS
;
172 return IDS_ASH_SCREENSHOT_NOTIFICATION_TEXT_FAIL
;
176 void PrepareWritableFileCallback(
177 const ChromeScreenshotGrabber::FileCallback
& callback
,
178 drive::FileError error
,
179 const base::FilePath
& local_path
) {
180 callback
.Run(error
== drive::FILE_ERROR_OK
181 ? ui::ScreenshotGrabberDelegate::FILE_SUCCESS
182 : ui::ScreenshotGrabberDelegate::FILE_CREATE_FAILED
,
186 void EnsureDirectoryExistsCallback(
187 const ChromeScreenshotGrabber::FileCallback
& callback
,
189 const base::FilePath
& path
,
190 drive::FileError error
) {
191 // It is okay to fail with FILE_ERROR_EXISTS since anyway the directory
192 // of the target file exists.
193 if (error
== drive::FILE_ERROR_OK
|| error
== drive::FILE_ERROR_EXISTS
) {
194 drive::util::PrepareWritableFileAndRun(
195 profile
, path
, base::Bind(&PrepareWritableFileCallback
, callback
));
197 LOG(ERROR
) << "Failed to ensure the existence of the specified directory "
198 << "in Google Drive: " << error
;
199 content::BrowserThread::GetBlockingPool()->PostTask(
202 ui::ScreenshotGrabberDelegate::FILE_CHECK_DIR_FAILED
,
208 bool ScreenshotsDisabled() {
209 return g_browser_process
->local_state()->GetBoolean(
210 prefs::kDisableScreenshots
);
213 bool ShouldUse24HourClock() {
214 #if defined(OS_CHROMEOS)
215 Profile
* profile
= ProfileManager::GetActiveUserProfile();
217 return profile
->GetPrefs()->GetBoolean(prefs::kUse24HourClock
);
220 return base::GetHourClockType() == base::k24HourClock
;
223 bool GetScreenshotDirectory(base::FilePath
* directory
) {
224 bool is_logged_in
= true;
226 #if defined(OS_CHROMEOS)
227 is_logged_in
= chromeos::LoginState::Get()->IsUserLoggedIn();
231 DownloadPrefs
* download_prefs
= DownloadPrefs::FromBrowserContext(
232 ProfileManager::GetActiveUserProfile());
233 *directory
= download_prefs
->DownloadPath();
235 if (!base::GetTempDir(directory
)) {
236 LOG(ERROR
) << "Failed to find temporary directory.";
243 std::string
GetScreenshotBaseFilename() {
244 base::Time::Exploded now
;
245 base::Time::Now().LocalExplode(&now
);
247 // We don't use base/i18n/time_formatting.h here because it doesn't
248 // support our format. Don't use ICU either to avoid i18n file names
249 // for non-English locales.
250 // TODO(mukai): integrate this logic somewhere time_formatting.h
251 std::string file_name
= base::StringPrintf(
252 "Screenshot %d-%02d-%02d at ", now
.year
, now
.month
, now
.day_of_month
);
254 if (ShouldUse24HourClock()) {
256 base::StringPrintf("%02d.%02d.%02d", now
.hour
, now
.minute
, now
.second
));
261 } else if (hour
== 0) {
265 base::StringPrintf("%d.%02d.%02d ", hour
, now
.minute
, now
.second
));
266 file_name
.append((now
.hour
>= 12) ? "PM" : "AM");
274 ChromeScreenshotGrabber::ChromeScreenshotGrabber()
275 : screenshot_grabber_(new ui::ScreenshotGrabber(
277 content::BrowserThread::GetBlockingPool()
278 ->GetTaskRunnerWithShutdownBehavior(
279 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN
))),
280 profile_for_test_(NULL
) {
281 screenshot_grabber_
->AddObserver(this);
284 ChromeScreenshotGrabber::~ChromeScreenshotGrabber() {
285 screenshot_grabber_
->RemoveObserver(this);
288 void ChromeScreenshotGrabber::HandleTakeScreenshotForAllRootWindows() {
289 if (ScreenshotsDisabled()) {
290 screenshot_grabber_
->NotifyScreenshotCompleted(
291 ui::ScreenshotGrabberObserver::SCREENSHOTS_DISABLED
, base::FilePath());
295 base::FilePath screenshot_directory
;
296 if (!GetScreenshotDirectory(&screenshot_directory
)) {
297 screenshot_grabber_
->NotifyScreenshotCompleted(
298 ui::ScreenshotGrabberObserver::SCREENSHOT_GET_DIR_FAILED
,
303 std::string screenshot_basename
= GetScreenshotBaseFilename();
304 aura::Window::Windows root_windows
= ash::Shell::GetAllRootWindows();
305 // Reorder root_windows to take the primary root window's snapshot at first.
306 aura::Window
* primary_root
= ash::Shell::GetPrimaryRootWindow();
307 if (*(root_windows
.begin()) != primary_root
) {
309 std::find(root_windows
.begin(), root_windows
.end(), primary_root
));
310 root_windows
.insert(root_windows
.begin(), primary_root
);
312 std::vector
<base::FilePath
> filenames
;
313 for (size_t i
= 0; i
< root_windows
.size(); ++i
) {
314 aura::Window
* root_window
= root_windows
[i
];
315 std::string basename
= screenshot_basename
;
316 gfx::Rect rect
= root_window
->bounds();
317 if (root_windows
.size() > 1)
318 basename
+= base::StringPrintf(" - Display %d", static_cast<int>(i
+ 1));
319 base::FilePath screenshot_path
=
320 screenshot_directory
.AppendASCII(basename
+ ".png");
321 screenshot_grabber_
->TakeScreenshot(root_window
, rect
, screenshot_path
);
323 content::RecordAction(base::UserMetricsAction("Screenshot_TakeFull"));
326 void ChromeScreenshotGrabber::HandleTakePartialScreenshot(
327 aura::Window
* window
,
328 const gfx::Rect
& rect
) {
329 if (ScreenshotsDisabled()) {
330 screenshot_grabber_
->NotifyScreenshotCompleted(
331 ui::ScreenshotGrabberObserver::SCREENSHOTS_DISABLED
, base::FilePath());
335 base::FilePath screenshot_directory
;
336 if (!GetScreenshotDirectory(&screenshot_directory
)) {
337 screenshot_grabber_
->NotifyScreenshotCompleted(
338 ui::ScreenshotGrabberObserver::SCREENSHOT_GET_DIR_FAILED
,
343 base::FilePath screenshot_path
=
344 screenshot_directory
.AppendASCII(GetScreenshotBaseFilename() + ".png");
345 screenshot_grabber_
->TakeScreenshot(window
, rect
, screenshot_path
);
346 content::RecordAction(base::UserMetricsAction("Screenshot_TakePartial"));
349 bool ChromeScreenshotGrabber::CanTakeScreenshot() {
350 return screenshot_grabber_
->CanTakeScreenshot();
353 void ChromeScreenshotGrabber::PrepareFileAndRunOnBlockingPool(
354 const base::FilePath
& path
,
355 scoped_refptr
<base::TaskRunner
> blocking_task_runner
,
356 const FileCallback
& callback
) {
357 #if defined(OS_CHROMEOS)
358 Profile
* profile
= ProfileManager::GetActiveUserProfile();
359 if (drive::util::IsUnderDriveMountPoint(path
)) {
360 drive::util::EnsureDirectoryExists(
361 profile
, path
.DirName(),
362 base::Bind(&EnsureDirectoryExistsCallback
, callback
, profile
, path
));
366 ui::ScreenshotGrabberDelegate::PrepareFileAndRunOnBlockingPool(
367 path
, blocking_task_runner
, callback
);
370 void ChromeScreenshotGrabber::OnScreenshotCompleted(
371 ui::ScreenshotGrabberObserver::Result result
,
372 const base::FilePath
& screenshot_path
) {
373 #if defined(OS_CHROMEOS)
374 // Do not show a notification that a screenshot was taken while no user is
375 // logged in, since it is confusing for the user to get a message about it
376 // after he logs in (crbug.com/235217).
377 if (!chromeos::LoginState::Get()->IsUserLoggedIn())
380 // TODO(sschmitz): make this work for Windows.
381 DesktopNotificationService
* const service
=
382 DesktopNotificationServiceFactory::GetForProfile(GetProfile());
383 if (service
->IsNotifierEnabled(message_center::NotifierId(
384 message_center::NotifierId::SYSTEM_COMPONENT
,
385 ash::system_notifier::kNotifierScreenshot
))) {
386 scoped_ptr
<Notification
> notification(
387 CreateNotification(result
, screenshot_path
));
388 g_browser_process
->notification_ui_manager()->Add(*notification
,
394 #if defined(OS_CHROMEOS)
395 Notification
* ChromeScreenshotGrabber::CreateNotification(
396 ui::ScreenshotGrabberObserver::Result screenshot_result
,
397 const base::FilePath
& screenshot_path
) {
398 const std::string
notification_id(kNotificationId
);
399 // We cancel a previous screenshot notification, if any, to ensure we get
400 // a fresh notification pop-up.
401 g_browser_process
->notification_ui_manager()->CancelById(
402 notification_id
, NotificationUIManager::GetProfileID(GetProfile()));
404 (screenshot_result
== ui::ScreenshotGrabberObserver::SCREENSHOT_SUCCESS
);
405 message_center::RichNotificationData optional_field
;
407 const base::string16 label
= l10n_util::GetStringUTF16(
408 IDS_MESSAGE_CENTER_NOTIFICATION_BUTTON_COPY_SCREENSHOT_TO_CLIPBOARD
);
409 optional_field
.buttons
.push_back(message_center::ButtonInfo(label
));
411 return new Notification(
412 message_center::NOTIFICATION_TYPE_SIMPLE
, GURL(kNotificationOriginUrl
),
413 l10n_util::GetStringUTF16(
414 GetScreenshotNotificationTitle(screenshot_result
)),
415 l10n_util::GetStringUTF16(
416 GetScreenshotNotificationText(screenshot_result
)),
417 ui::ResourceBundle::GetSharedInstance().GetImageNamed(
418 IDR_SCREENSHOT_NOTIFICATION_ICON
),
419 message_center::NotifierId(message_center::NotifierId::SYSTEM_COMPONENT
,
420 ash::system_notifier::kNotifierScreenshot
),
421 l10n_util::GetStringUTF16(IDS_MESSAGE_CENTER_NOTIFIER_SCREENSHOT_NAME
),
422 notification_id
, optional_field
,
423 new ScreenshotGrabberNotificationDelegate(success
, GetProfile(),
428 void ChromeScreenshotGrabber::SetProfileForTest(Profile
* profile
) {
429 profile_for_test_
= profile
;
432 Profile
* ChromeScreenshotGrabber::GetProfile() {
433 return profile_for_test_
? profile_for_test_
434 : ProfileManager::GetActiveUserProfile();