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