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.
5 #include "content/shell/browser/shell.h"
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"
29 class FillLayout : public aura::LayoutManager {
31 explicit FillLayout(aura::RootWindow* root)
35 virtual ~FillLayout() {}
38 // aura::LayoutManager:
39 virtual void OnWindowResized() OVERRIDE {
42 virtual void OnWindowAddedToLayout(aura::Window* child) OVERRIDE {
43 child->SetBounds(gfx::Rect(root_->GetHostSize()));
46 virtual void OnWillRemoveWindowFromLayout(aura::Window* child) OVERRIDE {
49 virtual void OnWindowRemovedFromLayout(aura::Window* child) OVERRIDE {
52 virtual void OnChildWindowVisibilityChanged(aura::Window* child,
53 bool visible) OVERRIDE {
56 virtual void SetChildBounds(aura::Window* child,
57 const gfx::Rect& requested_bounds) OVERRIDE {
58 SetChildBoundsDirect(child, requested_bounds);
61 aura::RootWindow* root_;
63 DISALLOW_COPY_AND_ASSIGN(FillLayout);
66 class MinimalInputEventFilter : public ui::internal::InputMethodDelegate,
67 public ui::EventHandler {
69 explicit MinimalInputEventFilter(aura::RootWindow* root)
71 input_method_(ui::CreateInputMethod(this, NULL)) {
72 input_method_->Init(true);
73 root_->AddPreTargetHandler(this);
74 root_->SetProperty(aura::client::kRootWindowInputMethodKey,
78 virtual ~MinimalInputEventFilter() {
79 root_->RemovePreTargetHandler(this);
80 root_->SetProperty(aura::client::kRootWindowInputMethodKey,
81 static_cast<ui::InputMethod*>(NULL));
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();
95 if (event->HasNativeEvent())
96 handled = input_method_->DispatchKeyEvent(event->native_event());
98 handled = input_method_->DispatchFabricatedKeyEvent(*event);
100 event->StopPropagation();
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(
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,
117 return root_->AsRootWindowHostDelegate()->OnHostKeyEvent(
121 aura::RootWindow* root_;
122 scoped_ptr<ui::InputMethod> input_method_;
124 DISALLOW_COPY_AND_ASSIGN(MinimalInputEventFilter);
129 ShellAuraPlatformData* Shell::platform_ = NULL;
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()));
138 focus_client_.reset(new aura::test::TestFocusClient());
139 aura::client::SetFocusClient(root_window_.get(), focus_client_.get());
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()));
150 ShellAuraPlatformData::~ShellAuraPlatformData() {
153 void ShellAuraPlatformData::ResizeWindow(int width, int height) {
154 root_window_->SetHostSize(gfx::Size(width, height));
158 void Shell::PlatformInitialize(const gfx::Size& default_window_size) {
160 aura::Env::CreateInstance();
161 platform_ = new ShellAuraPlatformData();
164 void Shell::PlatformExit() {
168 aura::Env::DeleteInstance();
171 void Shell::PlatformCleanUp() {
174 void Shell::PlatformEnableUIControl(UIControl control, bool is_enabled) {
177 void Shell::PlatformSetAddressBarURL(const GURL& url) {
180 void Shell::PlatformSetIsLoading(bool loading) {
183 void Shell::PlatformCreateWindow(int width, int height) {
185 platform_->ResizeWindow(width, height);
188 void Shell::PlatformSetContents() {
190 aura::Window* content = web_contents_->GetView()->GetNativeView();
191 aura::Window* parent = platform_->window();
192 if (parent->Contains(content))
194 parent->AddChild(content);
198 void Shell::PlatformResizeSubViews() {
201 void Shell::Close() {
202 web_contents_.reset();
205 void Shell::PlatformSetTitle(const string16& title) {
208 } // namespace content