- add sources.
[platform/framework/web/crosswalk.git] / src / content / shell / browser / shell_aura.cc
1 // Copyright 2013 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 "content/shell/browser/shell.h"
6
7 #include "content/public/browser/web_contents.h"
8 #include "content/public/browser/web_contents_view.h"
9 #include "content/shell/browser/shell_aura.h"
10 #include "ui/aura/client/aura_constants.h"
11 #include "ui/aura/client/default_activation_client.h"
12 #include "ui/aura/client/default_capture_client.h"
13 #include "ui/aura/env.h"
14 #include "ui/aura/layout_manager.h"
15 #include "ui/aura/root_window.h"
16 #include "ui/aura/test/test_focus_client.h"
17 #include "ui/aura/test/test_screen.h"
18 #include "ui/aura/test/test_window_tree_client.h"
19 #include "ui/aura/window.h"
20 #include "ui/base/ime/input_method.h"
21 #include "ui/base/ime/input_method_delegate.h"
22 #include "ui/base/ime/input_method_factory.h"
23 #include "ui/gfx/screen.h"
24
25 namespace content {
26
27 namespace {
28
29 class FillLayout : public aura::LayoutManager {
30  public:
31   explicit FillLayout(aura::RootWindow* root)
32       : root_(root) {
33   }
34
35   virtual ~FillLayout() {}
36
37  private:
38   // aura::LayoutManager:
39   virtual void OnWindowResized() OVERRIDE {
40   }
41
42   virtual void OnWindowAddedToLayout(aura::Window* child) OVERRIDE {
43     child->SetBounds(gfx::Rect(root_->GetHostSize()));
44   }
45
46   virtual void OnWillRemoveWindowFromLayout(aura::Window* child) OVERRIDE {
47   }
48
49   virtual void OnWindowRemovedFromLayout(aura::Window* child) OVERRIDE {
50   }
51
52   virtual void OnChildWindowVisibilityChanged(aura::Window* child,
53                                               bool visible) OVERRIDE {
54   }
55
56   virtual void SetChildBounds(aura::Window* child,
57                               const gfx::Rect& requested_bounds) OVERRIDE {
58     SetChildBoundsDirect(child, requested_bounds);
59   }
60
61   aura::RootWindow* root_;
62
63   DISALLOW_COPY_AND_ASSIGN(FillLayout);
64 };
65
66 class MinimalInputEventFilter : public ui::internal::InputMethodDelegate,
67                                 public ui::EventHandler {
68  public:
69   explicit MinimalInputEventFilter(aura::RootWindow* root)
70       : root_(root),
71         input_method_(ui::CreateInputMethod(this, NULL)) {
72     input_method_->Init(true);
73     root_->AddPreTargetHandler(this);
74     root_->SetProperty(aura::client::kRootWindowInputMethodKey,
75                        input_method_.get());
76   }
77
78   virtual ~MinimalInputEventFilter() {
79     root_->RemovePreTargetHandler(this);
80     root_->SetProperty(aura::client::kRootWindowInputMethodKey,
81                        static_cast<ui::InputMethod*>(NULL));
82   }
83
84  private:
85   // ui::EventHandler:
86   virtual void OnKeyEvent(ui::KeyEvent* event) OVERRIDE {
87     const ui::EventType type = event->type();
88     if (type == ui::ET_TRANSLATED_KEY_PRESS ||
89         type == ui::ET_TRANSLATED_KEY_RELEASE) {
90       // The |event| is already handled by this object, change the type of the
91       // event to ui::ET_KEY_* and pass it to the next filter.
92       static_cast<ui::TranslatedKeyEvent*>(event)->ConvertToKeyEvent();
93     } else {
94       bool handled = false;
95       if (event->HasNativeEvent())
96         handled = input_method_->DispatchKeyEvent(event->native_event());
97       else
98         handled = input_method_->DispatchFabricatedKeyEvent(*event);
99       if (handled)
100         event->StopPropagation();
101     }
102   }
103
104   // ui::InputMethodDelegate:
105   virtual bool DispatchKeyEventPostIME(
106       const base::NativeEvent& event) OVERRIDE {
107     ui::TranslatedKeyEvent aura_event(event, false /* is_char */);
108     return root_->AsRootWindowHostDelegate()->OnHostKeyEvent(
109         &aura_event);
110   }
111
112   virtual bool DispatchFabricatedKeyEventPostIME(ui::EventType type,
113                                                  ui::KeyboardCode key_code,
114                                                  int flags) OVERRIDE {
115     ui::TranslatedKeyEvent aura_event(type == ui::ET_KEY_PRESSED, key_code,
116                                       flags);
117     return root_->AsRootWindowHostDelegate()->OnHostKeyEvent(
118         &aura_event);
119   }
120
121   aura::RootWindow* root_;
122   scoped_ptr<ui::InputMethod> input_method_;
123
124   DISALLOW_COPY_AND_ASSIGN(MinimalInputEventFilter);
125 };
126
127 }
128
129 ShellAuraPlatformData* Shell::platform_ = NULL;
130
131 ShellAuraPlatformData::ShellAuraPlatformData() {
132   aura::TestScreen* screen = aura::TestScreen::Create();
133   gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE, screen);
134   root_window_.reset(screen->CreateRootWindowForPrimaryDisplay());
135   root_window_->ShowRootWindow();
136   root_window_->SetLayoutManager(new FillLayout(root_window_.get()));
137
138   focus_client_.reset(new aura::test::TestFocusClient());
139   aura::client::SetFocusClient(root_window_.get(), focus_client_.get());
140
141   activation_client_.reset(
142       new aura::client::DefaultActivationClient(root_window_.get()));
143   capture_client_.reset(
144       new aura::client::DefaultCaptureClient(root_window_.get()));
145   window_tree_client_.reset(
146       new aura::test::TestWindowTreeClient(root_window_.get()));
147   ime_filter_.reset(new MinimalInputEventFilter(root_window_.get()));
148 }
149
150 ShellAuraPlatformData::~ShellAuraPlatformData() {
151 }
152
153 void ShellAuraPlatformData::ResizeWindow(int width, int height) {
154   root_window_->SetHostSize(gfx::Size(width, height));
155 }
156
157 // static
158 void Shell::PlatformInitialize(const gfx::Size& default_window_size) {
159   CHECK(!platform_);
160   aura::Env::CreateInstance();
161   platform_ = new ShellAuraPlatformData();
162 }
163
164 void Shell::PlatformExit() {
165   CHECK(platform_);
166   delete platform_;
167   platform_ = NULL;
168   aura::Env::DeleteInstance();
169 }
170
171 void Shell::PlatformCleanUp() {
172 }
173
174 void Shell::PlatformEnableUIControl(UIControl control, bool is_enabled) {
175 }
176
177 void Shell::PlatformSetAddressBarURL(const GURL& url) {
178 }
179
180 void Shell::PlatformSetIsLoading(bool loading) {
181 }
182
183 void Shell::PlatformCreateWindow(int width, int height) {
184   CHECK(platform_);
185   platform_->ResizeWindow(width, height);
186 }
187
188 void Shell::PlatformSetContents() {
189   CHECK(platform_);
190   aura::Window* content = web_contents_->GetView()->GetNativeView();
191   aura::Window* parent = platform_->window();
192   if (parent->Contains(content))
193     return;
194   parent->AddChild(content);
195   content->Show();
196 }
197
198 void Shell::PlatformResizeSubViews() {
199 }
200
201 void Shell::Close() {
202   web_contents_.reset();
203 }
204
205 void Shell::PlatformSetTitle(const string16& title) {
206 }
207
208 }  // namespace content