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" |
(...skipping 15 matching lines...) Expand all Loading... |
26 #include "chrome/common/pref_names.h" | 26 #include "chrome/common/pref_names.h" |
27 #include "content/public/browser/browser_thread.h" | 27 #include "content/public/browser/browser_thread.h" |
28 #include "ui/aura/root_window.h" | 28 #include "ui/aura/root_window.h" |
29 #include "ui/aura/window.h" | 29 #include "ui/aura/window.h" |
30 | 30 |
31 #if defined(OS_CHROMEOS) | 31 #if defined(OS_CHROMEOS) |
32 #include "chrome/browser/chromeos/login/user_manager.h" | 32 #include "chrome/browser/chromeos/login/user_manager.h" |
33 #endif | 33 #endif |
34 | 34 |
35 namespace { | 35 namespace { |
| 36 const int kScreenshotMinimumIntervalInMS = 500; |
| 37 |
36 bool ShouldUse24HourClock() { | 38 bool ShouldUse24HourClock() { |
37 #if defined(OS_CHROMEOS) | 39 #if defined(OS_CHROMEOS) |
38 Profile* profile = ProfileManager::GetDefaultProfileOrOffTheRecord(); | 40 Profile* profile = ProfileManager::GetDefaultProfileOrOffTheRecord(); |
39 if (profile) { | 41 if (profile) { |
40 PrefService* pref_service = profile->GetPrefs(); | 42 PrefService* pref_service = profile->GetPrefs(); |
41 if (pref_service) { | 43 if (pref_service) { |
42 return pref_service->GetBoolean(prefs::kUse24HourClock); | 44 return pref_service->GetBoolean(prefs::kUse24HourClock); |
43 } | 45 } |
44 } | 46 } |
45 #endif | 47 #endif |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
131 const int64 kVisualFeedbackLayerDisplayTimeMs = 100; | 133 const int64 kVisualFeedbackLayerDisplayTimeMs = 100; |
132 | 134 |
133 } // namespace | 135 } // namespace |
134 | 136 |
135 ScreenshotTaker::ScreenshotTaker() { | 137 ScreenshotTaker::ScreenshotTaker() { |
136 } | 138 } |
137 | 139 |
138 ScreenshotTaker::~ScreenshotTaker() { | 140 ScreenshotTaker::~ScreenshotTaker() { |
139 } | 141 } |
140 | 142 |
| 143 void ScreenshotTaker::HandleTakeScreenshot(aura::Window* window) { |
| 144 HandleTakePartialScreenshot(window, window->bounds()); |
| 145 } |
| 146 |
141 void ScreenshotTaker::HandleTakePartialScreenshot( | 147 void ScreenshotTaker::HandleTakePartialScreenshot( |
142 aura::Window* window, const gfx::Rect& rect) { | 148 aura::Window* window, const gfx::Rect& rect) { |
143 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 149 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
144 | 150 |
145 scoped_refptr<base::RefCountedBytes> png_data(new base::RefCountedBytes); | 151 scoped_refptr<base::RefCountedBytes> png_data(new base::RefCountedBytes); |
146 | 152 |
147 bool is_logged_in = true; | 153 bool is_logged_in = true; |
148 #if defined(OS_CHROMEOS) | 154 #if defined(OS_CHROMEOS) |
149 is_logged_in = chromeos::UserManager::Get()->IsUserLoggedIn(); | 155 is_logged_in = chromeos::UserManager::Get()->IsUserLoggedIn(); |
150 #endif | 156 #endif |
151 | 157 |
152 FilePath screenshot_directory; | 158 FilePath screenshot_directory; |
153 if (is_logged_in) { | 159 if (is_logged_in) { |
154 DownloadPrefs* download_prefs = DownloadPrefs::FromBrowserContext( | 160 DownloadPrefs* download_prefs = DownloadPrefs::FromBrowserContext( |
155 ash::Shell::GetInstance()->delegate()->GetCurrentBrowserContext()); | 161 ash::Shell::GetInstance()->delegate()->GetCurrentBrowserContext()); |
156 screenshot_directory = download_prefs->DownloadPath(); | 162 screenshot_directory = download_prefs->DownloadPath(); |
157 } else { | 163 } else { |
158 if (!file_util::GetTempDir(&screenshot_directory)) { | 164 if (!file_util::GetTempDir(&screenshot_directory)) { |
159 LOG(ERROR) << "Failed to find temporary directory."; | 165 LOG(ERROR) << "Failed to find temporary directory."; |
160 return; | 166 return; |
161 } | 167 } |
162 } | 168 } |
163 | 169 |
164 bool use_24hour_clock = ShouldUse24HourClock(); | 170 bool use_24hour_clock = ShouldUse24HourClock(); |
165 | 171 |
166 if (GrabWindowSnapshot(window, rect, &png_data->data())) { | 172 if (GrabWindowSnapshot(window, rect, &png_data->data())) { |
| 173 last_screenshot_timestamp_ = base::Time::Now(); |
167 DisplayVisualFeedback(rect); | 174 DisplayVisualFeedback(rect); |
168 content::BrowserThread::PostTask( | 175 content::BrowserThread::PostTask( |
169 content::BrowserThread::FILE, FROM_HERE, | 176 content::BrowserThread::FILE, FROM_HERE, |
170 base::Bind(&SaveScreenshot, screenshot_directory, use_24hour_clock, | 177 base::Bind(&SaveScreenshot, screenshot_directory, use_24hour_clock, |
171 png_data)); | 178 png_data)); |
172 } else { | 179 } else { |
173 LOG(ERROR) << "Failed to grab the window screenshot"; | 180 LOG(ERROR) << "Failed to grab the window screenshot"; |
174 } | 181 } |
175 } | 182 } |
176 | 183 |
177 void ScreenshotTaker::HandleTakeScreenshot(aura::Window* window) { | 184 bool ScreenshotTaker::CanTakeScreenshot() { |
178 HandleTakePartialScreenshot(window, window->bounds()); | 185 return last_screenshot_timestamp_.is_null() || |
| 186 base::Time::Now() - last_screenshot_timestamp_ > |
| 187 base::TimeDelta::FromMilliseconds( |
| 188 kScreenshotMinimumIntervalInMS); |
179 } | 189 } |
180 | 190 |
181 void ScreenshotTaker::CloseVisualFeedbackLayer() { | 191 void ScreenshotTaker::CloseVisualFeedbackLayer() { |
182 visual_feedback_layer_.reset(); | 192 visual_feedback_layer_.reset(); |
183 } | 193 } |
184 | 194 |
185 void ScreenshotTaker::DisplayVisualFeedback(const gfx::Rect& rect) { | 195 void ScreenshotTaker::DisplayVisualFeedback(const gfx::Rect& rect) { |
186 visual_feedback_layer_.reset(new ui::Layer(ui::LAYER_SOLID_COLOR)); | 196 visual_feedback_layer_.reset(new ui::Layer(ui::LAYER_SOLID_COLOR)); |
187 visual_feedback_layer_->SetColor(SK_ColorWHITE); | 197 visual_feedback_layer_->SetColor(SK_ColorWHITE); |
188 visual_feedback_layer_->SetOpacity(kVisualFeedbackLayerOpacity); | 198 visual_feedback_layer_->SetOpacity(kVisualFeedbackLayerOpacity); |
189 visual_feedback_layer_->SetBounds(rect); | 199 visual_feedback_layer_->SetBounds(rect); |
190 | 200 |
191 ui::Layer* parent = ash::Shell::GetContainer( | 201 ui::Layer* parent = ash::Shell::GetContainer( |
192 ash::Shell::GetActiveRootWindow(), | 202 ash::Shell::GetActiveRootWindow(), |
193 ash::internal::kShellWindowId_OverlayContainer)->layer(); | 203 ash::internal::kShellWindowId_OverlayContainer)->layer(); |
194 parent->Add(visual_feedback_layer_.get()); | 204 parent->Add(visual_feedback_layer_.get()); |
195 visual_feedback_layer_->SetVisible(true); | 205 visual_feedback_layer_->SetVisible(true); |
196 | 206 |
197 MessageLoopForUI::current()->PostDelayedTask( | 207 MessageLoopForUI::current()->PostDelayedTask( |
198 FROM_HERE, | 208 FROM_HERE, |
199 base::Bind(&ScreenshotTaker::CloseVisualFeedbackLayer, | 209 base::Bind(&ScreenshotTaker::CloseVisualFeedbackLayer, |
200 base::Unretained(this)), | 210 base::Unretained(this)), |
201 base::TimeDelta::FromMilliseconds(kVisualFeedbackLayerDisplayTimeMs)); | 211 base::TimeDelta::FromMilliseconds(kVisualFeedbackLayerDisplayTimeMs)); |
202 } | 212 } |
OLD | NEW |