12a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
22a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
32a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// found in the LICENSE file.
42a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
5a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "ui/wm/core/focus_controller.h"
62a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
72a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/auto_reset.h"
82a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "ui/aura/client/aura_constants.h"
92a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "ui/aura/client/capture_client.h"
102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "ui/aura/client/focus_change_observer.h"
112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "ui/aura/env.h"
125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "ui/aura/window_tracker.h"
13010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)#include "ui/base/ime/text_input_focus_manager.h"
14d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)#include "ui/events/event.h"
15a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "ui/wm/core/focus_rules.h"
16a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "ui/wm/core/window_util.h"
17effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#include "ui/wm/public/activation_change_observer.h"
182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
19a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)namespace wm {
202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)namespace {
212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// When a modal window is activated, we bring its entire transient parent chain
232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// to the front. This function must be called before the modal transient is
242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// stacked at the top to ensure correct stacking order.
252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void StackTransientParentsBelowModalWindow(aura::Window* window) {
262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (window->GetProperty(aura::client::kModalKey) != ui::MODAL_TYPE_WINDOW)
272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return;
282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
29a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  aura::Window* transient_parent = wm::GetTransientParent(window);
302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  while (transient_parent) {
312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    transient_parent->parent()->StackChildAtTop(transient_parent);
32a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    transient_parent = wm::GetTransientParent(transient_parent);
332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}  // namespace
372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)////////////////////////////////////////////////////////////////////////////////
392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// FocusController, public:
402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)FocusController::FocusController(FocusRules* rules)
422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    : active_window_(NULL),
432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      focused_window_(NULL),
442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      updating_focus_(false),
452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      updating_activation_(false),
462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      rules_(rules),
47c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      observer_manager_(this) {
482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DCHECK(rules);
492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)FocusController::~FocusController() {
522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)////////////////////////////////////////////////////////////////////////////////
552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// FocusController, aura::client::ActivationClient implementation:
562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void FocusController::AddObserver(
582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    aura::client::ActivationChangeObserver* observer) {
592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  activation_observers_.AddObserver(observer);
602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void FocusController::RemoveObserver(
632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    aura::client::ActivationChangeObserver* observer) {
642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  activation_observers_.RemoveObserver(observer);
652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void FocusController::ActivateWindow(aura::Window* window) {
682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  FocusWindow(window);
692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void FocusController::DeactivateWindow(aura::Window* window) {
722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (window)
732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    FocusWindow(rules_->GetNextActivatableWindow(window));
742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)aura::Window* FocusController::GetActiveWindow() {
772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return active_window_;
782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)aura::Window* FocusController::GetActivatableWindow(aura::Window* window) {
812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return rules_->GetActivatableWindow(window);
822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)aura::Window* FocusController::GetToplevelWindow(aura::Window* window) {
852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return rules_->GetToplevelWindow(window);
862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool FocusController::CanActivateWindow(aura::Window* window) const {
892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return rules_->CanActivateWindow(window);
902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)////////////////////////////////////////////////////////////////////////////////
932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// FocusController, aura::client::FocusClient implementation:
942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void FocusController::AddObserver(
962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    aura::client::FocusChangeObserver* observer) {
972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  focus_observers_.AddObserver(observer);
982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void FocusController::RemoveObserver(
1012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    aura::client::FocusChangeObserver* observer) {
1022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  focus_observers_.RemoveObserver(observer);
1032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
1042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void FocusController::FocusWindow(aura::Window* window) {
1062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (window &&
1072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      (window->Contains(focused_window_) || window->Contains(active_window_))) {
1082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return;
1092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
1102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
11158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // We should not be messing with the focus if the window has capture, unless
11258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // no has focus.
11358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  if (window && (aura::client::GetCaptureWindow(window) == window) &&
11458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      focused_window_) {
1152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return;
11658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  }
1172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Focusing a window also activates its containing activatable window. Note
1192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // that the rules could redirect activation activation and/or focus.
1202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  aura::Window* focusable = rules_->GetFocusableWindow(window);
1212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  aura::Window* activatable =
1222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      focusable ? rules_->GetActivatableWindow(focusable) : NULL;
1232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // We need valid focusable/activatable windows in the event we're not clearing
1252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // focus. "Clearing focus" is inferred by whether or not |window| passed to
1262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // this function is non-NULL.
1272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (window && (!focusable || !activatable))
1282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return;
1292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DCHECK((focusable && activatable) || !window);
1302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Activation change observers may change the focused window. If this happens
1322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // we must not adjust the focus below since this will clobber that change.
1332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  aura::Window* last_focused_window = focused_window_;
1342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (!updating_activation_)
135c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    SetActiveWindow(window, activatable);
1362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // If the window's ActivationChangeObserver shifted focus to a valid window,
1382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // we don't want to focus the window we thought would be focused by default.
1392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool activation_changed_focus = last_focused_window != focused_window_;
1402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (!updating_focus_ && (!activation_changed_focus || !focused_window_)) {
1412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (active_window_ && focusable)
1422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      DCHECK(active_window_->Contains(focusable));
1432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    SetFocusedWindow(focusable);
1442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
1452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
1462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void FocusController::ResetFocusWithinActiveWindow(aura::Window* window) {
1482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DCHECK(window);
1492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (!active_window_)
1502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return;
1512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (!active_window_->Contains(window))
1522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return;
1532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  SetFocusedWindow(window);
1542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
1552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)aura::Window* FocusController::GetFocusedWindow() {
1572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return focused_window_;
1582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
1592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)////////////////////////////////////////////////////////////////////////////////
1612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// FocusController, ui::EventHandler implementation:
1622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void FocusController::OnKeyEvent(ui::KeyEvent* event) {
1632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
1642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void FocusController::OnMouseEvent(ui::MouseEvent* event) {
1660de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles)  if (event->type() == ui::ET_MOUSE_PRESSED && !event->handled())
1672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    WindowFocusedFromInputEvent(static_cast<aura::Window*>(event->target()));
1682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
1692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void FocusController::OnScrollEvent(ui::ScrollEvent* event) {
1712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
1722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void FocusController::OnTouchEvent(ui::TouchEvent* event) {
1742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
1752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void FocusController::OnGestureEvent(ui::GestureEvent* event) {
1772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (event->type() == ui::ET_GESTURE_BEGIN &&
1780de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles)      event->details().touch_points() == 1 &&
1790de6073388f4e2780db8536178b129cd8f6ab386Torne (Richard Coles)      !event->handled()) {
1802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    WindowFocusedFromInputEvent(static_cast<aura::Window*>(event->target()));
1812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
1822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
1832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)////////////////////////////////////////////////////////////////////////////////
1852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// FocusController, aura::WindowObserver implementation:
1862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void FocusController::OnWindowVisibilityChanged(aura::Window* window,
1882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                                bool visible) {
18923730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  if (!visible)
1902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    WindowLostFocusFromDispositionChange(window, window->parent());
1912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
1922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void FocusController::OnWindowDestroying(aura::Window* window) {
1942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  WindowLostFocusFromDispositionChange(window, window->parent());
1952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
1962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void FocusController::OnWindowHierarchyChanging(
1982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const HierarchyChangeParams& params) {
1992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (params.receiver == active_window_ &&
2002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      params.target->Contains(params.receiver) && (!params.new_parent ||
2012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      aura::client::GetFocusClient(params.new_parent) !=
2022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          aura::client::GetFocusClient(params.receiver))) {
2032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    WindowLostFocusFromDispositionChange(params.receiver, params.old_parent);
2042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
2052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
2062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void FocusController::OnWindowHierarchyChanged(
2082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const HierarchyChangeParams& params) {
2092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (params.receiver == focused_window_ &&
2102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      params.target->Contains(params.receiver) && (!params.new_parent ||
2112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      aura::client::GetFocusClient(params.new_parent) !=
2122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          aura::client::GetFocusClient(params.receiver))) {
2132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    WindowLostFocusFromDispositionChange(params.receiver, params.old_parent);
2142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
2152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
2162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)////////////////////////////////////////////////////////////////////////////////
2182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// FocusController, private:
2192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void FocusController::SetFocusedWindow(aura::Window* window) {
2212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (updating_focus_ || window == focused_window_)
2222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return;
2232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DCHECK(rules_->CanFocusWindow(window));
2242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (window)
2252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    DCHECK_EQ(window, rules_->GetFocusableWindow(window));
2262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  base::AutoReset<bool> updating_focus(&updating_focus_, true);
2282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  aura::Window* lost_focus = focused_window_;
229010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
230010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  // |window| is going to get the focus, so reset the text input client.
231010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  // OnWindowFocused() may set a proper text input client if the implementation
232010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  // supports text input.
233010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  ui::TextInputFocusManager* text_input_focus_manager =
234010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)      ui::TextInputFocusManager::GetInstance();
235010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  if (window)
236010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    text_input_focus_manager->FocusTextInputClient(NULL);
237010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
2385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Allow for the window losing focus to be deleted during dispatch. If it is
2395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // deleted pass NULL to observers instead of a deleted window.
2405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  aura::WindowTracker window_tracker;
2415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (lost_focus)
2425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    window_tracker.Add(lost_focus);
2432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (focused_window_ && observer_manager_.IsObserving(focused_window_) &&
2442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      focused_window_ != active_window_) {
2452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    observer_manager_.Remove(focused_window_);
2462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
2472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  focused_window_ = window;
2482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (focused_window_ && !observer_manager_.IsObserving(focused_window_))
2492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    observer_manager_.Add(focused_window_);
2502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  FOR_EACH_OBSERVER(aura::client::FocusChangeObserver,
2522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                    focus_observers_,
2535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                    OnWindowFocused(focused_window_,
2545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                    window_tracker.Contains(lost_focus) ?
2555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                    lost_focus : NULL));
2565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (window_tracker.Contains(lost_focus)) {
2575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    aura::client::FocusChangeObserver* observer =
2585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        aura::client::GetFocusChangeObserver(lost_focus);
2595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    if (observer)
2605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      observer->OnWindowFocused(focused_window_, lost_focus);
2615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
2622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  aura::client::FocusChangeObserver* observer =
2635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      aura::client::GetFocusChangeObserver(focused_window_);
2645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (observer) {
2655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    observer->OnWindowFocused(
2665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        focused_window_,
2675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        window_tracker.Contains(lost_focus) ? lost_focus : NULL);
2685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
269010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
270010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  // Ensure that the text input client is reset when the window loses the focus.
271010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  if (!window)
272010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    text_input_focus_manager->FocusTextInputClient(NULL);
2732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
2742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
275c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)void FocusController::SetActiveWindow(aura::Window* requested_window,
276c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                                      aura::Window* window) {
277c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (updating_activation_)
2782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return;
2792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
280c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (window == active_window_) {
281c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    if (requested_window) {
282c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      FOR_EACH_OBSERVER(aura::client::ActivationChangeObserver,
283c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                        activation_observers_,
284c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                        OnAttemptToReactivateWindow(requested_window,
285c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                                                    active_window_));
286c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    }
287c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    return;
288c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  }
289c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DCHECK(rules_->CanActivateWindow(window));
2912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (window)
2922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    DCHECK_EQ(window, rules_->GetActivatableWindow(window));
2932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  base::AutoReset<bool> updating_activation(&updating_activation_, true);
2952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  aura::Window* lost_activation = active_window_;
2965d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Allow for the window losing activation to be deleted during dispatch. If
2975d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // it is deleted pass NULL to observers instead of a deleted window.
2985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  aura::WindowTracker window_tracker;
2995d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (lost_activation)
3005d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    window_tracker.Add(lost_activation);
3012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (active_window_ && observer_manager_.IsObserving(active_window_) &&
3022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      focused_window_ != active_window_) {
3032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    observer_manager_.Remove(active_window_);
3042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
3052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  active_window_ = window;
3062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (active_window_ && !observer_manager_.IsObserving(active_window_))
3072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    observer_manager_.Add(active_window_);
3082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (active_window_) {
3092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    StackTransientParentsBelowModalWindow(active_window_);
3102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    active_window_->parent()->StackChildAtTop(active_window_);
3112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
3122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  aura::client::ActivationChangeObserver* observer = NULL;
3145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (window_tracker.Contains(lost_activation)) {
3155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    observer = aura::client::GetActivationChangeObserver(lost_activation);
3165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    if (observer)
3175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      observer->OnWindowActivated(active_window_, lost_activation);
3185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
3192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  observer = aura::client::GetActivationChangeObserver(active_window_);
3205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (observer) {
3215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    observer->OnWindowActivated(
3225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        active_window_,
3235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        window_tracker.Contains(lost_activation) ? lost_activation : NULL);
3245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
3252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  FOR_EACH_OBSERVER(aura::client::ActivationChangeObserver,
3262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                    activation_observers_,
3275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                    OnWindowActivated(active_window_,
3285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                      window_tracker.Contains(lost_activation) ?
3295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                      lost_activation : NULL));
3302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
3312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void FocusController::WindowLostFocusFromDispositionChange(
3332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    aura::Window* window,
3342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    aura::Window* next) {
3352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // A window's modality state will interfere with focus restoration during its
3362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // destruction.
3372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  window->ClearProperty(aura::client::kModalKey);
3382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // TODO(beng): See if this function can be replaced by a call to
3392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //             FocusWindow().
3402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Activation adjustments are handled first in the event of a disposition
3412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // changed. If an activation change is necessary, focus is reset as part of
3422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // that process so there's no point in updating focus independently.
3432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  if (window == active_window_) {
3442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    aura::Window* next_activatable = rules_->GetNextActivatableWindow(window);
345c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    SetActiveWindow(NULL, next_activatable);
346c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    if (!(active_window_ && active_window_->Contains(focused_window_)))
347c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      SetFocusedWindow(next_activatable);
3482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  } else if (window->Contains(focused_window_)) {
3492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // Active window isn't changing, but focused window might be.
3502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    SetFocusedWindow(rules_->GetFocusableWindow(next));
3512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
3522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
3532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void FocusController::WindowFocusedFromInputEvent(aura::Window* window) {
355c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Only focus |window| if it or any of its parents can be focused. Otherwise
356c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // FocusWindow() will focus the topmost window, which may not be the
357c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // currently focused one.
358c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  if (rules_->CanFocusWindow(GetToplevelWindow(window)))
359c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    FocusWindow(window);
3602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
3612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
362a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}  // namespace wm
363