summaryrefslogtreecommitdiffstats
path: root/mojo/services/window_manager/window_manager_app.cc
blob: 6afe720326aefa82e7169ec4996f55c3499f938e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "mojo/services/window_manager/window_manager_app.h"

#include "base/message_loop/message_loop.h"
#include "base/stl_util.h"
#include "mojo/aura/aura_init.h"
#include "mojo/aura/window_tree_host_mojo.h"
#include "mojo/public/cpp/application/application_connection.h"
#include "mojo/services/public/cpp/view_manager/node.h"
#include "mojo/services/public/cpp/view_manager/view_manager.h"
#include "ui/aura/window.h"
#include "ui/aura/window_property.h"
#include "ui/wm/core/capture_controller.h"
#include "ui/wm/core/focus_controller.h"
#include "ui/wm/core/focus_rules.h"
#include "ui/wm/public/activation_client.h"

DECLARE_WINDOW_PROPERTY_TYPE(mojo::Node*);

namespace mojo {
namespace {

DEFINE_WINDOW_PROPERTY_KEY(Node*, kNodeKey, NULL);

Id GetIdForWindow(aura::Window* window) {
  return window ? window->GetProperty(kNodeKey)->id() : 0;
}

class WMFocusRules : public wm::FocusRules {
 public:
  WMFocusRules() {}
  virtual ~WMFocusRules() {}

 private:
  // Overridden from wm::FocusRules:
  virtual bool IsToplevelWindow(aura::Window* window) const MOJO_OVERRIDE {
    return true;
  }
  virtual bool CanActivateWindow(aura::Window* window) const MOJO_OVERRIDE {
    return true;
  }
  virtual bool CanFocusWindow(aura::Window* window) const MOJO_OVERRIDE {
    return true;
  }
  virtual aura::Window* GetToplevelWindow(
      aura::Window* window) const MOJO_OVERRIDE {
    return window;
  }
  virtual aura::Window* GetActivatableWindow(
      aura::Window* window) const MOJO_OVERRIDE {
    return window;
  }
  virtual aura::Window* GetFocusableWindow(
      aura::Window* window) const MOJO_OVERRIDE {
    return window;
  }
  virtual aura::Window* GetNextActivatableWindow(
      aura::Window* ignore) const MOJO_OVERRIDE {
    return NULL;
  }

