1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "mojo/aura/window_tree_host_mojo.h"
9 #include "mojo/aura/window_tree_host_mojo_delegate.h"
10 #include "ui/aura/env.h"
11 #include "ui/aura/window.h"
12 #include "ui/aura/window_event_dispatcher.h"
13 #include "ui/events/event.h"
14 #include "ui/events/event_constants.h"
19 const char kTreeHostsKey
[] = "tree_hosts";
21 typedef std::vector
<WindowTreeHostMojo
*> Managers
;
23 class TreeHosts
: public base::SupportsUserData::Data
{
26 virtual ~TreeHosts() {}
28 static TreeHosts
* Get() {
29 TreeHosts
* hosts
= static_cast<TreeHosts
*>(
30 aura::Env::GetInstance()->GetUserData(kTreeHostsKey
));
32 hosts
= new TreeHosts
;
33 aura::Env::GetInstance()->SetUserData(kTreeHostsKey
, hosts
);
38 void Add(WindowTreeHostMojo
* manager
) {
39 managers_
.push_back(manager
);
42 void Remove(WindowTreeHostMojo
* manager
) {
43 Managers::iterator i
= std::find(managers_
.begin(), managers_
.end(),
45 DCHECK(i
!= managers_
.end());
49 const std::vector
<WindowTreeHostMojo
*> managers() const {
56 DISALLOW_COPY_AND_ASSIGN(TreeHosts
);
61 ////////////////////////////////////////////////////////////////////////////////
62 // WindowTreeHostMojo, public:
64 WindowTreeHostMojo::WindowTreeHostMojo(view_manager::Node
* node
,
65 WindowTreeHostMojoDelegate
* delegate
)
67 bounds_(node
->bounds()),
69 node_
->AddObserver(this);
70 CreateCompositor(GetAcceleratedWidget());
72 TreeHosts::Get()->Add(this);
75 WindowTreeHostMojo::~WindowTreeHostMojo() {
76 node_
->RemoveObserver(this);
77 TreeHosts::Get()->Remove(this);
83 WindowTreeHostMojo
* WindowTreeHostMojo::ForCompositor(
84 ui::Compositor
* compositor
) {
85 const Managers
& managers
= TreeHosts::Get()->managers();
86 for (size_t i
= 0; i
< managers
.size(); ++i
) {
87 if (managers
[i
]->compositor() == compositor
)
93 void WindowTreeHostMojo::SetContents(const SkBitmap
& contents
) {
94 delegate_
->CompositorContentsChanged(contents
);
97 ////////////////////////////////////////////////////////////////////////////////
98 // WindowTreeHostMojo, aura::WindowTreeHost implementation:
100 ui::EventSource
* WindowTreeHostMojo::GetEventSource() {
104 gfx::AcceleratedWidget
WindowTreeHostMojo::GetAcceleratedWidget() {
105 return gfx::kNullAcceleratedWidget
;
108 void WindowTreeHostMojo::Show() {
112 void WindowTreeHostMojo::Hide() {
115 gfx::Rect
WindowTreeHostMojo::GetBounds() const {
119 void WindowTreeHostMojo::SetBounds(const gfx::Rect
& bounds
) {
120 window()->SetBounds(gfx::Rect(bounds
.size()));
123 gfx::Point
WindowTreeHostMojo::GetLocationOnNativeScreen() const {
124 return gfx::Point(0, 0);
127 void WindowTreeHostMojo::SetCapture() {
131 void WindowTreeHostMojo::ReleaseCapture() {
135 void WindowTreeHostMojo::PostNativeEvent(
136 const base::NativeEvent
& native_event
) {
140 void WindowTreeHostMojo::SetCursorNative(gfx::NativeCursor cursor
) {
144 void WindowTreeHostMojo::MoveCursorToNative(const gfx::Point
& location
) {
148 void WindowTreeHostMojo::OnCursorVisibilityChangedNative(bool show
) {
152 ////////////////////////////////////////////////////////////////////////////////
153 // WindowTreeHostMojo, ui::EventSource implementation:
155 ui::EventProcessor
* WindowTreeHostMojo::GetEventProcessor() {
159 ////////////////////////////////////////////////////////////////////////////////
160 // WindowTreeHostMojo, view_manager::NodeObserver implementation:
162 void WindowTreeHostMojo::OnNodeBoundsChanged(
163 view_manager::Node
* node
,
164 const gfx::Rect
& old_bounds
,
165 const gfx::Rect
& new_bounds
) {
166 bounds_
= new_bounds
;
167 if (old_bounds
.origin() != new_bounds
.origin())
168 OnHostMoved(bounds_
.origin());
169 if (old_bounds
.size() != new_bounds
.size())
170 OnHostResized(bounds_
.size());