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

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: unittest 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
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 const char kNotificationOriginUrl[] = "chrome://screenshot";
51
52 class ScreenshotTakerNotificationDelegate : public NotificationDelegate {
53 public:
54 ScreenshotTakerNotificationDelegate(const std::string& id_text,
55 bool success,
56 const base::FilePath& screenshot_path)
57 : id_text_(id_text),
58 success_(success),
59 screenshot_path_(screenshot_path) {
60 }
61
62 virtual void Display() OVERRIDE {}
63
64 virtual void Error() OVERRIDE {}
65
66 virtual void Close(bool by_user) OVERRIDE {}
67
68 virtual void Click() OVERRIDE {
69 if (success_)
70 file_manager_util::ShowFileInFolder(screenshot_path_);
71 }
72
73 virtual std::string id() const OVERRIDE { return id_text_; }
74
75 virtual content::RenderViewHost* GetRenderViewHost() const OVERRIDE {
76 return NULL;
77 }
78
79 private:
80 virtual ~ScreenshotTakerNotificationDelegate() {}
81
82 std::string id_text_;
83 bool success_;
84 base::FilePath screenshot_path_;
85
86 DISALLOW_COPY_AND_ASSIGN(ScreenshotTakerNotificationDelegate);
87 };
88
89 void ShowNotification(bool success, const base::FilePath& screenshot_path) {
90 #if defined(OS_CHROMEOS)
91 // Notifications only on Chrome OS.
92 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
93 static int id = 0;
94 std::string id_text = base::StringPrintf("screenshot_%3.3d", ++id);
95 string16 replace_id = UTF8ToUTF16(id_text);
96 Notification notification(
97 GURL(kNotificationOriginUrl),
98 ui::ResourceBundle::GetSharedInstance().GetImageNamed(
99 IDR_SCREENSHOT_NOTIFICATION_ICON),
100 l10n_util::GetStringUTF16(
101 success ?
102 IDS_ASH_SCREENSHOT_NOTIFICATION_TITLE_SUCCESS :
103 IDS_ASH_SCREENSHOT_NOTIFICATION_TITLE_FAIL),
104 l10n_util::GetStringUTF16(
105 success ?
106 IDS_ASH_SCREENSHOT_NOTIFICATION_TEXT_SUCCESS :
107 IDS_ASH_SCREENSHOT_NOTIFICATION_TEXT_FAIL),
108 WebKit::WebTextDirectionDefault,
109 string16(),
110 replace_id,
111 new ScreenshotTakerNotificationDelegate(id_text,
112 success,
113 screenshot_path));
114 Profile* profile = ProfileManager::GetDefaultProfileOrOffTheRecord();
115 DCHECK(profile);
116 g_browser_process->notification_ui_manager()->Add(notification, profile);
117 #endif
118 }
119
120 void PostNotification(bool success, const base::FilePath& screenshot_path) {
121 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread());
122 content::BrowserThread::PostTask(
123 content::BrowserThread::UI, FROM_HERE,
124 base::Bind(&ShowNotification, success, screenshot_path));
125 }
47 126
48 void SaveScreenshotInternal(const base::FilePath& screenshot_path, 127 void SaveScreenshotInternal(const base::FilePath& screenshot_path,
49 scoped_refptr<base::RefCountedBytes> png_data) { 128 scoped_refptr<base::RefCountedBytes> png_data) {
50 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); 129 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread());
51 DCHECK(!screenshot_path.empty()); 130 DCHECK(!screenshot_path.empty());
131 bool success = true;
52 if (static_cast<size_t>(file_util::WriteFile( 132 if (static_cast<size_t>(file_util::WriteFile(
53 screenshot_path, 133 screenshot_path,
54 reinterpret_cast<char*>(&(png_data->data()[0])), 134 reinterpret_cast<char*>(&(png_data->data()[0])),
55 png_data->size())) != png_data->size()) { 135 png_data->size())) != png_data->size()) {
56 LOG(ERROR) << "Failed to save to " << screenshot_path.value(); 136 LOG(ERROR) << "Failed to save to " << screenshot_path.value();
137 success = false;
57 } 138 }
139 PostNotification(success, screenshot_path);
58 } 140 }
59 141
60 void SaveScreenshot(const base::FilePath& screenshot_path, 142 void SaveScreenshot(const base::FilePath& screenshot_path,
61 scoped_refptr<base::RefCountedBytes> png_data) { 143 scoped_refptr<base::RefCountedBytes> png_data) {
62 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); 144 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread());
63 DCHECK(!screenshot_path.empty()); 145 DCHECK(!screenshot_path.empty());
64 146
65 if (!file_util::CreateDirectory(screenshot_path.DirName())) { 147 if (!file_util::CreateDirectory(screenshot_path.DirName())) {
66 LOG(ERROR) << "Failed to ensure the existence of " 148 LOG(ERROR) << "Failed to ensure the existence of "
67 << screenshot_path.DirName().value(); 149 << screenshot_path.DirName().value();
150 PostNotification(false, screenshot_path);
68 return; 151 return;
69 } 152 }
70 SaveScreenshotInternal(screenshot_path, png_data); 153 SaveScreenshotInternal(screenshot_path, png_data);
71 } 154 }
72 155
73 // TODO(kinaba): crbug.com/140425, remove this ungly #ifdef dispatch. 156 // TODO(kinaba): crbug.com/140425, remove this ungly #ifdef dispatch.
74 #if defined(OS_CHROMEOS) 157 #if defined(OS_CHROMEOS)
75 void SaveScreenshotToDrive(scoped_refptr<base::RefCountedBytes> png_data, 158 void SaveScreenshotToDrive(scoped_refptr<base::RefCountedBytes> png_data,
76 drive::DriveFileError error, 159 drive::DriveFileError error,
77 const base::FilePath& local_path) { 160 const base::FilePath& local_path) {
78 if (error != drive::DRIVE_FILE_OK) { 161 if (error != drive::DRIVE_FILE_OK) {
79 LOG(ERROR) << "Failed to write screenshot image to Google Drive: " << error; 162 LOG(ERROR) << "Failed to write screenshot image to Google Drive: " << error;
163 PostNotification(false, local_path);
80 return; 164 return;
81 } 165 }
82 SaveScreenshotInternal(local_path, png_data); 166 SaveScreenshotInternal(local_path, png_data);
83 } 167 }
84 168
85 void EnsureDirectoryExistsCallback( 169 void EnsureDirectoryExistsCallback(
86 Profile* profile, 170 Profile* profile,
87 const base::FilePath& screenshot_path, 171 const base::FilePath& screenshot_path,
88 scoped_refptr<base::RefCountedBytes> png_data, 172 scoped_refptr<base::RefCountedBytes> png_data,
89 drive::DriveFileError error) { 173 drive::DriveFileError error) {
90 // It is okay to fail with DRIVE_FILE_ERROR_EXISTS since anyway the directory 174 // It is okay to fail with DRIVE_FILE_ERROR_EXISTS since anyway the directory
91 // of the target file exists. 175 // of the target file exists.
92 if (error == drive::DRIVE_FILE_OK || 176 if (error == drive::DRIVE_FILE_OK ||
93 error == drive::DRIVE_FILE_ERROR_EXISTS) { 177 error == drive::DRIVE_FILE_ERROR_EXISTS) {
178 // Note: The last two arguments of SaveScreenshotToDrive are appended by
179 // PrepareWritableFileAndRun.
94 drive::util::PrepareWritableFileAndRun( 180 drive::util::PrepareWritableFileAndRun(
95 profile, 181 profile,
96 screenshot_path, 182 screenshot_path,
97 base::Bind(&SaveScreenshotToDrive, png_data)); 183 base::Bind(&SaveScreenshotToDrive, png_data));
98 } else { 184 } else {
99 LOG(ERROR) << "Failed to ensure the existence of the specified directory " 185 LOG(ERROR) << "Failed to ensure the existence of the specified directory "
100 << "in Google Drive: " << error; 186 << "in Google Drive: " << error;
187 ShowNotification(false, screenshot_path);
101 } 188 }
102 } 189 }
103 190
104 void PostSaveScreenshotTask(const base::FilePath& screenshot_path, 191 void PostSaveScreenshotTask(const base::FilePath& screenshot_path,
105 scoped_refptr<base::RefCountedBytes> png_data) { 192 scoped_refptr<base::RefCountedBytes> png_data) {
106 if (drive::util::IsUnderDriveMountPoint(screenshot_path)) { 193 if (drive::util::IsUnderDriveMountPoint(screenshot_path)) {
107 Profile* profile = ProfileManager::GetDefaultProfileOrOffTheRecord(); 194 Profile* profile = ProfileManager::GetDefaultProfileOrOffTheRecord();
108 if (profile) { 195 if (profile) {
196 // Note: The last argument of EnsureDirectoryExistsCallback is
197 // appended by EnsureDirectoryExists.
109 drive::util::EnsureDirectoryExists( 198 drive::util::EnsureDirectoryExists(
110 profile, 199 profile,
111 screenshot_path.DirName(), 200 screenshot_path.DirName(),
112 base::Bind(&EnsureDirectoryExistsCallback, 201 base::Bind(&EnsureDirectoryExistsCallback,
113 profile, 202 profile,
114 screenshot_path, 203 screenshot_path,
115 png_data)); 204 png_data));
116 } 205 }
117 } else { 206 } else {
118 content::BrowserThread::GetBlockingPool()->PostTask( 207 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)); 258 root_windows.begin(), root_windows.end(), primary_root));
170 root_windows.insert(root_windows.begin(), primary_root); 259 root_windows.insert(root_windows.begin(), primary_root);
171 } 260 }
172 for (size_t i = 0; i < root_windows.size(); ++i) { 261 for (size_t i = 0; i < root_windows.size(); ++i) {
173 aura::RootWindow* root_window = root_windows[i]; 262 aura::RootWindow* root_window = root_windows[i];
174 scoped_refptr<base::RefCountedBytes> png_data(new base::RefCountedBytes); 263 scoped_refptr<base::RefCountedBytes> png_data(new base::RefCountedBytes);
175 std::string basename = screenshot_basename; 264 std::string basename = screenshot_basename;
176 gfx::Rect rect = root_window->bounds(); 265 gfx::Rect rect = root_window->bounds();
177 if (root_windows.size() > 1) 266 if (root_windows.size() > 1)
178 basename += base::StringPrintf(" - Display %d", static_cast<int>(i + 1)); 267 basename += base::StringPrintf(" - Display %d", static_cast<int>(i + 1));
268 base::FilePath screenshot_path =
269 screenshot_directory.AppendASCII(basename + ".png");
179 if (GrabWindowSnapshot(root_window, rect, &png_data->data())) { 270 if (GrabWindowSnapshot(root_window, rect, &png_data->data())) {
180 DisplayVisualFeedback(rect); 271 PostSaveScreenshotTask(screenshot_path, png_data);
181 PostSaveScreenshotTask(
182 screenshot_directory.AppendASCII(basename + ".png"), png_data);
183 } else { 272 } else {
184 LOG(ERROR) << "Failed to grab the window screenshot for " << i; 273 LOG(ERROR) << "Failed to grab the window screenshot for " << i;
274 ShowNotification(false, screenshot_path);
James Cook 2013/03/28 19:49:36 Isn't this called on the UI thread? If so, the DC
sschmitz 2013/03/28 23:52:41 Yes it is on the UI thread and No it works. I test
185 } 275 }
186 } 276 }
187 last_screenshot_timestamp_ = base::Time::Now(); 277 last_screenshot_timestamp_ = base::Time::Now();
188 } 278 }
189 279
190 void ScreenshotTaker::HandleTakePartialScreenshot( 280 void ScreenshotTaker::HandleTakePartialScreenshot(
191 aura::Window* window, const gfx::Rect& rect) { 281 aura::Window* window, const gfx::Rect& rect) {
192 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); 282 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
193 283
194 base::FilePath screenshot_directory; 284 base::FilePath screenshot_directory;
195 if (!ScreenshotSource::GetScreenshotDirectory(&screenshot_directory)) 285 if (!ScreenshotSource::GetScreenshotDirectory(&screenshot_directory))
196 return; 286 return;
197 287
198 scoped_refptr<base::RefCountedBytes> png_data(new base::RefCountedBytes); 288 scoped_refptr<base::RefCountedBytes> png_data(new base::RefCountedBytes);
199 289
290 base::FilePath screenshot_path =
291 screenshot_directory.AppendASCII(
292 ScreenshotSource::GetScreenshotBaseFilename() + ".png");
200 if (GrabWindowSnapshot(window, rect, &png_data->data())) { 293 if (GrabWindowSnapshot(window, rect, &png_data->data())) {
201 last_screenshot_timestamp_ = base::Time::Now(); 294 last_screenshot_timestamp_ = base::Time::Now();
202 DisplayVisualFeedback(rect); 295 PostSaveScreenshotTask(screenshot_path, png_data);
203 PostSaveScreenshotTask(
204 screenshot_directory.AppendASCII(
205 ScreenshotSource::GetScreenshotBaseFilename() + ".png"),
206 png_data);
207 } else { 296 } else {
208 LOG(ERROR) << "Failed to grab the window screenshot"; 297 LOG(ERROR) << "Failed to grab the window screenshot";
298 ShowNotification(false, screenshot_path);
James Cook 2013/03/28 19:49:36 ditto, this is on the UI thread
sschmitz 2013/03/28 23:52:41 same as above.
209 } 299 }
210 } 300 }
211 301
212 bool ScreenshotTaker::CanTakeScreenshot() { 302 bool ScreenshotTaker::CanTakeScreenshot() {
213 return last_screenshot_timestamp_.is_null() || 303 return last_screenshot_timestamp_.is_null() ||
214 base::Time::Now() - last_screenshot_timestamp_ > 304 base::Time::Now() - last_screenshot_timestamp_ >
215 base::TimeDelta::FromMilliseconds( 305 base::TimeDelta::FromMilliseconds(
216 kScreenshotMinimumIntervalInMS); 306 kScreenshotMinimumIntervalInMS);
217 } 307 }
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

Powered by Google App Engine
This is Rietveld 408576698