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

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: observer for test 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(Screenshot::Result screenshot_result,
90 const base::FilePath& screenshot_path) {
91 #if defined(OS_CHROMEOS)
92 // Notifications only on Chrome OS.
93 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
94 static int id = 0;
95 std::string id_text = base::StringPrintf("screenshot_%3.3d", ++id);
96 string16 replace_id = UTF8ToUTF16(id_text);
97 bool success = (screenshot_result == Screenshot::SCREENSHOT_SUCCESS);
98 Notification notification(
99 GURL(kNotificationOriginUrl),
100 ui::ResourceBundle::GetSharedInstance().GetImageNamed(
101 IDR_SCREENSHOT_NOTIFICATION_ICON),
102 l10n_util::GetStringUTF16(
103 success ?
104 IDS_ASH_SCREENSHOT_NOTIFICATION_TITLE_SUCCESS :
105 IDS_ASH_SCREENSHOT_NOTIFICATION_TITLE_FAIL),
106 l10n_util::GetStringUTF16(
107 success ?
108 IDS_ASH_SCREENSHOT_NOTIFICATION_TEXT_SUCCESS :
109 IDS_ASH_SCREENSHOT_NOTIFICATION_TEXT_FAIL),
110 WebKit::WebTextDirectionDefault,
111 string16(),
112 replace_id,
113 new ScreenshotTakerNotificationDelegate(id_text,
114 success,
115 screenshot_path));
116 Profile* profile = ProfileManager::GetDefaultProfileOrOffTheRecord();
117 DCHECK(profile);
118 g_browser_process->notification_ui_manager()->Add(notification, profile);
119
120 if (ScreenshotTaker::GetObserverForTest()) {
121 ScreenshotTaker::GetObserverForTest()->OnScreenshotCompleted(
122 screenshot_result, screenshot_path);
123 }
124 #endif
125 }
126
127 void PostShowNotification(Screenshot::Result screenshot_result,
128 const base::FilePath& screenshot_path) {
129 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread());
130 content::BrowserThread::PostTask(
131 content::BrowserThread::UI, FROM_HERE,
132 base::Bind(&ShowNotification, screenshot_result, screenshot_path));
133 }
47 134
48 void SaveScreenshotInternal(const base::FilePath& screenshot_path, 135 void SaveScreenshotInternal(const base::FilePath& screenshot_path,
49 scoped_refptr<base::RefCountedBytes> png_data) { 136 scoped_refptr<base::RefCountedBytes> png_data) {
50 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); 137 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread());
51 DCHECK(!screenshot_path.empty()); 138 DCHECK(!screenshot_path.empty());
52 if (static_cast<size_t>(file_util::WriteFile( 139 if (static_cast<size_t>(file_util::WriteFile(
53 screenshot_path, 140 screenshot_path,
54 reinterpret_cast<char*>(&(png_data->data()[0])), 141 reinterpret_cast<char*>(&(png_data->data()[0])),
55 png_data->size())) != png_data->size()) { 142 png_data->size())) != png_data->size()) {
56 LOG(ERROR) << "Failed to save to " << screenshot_path.value(); 143 LOG(ERROR) << "Failed to save to " << screenshot_path.value();
144 PostShowNotification(Screenshot::SCREENSHOT_WRITE_FILE_FAILED,
145 screenshot_path);
146 return;
57 } 147 }
148 PostShowNotification(Screenshot::SCREENSHOT_SUCCESS, screenshot_path);
58 } 149 }
59 150
60 void SaveScreenshot(const base::FilePath& screenshot_path, 151 void SaveScreenshot(const base::FilePath& screenshot_path,
61 scoped_refptr<base::RefCountedBytes> png_data) { 152 scoped_refptr<base::RefCountedBytes> png_data) {
62 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); 153 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread());
63 DCHECK(!screenshot_path.empty()); 154 DCHECK(!screenshot_path.empty());
64 155
65 if (!file_util::CreateDirectory(screenshot_path.DirName())) { 156 if (!file_util::CreateDirectory(screenshot_path.DirName())) {
66 LOG(ERROR) << "Failed to ensure the existence of " 157 LOG(ERROR) << "Failed to ensure the existence of "
67 << screenshot_path.DirName().value(); 158 << screenshot_path.DirName().value();
159 PostShowNotification(Screenshot::SCREENSHOT_CREATE_DIR_FAILED,
160 screenshot_path);
68 return; 161 return;
69 } 162 }
70 SaveScreenshotInternal(screenshot_path, png_data); 163 SaveScreenshotInternal(screenshot_path, png_data);
71 } 164 }
72 165
73 // TODO(kinaba): crbug.com/140425, remove this ungly #ifdef dispatch. 166 // TODO(kinaba): crbug.com/140425, remove this ungly #ifdef dispatch.
74 #if defined(OS_CHROMEOS) 167 #if defined(OS_CHROMEOS)
75 void SaveScreenshotToDrive(scoped_refptr<base::RefCountedBytes> png_data, 168 void SaveScreenshotToDrive(scoped_refptr<base::RefCountedBytes> png_data,
76 drive::DriveFileError error, 169 drive::DriveFileError error,
77 const base::FilePath& local_path) { 170 const base::FilePath& local_path) {
78 if (error != drive::DRIVE_FILE_OK) { 171 if (error != drive::DRIVE_FILE_OK) {
79 LOG(ERROR) << "Failed to write screenshot image to Google Drive: " << error; 172 LOG(ERROR) << "Failed to write screenshot image to Google Drive: " << error;
173 PostShowNotification(Screenshot::SCREENSHOT_CREATE_FILE_FAILED, local_path);
80 return; 174 return;
81 } 175 }
82 SaveScreenshotInternal(local_path, png_data); 176 SaveScreenshotInternal(local_path, png_data);
83 } 177 }
84 178
85 void EnsureDirectoryExistsCallback( 179 void EnsureDirectoryExistsCallback(
86 Profile* profile, 180 Profile* profile,
87 const base::FilePath& screenshot_path, 181 const base::FilePath& screenshot_path,
88 scoped_refptr<base::RefCountedBytes> png_data, 182 scoped_refptr<base::RefCountedBytes> png_data,
89 drive::DriveFileError error) { 183 drive::DriveFileError error) {
90 // It is okay to fail with DRIVE_FILE_ERROR_EXISTS since anyway the directory 184 // It is okay to fail with DRIVE_FILE_ERROR_EXISTS since anyway the directory
91 // of the target file exists. 185 // of the target file exists.
92 if (error == drive::DRIVE_FILE_OK || 186 if (error == drive::DRIVE_FILE_OK ||
93 error == drive::DRIVE_FILE_ERROR_EXISTS) { 187 error == drive::DRIVE_FILE_ERROR_EXISTS) {
188 // Note: The last two arguments of SaveScreenshotToDrive are appended by
189 // PrepareWritableFileAndRun.
94 drive::util::PrepareWritableFileAndRun( 190 drive::util::PrepareWritableFileAndRun(
95 profile, 191 profile,
96 screenshot_path, 192 screenshot_path,
97 base::Bind(&SaveScreenshotToDrive, png_data)); 193 base::Bind(&SaveScreenshotToDrive, png_data));
98 } else { 194 } else {
99 LOG(ERROR) << "Failed to ensure the existence of the specified directory " 195 LOG(ERROR) << "Failed to ensure the existence of the specified directory "
100 << "in Google Drive: " << error; 196 << "in Google Drive: " << error;
197 ShowNotification(Screenshot::SCREENSHOT_CHECK_DIR_FAILED, screenshot_path);
101 } 198 }
102 } 199 }
103 200
104 void PostSaveScreenshotTask(const base::FilePath& screenshot_path, 201 void PostSaveScreenshotTask(const base::FilePath& screenshot_path,
105 scoped_refptr<base::RefCountedBytes> png_data) { 202 scoped_refptr<base::RefCountedBytes> png_data) {
106 if (drive::util::IsUnderDriveMountPoint(screenshot_path)) { 203 if (drive::util::IsUnderDriveMountPoint(screenshot_path)) {
107 Profile* profile = ProfileManager::GetDefaultProfileOrOffTheRecord(); 204 Profile* profile = ProfileManager::GetDefaultProfileOrOffTheRecord();
108 if (profile) { 205 if (profile) {
206 // Note: The last argument of EnsureDirectoryExistsCallback is
207 // appended by EnsureDirectoryExists.
109 drive::util::EnsureDirectoryExists( 208 drive::util::EnsureDirectoryExists(
110 profile, 209 profile,
111 screenshot_path.DirName(), 210 screenshot_path.DirName(),
112 base::Bind(&EnsureDirectoryExistsCallback, 211 base::Bind(&EnsureDirectoryExistsCallback,
113 profile, 212 profile,
114 screenshot_path, 213 screenshot_path,
115 png_data)); 214 png_data));
116 } 215 }
117 } else { 216 } else {
118 content::BrowserThread::GetBlockingPool()->PostTask( 217 content::BrowserThread::GetBlockingPool()->PostTask(
(...skipping 22 matching lines...) Expand all
141 // TODO(mukai): remove this when the performance issue has been fixed. 240 // TODO(mukai): remove this when the performance issue has been fixed.
142 if (window->GetRootWindow()->GrabSnapshot(snapshot_bounds, png_data)) 241 if (window->GetRootWindow()->GrabSnapshot(snapshot_bounds, png_data))
143 return true; 242 return true;
144 #endif // OS_LINUX 243 #endif // OS_LINUX
145 244
146 return chrome::GrabWindowSnapshotForUser(window, png_data, snapshot_bounds); 245 return chrome::GrabWindowSnapshotForUser(window, png_data, snapshot_bounds);
147 } 246 }
148 247
149 } // namespace 248 } // namespace
150 249
250 ScreenshotTakerObserver* ScreenshotTaker::s_observer_for_test = 0;
James Cook 2013/03/29 20:00:32 I think we prefer NULL for null pointers.
sschmitz 2013/03/29 21:02:35 Done.
251
151 ScreenshotTaker::ScreenshotTaker() { 252 ScreenshotTaker::ScreenshotTaker() {
152 } 253 }
153 254
154 ScreenshotTaker::~ScreenshotTaker() { 255 ScreenshotTaker::~ScreenshotTaker() {
155 } 256 }
156 257
157 void ScreenshotTaker::HandleTakeScreenshotForAllRootWindows() { 258 void ScreenshotTaker::HandleTakeScreenshotForAllRootWindows() {
158 base::FilePath screenshot_directory; 259 base::FilePath screenshot_directory;
159 if (!ScreenshotSource::GetScreenshotDirectory(&screenshot_directory)) 260 if (screenshot_directory_for_test_.get()) {
261 screenshot_directory = *screenshot_directory_for_test_;
262 } else if (!ScreenshotSource::GetScreenshotDirectory(&screenshot_directory)) {
263 ShowNotification(Screenshot::SCREENSHOT_GET_DIR_FAILED, base::FilePath());
160 return; 264 return;
265 }
161 266
162 std::string screenshot_basename = 267 std::string screenshot_basename =
163 ScreenshotSource::GetScreenshotBaseFilename(); 268 ScreenshotSource::GetScreenshotBaseFilename();
164 ash::Shell::RootWindowList root_windows = ash::Shell::GetAllRootWindows(); 269 ash::Shell::RootWindowList root_windows = ash::Shell::GetAllRootWindows();
165 // Reorder root_windows to take the primary root window's snapshot at first. 270 // Reorder root_windows to take the primary root window's snapshot at first.
166 aura::RootWindow* primary_root = ash::Shell::GetPrimaryRootWindow(); 271 aura::RootWindow* primary_root = ash::Shell::GetPrimaryRootWindow();
167 if (*(root_windows.begin()) != primary_root) { 272 if (*(root_windows.begin()) != primary_root) {
168 root_windows.erase(std::find( 273 root_windows.erase(std::find(
169 root_windows.begin(), root_windows.end(), primary_root)); 274 root_windows.begin(), root_windows.end(), primary_root));
170 root_windows.insert(root_windows.begin(), primary_root); 275 root_windows.insert(root_windows.begin(), primary_root);
171 } 276 }
172 for (size_t i = 0; i < root_windows.size(); ++i) { 277 for (size_t i = 0; i < root_windows.size(); ++i) {
173 aura::RootWindow* root_window = root_windows[i]; 278 aura::RootWindow* root_window = root_windows[i];
174 scoped_refptr<base::RefCountedBytes> png_data(new base::RefCountedBytes); 279 scoped_refptr<base::RefCountedBytes> png_data(new base::RefCountedBytes);
175 std::string basename = screenshot_basename; 280 std::string basename = screenshot_basename;
176 gfx::Rect rect = root_window->bounds(); 281 gfx::Rect rect = root_window->bounds();
177 if (root_windows.size() > 1) 282 if (root_windows.size() > 1)
178 basename += base::StringPrintf(" - Display %d", static_cast<int>(i + 1)); 283 basename += base::StringPrintf(" - Display %d", static_cast<int>(i + 1));
284 base::FilePath screenshot_path =
285 screenshot_directory.AppendASCII(basename + ".png");
179 if (GrabWindowSnapshot(root_window, rect, &png_data->data())) { 286 if (GrabWindowSnapshot(root_window, rect, &png_data->data())) {
180 DisplayVisualFeedback(rect); 287 PostSaveScreenshotTask(screenshot_path, png_data);
181 PostSaveScreenshotTask(
182 screenshot_directory.AppendASCII(basename + ".png"), png_data);
183 } else { 288 } else {
184 LOG(ERROR) << "Failed to grab the window screenshot for " << i; 289 LOG(ERROR) << "Failed to grab the window screenshot for " << i;
290 ShowNotification(Screenshot::SCREENSHOT_GRABWINDOW_FULL_FAILED,
291 screenshot_path);
185 } 292 }
186 } 293 }
187 last_screenshot_timestamp_ = base::Time::Now(); 294 last_screenshot_timestamp_ = base::Time::Now();
188 } 295 }
189 296
190 void ScreenshotTaker::HandleTakePartialScreenshot( 297 void ScreenshotTaker::HandleTakePartialScreenshot(
191 aura::Window* window, const gfx::Rect& rect) { 298 aura::Window* window, const gfx::Rect& rect) {
192 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); 299 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
193 300
194 base::FilePath screenshot_directory; 301 base::FilePath screenshot_directory;
195 if (!ScreenshotSource::GetScreenshotDirectory(&screenshot_directory)) 302 if (screenshot_directory_for_test_.get()) {
303 screenshot_directory = *screenshot_directory_for_test_;
304 } else if (!ScreenshotSource::GetScreenshotDirectory(&screenshot_directory)) {
305 ShowNotification(Screenshot::SCREENSHOT_GET_DIR_FAILED, base::FilePath());
196 return; 306 return;
307 }
197 308
198 scoped_refptr<base::RefCountedBytes> png_data(new base::RefCountedBytes); 309 scoped_refptr<base::RefCountedBytes> png_data(new base::RefCountedBytes);
199 310
311 base::FilePath screenshot_path =
312 screenshot_directory.AppendASCII(
313 ScreenshotSource::GetScreenshotBaseFilename() + ".png");
200 if (GrabWindowSnapshot(window, rect, &png_data->data())) { 314 if (GrabWindowSnapshot(window, rect, &png_data->data())) {
201 last_screenshot_timestamp_ = base::Time::Now(); 315 last_screenshot_timestamp_ = base::Time::Now();
202 DisplayVisualFeedback(rect); 316 PostSaveScreenshotTask(screenshot_path, png_data);
203 PostSaveScreenshotTask(
204 screenshot_directory.AppendASCII(
205 ScreenshotSource::GetScreenshotBaseFilename() + ".png"),
206 png_data);
207 } else { 317 } else {
208 LOG(ERROR) << "Failed to grab the window screenshot"; 318 LOG(ERROR) << "Failed to grab the window screenshot";
319 ShowNotification(Screenshot::SCREENSHOT_GRABWINDOW_PARTIAL_FAILED,
320 screenshot_path);
209 } 321 }
210 } 322 }
211 323
212 bool ScreenshotTaker::CanTakeScreenshot() { 324 bool ScreenshotTaker::CanTakeScreenshot() {
213 return last_screenshot_timestamp_.is_null() || 325 return last_screenshot_timestamp_.is_null() ||
214 base::Time::Now() - last_screenshot_timestamp_ > 326 base::Time::Now() - last_screenshot_timestamp_ >
215 base::TimeDelta::FromMilliseconds( 327 base::TimeDelta::FromMilliseconds(
216 kScreenshotMinimumIntervalInMS); 328 kScreenshotMinimumIntervalInMS);
217 } 329 }
218 330
219 void ScreenshotTaker::CloseVisualFeedbackLayer() { 331 void ScreenshotTaker::SetScreenshotDirectoryForTest(
220 visual_feedback_layer_.reset(); 332 const base::FilePath& directory) {
333 screenshot_directory_for_test_.reset(new base::FilePath(directory));
221 } 334 }
222 335
223 void ScreenshotTaker::DisplayVisualFeedback(const gfx::Rect& rect) { 336 // static
224 visual_feedback_layer_.reset(new ui::Layer(ui::LAYER_SOLID_COLOR)); 337 void ScreenshotTaker::SetObserverForTest(ScreenshotTakerObserver* observer) {
225 visual_feedback_layer_->SetColor(SK_ColorWHITE); 338 s_observer_for_test = observer;
226 visual_feedback_layer_->SetOpacity(kVisualFeedbackLayerOpacity); 339 }
227 visual_feedback_layer_->SetBounds(rect);
228 340
229 ui::Layer* parent = ash::Shell::GetContainer( 341 // static
230 ash::Shell::GetActiveRootWindow(), 342 ScreenshotTakerObserver* ScreenshotTaker::GetObserverForTest() {
231 ash::internal::kShellWindowId_OverlayContainer)->layer(); 343 return s_observer_for_test;
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 } 344 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698