OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/chrome_screenshot_taker.h" |
6 | |
7 #include <climits> | |
8 #include <string> | |
9 | 6 |
10 #include "ash/shell.h" | 7 #include "ash/shell.h" |
11 #include "ash/shell_delegate.h" | |
12 #include "ash/system/system_notifier.h" | 8 #include "ash/system/system_notifier.h" |
13 #include "base/base64.h" | 9 #include "base/base64.h" |
14 #include "base/bind.h" | 10 #include "base/bind.h" |
15 #include "base/files/file_util.h" | 11 #include "base/callback.h" |
16 #include "base/i18n/time_formatting.h" | 12 #include "base/i18n/time_formatting.h" |
17 #include "base/logging.h" | |
18 #include "base/memory/ref_counted_memory.h" | |
19 #include "base/prefs/pref_service.h" | 13 #include "base/prefs/pref_service.h" |
20 #include "base/strings/stringprintf.h" | 14 #include "base/strings/stringprintf.h" |
21 #include "base/strings/utf_string_conversions.h" | 15 #include "base/strings/utf_string_conversions.h" |
22 #include "base/threading/sequenced_worker_pool.h" | |
23 #include "base/time/time.h" | |
24 #include "chrome/browser/browser_process.h" | 16 #include "chrome/browser/browser_process.h" |
25 #include "chrome/browser/download/download_prefs.h" | 17 #include "chrome/browser/download/download_prefs.h" |
26 #include "chrome/browser/notifications/notification_ui_manager.h" | 18 #include "chrome/browser/notifications/notification_ui_manager.h" |
27 #include "chrome/browser/profiles/profile.h" | 19 #include "chrome/browser/profiles/profile.h" |
28 #include "chrome/browser/profiles/profile_manager.h" | 20 #include "chrome/browser/profiles/profile_manager.h" |
29 #include "chrome/common/pref_names.h" | 21 #include "chrome/common/pref_names.h" |
30 #include "content/public/browser/browser_thread.h" | 22 #include "content/public/browser/browser_thread.h" |
31 #include "content/public/browser/user_metrics.h" | 23 #include "content/public/browser/user_metrics.h" |
32 #include "grit/ash_strings.h" | 24 #include "grit/ash_strings.h" |
33 #include "grit/theme_resources.h" | 25 #include "grit/theme_resources.h" |
34 #include "ui/aura/window.h" | |
35 #include "ui/aura/window_event_dispatcher.h" | |
36 #include "ui/base/clipboard/clipboard.h" | 26 #include "ui/base/clipboard/clipboard.h" |
37 #include "ui/base/clipboard/scoped_clipboard_writer.h" | 27 #include "ui/base/clipboard/scoped_clipboard_writer.h" |
38 #include "ui/base/l10n/l10n_util.h" | 28 #include "ui/base/l10n/l10n_util.h" |
39 #include "ui/base/resource/resource_bundle.h" | 29 #include "ui/base/resource/resource_bundle.h" |
40 #include "ui/gfx/image/image.h" | |
41 #include "ui/snapshot/snapshot.h" | |
42 #include "ui/strings/grit/ui_strings.h" | 30 #include "ui/strings/grit/ui_strings.h" |
43 | 31 |
44 #if defined(OS_CHROMEOS) | 32 #if defined(OS_CHROMEOS) |
45 #include "chrome/browser/chromeos/drive/file_system_interface.h" | 33 #include "chrome/browser/chromeos/drive/file_system_interface.h" |
46 #include "chrome/browser/chromeos/drive/file_system_util.h" | 34 #include "chrome/browser/chromeos/drive/file_system_util.h" |
47 #include "chrome/browser/chromeos/file_manager/open_util.h" | 35 #include "chrome/browser/chromeos/file_manager/open_util.h" |
48 #include "chrome/browser/notifications/desktop_notification_service.h" | 36 #include "chrome/browser/notifications/desktop_notification_service.h" |
49 #include "chrome/browser/notifications/desktop_notification_service_factory.h" | 37 #include "chrome/browser/notifications/desktop_notification_service_factory.h" |
50 #include "chromeos/login/login_state.h" | 38 #include "chromeos/login/login_state.h" |
51 #endif | 39 #endif |
52 | 40 |
53 namespace { | 41 namespace { |
54 // The minimum interval between two screenshot commands. It has to be | |
55 // more than 1000 to prevent the conflict of filenames. | |
56 const int kScreenshotMinimumIntervalInMS = 1000; | |
57 | 42 |
58 const char kNotificationId[] = "screenshot"; | 43 const char kNotificationId[] = "screenshot"; |
59 | 44 |
60 #if defined(OS_CHROMEOS) | 45 #if defined(OS_CHROMEOS) |
61 const char kNotificationOriginUrl[] = "chrome://screenshot"; | 46 const char kNotificationOriginUrl[] = "chrome://screenshot"; |
62 #endif | 47 #endif |
63 | 48 |
64 const char kImageClipboardFormatPrefix[] = "<img src='data:image/png;base64,"; | 49 const char kImageClipboardFormatPrefix[] = "<img src='data:image/png;base64,"; |
65 const char kImageClipboardFormatSuffix[] = "'>"; | 50 const char kImageClipboardFormatSuffix[] = "'>"; |
66 | 51 |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
155 FROM_HERE, base::Bind( | 140 FROM_HERE, base::Bind( |
156 &ReadFileAndCopyToClipboardLocal, screenshot_path_)); | 141 &ReadFileAndCopyToClipboardLocal, screenshot_path_)); |
157 } | 142 } |
158 bool HasClickedListener() override { return success_; } | 143 bool HasClickedListener() override { return success_; } |
159 std::string id() const override { return std::string(kNotificationId); } | 144 std::string id() const override { return std::string(kNotificationId); } |
160 | 145 |
161 private: | 146 private: |
162 ~ScreenshotTakerNotificationDelegate() override {} | 147 ~ScreenshotTakerNotificationDelegate() override {} |
163 | 148 |
164 const bool success_; | 149 const bool success_; |
165 Profile* profile_; | 150 Profile* profile_; |
oshima
2014/11/12 11:32:14
Is it hard to change this to BrowserContext? (It's
flackr
2014/11/18 16:46:30
It looks like it's only used as a browser context,
| |
166 const base::FilePath screenshot_path_; | 151 const base::FilePath screenshot_path_; |
167 | 152 |
168 DISALLOW_COPY_AND_ASSIGN(ScreenshotTakerNotificationDelegate); | 153 DISALLOW_COPY_AND_ASSIGN(ScreenshotTakerNotificationDelegate); |
169 }; | 154 }; |
170 | 155 |
171 typedef base::Callback< | 156 #if defined(OS_CHROMEOS) |
172 void(ScreenshotTakerObserver::Result screenshot_result, | 157 int GetScreenshotNotificationTitle( |
173 const base::FilePath& screenshot_path)> ShowNotificationCallback; | 158 ScreenshotTakerObserver::Result screenshot_result) { |
174 | 159 switch (screenshot_result) { |
175 void SaveScreenshotInternal(const ShowNotificationCallback& callback, | 160 case ScreenshotTakerObserver::SCREENSHOTS_DISABLED: |
176 const base::FilePath& screenshot_path, | 161 return IDS_ASH_SCREENSHOT_NOTIFICATION_TITLE_DISABLED; |
177 const base::FilePath& local_path, | 162 case ScreenshotTakerObserver::SCREENSHOT_SUCCESS: |
178 scoped_refptr<base::RefCountedBytes> png_data) { | 163 return IDS_ASH_SCREENSHOT_NOTIFICATION_TITLE_SUCCESS; |
179 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); | 164 default: |
180 DCHECK(!local_path.empty()); | 165 return IDS_ASH_SCREENSHOT_NOTIFICATION_TITLE_FAIL; |
181 ScreenshotTakerObserver::Result result = | |
182 ScreenshotTakerObserver::SCREENSHOT_SUCCESS; | |
183 if (static_cast<size_t>(base::WriteFile( | |
184 local_path, | |
185 reinterpret_cast<char*>(&(png_data->data()[0])), | |
186 png_data->size())) != png_data->size()) { | |
187 LOG(ERROR) << "Failed to save to " << local_path.value(); | |
188 result = ScreenshotTakerObserver::SCREENSHOT_WRITE_FILE_FAILED; | |
189 } | 166 } |
190 content::BrowserThread::PostTask( | |
191 content::BrowserThread::UI, FROM_HERE, | |
192 base::Bind(callback, result, screenshot_path)); | |
193 } | 167 } |
194 | 168 |
195 void SaveScreenshot(const ShowNotificationCallback& callback, | 169 int GetScreenshotNotificationText( |
196 const base::FilePath& screenshot_path, | 170 ScreenshotTakerObserver::Result screenshot_result) { |
197 scoped_refptr<base::RefCountedBytes> png_data) { | 171 switch (screenshot_result) { |
198 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); | 172 case ScreenshotTakerObserver::SCREENSHOTS_DISABLED: |
199 DCHECK(!screenshot_path.empty()); | 173 return IDS_ASH_SCREENSHOT_NOTIFICATION_TEXT_DISABLED; |
200 | 174 case ScreenshotTakerObserver::SCREENSHOT_SUCCESS: |
201 if (!base::CreateDirectory(screenshot_path.DirName())) { | 175 return IDS_ASH_SCREENSHOT_NOTIFICATION_TEXT_SUCCESS; |
202 LOG(ERROR) << "Failed to ensure the existence of " | 176 default: |
203 << screenshot_path.DirName().value(); | 177 return IDS_ASH_SCREENSHOT_NOTIFICATION_TEXT_FAIL; |
204 content::BrowserThread::PostTask( | |
205 content::BrowserThread::UI, FROM_HERE, | |
206 base::Bind(callback, | |
207 ScreenshotTakerObserver::SCREENSHOT_CREATE_DIR_FAILED, | |
208 screenshot_path)); | |
209 return; | |
210 } | 178 } |
211 SaveScreenshotInternal(callback, screenshot_path, screenshot_path, png_data); | |
212 } | 179 } |
213 | 180 |
214 // TODO(kinaba): crbug.com/140425, remove this ungly #ifdef dispatch. | 181 void PrepareWritableFileCallback( |
215 #if defined(OS_CHROMEOS) | 182 const ChromeScreenshotTaker::WritableFileCallback& callback, |
216 void SaveScreenshotToDrive(const ShowNotificationCallback& callback, | 183 drive::FileError error, |
217 const base::FilePath& screenshot_path, | 184 const base::FilePath& local_path) { |
218 scoped_refptr<base::RefCountedBytes> png_data, | 185 callback.Run(error == drive::FILE_ERROR_OK ? |
219 drive::FileError error, | 186 ScreenshotTakerClient::WRITABLE_FILE_SUCCESS : |
220 const base::FilePath& local_path) { | 187 ScreenshotTakerClient::WRITABLE_FILE_CREATE_FAILED, |
221 // |screenshot_path| is used in the notification callback. | 188 local_path); |
222 // |local_path| is a temporary file in a hidden cache directory used for | |
223 // internal work generated by drive::util::PrepareWritableFileAndRun. | |
224 if (error != drive::FILE_ERROR_OK) { | |
225 LOG(ERROR) << "Failed to write screenshot image to Google Drive: " << error; | |
226 content::BrowserThread::PostTask( | |
227 content::BrowserThread::UI, FROM_HERE, | |
228 base::Bind(callback, | |
229 ScreenshotTakerObserver::SCREENSHOT_CREATE_FILE_FAILED, | |
230 screenshot_path)); | |
231 return; | |
232 } | |
233 SaveScreenshotInternal(callback, screenshot_path, local_path, png_data); | |
234 } | 189 } |
235 | 190 |
236 void EnsureDirectoryExistsCallback( | 191 void EnsureDirectoryExistsCallback( |
237 const ShowNotificationCallback& callback, | 192 const ChromeScreenshotTaker::WritableFileCallback& callback, |
238 Profile* profile, | 193 Profile* profile, |
239 const base::FilePath& screenshot_path, | 194 const base::FilePath& path, |
240 scoped_refptr<base::RefCountedBytes> png_data, | |
241 drive::FileError error) { | 195 drive::FileError error) { |
242 // It is okay to fail with FILE_ERROR_EXISTS since anyway the directory | 196 // It is okay to fail with FILE_ERROR_EXISTS since anyway the directory |
243 // of the target file exists. | 197 // of the target file exists. |
244 if (error == drive::FILE_ERROR_OK || | 198 if (error == drive::FILE_ERROR_OK || |
245 error == drive::FILE_ERROR_EXISTS) { | 199 error == drive::FILE_ERROR_EXISTS) { |
246 drive::util::PrepareWritableFileAndRun( | 200 drive::util::PrepareWritableFileAndRun( |
247 profile, | 201 profile, |
248 screenshot_path, | 202 path, |
249 base::Bind(&SaveScreenshotToDrive, | 203 base::Bind(&PrepareWritableFileCallback, |
250 callback, | 204 callback)); |
251 screenshot_path, | |
252 png_data)); | |
253 } else { | 205 } else { |
254 LOG(ERROR) << "Failed to ensure the existence of the specified directory " | 206 LOG(ERROR) << "Failed to ensure the existence of the specified directory " |
255 << "in Google Drive: " << error; | 207 << "in Google Drive: " << error; |
256 callback.Run(ScreenshotTakerObserver::SCREENSHOT_CHECK_DIR_FAILED, | 208 content::BrowserThread::GetBlockingPool()->PostTask( |
257 screenshot_path); | 209 FROM_HERE, base::Bind( |
210 callback, | |
211 ScreenshotTakerClient::WRITABLE_FILE_CHECK_DIR_FAILED, | |
212 base::FilePath())); | |
258 } | 213 } |
259 } | 214 } |
215 #endif | |
260 | 216 |
261 void PostSaveScreenshotTask(const ShowNotificationCallback& callback, | 217 bool ScreenshotsDisabled() { |
262 Profile* profile, | 218 return g_browser_process->local_state()-> |
263 const base::FilePath& screenshot_path, | 219 GetBoolean(prefs::kDisableScreenshots); |
264 scoped_refptr<base::RefCountedBytes> png_data) { | |
265 if (drive::util::IsUnderDriveMountPoint(screenshot_path)) { | |
266 drive::util::EnsureDirectoryExists( | |
267 profile, | |
268 screenshot_path.DirName(), | |
269 base::Bind(&EnsureDirectoryExistsCallback, | |
270 callback, | |
271 profile, | |
272 screenshot_path, | |
273 png_data)); | |
274 } else { | |
275 content::BrowserThread::GetBlockingPool()->PostTask( | |
276 FROM_HERE, base::Bind(&SaveScreenshot, | |
277 callback, | |
278 screenshot_path, | |
279 png_data)); | |
280 } | |
281 } | 220 } |
282 #else | |
283 void PostSaveScreenshotTask(const ShowNotificationCallback& callback, | |
284 Profile* profile, | |
285 const base::FilePath& screenshot_path, | |
286 scoped_refptr<base::RefCountedBytes> png_data) { | |
287 content::BrowserThread::GetBlockingPool()->PostTask( | |
288 FROM_HERE, base::Bind(&SaveScreenshot, | |
289 callback, | |
290 screenshot_path, | |
291 png_data)); | |
292 } | |
293 #endif | |
294 | 221 |
295 bool ShouldUse24HourClock() { | 222 bool ShouldUse24HourClock() { |
296 #if defined(OS_CHROMEOS) | 223 #if defined(OS_CHROMEOS) |
297 Profile* profile = ProfileManager::GetActiveUserProfile(); | 224 Profile* profile = ProfileManager::GetActiveUserProfile(); |
298 if (profile) { | 225 if (profile) { |
299 return profile->GetPrefs()->GetBoolean(prefs::kUse24HourClock); | 226 return profile->GetPrefs()->GetBoolean(prefs::kUse24HourClock); |
300 } | 227 } |
301 #endif | 228 #endif |
302 return base::GetHourClockType() == base::k24HourClock; | 229 return base::GetHourClockType() == base::k24HourClock; |
303 } | 230 } |
304 | 231 |
232 bool GetScreenshotDirectory(base::FilePath* directory) { | |
233 bool is_logged_in = true; | |
234 | |
235 #if defined(OS_CHROMEOS) | |
236 is_logged_in = chromeos::LoginState::Get()->IsUserLoggedIn(); | |
237 #endif | |
238 | |
239 if (is_logged_in) { | |
240 DownloadPrefs* download_prefs = DownloadPrefs::FromBrowserContext( | |
241 ProfileManager::GetActiveUserProfile()); | |
242 *directory = download_prefs->DownloadPath(); | |
243 } else { | |
244 if (!base::GetTempDir(directory)) { | |
245 LOG(ERROR) << "Failed to find temporary directory."; | |
246 return false; | |
247 } | |
248 } | |
249 return true; | |
250 } | |
251 | |
305 std::string GetScreenshotBaseFilename() { | 252 std::string GetScreenshotBaseFilename() { |
306 base::Time::Exploded now; | 253 base::Time::Exploded now; |
307 base::Time::Now().LocalExplode(&now); | 254 base::Time::Now().LocalExplode(&now); |
308 | 255 |
309 // We don't use base/i18n/time_formatting.h here because it doesn't | 256 // We don't use base/i18n/time_formatting.h here because it doesn't |
310 // support our format. Don't use ICU either to avoid i18n file names | 257 // support our format. Don't use ICU either to avoid i18n file names |
311 // for non-English locales. | 258 // for non-English locales. |
312 // TODO(mukai): integrate this logic somewhere time_formatting.h | 259 // TODO(mukai): integrate this logic somewhere time_formatting.h |
313 std::string file_name = base::StringPrintf( | 260 std::string file_name = base::StringPrintf( |
314 "Screenshot %d-%02d-%02d at ", now.year, now.month, now.day_of_month); | 261 "Screenshot %d-%02d-%02d at ", now.year, now.month, now.day_of_month); |
315 | 262 |
316 if (ShouldUse24HourClock()) { | 263 if (ShouldUse24HourClock()) { |
317 file_name.append(base::StringPrintf( | 264 file_name.append(base::StringPrintf( |
318 "%02d.%02d.%02d", now.hour, now.minute, now.second)); | 265 "%02d.%02d.%02d", now.hour, now.minute, now.second)); |
319 } else { | 266 } else { |
320 int hour = now.hour; | 267 int hour = now.hour; |
321 if (hour > 12) { | 268 if (hour > 12) { |
322 hour -= 12; | 269 hour -= 12; |
323 } else if (hour == 0) { | 270 } else if (hour == 0) { |
324 hour = 12; | 271 hour = 12; |
325 } | 272 } |
326 file_name.append(base::StringPrintf( | 273 file_name.append(base::StringPrintf( |
327 "%d.%02d.%02d ", hour, now.minute, now.second)); | 274 "%d.%02d.%02d ", hour, now.minute, now.second)); |
328 file_name.append((now.hour >= 12) ? "PM" : "AM"); | 275 file_name.append((now.hour >= 12) ? "PM" : "AM"); |
329 } | 276 } |
330 | 277 |
331 return file_name; | 278 return file_name; |
332 } | 279 } |
333 | 280 |
334 bool GetScreenshotDirectory(base::FilePath* directory) { | 281 } // namespace |
335 bool is_logged_in = true; | |
336 | 282 |
337 #if defined(OS_CHROMEOS) | 283 ChromeScreenshotTaker::ChromeScreenshotTaker() |
338 is_logged_in = chromeos::LoginState::Get()->IsUserLoggedIn(); | 284 : screenshot_taker_(new ScreenshotTaker(this)) { |
339 #endif | 285 screenshot_taker_->AddObserver(this); |
340 | |
341 if (is_logged_in) { | |
342 DownloadPrefs* download_prefs = DownloadPrefs::FromBrowserContext( | |
343 ProfileManager::GetActiveUserProfile()); | |
344 *directory = download_prefs->DownloadPath(); | |
345 } else { | |
346 if (!base::GetTempDir(directory)) { | |
347 LOG(ERROR) << "Failed to find temporary directory."; | |
348 return false; | |
349 } | |
350 } | |
351 return true; | |
352 } | 286 } |
353 | 287 |
354 #if defined(OS_CHROMEOS) | 288 ChromeScreenshotTaker::~ChromeScreenshotTaker() { |
355 int GetScreenshotNotificationTitle( | 289 screenshot_taker_->RemoveObserver(this); |
356 ScreenshotTakerObserver::Result screenshot_result) { | |
357 switch (screenshot_result) { | |
358 case ScreenshotTakerObserver::SCREENSHOTS_DISABLED: | |
359 return IDS_ASH_SCREENSHOT_NOTIFICATION_TITLE_DISABLED; | |
360 case ScreenshotTakerObserver::SCREENSHOT_SUCCESS: | |
361 return IDS_ASH_SCREENSHOT_NOTIFICATION_TITLE_SUCCESS; | |
362 default: | |
363 return IDS_ASH_SCREENSHOT_NOTIFICATION_TITLE_FAIL; | |
364 } | |
365 } | 290 } |
366 | 291 |
367 int GetScreenshotNotificationText( | 292 void ChromeScreenshotTaker::HandleTakeScreenshotForAllRootWindows() { |
368 ScreenshotTakerObserver::Result screenshot_result) { | 293 if (ScreenshotsDisabled()) |
369 switch (screenshot_result) { | 294 return; |
370 case ScreenshotTakerObserver::SCREENSHOTS_DISABLED: | |
371 return IDS_ASH_SCREENSHOT_NOTIFICATION_TEXT_DISABLED; | |
372 case ScreenshotTakerObserver::SCREENSHOT_SUCCESS: | |
373 return IDS_ASH_SCREENSHOT_NOTIFICATION_TEXT_SUCCESS; | |
374 default: | |
375 return IDS_ASH_SCREENSHOT_NOTIFICATION_TEXT_FAIL; | |
376 } | |
377 } | |
378 #endif | |
379 | 295 |
380 } // namespace | 296 base::FilePath screenshot_directory; |
381 | 297 if (!GetScreenshotDirectory(&screenshot_directory)) { |
382 ScreenshotTaker::ScreenshotTaker() | 298 OnScreenshotCompleted(ScreenshotTakerObserver::SCREENSHOT_GET_DIR_FAILED, |
383 : profile_for_test_(NULL), | 299 base::FilePath()); |
384 factory_(this) { | |
385 } | |
386 | |
387 ScreenshotTaker::~ScreenshotTaker() { | |
388 } | |
389 | |
390 void ScreenshotTaker::HandleTakeScreenshotForAllRootWindows() { | |
391 if (g_browser_process->local_state()-> | |
392 GetBoolean(prefs::kDisableScreenshots)) { | |
393 ShowNotification(ScreenshotTakerObserver::SCREENSHOTS_DISABLED, | |
394 base::FilePath()); | |
395 return; | 300 return; |
396 } | 301 } |
397 base::FilePath screenshot_directory; | |
398 if (!screenshot_directory_for_test_.empty()) { | |
399 screenshot_directory = screenshot_directory_for_test_; | |
400 } else if (!GetScreenshotDirectory(&screenshot_directory)) { | |
401 ShowNotification(ScreenshotTakerObserver::SCREENSHOT_GET_DIR_FAILED, | |
402 base::FilePath()); | |
403 return; | |
404 } | |
405 std::string screenshot_basename = !screenshot_basename_for_test_.empty() ? | |
406 screenshot_basename_for_test_ : GetScreenshotBaseFilename(); | |
407 | 302 |
303 std::string screenshot_basename = GetScreenshotBaseFilename(); | |
408 aura::Window::Windows root_windows = ash::Shell::GetAllRootWindows(); | 304 aura::Window::Windows root_windows = ash::Shell::GetAllRootWindows(); |
409 // Reorder root_windows to take the primary root window's snapshot at first. | 305 // Reorder root_windows to take the primary root window's snapshot at first. |
410 aura::Window* primary_root = ash::Shell::GetPrimaryRootWindow(); | 306 aura::Window* primary_root = ash::Shell::GetPrimaryRootWindow(); |
411 if (*(root_windows.begin()) != primary_root) { | 307 if (*(root_windows.begin()) != primary_root) { |
412 root_windows.erase(std::find( | 308 root_windows.erase(std::find( |
413 root_windows.begin(), root_windows.end(), primary_root)); | 309 root_windows.begin(), root_windows.end(), primary_root)); |
414 root_windows.insert(root_windows.begin(), primary_root); | 310 root_windows.insert(root_windows.begin(), primary_root); |
415 } | 311 } |
312 std::vector<base::FilePath> filenames; | |
416 for (size_t i = 0; i < root_windows.size(); ++i) { | 313 for (size_t i = 0; i < root_windows.size(); ++i) { |
417 aura::Window* root_window = root_windows[i]; | 314 aura::Window* root_window = root_windows[i]; |
418 std::string basename = screenshot_basename; | 315 std::string basename = screenshot_basename; |
419 gfx::Rect rect = root_window->bounds(); | 316 gfx::Rect rect = root_window->bounds(); |
420 if (root_windows.size() > 1) | 317 if (root_windows.size() > 1) |
421 basename += base::StringPrintf(" - Display %d", static_cast<int>(i + 1)); | 318 basename += base::StringPrintf(" - Display %d", static_cast<int>(i + 1)); |
422 base::FilePath screenshot_path = | 319 base::FilePath screenshot_path = |
423 screenshot_directory.AppendASCII(basename + ".png"); | 320 screenshot_directory.AppendASCII(basename + ".png"); |
424 GrabFullWindowSnapshotAsync( | 321 screenshot_taker_->TakeScreenshot(root_window, |
425 root_window, rect, GetProfile(), screenshot_path, i); | 322 rect, |
323 screenshot_path); | |
426 } | 324 } |
427 content::RecordAction(base::UserMetricsAction("Screenshot_TakeFull")); | 325 content::RecordAction(base::UserMetricsAction("Screenshot_TakeFull")); |
428 } | 326 } |
429 | 327 |
430 void ScreenshotTaker::HandleTakePartialScreenshot( | 328 void ChromeScreenshotTaker::HandleTakePartialScreenshot( |
431 aura::Window* window, const gfx::Rect& rect) { | 329 aura::Window* window, |
432 if (g_browser_process->local_state()-> | 330 const gfx::Rect& rect) { |
433 GetBoolean(prefs::kDisableScreenshots)) { | 331 if (ScreenshotsDisabled()) |
434 ShowNotification(ScreenshotTakerObserver::SCREENSHOTS_DISABLED, | |
435 base::FilePath()); | |
436 return; | 332 return; |
437 } | |
438 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
439 | 333 |
440 base::FilePath screenshot_directory; | 334 base::FilePath screenshot_directory; |
441 if (!screenshot_directory_for_test_.empty()) { | 335 if (!GetScreenshotDirectory(&screenshot_directory)) { |
442 screenshot_directory = screenshot_directory_for_test_; | 336 OnScreenshotCompleted(ScreenshotTakerObserver::SCREENSHOT_GET_DIR_FAILED, |
443 } else if (!GetScreenshotDirectory(&screenshot_directory)) { | 337 base::FilePath()); |
444 ShowNotification(ScreenshotTakerObserver::SCREENSHOT_GET_DIR_FAILED, | |
445 base::FilePath()); | |
446 return; | 338 return; |
447 } | 339 } |
448 | 340 |
449 std::string screenshot_basename = !screenshot_basename_for_test_.empty() ? | |
450 screenshot_basename_for_test_ : GetScreenshotBaseFilename(); | |
451 base::FilePath screenshot_path = | 341 base::FilePath screenshot_path = |
452 screenshot_directory.AppendASCII(screenshot_basename + ".png"); | 342 screenshot_directory.AppendASCII(GetScreenshotBaseFilename() + ".png"); |
453 GrabPartialWindowSnapshotAsync(window, rect, GetProfile(), screenshot_path); | 343 screenshot_taker_->TakeScreenshot(window, rect, screenshot_path); |
454 content::RecordAction(base::UserMetricsAction("Screenshot_TakePartial")); | 344 content::RecordAction(base::UserMetricsAction("Screenshot_TakePartial")); |
455 } | 345 } |
456 | 346 |
457 bool ScreenshotTaker::CanTakeScreenshot() { | 347 bool ChromeScreenshotTaker::CanTakeScreenshot() { |
458 return last_screenshot_timestamp_.is_null() || | 348 return screenshot_taker_->CanTakeScreenshot(); |
459 base::Time::Now() - last_screenshot_timestamp_ > | 349 } |
460 base::TimeDelta::FromMilliseconds( | 350 |
461 kScreenshotMinimumIntervalInMS); | 351 void ChromeScreenshotTaker::PrepareWritableFileAndRunOnBlockingPool( |
352 const base::FilePath& path, | |
353 WritableFileCallback callback) { | |
354 #if defined(OS_CHROMEOS) | |
355 Profile* profile = ProfileManager::GetActiveUserProfile(); | |
356 if (drive::util::IsUnderDriveMountPoint(path)) { | |
357 drive::util::EnsureDirectoryExists( | |
358 profile, | |
359 path.DirName(), | |
360 base::Bind(&EnsureDirectoryExistsCallback, | |
361 callback, | |
362 profile, | |
363 path)); | |
364 return; | |
365 } | |
366 #endif | |
367 ScreenshotTakerClient::PrepareWritableFileAndRunOnBlockingPool( | |
368 path, callback); | |
369 } | |
370 | |
371 void ChromeScreenshotTaker::OnScreenshotCompleted( | |
372 ScreenshotTakerObserver::Result result, | |
373 const base::FilePath& screenshot_path) { | |
374 #if defined(OS_CHROMEOS) | |
375 // Do not show a notification that a screenshot was taken while no user is | |
376 // logged in, since it is confusing for the user to get a message about it | |
377 // after he logs in (crbug.com/235217). | |
378 if (!chromeos::LoginState::Get()->IsUserLoggedIn()) | |
379 return; | |
380 | |
381 // TODO(sschmitz): make this work for Windows. | |
382 Profile* profile = ProfileManager::GetActiveUserProfile(); | |
383 DesktopNotificationService* const service = | |
384 DesktopNotificationServiceFactory::GetForProfile(profile); | |
385 if (service->IsNotifierEnabled(message_center::NotifierId( | |
386 message_center::NotifierId::SYSTEM_COMPONENT, | |
387 ash::system_notifier::kNotifierScreenshot))) { | |
388 scoped_ptr<Notification> notification( | |
389 CreateNotification(result, screenshot_path)); | |
390 g_browser_process->notification_ui_manager()->Add(*notification, profile); | |
391 } | |
392 #endif | |
462 } | 393 } |
463 | 394 |
464 #if defined(OS_CHROMEOS) | 395 #if defined(OS_CHROMEOS) |
465 Notification* ScreenshotTaker::CreateNotification( | 396 Notification* ChromeScreenshotTaker::CreateNotification( |
466 ScreenshotTakerObserver::Result screenshot_result, | 397 ScreenshotTakerObserver::Result screenshot_result, |
467 const base::FilePath& screenshot_path) { | 398 const base::FilePath& screenshot_path) { |
468 const std::string notification_id(kNotificationId); | 399 const std::string notification_id(kNotificationId); |
469 // We cancel a previous screenshot notification, if any, to ensure we get | 400 // We cancel a previous screenshot notification, if any, to ensure we get |
470 // a fresh notification pop-up. | 401 // a fresh notification pop-up. |
402 Profile* profile = ProfileManager::GetActiveUserProfile(); | |
471 g_browser_process->notification_ui_manager()->CancelById( | 403 g_browser_process->notification_ui_manager()->CancelById( |
472 notification_id, NotificationUIManager::GetProfileID(GetProfile())); | 404 notification_id, NotificationUIManager::GetProfileID(profile)); |
473 const base::string16 replace_id(base::UTF8ToUTF16(notification_id)); | 405 const base::string16 replace_id(base::UTF8ToUTF16(notification_id)); |
474 bool success = | 406 bool success = |
475 (screenshot_result == ScreenshotTakerObserver::SCREENSHOT_SUCCESS); | 407 (screenshot_result == ScreenshotTakerObserver::SCREENSHOT_SUCCESS); |
476 message_center::RichNotificationData optional_field; | 408 message_center::RichNotificationData optional_field; |
477 if (success) { | 409 if (success) { |
478 const base::string16 label = l10n_util::GetStringUTF16( | 410 const base::string16 label = l10n_util::GetStringUTF16( |
479 IDS_MESSAGE_CENTER_NOTIFICATION_BUTTON_COPY_SCREENSHOT_TO_CLIPBOARD); | 411 IDS_MESSAGE_CENTER_NOTIFICATION_BUTTON_COPY_SCREENSHOT_TO_CLIPBOARD); |
480 optional_field.buttons.push_back(message_center::ButtonInfo(label)); | 412 optional_field.buttons.push_back(message_center::ButtonInfo(label)); |
481 } | 413 } |
482 return new Notification( | 414 return new Notification( |
483 message_center::NOTIFICATION_TYPE_SIMPLE, | 415 message_center::NOTIFICATION_TYPE_SIMPLE, |
484 GURL(kNotificationOriginUrl), | 416 GURL(kNotificationOriginUrl), |
485 l10n_util::GetStringUTF16( | 417 l10n_util::GetStringUTF16( |
486 GetScreenshotNotificationTitle(screenshot_result)), | 418 GetScreenshotNotificationTitle(screenshot_result)), |
487 l10n_util::GetStringUTF16( | 419 l10n_util::GetStringUTF16( |
488 GetScreenshotNotificationText(screenshot_result)), | 420 GetScreenshotNotificationText(screenshot_result)), |
489 ui::ResourceBundle::GetSharedInstance().GetImageNamed( | 421 ui::ResourceBundle::GetSharedInstance().GetImageNamed( |
490 IDR_SCREENSHOT_NOTIFICATION_ICON), | 422 IDR_SCREENSHOT_NOTIFICATION_ICON), |
491 blink::WebTextDirectionDefault, | 423 blink::WebTextDirectionDefault, |
492 message_center::NotifierId( | 424 message_center::NotifierId( |
493 message_center::NotifierId::SYSTEM_COMPONENT, | 425 message_center::NotifierId::SYSTEM_COMPONENT, |
494 ash::system_notifier::kNotifierScreenshot), | 426 ash::system_notifier::kNotifierScreenshot), |
495 l10n_util::GetStringUTF16(IDS_MESSAGE_CENTER_NOTIFIER_SCREENSHOT_NAME), | 427 l10n_util::GetStringUTF16(IDS_MESSAGE_CENTER_NOTIFIER_SCREENSHOT_NAME), |
496 replace_id, | 428 replace_id, |
497 optional_field, | 429 optional_field, |
498 new ScreenshotTakerNotificationDelegate( | 430 new ScreenshotTakerNotificationDelegate( |
499 success, GetProfile(), screenshot_path)); | 431 success, profile, screenshot_path)); |
500 } | 432 } |
501 #endif | 433 #endif |
502 | |
503 void ScreenshotTaker::ShowNotification( | |
504 ScreenshotTakerObserver::Result screenshot_result, | |
505 const base::FilePath& screenshot_path) { | |
506 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
507 #if defined(OS_CHROMEOS) | |
508 // Do not show a notification that a screenshot was taken while no user is | |
509 // logged in, since it is confusing for the user to get a message about it | |
510 // after he logs in (crbug.com/235217). | |
511 if (!chromeos::LoginState::Get()->IsUserLoggedIn()) | |
512 return; | |
513 | |
514 // TODO(sschmitz): make this work for Windows. | |
515 DesktopNotificationService* const service = | |
516 DesktopNotificationServiceFactory::GetForProfile(GetProfile()); | |
517 if (service->IsNotifierEnabled(message_center::NotifierId( | |
518 message_center::NotifierId::SYSTEM_COMPONENT, | |
519 ash::system_notifier::kNotifierScreenshot))) { | |
520 scoped_ptr<Notification> notification( | |
521 CreateNotification(screenshot_result, screenshot_path)); | |
522 g_browser_process->notification_ui_manager()->Add(*notification, | |
523 GetProfile()); | |
524 } | |
525 #endif | |
526 FOR_EACH_OBSERVER(ScreenshotTakerObserver, observers_, | |
527 OnScreenshotCompleted(screenshot_result, screenshot_path)); | |
528 } | |
529 | |
530 void ScreenshotTaker::AddObserver(ScreenshotTakerObserver* observer) { | |
531 observers_.AddObserver(observer); | |
532 } | |
533 | |
534 void ScreenshotTaker::RemoveObserver(ScreenshotTakerObserver* observer) { | |
535 observers_.RemoveObserver(observer); | |
536 } | |
537 | |
538 bool ScreenshotTaker::HasObserver( | |
539 const ScreenshotTakerObserver* observer) const { | |
540 return observers_.HasObserver(observer); | |
541 } | |
542 | |
543 void ScreenshotTaker::GrabWindowSnapshotAsyncCallback( | |
544 base::FilePath screenshot_path, | |
545 bool is_partial, | |
546 int window_idx, | |
547 scoped_refptr<base::RefCountedBytes> png_data) { | |
548 if (!png_data.get()) { | |
549 if (is_partial) { | |
550 LOG(ERROR) << "Failed to grab the window screenshot"; | |
551 ShowNotification( | |
552 ScreenshotTakerObserver::SCREENSHOT_GRABWINDOW_PARTIAL_FAILED, | |
553 screenshot_path); | |
554 } else { | |
555 LOG(ERROR) << "Failed to grab the window screenshot for " << window_idx; | |
556 ShowNotification( | |
557 ScreenshotTakerObserver::SCREENSHOT_GRABWINDOW_FULL_FAILED, | |
558 screenshot_path); | |
559 } | |
560 return; | |
561 } | |
562 | |
563 PostSaveScreenshotTask( | |
564 base::Bind(&ScreenshotTaker::ShowNotification, factory_.GetWeakPtr()), | |
565 GetProfile(), | |
566 screenshot_path, | |
567 png_data); | |
568 } | |
569 | |
570 void ScreenshotTaker::GrabPartialWindowSnapshotAsync( | |
571 aura::Window* window, | |
572 const gfx::Rect& snapshot_bounds, | |
573 Profile* profile, | |
574 base::FilePath screenshot_path) { | |
575 last_screenshot_timestamp_ = base::Time::Now(); | |
576 | |
577 bool is_partial = true; | |
578 int window_idx = -1; // unused | |
579 ui::GrabWindowSnapshotAsync( | |
580 window, | |
581 snapshot_bounds, | |
582 content::BrowserThread::GetBlockingPool(), | |
583 base::Bind(&ScreenshotTaker::GrabWindowSnapshotAsyncCallback, | |
584 factory_.GetWeakPtr(), | |
585 screenshot_path, | |
586 is_partial, | |
587 window_idx)); | |
588 } | |
589 | |
590 void ScreenshotTaker::GrabFullWindowSnapshotAsync( | |
591 aura::Window* window, | |
592 const gfx::Rect& snapshot_bounds, | |
593 Profile* profile, | |
594 base::FilePath screenshot_path, | |
595 int window_idx) { | |
596 last_screenshot_timestamp_ = base::Time::Now(); | |
597 | |
598 bool is_partial = false; | |
599 ui::GrabWindowSnapshotAsync( | |
600 window, | |
601 snapshot_bounds, | |
602 content::BrowserThread::GetBlockingPool(), | |
603 base::Bind(&ScreenshotTaker::GrabWindowSnapshotAsyncCallback, | |
604 factory_.GetWeakPtr(), | |
605 screenshot_path, | |
606 is_partial, | |
607 window_idx)); | |
608 } | |
609 | |
610 Profile* ScreenshotTaker::GetProfile() { | |
611 if (profile_for_test_) | |
612 return profile_for_test_; | |
613 return ProfileManager::GetActiveUserProfile(); | |
614 } | |
615 | |
616 void ScreenshotTaker::SetScreenshotDirectoryForTest( | |
617 const base::FilePath& directory) { | |
618 screenshot_directory_for_test_ = directory; | |
619 } | |
620 | |
621 void ScreenshotTaker::SetScreenshotBasenameForTest( | |
622 const std::string& basename) { | |
623 screenshot_basename_for_test_ = basename; | |
624 } | |
625 | |
626 void ScreenshotTaker::SetScreenshotProfileForTest(Profile* profile) { | |
627 profile_for_test_ = profile; | |
628 } | |
OLD | NEW |