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 "athena/main/public/athena_launcher.h"
6
7#include "athena/activity/public/activity_factory.h"
8#include "athena/activity/public/activity_manager.h"
9#include "athena/content/public/app_registry.h"
10#include "athena/content/public/content_activity_factory_creator.h"
11#include "athena/env/public/athena_env.h"
12#include "athena/extensions/public/extension_app_model_builder.h"
13#include "athena/extensions/public/extensions_delegate.h"
14#include "athena/home/public/home_card.h"
15#include "athena/input/public/input_manager.h"
16#include "athena/main/athena_views_delegate.h"
17#include "athena/main/placeholder.h"
18#include "athena/main/placeholder.h"
19#include "athena/main/url_search_provider.h"
20#include "athena/resource_manager/public/resource_manager.h"
21#include "athena/screen/public/screen_manager.h"
22#include "athena/system/public/system_ui.h"
23#include "athena/virtual_keyboard/public/virtual_keyboard_manager.h"
24#include "athena/wm/public/window_manager.h"
25#include "base/command_line.h"
26#include "base/memory/scoped_ptr.h"
27#include "content/public/common/content_switches.h"
28#include "ui/app_list/app_list_switches.h"
29#include "ui/aura/window_property.h"
30#include "ui/aura/window_tree_host.h"
31#include "ui/keyboard/keyboard_controller.h"
32#include "ui/keyboard/keyboard_controller_observer.h"
33#include "ui/native_theme/native_theme_switches.h"
34#include "ui/wm/core/visibility_controller.h"
35
36#if defined(USE_X11)
37#include "ui/events/x/touch_factory_x11.h"
38#endif
39
40namespace athena {
41struct AthenaEnvState;
42}
43
44DECLARE_WINDOW_PROPERTY_TYPE(athena::AthenaEnvState*);
45
46namespace athena {
47
48namespace {
49
50bool session_started = false;
51
52}  // namespace
53
54class VirtualKeyboardObserver;
55
56// Athena's env state.
57struct AthenaEnvState {
58  scoped_ptr< ::wm::VisibilityController> visibility_client;
59  scoped_ptr<VirtualKeyboardObserver> virtual_keyboard_observer;
60};
61
62DEFINE_OWNED_WINDOW_PROPERTY_KEY(athena::AthenaEnvState,
63                                 kAthenaEnvStateKey,
64                                 NULL);
65
66// This class observes the change of the virtual keyboard and distribute the
67// change to appropriate modules of athena.
68// TODO(oshima): move the VK bounds logic to screen manager.
69class VirtualKeyboardObserver : public keyboard::KeyboardControllerObserver {
70 public:
71  VirtualKeyboardObserver() {
72    keyboard::KeyboardController::GetInstance()->AddObserver(this);
73  }
74
75  virtual ~VirtualKeyboardObserver() {
76    keyboard::KeyboardController::GetInstance()->RemoveObserver(this);
77  }
78
79 private:
80  virtual void OnKeyboardBoundsChanging(const gfx::Rect& new_bounds) OVERRIDE {
81    HomeCard::Get()->UpdateVirtualKeyboardBounds(new_bounds);
82  }
83
84  DISALLOW_COPY_AND_ASSIGN(VirtualKeyboardObserver);
85};
86
87void StartAthenaEnv(scoped_refptr<base::TaskRunner> blocking_task_runner) {
88  athena::AthenaEnv::Create();
89
90  base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
91
92  // Force showing in the experimental app-list view.
93  command_line->AppendSwitch(app_list::switches::kEnableExperimentalAppList);
94  command_line->AppendSwitch(switches::kEnableOverlayScrollbar);
95
96  // Enable vertical overscroll.
97  command_line->AppendSwitchASCII(switches::kScrollEndEffect, "1");
98
99#if defined(USE_X11)
100  ui::TouchFactory::SetTouchDeviceListFromCommandLine();
101#endif
102
103  CreateAthenaViewsDelegate();
104
105  AthenaEnvState* env_state = new AthenaEnvState;
106
107  // Setup VisibilityClient
108  env_state->visibility_client.reset(new ::wm::VisibilityController);
109  aura::Window* root_window = athena::AthenaEnv::Get()->GetHost()->window();
110
111  aura::client::SetVisibilityClient(root_window,
112                                    env_state->visibility_client.get());
113
114  athena::InputManager::Create()->OnRootWindowCreated(root_window);
115  athena::ScreenManager::Create(root_window);
116  athena::SystemUI::Create(blocking_task_runner);
117  athena::WindowManager::Create();
118  athena::AppRegistry::Create();
119  SetupBackgroundImage();
120
121  athena::ScreenManager::Get()->GetContext()->SetProperty(
122      kAthenaEnvStateKey, env_state);
123}
124
125void CreateVirtualKeyboardWithContext(content::BrowserContext* context) {
126  athena::VirtualKeyboardManager::Create(context);
127}
128
129void StartAthenaSessionWithContext(content::BrowserContext* context) {
130  StartAthenaSession(athena::CreateContentActivityFactory(),
131                     new athena::ExtensionAppModelBuilder(context));
132  athena::HomeCard::Get()->RegisterSearchProvider(
133      new athena::UrlSearchProvider(context));
134  AthenaEnvState* env_state =
135      athena::ScreenManager::Get()->GetContext()->GetProperty(
136          kAthenaEnvStateKey);
137
138  env_state->virtual_keyboard_observer.reset(new VirtualKeyboardObserver);
139  CreateTestPages(context);
140}
141
142void StartAthenaSession(athena::ActivityFactory* activity_factory,
143                        athena::AppModelBuilder* app_model_builder) {
144  DCHECK(!session_started);
145  session_started = true;
146  athena::HomeCard::Create(app_model_builder);
147  athena::ActivityManager::Create();
148  athena::ResourceManager::Create();
149  athena::ActivityFactory::RegisterActivityFactory(activity_factory);
150}
151
152void ShutdownAthena() {
153  if (session_started) {
154    athena::ActivityFactory::Shutdown();
155    athena::ResourceManager::Shutdown();
156    athena::ActivityManager::Shutdown();
157    athena::HomeCard::Shutdown();
158    session_started = false;
159  }
160  athena::AppRegistry::ShutDown();
161  athena::WindowManager::Shutdown();
162  athena::SystemUI::Shutdown();
163  athena::ScreenManager::Shutdown();
164  athena::InputManager::Shutdown();
165  athena::ExtensionsDelegate::Shutdown();
166  athena::AthenaEnv::Shutdown();
167
168  ShutdownAthenaViewsDelegate();
169}
170
171}  // namespace athena
172