OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/ui/ash/chrome_screenshot_grabber.h" | 5 #include "chrome/browser/ui/ash/chrome_screenshot_grabber.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include "ash/common/system/system_notifier.h" | 9 #include "ash/common/system/system_notifier.h" |
10 #include "ash/shell.h" | 10 #include "ash/shell.h" |
11 #include "ash/strings/grit/ash_strings.h" | 11 #include "ash/strings/grit/ash_strings.h" |
12 #include "base/base64.h" | 12 #include "base/base64.h" |
13 #include "base/bind.h" | 13 #include "base/bind.h" |
14 #include "base/callback.h" | 14 #include "base/callback.h" |
15 #include "base/files/file_util.h" | 15 #include "base/files/file_util.h" |
16 #include "base/i18n/time_formatting.h" | 16 #include "base/i18n/time_formatting.h" |
17 #include "base/macros.h" | 17 #include "base/macros.h" |
| 18 #include "base/metrics/user_metrics.h" |
18 #include "base/strings/stringprintf.h" | 19 #include "base/strings/stringprintf.h" |
19 #include "base/strings/utf_string_conversions.h" | 20 #include "base/strings/utf_string_conversions.h" |
20 #include "base/threading/sequenced_worker_pool.h" | 21 #include "base/threading/sequenced_worker_pool.h" |
21 #include "build/build_config.h" | 22 #include "build/build_config.h" |
22 #include "chrome/browser/browser_process.h" | 23 #include "chrome/browser/browser_process.h" |
23 #include "chrome/browser/chromeos/drive/file_system_util.h" | 24 #include "chrome/browser/chromeos/drive/file_system_util.h" |
24 #include "chrome/browser/chromeos/file_manager/open_util.h" | 25 #include "chrome/browser/chromeos/file_manager/open_util.h" |
25 #include "chrome/browser/chromeos/note_taking_helper.h" | 26 #include "chrome/browser/chromeos/note_taking_helper.h" |
26 #include "chrome/browser/download/download_prefs.h" | 27 #include "chrome/browser/download/download_prefs.h" |
27 #include "chrome/browser/notifications/notification_ui_manager.h" | 28 #include "chrome/browser/notifications/notification_ui_manager.h" |
28 #include "chrome/browser/notifications/notifier_state_tracker.h" | 29 #include "chrome/browser/notifications/notifier_state_tracker.h" |
29 #include "chrome/browser/notifications/notifier_state_tracker_factory.h" | 30 #include "chrome/browser/notifications/notifier_state_tracker_factory.h" |
30 #include "chrome/browser/platform_util.h" | 31 #include "chrome/browser/platform_util.h" |
31 #include "chrome/browser/profiles/profile.h" | 32 #include "chrome/browser/profiles/profile.h" |
32 #include "chrome/browser/profiles/profile_manager.h" | 33 #include "chrome/browser/profiles/profile_manager.h" |
33 #include "chrome/common/pref_names.h" | 34 #include "chrome/common/pref_names.h" |
34 #include "chrome/grit/theme_resources.h" | 35 #include "chrome/grit/theme_resources.h" |
35 #include "chromeos/login/login_state.h" | 36 #include "chromeos/login/login_state.h" |
36 #include "components/drive/chromeos/file_system_interface.h" | 37 #include "components/drive/chromeos/file_system_interface.h" |
37 #include "components/prefs/pref_service.h" | 38 #include "components/prefs/pref_service.h" |
38 #include "content/public/browser/browser_thread.h" | 39 #include "content/public/browser/browser_thread.h" |
39 #include "content/public/browser/user_metrics.h" | |
40 #include "ui/base/clipboard/clipboard.h" | 40 #include "ui/base/clipboard/clipboard.h" |
41 #include "ui/base/clipboard/scoped_clipboard_writer.h" | 41 #include "ui/base/clipboard/scoped_clipboard_writer.h" |
42 #include "ui/base/l10n/l10n_util.h" | 42 #include "ui/base/l10n/l10n_util.h" |
43 #include "ui/base/resource/resource_bundle.h" | 43 #include "ui/base/resource/resource_bundle.h" |
44 #include "ui/strings/grit/ui_strings.h" | 44 #include "ui/strings/grit/ui_strings.h" |
45 | 45 |
46 namespace { | 46 namespace { |
47 | 47 |
48 const char kNotificationId[] = "screenshot"; | 48 const char kNotificationId[] = "screenshot"; |
49 | 49 |
(...skipping 11 matching lines...) Expand all Loading... |
61 // Only care about HTML because Chrome OS doesn't need other formats. | 61 // Only care about HTML because Chrome OS doesn't need other formats. |
62 // TODO(dcheng): Why don't we take advantage of the ability to write bitmaps | 62 // TODO(dcheng): Why don't we take advantage of the ability to write bitmaps |
63 // to the clipboard here? | 63 // to the clipboard here? |
64 { | 64 { |
65 ui::ScopedClipboardWriter scw(ui::CLIPBOARD_TYPE_COPY_PASTE); | 65 ui::ScopedClipboardWriter scw(ui::CLIPBOARD_TYPE_COPY_PASTE); |
66 std::string html(kImageClipboardFormatPrefix); | 66 std::string html(kImageClipboardFormatPrefix); |
67 html += encoded; | 67 html += encoded; |
68 html += kImageClipboardFormatSuffix; | 68 html += kImageClipboardFormatSuffix; |
69 scw.WriteHTML(base::UTF8ToUTF16(html), std::string()); | 69 scw.WriteHTML(base::UTF8ToUTF16(html), std::string()); |
70 } | 70 } |
71 content::RecordAction(base::UserMetricsAction("Screenshot_CopyClipboard")); | 71 base::RecordAction(base::UserMetricsAction("Screenshot_CopyClipboard")); |
72 } | 72 } |
73 | 73 |
74 void ReadFileAndCopyToClipboardLocal(const base::FilePath& screenshot_path) { | 74 void ReadFileAndCopyToClipboardLocal(const base::FilePath& screenshot_path) { |
75 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); | 75 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); |
76 | 76 |
77 scoped_refptr<base::RefCountedString> png_data(new base::RefCountedString()); | 77 scoped_refptr<base::RefCountedString> png_data(new base::RefCountedString()); |
78 if (!base::ReadFileToString(screenshot_path, &(png_data->data()))) { | 78 if (!base::ReadFileToString(screenshot_path, &(png_data->data()))) { |
79 LOG(ERROR) << "Failed to read the screenshot file: " | 79 LOG(ERROR) << "Failed to read the screenshot file: " |
80 << screenshot_path.value(); | 80 << screenshot_path.value(); |
81 return; | 81 return; |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
133 } | 133 } |
134 content::BrowserThread::GetBlockingPool()->PostTask( | 134 content::BrowserThread::GetBlockingPool()->PostTask( |
135 FROM_HERE, | 135 FROM_HERE, |
136 base::Bind(&ReadFileAndCopyToClipboardLocal, screenshot_path_)); | 136 base::Bind(&ReadFileAndCopyToClipboardLocal, screenshot_path_)); |
137 break; | 137 break; |
138 } | 138 } |
139 case BUTTON_ANNOTATE: { | 139 case BUTTON_ANNOTATE: { |
140 chromeos::NoteTakingHelper* helper = chromeos::NoteTakingHelper::Get(); | 140 chromeos::NoteTakingHelper* helper = chromeos::NoteTakingHelper::Get(); |
141 if (helper->IsAppAvailable(profile_)) { | 141 if (helper->IsAppAvailable(profile_)) { |
142 helper->LaunchAppForNewNote(profile_, screenshot_path_); | 142 helper->LaunchAppForNewNote(profile_, screenshot_path_); |
143 content::RecordAction(base::UserMetricsAction("Screenshot_Annotate")); | 143 base::RecordAction(base::UserMetricsAction("Screenshot_Annotate")); |
144 } | 144 } |
145 break; | 145 break; |
146 } | 146 } |
147 default: | 147 default: |
148 NOTREACHED() << "Unhandled button index " << button_index; | 148 NOTREACHED() << "Unhandled button index " << button_index; |
149 } | 149 } |
150 } | 150 } |
151 bool HasClickedListener() override { return success_; } | 151 bool HasClickedListener() override { return success_; } |
152 std::string id() const override { return std::string(kNotificationId); } | 152 std::string id() const override { return std::string(kNotificationId); } |
153 | 153 |
(...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
320 for (size_t i = 0; i < root_windows.size(); ++i) { | 320 for (size_t i = 0; i < root_windows.size(); ++i) { |
321 aura::Window* root_window = root_windows[i]; | 321 aura::Window* root_window = root_windows[i]; |
322 std::string basename = screenshot_basename; | 322 std::string basename = screenshot_basename; |
323 gfx::Rect rect = root_window->bounds(); | 323 gfx::Rect rect = root_window->bounds(); |
324 if (root_windows.size() > 1) | 324 if (root_windows.size() > 1) |
325 basename += base::StringPrintf(" - Display %d", static_cast<int>(i + 1)); | 325 basename += base::StringPrintf(" - Display %d", static_cast<int>(i + 1)); |
326 base::FilePath screenshot_path = | 326 base::FilePath screenshot_path = |
327 screenshot_directory.AppendASCII(basename + ".png"); | 327 screenshot_directory.AppendASCII(basename + ".png"); |
328 screenshot_grabber_->TakeScreenshot(root_window, rect, screenshot_path); | 328 screenshot_grabber_->TakeScreenshot(root_window, rect, screenshot_path); |
329 } | 329 } |
330 content::RecordAction(base::UserMetricsAction("Screenshot_TakeFull")); | 330 base::RecordAction(base::UserMetricsAction("Screenshot_TakeFull")); |
331 } | 331 } |
332 | 332 |
333 void ChromeScreenshotGrabber::HandleTakePartialScreenshot( | 333 void ChromeScreenshotGrabber::HandleTakePartialScreenshot( |
334 aura::Window* window, | 334 aura::Window* window, |
335 const gfx::Rect& rect) { | 335 const gfx::Rect& rect) { |
336 if (ScreenshotsDisabled()) { | 336 if (ScreenshotsDisabled()) { |
337 screenshot_grabber_->NotifyScreenshotCompleted( | 337 screenshot_grabber_->NotifyScreenshotCompleted( |
338 ui::ScreenshotGrabberObserver::SCREENSHOTS_DISABLED, base::FilePath()); | 338 ui::ScreenshotGrabberObserver::SCREENSHOTS_DISABLED, base::FilePath()); |
339 return; | 339 return; |
340 } | 340 } |
341 | 341 |
342 base::FilePath screenshot_directory; | 342 base::FilePath screenshot_directory; |
343 if (!GetScreenshotDirectory(&screenshot_directory)) { | 343 if (!GetScreenshotDirectory(&screenshot_directory)) { |
344 screenshot_grabber_->NotifyScreenshotCompleted( | 344 screenshot_grabber_->NotifyScreenshotCompleted( |
345 ui::ScreenshotGrabberObserver::SCREENSHOT_GET_DIR_FAILED, | 345 ui::ScreenshotGrabberObserver::SCREENSHOT_GET_DIR_FAILED, |
346 base::FilePath()); | 346 base::FilePath()); |
347 return; | 347 return; |
348 } | 348 } |
349 | 349 |
350 base::FilePath screenshot_path = | 350 base::FilePath screenshot_path = |
351 screenshot_directory.AppendASCII(GetScreenshotBaseFilename() + ".png"); | 351 screenshot_directory.AppendASCII(GetScreenshotBaseFilename() + ".png"); |
352 screenshot_grabber_->TakeScreenshot(window, rect, screenshot_path); | 352 screenshot_grabber_->TakeScreenshot(window, rect, screenshot_path); |
353 content::RecordAction(base::UserMetricsAction("Screenshot_TakePartial")); | 353 base::RecordAction(base::UserMetricsAction("Screenshot_TakePartial")); |
354 } | 354 } |
355 | 355 |
356 void ChromeScreenshotGrabber::HandleTakeWindowScreenshot(aura::Window* window) { | 356 void ChromeScreenshotGrabber::HandleTakeWindowScreenshot(aura::Window* window) { |
357 if (ScreenshotsDisabled()) { | 357 if (ScreenshotsDisabled()) { |
358 screenshot_grabber_->NotifyScreenshotCompleted( | 358 screenshot_grabber_->NotifyScreenshotCompleted( |
359 ui::ScreenshotGrabberObserver::SCREENSHOTS_DISABLED, base::FilePath()); | 359 ui::ScreenshotGrabberObserver::SCREENSHOTS_DISABLED, base::FilePath()); |
360 return; | 360 return; |
361 } | 361 } |
362 | 362 |
363 base::FilePath screenshot_directory; | 363 base::FilePath screenshot_directory; |
364 if (!GetScreenshotDirectory(&screenshot_directory)) { | 364 if (!GetScreenshotDirectory(&screenshot_directory)) { |
365 screenshot_grabber_->NotifyScreenshotCompleted( | 365 screenshot_grabber_->NotifyScreenshotCompleted( |
366 ui::ScreenshotGrabberObserver::SCREENSHOT_GET_DIR_FAILED, | 366 ui::ScreenshotGrabberObserver::SCREENSHOT_GET_DIR_FAILED, |
367 base::FilePath()); | 367 base::FilePath()); |
368 return; | 368 return; |
369 } | 369 } |
370 | 370 |
371 base::FilePath screenshot_path = | 371 base::FilePath screenshot_path = |
372 screenshot_directory.AppendASCII(GetScreenshotBaseFilename() + ".png"); | 372 screenshot_directory.AppendASCII(GetScreenshotBaseFilename() + ".png"); |
373 screenshot_grabber_->TakeScreenshot(window, | 373 screenshot_grabber_->TakeScreenshot(window, |
374 gfx::Rect(window->bounds().size()), | 374 gfx::Rect(window->bounds().size()), |
375 screenshot_path); | 375 screenshot_path); |
376 content::RecordAction(base::UserMetricsAction("Screenshot_TakeWindow")); | 376 base::RecordAction(base::UserMetricsAction("Screenshot_TakeWindow")); |
377 } | 377 } |
378 | 378 |
379 bool ChromeScreenshotGrabber::CanTakeScreenshot() { | 379 bool ChromeScreenshotGrabber::CanTakeScreenshot() { |
380 return screenshot_grabber_->CanTakeScreenshot(); | 380 return screenshot_grabber_->CanTakeScreenshot(); |
381 } | 381 } |
382 | 382 |
383 void ChromeScreenshotGrabber::PrepareFileAndRunOnBlockingPool( | 383 void ChromeScreenshotGrabber::PrepareFileAndRunOnBlockingPool( |
384 const base::FilePath& path, | 384 const base::FilePath& path, |
385 scoped_refptr<base::TaskRunner> blocking_task_runner, | 385 scoped_refptr<base::TaskRunner> blocking_task_runner, |
386 const FileCallback& callback) { | 386 const FileCallback& callback) { |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
466 } | 466 } |
467 | 467 |
468 void ChromeScreenshotGrabber::SetProfileForTest(Profile* profile) { | 468 void ChromeScreenshotGrabber::SetProfileForTest(Profile* profile) { |
469 profile_for_test_ = profile; | 469 profile_for_test_ = profile; |
470 } | 470 } |
471 | 471 |
472 Profile* ChromeScreenshotGrabber::GetProfile() { | 472 Profile* ChromeScreenshotGrabber::GetProfile() { |
473 return profile_for_test_ ? profile_for_test_ | 473 return profile_for_test_ ? profile_for_test_ |
474 : ProfileManager::GetActiveUserProfile(); | 474 : ProfileManager::GetActiveUserProfile(); |
475 } | 475 } |
OLD | NEW |