Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(78)

Side by Side Diff: chrome/browser/ui/ash/chrome_screenshot_grabber.cc

Issue 1960293003: Remove OS_CHROMEOS from ui/ash code (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebase Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/shell.h" 9 #include "ash/shell.h"
10 #include "ash/system/system_notifier.h" 10 #include "ash/system/system_notifier.h"
11 #include "base/base64.h" 11 #include "base/base64.h"
12 #include "base/bind.h" 12 #include "base/bind.h"
13 #include "base/callback.h" 13 #include "base/callback.h"
14 #include "base/files/file_util.h" 14 #include "base/files/file_util.h"
15 #include "base/i18n/time_formatting.h" 15 #include "base/i18n/time_formatting.h"
16 #include "base/macros.h" 16 #include "base/macros.h"
17 #include "base/strings/stringprintf.h" 17 #include "base/strings/stringprintf.h"
18 #include "base/strings/utf_string_conversions.h" 18 #include "base/strings/utf_string_conversions.h"
19 #include "build/build_config.h" 19 #include "build/build_config.h"
20 #include "chrome/browser/browser_process.h" 20 #include "chrome/browser/browser_process.h"
21 #include "chrome/browser/chromeos/drive/file_system_util.h"
22 #include "chrome/browser/chromeos/file_manager/open_util.h"
21 #include "chrome/browser/download/download_prefs.h" 23 #include "chrome/browser/download/download_prefs.h"
22 #include "chrome/browser/notifications/notification_ui_manager.h" 24 #include "chrome/browser/notifications/notification_ui_manager.h"
25 #include "chrome/browser/notifications/notifier_state_tracker.h"
26 #include "chrome/browser/notifications/notifier_state_tracker_factory.h"
23 #include "chrome/browser/platform_util.h" 27 #include "chrome/browser/platform_util.h"
24 #include "chrome/browser/profiles/profile.h" 28 #include "chrome/browser/profiles/profile.h"
25 #include "chrome/browser/profiles/profile_manager.h" 29 #include "chrome/browser/profiles/profile_manager.h"
26 #include "chrome/common/pref_names.h" 30 #include "chrome/common/pref_names.h"
31 #include "chromeos/login/login_state.h"
32 #include "components/drive/chromeos/file_system_interface.h"
27 #include "components/prefs/pref_service.h" 33 #include "components/prefs/pref_service.h"
28 #include "content/public/browser/browser_thread.h" 34 #include "content/public/browser/browser_thread.h"
29 #include "content/public/browser/user_metrics.h" 35 #include "content/public/browser/user_metrics.h"
30 #include "grit/ash_strings.h" 36 #include "grit/ash_strings.h"
31 #include "grit/theme_resources.h" 37 #include "grit/theme_resources.h"
32 #include "ui/base/clipboard/clipboard.h" 38 #include "ui/base/clipboard/clipboard.h"
33 #include "ui/base/clipboard/scoped_clipboard_writer.h" 39 #include "ui/base/clipboard/scoped_clipboard_writer.h"
34 #include "ui/base/l10n/l10n_util.h" 40 #include "ui/base/l10n/l10n_util.h"
35 #include "ui/base/resource/resource_bundle.h" 41 #include "ui/base/resource/resource_bundle.h"
36 #include "ui/strings/grit/ui_strings.h" 42 #include "ui/strings/grit/ui_strings.h"
37 43
38 #if defined(OS_CHROMEOS)
39 #include "chrome/browser/chromeos/drive/file_system_util.h"
40 #include "chrome/browser/chromeos/file_manager/open_util.h"
41 #include "chrome/browser/notifications/notifier_state_tracker.h"
42 #include "chrome/browser/notifications/notifier_state_tracker_factory.h"
43 #include "chromeos/login/login_state.h"
44 #include "components/drive/chromeos/file_system_interface.h"
45 #endif
46
47 namespace { 44 namespace {
48 45
49 const char kNotificationId[] = "screenshot"; 46 const char kNotificationId[] = "screenshot";
50 47
51 #if defined(OS_CHROMEOS)
52 const char kNotificationOriginUrl[] = "chrome://screenshot"; 48 const char kNotificationOriginUrl[] = "chrome://screenshot";
53 #endif
54 49
55 const char kImageClipboardFormatPrefix[] = "<img src='data:image/png;base64,"; 50 const char kImageClipboardFormatPrefix[] = "<img src='data:image/png;base64,";
56 const char kImageClipboardFormatSuffix[] = "'>"; 51 const char kImageClipboardFormatSuffix[] = "'>";
57 52
58 void CopyScreenshotToClipboard(scoped_refptr<base::RefCountedString> png_data) { 53 void CopyScreenshotToClipboard(scoped_refptr<base::RefCountedString> png_data) {
59 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 54 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
60 55
61 std::string encoded; 56 std::string encoded;
62 base::Base64Encode(png_data->data(), &encoded); 57 base::Base64Encode(png_data->data(), &encoded);
63 58
(...skipping 18 matching lines...) Expand all
82 LOG(ERROR) << "Failed to read the screenshot file: " 77 LOG(ERROR) << "Failed to read the screenshot file: "
83 << screenshot_path.value(); 78 << screenshot_path.value();
84 return; 79 return;
85 } 80 }
86 81
87 content::BrowserThread::PostTask( 82 content::BrowserThread::PostTask(
88 content::BrowserThread::UI, FROM_HERE, 83 content::BrowserThread::UI, FROM_HERE,
89 base::Bind(CopyScreenshotToClipboard, png_data)); 84 base::Bind(CopyScreenshotToClipboard, png_data));
90 } 85 }
91 86
92 #if defined(OS_CHROMEOS)
93 void ReadFileAndCopyToClipboardDrive( 87 void ReadFileAndCopyToClipboardDrive(
94 drive::FileError error, 88 drive::FileError error,
95 const base::FilePath& file_path, 89 const base::FilePath& file_path,
96 std::unique_ptr<drive::ResourceEntry> entry) { 90 std::unique_ptr<drive::ResourceEntry> entry) {
97 if (error != drive::FILE_ERROR_OK) { 91 if (error != drive::FILE_ERROR_OK) {
98 LOG(ERROR) << "Failed to read the screenshot path on drive: " 92 LOG(ERROR) << "Failed to read the screenshot path on drive: "
99 << drive::FileErrorToString(error); 93 << drive::FileErrorToString(error);
100 return; 94 return;
101 } 95 }
102 content::BrowserThread::GetBlockingPool()->PostTask( 96 content::BrowserThread::GetBlockingPool()->PostTask(
103 FROM_HERE, base::Bind(&ReadFileAndCopyToClipboardLocal, file_path)); 97 FROM_HERE, base::Bind(&ReadFileAndCopyToClipboardLocal, file_path));
104 } 98 }
105 #endif
106 99
107 // Delegate for a notification. This class has two roles: to implement callback 100 // Delegate for a notification. This class has two roles: to implement callback
108 // methods for notification, and to provide an identity of the associated 101 // methods for notification, and to provide an identity of the associated
109 // notification. 102 // notification.
110 class ScreenshotGrabberNotificationDelegate : public NotificationDelegate { 103 class ScreenshotGrabberNotificationDelegate : public NotificationDelegate {
111 public: 104 public:
112 ScreenshotGrabberNotificationDelegate(bool success, 105 ScreenshotGrabberNotificationDelegate(bool success,
113 Profile* profile, 106 Profile* profile,
114 const base::FilePath& screenshot_path) 107 const base::FilePath& screenshot_path)
115 : success_(success), 108 : success_(success),
116 profile_(profile), 109 profile_(profile),
117 screenshot_path_(screenshot_path) {} 110 screenshot_path_(screenshot_path) {}
118 111
119 // Overridden from NotificationDelegate: 112 // Overridden from NotificationDelegate:
120 void Click() override { 113 void Click() override {
121 if (!success_) 114 if (!success_)
122 return; 115 return;
123 platform_util::ShowItemInFolder(profile_, screenshot_path_); 116 platform_util::ShowItemInFolder(profile_, screenshot_path_);
124 } 117 }
125 void ButtonClick(int button_index) override { 118 void ButtonClick(int button_index) override {
126 DCHECK(success_ && button_index == 0); 119 DCHECK(success_ && button_index == 0);
127 120
128 // To avoid keeping the screenshot image on memory, it will re-read the 121 // To avoid keeping the screenshot image on memory, it will re-read the
129 // screenshot file and copy it to the clipboard. 122 // screenshot file and copy it to the clipboard.
130 #if defined(OS_CHROMEOS)
131 if (drive::util::IsUnderDriveMountPoint(screenshot_path_)) { 123 if (drive::util::IsUnderDriveMountPoint(screenshot_path_)) {
132 drive::FileSystemInterface* file_system = 124 drive::FileSystemInterface* file_system =
133 drive::util::GetFileSystemByProfile(profile_); 125 drive::util::GetFileSystemByProfile(profile_);
134 file_system->GetFile(drive::util::ExtractDrivePath(screenshot_path_), 126 file_system->GetFile(drive::util::ExtractDrivePath(screenshot_path_),
135 base::Bind(&ReadFileAndCopyToClipboardDrive)); 127 base::Bind(&ReadFileAndCopyToClipboardDrive));
136 return; 128 return;
137 } 129 }
138 #endif 130
139 content::BrowserThread::GetBlockingPool()->PostTask( 131 content::BrowserThread::GetBlockingPool()->PostTask(
140 FROM_HERE, 132 FROM_HERE,
141 base::Bind(&ReadFileAndCopyToClipboardLocal, screenshot_path_)); 133 base::Bind(&ReadFileAndCopyToClipboardLocal, screenshot_path_));
142 } 134 }
143 bool HasClickedListener() override { return success_; } 135 bool HasClickedListener() override { return success_; }
144 std::string id() const override { return std::string(kNotificationId); } 136 std::string id() const override { return std::string(kNotificationId); }
145 137
146 private: 138 private:
147 ~ScreenshotGrabberNotificationDelegate() override {} 139 ~ScreenshotGrabberNotificationDelegate() override {}
148 140
149 const bool success_; 141 const bool success_;
150 Profile* profile_; 142 Profile* profile_;
151 const base::FilePath screenshot_path_; 143 const base::FilePath screenshot_path_;
152 144
153 DISALLOW_COPY_AND_ASSIGN(ScreenshotGrabberNotificationDelegate); 145 DISALLOW_COPY_AND_ASSIGN(ScreenshotGrabberNotificationDelegate);
154 }; 146 };
155 147
156 #if defined(OS_CHROMEOS)
157 int GetScreenshotNotificationTitle( 148 int GetScreenshotNotificationTitle(
158 ui::ScreenshotGrabberObserver::Result screenshot_result) { 149 ui::ScreenshotGrabberObserver::Result screenshot_result) {
159 switch (screenshot_result) { 150 switch (screenshot_result) {
160 case ui::ScreenshotGrabberObserver::SCREENSHOTS_DISABLED: 151 case ui::ScreenshotGrabberObserver::SCREENSHOTS_DISABLED:
161 return IDS_ASH_SCREENSHOT_NOTIFICATION_TITLE_DISABLED; 152 return IDS_ASH_SCREENSHOT_NOTIFICATION_TITLE_DISABLED;
162 case ui::ScreenshotGrabberObserver::SCREENSHOT_SUCCESS: 153 case ui::ScreenshotGrabberObserver::SCREENSHOT_SUCCESS:
163 return IDS_ASH_SCREENSHOT_NOTIFICATION_TITLE_SUCCESS; 154 return IDS_ASH_SCREENSHOT_NOTIFICATION_TITLE_SUCCESS;
164 default: 155 default:
165 return IDS_ASH_SCREENSHOT_NOTIFICATION_TITLE_FAIL; 156 return IDS_ASH_SCREENSHOT_NOTIFICATION_TITLE_FAIL;
166 } 157 }
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
201 } else { 192 } else {
202 LOG(ERROR) << "Failed to ensure the existence of the specified directory " 193 LOG(ERROR) << "Failed to ensure the existence of the specified directory "
203 << "in Google Drive: " << error; 194 << "in Google Drive: " << error;
204 content::BrowserThread::GetBlockingPool()->PostTask( 195 content::BrowserThread::GetBlockingPool()->PostTask(
205 FROM_HERE, 196 FROM_HERE,
206 base::Bind(callback, 197 base::Bind(callback,
207 ui::ScreenshotGrabberDelegate::FILE_CHECK_DIR_FAILED, 198 ui::ScreenshotGrabberDelegate::FILE_CHECK_DIR_FAILED,
208 base::FilePath())); 199 base::FilePath()));
209 } 200 }
210 } 201 }
211 #endif
212 202
213 bool ScreenshotsDisabled() { 203 bool ScreenshotsDisabled() {
214 return g_browser_process->local_state()->GetBoolean( 204 return g_browser_process->local_state()->GetBoolean(
215 prefs::kDisableScreenshots); 205 prefs::kDisableScreenshots);
216 } 206 }
217 207
218 bool ShouldUse24HourClock() { 208 bool ShouldUse24HourClock() {
219 #if defined(OS_CHROMEOS)
220 Profile* profile = ProfileManager::GetActiveUserProfile(); 209 Profile* profile = ProfileManager::GetActiveUserProfile();
221 if (profile) { 210 if (profile)
222 return profile->GetPrefs()->GetBoolean(prefs::kUse24HourClock); 211 return profile->GetPrefs()->GetBoolean(prefs::kUse24HourClock);
223 }
224 #endif
225 return base::GetHourClockType() == base::k24HourClock; 212 return base::GetHourClockType() == base::k24HourClock;
226 } 213 }
227 214
228 bool GetScreenshotDirectory(base::FilePath* directory) { 215 bool GetScreenshotDirectory(base::FilePath* directory) {
229 bool is_logged_in = true; 216 if (chromeos::LoginState::Get()->IsUserLoggedIn()) {
230
231 #if defined(OS_CHROMEOS)
232 is_logged_in = chromeos::LoginState::Get()->IsUserLoggedIn();
233 #endif
234
235 if (is_logged_in) {
236 DownloadPrefs* download_prefs = DownloadPrefs::FromBrowserContext( 217 DownloadPrefs* download_prefs = DownloadPrefs::FromBrowserContext(
237 ProfileManager::GetActiveUserProfile()); 218 ProfileManager::GetActiveUserProfile());
238 *directory = download_prefs->DownloadPath(); 219 *directory = download_prefs->DownloadPath();
239 } else { 220 } else {
240 if (!base::GetTempDir(directory)) { 221 if (!base::GetTempDir(directory)) {
241 LOG(ERROR) << "Failed to find temporary directory."; 222 LOG(ERROR) << "Failed to find temporary directory.";
242 return false; 223 return false;
243 } 224 }
244 } 225 }
245 return true; 226 return true;
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after
375 } 356 }
376 357
377 bool ChromeScreenshotGrabber::CanTakeScreenshot() { 358 bool ChromeScreenshotGrabber::CanTakeScreenshot() {
378 return screenshot_grabber_->CanTakeScreenshot(); 359 return screenshot_grabber_->CanTakeScreenshot();
379 } 360 }
380 361
381 void ChromeScreenshotGrabber::PrepareFileAndRunOnBlockingPool( 362 void ChromeScreenshotGrabber::PrepareFileAndRunOnBlockingPool(
382 const base::FilePath& path, 363 const base::FilePath& path,
383 scoped_refptr<base::TaskRunner> blocking_task_runner, 364 scoped_refptr<base::TaskRunner> blocking_task_runner,
384 const FileCallback& callback) { 365 const FileCallback& callback) {
385 #if defined(OS_CHROMEOS)
386 Profile* profile = ProfileManager::GetActiveUserProfile(); 366 Profile* profile = ProfileManager::GetActiveUserProfile();
387 if (drive::util::IsUnderDriveMountPoint(path)) { 367 if (drive::util::IsUnderDriveMountPoint(path)) {
388 drive::util::EnsureDirectoryExists( 368 drive::util::EnsureDirectoryExists(
389 profile, path.DirName(), 369 profile, path.DirName(),
390 base::Bind(&EnsureDirectoryExistsCallback, callback, profile, path)); 370 base::Bind(&EnsureDirectoryExistsCallback, callback, profile, path));
391 return; 371 return;
392 } 372 }
393 #endif
394 ui::ScreenshotGrabberDelegate::PrepareFileAndRunOnBlockingPool( 373 ui::ScreenshotGrabberDelegate::PrepareFileAndRunOnBlockingPool(
395 path, blocking_task_runner, callback); 374 path, blocking_task_runner, callback);
396 } 375 }
397 376
398 void ChromeScreenshotGrabber::OnScreenshotCompleted( 377 void ChromeScreenshotGrabber::OnScreenshotCompleted(
399 ui::ScreenshotGrabberObserver::Result result, 378 ui::ScreenshotGrabberObserver::Result result,
400 const base::FilePath& screenshot_path) { 379 const base::FilePath& screenshot_path) {
401 #if defined(OS_CHROMEOS)
402 // Do not show a notification that a screenshot was taken while no user is 380 // Do not show a notification that a screenshot was taken while no user is
403 // logged in, since it is confusing for the user to get a message about it 381 // logged in, since it is confusing for the user to get a message about it
404 // after he logs in (crbug.com/235217). 382 // after he logs in (crbug.com/235217).
405 if (!chromeos::LoginState::Get()->IsUserLoggedIn()) 383 if (!chromeos::LoginState::Get()->IsUserLoggedIn())
406 return; 384 return;
407 385
408 // TODO(sschmitz): make this work for Windows. 386 // TODO(sschmitz): make this work for Windows.
409 NotifierStateTracker* const notifier_state_tracker = 387 NotifierStateTracker* const notifier_state_tracker =
410 NotifierStateTrackerFactory::GetForProfile(GetProfile()); 388 NotifierStateTrackerFactory::GetForProfile(GetProfile());
411 if (notifier_state_tracker->IsNotifierEnabled(message_center::NotifierId( 389 if (notifier_state_tracker->IsNotifierEnabled(message_center::NotifierId(
412 message_center::NotifierId::SYSTEM_COMPONENT, 390 message_center::NotifierId::SYSTEM_COMPONENT,
413 ash::system_notifier::kNotifierScreenshot))) { 391 ash::system_notifier::kNotifierScreenshot))) {
414 std::unique_ptr<Notification> notification( 392 std::unique_ptr<Notification> notification(
415 CreateNotification(result, screenshot_path)); 393 CreateNotification(result, screenshot_path));
416 g_browser_process->notification_ui_manager()->Add(*notification, 394 g_browser_process->notification_ui_manager()->Add(*notification,
417 GetProfile()); 395 GetProfile());
418 } 396 }
419 #endif
420 } 397 }
421 398
422 #if defined(OS_CHROMEOS)
423 Notification* ChromeScreenshotGrabber::CreateNotification( 399 Notification* ChromeScreenshotGrabber::CreateNotification(
424 ui::ScreenshotGrabberObserver::Result screenshot_result, 400 ui::ScreenshotGrabberObserver::Result screenshot_result,
425 const base::FilePath& screenshot_path) { 401 const base::FilePath& screenshot_path) {
426 const std::string notification_id(kNotificationId); 402 const std::string notification_id(kNotificationId);
427 // We cancel a previous screenshot notification, if any, to ensure we get 403 // We cancel a previous screenshot notification, if any, to ensure we get
428 // a fresh notification pop-up. 404 // a fresh notification pop-up.
429 g_browser_process->notification_ui_manager()->CancelById( 405 g_browser_process->notification_ui_manager()->CancelById(
430 notification_id, NotificationUIManager::GetProfileID(GetProfile())); 406 notification_id, NotificationUIManager::GetProfileID(GetProfile()));
431 bool success = 407 bool success =
432 (screenshot_result == ui::ScreenshotGrabberObserver::SCREENSHOT_SUCCESS); 408 (screenshot_result == ui::ScreenshotGrabberObserver::SCREENSHOT_SUCCESS);
(...skipping 11 matching lines...) Expand all
444 GetScreenshotNotificationText(screenshot_result)), 420 GetScreenshotNotificationText(screenshot_result)),
445 ui::ResourceBundle::GetSharedInstance().GetImageNamed( 421 ui::ResourceBundle::GetSharedInstance().GetImageNamed(
446 IDR_SCREENSHOT_NOTIFICATION_ICON), 422 IDR_SCREENSHOT_NOTIFICATION_ICON),
447 message_center::NotifierId(message_center::NotifierId::SYSTEM_COMPONENT, 423 message_center::NotifierId(message_center::NotifierId::SYSTEM_COMPONENT,
448 ash::system_notifier::kNotifierScreenshot), 424 ash::system_notifier::kNotifierScreenshot),
449 l10n_util::GetStringUTF16(IDS_MESSAGE_CENTER_NOTIFIER_SCREENSHOT_NAME), 425 l10n_util::GetStringUTF16(IDS_MESSAGE_CENTER_NOTIFIER_SCREENSHOT_NAME),
450 GURL(kNotificationOriginUrl), notification_id, optional_field, 426 GURL(kNotificationOriginUrl), notification_id, optional_field,
451 new ScreenshotGrabberNotificationDelegate(success, GetProfile(), 427 new ScreenshotGrabberNotificationDelegate(success, GetProfile(),
452 screenshot_path)); 428 screenshot_path));
453 } 429 }
454 #endif
455 430
456 void ChromeScreenshotGrabber::SetProfileForTest(Profile* profile) { 431 void ChromeScreenshotGrabber::SetProfileForTest(Profile* profile) {
457 profile_for_test_ = profile; 432 profile_for_test_ = profile;
458 } 433 }
459 434
460 Profile* ChromeScreenshotGrabber::GetProfile() { 435 Profile* ChromeScreenshotGrabber::GetProfile() {
461 return profile_for_test_ ? profile_for_test_ 436 return profile_for_test_ ? profile_for_test_
462 : ProfileManager::GetActiveUserProfile(); 437 : ProfileManager::GetActiveUserProfile();
463 } 438 }
OLDNEW
« no previous file with comments | « chrome/browser/ui/ash/chrome_screenshot_grabber.h ('k') | chrome/browser/ui/ash/chrome_screenshot_grabber_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698