Update V8 to version 4.6.22.
[chromium-blink-merge.git] / chrome / browser / ui / ash / chrome_screenshot_grabber.cc
blob005423b11cd4cb4766ac65b83e0d70a34466d0af
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"
7 #include "ash/shell.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_core_util.h"
36 #include "chrome/browser/chromeos/drive/file_system_interface.h"
37 #include "chrome/browser/chromeos/drive/file_system_util.h"
38 #include "chrome/browser/chromeos/file_manager/open_util.h"
39 #include "chrome/browser/notifications/desktop_notification_service.h"
40 #include "chrome/browser/notifications/desktop_notification_service_factory.h"
41 #include "chromeos/login/login_state.h"
42 #endif
44 namespace {
46 const char kNotificationId[] = "screenshot";
48 #if defined(OS_CHROMEOS)
49 const char kNotificationOriginUrl[] = "chrome://screenshot";
50 #endif
52 const char kImageClipboardFormatPrefix[] = "<img src='data:image/png;base64,";
53 const char kImageClipboardFormatSuffix[] = "'>";
55 void CopyScreenshotToClipboard(scoped_refptr<base::RefCountedString> png_data) {
56 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
58 std::string encoded;
59 base::Base64Encode(png_data->data(), &encoded);
61 // Only cares about HTML because ChromeOS doesn't need other formats.
62 // TODO(dcheng): Why don't we take advantage of the ability to write bitmaps
63 // to the clipboard here?
65 ui::ScopedClipboardWriter scw(ui::CLIPBOARD_TYPE_COPY_PASTE);
66 std::string html(kImageClipboardFormatPrefix);
67 html += encoded;
68 html += kImageClipboardFormatSuffix;
69 scw.WriteHTML(base::UTF8ToUTF16(html), std::string());
71 content::RecordAction(base::UserMetricsAction("Screenshot_CopyClipboard"));
74 void ReadFileAndCopyToClipboardLocal(const base::FilePath& screenshot_path) {
75 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread());
77 scoped_refptr<base::RefCountedString> png_data(new base::RefCountedString());
78 if (!base::ReadFileToString(screenshot_path, &(png_data->data()))) {
79 LOG(ERROR) << "Failed to read the screenshot file: "
80 << screenshot_path.value();
81 return;
84 content::BrowserThread::PostTask(
85 content::BrowserThread::UI, FROM_HERE,
86 base::Bind(CopyScreenshotToClipboard, png_data));
89 #if defined(OS_CHROMEOS)
90 void ReadFileAndCopyToClipboardDrive(drive::FileError error,
91 const base::FilePath& file_path,
92 scoped_ptr<drive::ResourceEntry> entry) {
93 if (error != drive::FILE_ERROR_OK) {
94 LOG(ERROR) << "Failed to read the screenshot path on drive: "
95 << drive::FileErrorToString(error);
96 return;
98 content::BrowserThread::GetBlockingPool()->PostTask(
99 FROM_HERE, base::Bind(&ReadFileAndCopyToClipboardLocal, file_path));
101 #endif
103 // Delegate for a notification. This class has two roles: to implement callback
104 // methods for notification, and to provide an identity of the associated
105 // notification.
106 class ScreenshotGrabberNotificationDelegate : public NotificationDelegate {
107 public:
108 ScreenshotGrabberNotificationDelegate(bool success,
109 Profile* profile,
110 const base::FilePath& screenshot_path)
111 : success_(success),
112 profile_(profile),
113 screenshot_path_(screenshot_path) {}
115 // Overridden from NotificationDelegate:
116 void Click() override {
117 if (!success_)
118 return;
119 platform_util::ShowItemInFolder(profile_, screenshot_path_);
121 void ButtonClick(int button_index) override {
122 DCHECK(success_ && button_index == 0);
124 // To avoid keeping the screenshot image on memory, it will re-read the
125 // screenshot file and copy it to the clipboard.
126 #if defined(OS_CHROMEOS)
127 if (drive::util::IsUnderDriveMountPoint(screenshot_path_)) {
128 drive::FileSystemInterface* file_system =
129 drive::util::GetFileSystemByProfile(profile_);
130 file_system->GetFile(drive::util::ExtractDrivePath(screenshot_path_),
131 base::Bind(&ReadFileAndCopyToClipboardDrive));
132 return;
134 #endif
135 content::BrowserThread::GetBlockingPool()->PostTask(
136 FROM_HERE,
137 base::Bind(&ReadFileAndCopyToClipboardLocal, screenshot_path_));
139 bool HasClickedListener() override { return success_; }
140 std::string id() const override { return std::string(kNotificationId); }
142 private:
143 ~ScreenshotGrabberNotificationDelegate() override {}
145 const bool success_;
146 Profile* profile_;
147 const base::FilePath screenshot_path_;
149 DISALLOW_COPY_AND_ASSIGN(ScreenshotGrabberNotificationDelegate);
152 #if defined(OS_CHROMEOS)
153 int GetScreenshotNotificationTitle(
154 ui::ScreenshotGrabberObserver::Result screenshot_result) {
155 switch (screenshot_result) {
156 case ui::ScreenshotGrabberObserver::SCREENSHOTS_DISABLED:
157 return IDS_ASH_SCREENSHOT_NOTIFICATION_TITLE_DISABLED;
158 case ui::ScreenshotGrabberObserver::SCREENSHOT_SUCCESS:
159 return IDS_ASH_SCREENSHOT_NOTIFICATION_TITLE_SUCCESS;
160 default:
161 return IDS_ASH_SCREENSHOT_NOTIFICATION_TITLE_FAIL;
165 int GetScreenshotNotificationText(
166 ui::ScreenshotGrabberObserver::Result screenshot_result) {
167 switch (screenshot_result) {
168 case ui::ScreenshotGrabberObserver::SCREENSHOTS_DISABLED:
169 return IDS_ASH_SCREENSHOT_NOTIFICATION_TEXT_DISABLED;
170 case ui::ScreenshotGrabberObserver::SCREENSHOT_SUCCESS:
171 return IDS_ASH_SCREENSHOT_NOTIFICATION_TEXT_SUCCESS;
172 default:
173 return IDS_ASH_SCREENSHOT_NOTIFICATION_TEXT_FAIL;
177 void PrepareWritableFileCallback(
178 const ChromeScreenshotGrabber::FileCallback& callback,
179 drive::FileError error,
180 const base::FilePath& local_path) {
181 callback.Run(error == drive::FILE_ERROR_OK
182 ? ui::ScreenshotGrabberDelegate::FILE_SUCCESS
183 : ui::ScreenshotGrabberDelegate::FILE_CREATE_FAILED,
184 local_path);
187 void EnsureDirectoryExistsCallback(
188 const ChromeScreenshotGrabber::FileCallback& callback,
189 Profile* profile,
190 const base::FilePath& path,
191 drive::FileError error) {
192 // It is okay to fail with FILE_ERROR_EXISTS since anyway the directory
193 // of the target file exists.
194 if (error == drive::FILE_ERROR_OK || error == drive::FILE_ERROR_EXISTS) {
195 drive::util::PrepareWritableFileAndRun(
196 profile, path, base::Bind(&PrepareWritableFileCallback, callback));
197 } else {
198 LOG(ERROR) << "Failed to ensure the existence of the specified directory "
199 << "in Google Drive: " << error;
200 content::BrowserThread::GetBlockingPool()->PostTask(
201 FROM_HERE,
202 base::Bind(callback,
203 ui::ScreenshotGrabberDelegate::FILE_CHECK_DIR_FAILED,
204 base::FilePath()));
207 #endif
209 bool ScreenshotsDisabled() {
210 return g_browser_process->local_state()->GetBoolean(
211 prefs::kDisableScreenshots);
214 bool ShouldUse24HourClock() {
215 #if defined(OS_CHROMEOS)
216 Profile* profile = ProfileManager::GetActiveUserProfile();
217 if (profile) {
218 return profile->GetPrefs()->GetBoolean(prefs::kUse24HourClock);
220 #endif
221 return base::GetHourClockType() == base::k24HourClock;
224 bool GetScreenshotDirectory(base::FilePath* directory) {
225 bool is_logged_in = true;
227 #if defined(OS_CHROMEOS)
228 is_logged_in = chromeos::LoginState::Get()->IsUserLoggedIn();
229 #endif
231 if (is_logged_in) {
232 DownloadPrefs* download_prefs = DownloadPrefs::FromBrowserContext(
233 ProfileManager::GetActiveUserProfile());
234 *directory = download_prefs->DownloadPath();
235 } else {
236 if (!base::GetTempDir(directory)) {
237 LOG(ERROR) << "Failed to find temporary directory.";
238 return false;
241 return true;
244 std::string GetScreenshotBaseFilename() {
245 base::Time::Exploded now;
246 base::Time::Now().LocalExplode(&now);
248 // We don't use base/i18n/time_formatting.h here because it doesn't
249 // support our format. Don't use ICU either to avoid i18n file names
250 // for non-English locales.
251 // TODO(mukai): integrate this logic somewhere time_formatting.h
252 std::string file_name = base::StringPrintf(
253 "Screenshot %d-%02d-%02d at ", now.year, now.month, now.day_of_month);
255 if (ShouldUse24HourClock()) {
256 file_name.append(
257 base::StringPrintf("%02d.%02d.%02d", now.hour, now.minute, now.second));
258 } else {
259 int hour = now.hour;
260 if (hour > 12) {
261 hour -= 12;
262 } else if (hour == 0) {
263 hour = 12;
265 file_name.append(
266 base::StringPrintf("%d.%02d.%02d ", hour, now.minute, now.second));
267 file_name.append((now.hour >= 12) ? "PM" : "AM");
270 return file_name;
273 } // namespace
275 ChromeScreenshotGrabber::ChromeScreenshotGrabber()
276 : screenshot_grabber_(new ui::ScreenshotGrabber(
277 this,
278 content::BrowserThread::GetBlockingPool()
279 ->GetTaskRunnerWithShutdownBehavior(
280 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN))),
281 profile_for_test_(NULL) {
282 screenshot_grabber_->AddObserver(this);
285 ChromeScreenshotGrabber::~ChromeScreenshotGrabber() {
286 screenshot_grabber_->RemoveObserver(this);
289 void ChromeScreenshotGrabber::HandleTakeScreenshotForAllRootWindows() {
290 if (ScreenshotsDisabled()) {
291 screenshot_grabber_->NotifyScreenshotCompleted(
292 ui::ScreenshotGrabberObserver::SCREENSHOTS_DISABLED, base::FilePath());
293 return;
296 base::FilePath screenshot_directory;
297 if (!GetScreenshotDirectory(&screenshot_directory)) {
298 screenshot_grabber_->NotifyScreenshotCompleted(
299 ui::ScreenshotGrabberObserver::SCREENSHOT_GET_DIR_FAILED,
300 base::FilePath());
301 return;
304 std::string screenshot_basename = GetScreenshotBaseFilename();
305 aura::Window::Windows root_windows = ash::Shell::GetAllRootWindows();
306 // Reorder root_windows to take the primary root window's snapshot at first.
307 aura::Window* primary_root = ash::Shell::GetPrimaryRootWindow();
308 if (*(root_windows.begin()) != primary_root) {
309 root_windows.erase(
310 std::find(root_windows.begin(), root_windows.end(), primary_root));
311 root_windows.insert(root_windows.begin(), primary_root);
313 std::vector<base::FilePath> filenames;
314 for (size_t i = 0; i < root_windows.size(); ++i) {
315 aura::Window* root_window = root_windows[i];
316 std::string basename = screenshot_basename;
317 gfx::Rect rect = root_window->bounds();
318 if (root_windows.size() > 1)
319 basename += base::StringPrintf(" - Display %d", static_cast<int>(i + 1));
320 base::FilePath screenshot_path =
321 screenshot_directory.AppendASCII(basename + ".png");
322 screenshot_grabber_->TakeScreenshot(root_window, rect, screenshot_path);
324 content::RecordAction(base::UserMetricsAction("Screenshot_TakeFull"));
327 void ChromeScreenshotGrabber::HandleTakePartialScreenshot(
328 aura::Window* window,
329 const gfx::Rect& rect) {
330 if (ScreenshotsDisabled()) {
331 screenshot_grabber_->NotifyScreenshotCompleted(
332 ui::ScreenshotGrabberObserver::SCREENSHOTS_DISABLED, base::FilePath());
333 return;
336 base::FilePath screenshot_directory;
337 if (!GetScreenshotDirectory(&screenshot_directory)) {
338 screenshot_grabber_->NotifyScreenshotCompleted(
339 ui::ScreenshotGrabberObserver::SCREENSHOT_GET_DIR_FAILED,
340 base::FilePath());
341 return;
344 base::FilePath screenshot_path =
345 screenshot_directory.AppendASCII(GetScreenshotBaseFilename() + ".png");
346 screenshot_grabber_->TakeScreenshot(window, rect, screenshot_path);
347 content::RecordAction(base::UserMetricsAction("Screenshot_TakePartial"));
350 bool ChromeScreenshotGrabber::CanTakeScreenshot() {
351 return screenshot_grabber_->CanTakeScreenshot();
354 void ChromeScreenshotGrabber::PrepareFileAndRunOnBlockingPool(
355 const base::FilePath& path,
356 scoped_refptr<base::TaskRunner> blocking_task_runner,
357 const FileCallback& callback) {
358 #if defined(OS_CHROMEOS)
359 Profile* profile = ProfileManager::GetActiveUserProfile();
360 if (drive::util::IsUnderDriveMountPoint(path)) {
361 drive::util::EnsureDirectoryExists(
362 profile, path.DirName(),
363 base::Bind(&EnsureDirectoryExistsCallback, callback, profile, path));
364 return;
366 #endif
367 ui::ScreenshotGrabberDelegate::PrepareFileAndRunOnBlockingPool(
368 path, blocking_task_runner, callback);
371 void ChromeScreenshotGrabber::OnScreenshotCompleted(
372 ui::ScreenshotGrabberObserver::Result result,
373 const base::FilePath& screenshot_path) {
374 #if defined(OS_CHROMEOS)
375 // Do not show a notification that a screenshot was taken while no user is
376 // logged in, since it is confusing for the user to get a message about it
377 // after he logs in (crbug.com/235217).
378 if (!chromeos::LoginState::Get()->IsUserLoggedIn())
379 return;
381 // TODO(sschmitz): make this work for Windows.
382 DesktopNotificationService* const service =
383 DesktopNotificationServiceFactory::GetForProfile(GetProfile());
384 if (service->IsNotifierEnabled(message_center::NotifierId(
385 message_center::NotifierId::SYSTEM_COMPONENT,
386 ash::system_notifier::kNotifierScreenshot))) {
387 scoped_ptr<Notification> notification(
388 CreateNotification(result, screenshot_path));
389 g_browser_process->notification_ui_manager()->Add(*notification,
390 GetProfile());
392 #endif
395 #if defined(OS_CHROMEOS)
396 Notification* ChromeScreenshotGrabber::CreateNotification(
397 ui::ScreenshotGrabberObserver::Result screenshot_result,
398 const base::FilePath& screenshot_path) {
399 const std::string notification_id(kNotificationId);
400 // We cancel a previous screenshot notification, if any, to ensure we get
401 // a fresh notification pop-up.
402 g_browser_process->notification_ui_manager()->CancelById(
403 notification_id, NotificationUIManager::GetProfileID(GetProfile()));
404 bool success =
405 (screenshot_result == ui::ScreenshotGrabberObserver::SCREENSHOT_SUCCESS);
406 message_center::RichNotificationData optional_field;
407 if (success) {
408 const base::string16 label = l10n_util::GetStringUTF16(
409 IDS_MESSAGE_CENTER_NOTIFICATION_BUTTON_COPY_SCREENSHOT_TO_CLIPBOARD);
410 optional_field.buttons.push_back(message_center::ButtonInfo(label));
412 return new Notification(
413 message_center::NOTIFICATION_TYPE_SIMPLE, GURL(kNotificationOriginUrl),
414 l10n_util::GetStringUTF16(
415 GetScreenshotNotificationTitle(screenshot_result)),
416 l10n_util::GetStringUTF16(
417 GetScreenshotNotificationText(screenshot_result)),
418 ui::ResourceBundle::GetSharedInstance().GetImageNamed(
419 IDR_SCREENSHOT_NOTIFICATION_ICON),
420 message_center::NotifierId(message_center::NotifierId::SYSTEM_COMPONENT,
421 ash::system_notifier::kNotifierScreenshot),
422 l10n_util::GetStringUTF16(IDS_MESSAGE_CENTER_NOTIFIER_SCREENSHOT_NAME),
423 notification_id, optional_field,
424 new ScreenshotGrabberNotificationDelegate(success, GetProfile(),
425 screenshot_path));
427 #endif
429 void ChromeScreenshotGrabber::SetProfileForTest(Profile* profile) {
430 profile_for_test_ = profile;
433 Profile* ChromeScreenshotGrabber::GetProfile() {
434 return profile_for_test_ ? profile_for_test_
435 : ProfileManager::GetActiveUserProfile();