native_viewport_impl.cc revision 03b57e008b61dfcb1fbad3aea950ae0e001748b0
1// Copyright 2014 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "mojo/services/native_viewport/native_viewport_impl.h"
6
7#include "base/bind.h"
8#include "base/macros.h"
9#include "base/message_loop/message_loop.h"
10#include "base/time/time.h"
11#include "mojo/public/cpp/application/application_delegate.h"
12#include "mojo/public/cpp/application/interface_factory.h"
13#include "mojo/services/public/cpp/geometry/geometry_type_converters.h"
14#include "mojo/services/public/cpp/input_events/input_events_type_converters.h"
15#include "ui/events/event.h"
16
17namespace mojo {
18namespace {
19
20bool IsRateLimitedEventType(ui::Event* event) {
21  return event->type() == ui::ET_MOUSE_MOVED ||
22         event->type() == ui::ET_MOUSE_DRAGGED ||
23         event->type() == ui::ET_TOUCH_MOVED;
24}
25
26}  // namespace
27
28NativeViewportImpl::NativeViewportImpl()
29      : widget_(gfx::kNullAcceleratedWidget),
30        waiting_for_event_ack_(false),
31        weak_factory_(this) {}
32
33NativeViewportImpl::~NativeViewportImpl() {
34  // Destroy the NativeViewport early on as it may call us back during
35  // destruction and we want to be in a known state.
36  platform_viewport_.reset();
37}
38
39void NativeViewportImpl::Create(RectPtr bounds) {
40  platform_viewport_ = PlatformViewport::Create(this);
41  platform_viewport_->Init(bounds.To<gfx::Rect>());
42  OnBoundsChanged(bounds.To<gfx::Rect>());
43}
44
45void NativeViewportImpl::Show() {
46  platform_viewport_->Show();
47}
48
49void NativeViewportImpl::Hide() {
50  platform_viewport_->Hide();
51}
52
53void NativeViewportImpl::Close() {
54  DCHECK(platform_viewport_);
55  platform_viewport_->Close();
56}
57
58void NativeViewportImpl::SetBounds(RectPtr bounds) {
59  platform_viewport_->SetBounds(bounds.To<gfx::Rect>());
60}
61
62void NativeViewportImpl::OnBoundsChanged(const gfx::Rect& bounds) {
63  client()->OnBoundsChanged(Rect::From(bounds));
64}
65
66void NativeViewportImpl::OnAcceleratedWidgetAvailable(
67    gfx::AcceleratedWidget widget) {
68  widget_ = widget;
69  uintptr_t widget_ptr = bit_cast<uintptr_t>(widget);
70  client()->OnCreated(static_cast<uint64_t>(widget_ptr));
71}
72
73bool NativeViewportImpl::OnEvent(ui::Event* ui_event) {
74  // Must not return early before updating capture.
75  switch (ui_event->type()) {
76    case ui::ET_MOUSE_PRESSED:
77    case ui::ET_TOUCH_PRESSED:
78      platform_viewport_->SetCapture();
79      break;
80    case ui::ET_MOUSE_RELEASED:
81    case ui::ET_TOUCH_RELEASED:
82      platform_viewport_->ReleaseCapture();
83      break;
84    default:
85      break;
86  }
87
88  if (waiting_for_event_ack_ && IsRateLimitedEventType(ui_event))
89    return false;
90
91  client()->OnEvent(
92      TypeConverter<EventPtr, ui::Event>::ConvertFrom(*ui_event),
93      base::Bind(&NativeViewportImpl::AckEvent,
94                 weak_factory_.GetWeakPtr()));
95  waiting_for_event_ack_ = true;
96  return false;
97}
98
99void NativeViewportImpl::OnDestroyed() {
100  client()->OnDestroyed();
101}
102
103void NativeViewportImpl::AckEvent() {
104  waiting_for_event_ack_ = false;
105}
106
107}  // namespace mojo
108
109