summaryrefslogtreecommitdiff
path: root/chromium/ui/aura/mus/embed_root.cc
blob: 058bacaaeb47fdfb217aeb44bb4e635d611b263c (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
// Copyright 2018 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 "ui/aura/mus/embed_root.h"

#include "base/auto_reset.h"
#include "base/bind.h"
#include "ui/aura/client/focus_change_observer.h"
#include "ui/aura/client/focus_client.h"
#include "ui/aura/mus/embed_root_delegate.h"
#include "ui/aura/mus/window_tree_client.h"
#include "ui/aura/window.h"
#include "ui/aura/window_observer.h"
#include "ui/aura/window_tracker.h"
#include "ui/aura/window_tree_host.h"

namespace aura {
namespace {

// FocusClient implementation used for embedded windows. This has minimal
// checks as to what can get focus.
class EmbeddedFocusClient : public client::FocusClient, public WindowObserver {
 public:
  explicit EmbeddedFocusClient(Window* root) : root_(root) {
    client::SetFocusClient(root, this);
  }

  ~EmbeddedFocusClient() override {
    client::SetFocusClient(root_, nullptr);
    if (focused_window_)
      focused_window_->RemoveObserver(this);
  }

  // client::FocusClient:
  void AddObserver(client::FocusChangeObserver* observer) override {
    observers_.AddObserver(observer);
  }
  void RemoveObserver(client::FocusChangeObserver* observer) override {
    observers_.RemoveObserver(observer);
  }
  void FocusWindow(Window* window) override {
    if (IsValidWindowForFocus(window) && window != GetFocusedWindow())
      FocusWindowImpl(window);
  }
  void ResetFocusWithinActiveWindow(Window* window) override {
    // This is never called in the embedding case.
    NOTREACHED();
  }
  Window* GetFocusedWindow() override { return focused_window_; }

 private:
  bool IsValidWindowForFocus(Window* window) const {
    return !window || (root_->Contains(window) && window->CanFocus());
  }

  void FocusWindowImpl(Window* window) {
    Window* previously_focused_window = focused_window_;

    if (previously_focused_window)
      previously_focused_window->RemoveObserver(this);
    focused_window_ = window;
    if (focused_window_)
      focused_window_->AddObserver(this);

    WindowTracker window_tracker;
    if (previously_focused_window)
      window_tracker.Add(previously_focused_window);
    for (auto& observer : observers_) {
      observer.OnWindowFocused(
          focused_window_, window_tracker.Contains(previously_focused_window)
                               ? previously_focused_window
                               : nullptr);
    }
  }

  // WindowObserver:
  void OnWindowDestroying(Window* window) override {
    DCHECK_EQ(window, focused_window_);
  }

  // Root of the hierarchy this is the FocusClient for.
  Window* const root_;

  Window* focused_window_ = nullptr;

  base::ObserverList<client::FocusChangeObserver> observers_;

  DISALLOW_COPY_AND_ASSIGN(EmbeddedFocusClient);
};

}  // namespace

EmbedRoot::~EmbedRoot() {
  window_tree_client_->OnEmbedRootDestroyed(this);
  // Makes use of window_tree_host_->window(), so needs to be destroyed before
  // |window_tree_host_|.
  focus_client_.reset();
}

Window* EmbedRoot::window() {
  return window_tree_host_ ? window_tree_host_->window() : nullptr;
}

EmbedRoot::EmbedRoot(WindowTreeClient* window_tree_client,
                     EmbedRootDelegate* delegate,
                     ui::ClientSpecificId window_id)
    : window_tree_client_(window_tree_client),
      delegate_(delegate),
      weak_factory_(this) {
  window_tree_client_->tree_->ScheduleEmbedForExistingClient(
      window_id, base::BindOnce(&EmbedRoot::OnScheduledEmbedForExistingClient,
                                weak_factory_.GetWeakPtr()));
}

void EmbedRoot::OnScheduledEmbedForExistingClient(
    const base::UnguessableToken& token) {
  token_ = token;
  delegate_->OnEmbedTokenAvailable(token);
}

void EmbedRoot::OnEmbed(std::unique_ptr<WindowTreeHost> window_tree_host) {
  focus_client_ =
      std::make_unique<EmbeddedFocusClient>(window_tree_host->window());
  window_tree_host_ = std::move(window_tree_host);
  delegate_->OnEmbed(window());
}

void EmbedRoot::OnUnembed() {
  delegate_->OnUnembed();
}

}  // namespace aura