Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / extensions / shell / browser / shell_desktop_controller.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 "extensions/shell/browser/shell_desktop_controller.h"
6
7 #include "base/command_line.h"
8 #include "content/public/browser/context_factory.h"
9 #include "extensions/shell/browser/shell_app_window_controller.h"
10 #include "extensions/shell/common/switches.h"
11 #include "ui/aura/client/cursor_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/test/test_screen.h"
16 #include "ui/aura/window.h"
17 #include "ui/aura/window_event_dispatcher.h"
18 #include "ui/aura/window_tree_host.h"
19 #include "ui/base/cursor/cursor.h"
20 #include "ui/base/cursor/image_cursors.h"
21 #include "ui/base/ime/input_method_initializer.h"
22 #include "ui/gfx/native_widget_types.h"
23 #include "ui/gfx/screen.h"
24 #include "ui/wm/core/base_focus_rules.h"
25 #include "ui/wm/core/compound_event_filter.h"
26 #include "ui/wm/core/cursor_manager.h"
27 #include "ui/wm/core/focus_controller.h"
28 #include "ui/wm/core/input_method_event_filter.h"
29 #include "ui/wm/core/native_cursor_manager.h"
30 #include "ui/wm/core/native_cursor_manager_delegate.h"
31 #include "ui/wm/core/user_activity_detector.h"
32
33 #if defined(OS_CHROMEOS)
34 #include "ui/chromeos/user_activity_power_manager_notifier.h"
35 #include "ui/display/types/chromeos/display_mode.h"
36 #include "ui/display/types/chromeos/display_snapshot.h"
37 #endif
38
39 namespace extensions {
40 namespace {
41
42 // A simple layout manager that makes each new window fill its parent.
43 class FillLayout : public aura::LayoutManager {
44  public:
45   FillLayout() {}
46   virtual ~FillLayout() {}
47
48  private:
49   // aura::LayoutManager:
50   virtual void OnWindowResized() OVERRIDE {}
51
52   virtual void OnWindowAddedToLayout(aura::Window* child) OVERRIDE {
53     if (!child->parent())
54       return;
55
56     // Create a rect at 0,0 with the size of the parent.
57     gfx::Size parent_size = child->parent()->bounds().size();
58     child->SetBounds(gfx::Rect(parent_size));
59   }
60
61   virtual void OnWillRemoveWindowFromLayout(aura::Window* child) OVERRIDE {}
62
63   virtual void OnWindowRemovedFromLayout(aura::Window* child) OVERRIDE {}
64
65   virtual void OnChildWindowVisibilityChanged(aura::Window* child,
66                                               bool visible) OVERRIDE {}
67
68   virtual void SetChildBounds(aura::Window* child,
69                               const gfx::Rect& requested_bounds) OVERRIDE {
70     SetChildBoundsDirect(child, requested_bounds);
71   }
72
73   DISALLOW_COPY_AND_ASSIGN(FillLayout);
74 };
75
76 // A class that bridges the gap between CursorManager and Aura. It borrows
77 // heavily from AshNativeCursorManager.
78 class ShellNativeCursorManager : public wm::NativeCursorManager {
79  public:
80   explicit ShellNativeCursorManager(aura::WindowTreeHost* host)
81       : host_(host), image_cursors_(new ui::ImageCursors) {}
82   virtual ~ShellNativeCursorManager() {}
83
84   // wm::NativeCursorManager overrides.
85   virtual void SetDisplay(const gfx::Display& display,
86                           wm::NativeCursorManagerDelegate* delegate) OVERRIDE {
87     if (image_cursors_->SetDisplay(display, display.device_scale_factor()))
88       SetCursor(delegate->GetCursor(), delegate);
89   }
90
91   virtual void SetCursor(gfx::NativeCursor cursor,
92                          wm::NativeCursorManagerDelegate* delegate) OVERRIDE {
93     image_cursors_->SetPlatformCursor(&cursor);
94     cursor.set_device_scale_factor(image_cursors_->GetScale());
95     delegate->CommitCursor(cursor);
96
97     if (delegate->IsCursorVisible())
98       ApplyCursor(cursor);
99   }
100
101   virtual void SetVisibility(
102       bool visible,
103       wm::NativeCursorManagerDelegate* delegate) OVERRIDE {
104     delegate->CommitVisibility(visible);
105
106     if (visible) {
107       SetCursor(delegate->GetCursor(), delegate);
108     } else {
109       gfx::NativeCursor invisible_cursor(ui::kCursorNone);
110       image_cursors_->SetPlatformCursor(&invisible_cursor);
111       ApplyCursor(invisible_cursor);
112     }
113   }
114
115   virtual void SetCursorSet(
116       ui::CursorSetType cursor_set,
117       wm::NativeCursorManagerDelegate* delegate) OVERRIDE {
118     image_cursors_->SetCursorSet(cursor_set);
119     delegate->CommitCursorSet(cursor_set);
120     if (delegate->IsCursorVisible())
121       SetCursor(delegate->GetCursor(), delegate);
122   }
123
124   virtual void SetMouseEventsEnabled(
125       bool enabled,
126       wm::NativeCursorManagerDelegate* delegate) OVERRIDE {
127     delegate->CommitMouseEventsEnabled(enabled);
128     SetVisibility(delegate->IsCursorVisible(), delegate);
129   }
130
131  private:
132   // Sets |cursor| as the active cursor within Aura.
133   void ApplyCursor(gfx::NativeCursor cursor) { host_->SetCursor(cursor); }
134
135   aura::WindowTreeHost* host_;  // Not owned.
136
137   scoped_ptr<ui::ImageCursors> image_cursors_;
138
139   DISALLOW_COPY_AND_ASSIGN(ShellNativeCursorManager);
140 };
141
142 class AppsFocusRules : public wm::BaseFocusRules {
143  public:
144   AppsFocusRules() {}
145   virtual ~AppsFocusRules() {}
146
147   virtual bool SupportsChildActivation(aura::Window* window) const OVERRIDE {
148     return true;
149   }
150
151  private:
152   DISALLOW_COPY_AND_ASSIGN(AppsFocusRules);
153 };
154
155 ShellDesktopController* g_instance = NULL;
156
157 }  // namespace
158
159 ShellDesktopController::ShellDesktopController() {
160 #if defined(OS_CHROMEOS)
161   display_configurator_.reset(new ui::DisplayConfigurator);
162   display_configurator_->Init(false);
163   display_configurator_->ForceInitialConfigure(0);
164   display_configurator_->AddObserver(this);
165 #endif
166   aura::Env::CreateInstance(true);
167   aura::Env::GetInstance()->set_context_factory(content::GetContextFactory());
168
169   g_instance = this;
170 }
171
172 ShellDesktopController::~ShellDesktopController() {
173   app_window_controller_.reset();
174   g_instance = NULL;
175   DestroyRootWindow();
176   aura::Env::DeleteInstance();
177 }
178
179 // static
180 ShellDesktopController* ShellDesktopController::instance() {
181   return g_instance;
182 }
183
184 void ShellDesktopController::SetAppWindowController(
185     ShellAppWindowController* app_window_controller) {
186   app_window_controller_.reset(app_window_controller);
187 }
188
189 ShellAppWindow* ShellDesktopController::CreateAppWindow(
190     content::BrowserContext* context) {
191   return app_window_controller_->CreateAppWindow(context);
192 }
193
194 void ShellDesktopController::CloseAppWindows() {
195   if (app_window_controller_)
196     app_window_controller_->CloseAppWindows();
197 }
198
199 aura::Window* ShellDesktopController::GetDefaultParent(
200     aura::Window* context,
201     aura::Window* window,
202     const gfx::Rect& bounds) {
203   return host_->window();
204 }
205
206 #if defined(OS_CHROMEOS)
207 void ShellDesktopController::OnDisplayModeChanged(
208     const std::vector<ui::DisplayConfigurator::DisplayState>& displays) {
209   gfx::Size size = GetPrimaryDisplaySize();
210   if (!size.IsEmpty())
211     host_->UpdateRootWindowSize(size);
212 }
213 #endif
214
215 void ShellDesktopController::OnHostCloseRequested(
216     const aura::WindowTreeHost* host) {
217   DCHECK_EQ(host_.get(), host);
218   CloseAppWindows();
219   base::MessageLoop::current()->PostTask(FROM_HERE,
220                                          base::MessageLoop::QuitClosure());
221 }
222
223 void ShellDesktopController::CreateRootWindow() {
224   // Set up basic pieces of ui::wm.
225   gfx::Size size;
226   base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
227   if (command_line->HasSwitch(switches::kAppShellHostWindowBounds)) {
228     const std::string size_str =
229         command_line->GetSwitchValueASCII(switches::kAppShellHostWindowBounds);
230     int width, height;
231     CHECK_EQ(2, sscanf(size_str.c_str(), "%dx%d", &width, &height));
232     size = gfx::Size(width, height);
233   } else {
234     size = GetPrimaryDisplaySize();
235   }
236   if (size.IsEmpty())
237     size = gfx::Size(1280, 720);
238
239   test_screen_.reset(aura::TestScreen::Create(size));
240   // TODO(jamescook): Replace this with a real Screen implementation.
241   gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE, test_screen_.get());
242   // TODO(mukai): Set up input method.
243
244   host_.reset(test_screen_->CreateHostForPrimaryDisplay());
245   host_->InitHost();
246   aura::client::SetWindowTreeClient(host_->window(), this);
247   root_window_event_filter_.reset(new wm::CompoundEventFilter);
248   host_->window()->AddPreTargetHandler(root_window_event_filter_.get());
249   InitWindowManager();
250
251   host_->AddObserver(this);
252
253   // Ensure the X window gets mapped.
254   host_->Show();
255 }
256
257 void ShellDesktopController::InitWindowManager() {
258   wm::FocusController* focus_controller =
259       new wm::FocusController(CreateFocusRules());
260   aura::client::SetFocusClient(host_->window(), focus_controller);
261   host_->window()->AddPreTargetHandler(focus_controller);
262   aura::client::SetActivationClient(host_->window(), focus_controller);
263   focus_client_.reset(focus_controller);
264
265   input_method_filter_.reset(
266       new wm::InputMethodEventFilter(host_->GetAcceleratedWidget()));
267   input_method_filter_->SetInputMethodPropertyInRootWindow(host_->window());
268   root_window_event_filter_->AddHandler(input_method_filter_.get());
269
270   capture_client_.reset(
271       new aura::client::DefaultCaptureClient(host_->window()));
272
273   // Ensure new windows fill the display.
274   host_->window()->SetLayoutManager(new FillLayout);
275
276   cursor_manager_.reset(
277       new wm::CursorManager(scoped_ptr<wm::NativeCursorManager>(
278           new ShellNativeCursorManager(host_.get()))));
279   cursor_manager_->SetDisplay(
280       gfx::Screen::GetNativeScreen()->GetPrimaryDisplay());
281   cursor_manager_->SetCursor(ui::kCursorPointer);
282   aura::client::SetCursorClient(host_->window(), cursor_manager_.get());
283
284   user_activity_detector_.reset(new wm::UserActivityDetector);
285   host_->event_processor()->GetRootTarget()->AddPreTargetHandler(
286       user_activity_detector_.get());
287 #if defined(OS_CHROMEOS)
288   user_activity_notifier_.reset(
289       new ui::UserActivityPowerManagerNotifier(user_activity_detector_.get()));
290 #endif
291 }
292
293 wm::FocusRules* ShellDesktopController::CreateFocusRules() {
294   return new AppsFocusRules();
295 }
296
297 void ShellDesktopController::DestroyRootWindow() {
298   host_->RemoveObserver(this);
299   if (input_method_filter_)
300     root_window_event_filter_->RemoveHandler(input_method_filter_.get());
301   if (user_activity_detector_) {
302     host_->event_processor()->GetRootTarget()->RemovePreTargetHandler(
303         user_activity_detector_.get());
304   }
305   wm::FocusController* focus_controller =
306       static_cast<wm::FocusController*>(focus_client_.get());
307   if (focus_controller) {
308     host_->window()->RemovePreTargetHandler(focus_controller);
309     aura::client::SetActivationClient(host_->window(), NULL);
310   }
311   root_window_event_filter_.reset();
312   capture_client_.reset();
313   input_method_filter_.reset();
314   focus_client_.reset();
315   cursor_manager_.reset();
316 #if defined(OS_CHROMEOS)
317   user_activity_notifier_.reset();
318 #endif
319   user_activity_detector_.reset();
320   host_.reset();
321 }
322
323 gfx::Size ShellDesktopController::GetPrimaryDisplaySize() {
324 #if defined(OS_CHROMEOS)
325   const std::vector<ui::DisplayConfigurator::DisplayState>& displays =
326       display_configurator_->cached_displays();
327   if (displays.empty())
328     return gfx::Size();
329   const ui::DisplayMode* mode = displays[0].display->current_mode();
330   return mode ? mode->size() : gfx::Size();
331 #else
332   return gfx::Size();
333 #endif
334 }
335
336 }  // namespace extensions