  DISALLOW_COPY_AND_ASSIGN(WMFocusRules);
};

}  // namespace

////////////////////////////////////////////////////////////////////////////////
// WindowManagerApp, public:

WindowManagerApp::WindowManagerApp(ViewManagerDelegate* delegate)
    : InterfaceFactoryWithContext(this),
      wrapped_delegate_(delegate),
      view_manager_(NULL),
      view_manager_client_factory_(this),
      root_(NULL) {
}

WindowManagerApp::~WindowManagerApp() {
  // TODO(beng): Figure out if this should be done in
  //             OnViewManagerDisconnected().
  STLDeleteValues(&node_id_to_window_map_);
  if (focus_client_.get())
    focus_client_->RemoveObserver(this);
  if (activation_client_)
    activation_client_->RemoveObserver(this);
}

void WindowManagerApp::AddConnection(WindowManagerServiceImpl* connection) {
  DCHECK(connections_.find(connection) == connections_.end());
  connections_.insert(connection);
}

void WindowManagerApp::RemoveConnection(WindowManagerServiceImpl* connection) {
  DCHECK(connections_.find(connection) != connections_.end());
  connections_.erase(connection);
}

Id WindowManagerApp::OpenWindow() {
  Node* node = Node::Create(view_manager_);
  root_->AddChild(node);
  return node->id();
}

Id WindowManagerApp::OpenWindowWithURL(const String& url) {
  Node* node = Node::Create(view_manager_);
  root_->AddChild(node);
  node->Embed(url);
  return node->id();
}

void WindowManagerApp::SetCapture(Id node) {
  capture_client_->capture_client()->SetCapture(GetWindowForNodeId(node));
  // TODO(beng): notify connected clients that capture has changed, probably
  //             by implementing some capture-client observer.
}

void WindowManagerApp::FocusWindow(Id node) {
  aura::Window* window = GetWindowForNodeId(node);
  DCHECK(window);
  focus_client_->FocusWindow(window);
}

void WindowManagerApp::ActivateWindow(Id node) {
  aura::Window* window = GetWindowForNodeId(node);
  DCHECK(window);
  activation_client_->ActivateWindow(window);
}

bool WindowManagerApp::IsReady() const {
  return view_manager_ && root_;
}

////////////////////////////////////////////////////////////////////////////////
// WindowManagerApp, ApplicationDelegate implementation:

void WindowManagerApp::Initialize(ApplicationImpl* impl) {
  aura_init_.reset(new AuraInit);
}

bool WindowManagerApp::ConfigureIncomingConnection(
    ApplicationConnection* connection) {
  connection->AddService(this);
  connection->AddService(&view_manager_client_factory_);
  return true;
}

////////////////////////////////////////////////////////////////////////////////
// WindowManagerApp, ViewManagerDelegate implementation:

void WindowManagerApp::OnEmbed(ViewManager* view_manager, Node* root) {
  DCHECK(!view_manager_ && !root_);
  view_manager_ = view_manager;
  root_ = root;
  root_->AddObserver(this);

  window_tree_host_.reset(new WindowTreeHostMojo(root_, this));

  RegisterSubtree(root_->id(), window_tree_host_->window());

  capture_client_.reset(
      new wm::ScopedCaptureClient(window_tree_host_->window()));
  wm::FocusController* focus_controller =
      new wm::FocusController(new WMFocusRules);
  activation_client_ = focus_controller;
  focus_client_.reset(focus_controller);

  focus_client_->AddObserver(this);
  activation_client_->AddObserver(this);

  if (wrapped_delegate_)
    wrapped_delegate_->OnEmbed(view_manager, root);

  for (Connections::const_iterator it = connections_.begin();
       it != connections_.end(); ++it) {
    (*it)->NotifyReady();
  }
}

void WindowManagerApp::OnViewManagerDisconnected(
    ViewManager* view_manager) {
  DCHECK_EQ(view_manager_, view_manager);
  if (wrapped_delegate_)
    wrapped_delegate_->OnViewManagerDisconnected(view_manager);
  root_->RemoveObserver(this);
  root_ = NULL;
  view_manager_ = NULL;
  base::MessageLoop::current()->Quit();
}

////////////////////////////////////////////////////////////////////////////////
// WindowManagerApp, NodeObserver implementation:

void WindowManagerApp::OnTreeChanged(
    const NodeObserver::TreeChangeParams& params) {
  DCHECK_EQ(params.receiver, root_);
  DCHECK(params.old_parent || params.new_parent);
  if (!params.target)
    return;

  if (params.new_parent) {
    if (node_id_to_window_map_.find(params.target->id()) ==
        node_id_to_window_map_.end()) {
      NodeIdToWindowMap::const_iterator it =
          node_id_to_window_map_.find(params.new_parent->id());
      DCHECK(it != node_id_to_window_map_.end());
      RegisterSubtree(params.target->id(), it->second);
    }
  } else if (params.old_parent) {
    UnregisterSubtree(params.target->id());
  }
}

////////////////////////////////////////////////////////////////////////////////
// WindowManagerApp, WindowTreeHostMojoDelegate implementation:

void WindowManagerApp::CompositorContentsChanged(const SkBitmap& bitmap) {
  // We draw nothing.
  NOTREACHED();
}

////////////////////////////////////////////////////////////////////////////////
// WindowManagerApp, aura::client::FocusChangeObserver implementation:

void WindowManagerApp::OnWindowFocused(aura::Window* gained_focus,
                                       aura::Window* lost_focus) {
  for (Connections::const_iterator it = connections_.begin();
       it != connections_.end(); ++it) {
    (*it)->NotifyNodeFocused(GetIdForWindow(gained_focus),
                             GetIdForWindow(lost_focus));
  }
}

////////////////////////////////////////////////////////////////////////////////
// WindowManagerApp, aura::client::ActivationChangeObserver implementation:

void WindowManagerApp::OnWindowActivated(aura::Window* gained_active,
                                         aura::Window* lost_active) {
  for (Connections::const_iterator it = connections_.begin();
       it != connections_.end(); ++it) {
    (*it)->NotifyWindowActivated(GetIdForWindow(gained_active),
                                 GetIdForWindow(lost_active));
  }
}

////////////////////////////////////////////////////////////////////////////////
// WindowManagerApp, private:

aura::Window* WindowManagerApp::GetWindowForNodeId(
    Id node) const {
  NodeIdToWindowMap::const_iterator it = node_id_to_window_map_.find(node);
  return it != node_id_to_window_map_.end() ? it->second : NULL;
}

void WindowManagerApp::RegisterSubtree(Id id,
                                       aura::Window* parent) {
  Node* node = view_manager_->GetNodeById(id);
  DCHECK(node_id_to_window_map_.find(id) == node_id_to_window_map_.end());
  aura::Window* window = new aura::Window(NULL);
  window->SetProperty(kNodeKey, node);
  parent->AddChild(window);
  node_id_to_window_map_[id] = window;
  Node::Children::const_iterator it = node->children().begin();
  for (; it != node->children().end(); ++it)
    RegisterSubtree((*it)->id(), window);
}

void WindowManagerApp::UnregisterSubtree(Id id) {
  Node* node = view_manager_->GetNodeById(id);
  NodeIdToWindowMap::iterator it = node_id_to_window_map_.find(id);
  DCHECK(it != node_id_to_window_map_.end());
  scoped_ptr<aura::Window> window(it->second);
  node_id_to_window_map_.erase(it);
  Node::Children::const_iterator child = node->children().begin();
  for (; child != node->children().end(); ++child)
    UnregisterSubtree((*child)->id());
}

}  // namespace mojo