OLD | NEW |
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" | |
12 #include "ash/shell_window_ids.h" | |
13 #include "base/bind.h" | 11 #include "base/bind.h" |
14 #include "base/file_util.h" | 12 #include "base/file_util.h" |
15 #include "base/files/file_path.h" | |
16 #include "base/logging.h" | 13 #include "base/logging.h" |
17 #include "base/memory/ref_counted_memory.h" | 14 #include "base/memory/ref_counted_memory.h" |
18 #include "base/stringprintf.h" | 15 #include "base/stringprintf.h" |
19 #include "base/threading/sequenced_worker_pool.h" | 16 #include "base/threading/sequenced_worker_pool.h" |
20 #include "base/time.h" | 17 #include "base/time.h" |
| 18 #include "base/utf_string_conversions.h" |
| 19 #include "chrome/browser/browser_process.h" |
21 #include "chrome/browser/download/download_prefs.h" | 20 #include "chrome/browser/download/download_prefs.h" |
| 21 #include "chrome/browser/notifications/notification.h" |
| 22 #include "chrome/browser/notifications/notification_ui_manager.h" |
22 #include "chrome/browser/profiles/profile.h" | 23 #include "chrome/browser/profiles/profile.h" |
23 #include "chrome/browser/profiles/profile_manager.h" | |
24 #include "chrome/browser/ui/webui/screenshot_source.h" | 24 #include "chrome/browser/ui/webui/screenshot_source.h" |
25 #include "chrome/browser/ui/window_snapshot/window_snapshot.h" | 25 #include "chrome/browser/ui/window_snapshot/window_snapshot.h" |
26 #include "content/public/browser/browser_thread.h" | 26 #include "content/public/browser/browser_thread.h" |
| 27 #include "grit/ash_strings.h" |
| 28 #include "grit/theme_resources.h" |
27 #include "ui/aura/root_window.h" | 29 #include "ui/aura/root_window.h" |
28 #include "ui/aura/window.h" | 30 #include "ui/aura/window.h" |
| 31 #include "ui/base/l10n/l10n_util.h" |
| 32 #include "ui/base/resource/resource_bundle.h" |
| 33 #include "ui/gfx/image/image.h" |
29 | 34 |
30 #if defined(OS_CHROMEOS) | 35 #if defined(OS_CHROMEOS) |
31 #include "chrome/browser/chromeos/drive/drive_file_system_util.h" | 36 #include "chrome/browser/chromeos/drive/drive_file_system_util.h" |
| 37 #include "chrome/browser/chromeos/extensions/file_manager_util.h" |
32 #include "chrome/browser/chromeos/login/user_manager.h" | 38 #include "chrome/browser/chromeos/login/user_manager.h" |
33 #endif | 39 #endif |
34 | 40 |
35 namespace { | 41 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 | 42 // The minimum interval between two screenshot commands. It has to be |
44 // more than 1000 to prevent the conflict of filenames. | 43 // more than 1000 to prevent the conflict of filenames. |
45 const int kScreenshotMinimumIntervalInMS = 1000; | 44 const int kScreenshotMinimumIntervalInMS = 1000; |
46 | 45 |
| 46 const char kNotificationOriginUrl[] = "chrome://screenshot"; |
47 | 47 |
48 void SaveScreenshotInternal(const base::FilePath& screenshot_path, | 48 // Delegate for a notification. This class has two roles: to implement callback |
| 49 // methods for notification, and to provide an identity of the associated |
| 50 // notification. |
| 51 class ScreenshotTakerNotificationDelegate : public NotificationDelegate { |
| 52 public: |
| 53 ScreenshotTakerNotificationDelegate(const std::string& id_text, |
| 54 bool success, |
| 55 const base::FilePath& screenshot_path) |
| 56 : id_text_(id_text), |
| 57 success_(success), |
| 58 screenshot_path_(screenshot_path) { |
| 59 } |
| 60 |
| 61 // Overridden from NotificationDelegate: |
| 62 virtual void Display() OVERRIDE {} |
| 63 virtual void Error() OVERRIDE {} |
| 64 virtual void Close(bool by_user) OVERRIDE {} |
| 65 virtual void Click() OVERRIDE { |
| 66 if (!success_) |
| 67 return; |
| 68 #if defined(OS_CHROMEOS) |
| 69 file_manager_util::ShowFileInFolder(screenshot_path_); |
| 70 #else |
| 71 // TODO(sschmitz): perhaps add similar action for Windows. |
| 72 #endif |
| 73 } |
| 74 virtual std::string id() const OVERRIDE { return id_text_; } |
| 75 virtual content::RenderViewHost* GetRenderViewHost() const OVERRIDE { |
| 76 return NULL; |
| 77 } |
| 78 |
| 79 private: |
| 80 virtual ~ScreenshotTakerNotificationDelegate() {} |
| 81 |
| 82 const std::string id_text_; |
| 83 const bool success_; |
| 84 const base::FilePath screenshot_path_; |
| 85 |
| 86 DISALLOW_COPY_AND_ASSIGN(ScreenshotTakerNotificationDelegate); |
| 87 }; |
| 88 |
| 89 typedef base::Callback< |
| 90 void(ScreenshotTakerObserver::Result screenshot_result, |
| 91 const base::FilePath& screenshot_path)> ShowNotificationCallback; |
| 92 |
| 93 void SaveScreenshotInternal(const ShowNotificationCallback& callback, |
| 94 const base::FilePath& screenshot_path, |
49 scoped_refptr<base::RefCountedBytes> png_data) { | 95 scoped_refptr<base::RefCountedBytes> png_data) { |
50 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); | 96 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); |
51 DCHECK(!screenshot_path.empty()); | 97 DCHECK(!screenshot_path.empty()); |
| 98 ScreenshotTakerObserver::Result result = |
| 99 ScreenshotTakerObserver::SCREENSHOT_SUCCESS; |
52 if (static_cast<size_t>(file_util::WriteFile( | 100 if (static_cast<size_t>(file_util::WriteFile( |
53 screenshot_path, | 101 screenshot_path, |
54 reinterpret_cast<char*>(&(png_data->data()[0])), | 102 reinterpret_cast<char*>(&(png_data->data()[0])), |
55 png_data->size())) != png_data->size()) { | 103 png_data->size())) != png_data->size()) { |
56 LOG(ERROR) << "Failed to save to " << screenshot_path.value(); | 104 LOG(ERROR) << "Failed to save to " << screenshot_path.value(); |
| 105 result = ScreenshotTakerObserver::SCREENSHOT_WRITE_FILE_FAILED; |
57 } | 106 } |
| 107 content::BrowserThread::PostTask( |
| 108 content::BrowserThread::UI, FROM_HERE, |
| 109 base::Bind(callback, result, screenshot_path)); |
58 } | 110 } |
59 | 111 |
60 void SaveScreenshot(const base::FilePath& screenshot_path, | 112 void SaveScreenshot(const ShowNotificationCallback& callback, |
| 113 const base::FilePath& screenshot_path, |
61 scoped_refptr<base::RefCountedBytes> png_data) { | 114 scoped_refptr<base::RefCountedBytes> png_data) { |
62 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); | 115 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); |
63 DCHECK(!screenshot_path.empty()); | 116 DCHECK(!screenshot_path.empty()); |
64 | 117 |
65 if (!file_util::CreateDirectory(screenshot_path.DirName())) { | 118 if (!file_util::CreateDirectory(screenshot_path.DirName())) { |
66 LOG(ERROR) << "Failed to ensure the existence of " | 119 LOG(ERROR) << "Failed to ensure the existence of " |
67 << screenshot_path.DirName().value(); | 120 << screenshot_path.DirName().value(); |
| 121 content::BrowserThread::PostTask( |
| 122 content::BrowserThread::UI, FROM_HERE, |
| 123 base::Bind(callback, |
| 124 ScreenshotTakerObserver::SCREENSHOT_CREATE_DIR_FAILED, |
| 125 screenshot_path)); |
68 return; | 126 return; |
69 } | 127 } |
70 SaveScreenshotInternal(screenshot_path, png_data); | 128 SaveScreenshotInternal(callback, screenshot_path, png_data); |
71 } | 129 } |
72 | 130 |
73 // TODO(kinaba): crbug.com/140425, remove this ungly #ifdef dispatch. | 131 // TODO(kinaba): crbug.com/140425, remove this ungly #ifdef dispatch. |
74 #if defined(OS_CHROMEOS) | 132 #if defined(OS_CHROMEOS) |
75 void SaveScreenshotToDrive(scoped_refptr<base::RefCountedBytes> png_data, | 133 void SaveScreenshotToDrive(const ShowNotificationCallback& callback, |
| 134 scoped_refptr<base::RefCountedBytes> png_data, |
76 drive::DriveFileError error, | 135 drive::DriveFileError error, |
77 const base::FilePath& local_path) { | 136 const base::FilePath& local_path) { |
78 if (error != drive::DRIVE_FILE_OK) { | 137 if (error != drive::DRIVE_FILE_OK) { |
79 LOG(ERROR) << "Failed to write screenshot image to Google Drive: " << error; | 138 LOG(ERROR) << "Failed to write screenshot image to Google Drive: " << error; |
| 139 content::BrowserThread::PostTask( |
| 140 content::BrowserThread::UI, FROM_HERE, |
| 141 base::Bind(callback, |
| 142 ScreenshotTakerObserver::SCREENSHOT_CREATE_FILE_FAILED, |
| 143 local_path)); |
80 return; | 144 return; |
81 } | 145 } |
82 SaveScreenshotInternal(local_path, png_data); | 146 SaveScreenshotInternal(callback, local_path, png_data); |
83 } | 147 } |
84 | 148 |
85 void EnsureDirectoryExistsCallback( | 149 void EnsureDirectoryExistsCallback( |
| 150 const ShowNotificationCallback& callback, |
86 Profile* profile, | 151 Profile* profile, |
87 const base::FilePath& screenshot_path, | 152 const base::FilePath& screenshot_path, |
88 scoped_refptr<base::RefCountedBytes> png_data, | 153 scoped_refptr<base::RefCountedBytes> png_data, |
89 drive::DriveFileError error) { | 154 drive::DriveFileError error) { |
90 // It is okay to fail with DRIVE_FILE_ERROR_EXISTS since anyway the directory | 155 // It is okay to fail with DRIVE_FILE_ERROR_EXISTS since anyway the directory |
91 // of the target file exists. | 156 // of the target file exists. |
92 if (error == drive::DRIVE_FILE_OK || | 157 if (error == drive::DRIVE_FILE_OK || |
93 error == drive::DRIVE_FILE_ERROR_EXISTS) { | 158 error == drive::DRIVE_FILE_ERROR_EXISTS) { |
94 drive::util::PrepareWritableFileAndRun( | 159 drive::util::PrepareWritableFileAndRun( |
95 profile, | 160 profile, |
96 screenshot_path, | 161 screenshot_path, |
97 base::Bind(&SaveScreenshotToDrive, png_data)); | 162 base::Bind(&SaveScreenshotToDrive, callback, png_data)); |
98 } else { | 163 } else { |
99 LOG(ERROR) << "Failed to ensure the existence of the specified directory " | 164 LOG(ERROR) << "Failed to ensure the existence of the specified directory " |
100 << "in Google Drive: " << error; | 165 << "in Google Drive: " << error; |
| 166 callback.Run(ScreenshotTakerObserver::SCREENSHOT_CHECK_DIR_FAILED, |
| 167 screenshot_path); |
101 } | 168 } |
102 } | 169 } |
103 | 170 |
104 void PostSaveScreenshotTask(const base::FilePath& screenshot_path, | 171 void PostSaveScreenshotTask(const ShowNotificationCallback& callback, |
| 172 Profile* profile, |
| 173 const base::FilePath& screenshot_path, |
105 scoped_refptr<base::RefCountedBytes> png_data) { | 174 scoped_refptr<base::RefCountedBytes> png_data) { |
106 if (drive::util::IsUnderDriveMountPoint(screenshot_path)) { | 175 if (drive::util::IsUnderDriveMountPoint(screenshot_path)) { |
107 Profile* profile = ProfileManager::GetDefaultProfileOrOffTheRecord(); | 176 drive::util::EnsureDirectoryExists( |
108 if (profile) { | 177 profile, |
109 drive::util::EnsureDirectoryExists( | 178 screenshot_path.DirName(), |
110 profile, | 179 base::Bind(&EnsureDirectoryExistsCallback, |
111 screenshot_path.DirName(), | 180 callback, |
112 base::Bind(&EnsureDirectoryExistsCallback, | 181 profile, |
113 profile, | 182 screenshot_path, |
114 screenshot_path, | 183 png_data)); |
115 png_data)); | |
116 } | |
117 } else { | 184 } else { |
118 content::BrowserThread::GetBlockingPool()->PostTask( | 185 content::BrowserThread::GetBlockingPool()->PostTask( |
119 FROM_HERE, base::Bind(&SaveScreenshot, screenshot_path, png_data)); | 186 FROM_HERE, base::Bind(&SaveScreenshot, |
| 187 callback, |
| 188 screenshot_path, |
| 189 png_data)); |
120 } | 190 } |
121 } | 191 } |
122 #else | 192 #else |
123 void PostSaveScreenshotTask(const base::FilePath& screenshot_path, | 193 void PostSaveScreenshotTask(const ShowNotificationCallback& callback, |
| 194 Profile* profile, |
| 195 const base::FilePath& screenshot_path, |
124 scoped_refptr<base::RefCountedBytes> png_data) { | 196 scoped_refptr<base::RefCountedBytes> png_data) { |
125 content::BrowserThread::GetBlockingPool()->PostTask( | 197 content::BrowserThread::GetBlockingPool()->PostTask( |
126 FROM_HERE, base::Bind(&SaveScreenshot, screenshot_path, png_data)); | 198 FROM_HERE, base::Bind(&SaveScreenshot, |
| 199 callback, |
| 200 screenshot_path, |
| 201 png_data)); |
127 } | 202 } |
128 #endif | 203 #endif |
129 | 204 |
130 bool GrabWindowSnapshot(aura::Window* window, | 205 bool GrabWindowSnapshot(aura::Window* window, |
131 const gfx::Rect& snapshot_bounds, | 206 const gfx::Rect& snapshot_bounds, |
132 std::vector<unsigned char>* png_data) { | 207 std::vector<unsigned char>* png_data) { |
133 #if defined(OS_LINUX) | 208 #if defined(OS_LINUX) |
134 // chrome::GrabWindowSnapshotForUser checks this too, but | 209 // chrome::GrabWindowSnapshotForUser checks this too, but |
135 // RootWindow::GrabSnapshot does not. | 210 // RootWindow::GrabSnapshot does not. |
136 if (ScreenshotSource::AreScreenshotsDisabled()) | 211 if (ScreenshotSource::AreScreenshotsDisabled()) |
137 return false; | 212 return false; |
138 | 213 |
139 // We use XGetImage() for Linux/ChromeOS for performance reasons. | 214 // We use XGetImage() for Linux/ChromeOS for performance reasons. |
140 // See crbug.com/119492 | 215 // See crbug.com/119492 |
141 // TODO(mukai): remove this when the performance issue has been fixed. | 216 // TODO(mukai): remove this when the performance issue has been fixed. |
142 if (window->GetRootWindow()->GrabSnapshot(snapshot_bounds, png_data)) | 217 if (window->GetRootWindow()->GrabSnapshot(snapshot_bounds, png_data)) |
143 return true; | 218 return true; |
144 #endif // OS_LINUX | 219 #endif // OS_LINUX |
145 | 220 |
146 return chrome::GrabWindowSnapshotForUser(window, png_data, snapshot_bounds); | 221 return chrome::GrabWindowSnapshotForUser(window, png_data, snapshot_bounds); |
147 } | 222 } |
148 | 223 |
149 } // namespace | 224 } // namespace |
150 | 225 |
151 ScreenshotTaker::ScreenshotTaker() { | 226 ScreenshotTaker::ScreenshotTaker(Profile* profile) |
| 227 : profile_(profile), |
| 228 ALLOW_THIS_IN_INITIALIZER_LIST(factory_(this)) { |
| 229 DCHECK(profile_); |
152 } | 230 } |
153 | 231 |
154 ScreenshotTaker::~ScreenshotTaker() { | 232 ScreenshotTaker::~ScreenshotTaker() { |
155 } | 233 } |
156 | 234 |
157 void ScreenshotTaker::HandleTakeScreenshotForAllRootWindows() { | 235 void ScreenshotTaker::HandleTakeScreenshotForAllRootWindows() { |
158 base::FilePath screenshot_directory; | 236 base::FilePath screenshot_directory; |
159 if (!ScreenshotSource::GetScreenshotDirectory(&screenshot_directory)) | 237 if (!screenshot_directory_for_test_.empty()) { |
| 238 screenshot_directory = screenshot_directory_for_test_; |
| 239 } else if (!ScreenshotSource::GetScreenshotDirectory(&screenshot_directory)) { |
| 240 ShowNotification(ScreenshotTakerObserver::SCREENSHOT_GET_DIR_FAILED, |
| 241 base::FilePath()); |
160 return; | 242 return; |
| 243 } |
| 244 std::string screenshot_basename = !screenshot_basename_for_test_.empty() ? |
| 245 screenshot_basename_for_test_ : |
| 246 ScreenshotSource::GetScreenshotBaseFilename(); |
161 | 247 |
162 std::string screenshot_basename = | |
163 ScreenshotSource::GetScreenshotBaseFilename(); | |
164 ash::Shell::RootWindowList root_windows = ash::Shell::GetAllRootWindows(); | 248 ash::Shell::RootWindowList root_windows = ash::Shell::GetAllRootWindows(); |
165 // Reorder root_windows to take the primary root window's snapshot at first. | 249 // Reorder root_windows to take the primary root window's snapshot at first. |
166 aura::RootWindow* primary_root = ash::Shell::GetPrimaryRootWindow(); | 250 aura::RootWindow* primary_root = ash::Shell::GetPrimaryRootWindow(); |
167 if (*(root_windows.begin()) != primary_root) { | 251 if (*(root_windows.begin()) != primary_root) { |
168 root_windows.erase(std::find( | 252 root_windows.erase(std::find( |
169 root_windows.begin(), root_windows.end(), primary_root)); | 253 root_windows.begin(), root_windows.end(), primary_root)); |
170 root_windows.insert(root_windows.begin(), primary_root); | 254 root_windows.insert(root_windows.begin(), primary_root); |
171 } | 255 } |
172 for (size_t i = 0; i < root_windows.size(); ++i) { | 256 for (size_t i = 0; i < root_windows.size(); ++i) { |
173 aura::RootWindow* root_window = root_windows[i]; | 257 aura::RootWindow* root_window = root_windows[i]; |
174 scoped_refptr<base::RefCountedBytes> png_data(new base::RefCountedBytes); | 258 scoped_refptr<base::RefCountedBytes> png_data(new base::RefCountedBytes); |
175 std::string basename = screenshot_basename; | 259 std::string basename = screenshot_basename; |
176 gfx::Rect rect = root_window->bounds(); | 260 gfx::Rect rect = root_window->bounds(); |
177 if (root_windows.size() > 1) | 261 if (root_windows.size() > 1) |
178 basename += base::StringPrintf(" - Display %d", static_cast<int>(i + 1)); | 262 basename += base::StringPrintf(" - Display %d", static_cast<int>(i + 1)); |
| 263 base::FilePath screenshot_path = |
| 264 screenshot_directory.AppendASCII(basename + ".png"); |
179 if (GrabWindowSnapshot(root_window, rect, &png_data->data())) { | 265 if (GrabWindowSnapshot(root_window, rect, &png_data->data())) { |
180 DisplayVisualFeedback(rect); | |
181 PostSaveScreenshotTask( | 266 PostSaveScreenshotTask( |
182 screenshot_directory.AppendASCII(basename + ".png"), png_data); | 267 base::Bind(&ScreenshotTaker::ShowNotification, factory_.GetWeakPtr()), |
| 268 profile_, |
| 269 screenshot_path, |
| 270 png_data); |
183 } else { | 271 } else { |
184 LOG(ERROR) << "Failed to grab the window screenshot for " << i; | 272 LOG(ERROR) << "Failed to grab the window screenshot for " << i; |
| 273 ShowNotification( |
| 274 ScreenshotTakerObserver::SCREENSHOT_GRABWINDOW_FULL_FAILED, |
| 275 screenshot_path); |
185 } | 276 } |
186 } | 277 } |
187 last_screenshot_timestamp_ = base::Time::Now(); | 278 last_screenshot_timestamp_ = base::Time::Now(); |
188 } | 279 } |
189 | 280 |
190 void ScreenshotTaker::HandleTakePartialScreenshot( | 281 void ScreenshotTaker::HandleTakePartialScreenshot( |
191 aura::Window* window, const gfx::Rect& rect) { | 282 aura::Window* window, const gfx::Rect& rect) { |
192 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 283 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
193 | 284 |
194 base::FilePath screenshot_directory; | 285 base::FilePath screenshot_directory; |
195 if (!ScreenshotSource::GetScreenshotDirectory(&screenshot_directory)) | 286 if (!screenshot_directory_for_test_.empty()) { |
| 287 screenshot_directory = screenshot_directory_for_test_; |
| 288 } else if (!ScreenshotSource::GetScreenshotDirectory(&screenshot_directory)) { |
| 289 ShowNotification(ScreenshotTakerObserver::SCREENSHOT_GET_DIR_FAILED, |
| 290 base::FilePath()); |
196 return; | 291 return; |
| 292 } |
197 | 293 |
198 scoped_refptr<base::RefCountedBytes> png_data(new base::RefCountedBytes); | 294 scoped_refptr<base::RefCountedBytes> png_data(new base::RefCountedBytes); |
199 | 295 |
| 296 std::string screenshot_basename = !screenshot_basename_for_test_.empty() ? |
| 297 screenshot_basename_for_test_ : |
| 298 ScreenshotSource::GetScreenshotBaseFilename(); |
| 299 base::FilePath screenshot_path = |
| 300 screenshot_directory.AppendASCII(screenshot_basename + ".png"); |
200 if (GrabWindowSnapshot(window, rect, &png_data->data())) { | 301 if (GrabWindowSnapshot(window, rect, &png_data->data())) { |
201 last_screenshot_timestamp_ = base::Time::Now(); | 302 last_screenshot_timestamp_ = base::Time::Now(); |
202 DisplayVisualFeedback(rect); | |
203 PostSaveScreenshotTask( | 303 PostSaveScreenshotTask( |
204 screenshot_directory.AppendASCII( | 304 base::Bind(&ScreenshotTaker::ShowNotification, factory_.GetWeakPtr()), |
205 ScreenshotSource::GetScreenshotBaseFilename() + ".png"), | 305 profile_, |
206 png_data); | 306 screenshot_path, |
| 307 png_data); |
207 } else { | 308 } else { |
208 LOG(ERROR) << "Failed to grab the window screenshot"; | 309 LOG(ERROR) << "Failed to grab the window screenshot"; |
| 310 ShowNotification( |
| 311 ScreenshotTakerObserver::SCREENSHOT_GRABWINDOW_PARTIAL_FAILED, |
| 312 screenshot_path); |
209 } | 313 } |
210 } | 314 } |
211 | 315 |
212 bool ScreenshotTaker::CanTakeScreenshot() { | 316 bool ScreenshotTaker::CanTakeScreenshot() { |
213 return last_screenshot_timestamp_.is_null() || | 317 return last_screenshot_timestamp_.is_null() || |
214 base::Time::Now() - last_screenshot_timestamp_ > | 318 base::Time::Now() - last_screenshot_timestamp_ > |
215 base::TimeDelta::FromMilliseconds( | 319 base::TimeDelta::FromMilliseconds( |
216 kScreenshotMinimumIntervalInMS); | 320 kScreenshotMinimumIntervalInMS); |
217 } | 321 } |
218 | 322 |
219 void ScreenshotTaker::CloseVisualFeedbackLayer() { | 323 void ScreenshotTaker::ShowNotification( |
220 visual_feedback_layer_.reset(); | 324 ScreenshotTakerObserver::Result screenshot_result, |
| 325 const base::FilePath& screenshot_path) { |
| 326 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 327 #if defined(OS_CHROMEOS) |
| 328 // TODO(sschmitz): make this work for Windows. |
| 329 static int id = 0; |
| 330 std::string id_text = base::StringPrintf("screenshot_%3.3d", ++id); |
| 331 string16 replace_id = UTF8ToUTF16(id_text); |
| 332 bool success = |
| 333 (screenshot_result == ScreenshotTakerObserver::SCREENSHOT_SUCCESS); |
| 334 Notification notification( |
| 335 GURL(kNotificationOriginUrl), |
| 336 ui::ResourceBundle::GetSharedInstance().GetImageNamed( |
| 337 IDR_SCREENSHOT_NOTIFICATION_ICON), |
| 338 l10n_util::GetStringUTF16( |
| 339 success ? |
| 340 IDS_ASH_SCREENSHOT_NOTIFICATION_TITLE_SUCCESS : |
| 341 IDS_ASH_SCREENSHOT_NOTIFICATION_TITLE_FAIL), |
| 342 l10n_util::GetStringUTF16( |
| 343 success ? |
| 344 IDS_ASH_SCREENSHOT_NOTIFICATION_TEXT_SUCCESS : |
| 345 IDS_ASH_SCREENSHOT_NOTIFICATION_TEXT_FAIL), |
| 346 WebKit::WebTextDirectionDefault, |
| 347 string16(), |
| 348 replace_id, |
| 349 new ScreenshotTakerNotificationDelegate(id_text, |
| 350 success, |
| 351 screenshot_path)); |
| 352 g_browser_process->notification_ui_manager()->Add(notification, profile_); |
| 353 #endif |
| 354 |
| 355 FOR_EACH_OBSERVER(ScreenshotTakerObserver, observers_, |
| 356 OnScreenshotCompleted(screenshot_result, screenshot_path)); |
221 } | 357 } |
222 | 358 |
223 void ScreenshotTaker::DisplayVisualFeedback(const gfx::Rect& rect) { | 359 void ScreenshotTaker::AddObserver(ScreenshotTakerObserver* observer) { |
224 visual_feedback_layer_.reset(new ui::Layer(ui::LAYER_SOLID_COLOR)); | 360 observers_.AddObserver(observer); |
225 visual_feedback_layer_->SetColor(SK_ColorWHITE); | 361 } |
226 visual_feedback_layer_->SetOpacity(kVisualFeedbackLayerOpacity); | |
227 visual_feedback_layer_->SetBounds(rect); | |
228 | 362 |
229 ui::Layer* parent = ash::Shell::GetContainer( | 363 void ScreenshotTaker::RemoveObserver(ScreenshotTakerObserver* observer) { |
230 ash::Shell::GetActiveRootWindow(), | 364 observers_.RemoveObserver(observer); |
231 ash::internal::kShellWindowId_OverlayContainer)->layer(); | 365 } |
232 parent->Add(visual_feedback_layer_.get()); | |
233 visual_feedback_layer_->SetVisible(true); | |
234 | 366 |
235 MessageLoopForUI::current()->PostDelayedTask( | 367 bool ScreenshotTaker::HasObserver(ScreenshotTakerObserver* observer) const { |
236 FROM_HERE, | 368 return observers_.HasObserver(observer); |
237 base::Bind(&ScreenshotTaker::CloseVisualFeedbackLayer, | |
238 base::Unretained(this)), | |
239 base::TimeDelta::FromMilliseconds(kVisualFeedbackLayerDisplayTimeMs)); | |
240 } | 369 } |
| 370 |
| 371 void ScreenshotTaker::SetScreenshotDirectoryForTest( |
| 372 const base::FilePath& directory) { |
| 373 screenshot_directory_for_test_ = directory; |
| 374 } |
| 375 |
| 376 void ScreenshotTaker::SetScreenshotBasenameForTest( |
| 377 const std::string& basename){ |
| 378 screenshot_basename_for_test_ = basename; |
| 379 } |
OLD | NEW |