| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/ui/views/aura/screenshot_taker.h" | |
| 6 | |
| 7 #include <string> | |
| 8 | |
| 9 #include "base/bind.h" | |
| 10 #include "base/file_path.h" | |
| 11 #include "base/file_util.h" | |
| 12 #include "base/logging.h" | |
| 13 #include "base/memory/ref_counted_memory.h" | |
| 14 #include "base/stringprintf.h" | |
| 15 #include "base/time.h" | |
| 16 #include "chrome/browser/download/download_util.h" | |
| 17 #include "chrome/browser/ui/window_snapshot/window_snapshot.h" | |
| 18 #include "content/public/browser/browser_thread.h" | |
| 19 #include "ui/aura/window.h" | |
| 20 | |
| 21 #if defined(OS_CHROMEOS) | |
| 22 #include "chrome/browser/chromeos/login/user_manager.h" | |
| 23 #endif | |
| 24 | |
| 25 namespace { | |
| 26 std::string GetScreenshotFileName() { | |
| 27 base::Time::Exploded now; | |
| 28 base::Time::Now().LocalExplode(&now); | |
| 29 | |
| 30 return base::StringPrintf("screenshot-%d%02d%02d-%02d%02d%02d.png", | |
| 31 now.year, now.month, now.day_of_month, | |
| 32 now.hour, now.minute, now.second); | |
| 33 } | |
| 34 | |
| 35 // |is_logged_in| is used only for ChromeOS. Otherwise it is always true. | |
| 36 void SaveScreenshot(bool is_logged_in, | |
| 37 scoped_refptr<RefCountedBytes> png_data) { | |
| 38 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE)); | |
| 39 | |
| 40 std::string screenshot_filename = GetScreenshotFileName(); | |
| 41 FilePath screenshot_path; | |
| 42 if (is_logged_in) { | |
| 43 screenshot_path = download_util::GetDefaultDownloadDirectory().AppendASCII( | |
| 44 screenshot_filename); | |
| 45 } else { | |
| 46 file_util::CreateTemporaryFile(&screenshot_path); | |
| 47 } | |
| 48 | |
| 49 if (static_cast<size_t>(file_util::WriteFile( | |
| 50 screenshot_path, | |
| 51 reinterpret_cast<char*>(&(png_data->data()[0])), | |
| 52 png_data->size())) == png_data->size()) { | |
| 53 if (!is_logged_in) { | |
| 54 // We created a temporary file without .png suffix. Rename it | |
| 55 // here. | |
| 56 FilePath real_path = screenshot_path.DirName().AppendASCII( | |
| 57 screenshot_filename); | |
| 58 if (!file_util::ReplaceFile(screenshot_path, real_path)) { | |
| 59 LOG(ERROR) << "Failed to rename the file to " << real_path.value(); | |
| 60 } | |
| 61 } | |
| 62 } else { | |
| 63 LOG(ERROR) << "Failed to save to " << screenshot_path.value(); | |
| 64 } | |
| 65 } | |
| 66 | |
| 67 } // namespace | |
| 68 | |
| 69 ScreenshotTaker::ScreenshotTaker() { | |
| 70 } | |
| 71 | |
| 72 void ScreenshotTaker::HandleTakePartialScreenshot( | |
| 73 aura::Window* window, const gfx::Rect& rect) { | |
| 74 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
| 75 | |
| 76 scoped_refptr<RefCountedBytes> png_data(new RefCountedBytes); | |
| 77 | |
| 78 bool is_logged_in = true; | |
| 79 #if defined(OS_CHROMEOS) | |
| 80 is_logged_in = chromeos::UserManager::Get()->user_is_logged_in(); | |
| 81 #endif | |
| 82 | |
| 83 if (browser::GrabWindowSnapshot(window, &png_data->data(), rect)) { | |
| 84 content::BrowserThread::PostTask( | |
| 85 content::BrowserThread::FILE, FROM_HERE, | |
| 86 base::Bind(&SaveScreenshot, is_logged_in, png_data)); | |
| 87 } else { | |
| 88 LOG(ERROR) << "Failed to grab the window screenshot"; | |
| 89 } | |
| 90 } | |
| 91 | |
| 92 void ScreenshotTaker::HandleTakeScreenshot(aura::Window* window) { | |
| 93 HandleTakePartialScreenshot(window, window->bounds()); | |
| 94 } | |
| OLD | NEW |