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/views/aura/screenshot_taker.h" | 5 #include "chrome/browser/ui/views/aura/screenshot_taker.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/file_path.h" | 10 #include "base/file_path.h" |
11 #include "base/file_util.h" | 11 #include "base/file_util.h" |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
13 #include "base/memory/ref_counted_memory.h" | 13 #include "base/memory/ref_counted_memory.h" |
14 #include "base/stringprintf.h" | 14 #include "base/stringprintf.h" |
15 #include "base/time.h" | 15 #include "base/time.h" |
16 #include "ash/shell.h" | |
Daniel Erat
2012/02/22 17:22:06
alphabetize these
Jun Mukai
2012/02/23 03:28:58
Done.
| |
17 #include "ash/shell_window_ids.h" | |
16 #include "chrome/browser/download/download_util.h" | 18 #include "chrome/browser/download/download_util.h" |
17 #include "chrome/browser/ui/window_snapshot/window_snapshot.h" | 19 #include "chrome/browser/ui/window_snapshot/window_snapshot.h" |
18 #include "content/public/browser/browser_thread.h" | 20 #include "content/public/browser/browser_thread.h" |
19 #include "ui/aura/window.h" | 21 #include "ui/aura/window.h" |
22 #include "ui/gfx/canvas.h" | |
23 #include "ui/views/widget/widget.h" | |
24 #include "ui/views/widget/widget_delegate.h" | |
20 | 25 |
21 #if defined(OS_CHROMEOS) | 26 #if defined(OS_CHROMEOS) |
22 #include "chrome/browser/chromeos/login/user_manager.h" | 27 #include "chrome/browser/chromeos/login/user_manager.h" |
23 #endif | 28 #endif |
24 | 29 |
25 namespace { | 30 namespace { |
26 std::string GetScreenshotFileName() { | 31 std::string GetScreenshotFileName() { |
27 base::Time::Exploded now; | 32 base::Time::Exploded now; |
28 base::Time::Now().LocalExplode(&now); | 33 base::Time::Now().LocalExplode(&now); |
29 | 34 |
(...skipping 27 matching lines...) Expand all Loading... | |
57 screenshot_filename); | 62 screenshot_filename); |
58 if (!file_util::ReplaceFile(screenshot_path, real_path)) { | 63 if (!file_util::ReplaceFile(screenshot_path, real_path)) { |
59 LOG(ERROR) << "Failed to rename the file to " << real_path.value(); | 64 LOG(ERROR) << "Failed to rename the file to " << real_path.value(); |
60 } | 65 } |
61 } | 66 } |
62 } else { | 67 } else { |
63 LOG(ERROR) << "Failed to save to " << screenshot_path.value(); | 68 LOG(ERROR) << "Failed to save to " << screenshot_path.value(); |
64 } | 69 } |
65 } | 70 } |
66 | 71 |
72 // How opaque should the window that we flash onscreen to provide visual | |
73 // feedback after the screenshot is taken be? | |
74 const unsigned char kVisualFeedbackWindowOpacity = 64; | |
75 | |
76 // How long should the visual feedback window be displayed? | |
77 const uint64_t kVisualFeedbackWindowDisplayTimeMs = 100; | |
78 | |
79 // The view of making visual feedback for screenshot, i.e.: flashing the screen. | |
Daniel Erat
2012/02/22 17:22:06
// Flashes the screen to provide visual feedback t
Jun Mukai
2012/02/23 03:28:58
Done.
| |
80 class VisualFeedbackView : public views::WidgetDelegateView { | |
Daniel Erat
2012/02/22 17:22:06
not sure how much to care about this, but it'd be
Jun Mukai
2012/02/23 03:28:58
Done.
| |
81 public: | |
82 VisualFeedbackView(); | |
83 | |
84 private: | |
85 // Overridden from View: | |
86 virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE; | |
87 | |
88 DISALLOW_COPY_AND_ASSIGN(VisualFeedbackView); | |
89 }; | |
90 | |
91 VisualFeedbackView::VisualFeedbackView() { | |
Daniel Erat
2012/02/22 17:22:06
define this inline in the class declaration?
Jun Mukai
2012/02/23 03:28:58
removed the class itself and use ui::Layer instead
| |
92 } | |
93 | |
94 void VisualFeedbackView::OnPaint(gfx::Canvas* canvas) { | |
95 // All white. | |
Daniel Erat
2012/02/22 17:22:06
delete unnecessary comment
Jun Mukai
2012/02/23 03:28:58
removed the class itself.
| |
96 canvas->FillRect(bounds(), SK_ColorWHITE); | |
97 } | |
98 | |
99 void CleanupFeedbackView(aura::Window* window) { | |
100 DCHECK(window); | |
101 window->Hide(); | |
102 MessageLoopForUI::current()->DeleteSoon(FROM_HERE, window); | |
103 } | |
104 | |
105 // Makes the actual visual feedback, i.e.: flashing the screen. It's | |
106 // achieved by creating a transparent white window and removing it | |
107 // 100msec later. | |
108 void MakeVisualFeedback(const gfx::Rect& rect) { | |
Daniel Erat
2012/02/22 17:22:06
nit: s/MakeVisualFeedback/DisplayVisualFeedback/
Jun Mukai
2012/02/23 03:28:58
Done.
| |
109 views::Widget* widget = new views::Widget; | |
110 VisualFeedbackView* view = new VisualFeedbackView(); | |
111 | |
112 views::Widget::InitParams params( | |
113 views::Widget::InitParams::TYPE_WINDOW_FRAMELESS); | |
114 params.transparent = true; | |
115 params.delegate = view; | |
116 params.parent = ash::Shell::GetInstance()->GetContainer( | |
117 ash::internal::kShellWindowId_OverlayContainer); | |
118 | |
119 widget->Init(params); | |
120 widget->SetContentsView(view); | |
121 widget->SetBounds(rect); | |
122 widget->GetNativeView()->SetName("ScreenshotVisualFeedback"); | |
123 widget->SetOpacity(kVisualFeedbackWindowOpacity); | |
124 widget->StackAtTop(); | |
125 widget->Show(); | |
126 | |
127 MessageLoopForUI::current()->PostDelayedTask( | |
128 FROM_HERE, | |
129 base::Bind(&CleanupFeedbackView, | |
130 base::Unretained(widget->GetNativeView())), | |
131 kVisualFeedbackWindowDisplayTimeMs); | |
132 } | |
133 | |
67 } // namespace | 134 } // namespace |
68 | 135 |
69 ScreenshotTaker::ScreenshotTaker() { | 136 ScreenshotTaker::ScreenshotTaker() { |
70 } | 137 } |
71 | 138 |
72 void ScreenshotTaker::HandleTakePartialScreenshot( | 139 void ScreenshotTaker::HandleTakePartialScreenshot( |
73 aura::Window* window, const gfx::Rect& rect) { | 140 aura::Window* window, const gfx::Rect& rect) { |
74 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 141 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
75 | 142 |
76 scoped_refptr<RefCountedBytes> png_data(new RefCountedBytes); | 143 scoped_refptr<RefCountedBytes> png_data(new RefCountedBytes); |
77 | 144 |
78 bool is_logged_in = true; | 145 bool is_logged_in = true; |
79 #if defined(OS_CHROMEOS) | 146 #if defined(OS_CHROMEOS) |
80 is_logged_in = chromeos::UserManager::Get()->user_is_logged_in(); | 147 is_logged_in = chromeos::UserManager::Get()->user_is_logged_in(); |
81 #endif | 148 #endif |
82 | 149 |
83 if (browser::GrabWindowSnapshot(window, &png_data->data(), rect)) { | 150 if (browser::GrabWindowSnapshot(window, &png_data->data(), rect)) { |
151 MakeVisualFeedback(rect); | |
84 content::BrowserThread::PostTask( | 152 content::BrowserThread::PostTask( |
85 content::BrowserThread::FILE, FROM_HERE, | 153 content::BrowserThread::FILE, FROM_HERE, |
86 base::Bind(&SaveScreenshot, is_logged_in, png_data)); | 154 base::Bind(&SaveScreenshot, is_logged_in, png_data)); |
87 } else { | 155 } else { |
88 LOG(ERROR) << "Failed to grab the window screenshot"; | 156 LOG(ERROR) << "Failed to grab the window screenshot"; |
89 } | 157 } |
90 } | 158 } |
91 | 159 |
92 void ScreenshotTaker::HandleTakeScreenshot(aura::Window* window) { | 160 void ScreenshotTaker::HandleTakeScreenshot(aura::Window* window) { |
93 HandleTakePartialScreenshot(window, window->bounds()); | 161 HandleTakePartialScreenshot(window, window->bounds()); |
94 } | 162 } |
OLD | NEW |