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/ash/screenshot_taker.h" | 5 #include "chrome/browser/ui/views/ash/screenshot_taker.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "ash/shell.h" | 9 #include "ash/shell.h" |
10 #include "ash/shell_window_ids.h" | 10 #include "ash/shell_window_ids.h" |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
59 screenshot_filename); | 59 screenshot_filename); |
60 if (!file_util::ReplaceFile(screenshot_path, real_path)) { | 60 if (!file_util::ReplaceFile(screenshot_path, real_path)) { |
61 LOG(ERROR) << "Failed to rename the file to " << real_path.value(); | 61 LOG(ERROR) << "Failed to rename the file to " << real_path.value(); |
62 } | 62 } |
63 } | 63 } |
64 } else { | 64 } else { |
65 LOG(ERROR) << "Failed to save to " << screenshot_path.value(); | 65 LOG(ERROR) << "Failed to save to " << screenshot_path.value(); |
66 } | 66 } |
67 } | 67 } |
68 | 68 |
69 // How opaque should the layer that we flash onscreen to provide visual | 69 // Actually takes the screenshot. |
70 // feedback after the screenshot is taken be? | 70 void TakeScreenshot(aura::Window* window, const gfx::Rect& rect) { |
71 const float kVisualFeedbackLayerOpacity = 0.25f; | |
72 | |
73 // How long should the visual feedback layer be displayed? | |
74 const int64 kVisualFeedbackLayerDisplayTimeMs = 100; | |
75 | |
76 } // namespace | |
77 | |
78 ScreenshotTaker::ScreenshotTaker() { | |
79 } | |
80 | |
81 ScreenshotTaker::~ScreenshotTaker() { | |
82 } | |
83 | |
84 void ScreenshotTaker::HandleTakePartialScreenshot( | |
85 aura::Window* window, const gfx::Rect& rect) { | |
86 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 71 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
87 | 72 |
88 scoped_refptr<RefCountedBytes> png_data(new RefCountedBytes); | 73 scoped_refptr<RefCountedBytes> png_data(new RefCountedBytes); |
89 | 74 |
90 bool is_logged_in = true; | 75 bool is_logged_in = true; |
91 #if defined(OS_CHROMEOS) | 76 #if defined(OS_CHROMEOS) |
92 is_logged_in = chromeos::UserManager::Get()->IsUserLoggedIn(); | 77 is_logged_in = chromeos::UserManager::Get()->IsUserLoggedIn(); |
93 #endif | 78 #endif |
94 | 79 |
95 if (browser::GrabWindowSnapshot(window, &png_data->data(), rect)) { | 80 if (browser::GrabWindowSnapshot(window, &png_data->data(), rect)) { |
96 DisplayVisualFeedback(rect); | |
97 content::BrowserThread::PostTask( | 81 content::BrowserThread::PostTask( |
98 content::BrowserThread::FILE, FROM_HERE, | 82 content::BrowserThread::FILE, FROM_HERE, |
99 base::Bind(&SaveScreenshot, is_logged_in, png_data)); | 83 base::Bind(&SaveScreenshot, is_logged_in, png_data)); |
100 } else { | 84 } else { |
101 LOG(ERROR) << "Failed to grab the window screenshot"; | 85 LOG(ERROR) << "Failed to grab the window screenshot"; |
102 } | 86 } |
103 } | 87 } |
104 | 88 |
| 89 // How opaque should the layer that we flash onscreen to provide visual |
| 90 // feedback after the screenshot is taken be? |
| 91 const float kVisualFeedbackLayerOpacity = 0.25f; |
| 92 |
| 93 // How long should the visual feedback layer be displayed? |
| 94 const int64 kVisualFeedbackLayerDisplayTimeMs = 100; |
| 95 |
| 96 } // namespace |
| 97 |
| 98 ScreenshotTaker::ScreenshotTaker() { |
| 99 } |
| 100 |
| 101 ScreenshotTaker::~ScreenshotTaker() { |
| 102 } |
| 103 |
| 104 void ScreenshotTaker::HandleTakePartialScreenshot(aura::Window* window, |
| 105 const gfx::Rect& rect) { |
| 106 // browser::GrabWindowSnapshot takes ~100msec and making visual feedback after |
| 107 // that leads noticeable delay. To prevent this delay, we just make the |
| 108 // visual effect first, and then run the actual task of taking screenshot. |
| 109 DisplayVisualFeedback( |
| 110 rect, |
| 111 base::Bind(&TakeScreenshot, base::Unretained(window), rect)); |
| 112 } |
| 113 |
105 void ScreenshotTaker::HandleTakeScreenshot(aura::Window* window) { | 114 void ScreenshotTaker::HandleTakeScreenshot(aura::Window* window) { |
106 HandleTakePartialScreenshot(window, window->bounds()); | 115 HandleTakePartialScreenshot(window, window->bounds()); |
107 } | 116 } |
108 | 117 |
109 void ScreenshotTaker::CloseVisualFeedbackLayer() { | 118 void ScreenshotTaker::CloseVisualFeedbackLayer(const base::Closure& task) { |
110 visual_feedback_layer_.reset(); | 119 visual_feedback_layer_.reset(); |
| 120 task.Run(); |
111 } | 121 } |
112 | 122 |
113 void ScreenshotTaker::DisplayVisualFeedback(const gfx::Rect& rect) { | 123 void ScreenshotTaker::DisplayVisualFeedback(const gfx::Rect& rect, |
| 124 const base::Closure& task) { |
114 visual_feedback_layer_.reset(new ui::Layer(ui::Layer::LAYER_SOLID_COLOR)); | 125 visual_feedback_layer_.reset(new ui::Layer(ui::Layer::LAYER_SOLID_COLOR)); |
115 visual_feedback_layer_->SetColor(SK_ColorWHITE); | 126 visual_feedback_layer_->SetColor(SK_ColorWHITE); |
116 visual_feedback_layer_->SetOpacity(kVisualFeedbackLayerOpacity); | 127 visual_feedback_layer_->SetOpacity(kVisualFeedbackLayerOpacity); |
117 visual_feedback_layer_->SetBounds(rect); | 128 visual_feedback_layer_->SetBounds(rect); |
118 | 129 |
119 ui::Layer* parent = ash::Shell::GetInstance()->GetContainer( | 130 ui::Layer* parent = ash::Shell::GetInstance()->GetContainer( |
120 ash::internal::kShellWindowId_OverlayContainer)->layer(); | 131 ash::internal::kShellWindowId_OverlayContainer)->layer(); |
121 parent->Add(visual_feedback_layer_.get()); | 132 parent->Add(visual_feedback_layer_.get()); |
122 visual_feedback_layer_->SetVisible(true); | 133 visual_feedback_layer_->SetVisible(true); |
123 | 134 |
124 MessageLoopForUI::current()->PostDelayedTask( | 135 MessageLoopForUI::current()->PostDelayedTask( |
125 FROM_HERE, | 136 FROM_HERE, |
126 base::Bind(&ScreenshotTaker::CloseVisualFeedbackLayer, | 137 base::Bind(&ScreenshotTaker::CloseVisualFeedbackLayer, |
127 base::Unretained(this)), | 138 base::Unretained(this), |
| 139 task), |
128 base::TimeDelta::FromMilliseconds(kVisualFeedbackLayerDisplayTimeMs)); | 140 base::TimeDelta::FromMilliseconds(kVisualFeedbackLayerDisplayTimeMs)); |
129 } | 141 } |
OLD | NEW |