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:
FillLayout(aura::RootWindow * root)31 explicit FillLayout(aura::RootWindow* root)
32 : root_(root) {
33 }
34
~FillLayout()35 virtual ~FillLayout() {}
36
37 private:
38 // aura::LayoutManager:
OnWindowResized()39 virtual void OnWindowResized() OVERRIDE {
40 }
41
OnWindowAddedToLayout(aura::Window * child)42 virtual void OnWindowAddedToLayout(aura::Window* child) OVERRIDE {
43 child->SetBounds(gfx::Rect(root_->host()->GetBounds().size()));
44 }
45
OnWillRemoveWindowFromLayout(aura::Window * child)46 virtual void OnWillRemoveWindowFromLayout(aura::Window* child) OVERRIDE {
47 }
48
OnWindowRemovedFromLayout(aura::Window * child)49 virtual void OnWindowRemovedFromLayout(aura::Window* child) OVERRIDE {
50 }
51
OnChildWindowVisibilityChanged(aura::Window * child,bool visible)52 virtual void OnChildWindowVisibilityChanged(aura::Window* child,
53 bool visible) OVERRIDE {
54 }
55
SetChildBounds(aura::Window * child,const gfx::Rect & requested_bounds)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:
MinimalInputEventFilter(aura::RootWindow * root)69 explicit MinimalInputEventFilter(aura::RootWindow* root)
70 : root_(root),
71 input_method_(ui::CreateInputMethod(this,
72 gfx::kNullAcceleratedWidget)) {
73 input_method_->Init(true);
74 root_->window()->AddPreTargetHandler(this);
75 root_->window()->SetProperty(aura::client::kRootWindowInputMethodKey,
76 input_method_.get());
77 }
78
~MinimalInputEventFilter()79 virtual ~MinimalInputEventFilter() {
80 root_->window()->RemovePreTargetHandler(this);
81 root_->window()->SetProperty(aura::client::kRootWindowInputMethodKey,
82 static_cast<ui::InputMethod*>(NULL));
83 }
84
85 private:
86 // ui::EventHandler:
OnKeyEvent(ui::KeyEvent * event)87 virtual void OnKeyEvent(ui::KeyEvent* event) OVERRIDE {
88 const ui::EventType type = event->type();
89 if (type == ui::ET_TRANSLATED_KEY_PRESS ||
90 type == ui::ET_TRANSLATED_KEY_RELEASE) {
91 // The |event| is already handled by this object, change the type of the
92 // event to ui::ET_KEY_* and pass it to the next filter.
93 static_cast<ui::TranslatedKeyEvent*>(event)->ConvertToKeyEvent();
94 } else {
95 if (input_method_->DispatchKeyEvent(*event))
96 event->StopPropagation();
97 }
98 }
99
100 // ui::InputMethodDelegate:
DispatchKeyEventPostIME(const base::NativeEvent & event)101 virtual bool DispatchKeyEventPostIME(
102 const base::NativeEvent& event) OVERRIDE {
103 ui::TranslatedKeyEvent aura_event(event, false /* is_char */);
104 return root_->AsRootWindowHostDelegate()->OnHostKeyEvent(
105 &aura_event);
106 }
107
DispatchFabricatedKeyEventPostIME(ui::EventType type,ui::KeyboardCode key_code,int flags)108 virtual bool DispatchFabricatedKeyEventPostIME(ui::EventType type,
109 ui::KeyboardCode key_code,
110 int flags) OVERRIDE {
111 ui::TranslatedKeyEvent aura_event(type == ui::ET_KEY_PRESSED, key_code,
112 flags);
113 return root_->AsRootWindowHostDelegate()->OnHostKeyEvent(
114 &aura_event);
115 }
116
117 aura::RootWindow* root_;
118 scoped_ptr<ui::InputMethod> input_method_;
119
120 DISALLOW_COPY_AND_ASSIGN(MinimalInputEventFilter);
121 };
122
123 }
124
125 ShellAuraPlatformData* Shell::platform_ = NULL;
126
ShellAuraPlatformData()127 ShellAuraPlatformData::ShellAuraPlatformData() {
128 aura::TestScreen* screen = aura::TestScreen::Create();
129 gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE, screen);
130 root_window_.reset(screen->CreateRootWindowForPrimaryDisplay());
131 root_window_->host()->Show();
132 root_window_->window()->SetLayoutManager(new FillLayout(root_window_.get()));
133
134 focus_client_.reset(new aura::test::TestFocusClient());
135 aura::client::SetFocusClient(root_window_->window(), focus_client_.get());
136
137 activation_client_.reset(
138 new aura::client::DefaultActivationClient(root_window_->window()));
139 capture_client_.reset(
140 new aura::client::DefaultCaptureClient(root_window_->window()));
141 window_tree_client_.reset(
142 new aura::test::TestWindowTreeClient(root_window_->window()));
143 ime_filter_.reset(new MinimalInputEventFilter(root_window_.get()));
144 }
145
~ShellAuraPlatformData()146 ShellAuraPlatformData::~ShellAuraPlatformData() {
147 }
148
ResizeWindow(int width,int height)149 void ShellAuraPlatformData::ResizeWindow(int width, int height) {
150 root_window_->SetHostSize(gfx::Size(width, height));
151 }
152
153 // static
PlatformInitialize(const gfx::Size & default_window_size)154 void Shell::PlatformInitialize(const gfx::Size& default_window_size) {
155 CHECK(!platform_);
156 aura::Env::CreateInstance();
157 platform_ = new ShellAuraPlatformData();
158 }
159
PlatformExit()160 void Shell::PlatformExit() {
161 CHECK(platform_);
162 delete platform_;
163 platform_ = NULL;
164 aura::Env::DeleteInstance();
165 }
166
PlatformCleanUp()167 void Shell::PlatformCleanUp() {
168 }
169
PlatformEnableUIControl(UIControl control,bool is_enabled)170 void Shell::PlatformEnableUIControl(UIControl control, bool is_enabled) {
171 }
172
PlatformSetAddressBarURL(const GURL & url)173 void Shell::PlatformSetAddressBarURL(const GURL& url) {
174 }
175
PlatformSetIsLoading(bool loading)176 void Shell::PlatformSetIsLoading(bool loading) {
177 }
178
PlatformCreateWindow(int width,int height)179 void Shell::PlatformCreateWindow(int width, int height) {
180 CHECK(platform_);
181 platform_->ResizeWindow(width, height);
182 }
183
PlatformSetContents()184 void Shell::PlatformSetContents() {
185 CHECK(platform_);
186 aura::Window* content = web_contents_->GetView()->GetNativeView();
187 aura::Window* parent = platform_->window()->window();
188 if (parent->Contains(content))
189 return;
190 parent->AddChild(content);
191 content->Show();
192 }
193
PlatformResizeSubViews()194 void Shell::PlatformResizeSubViews() {
195 }
196
Close()197 void Shell::Close() {
198 web_contents_.reset();
199 }
200
PlatformSetTitle(const base::string16 & title)201 void Shell::PlatformSetTitle(const base::string16& title) {
202 }
203
204 } // namespace content
205