Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / mojo / views / native_widget_view_manager.cc
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/views/native_widget_view_manager.h"
6
7 #include "mojo/aura/window_tree_host_mojo.h"
8 #include "mojo/services/public/cpp/input_events/input_events_type_converters.h"
9 #include "ui/aura/client/aura_constants.h"
10 #include "ui/aura/client/default_capture_client.h"
11 #include "ui/aura/window.h"
12 #include "ui/aura/window_event_dispatcher.h"
13 #include "ui/base/ime/input_method.h"
14 #include "ui/base/ime/input_method_delegate.h"
15 #include "ui/base/ime/input_method_factory.h"
16 #include "ui/base/ime/text_input_client.h"
17 #include "ui/wm/core/base_focus_rules.h"
18 #include "ui/wm/core/capture_controller.h"
19 #include "ui/wm/core/focus_controller.h"
20
21 namespace mojo {
22 namespace {
23
24 // TODO: figure out what this should be.
25 class FocusRulesImpl : public wm::BaseFocusRules {
26  public:
27   FocusRulesImpl() {}
28   virtual ~FocusRulesImpl() {}
29
30   virtual bool SupportsChildActivation(aura::Window* window) const OVERRIDE {
31     return true;
32   }
33
34  private:
35   DISALLOW_COPY_AND_ASSIGN(FocusRulesImpl);
36 };
37
38 class MinimalInputEventFilter : public ui::internal::InputMethodDelegate,
39                                 public ui::EventHandler {
40  public:
41   explicit MinimalInputEventFilter(aura::Window* root)
42       : root_(root) {
43     ui::InitializeInputMethodForTesting();
44     input_method_ = ui::CreateInputMethod(this, gfx::kNullAcceleratedWidget);
45     input_method_->Init(true);
46     root_->AddPreTargetHandler(this);
47     root_->SetProperty(aura::client::kRootWindowInputMethodKey,
48                        input_method_.get());
49   }
50
51   virtual ~MinimalInputEventFilter() {
52     root_->RemovePreTargetHandler(this);
53     root_->SetProperty(aura::client::kRootWindowInputMethodKey,
54                        static_cast<ui::InputMethod*>(NULL));
55   }
56
57  private:
58   // ui::EventHandler:
59   virtual void OnKeyEvent(ui::KeyEvent* event) OVERRIDE {
60     // See the comment in InputMethodEventFilter::OnKeyEvent() for details.
61     if (event->IsTranslated()) {
62       event->SetTranslated(false);
63     } else {
64       if (input_method_->DispatchKeyEvent(*event))
65         event->StopPropagation();
66     }
67   }
68
69   // ui::internal::InputMethodDelegate:
70   virtual bool DispatchKeyEventPostIME(const ui::KeyEvent& event) OVERRIDE {
71     // See the comment in InputMethodEventFilter::DispatchKeyEventPostIME() for
72     // details.
73     ui::KeyEvent aura_event(event);
74     aura_event.SetTranslated(true);
75     ui::EventDispatchDetails details =
76         root_->GetHost()->dispatcher()->OnEventFromSource(&aura_event);
77     return aura_event.handled() || details.dispatcher_destroyed;
78   }
79
80   aura::Window* root_;
81   scoped_ptr<ui::InputMethod> input_method_;
82
83   DISALLOW_COPY_AND_ASSIGN(MinimalInputEventFilter);
84 };
85
86 }  // namespace
87
88 NativeWidgetViewManager::NativeWidgetViewManager(
89     views::internal::NativeWidgetDelegate* delegate, View* view)
90     : NativeWidgetAura(delegate),
91       view_(view) {
92   view_->AddObserver(this);
93   window_tree_host_.reset(new WindowTreeHostMojo(view_, this));
94   window_tree_host_->InitHost();
95
96   ime_filter_.reset(
97       new MinimalInputEventFilter(window_tree_host_->window()));
98
99   focus_client_.reset(new wm::FocusController(new FocusRulesImpl));
100
101   aura::client::SetFocusClient(window_tree_host_->window(),
102                                focus_client_.get());
103   aura::client::SetActivationClient(window_tree_host_->window(),
104                                     focus_client_.get());
105   window_tree_host_->window()->AddPreTargetHandler(focus_client_.get());
106
107   aura::client::SetCaptureClient(
108       window_tree_host_->window(),
109       new aura::client::DefaultCaptureClient(window_tree_host_->window()));
110 }
111
112 NativeWidgetViewManager::~NativeWidgetViewManager() {
113   if (view_)
114     view_->RemoveObserver(this);
115 }
116
117 void NativeWidgetViewManager::InitNativeWidget(
118     const views::Widget::InitParams& in_params) {
119   views::Widget::InitParams params(in_params);
120   params.parent = window_tree_host_->window();
121   NativeWidgetAura::InitNativeWidget(params);
122   capture_client_.reset(
123       new wm::ScopedCaptureClient(window_tree_host_->window()));
124 }
125
126 void NativeWidgetViewManager::CompositorContentsChanged(
127     const SkBitmap& bitmap) {
128   if (view_)
129     view_->SetContents(bitmap);
130 }
131
132 void NativeWidgetViewManager::OnViewDestroyed(View* view) {
133   DCHECK_EQ(view, view_);
134   view->RemoveObserver(this);
135   view_ = NULL;
136   window_tree_host_.reset();
137 }
138
139 void NativeWidgetViewManager::OnViewBoundsChanged(View* view,
140                                                   const gfx::Rect& old_bounds,
141                                                   const gfx::Rect& new_bounds) {
142   GetWidget()->SetBounds(gfx::Rect(view->bounds().size()));
143 }
144
145 void NativeWidgetViewManager::OnViewInputEvent(View* view,
146                                                const EventPtr& event) {
147   scoped_ptr<ui::Event> ui_event(event.To<scoped_ptr<ui::Event> >());
148   if (ui_event)
149     window_tree_host_->SendEventToProcessor(ui_event.get());
150 }
151
152 }  // namespace mojo