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

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

Issue 13105002: Screenshot effect non-obvious (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: review Created 7 years, 8 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
« no previous file with comments | « chrome/browser/ui/ash/screenshot_taker.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 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/screenshot_taker.h" 5 #include "chrome/browser/ui/ash/screenshot_taker.h"
6 6
7 #include <climits> 7 #include <climits>
8 #include <string> 8 #include <string>
9 9
10 #include "ash/shell.h" 10 #include "ash/shell.h"
11 #include "ash/shell_delegate.h" 11 #include "ash/shell_delegate.h"
12 #include "ash/shell_window_ids.h" 12 #include "ash/shell_window_ids.h"
13 #include "base/bind.h" 13 #include "base/bind.h"
14 #include "base/file_util.h" 14 #include "base/file_util.h"
15 #include "base/files/file_path.h" 15 #include "base/files/file_path.h"
16 #include "base/logging.h" 16 #include "base/logging.h"
17 #include "base/memory/ref_counted_memory.h" 17 #include "base/memory/ref_counted_memory.h"
18 #include "base/stringprintf.h" 18 #include "base/stringprintf.h"
19 #include "base/threading/sequenced_worker_pool.h" 19 #include "base/threading/sequenced_worker_pool.h"
20 #include "base/time.h" 20 #include "base/time.h"
21 #include "base/utf_string_conversions.h"
22 #include "chrome/browser/browser_process.h"
23 #include "chrome/browser/chromeos/extensions/file_manager_util.h"
21 #include "chrome/browser/download/download_prefs.h" 24 #include "chrome/browser/download/download_prefs.h"
25 #include "chrome/browser/notifications/notification.h"
26 #include "chrome/browser/notifications/notification_ui_manager.h"
22 #include "chrome/browser/profiles/profile.h" 27 #include "chrome/browser/profiles/profile.h"
23 #include "chrome/browser/profiles/profile_manager.h" 28 #include "chrome/browser/profiles/profile_manager.h"
24 #include "chrome/browser/ui/webui/screenshot_source.h" 29 #include "chrome/browser/ui/webui/screenshot_source.h"
25 #include "chrome/browser/ui/window_snapshot/window_snapshot.h" 30 #include "chrome/browser/ui/window_snapshot/window_snapshot.h"
26 #include "content/public/browser/browser_thread.h" 31 #include "content/public/browser/browser_thread.h"
32 #include "grit/ash_strings.h"
33 #include "grit/theme_resources.h"
27 #include "ui/aura/root_window.h" 34 #include "ui/aura/root_window.h"
28 #include "ui/aura/window.h" 35 #include "ui/aura/window.h"
36 #include "ui/base/l10n/l10n_util.h"
37 #include "ui/base/resource/resource_bundle.h"
38 #include "ui/gfx/image/image.h"
29 39
30 #if defined(OS_CHROMEOS) 40 #if defined(OS_CHROMEOS)
31 #include "chrome/browser/chromeos/drive/drive_file_system_util.h" 41 #include "chrome/browser/chromeos/drive/drive_file_system_util.h"
32 #include "chrome/browser/chromeos/login/user_manager.h" 42 #include "chrome/browser/chromeos/login/user_manager.h"
33 #endif 43 #endif
34 44
35 namespace { 45 namespace {
36 // How opaque should the layer that we flash onscreen to provide visual
37 // feedback after the screenshot is taken be?
38 const float kVisualFeedbackLayerOpacity = 0.25f;
39
40 // How long should the visual feedback layer be displayed?
41 const int64 kVisualFeedbackLayerDisplayTimeMs = 100;
42
43 // The minimum interval between two screenshot commands. It has to be 46 // The minimum interval between two screenshot commands. It has to be
44 // more than 1000 to prevent the conflict of filenames. 47 // more than 1000 to prevent the conflict of filenames.
45 const int kScreenshotMinimumIntervalInMS = 1000; 48 const int kScreenshotMinimumIntervalInMS = 1000;
46 49
50 // Origin URL for notifications.
51 const GURL kNotificationOriginUrl("chrome://screenshot/");
52
53 // Mostly unchanging data used in notifications.
54 struct NotificationData {
55 static void init() {
56 static bool is_init = false;
57 if (is_init)
58 return;
59 is_init = true;
60 notification_icon = ui::ResourceBundle::GetSharedInstance().GetImageNamed(
James Cook 2013/03/27 16:49:17 Why not just look up these images and resources ev
sschmitz 2013/03/27 17:44:29 Great. Makes it much simpler. Done.
61 IDR_SCREENSHOT_NOTIFICATION_ICON);
62 notification_title_success = l10n_util::GetStringUTF16(
63 IDS_ASH_SCREENSHOT_NOTIFICATION_TITLE_SUCCESS);
64 notification_title_fail = l10n_util::GetStringUTF16(
65 IDS_ASH_SCREENSHOT_NOTIFICATION_TITLE_FAIL);
66 notification_text_success = l10n_util::GetStringUTF16(
67 IDS_ASH_SCREENSHOT_NOTIFICATION_TEXT_SUCCESS);
68 notification_text_fail = l10n_util::GetStringUTF16(
69 IDS_ASH_SCREENSHOT_NOTIFICATION_TEXT_FAIL);
70 }
71 static int notification_id;
72 static gfx::Image notification_icon;
73 static string16 notification_title_success;
74 static string16 notification_title_fail;
75 static string16 notification_text_success;
76 static string16 notification_text_fail;
77 };
78 int NotificationData::notification_id = 0;
79 gfx::Image NotificationData::notification_icon;
80 string16 NotificationData::notification_title_success;
81 string16 NotificationData::notification_title_fail;
82 string16 NotificationData::notification_text_success;
83 string16 NotificationData::notification_text_fail;
84
85 class ScreenshotTakerNotificationDelegate : public NotificationDelegate {
86 public:
87 ScreenshotTakerNotificationDelegate(const std::string& id_text,
88 bool success,
89 const base::FilePath& screenshot_path)
90 : id_text_(id_text),
91 success_(success),
92 screenshot_path_(screenshot_path) {
93 }
94
95 virtual void Display() OVERRIDE {}
96
97 virtual void Error() OVERRIDE {}
98
99 virtual void Close(bool by_user) OVERRIDE {}
100
101 virtual void Click() OVERRIDE {
102 if (success_)
103 file_manager_util::ShowFileInFolder(screenshot_path_);
104 }
105
106 virtual std::string id() const OVERRIDE { return id_text_; }
107
108 virtual content::RenderViewHost* GetRenderViewHost() const OVERRIDE {
109 return NULL;
110 }
111
112 private:
113 virtual ~ScreenshotTakerNotificationDelegate() {}
114
115 std::string id_text_;
116 bool success_;
117 base::FilePath screenshot_path_;
118
119 DISALLOW_COPY_AND_ASSIGN(ScreenshotTakerNotificationDelegate);
120 };
121
122 void ShowNotification(bool success, const base::FilePath& screenshot_path) {
James Cook 2013/03/27 16:49:17 If you pass notification_id in here and look up th
sschmitz 2013/03/27 17:44:29 Decided to use static int id inside ShowNotificati
123 #if defined(OS_CHROMEOS)
124 // Notifications only on Chrome OS.
125 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
126 NotificationData::init();
127 std::string id_text =
128 base::StringPrintf("screenshot_%3.3d",
129 ++NotificationData::notification_id);
130 string16 notification_replace_id = UTF8ToUTF16(id_text);
131 Notification notification(
132 kNotificationOriginUrl,
133 NotificationData::notification_icon,
134 success ? NotificationData::notification_title_success :
135 NotificationData::notification_title_fail,
136 success ? NotificationData::notification_text_success :
137 NotificationData::notification_text_fail,
138 WebKit::WebTextDirectionDefault,
139 string16(), // display_source
140 notification_replace_id,
141 new ScreenshotTakerNotificationDelegate(id_text,
142 success,
143 screenshot_path));
144 Profile* profile = ProfileManager::GetDefaultProfileOrOffTheRecord();
145 DCHECK(profile);
146 DCHECK(g_browser_process);
147 g_browser_process->notification_ui_manager()->Add(notification, profile);
148 #endif
149 }
150
151 void PostNotification(bool success, const base::FilePath& screenshot_path) {
152 content::BrowserThread::PostTask(
153 content::BrowserThread::UI, FROM_HERE,
154 base::Bind(&ShowNotification, success, screenshot_path));
155 }
47 156
48 void SaveScreenshotInternal(const base::FilePath& screenshot_path, 157 void SaveScreenshotInternal(const base::FilePath& screenshot_path,
49 scoped_refptr<base::RefCountedBytes> png_data) { 158 scoped_refptr<base::RefCountedBytes> png_data) {
50 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); 159 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread());
51 DCHECK(!screenshot_path.empty()); 160 DCHECK(!screenshot_path.empty());
161 bool success = true;
52 if (static_cast<size_t>(file_util::WriteFile( 162 if (static_cast<size_t>(file_util::WriteFile(
53 screenshot_path, 163 screenshot_path,
54 reinterpret_cast<char*>(&(png_data->data()[0])), 164 reinterpret_cast<char*>(&(png_data->data()[0])),
55 png_data->size())) != png_data->size()) { 165 png_data->size())) != png_data->size()) {
56 LOG(ERROR) << "Failed to save to " << screenshot_path.value(); 166 LOG(ERROR) << "Failed to save to " << screenshot_path.value();
167 success = false;
57 } 168 }
169 PostNotification(success, screenshot_path);
58 } 170 }
59 171
60 void SaveScreenshot(const base::FilePath& screenshot_path, 172 void SaveScreenshot(const base::FilePath& screenshot_path,
61 scoped_refptr<base::RefCountedBytes> png_data) { 173 scoped_refptr<base::RefCountedBytes> png_data) {
62 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); 174 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread());
63 DCHECK(!screenshot_path.empty()); 175 DCHECK(!screenshot_path.empty());
64 176
65 if (!file_util::CreateDirectory(screenshot_path.DirName())) { 177 if (!file_util::CreateDirectory(screenshot_path.DirName())) {
66 LOG(ERROR) << "Failed to ensure the existence of " 178 LOG(ERROR) << "Failed to ensure the existence of "
67 << screenshot_path.DirName().value(); 179 << screenshot_path.DirName().value();
180 PostNotification(false, screenshot_path);
68 return; 181 return;
69 } 182 }
70 SaveScreenshotInternal(screenshot_path, png_data); 183 SaveScreenshotInternal(screenshot_path, png_data);
71 } 184 }
72 185
73 // TODO(kinaba): crbug.com/140425, remove this ungly #ifdef dispatch. 186 // TODO(kinaba): crbug.com/140425, remove this ungly #ifdef dispatch.
74 #if defined(OS_CHROMEOS) 187 #if defined(OS_CHROMEOS)
75 void SaveScreenshotToDrive(scoped_refptr<base::RefCountedBytes> png_data, 188 void SaveScreenshotToDrive(scoped_refptr<base::RefCountedBytes> png_data,
76 drive::DriveFileError error, 189 drive::DriveFileError error,
77 const base::FilePath& local_path) { 190 const base::FilePath& local_path) {
78 if (error != drive::DRIVE_FILE_OK) { 191 if (error != drive::DRIVE_FILE_OK) {
79 LOG(ERROR) << "Failed to write screenshot image to Google Drive: " << error; 192 LOG(ERROR) << "Failed to write screenshot image to Google Drive: " << error;
193 PostNotification(false, local_path);
80 return; 194 return;
81 } 195 }
82 SaveScreenshotInternal(local_path, png_data); 196 SaveScreenshotInternal(local_path, png_data);
83 } 197 }
84 198
85 void EnsureDirectoryExistsCallback( 199 void EnsureDirectoryExistsCallback(
86 Profile* profile, 200 Profile* profile,
87 const base::FilePath& screenshot_path, 201 const base::FilePath& screenshot_path,
88 scoped_refptr<base::RefCountedBytes> png_data, 202 scoped_refptr<base::RefCountedBytes> png_data,
89 drive::DriveFileError error) { 203 drive::DriveFileError error) {
90 // It is okay to fail with DRIVE_FILE_ERROR_EXISTS since anyway the directory 204 // It is okay to fail with DRIVE_FILE_ERROR_EXISTS since anyway the directory
91 // of the target file exists. 205 // of the target file exists.
92 if (error == drive::DRIVE_FILE_OK || 206 if (error == drive::DRIVE_FILE_OK ||
93 error == drive::DRIVE_FILE_ERROR_EXISTS) { 207 error == drive::DRIVE_FILE_ERROR_EXISTS) {
208 // Note: The last two arguments of SaveScreenshotToDrive are appended by
209 // PrepareWritableFileAndRun.
94 drive::util::PrepareWritableFileAndRun( 210 drive::util::PrepareWritableFileAndRun(
95 profile, 211 profile,
96 screenshot_path, 212 screenshot_path,
97 base::Bind(&SaveScreenshotToDrive, png_data)); 213 base::Bind(&SaveScreenshotToDrive, png_data));
98 } else { 214 } else {
99 LOG(ERROR) << "Failed to ensure the existence of the specified directory " 215 LOG(ERROR) << "Failed to ensure the existence of the specified directory "
100 << "in Google Drive: " << error; 216 << "in Google Drive: " << error;
217 PostNotification(false, screenshot_path);
101 } 218 }
102 } 219 }
103 220
104 void PostSaveScreenshotTask(const base::FilePath& screenshot_path, 221 void PostSaveScreenshotTask(const base::FilePath& screenshot_path,
105 scoped_refptr<base::RefCountedBytes> png_data) { 222 scoped_refptr<base::RefCountedBytes> png_data) {
106 if (drive::util::IsUnderDriveMountPoint(screenshot_path)) { 223 if (drive::util::IsUnderDriveMountPoint(screenshot_path)) {
107 Profile* profile = ProfileManager::GetDefaultProfileOrOffTheRecord(); 224 Profile* profile = ProfileManager::GetDefaultProfileOrOffTheRecord();
108 if (profile) { 225 if (profile) {
226 // Note: The last argument of EnsureDirectoryExistsCallback is
227 // appended by EnsureDirectoryExists.
109 drive::util::EnsureDirectoryExists( 228 drive::util::EnsureDirectoryExists(
110 profile, 229 profile,
111 screenshot_path.DirName(), 230 screenshot_path.DirName(),
112 base::Bind(&EnsureDirectoryExistsCallback, 231 base::Bind(&EnsureDirectoryExistsCallback,
113 profile, 232 profile,
114 screenshot_path, 233 screenshot_path,
115 png_data)); 234 png_data));
116 } 235 }
117 } else { 236 } else {
118 content::BrowserThread::GetBlockingPool()->PostTask( 237 content::BrowserThread::GetBlockingPool()->PostTask(
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
169 root_windows.begin(), root_windows.end(), primary_root)); 288 root_windows.begin(), root_windows.end(), primary_root));
170 root_windows.insert(root_windows.begin(), primary_root); 289 root_windows.insert(root_windows.begin(), primary_root);
171 } 290 }
172 for (size_t i = 0; i < root_windows.size(); ++i) { 291 for (size_t i = 0; i < root_windows.size(); ++i) {
173 aura::RootWindow* root_window = root_windows[i]; 292 aura::RootWindow* root_window = root_windows[i];
174 scoped_refptr<base::RefCountedBytes> png_data(new base::RefCountedBytes); 293 scoped_refptr<base::RefCountedBytes> png_data(new base::RefCountedBytes);
175 std::string basename = screenshot_basename; 294 std::string basename = screenshot_basename;
176 gfx::Rect rect = root_window->bounds(); 295 gfx::Rect rect = root_window->bounds();
177 if (root_windows.size() > 1) 296 if (root_windows.size() > 1)
178 basename += base::StringPrintf(" - Display %d", static_cast<int>(i + 1)); 297 basename += base::StringPrintf(" - Display %d", static_cast<int>(i + 1));
298 base::FilePath screenshot_path =
299 screenshot_directory.AppendASCII(basename + ".png");
179 if (GrabWindowSnapshot(root_window, rect, &png_data->data())) { 300 if (GrabWindowSnapshot(root_window, rect, &png_data->data())) {
180 DisplayVisualFeedback(rect); 301 PostSaveScreenshotTask(screenshot_path, png_data);
181 PostSaveScreenshotTask(
182 screenshot_directory.AppendASCII(basename + ".png"), png_data);
183 } else { 302 } else {
184 LOG(ERROR) << "Failed to grab the window screenshot for " << i; 303 LOG(ERROR) << "Failed to grab the window screenshot for " << i;
304 ShowNotification(false, screenshot_path);
185 } 305 }
186 } 306 }
187 last_screenshot_timestamp_ = base::Time::Now(); 307 last_screenshot_timestamp_ = base::Time::Now();
188 } 308 }
189 309
190 void ScreenshotTaker::HandleTakePartialScreenshot( 310 void ScreenshotTaker::HandleTakePartialScreenshot(
191 aura::Window* window, const gfx::Rect& rect) { 311 aura::Window* window, const gfx::Rect& rect) {
192 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); 312 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
193 313
194 base::FilePath screenshot_directory; 314 base::FilePath screenshot_directory;
195 if (!ScreenshotSource::GetScreenshotDirectory(&screenshot_directory)) 315 if (!ScreenshotSource::GetScreenshotDirectory(&screenshot_directory))
196 return; 316 return;
197 317
198 scoped_refptr<base::RefCountedBytes> png_data(new base::RefCountedBytes); 318 scoped_refptr<base::RefCountedBytes> png_data(new base::RefCountedBytes);
199 319
320 base::FilePath screenshot_path =
321 screenshot_directory.AppendASCII(
322 ScreenshotSource::GetScreenshotBaseFilename() + ".png");
200 if (GrabWindowSnapshot(window, rect, &png_data->data())) { 323 if (GrabWindowSnapshot(window, rect, &png_data->data())) {
201 last_screenshot_timestamp_ = base::Time::Now(); 324 last_screenshot_timestamp_ = base::Time::Now();
202 DisplayVisualFeedback(rect); 325 PostSaveScreenshotTask(screenshot_path, png_data);
203 PostSaveScreenshotTask(
204 screenshot_directory.AppendASCII(
205 ScreenshotSource::GetScreenshotBaseFilename() + ".png"),
206 png_data);
207 } else { 326 } else {
208 LOG(ERROR) << "Failed to grab the window screenshot"; 327 LOG(ERROR) << "Failed to grab the window screenshot";
328 ShowNotification(false, screenshot_path);
209 } 329 }
210 } 330 }
211 331
212 bool ScreenshotTaker::CanTakeScreenshot() { 332 bool ScreenshotTaker::CanTakeScreenshot() {
213 return last_screenshot_timestamp_.is_null() || 333 return last_screenshot_timestamp_.is_null() ||
214 base::Time::Now() - last_screenshot_timestamp_ > 334 base::Time::Now() - last_screenshot_timestamp_ >
215 base::TimeDelta::FromMilliseconds( 335 base::TimeDelta::FromMilliseconds(
216 kScreenshotMinimumIntervalInMS); 336 kScreenshotMinimumIntervalInMS);
217 } 337 }
218
219 void ScreenshotTaker::CloseVisualFeedbackLayer() {
220 visual_feedback_layer_.reset();
221 }
222
223 void ScreenshotTaker::DisplayVisualFeedback(const gfx::Rect& rect) {
224 visual_feedback_layer_.reset(new ui::Layer(ui::LAYER_SOLID_COLOR));
225 visual_feedback_layer_->SetColor(SK_ColorWHITE);
226 visual_feedback_layer_->SetOpacity(kVisualFeedbackLayerOpacity);
227 visual_feedback_layer_->SetBounds(rect);
228
229 ui::Layer* parent = ash::Shell::GetContainer(
230 ash::Shell::GetActiveRootWindow(),
231 ash::internal::kShellWindowId_OverlayContainer)->layer();
232 parent->Add(visual_feedback_layer_.get());
233 visual_feedback_layer_->SetVisible(true);
234
235 MessageLoopForUI::current()->PostDelayedTask(
236 FROM_HERE,
237 base::Bind(&ScreenshotTaker::CloseVisualFeedbackLayer,
238 base::Unretained(this)),
239 base::TimeDelta::FromMilliseconds(kVisualFeedbackLayerDisplayTimeMs));
240 }
OLDNEW
« no previous file with comments | « chrome/browser/ui/ash/screenshot_taker.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698