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" | 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_path.h" | 14 #include "base/file_path.h" |
15 #include "base/file_util.h" | 15 #include "base/file_util.h" |
16 #include "base/i18n/time_formatting.h" | |
17 #include "base/logging.h" | 16 #include "base/logging.h" |
18 #include "base/memory/ref_counted_memory.h" | 17 #include "base/memory/ref_counted_memory.h" |
19 #include "base/stringprintf.h" | 18 #include "base/stringprintf.h" |
20 #include "base/threading/sequenced_worker_pool.h" | 19 #include "base/threading/sequenced_worker_pool.h" |
21 #include "base/time.h" | 20 #include "base/time.h" |
22 #include "chrome/browser/browser_process.h" | |
23 #include "chrome/browser/download/download_prefs.h" | 21 #include "chrome/browser/download/download_prefs.h" |
24 #include "chrome/browser/prefs/pref_service.h" | |
25 #include "chrome/browser/profiles/profile.h" | 22 #include "chrome/browser/profiles/profile.h" |
26 #include "chrome/browser/profiles/profile_manager.h" | 23 #include "chrome/browser/profiles/profile_manager.h" |
| 24 #include "chrome/browser/ui/webui/screenshot_source.h" |
27 #include "chrome/browser/ui/window_snapshot/window_snapshot.h" | 25 #include "chrome/browser/ui/window_snapshot/window_snapshot.h" |
28 #include "chrome/common/pref_names.h" | |
29 #include "content/public/browser/browser_thread.h" | 26 #include "content/public/browser/browser_thread.h" |
30 #include "ui/aura/root_window.h" | 27 #include "ui/aura/root_window.h" |
31 #include "ui/aura/window.h" | 28 #include "ui/aura/window.h" |
32 | 29 |
33 #if defined(OS_CHROMEOS) | 30 #if defined(OS_CHROMEOS) |
34 #include "chrome/browser/chromeos/gdata/drive_file_system_util.h" | 31 #include "chrome/browser/chromeos/gdata/drive_file_system_util.h" |
35 #include "chrome/browser/chromeos/login/user_manager.h" | 32 #include "chrome/browser/chromeos/login/user_manager.h" |
36 #endif | 33 #endif |
37 | 34 |
38 namespace { | 35 namespace { |
39 // How opaque should the layer that we flash onscreen to provide visual | 36 // How opaque should the layer that we flash onscreen to provide visual |
40 // feedback after the screenshot is taken be? | 37 // feedback after the screenshot is taken be? |
41 const float kVisualFeedbackLayerOpacity = 0.25f; | 38 const float kVisualFeedbackLayerOpacity = 0.25f; |
42 | 39 |
43 // How long should the visual feedback layer be displayed? | 40 // How long should the visual feedback layer be displayed? |
44 const int64 kVisualFeedbackLayerDisplayTimeMs = 100; | 41 const int64 kVisualFeedbackLayerDisplayTimeMs = 100; |
45 | 42 |
46 // The minimum interval between two screenshot commands. It has to be | 43 // The minimum interval between two screenshot commands. It has to be |
47 // more than 1000 to prevent the conflict of filenames. | 44 // more than 1000 to prevent the conflict of filenames. |
48 const int kScreenshotMinimumIntervalInMS = 1000; | 45 const int kScreenshotMinimumIntervalInMS = 1000; |
49 | 46 |
50 bool ShouldUse24HourClock() { | |
51 #if defined(OS_CHROMEOS) | |
52 Profile* profile = ProfileManager::GetDefaultProfileOrOffTheRecord(); | |
53 if (profile) { | |
54 PrefService* pref_service = profile->GetPrefs(); | |
55 if (pref_service) { | |
56 return pref_service->GetBoolean(prefs::kUse24HourClock); | |
57 } | |
58 } | |
59 #endif | |
60 return base::GetHourClockType() == base::k24HourClock; | |
61 } | |
62 | |
63 bool AreScreenshotsDisabled() { | |
64 return g_browser_process->local_state()->GetBoolean( | |
65 prefs::kDisableScreenshots); | |
66 } | |
67 | |
68 std::string GetScreenshotBaseFilename() { | |
69 base::Time::Exploded now; | |
70 base::Time::Now().LocalExplode(&now); | |
71 | |
72 // We don't use base/i18n/time_formatting.h here because it doesn't | |
73 // support our format. Don't use ICU either to avoid i18n file names | |
74 // for non-English locales. | |
75 // TODO(mukai): integrate this logic somewhere time_formatting.h | |
76 std::string file_name = base::StringPrintf( | |
77 "Screenshot %d-%02d-%02d at ", now.year, now.month, now.day_of_month); | |
78 | |
79 if (ShouldUse24HourClock()) { | |
80 file_name.append(base::StringPrintf( | |
81 "%02d.%02d.%02d", now.hour, now.minute, now.second)); | |
82 } else { | |
83 int hour = now.hour; | |
84 if (hour > 12) { | |
85 hour -= 12; | |
86 } else if (hour == 0) { | |
87 hour = 12; | |
88 } | |
89 file_name.append(base::StringPrintf( | |
90 "%d.%02d.%02d ", hour, now.minute, now.second)); | |
91 file_name.append((now.hour >= 12) ? "PM" : "AM"); | |
92 } | |
93 | |
94 return file_name; | |
95 } | |
96 | |
97 bool GetScreenshotDirectory(FilePath* directory) { | |
98 if (AreScreenshotsDisabled()) | |
99 return false; | |
100 | |
101 bool is_logged_in = true; | |
102 #if defined(OS_CHROMEOS) | |
103 is_logged_in = chromeos::UserManager::Get()->IsUserLoggedIn(); | |
104 #endif | |
105 | |
106 if (is_logged_in) { | |
107 DownloadPrefs* download_prefs = DownloadPrefs::FromBrowserContext( | |
108 ash::Shell::GetInstance()->delegate()->GetCurrentBrowserContext()); | |
109 *directory = download_prefs->DownloadPath(); | |
110 } else { | |
111 if (!file_util::GetTempDir(directory)) { | |
112 LOG(ERROR) << "Failed to find temporary directory."; | |
113 return false; | |
114 } | |
115 } | |
116 return true; | |
117 } | |
118 | 47 |
119 void SaveScreenshotInternal(const FilePath& screenshot_path, | 48 void SaveScreenshotInternal(const FilePath& screenshot_path, |
120 scoped_refptr<base::RefCountedBytes> png_data) { | 49 scoped_refptr<base::RefCountedBytes> png_data) { |
121 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); | 50 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); |
122 DCHECK(!screenshot_path.empty()); | 51 DCHECK(!screenshot_path.empty()); |
123 if (static_cast<size_t>(file_util::WriteFile( | 52 if (static_cast<size_t>(file_util::WriteFile( |
124 screenshot_path, | 53 screenshot_path, |
125 reinterpret_cast<char*>(&(png_data->data()[0])), | 54 reinterpret_cast<char*>(&(png_data->data()[0])), |
126 png_data->size())) != png_data->size()) { | 55 png_data->size())) != png_data->size()) { |
127 LOG(ERROR) << "Failed to save to " << screenshot_path.value(); | 56 LOG(ERROR) << "Failed to save to " << screenshot_path.value(); |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
178 FROM_HERE, base::Bind(&SaveScreenshot, screenshot_path, png_data)); | 107 FROM_HERE, base::Bind(&SaveScreenshot, screenshot_path, png_data)); |
179 } | 108 } |
180 #endif | 109 #endif |
181 | 110 |
182 bool GrabWindowSnapshot(aura::Window* window, | 111 bool GrabWindowSnapshot(aura::Window* window, |
183 const gfx::Rect& snapshot_bounds, | 112 const gfx::Rect& snapshot_bounds, |
184 std::vector<unsigned char>* png_data) { | 113 std::vector<unsigned char>* png_data) { |
185 #if defined(OS_LINUX) | 114 #if defined(OS_LINUX) |
186 // chrome::GrabWindowSnapshotForUser checks this too, but | 115 // chrome::GrabWindowSnapshotForUser checks this too, but |
187 // RootWindow::GrabSnapshot does not. | 116 // RootWindow::GrabSnapshot does not. |
188 if (AreScreenshotsDisabled()) | 117 if (ScreenshotSource::AreScreenshotsDisabled()) |
189 return false; | 118 return false; |
190 | 119 |
191 // We use XGetImage() for Linux/ChromeOS for performance reasons. | 120 // We use XGetImage() for Linux/ChromeOS for performance reasons. |
192 // See crbug.com/119492 | 121 // See crbug.com/119492 |
193 // TODO(mukai): remove this when the performance issue has been fixed. | 122 // TODO(mukai): remove this when the performance issue has been fixed. |
194 if (window->GetRootWindow()->GrabSnapshot(snapshot_bounds, png_data)) | 123 if (window->GetRootWindow()->GrabSnapshot(snapshot_bounds, png_data)) |
195 return true; | 124 return true; |
196 #endif // OS_LINUX | 125 #endif // OS_LINUX |
197 | 126 |
198 return chrome::GrabWindowSnapshotForUser(window, png_data, snapshot_bounds); | 127 return chrome::GrabWindowSnapshotForUser(window, png_data, snapshot_bounds); |
199 } | 128 } |
200 | 129 |
201 } // namespace | 130 } // namespace |
202 | 131 |
203 ScreenshotTaker::ScreenshotTaker() { | 132 ScreenshotTaker::ScreenshotTaker() { |
204 } | 133 } |
205 | 134 |
206 ScreenshotTaker::~ScreenshotTaker() { | 135 ScreenshotTaker::~ScreenshotTaker() { |
207 } | 136 } |
208 | 137 |
209 void ScreenshotTaker::HandleTakeScreenshotForAllRootWindows() { | 138 void ScreenshotTaker::HandleTakeScreenshotForAllRootWindows() { |
210 FilePath screenshot_directory; | 139 FilePath screenshot_directory; |
211 if (!GetScreenshotDirectory(&screenshot_directory)) | 140 if (!ScreenshotSource::GetScreenshotDirectory(&screenshot_directory)) |
212 return; | 141 return; |
213 | 142 |
214 std::string screenshot_basename = GetScreenshotBaseFilename(); | 143 std::string screenshot_basename = |
| 144 ScreenshotSource::GetScreenshotBaseFilename(); |
215 ash::Shell::RootWindowList root_windows = ash::Shell::GetAllRootWindows(); | 145 ash::Shell::RootWindowList root_windows = ash::Shell::GetAllRootWindows(); |
216 for (size_t i = 0; i < root_windows.size(); ++i) { | 146 for (size_t i = 0; i < root_windows.size(); ++i) { |
217 aura::RootWindow* root_window = root_windows[i]; | 147 aura::RootWindow* root_window = root_windows[i]; |
218 scoped_refptr<base::RefCountedBytes> png_data(new base::RefCountedBytes); | 148 scoped_refptr<base::RefCountedBytes> png_data(new base::RefCountedBytes); |
219 std::string basename = screenshot_basename; | 149 std::string basename = screenshot_basename; |
220 gfx::Rect rect = root_window->bounds(); | 150 gfx::Rect rect = root_window->bounds(); |
221 if (root_windows.size() > 1) | 151 if (root_windows.size() > 1) |
222 basename += base::StringPrintf(" - Display %d", static_cast<int>(i + 1)); | 152 basename += base::StringPrintf(" - Display %d", static_cast<int>(i + 1)); |
223 if (GrabWindowSnapshot(root_window, rect, &png_data->data())) { | 153 if (GrabWindowSnapshot(root_window, rect, &png_data->data())) { |
224 DisplayVisualFeedback(rect); | 154 DisplayVisualFeedback(rect); |
225 PostSaveScreenshotTask( | 155 PostSaveScreenshotTask( |
226 screenshot_directory.AppendASCII(basename + ".png"), png_data); | 156 screenshot_directory.AppendASCII(basename + ".png"), png_data); |
227 } else { | 157 } else { |
228 LOG(ERROR) << "Failed to grab the window screenshot for " << i; | 158 LOG(ERROR) << "Failed to grab the window screenshot for " << i; |
229 } | 159 } |
230 } | 160 } |
231 last_screenshot_timestamp_ = base::Time::Now(); | 161 last_screenshot_timestamp_ = base::Time::Now(); |
232 } | 162 } |
233 | 163 |
234 void ScreenshotTaker::HandleTakePartialScreenshot( | 164 void ScreenshotTaker::HandleTakePartialScreenshot( |
235 aura::Window* window, const gfx::Rect& rect) { | 165 aura::Window* window, const gfx::Rect& rect) { |
236 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 166 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
237 | 167 |
238 FilePath screenshot_directory; | 168 FilePath screenshot_directory; |
239 if (!GetScreenshotDirectory(&screenshot_directory)) | 169 if (!ScreenshotSource::GetScreenshotDirectory(&screenshot_directory)) |
240 return; | 170 return; |
241 | 171 |
242 scoped_refptr<base::RefCountedBytes> png_data(new base::RefCountedBytes); | 172 scoped_refptr<base::RefCountedBytes> png_data(new base::RefCountedBytes); |
243 | 173 |
244 if (GrabWindowSnapshot(window, rect, &png_data->data())) { | 174 if (GrabWindowSnapshot(window, rect, &png_data->data())) { |
245 last_screenshot_timestamp_ = base::Time::Now(); | 175 last_screenshot_timestamp_ = base::Time::Now(); |
246 DisplayVisualFeedback(rect); | 176 DisplayVisualFeedback(rect); |
247 PostSaveScreenshotTask( | 177 PostSaveScreenshotTask( |
248 screenshot_directory.AppendASCII(GetScreenshotBaseFilename() + ".png"), | 178 screenshot_directory.AppendASCII( |
249 png_data); | 179 ScreenshotSource::GetScreenshotBaseFilename() + ".png"), |
| 180 png_data); |
250 } else { | 181 } else { |
251 LOG(ERROR) << "Failed to grab the window screenshot"; | 182 LOG(ERROR) << "Failed to grab the window screenshot"; |
252 } | 183 } |
253 } | 184 } |
254 | 185 |
255 bool ScreenshotTaker::CanTakeScreenshot() { | 186 bool ScreenshotTaker::CanTakeScreenshot() { |
256 return last_screenshot_timestamp_.is_null() || | 187 return last_screenshot_timestamp_.is_null() || |
257 base::Time::Now() - last_screenshot_timestamp_ > | 188 base::Time::Now() - last_screenshot_timestamp_ > |
258 base::TimeDelta::FromMilliseconds( | 189 base::TimeDelta::FromMilliseconds( |
259 kScreenshotMinimumIntervalInMS); | 190 kScreenshotMinimumIntervalInMS); |
(...skipping 14 matching lines...) Expand all Loading... |
274 ash::internal::kShellWindowId_OverlayContainer)->layer(); | 205 ash::internal::kShellWindowId_OverlayContainer)->layer(); |
275 parent->Add(visual_feedback_layer_.get()); | 206 parent->Add(visual_feedback_layer_.get()); |
276 visual_feedback_layer_->SetVisible(true); | 207 visual_feedback_layer_->SetVisible(true); |
277 | 208 |
278 MessageLoopForUI::current()->PostDelayedTask( | 209 MessageLoopForUI::current()->PostDelayedTask( |
279 FROM_HERE, | 210 FROM_HERE, |
280 base::Bind(&ScreenshotTaker::CloseVisualFeedbackLayer, | 211 base::Bind(&ScreenshotTaker::CloseVisualFeedbackLayer, |
281 base::Unretained(this)), | 212 base::Unretained(this)), |
282 base::TimeDelta::FromMilliseconds(kVisualFeedbackLayerDisplayTimeMs)); | 213 base::TimeDelta::FromMilliseconds(kVisualFeedbackLayerDisplayTimeMs)); |
283 } | 214 } |
OLD | NEW |