1116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// Copyright 2014 The Chromium Authors. All rights reserved.
2116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// Use of this source code is governed by a BSD-style license that can be
3116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// found in the LICENSE file.
4116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
5116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "mojo/services/window_manager/window_manager_app.h"
6116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
7116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "base/message_loop/message_loop.h"
85f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include "base/stl_util.h"
9116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "mojo/aura/aura_init.h"
10116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "mojo/public/cpp/application/application_connection.h"
111320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "mojo/public/cpp/application/application_impl.h"
126e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#include "mojo/services/public/cpp/input_events/input_events_type_converters.h"
136e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#include "mojo/services/public/cpp/view_manager/view.h"
14116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "mojo/services/public/cpp/view_manager/view_manager.h"
155f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include "ui/aura/window.h"
166e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#include "ui/aura/window_delegate.h"
175f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include "ui/aura/window_property.h"
186e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#include "ui/base/hit_test.h"
19116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "ui/wm/core/capture_controller.h"
205f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include "ui/wm/core/focus_controller.h"
215f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include "ui/wm/core/focus_rules.h"
225f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include "ui/wm/public/activation_client.h"
235f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
246e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)DECLARE_WINDOW_PROPERTY_TYPE(mojo::View*);
25116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
26116680a4aac90f2aa7413d9095a592090648e557Ben Murdochnamespace mojo {
276e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
286e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)// The aura::Windows we use to track Views don't render, so we don't actually
296e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)// need to supply a fully functional WindowDelegate. We do need to provide _a_
306e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)// delegate however, otherwise the event dispatcher won't dispatch events to
316e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)// these windows. (The aura WindowTargeter won't allow a delegate-less window
326e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)// to be the target of an event, since the window delegate is considered the
336e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)// "target handler").
346e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)class DummyDelegate : public aura::WindowDelegate {
356e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) public:
366e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  DummyDelegate() {}
376e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  virtual ~DummyDelegate() {}
386e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
396e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) private:
406e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  // WindowDelegate overrides:
416e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  virtual gfx::Size GetMinimumSize() const OVERRIDE { return gfx::Size(); }
426e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  virtual gfx::Size GetMaximumSize() const OVERRIDE { return gfx::Size(); }
436e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  virtual void OnBoundsChanged(const gfx::Rect& old_bounds,
446e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)                               const gfx::Rect& new_bounds) OVERRIDE {}
456e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  virtual gfx::NativeCursor GetCursor(const gfx::Point& point) OVERRIDE {
466e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    return gfx::kNullCursor;
476e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  }
486e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  virtual int GetNonClientComponent(const gfx::Point& point) const OVERRIDE {
496e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    return HTCAPTION;
506e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  }
516e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  virtual bool ShouldDescendIntoChildForEventHandling(
526e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      aura::Window* child,
536e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      const gfx::Point& location) OVERRIDE { return true; }
546e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  virtual bool CanFocus() OVERRIDE { return true; }
556e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  virtual void OnCaptureLost() OVERRIDE {}
566e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE {}
576e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  virtual void OnDeviceScaleFactorChanged(float device_scale_factor) OVERRIDE {}
586e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  virtual void OnWindowDestroying(aura::Window* window) OVERRIDE {}
596e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  virtual void OnWindowDestroyed(aura::Window* window) OVERRIDE {}
606e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  virtual void OnWindowTargetVisibilityChanged(bool visible) OVERRIDE {}
616e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  virtual bool HasHitTestMask() const OVERRIDE { return false; }
626e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  virtual void GetHitTestMask(gfx::Path* mask) const OVERRIDE {}
636e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
646e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(DummyDelegate);
656e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)};
666e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
675f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)namespace {
685f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
696e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)DEFINE_WINDOW_PROPERTY_KEY(View*, kViewKey, NULL);
705f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
715f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)Id GetIdForWindow(aura::Window* window) {
726e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  return window ? WindowManagerApp::GetViewForWindow(window)->id() : 0;
735f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)}
745f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
755f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)class WMFocusRules : public wm::FocusRules {
765f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) public:
775f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  WMFocusRules() {}
785f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  virtual ~WMFocusRules() {}
795f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
805f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) private:
815f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  // Overridden from wm::FocusRules:
825f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  virtual bool IsToplevelWindow(aura::Window* window) const MOJO_OVERRIDE {
835f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    return true;
845f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  }
855f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  virtual bool CanActivateWindow(aura::Window* window) const MOJO_OVERRIDE {
865f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    return true;
875f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  }
885f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  virtual bool CanFocusWindow(aura::Window* window) const MOJO_OVERRIDE {
895f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    return true;
905f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  }
915f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  virtual aura::Window* GetToplevelWindow(
925f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      aura::Window* window) const MOJO_OVERRIDE {
935f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    return window;
945f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  }
955f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  virtual aura::Window* GetActivatableWindow(
965f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      aura::Window* window) const MOJO_OVERRIDE {
975f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    return window;
985f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  }
995f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  virtual aura::Window* GetFocusableWindow(
1005f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      aura::Window* window) const MOJO_OVERRIDE {
1015f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    return window;
1025f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  }
1035f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  virtual aura::Window* GetNextActivatableWindow(
1045f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      aura::Window* ignore) const MOJO_OVERRIDE {
1055f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    return NULL;
1065f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  }
1075f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
1085f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(WMFocusRules);
1095f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)};
1105f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
1115f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)}  // namespace
112116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
113116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch////////////////////////////////////////////////////////////////////////////////
114116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// WindowManagerApp, public:
115116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
1166e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)WindowManagerApp::WindowManagerApp(
1176e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    ViewManagerDelegate* view_manager_delegate,
1186e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    WindowManagerDelegate* window_manager_delegate)
1195f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    : window_manager_service_factory_(this),
1206e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      wrapped_view_manager_delegate_(view_manager_delegate),
1216e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      wrapped_window_manager_delegate_(window_manager_delegate),
1225f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      view_manager_(NULL),
1236e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      root_(NULL),
1246e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      dummy_delegate_(new DummyDelegate) {
1255f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)}
1265f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
1276e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)WindowManagerApp::~WindowManagerApp() {}
1286e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
1296e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)// static
1306e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)View* WindowManagerApp::GetViewForWindow(aura::Window* window) {
1316e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  return window->GetProperty(kViewKey);
1325f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)}
133116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
134116680a4aac90f2aa7413d9095a592090648e557Ben Murdochvoid WindowManagerApp::AddConnection(WindowManagerServiceImpl* connection) {
135116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  DCHECK(connections_.find(connection) == connections_.end());
136116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  connections_.insert(connection);
137116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
138116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
139116680a4aac90f2aa7413d9095a592090648e557Ben Murdochvoid WindowManagerApp::RemoveConnection(WindowManagerServiceImpl* connection) {
140116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  DCHECK(connections_.find(connection) != connections_.end());
141116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  connections_.erase(connection);
142116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
143116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
1446e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)void WindowManagerApp::SetCapture(Id view) {
1456e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  capture_client_->capture_client()->SetCapture(GetWindowForViewId(view));
146116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // TODO(beng): notify connected clients that capture has changed, probably
147116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  //             by implementing some capture-client observer.
148116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
149116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
1506e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)void WindowManagerApp::FocusWindow(Id view) {
1516e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  aura::Window* window = GetWindowForViewId(view);
1525f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  DCHECK(window);
1535f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  focus_client_->FocusWindow(window);
1545f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)}
1555f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
1566e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)void WindowManagerApp::ActivateWindow(Id view) {
1576e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  aura::Window* window = GetWindowForViewId(view);
1585f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  DCHECK(window);
1595f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  activation_client_->ActivateWindow(window);
1605f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)}
1615f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
162116680a4aac90f2aa7413d9095a592090648e557Ben Murdochbool WindowManagerApp::IsReady() const {
163116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  return view_manager_ && root_;
164116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
165116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
166116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch////////////////////////////////////////////////////////////////////////////////
167116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// WindowManagerApp, ApplicationDelegate implementation:
168116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
169116680a4aac90f2aa7413d9095a592090648e557Ben Murdochvoid WindowManagerApp::Initialize(ApplicationImpl* impl) {
170116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  aura_init_.reset(new AuraInit);
1711320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  view_manager_client_factory_.reset(
1721320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      new ViewManagerClientFactory(impl->shell(), this));
173116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
174116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
175116680a4aac90f2aa7413d9095a592090648e557Ben Murdochbool WindowManagerApp::ConfigureIncomingConnection(
176116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    ApplicationConnection* connection) {
1775f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  connection->AddService(&window_manager_service_factory_);
1781320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  connection->AddService(view_manager_client_factory_.get());
179116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  return true;
180116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
181116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
182116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch////////////////////////////////////////////////////////////////////////////////
1835f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)// WindowManagerApp, ViewManagerDelegate implementation:
184116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
1856e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)void WindowManagerApp::OnEmbed(ViewManager* view_manager,
1866e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)                               View* root,
1876e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)                               ServiceProviderImpl* exported_services,
1886e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)                               scoped_ptr<ServiceProvider> imported_services) {
189116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  DCHECK(!view_manager_ && !root_);
190116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  view_manager_ = view_manager;
1916e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  view_manager_->SetWindowManagerDelegate(this);
192116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  root_ = root;
193116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
194116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  window_tree_host_.reset(new WindowTreeHostMojo(root_, this));
1956e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  window_tree_host_->window()->SetBounds(root->bounds());
1966e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  window_tree_host_->window()->Show();
197116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
1986e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  RegisterSubtree(root_, window_tree_host_->window());
1995f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
200116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  capture_client_.reset(
201116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      new wm::ScopedCaptureClient(window_tree_host_->window()));
2025f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  wm::FocusController* focus_controller =
2035f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      new wm::FocusController(new WMFocusRules);
2045f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  activation_client_ = focus_controller;
2055f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  focus_client_.reset(focus_controller);
2066e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  aura::client::SetFocusClient(window_tree_host_->window(), focus_controller);
207116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
2085f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  focus_client_->AddObserver(this);
2095f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  activation_client_->AddObserver(this);
2105f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
2116e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  if (wrapped_view_manager_delegate_) {
2126e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    wrapped_view_manager_delegate_->OnEmbed(
2136e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)        view_manager, root, exported_services, imported_services.Pass());
2146e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  }
215116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
216116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  for (Connections::const_iterator it = connections_.begin();
217116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch       it != connections_.end(); ++it) {
218116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    (*it)->NotifyReady();
219116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  }
220116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
221116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
222116680a4aac90f2aa7413d9095a592090648e557Ben Murdochvoid WindowManagerApp::OnViewManagerDisconnected(
2235f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    ViewManager* view_manager) {
224116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  DCHECK_EQ(view_manager_, view_manager);
2256e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  if (wrapped_view_manager_delegate_)
2266e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    wrapped_view_manager_delegate_->OnViewManagerDisconnected(view_manager);
227116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  view_manager_ = NULL;
228116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  base::MessageLoop::current()->Quit();
229116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
230116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
231116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch////////////////////////////////////////////////////////////////////////////////
2326e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)// WindowManagerApp, WindowManagerDelegate implementation:
2336e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
2346e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)void WindowManagerApp::Embed(
2356e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    const String& url,
2366e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    InterfaceRequest<ServiceProvider> service_provider) {
2376e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  if (wrapped_window_manager_delegate_)
2386e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    wrapped_window_manager_delegate_->Embed(url, service_provider.Pass());
2396e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)}
2406e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
2416e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)void WindowManagerApp::DispatchEvent(EventPtr event) {
2421320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  scoped_ptr<ui::Event> ui_event = event.To<scoped_ptr<ui::Event> >();
2436e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  if (ui_event)
2446e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    window_tree_host_->SendEventToProcessor(ui_event.get());
2456e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)}
2466e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
2476e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)////////////////////////////////////////////////////////////////////////////////
2486e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)// WindowManagerApp, ViewObserver implementation:
2495f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
2505f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)void WindowManagerApp::OnTreeChanged(
2516e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    const ViewObserver::TreeChangeParams& params) {
2526e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  if (params.receiver != root_)
2536e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    return;
2545f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  DCHECK(params.old_parent || params.new_parent);
2555f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  if (!params.target)
2565f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    return;
2575f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
2585f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  if (params.new_parent) {
2596e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    if (view_id_to_window_map_.find(params.target->id()) ==
2606e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)        view_id_to_window_map_.end()) {
2616e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      ViewIdToWindowMap::const_iterator it =
2626e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)          view_id_to_window_map_.find(params.new_parent->id());
2636e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      DCHECK(it != view_id_to_window_map_.end());
2646e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      RegisterSubtree(params.target, it->second);
2655f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    }
2665f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  } else if (params.old_parent) {
2676e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    UnregisterSubtree(params.target);
2685f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  }
2695f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)}
2705f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
2716e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)void WindowManagerApp::OnViewDestroyed(View* view) {
2726e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  if (view != root_)
2736e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    return;
2746e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  aura::Window* window = GetWindowForViewId(view->id());
2756e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  window->RemovePreTargetHandler(this);
2766e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  root_ = NULL;
2776e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  STLDeleteValues(&view_id_to_window_map_);
2786e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  if (focus_client_.get())
2796e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    focus_client_->RemoveObserver(this);
2806e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  if (activation_client_)
2816e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    activation_client_->RemoveObserver(this);
2826e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  window_tree_host_.reset();
2836e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)}
2846e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
2856e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)void WindowManagerApp::OnViewBoundsChanged(View* view,
2866e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)                                           const gfx::Rect& old_bounds,
2876e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)                                           const gfx::Rect& new_bounds) {
2886e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  aura::Window* window = GetWindowForViewId(view->id());
2896e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  window->SetBounds(new_bounds);
2906e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)}
2916e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
2925f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)////////////////////////////////////////////////////////////////////////////////
293116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// WindowManagerApp, WindowTreeHostMojoDelegate implementation:
294116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
295116680a4aac90f2aa7413d9095a592090648e557Ben Murdochvoid WindowManagerApp::CompositorContentsChanged(const SkBitmap& bitmap) {
296116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // We draw nothing.
297116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  NOTREACHED();
298116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
299116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
300116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch////////////////////////////////////////////////////////////////////////////////
3016e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)// WindowManagerApp, ui::EventHandler implementation:
3026e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
3036e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)void WindowManagerApp::OnEvent(ui::Event* event) {
3046e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  aura::Window* window = static_cast<aura::Window*>(event->target());
3051320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  view_manager_->DispatchEvent(GetViewForWindow(window), Event::From(*event));
3066e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)}
3076e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
3086e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)////////////////////////////////////////////////////////////////////////////////
3095f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)// WindowManagerApp, aura::client::FocusChangeObserver implementation:
3105f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
3115f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)void WindowManagerApp::OnWindowFocused(aura::Window* gained_focus,
3125f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                                       aura::Window* lost_focus) {
3135f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  for (Connections::const_iterator it = connections_.begin();
3145f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)       it != connections_.end(); ++it) {
3156e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    (*it)->NotifyViewFocused(GetIdForWindow(gained_focus),
3165f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                             GetIdForWindow(lost_focus));
3175f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  }
3185f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)}
3195f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
3205f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)////////////////////////////////////////////////////////////////////////////////
3215f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)// WindowManagerApp, aura::client::ActivationChangeObserver implementation:
3225f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
3235f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)void WindowManagerApp::OnWindowActivated(aura::Window* gained_active,
3245f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                                         aura::Window* lost_active) {
3255f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  for (Connections::const_iterator it = connections_.begin();
3265f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)       it != connections_.end(); ++it) {
3275f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    (*it)->NotifyWindowActivated(GetIdForWindow(gained_active),
3285f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                                 GetIdForWindow(lost_active));
3295f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  }
3305f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)}
3315f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
3325f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)////////////////////////////////////////////////////////////////////////////////
333116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// WindowManagerApp, private:
334116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
3356e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)aura::Window* WindowManagerApp::GetWindowForViewId(Id view) const {
3366e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  ViewIdToWindowMap::const_iterator it = view_id_to_window_map_.find(view);
3376e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  return it != view_id_to_window_map_.end() ? it->second : NULL;
338116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
339116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
3406e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)void WindowManagerApp::RegisterSubtree(View* view, aura::Window* parent) {
3416e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  view->AddObserver(this);
3426e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  DCHECK(view_id_to_window_map_.find(view->id()) ==
3436e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)         view_id_to_window_map_.end());
3446e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  aura::Window* window = new aura::Window(dummy_delegate_.get());
3456e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  window->set_id(view->id());
3466e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  window->SetProperty(kViewKey, view);
3476e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  // All events pass through the root during dispatch, so we only need a handler
3486e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  // installed there.
3496e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  if (view == root_)
3506e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    window->AddPreTargetHandler(this);
3515f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  parent->AddChild(window);
3526e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  window->SetBounds(view->bounds());
3536e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  window->Show();
3546e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  view_id_to_window_map_[view->id()] = window;
3556e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  View::Children::const_iterator it = view->children().begin();
3566e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  for (; it != view->children().end(); ++it)
3576e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    RegisterSubtree(*it, window);
3585f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)}
359116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
3606e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)void WindowManagerApp::UnregisterSubtree(View* view) {
3616e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  view->RemoveObserver(this);
3626e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  ViewIdToWindowMap::iterator it = view_id_to_window_map_.find(view->id());
3636e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  DCHECK(it != view_id_to_window_map_.end());
3645f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  scoped_ptr<aura::Window> window(it->second);
3656e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  view_id_to_window_map_.erase(it);
3666e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  View::Children::const_iterator child = view->children().begin();
3676e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  for (; child != view->children().end(); ++child)
3686e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    UnregisterSubtree(*child);
369116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
370116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
371116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}  // namespace mojo
372