Upstream version 5.34.98.0
[platform/framework/web/crosswalk.git] / src / apps / shell / shell_browser_main_parts.cc
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.
4
5 #include "apps/shell/shell_browser_main_parts.h"
6
7 #include "apps/shell/shell_browser_context.h"
8 #include "apps/shell/shell_extension_system.h"
9 #include "apps/shell/shell_extensions_browser_client.h"
10 #include "apps/shell/shell_extensions_client.h"
11 #include "apps/shell/web_view_window.h"
12 #include "base/command_line.h"
13 #include "base/files/file_path.h"
14 #include "base/path_service.h"
15 #include "base/run_loop.h"
16 #include "chrome/browser/extensions/extension_system.h"
17 #include "chrome/common/chrome_paths.h"
18 #include "components/browser_context_keyed_service/browser_context_dependency_manager.h"
19 #include "content/public/common/result_codes.h"
20 #include "extensions/browser/renderer_startup_helper.h"
21 #include "extensions/common/extension_paths.h"
22 #include "ui/aura/env.h"
23 #include "ui/aura/root_window.h"
24 #include "ui/aura/test/test_screen.h"
25 #include "ui/base/ime/input_method_initializer.h"
26 #include "ui/base/resource/resource_bundle.h"
27 #include "ui/gfx/screen.h"
28 #include "ui/wm/test/wm_test_helper.h"
29
30 #if defined(OS_CHROMEOS)
31 #include "chromeos/chromeos_paths.h"
32 #endif
33
34 using content::BrowserContext;
35 using extensions::Extension;
36 using extensions::ExtensionSystem;
37 using extensions::ShellExtensionSystem;
38
39 namespace apps {
40 namespace {
41
42 // Register additional BrowserContextKeyedService factories here. See
43 // ChromeBrowserMainExtraPartsProfiles for details.
44 void EnsureBrowserContextKeyedServiceFactoriesBuilt() {
45   extensions::RendererStartupHelperFactory::GetInstance();
46 }
47
48 }  // namespace
49
50 ShellBrowserMainParts::ShellBrowserMainParts(
51     const content::MainFunctionParams& parameters)
52     : extension_system_(NULL) {
53 }
54
55 ShellBrowserMainParts::~ShellBrowserMainParts() {
56 }
57
58 void ShellBrowserMainParts::PreMainMessageLoopStart() {
59   // TODO(jamescook): Initialize touch here?
60 }
61
62 void ShellBrowserMainParts::PostMainMessageLoopStart() {
63 }
64
65 void ShellBrowserMainParts::PreEarlyInitialization() {
66 }
67
68 int ShellBrowserMainParts::PreCreateThreads() {
69   // TODO(jamescook): Initialize chromeos::CrosSettings here?
70
71   // Return no error.
72   return 0;
73 }
74
75 void ShellBrowserMainParts::PreMainMessageLoopRun() {
76   // NOTE: Much of this is culled from chrome/test/base/chrome_test_suite.cc
77   // Set up all the paths to load files.
78   chrome::RegisterPathProvider();
79 #if defined(OS_CHROMEOS)
80   chromeos::RegisterPathProvider();
81 #endif
82   extensions::RegisterPathProvider();
83
84   // The extensions system needs manifest data from the Chrome PAK file.
85   base::FilePath resources_pack_path;
86   PathService::Get(chrome::FILE_RESOURCES_PACK, &resources_pack_path);
87   ResourceBundle::GetSharedInstance().AddDataPackFromPath(
88       resources_pack_path, ui::SCALE_FACTOR_NONE);
89
90   // TODO(jamescook): Initialize chromeos::UserManager.
91
92   // Initialize our "profile" equivalent.
93   browser_context_.reset(new ShellBrowserContext);
94
95   extensions_client_.reset(new ShellExtensionsClient());
96   extensions::ExtensionsClient::Set(extensions_client_.get());
97
98   extensions_browser_client_.reset(
99       new extensions::ShellExtensionsBrowserClient(browser_context_.get()));
100   extensions::ExtensionsBrowserClient::Set(extensions_browser_client_.get());
101
102   // Create our custom ExtensionSystem first because other
103   // BrowserContextKeyedServices depend on it.
104   CreateExtensionSystem();
105
106   EnsureBrowserContextKeyedServiceFactoriesBuilt();
107   BrowserContextDependencyManager::GetInstance()->CreateBrowserContextServices(
108       browser_context_.get());
109
110   CreateRootWindow();
111
112   const std::string kAppSwitch = "app";
113   CommandLine* command_line = CommandLine::ForCurrentProcess();
114   if (command_line->HasSwitch(kAppSwitch)) {
115     base::FilePath app_dir(command_line->GetSwitchValueNative(kAppSwitch));
116     extension_system_->LoadAndLaunchApp(app_dir);
117   } else {
118     // TODO(jamescook): For demo purposes create a window with a WebView just
119     // to ensure that the content module is properly initialized.
120     ShowWebViewWindow(browser_context_.get(),
121                       wm_test_helper_->root_window()->window());
122   }
123 }
124
125 bool ShellBrowserMainParts::MainMessageLoopRun(int* result_code)  {
126   base::RunLoop run_loop;
127   run_loop.Run();
128   *result_code = content::RESULT_CODE_NORMAL_EXIT;
129   return true;
130 }
131
132 void ShellBrowserMainParts::PostMainMessageLoopRun() {
133   DestroyRootWindow();
134   BrowserContextDependencyManager::GetInstance()->DestroyBrowserContextServices(
135       browser_context_.get());
136   extension_system_ = NULL;
137   extensions::ExtensionsBrowserClient::Set(NULL);
138   extensions_browser_client_.reset();
139   browser_context_.reset();
140   aura::Env::DeleteInstance();
141 }
142
143 void ShellBrowserMainParts::OnWindowTreeHostCloseRequested(
144     const aura::RootWindow* root) {
145   base::MessageLoop::current()->PostTask(FROM_HERE,
146                                          base::MessageLoop::QuitClosure());
147 }
148
149 void ShellBrowserMainParts::CreateRootWindow() {
150   test_screen_.reset(aura::TestScreen::Create());
151   // TODO(jamescook): Replace this with a real Screen implementation.
152   gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE, test_screen_.get());
153   // TODO(jamescook): Initialize a real input method.
154   ui::InitializeInputMethodForTesting();
155   // Set up basic pieces of views::corewm.
156   wm_test_helper_.reset(new wm::WMTestHelper(gfx::Size(800, 600)));
157   // Ensure the X window gets mapped.
158   wm_test_helper_->root_window()->host()->Show();
159   // Watch for the user clicking the close box.
160   wm_test_helper_->root_window()->AddRootWindowObserver(this);
161 }
162
163 void ShellBrowserMainParts::DestroyRootWindow() {
164   wm_test_helper_->root_window()->RemoveRootWindowObserver(this);
165   wm_test_helper_->root_window()->PrepareForShutdown();
166   wm_test_helper_.reset();
167   ui::ShutdownInputMethodForTesting();
168 }
169
170 void ShellBrowserMainParts::CreateExtensionSystem() {
171   DCHECK(browser_context_);
172   extension_system_ = static_cast<ShellExtensionSystem*>(
173       ExtensionSystem::GetForBrowserContext(browser_context_.get()));
174   extension_system_->InitForRegularProfile(true);
175 }
176
177 }  // namespace apps