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 <utility> | 7 #include <utility> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "ash/accelerators/accelerator_controller.h" | 10 #include "ash/accelerators/accelerator_controller.h" |
11 #include "ash/shell.h" | 11 #include "ash/shell.h" |
| 12 #include "base/bind.h" |
12 #include "base/command_line.h" | 13 #include "base/command_line.h" |
| 14 #include "base/containers/flat_set.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> CreateLayerTreeForSnapshot( |
| 54 aura::Window* root_window) { |
| 55 using LayerSet = base::flat_set<const ui::Layer*>; |
| 56 LayerSet blocked_layers; |
| 57 for (auto* browser : *BrowserList::GetInstance()) { |
| 58 if (browser->profile()->IsOffTheRecord()) |
| 59 blocked_layers.insert(browser->window()->GetNativeWindow()->layer()); |
| 60 } |
| 61 |
| 62 auto layer_tree_owner = ::wm::RecreateLayersWithClosure( |
| 63 root_window, base::BindRepeating( |
| 64 [](LayerSet blocked_layers, |
| 65 ui::LayerOwner* owner) -> std::unique_ptr<ui::Layer> { |
| 66 // Parent layer is excluded meaning that it's pointless |
| 67 // to clone current child and all its descendants. This |
| 68 // reduces the number of layers to create. |
| 69 if (blocked_layers.count(owner->layer()->parent())) |
| 70 return nullptr; |
| 71 if (blocked_layers.count(owner->layer())) { |
| 72 auto layer = base::MakeUnique<ui::Layer>( |
| 73 ui::LayerType::LAYER_SOLID_COLOR); |
| 74 layer->SetBounds(owner->layer()->bounds()); |
| 75 layer->SetColor(SK_ColorBLACK); |
| 76 return layer; |
| 77 } |
| 78 return owner->RecreateLayer(); |
| 79 }, |
| 80 std::move(blocked_layers))); |
| 81 |
| 82 // layer_tree_owner cannot be null since we are starting off from the root |
| 83 // window, which could never be incognito. |
| 84 DCHECK(layer_tree_owner); |
| 85 |
| 86 auto* root_layer = layer_tree_owner->root(); |
| 87 root_window->layer()->Add(root_layer); |
| 88 root_window->layer()->StackAtBottom(root_layer); |
| 89 return layer_tree_owner; |
| 90 } |
| 91 |
| 92 void EncodeAndReturnImage( |
| 93 const ArcVoiceInteractionFrameworkService::CaptureFullscreenCallback& |
| 94 callback, |
| 95 std::unique_ptr<ui::LayerTreeOwner> old_layer_owner, |
| 96 const gfx::Image& image) { |
| 97 old_layer_owner.reset(); |
| 98 base::PostTaskWithTraitsAndReplyWithResult( |
| 99 FROM_HERE, |
| 100 base::TaskTraits{base::MayBlock(), base::TaskPriority::USER_BLOCKING}, |
| 101 base::Bind( |
| 102 [](const gfx::Image& image) -> std::vector<uint8_t> { |
| 103 std::vector<uint8_t> res; |
| 104 gfx::JPEG1xEncodedDataFromImage(image, 100, &res); |
| 105 return res; |
| 106 }, |
| 107 image), |
| 108 callback); |
| 109 } |
| 110 |
40 } // namespace | 111 } // namespace |
41 | 112 |
42 // static | 113 // static |
43 const char ArcVoiceInteractionFrameworkService::kArcServiceName[] = | 114 const char ArcVoiceInteractionFrameworkService::kArcServiceName[] = |
44 "arc::ArcVoiceInteractionFrameworkService"; | 115 "arc::ArcVoiceInteractionFrameworkService"; |
45 | 116 |
46 ArcVoiceInteractionFrameworkService::ArcVoiceInteractionFrameworkService( | 117 ArcVoiceInteractionFrameworkService::ArcVoiceInteractionFrameworkService( |
47 ArcBridgeService* bridge_service) | 118 ArcBridgeService* bridge_service) |
48 : ArcService(bridge_service), binding_(this) { | 119 : ArcService(bridge_service), binding_(this) { |
49 arc_bridge_service()->voice_interaction_framework()->AddObserver(this); | 120 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)); | 200 base::Bind(&ScreenshotCallback, callback)); |
130 } | 201 } |
131 | 202 |
132 void ArcVoiceInteractionFrameworkService::CaptureFullscreen( | 203 void ArcVoiceInteractionFrameworkService::CaptureFullscreen( |
133 const CaptureFullscreenCallback& callback) { | 204 const CaptureFullscreenCallback& callback) { |
134 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 205 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
135 // Since ARC currently only runs in primary display, we restrict | 206 // Since ARC currently only runs in primary display, we restrict |
136 // the screenshot to it. | 207 // the screenshot to it. |
137 aura::Window* window = ash::Shell::GetPrimaryRootWindow(); | 208 aura::Window* window = ash::Shell::GetPrimaryRootWindow(); |
138 DCHECK(window); | 209 DCHECK(window); |
139 ui::GrabWindowSnapshotAsyncJPEG( | 210 |
140 window, gfx::Rect(window->bounds().size()), | 211 auto old_layer_owner = CreateLayerTreeForSnapshot(window); |
141 base::CreateTaskRunnerWithTraits( | 212 ui::GrabLayerSnapshotAsync( |
142 {base::MayBlock(), base::TaskPriority::USER_BLOCKING}), | 213 old_layer_owner->root(), gfx::Rect(window->bounds().size()), |
143 base::Bind(&ScreenshotCallback, callback)); | 214 base::Bind(&EncodeAndReturnImage, callback, |
| 215 base::Passed(std::move(old_layer_owner)))); |
144 } | 216 } |
145 | 217 |
146 void ArcVoiceInteractionFrameworkService::OnMetalayerClosed() { | 218 void ArcVoiceInteractionFrameworkService::OnMetalayerClosed() { |
147 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 219 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
148 if (!metalayer_closed_callback_.is_null()) | 220 if (!metalayer_closed_callback_.is_null()) |
149 base::ResetAndReturn(&metalayer_closed_callback_).Run(); | 221 base::ResetAndReturn(&metalayer_closed_callback_).Run(); |
150 } | 222 } |
151 | 223 |
152 void ArcVoiceInteractionFrameworkService::SetMetalayerEnabled(bool enabled) { | 224 void ArcVoiceInteractionFrameworkService::SetMetalayerEnabled(bool enabled) { |
153 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 225 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
190 SetMetalayerVisibility); | 262 SetMetalayerVisibility); |
191 if (!framework_instance) { | 263 if (!framework_instance) { |
192 if (!metalayer_closed_callback_.is_null()) | 264 if (!metalayer_closed_callback_.is_null()) |
193 base::ResetAndReturn(&metalayer_closed_callback_).Run(); | 265 base::ResetAndReturn(&metalayer_closed_callback_).Run(); |
194 return; | 266 return; |
195 } | 267 } |
196 framework_instance->SetMetalayerVisibility(visible); | 268 framework_instance->SetMetalayerVisibility(visible); |
197 } | 269 } |
198 | 270 |
199 } // namespace arc | 271 } // namespace arc |
OLD | NEW |