Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / athena / main / athena_main.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 "athena/content/public/content_activity_factory.h"
6 #include "athena/content/public/content_app_model_builder.h"
7 #include "athena/content/public/web_contents_view_delegate_creator.h"
8 #include "athena/home/public/home_card.h"
9 #include "athena/main/athena_app_window_controller.h"
10 #include "athena/main/athena_launcher.h"
11 #include "athena/main/debug/debug_window.h"
12 #include "athena/main/placeholder.h"
13 #include "athena/main/url_search_provider.h"
14 #include "athena/screen/public/screen_manager.h"
15 #include "athena/virtual_keyboard/public/virtual_keyboard_manager.h"
16 #include "base/command_line.h"
17 #include "base/file_util.h"
18 #include "base/path_service.h"
19 #include "content/public/app/content_main.h"
20 #include "extensions/shell/app/shell_main_delegate.h"
21 #include "extensions/shell/browser/shell_browser_main_delegate.h"
22 #include "extensions/shell/browser/shell_content_browser_client.h"
23 #include "extensions/shell/browser/shell_desktop_controller.h"
24 #include "extensions/shell/browser/shell_extension_system.h"
25 #include "extensions/shell/common/switches.h"
26 #include "extensions/shell/renderer/shell_renderer_main_delegate.h"
27 #include "ui/app_list/app_list_switches.h"
28 #include "ui/aura/window_tree_host.h"
29 #include "ui/base/resource/resource_bundle.h"
30 #include "ui/keyboard/keyboard_controller.h"
31 #include "ui/keyboard/keyboard_controller_observer.h"
32 #include "ui/native_theme/native_theme_switches.h"
33 #include "ui/wm/core/visibility_controller.h"
34
35 namespace {
36
37 // We want to load the sample calculator app by default, for a while. Expecting
38 // to run athena_main at src/
39 const char kDefaultAppPath[] =
40     "chrome/common/extensions/docs/examples/apps/calculator/app";
41 }  // namespace
42
43 // This class observes the change of the virtual keyboard and distribute the
44 // change to appropriate modules of athena.
45 // TODO(oshima): move the VK bounds logic to screen manager.
46 class VirtualKeyboardObserver : public keyboard::KeyboardControllerObserver {
47  public:
48   VirtualKeyboardObserver() {
49     keyboard::KeyboardController::GetInstance()->AddObserver(this);
50   }
51
52   virtual ~VirtualKeyboardObserver() {
53     keyboard::KeyboardController::GetInstance()->RemoveObserver(this);
54   }
55
56  private:
57   virtual void OnKeyboardBoundsChanging(const gfx::Rect& new_bounds) OVERRIDE {
58     athena::HomeCard::Get()->UpdateVirtualKeyboardBounds(new_bounds);
59   }
60
61   DISALLOW_COPY_AND_ASSIGN(VirtualKeyboardObserver);
62 };
63
64 class AthenaDesktopController : public extensions::ShellDesktopController {
65  public:
66   AthenaDesktopController() {}
67   virtual ~AthenaDesktopController() {}
68
69  private:
70   // extensions::ShellDesktopController:
71   virtual wm::FocusRules* CreateFocusRules() OVERRIDE {
72     return athena::ScreenManager::CreateFocusRules();
73   }
74
75   DISALLOW_COPY_AND_ASSIGN(AthenaDesktopController);
76 };
77
78 class AthenaBrowserMainDelegate : public extensions::ShellBrowserMainDelegate {
79  public:
80   AthenaBrowserMainDelegate() {}
81   virtual ~AthenaBrowserMainDelegate() {}
82
83   // extensions::ShellBrowserMainDelegate:
84   virtual void Start(content::BrowserContext* context) OVERRIDE {
85     base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
86
87     // Force showing in the experimental app-list view.
88     command_line->AppendSwitch(app_list::switches::kEnableExperimentalAppList);
89     command_line->AppendSwitch(switches::kEnableOverlayScrollbar);
90
91     base::FilePath app_dir = base::FilePath::FromUTF8Unsafe(
92         command_line->HasSwitch(extensions::switches::kAppShellAppPath)
93             ? command_line->GetSwitchValueNative(
94                   extensions::switches::kAppShellAppPath)
95             : kDefaultAppPath);
96
97     base::FilePath app_absolute_dir = base::MakeAbsoluteFilePath(app_dir);
98     if (base::DirectoryExists(app_absolute_dir)) {
99       extensions::ShellExtensionSystem* extension_system =
100           static_cast<extensions::ShellExtensionSystem*>(
101               extensions::ExtensionSystem::Get(context));
102       extension_system->LoadApp(app_absolute_dir);
103     }
104
105     athena::StartAthena(
106         extensions::ShellDesktopController::instance()->host()->window(),
107         new athena::ContentActivityFactory(),
108         new athena::ContentAppModelBuilder(context));
109     athena::HomeCard::Get()->RegisterSearchProvider(
110         new athena::UrlSearchProvider(context));
111     athena::VirtualKeyboardManager::Create(context);
112     keyboard_observer_.reset(new VirtualKeyboardObserver());
113
114     CreateTestPages(context);
115     CreateDebugWindow();
116   }
117
118   virtual void Shutdown() OVERRIDE {
119     keyboard_observer_.reset();
120     athena::ShutdownAthena();
121   }
122
123   virtual extensions::ShellDesktopController* CreateDesktopController()
124       OVERRIDE {
125     // TODO(mukai): create Athena's own ShellDesktopController subclass so that
126     // it can initialize its own window manager logic.
127     extensions::ShellDesktopController* desktop = new AthenaDesktopController();
128     desktop->SetAppWindowController(new athena::AthenaAppWindowController());
129     return desktop;
130   }
131
132  private:
133   scoped_ptr<VirtualKeyboardObserver> keyboard_observer_;
134
135   DISALLOW_COPY_AND_ASSIGN(AthenaBrowserMainDelegate);
136 };
137
138 class AthenaContentBrowserClient
139     : public extensions::ShellContentBrowserClient {
140  public:
141   AthenaContentBrowserClient()
142       : extensions::ShellContentBrowserClient(new AthenaBrowserMainDelegate()) {
143   }
144   virtual ~AthenaContentBrowserClient() {}
145
146   // content::ContentBrowserClient:
147   virtual content::WebContentsViewDelegate* GetWebContentsViewDelegate(
148       content::WebContents* web_contents) OVERRIDE {
149     return athena::CreateWebContentsViewDelegate(web_contents);
150   }
151
152  private:
153   DISALLOW_COPY_AND_ASSIGN(AthenaContentBrowserClient);
154 };
155
156 class AthenaRendererMainDelegate
157     : public extensions::ShellRendererMainDelegate {
158  public:
159   AthenaRendererMainDelegate() {}
160   virtual ~AthenaRendererMainDelegate() {}
161
162  private:
163   // extensions::ShellRendererMainDelegate:
164   virtual void OnThreadStarted(content::RenderThread* thread) OVERRIDE {}
165
166   virtual void OnViewCreated(content::RenderView* render_view) OVERRIDE {
167   }
168
169   DISALLOW_COPY_AND_ASSIGN(AthenaRendererMainDelegate);
170 };
171
172 class AthenaMainDelegate : public extensions::ShellMainDelegate {
173  public:
174   AthenaMainDelegate() {}
175   virtual ~AthenaMainDelegate() {}
176
177  private:
178   // extensions::ShellMainDelegate:
179   virtual content::ContentBrowserClient* CreateShellContentBrowserClient()
180       OVERRIDE {
181     return new AthenaContentBrowserClient();
182   }
183
184   virtual scoped_ptr<extensions::ShellRendererMainDelegate>
185   CreateShellRendererMainDelegate() OVERRIDE {
186     return scoped_ptr<extensions::ShellRendererMainDelegate>(
187         new AthenaRendererMainDelegate());
188   }
189
190   virtual void InitializeResourceBundle() OVERRIDE {
191     base::FilePath pak_dir;
192     PathService::Get(base::DIR_MODULE, &pak_dir);
193     base::FilePath pak_file =
194         pak_dir.Append(FILE_PATH_LITERAL("athena_resources.pak"));
195     ui::ResourceBundle::InitSharedInstanceWithPakPath(pak_file);
196   }
197
198   DISALLOW_COPY_AND_ASSIGN(AthenaMainDelegate);
199 };
200
201 int main(int argc, const char** argv) {
202   AthenaMainDelegate delegate;
203   content::ContentMainParams params(&delegate);
204
205   params.argc = argc;
206   params.argv = argv;
207
208   return content::ContentMain(params);
209 }