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/platform_viewport.h"
6
7#include "ui/events/event.h"
8#include "ui/events/platform/platform_event_dispatcher.h"
9#include "ui/events/platform/platform_event_source.h"
10#include "ui/ozone/public/cursor_factory_ozone.h"
11#include "ui/ozone/public/ozone_platform.h"
12#include "ui/ozone/public/surface_factory_ozone.h"
13#include "ui/platform_window/platform_window.h"
14#include "ui/platform_window/platform_window_delegate.h"
15
16namespace mojo {
17
18// TODO(spang): Deduplicate with PlatformViewportX11.. but there's a hack
19// in there that prevents this.
20class PlatformViewportOzone : public PlatformViewport,
21                              public ui::PlatformWindowDelegate {
22 public:
23  explicit PlatformViewportOzone(Delegate* delegate) : delegate_(delegate) {
24    ui::OzonePlatform::InitializeForUI();
25  }
26
27  virtual ~PlatformViewportOzone() {
28    // Destroy the platform-window while |this| is still alive.
29    platform_window_.reset();
30  }
31
32 private:
33  // Overridden from PlatformViewport:
34  virtual void Init(const gfx::Rect& bounds) OVERRIDE {
35    platform_window_ =
36        ui::OzonePlatform::GetInstance()->CreatePlatformWindow(this, bounds);
37  }
38
39  virtual void Show() OVERRIDE { platform_window_->Show(); }
40
41  virtual void Hide() OVERRIDE { platform_window_->Hide(); }
42
43  virtual void Close() OVERRIDE { platform_window_->Close(); }
44
45  virtual gfx::Size GetSize() OVERRIDE {
46    return platform_window_->GetBounds().size();
47  }
48
49  virtual void SetBounds(const gfx::Rect& bounds) OVERRIDE {
50    platform_window_->SetBounds(bounds);
51  }
52
53  virtual void SetCapture() OVERRIDE { platform_window_->SetCapture(); }
54
55  virtual void ReleaseCapture() OVERRIDE { platform_window_->ReleaseCapture(); }
56
57  // ui::PlatformWindowDelegate:
58  virtual void OnBoundsChanged(const gfx::Rect& new_bounds) OVERRIDE {
59    delegate_->OnBoundsChanged(new_bounds);
60  }
61
62  virtual void OnDamageRect(const gfx::Rect& damaged_region) OVERRIDE {}
63
64  virtual void DispatchEvent(ui::Event* event) OVERRIDE {
65    delegate_->OnEvent(event);
66  }
67
68  virtual void OnCloseRequest() OVERRIDE { platform_window_->Close(); }
69
70  virtual void OnClosed() OVERRIDE { delegate_->OnDestroyed(); }
71
72  virtual void OnWindowStateChanged(ui::PlatformWindowState state) OVERRIDE {}
73
74  virtual void OnLostCapture() OVERRIDE {}
75
76  virtual void OnAcceleratedWidgetAvailable(
77      gfx::AcceleratedWidget widget) OVERRIDE {
78    delegate_->OnAcceleratedWidgetAvailable(widget);
79  }
80
81  virtual void OnActivationChanged(bool active) OVERRIDE {}
82
83  scoped_ptr<ui::PlatformWindow> platform_window_;
84  Delegate* delegate_;
85
86  DISALLOW_COPY_AND_ASSIGN(PlatformViewportOzone);
87};
88
89// static
90scoped_ptr<PlatformViewport> PlatformViewport::Create(Delegate* delegate) {
91  return scoped_ptr<PlatformViewport>(
92      new PlatformViewportOzone(delegate)).Pass();
93}
94
95}  // namespace mojo
96