Refactor management of overview window copy lifetime into a separate class.
[chromium-blink-merge.git] / content / shell / browser / shell_aura.cc
blob5b9cff529b744b81e016fb9e486e43ed21982957
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"
25 namespace content {
27 namespace {
29 class FillLayout : public aura::LayoutManager {
30 public:
31 explicit FillLayout(aura::RootWindow* root)
32 : root_(root) {
35 virtual ~FillLayout() {}
37 private:
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 {
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());
78 virtual ~MinimalInputEventFilter() {
79 root_->RemovePreTargetHandler(this);
80 root_->SetProperty(aura::client::kRootWindowInputMethodKey,
81 static_cast<ui::InputMethod*>(NULL));
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();
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);
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);
121 aura::RootWindow* root_;
122 scoped_ptr<ui::InputMethod> input_method_;
124 DISALLOW_COPY_AND_ASSIGN(MinimalInputEventFilter);
129 ShellAuraPlatformData::ShellAuraPlatformData() {
132 ShellAuraPlatformData::~ShellAuraPlatformData() {
135 void ShellAuraPlatformData::CreateWindow(int width, int height) {
136 aura::TestScreen* screen = aura::TestScreen::Create();
137 gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE, screen);
138 root_window_.reset(screen->CreateRootWindowForPrimaryDisplay());
139 root_window_->SetHostSize(gfx::Size(width, height));
140 root_window_->ShowRootWindow();
141 root_window_->SetLayoutManager(new FillLayout(root_window_.get()));
143 focus_client_.reset(new aura::test::TestFocusClient());
144 aura::client::SetFocusClient(root_window_.get(), focus_client_.get());
146 activation_client_.reset(
147 new aura::client::DefaultActivationClient(root_window_.get()));
148 capture_client_.reset(
149 new aura::client::DefaultCaptureClient(root_window_.get()));
150 window_tree_client_.reset(
151 new aura::test::TestWindowTreeClient(root_window_.get()));
152 ime_filter_.reset(new MinimalInputEventFilter(root_window_.get()));
155 // static
156 void Shell::PlatformInitialize(const gfx::Size& default_window_size) {
157 aura::Env::CreateInstance();
160 void Shell::PlatformExit() {
161 aura::Env::DeleteInstance();
164 void Shell::PlatformCleanUp() {
167 void Shell::PlatformEnableUIControl(UIControl control, bool is_enabled) {
170 void Shell::PlatformSetAddressBarURL(const GURL& url) {
173 void Shell::PlatformSetIsLoading(bool loading) {
176 void Shell::PlatformCreateWindow(int width, int height) {
177 platform_.reset(new ShellAuraPlatformData());
178 platform_->CreateWindow(width, height);
181 void Shell::PlatformSetContents() {
182 platform_->window()->AddChild(web_contents_->GetView()->GetNativeView());
183 web_contents_->GetView()->GetNativeView()->Show();
186 void Shell::PlatformResizeSubViews() {
189 void Shell::Close() {
190 web_contents_.reset();
191 platform_.reset();
194 void Shell::PlatformSetTitle(const string16& title) {
197 } // namespace content