summaryrefslogtreecommitdiffstats
path: root/mash/wm/window_manager_application.cc
blob: 4436e2e788cfa62d191a9bdfce7bad908afa1993 (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
// Copyright 2015 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 "mash/wm/window_manager_application.h"

#include <stdint.h>
#include <utility>

#include "base/bind.h"
#include "components/mus/common/util.h"
#include "components/mus/public/cpp/event_matcher.h"
#include "components/mus/public/cpp/window.h"
#include "components/mus/public/cpp/window_tree_connection.h"
#include "components/mus/public/cpp/window_tree_host_factory.h"
#include "mash/wm/accelerator_registrar_impl.h"
#include "mash/wm/background_layout.h"
#include "mash/wm/shadow_controller.h"
#include "mash/wm/shelf_layout.h"
#include "mash/wm/user_window_controller_impl.h"
#include "mash/wm/window_layout.h"
#include "mash/wm/window_manager_impl.h"
#include "mojo/services/tracing/public/cpp/tracing_impl.h"
#include "mojo/shell/public/cpp/application_connection.h"
#include "ui/mojo/init/ui_init.h"
#include "ui/views/mus/aura_init.h"
#include "ui/views/mus/display_converter.h"

namespace mash {
namespace wm {
namespace {
const uint32_t kWindowSwitchCmd = 1;

void AssertTrue(bool success) {
  DCHECK(success);
}

}  // namespace

WindowManagerApplication::WindowManagerApplication()
    : root_(nullptr),
      window_count_(0),
      app_(nullptr),
      host_client_binding_(this) {}

WindowManagerApplication::~WindowManagerApplication() {}

mus::Window* WindowManagerApplication::GetWindowForContainer(
    mojom::Container container) {
  const mus::Id window_id = root_->connection()->GetConnectionId() << 16 |
                            static_cast<uint16_t>(container);
  return root_->GetChildById(window_id);
}

mus::Window* WindowManagerApplication::GetWindowById(mus::Id id) {
  return root_->GetChildById(id);
}

bool WindowManagerApplication::WindowIsContainer(
    const mus::Window* window) const {
  return window && window->parent() == root_;
}

void WindowManagerApplication::AddAccelerators() {
  window_tree_host_->AddAccelerator(
      kWindowSwitchCmd,
      mus::CreateKeyMatcher(mus::mojom::KEYBOARD_CODE_TAB,
                            mus::mojom::EVENT_FLAGS_CONTROL_DOWN),
      base::Bind(&AssertTrue));
}

void WindowManagerApplication::OnAcceleratorRegistrarDestroyed(
    AcceleratorRegistrarImpl* registrar) {
  accelerator_registrars_.erase(registrar);
}

void WindowManagerApplication::Initialize(mojo::ApplicationImpl* app) {
  app_ = app;
  tracing_.Initialize(app);
  window_manager_.reset(new WindowManagerImpl());
  // Don't bind to the WindowManager immediately. Wait for OnEmbed() first.
  mus::mojom::WindowManagerPtr window_manager;
  requests_.push_back(
      make_scoped_ptr(new mojo::InterfaceRequest<mus::mojom::WindowManager>(
          mojo::GetProxy(&window_manager))));
  user_window_controller_.reset(new UserWindowControllerImpl());
  mus::mojom::WindowTreeHostClientPtr host_client;
  host_client_binding_.Bind(GetProxy(&host_client));
  mus::CreateSingleWindowTreeHost(app, std::move(host_client), this,
                                  &window_tree_host_, std::move(window_manager),
                                  window_manager_.get());
}

bool WindowManagerApplication::ConfigureIncomingConnection(
    mojo::ApplicationConnection* connection) {
  connection->AddService<mash::wm::mojom::UserWindowController>(this);
  connection->AddService<mus::mojom::AcceleratorRegistrar>(this);
  connection->AddService<mus::mojom::WindowManager>(this);
  return true;
}

void WindowManagerApplication::OnAccelerator(uint32_t id,
                                             mus::mojom::EventPtr event) {
  switch (id) {
    case kWindowSwitchCmd:
      window_tree_host_->ActivateNextWindow();
      break;
    default:
      for (auto* registrar : accelerator_registrars_) {
        if (registrar->OwnsAccelerator(id)) {
          registrar->ProcessAccelerator(id, std::move(event));
          break;
        }
      }
  }
}

void WindowManagerApplication::OnEmbed(mus::Window* root) {
  root_ = root;
  root_->AddObserver(this);
  CreateContainers();
  background_layout_.reset(new BackgroundLayout(
      GetWindowForContainer(mojom::CONTAINER_USER_BACKGROUND)));
  shelf_layout_.reset(
      new ShelfLayout(GetWindowForContainer(mojom::CONTAINER_USER_SHELF)));

  mus::Window* window = GetWindowForContainer(mojom::CONTAINER_USER_WINDOWS);
  window_layout_.reset(
      new WindowLayout(GetWindowForContainer(mojom::CONTAINER_USER_WINDOWS)));
  window_tree_host_->AddActivationParent(window->id());
  window_tree_host_->SetTitle("Mash");

  AddAccelerators();

  ui_init_.reset(new ui::mojo::UIInit(views::GetDisplaysFromWindow(root)));
  aura_init_.reset(new views::AuraInit(app_, "mash_wm_resources.pak"));
  window_manager_->Initialize(this);

  for (auto& request : requests_)
    window_manager_binding_.AddBinding(window_manager_.get(),
                                       std::move(*request));
  requests_.clear();

  user_window_controller_->Initialize(this);
  for (auto& request : user_window_controller_requests_)
    user_window_controller_binding_.AddBinding(user_window_controller_.get(),
                                               std::move(*request));
  user_window_controller_requests_.clear();

  shadow_controller_.reset(new ShadowController(root->connection()));
}

void WindowManagerApplication::OnConnectionLost(
    mus::WindowTreeConnection* connection) {
  // TODO(sky): shutdown.
  NOTIMPLEMENTED();
  shadow_controller_.reset();
}

void WindowManagerApplication::Create(
    mojo::ApplicationConnection* connection,
    mojo::InterfaceRequest<mash::wm::mojom::UserWindowController> request) {
  if (root_) {
    user_window_controller_binding_.AddBinding(user_window_controller_.get(),
                                               std::move(request));
  } else {
    user_window_controller_requests_.push_back(make_scoped_ptr(
        new mojo::InterfaceRequest<mash::wm::mojom::UserWindowController>(
            std::move(request))));
  }
}

void WindowManagerApplication::Create(
    mojo::ApplicationConnection* connection,
    mojo::InterfaceRequest<mus::mojom::AcceleratorRegistrar> request) {
  static int accelerator_registrar_count = 0;
  if (accelerator_registrar_count == std::numeric_limits<int>::max()) {
    // Restart from zero if we have reached the limit. It is technically
    // possible to end up with multiple active registrars with the same
    // namespace, but it is highly unlikely. In the event that multiple
    // registrars have the same namespace, this new registrar will be unable to
    // install accelerators.
    accelerator_registrar_count = 0;
  }
  accelerator_registrars_.insert(new AcceleratorRegistrarImpl(
      window_tree_host_.get(), ++accelerator_registrar_count,
      std::move(request),
      base::Bind(&WindowManagerApplication::OnAcceleratorRegistrarDestroyed,
                 base::Unretained(this))));
}

void WindowManagerApplication::Create(
    mojo::ApplicationConnection* connection,
    mojo::InterfaceRequest<mus::mojom::WindowManager> request) {
  if (root_) {
    window_manager_binding_.AddBinding(window_manager_.get(),
                                       std::move(request));
  } else {
    requests_.push_back(
        make_scoped_ptr(new mojo::InterfaceRequest<mus::mojom::WindowManager>(
            std::move(request))));
  }
}

void WindowManagerApplication::OnWindowDestroyed(mus::Window* window) {
  DCHECK_EQ(window, root_);
  root_->RemoveObserver(this);
  // Delete the |window_manager_| here so that WindowManager doesn't have to
  // worry about the possibility of |root_| being null.
  window_manager_.reset();
  root_ = nullptr;
}

void WindowManagerApplication::CreateContainers() {
  for (uint16_t container =
           static_cast<uint16_t>(mojom::CONTAINER_ALL_USER_BACKGROUND);
       container < static_cast<uint16_t>(mojom::CONTAINER_COUNT); ++container) {
    mus::Window* window = root_->connection()->NewWindow();
    DCHECK_EQ(mus::LoWord(window->id()), container)
        << "Containers must be created before other windows!";
    window->SetBounds(root_->bounds());
    window->SetVisible(true);
    root_->AddChild(window);
  }
}

}  // namespace wm
}  // namespace mash