Convert env to a defaultdict in run_executable() to fix other callers of that function.
[chromium-blink-merge.git] / extensions / shell / browser / shell_browser_main_parts.cc
blob717305bbdc116a90c54e53f300f35a7e710a81d8
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.
5 #include "extensions/shell/browser/shell_browser_main_parts.h"
7 #include "base/command_line.h"
8 #include "base/run_loop.h"
9 #include "components/keyed_service/content/browser_context_dependency_manager.h"
10 #include "components/omaha_query_params/omaha_query_params.h"
11 #include "content/public/browser/child_process_security_policy.h"
12 #include "content/public/browser/context_factory.h"
13 #include "content/public/common/result_codes.h"
14 #include "content/shell/browser/shell_devtools_delegate.h"
15 #include "content/shell/browser/shell_net_log.h"
16 #include "extensions/browser/app_window/app_window_client.h"
17 #include "extensions/browser/browser_context_keyed_service_factories.h"
18 #include "extensions/browser/extension_system.h"
19 #include "extensions/common/constants.cc"
20 #include "extensions/common/switches.h"
21 #include "extensions/shell/browser/shell_browser_context.h"
22 #include "extensions/shell/browser/shell_browser_main_delegate.h"
23 #include "extensions/shell/browser/shell_desktop_controller.h"
24 #include "extensions/shell/browser/shell_device_client.h"
25 #include "extensions/shell/browser/shell_extension_system.h"
26 #include "extensions/shell/browser/shell_extension_system_factory.h"
27 #include "extensions/shell/browser/shell_extensions_browser_client.h"
28 #include "extensions/shell/browser/shell_omaha_query_params_delegate.h"
29 #include "extensions/shell/common/shell_extensions_client.h"
30 #include "extensions/shell/common/switches.h"
31 #include "ui/aura/env.h"
32 #include "ui/aura/window_tree_host.h"
33 #include "ui/base/ime/input_method_initializer.h"
34 #include "ui/base/resource/resource_bundle.h"
36 #if !defined(OS_ANDROID) && !defined(OS_IOS)
37 #include "components/storage_monitor/storage_monitor.h"
38 #endif
40 #if defined(OS_CHROMEOS)
41 #include "chromeos/audio/cras_audio_handler.h"
42 #include "chromeos/dbus/dbus_thread_manager.h"
43 #include "chromeos/network/network_handler.h"
44 #include "extensions/shell/browser/shell_audio_controller_chromeos.h"
45 #include "extensions/shell/browser/shell_network_controller_chromeos.h"
46 #endif
48 #if !defined(DISABLE_NACL)
49 #include "components/nacl/browser/nacl_browser.h"
50 #include "components/nacl/browser/nacl_process_host.h"
51 #include "content/public/browser/browser_thread.h"
52 #include "extensions/shell/browser/shell_nacl_browser_delegate.h"
53 #endif
55 using content::BrowserContext;
57 namespace extensions {
59 ShellBrowserMainParts::ShellBrowserMainParts(
60 const content::MainFunctionParams& parameters,
61 ShellBrowserMainDelegate* browser_main_delegate)
62 : extension_system_(NULL),
63 parameters_(parameters),
64 run_message_loop_(true),
65 browser_main_delegate_(browser_main_delegate) {
68 ShellBrowserMainParts::~ShellBrowserMainParts() {
71 void ShellBrowserMainParts::PreMainMessageLoopStart() {
72 // TODO(jamescook): Initialize touch here?
75 void ShellBrowserMainParts::PostMainMessageLoopStart() {
76 #if defined(OS_CHROMEOS)
77 // Perform initialization of D-Bus objects here rather than in the below
78 // helper classes so those classes' tests can initialize stub versions of the
79 // D-Bus objects.
80 chromeos::DBusThreadManager::Initialize();
82 chromeos::NetworkHandler::Initialize();
83 network_controller_.reset(new ShellNetworkController(
84 base::CommandLine::ForCurrentProcess()->GetSwitchValueNative(
85 switches::kAppShellPreferredNetwork)));
87 chromeos::CrasAudioHandler::Initialize(
88 new ShellAudioController::PrefHandler());
89 audio_controller_.reset(new ShellAudioController());
90 #else
91 // Non-Chrome OS platforms are for developer convenience, so use a test IME.
92 ui::InitializeInputMethodForTesting();
93 #endif
96 void ShellBrowserMainParts::PreEarlyInitialization() {
99 int ShellBrowserMainParts::PreCreateThreads() {
100 // TODO(jamescook): Initialize chromeos::CrosSettings here?
102 content::ChildProcessSecurityPolicy::GetInstance()->RegisterWebSafeScheme(
103 kExtensionScheme);
104 content::ChildProcessSecurityPolicy::GetInstance()->RegisterWebSafeScheme(
105 kExtensionResourceScheme);
107 // Return no error.
108 return 0;
111 void ShellBrowserMainParts::PreMainMessageLoopRun() {
112 // Initialize our "profile" equivalent.
113 browser_context_.reset(new ShellBrowserContext(net_log_.get()));
115 aura::Env::GetInstance()->set_context_factory(content::GetContextFactory());
117 #if !defined(OS_ANDROID) && !defined(OS_IOS)
118 storage_monitor::StorageMonitor::Create();
119 #endif
121 desktop_controller_.reset(browser_main_delegate_->CreateDesktopController());
123 // NOTE: Much of this is culled from chrome/test/base/chrome_test_suite.cc
124 // TODO(jamescook): Initialize user_manager::UserManager.
125 net_log_.reset(new content::ShellNetLog("app_shell"));
127 device_client_.reset(new ShellDeviceClient);
129 extensions_client_.reset(new ShellExtensionsClient());
130 ExtensionsClient::Set(extensions_client_.get());
132 extensions_browser_client_.reset(
133 new ShellExtensionsBrowserClient(browser_context_.get()));
134 ExtensionsBrowserClient::Set(extensions_browser_client_.get());
136 omaha_query_params_delegate_.reset(new ShellOmahaQueryParamsDelegate);
137 omaha_query_params::OmahaQueryParams::SetDelegate(
138 omaha_query_params_delegate_.get());
140 // Create our custom ExtensionSystem first because other
141 // KeyedServices depend on it.
142 // TODO(yoz): Move this after EnsureBrowserContextKeyedServiceFactoriesBuilt.
143 CreateExtensionSystem();
145 // Register additional KeyedService factories here. See
146 // ChromeBrowserMainExtraPartsProfiles for details.
147 EnsureBrowserContextKeyedServiceFactoriesBuilt();
148 ShellExtensionSystemFactory::GetInstance();
150 BrowserContextDependencyManager::GetInstance()->CreateBrowserContextServices(
151 browser_context_.get());
153 #if !defined(DISABLE_NACL)
154 // Takes ownership.
155 nacl::NaClBrowser::SetDelegate(
156 new ShellNaClBrowserDelegate(browser_context_.get()));
157 content::BrowserThread::PostTask(
158 content::BrowserThread::IO,
159 FROM_HERE,
160 base::Bind(nacl::NaClProcessHost::EarlyStartup));
161 #endif
163 devtools_delegate_.reset(
164 new content::ShellDevToolsDelegate(browser_context_.get()));
165 if (parameters_.ui_task) {
166 // For running browser tests.
167 parameters_.ui_task->Run();
168 delete parameters_.ui_task;
169 run_message_loop_ = false;
170 } else {
171 browser_main_delegate_->Start(browser_context_.get());
175 bool ShellBrowserMainParts::MainMessageLoopRun(int* result_code) {
176 if (!run_message_loop_)
177 return true;
178 // TODO(yoz): just return false here?
179 base::RunLoop run_loop;
180 run_loop.Run();
181 *result_code = content::RESULT_CODE_NORMAL_EXIT;
182 return true;
185 void ShellBrowserMainParts::PostMainMessageLoopRun() {
186 browser_main_delegate_->Shutdown();
188 BrowserContextDependencyManager::GetInstance()->DestroyBrowserContextServices(
189 browser_context_.get());
190 extension_system_ = NULL;
191 ExtensionsBrowserClient::Set(NULL);
192 extensions_browser_client_.reset();
193 browser_context_.reset();
195 desktop_controller_.reset();
197 #if !defined(OS_ANDROID) && !defined(OS_IOS)
198 storage_monitor::StorageMonitor::Destroy();
199 #endif
202 void ShellBrowserMainParts::PostDestroyThreads() {
203 #if defined(OS_CHROMEOS)
204 audio_controller_.reset();
205 chromeos::CrasAudioHandler::Shutdown();
206 network_controller_.reset();
207 chromeos::NetworkHandler::Shutdown();
208 chromeos::DBusThreadManager::Shutdown();
209 #endif
212 void ShellBrowserMainParts::CreateExtensionSystem() {
213 DCHECK(browser_context_);
214 extension_system_ = static_cast<ShellExtensionSystem*>(
215 ExtensionSystem::Get(browser_context_.get()));
216 extension_system_->InitForRegularProfile(true);
219 } // namespace extensions