summaryrefslogtreecommitdiffstats
path: root/content/renderer/mus/compositor_mus_connection.cc
blob: 9e0813e3b86e95444febdad82d725821dc06d846 (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
// 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 "content/renderer/mus/compositor_mus_connection.h"

#include "base/single_thread_task_runner.h"
#include "content/common/input/web_input_event_traits.h"
#include "content/renderer/input/input_handler_manager.h"
#include "content/renderer/mus/render_widget_mus_connection.h"
#include "mojo/converters/blink/blink_input_events_type_converters.h"
#include "ui/events/latency_info.h"

namespace content {

CompositorMusConnection::CompositorMusConnection(
    int routing_id,
    const scoped_refptr<base::SingleThreadTaskRunner>& main_task_runner,
    const scoped_refptr<base::SingleThreadTaskRunner>& compositor_task_runner,
    mojo::InterfaceRequest<mus::mojom::WindowTreeClient> request,
    InputHandlerManager* input_handler_manager)
    : routing_id_(routing_id),
      root_(nullptr),
      main_task_runner_(main_task_runner),
      compositor_task_runner_(compositor_task_runner),
      input_handler_manager_(input_handler_manager) {
  DCHECK(main_task_runner_->BelongsToCurrentThread());
  compositor_task_runner_->PostTask(
      FROM_HERE, base::Bind(&CompositorMusConnection::
                                CreateWindowTreeConnectionOnCompositorThread,
                            this, base::Passed(std::move(request))));
}

void CompositorMusConnection::AttachSurfaceOnMainThread(
    scoped_ptr<mus::WindowSurfaceBinding> surface_binding) {
  DCHECK(main_task_runner_->BelongsToCurrentThread());
  compositor_task_runner_->PostTask(
      FROM_HERE,
      base::Bind(&CompositorMusConnection::AttachSurfaceOnCompositorThread,
                 this, base::Passed(std::move(surface_binding))));
}

CompositorMusConnection::~CompositorMusConnection() {}

void CompositorMusConnection::AttachSurfaceOnCompositorThread(
    scoped_ptr<mus::WindowSurfaceBinding> surface_binding) {
  DCHECK(compositor_task_runner_->BelongsToCurrentThread());
  window_surface_binding_ = std::move(surface_binding);
  if (root_) {
    root_->AttachSurface(mus::mojom::SURFACE_TYPE_DEFAULT,
                         std::move(window_surface_binding_));
  }
}

void CompositorMusConnection::CreateWindowTreeConnectionOnCompositorThread(
    mojo::InterfaceRequest<mus::mojom::WindowTreeClient> request) {
  DCHECK(compositor_task_runner_->BelongsToCurrentThread());
  mus::WindowTreeConnection::Create(
      this, std::move(request),
      mus::WindowTreeConnection::CreateType::DONT_WAIT_FOR_EMBED);
}

void CompositorMusConnection::OnConnectionLostOnMainThread() {
  DCHECK(main_task_runner_->BelongsToCurrentThread());
  RenderWidgetMusConnection* connection =
      RenderWidgetMusConnection::Get(routing_id_);
  if (!connection)
    return;
  connection->OnConnectionLost();
}

void CompositorMusConnection::OnWindowInputEventOnMainThread(
    scoped_ptr<blink::WebInputEvent> web_event,
    const base::Closure& ack) {
  DCHECK(main_task_runner_->BelongsToCurrentThread());
  RenderWidgetMusConnection* connection =
      RenderWidgetMusConnection::Get(routing_id_);
  if (!connection) {
    ack.Run();
    return;
  }
  connection->OnWindowInputEvent(std::move(web_event), ack);
}

void CompositorMusConnection::OnWindowInputEventAckOnMainThread(
    const base::Closure& ack) {
  DCHECK(main_task_runner_->BelongsToCurrentThread());
  compositor_task_runner_->PostTask(FROM_HERE, ack);
}

void CompositorMusConnection::OnConnectionLost(
    mus::WindowTreeConnection* connection) {
  DCHECK(compositor_task_runner_->BelongsToCurrentThread());
  main_task_runner_->PostTask(
      FROM_HERE,
      base::Bind(&CompositorMusConnection::OnConnectionLostOnMainThread, this));
}

void CompositorMusConnection::OnEmbed(mus::Window* root) {
  DCHECK(compositor_task_runner_->BelongsToCurrentThread());
  root_ = root;
  root_->AddObserver(this);
  if (window_surface_binding_) {
    root->AttachSurface(mus::mojom::SURFACE_TYPE_DEFAULT,
                        std::move(window_surface_binding_));
  }
}

void CompositorMusConnection::OnWindowInputEvent(
    mus::Window* window,
    const mus::mojom::EventPtr& event) {
  DCHECK(compositor_task_runner_->BelongsToCurrentThread());
  scoped_ptr<blink::WebInputEvent> web_event =
      event.To<scoped_ptr<blink::WebInputEvent>>();
  // TODO(sad): We probably need to plumb LatencyInfo through Mus.
  ui::LatencyInfo info;
  InputEventAckState ack_state = input_handler_manager_->HandleInputEvent(
      routing_id_, web_event.get(), &info);
  if (ack_state != INPUT_EVENT_ACK_STATE_NOT_CONSUMED)
    return;
  // TODO(sad): Do something more useful once we can do async acks.
  base::Closure ack = base::Bind(&base::DoNothing);
  const bool send_ack =
      WebInputEventTraits::WillReceiveAckFromRenderer(*web_event);
  if (send_ack) {
    // Ultimately, this ACK needs to go back to the Mus client lib which is not
    // thread-safe and lives on the compositor thread. For ACKs that are passed
    // to the main thread we pass them back to the compositor thread via
    // OnWindowInputEventAckOnMainThread.
    ack = base::Bind(
        &CompositorMusConnection::OnWindowInputEventAckOnMainThread, this, ack);
  }
  main_task_runner_->PostTask(
      FROM_HERE,
      base::Bind(&CompositorMusConnection::OnWindowInputEventOnMainThread, this,
                 base::Passed(std::move(web_event)), ack));
}

}  // namespace content