OLD | NEW |
---|---|
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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/chromeos/arc/voice_interaction/arc_voice_interaction_fr amework_service.h" | 5 #include "chrome/browser/chromeos/arc/voice_interaction/arc_voice_interaction_fr amework_service.h" |
6 | 6 |
7 #include <set> | |
7 #include <utility> | 8 #include <utility> |
8 #include <vector> | 9 #include <vector> |
9 | 10 |
10 #include "ash/accelerators/accelerator_controller.h" | 11 #include "ash/accelerators/accelerator_controller.h" |
11 #include "ash/shell.h" | 12 #include "ash/shell.h" |
13 #include "base/bind.h" | |
12 #include "base/command_line.h" | 14 #include "base/command_line.h" |
13 #include "base/logging.h" | 15 #include "base/logging.h" |
14 #include "base/memory/ref_counted.h" | 16 #include "base/memory/ref_counted.h" |
15 #include "base/task_scheduler/post_task.h" | 17 #include "base/task_scheduler/post_task.h" |
18 #include "chrome/browser/ui/browser.h" | |
19 #include "chrome/browser/ui/browser_list.h" | |
20 #include "chrome/browser/ui/browser_window.h" | |
16 #include "chromeos/chromeos_switches.h" | 21 #include "chromeos/chromeos_switches.h" |
17 #include "components/arc/arc_bridge_service.h" | 22 #include "components/arc/arc_bridge_service.h" |
18 #include "components/arc/instance_holder.h" | 23 #include "components/arc/instance_holder.h" |
19 #include "content/public/browser/browser_thread.h" | 24 #include "content/public/browser/browser_thread.h" |
20 #include "ui/aura/window.h" | 25 #include "ui/aura/window.h" |
26 #include "ui/compositor/layer.h" | |
27 #include "ui/compositor/layer_owner.h" | |
28 #include "ui/compositor/layer_tree_owner.h" | |
29 #include "ui/gfx/image/image.h" | |
30 #include "ui/gfx/image/image_util.h" | |
31 #include "ui/gfx/native_widget_types.h" | |
21 #include "ui/snapshot/snapshot.h" | 32 #include "ui/snapshot/snapshot.h" |
33 #include "ui/snapshot/snapshot_aura.h" | |
34 #include "ui/wm/core/window_util.h" | |
22 #include "ui/wm/public/activation_client.h" | 35 #include "ui/wm/public/activation_client.h" |
23 | 36 |
24 namespace arc { | 37 namespace arc { |
25 | 38 |
26 namespace { | 39 namespace { |
27 | 40 |
28 void ScreenshotCallback( | 41 void ScreenshotCallback( |
29 const mojom::VoiceInteractionFrameworkHost::CaptureFocusedWindowCallback& | 42 const mojom::VoiceInteractionFrameworkHost::CaptureFocusedWindowCallback& |
30 callback, | 43 callback, |
31 scoped_refptr<base::RefCountedMemory> data) { | 44 scoped_refptr<base::RefCountedMemory> data) { |
32 if (data.get() == nullptr) { | 45 if (data.get() == nullptr) { |
33 callback.Run(std::vector<uint8_t>{}); | 46 callback.Run(std::vector<uint8_t>{}); |
34 return; | 47 return; |
35 } | 48 } |
36 std::vector<uint8_t> result(data->front(), data->front() + data->size()); | 49 std::vector<uint8_t> result(data->front(), data->front() + data->size()); |
37 callback.Run(result); | 50 callback.Run(result); |
38 } | 51 } |
39 | 52 |
53 std::unique_ptr<ui::LayerTreeOwner> BlockIncognitoWindows( | |
54 aura::Window* root_window) { | |
55 using LayerSet = std::set<const ui::Layer*>; | |
56 auto blocked_layers = LayerSet(); | |
Luis Héctor Chávez
2017/05/15 16:47:52
Why not
LayerSet blocked_layers;
? It's shorter
Muyuan
2017/05/15 18:09:54
Done.
| |
57 auto* browser_list = BrowserList::GetInstance(); | |
58 for (auto* browser : *browser_list) { | |
Luis Héctor Chávez
2017/05/15 16:47:51
nit: BrowserList itself does
for (auto* browser :
Muyuan
2017/05/15 18:09:54
Done.
| |
59 if (browser->profile()->IsOffTheRecord()) | |
60 blocked_layers.insert(browser->window()->GetNativeWindow()->layer()); | |
61 } | |
62 | |
63 auto layer_tree_owner = ::wm::MapLayers( | |
64 root_window, base::Bind( | |
65 [](LayerSet layers, | |
66 ui::LayerOwner* owner) -> std::unique_ptr<ui::Layer> { | |
67 // Parent layer is excluded meaning that it's pointless | |
68 // to clone current child and all its descendants. This | |
69 // reduces the number of layers to create. | |
70 if (layers.count(owner->layer()->parent())) | |
71 return nullptr; | |
72 if (layers.count(owner->layer())) { | |
73 auto layer = base::MakeUnique<ui::Layer>( | |
74 ui::LayerType::LAYER_SOLID_COLOR); | |
75 layer->SetBounds(owner->layer()->bounds()); | |
76 layer->SetColor(SK_ColorBLACK); | |
77 return layer; | |
78 } | |
79 return owner->RecreateLayer(); | |
80 }, | |
81 std::move(blocked_layers))); | |
82 if (layer_tree_owner) { | |
Luis Héctor Chávez
2017/05/15 16:47:52
nit: use the guard clause pattern:
if (!layer_tre
Muyuan
2017/05/15 18:09:54
Done.
| |
83 auto* root_layer = layer_tree_owner->root(); | |
84 root_window->layer()->Add(root_layer); | |
85 root_window->layer()->StackAtBottom(root_layer); | |
86 } | |
87 return layer_tree_owner; | |
88 } | |
89 | |
40 } // namespace | 90 } // namespace |
41 | 91 |
42 // static | 92 // static |
43 const char ArcVoiceInteractionFrameworkService::kArcServiceName[] = | 93 const char ArcVoiceInteractionFrameworkService::kArcServiceName[] = |
44 "arc::ArcVoiceInteractionFrameworkService"; | 94 "arc::ArcVoiceInteractionFrameworkService"; |
45 | 95 |
46 ArcVoiceInteractionFrameworkService::ArcVoiceInteractionFrameworkService( | 96 ArcVoiceInteractionFrameworkService::ArcVoiceInteractionFrameworkService( |
47 ArcBridgeService* bridge_service) | 97 ArcBridgeService* bridge_service) |
48 : ArcService(bridge_service), binding_(this) { | 98 : ArcService(bridge_service), binding_(this) { |
49 arc_bridge_service()->voice_interaction_framework()->AddObserver(this); | 99 arc_bridge_service()->voice_interaction_framework()->AddObserver(this); |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
129 base::Bind(&ScreenshotCallback, callback)); | 179 base::Bind(&ScreenshotCallback, callback)); |
130 } | 180 } |
131 | 181 |
132 void ArcVoiceInteractionFrameworkService::CaptureFullscreen( | 182 void ArcVoiceInteractionFrameworkService::CaptureFullscreen( |
133 const CaptureFullscreenCallback& callback) { | 183 const CaptureFullscreenCallback& callback) { |
134 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 184 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
135 // Since ARC currently only runs in primary display, we restrict | 185 // Since ARC currently only runs in primary display, we restrict |
136 // the screenshot to it. | 186 // the screenshot to it. |
137 aura::Window* window = ash::Shell::GetPrimaryRootWindow(); | 187 aura::Window* window = ash::Shell::GetPrimaryRootWindow(); |
138 DCHECK(window); | 188 DCHECK(window); |
139 ui::GrabWindowSnapshotAsyncJPEG( | 189 |
140 window, gfx::Rect(window->bounds().size()), | 190 old_layer_owner_ = BlockIncognitoWindows(window); |
Luis Héctor Chávez
2017/05/15 16:47:52
It doesn't seem like you *need* to store this as a
| |
141 base::CreateTaskRunnerWithTraits( | 191 DCHECK(old_layer_owner_); |
Luis Héctor Chávez
2017/05/15 16:47:52
I don't think you can DCHECK here since it seems l
Muyuan
2017/05/15 18:09:54
Done.
| |
142 {base::MayBlock(), base::TaskPriority::USER_BLOCKING}), | 192 |
143 base::Bind(&ScreenshotCallback, callback)); | 193 ui::GrabLayerSnapshotAsync( |
194 old_layer_owner_->root(), gfx::Rect(window->bounds().size()), | |
195 base::Bind(&ArcVoiceInteractionFrameworkService::EncodeAndReturnImage, | |
196 base::Unretained(this), callback)); | |
144 } | 197 } |
145 | 198 |
146 void ArcVoiceInteractionFrameworkService::OnMetalayerClosed() { | 199 void ArcVoiceInteractionFrameworkService::OnMetalayerClosed() { |
147 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 200 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
148 if (!metalayer_closed_callback_.is_null()) | 201 if (!metalayer_closed_callback_.is_null()) |
149 base::ResetAndReturn(&metalayer_closed_callback_).Run(); | 202 base::ResetAndReturn(&metalayer_closed_callback_).Run(); |
150 } | 203 } |
151 | 204 |
152 void ArcVoiceInteractionFrameworkService::SetMetalayerEnabled(bool enabled) { | 205 void ArcVoiceInteractionFrameworkService::SetMetalayerEnabled(bool enabled) { |
153 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 206 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
189 arc_bridge_service()->voice_interaction_framework(), | 242 arc_bridge_service()->voice_interaction_framework(), |
190 SetMetalayerVisibility); | 243 SetMetalayerVisibility); |
191 if (!framework_instance) { | 244 if (!framework_instance) { |
192 if (!metalayer_closed_callback_.is_null()) | 245 if (!metalayer_closed_callback_.is_null()) |
193 base::ResetAndReturn(&metalayer_closed_callback_).Run(); | 246 base::ResetAndReturn(&metalayer_closed_callback_).Run(); |
194 return; | 247 return; |
195 } | 248 } |
196 framework_instance->SetMetalayerVisibility(visible); | 249 framework_instance->SetMetalayerVisibility(visible); |
197 } | 250 } |
198 | 251 |
252 void ArcVoiceInteractionFrameworkService::EncodeAndReturnImage( | |
253 const CaptureFullscreenCallback& callback, | |
254 const gfx::Image& image) { | |
255 old_layer_owner_.reset(); | |
256 base::PostTaskWithTraitsAndReplyWithResult( | |
257 FROM_HERE, | |
258 base::TaskTraits({base::MayBlock(), base::TaskPriority::USER_BLOCKING}), | |
Luis Héctor Chávez
2017/05/15 16:47:51
nit:
base::TaskTraits{base::MayBlock(), base::Tas
Muyuan
2017/05/15 18:09:54
Done.
| |
259 base::Bind( | |
260 [](const gfx::Image& image) -> std::vector<uint8_t> { | |
261 std::vector<uint8_t> res; | |
262 gfx::JPEG1xEncodedDataFromImage(image, 100, &res); | |
263 return res; | |
264 }, | |
265 image), | |
266 callback); | |
267 } | |
268 | |
199 } // namespace arc | 269 } // namespace arc |
OLD | NEW |