blob: f96fef1c2ec4c5ba93adf8e4bca57df4a2a85c9b [file] [log] [blame]
[email protected]c65aafcc2012-01-04 15:05:501// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
[email protected]e6b5bc22011-09-08 22:01:565#include "chrome/browser/chrome_browser_main.h"
[email protected]5c9587c2008-12-09 21:20:166
[email protected]1bcdb532009-01-16 17:47:577#include <algorithm>
[email protected]01109dcf2010-03-27 00:56:438#include <string>
9#include <vector>
[email protected]a1a130f7d2009-01-09 20:28:4410
[email protected]e2cc2e62010-08-28 00:26:3711#include "base/at_exit.h"
[email protected]97f3ac632011-10-19 20:09:5612#include "base/bind.h"
[email protected]1bcdb532009-01-16 17:47:5713#include "base/command_line.h"
[email protected]58580352010-10-26 04:07:5014#include "base/debug/trace_event.h"
[email protected]f805fe82010-08-03 22:47:1015#include "base/file_path.h"
[email protected]1bcdb532009-01-16 17:47:5716#include "base/file_util.h"
[email protected]835d7c82010-10-14 04:38:3817#include "base/metrics/field_trial.h"
18#include "base/metrics/histogram.h"
[email protected]1bcdb532009-01-16 17:47:5719#include "base/path_service.h"
[email protected]843bc462012-06-19 17:43:3120#include "base/process_info.h"
[email protected]1bcdb532009-01-16 17:47:5721#include "base/process_util.h"
[email protected]8e937c1e2012-06-28 22:57:3022#include "base/run_loop.h"
[email protected]e83326f2010-07-31 17:29:2523#include "base/string_number_conversions.h"
[email protected]1bcdb532009-01-16 17:47:5724#include "base/string_piece.h"
[email protected]4e5ae20f2010-09-24 04:52:1125#include "base/string_split.h"
[email protected]4efe77c2009-07-18 03:25:3226#include "base/sys_string_conversions.h"
[email protected]93270d002011-01-19 22:32:5927#include "base/threading/platform_thread.h"
[email protected]6b5f21d2009-04-13 17:01:3528#include "base/time.h"
[email protected]be1ce6a72010-08-03 14:35:2229#include "base/utf_string_conversions.h"
[email protected]1bcdb532009-01-16 17:47:5730#include "base/values.h"
[email protected]01109dcf2010-03-27 00:56:4331#include "build/build_config.h"
[email protected]4bc5050c2010-11-18 17:55:5432#include "chrome/browser/about_flags.h"
[email protected]5d364542012-04-05 07:15:3933#include "chrome/browser/browser_process.h"
[email protected]b112a4c2009-02-01 20:24:0134#include "chrome/browser/browser_process_impl.h"
[email protected]7f0ebc92009-02-05 18:34:2135#include "chrome/browser/browser_shutdown.h"
[email protected]50462bf02011-11-21 19:13:3136#include "chrome/browser/chrome_browser_main_extra_parts.h"
[email protected]10189862011-04-15 21:42:0337#include "chrome/browser/defaults.h"
[email protected]f7e615a2009-06-26 19:50:5238#include "chrome/browser/extensions/extension_protocols.h"
[email protected]eaa7dd182010-12-14 11:09:0039#include "chrome/browser/extensions/extension_service.h"
[email protected]d9ede582012-08-14 19:21:3840#include "chrome/browser/extensions/startup_helper.h"
[email protected]815856722011-04-13 17:19:1941#include "chrome/browser/first_run/upgrade_util.h"
[email protected]ed666562012-03-21 19:49:5242#include "chrome/browser/google/google_search_counter.h"
[email protected]7a336d6ec2011-10-07 14:17:5843#include "chrome/browser/google/google_util.h"
[email protected]d56ecf922012-02-15 16:03:1144#include "chrome/browser/gpu_blacklist.h"
[email protected]86230b92009-11-23 20:38:3845#include "chrome/browser/jankometer.h"
[email protected]04261032011-07-21 08:42:0846#include "chrome/browser/language_usage_metrics.h"
[email protected]a6c7a8252012-06-25 18:28:2447#include "chrome/browser/managed_mode.h"
[email protected]4cb3c1fc2012-04-05 07:23:4848#include "chrome/browser/metrics/field_trial_synchronizer.h"
[email protected]679082052010-07-21 21:30:1349#include "chrome/browser/metrics/metrics_log.h"
[email protected]dc6f4962009-02-13 01:25:5050#include "chrome/browser/metrics/metrics_service.h"
[email protected]0b565182011-03-02 18:11:1551#include "chrome/browser/metrics/thread_watcher.h"
[email protected]10fe40c2011-11-05 03:27:4652#include "chrome/browser/metrics/tracking_synchronizer.h"
[email protected]cf265dc02012-08-15 01:01:1653#include "chrome/browser/metrics/variations/variations_service.h"
[email protected]773ebb92011-11-15 19:06:5254#include "chrome/browser/nacl_host/nacl_process_host.h"
[email protected]82c71ba2011-05-27 18:51:5355#include "chrome/browser/net/chrome_net_log.h"
[email protected]59368d9d2012-01-31 01:02:5456#include "chrome/browser/notifications/desktop_notification_service.h"
[email protected]aa84a7e2012-03-15 21:29:0657#include "chrome/browser/notifications/desktop_notification_service_factory.h"
[email protected]684dace42012-07-01 14:30:4158#include "chrome/browser/page_cycler/page_cycler.h"
[email protected]d378f192012-08-14 18:42:0359#include "chrome/browser/performance_monitor/performance_monitor.h"
[email protected]0c1c4752012-08-09 20:30:2460#include "chrome/browser/performance_monitor/startup_timer.h"
[email protected]8a2713682011-08-19 10:36:5961#include "chrome/browser/plugin_prefs.h"
[email protected]37858e52010-08-26 00:22:0262#include "chrome/browser/prefs/pref_service.h"
63#include "chrome/browser/prefs/pref_value_store.h"
[email protected]1583def2012-01-25 11:26:3264#include "chrome/browser/prefs/scoped_user_pref_update.h"
[email protected]59368d9d2012-01-31 01:02:5465#include "chrome/browser/printing/cloud_print/cloud_print_proxy_service.h"
[email protected]aa84a7e2012-03-15 21:29:0666#include "chrome/browser/printing/cloud_print/cloud_print_proxy_service_factory.h"
[email protected]e3db4fab2009-04-21 21:47:4067#include "chrome/browser/process_singleton.h"
[email protected]8ecad5e2010-12-02 21:18:3368#include "chrome/browser/profiles/profile.h"
69#include "chrome/browser/profiles/profile_manager.h"
[email protected]23872902010-10-26 03:43:5970#include "chrome/browser/search_engines/search_engine_type.h"
[email protected]8b62334b2010-08-31 22:37:1171#include "chrome/browser/search_engines/template_url.h"
[email protected]b37bdfe2012-03-16 20:53:2772#include "chrome/browser/search_engines/template_url_prepopulate_data.h"
[email protected]59368d9d2012-01-31 01:02:5473#include "chrome/browser/search_engines/template_url_service.h"
[email protected]aa84a7e2012-03-15 21:29:0674#include "chrome/browser/search_engines/template_url_service_factory.h"
[email protected]e96bf0c52010-08-23 23:20:5775#include "chrome/browser/service/service_process_control.h"
[email protected]1bcdb532009-01-16 17:47:5776#include "chrome/browser/shell_integration.h"
[email protected]509acb8e2010-07-01 20:35:3177#include "chrome/browser/translate/translate_manager.h"
[email protected]f46be6e2010-11-16 03:52:3278#include "chrome/browser/ui/browser.h"
[email protected]278a34d2012-06-07 00:34:5879#include "chrome/browser/ui/browser_finder.h"
[email protected]2cd4fde2012-06-26 03:10:2680#include "chrome/browser/ui/browser_ui_prefs.h"
[email protected]eed73252012-08-03 18:47:4481#include "chrome/browser/ui/startup/default_browser_prompt.h"
[email protected]fe7c4872012-05-10 20:06:0382#include "chrome/browser/ui/startup/startup_browser_creator.h"
[email protected]3770c242012-07-12 22:58:1783#include "chrome/browser/ui/uma_browsing_activity_observer.h"
[email protected]eea4c6d2012-05-15 18:10:5684#include "chrome/browser/ui/user_data_dir_dialog.h"
[email protected]fd42ac30f2011-02-27 19:33:3685#include "chrome/browser/ui/webui/chrome_url_data_manager_backend.h"
[email protected]783f06f2011-09-14 01:36:1186#include "chrome/common/child_process_logging.h"
[email protected]1bcdb532009-01-16 17:47:5787#include "chrome/common/chrome_constants.h"
88#include "chrome/common/chrome_paths.h"
[email protected]1fcfb202011-07-19 19:53:1489#include "chrome/common/chrome_result_codes.h"
[email protected]1bcdb532009-01-16 17:47:5790#include "chrome/common/chrome_switches.h"
[email protected]1b084b5e2010-08-03 07:58:1991#include "chrome/common/env_vars.h"
[email protected]ea587b02010-05-21 15:01:3592#include "chrome/common/json_pref_store.h"
[email protected]1bcdb532009-01-16 17:47:5793#include "chrome/common/jstemplate_builder.h"
[email protected]30f547c2010-08-04 01:00:0194#include "chrome/common/logging_chrome.h"
[email protected]cf265dc02012-08-15 01:01:1695#include "chrome/common/metrics/variations/variations_util.h"
[email protected]64d50ed2009-09-22 21:05:4796#include "chrome/common/net/net_resource_provider.h"
[email protected]1bcdb532009-01-16 17:47:5797#include "chrome/common/pref_names.h"
[email protected]7664ab32011-02-01 23:35:2598#include "chrome/common/profiling.h"
[email protected]1e7377d2009-05-28 20:23:0699#include "chrome/installer/util/google_update_settings.h"
[email protected]c38831a12011-10-28 12:44:49100#include "content/public/browser/browser_thread.h"
[email protected]79078df2012-02-16 01:22:32101#include "content/public/browser/gpu_data_manager.h"
[email protected]687ae4f2012-04-19 00:55:01102#include "content/public/browser/render_process_host.h"
[email protected]5d1fa242011-10-18 23:35:38103#include "content/public/common/content_client.h"
[email protected]4573fbd2011-10-31 20:25:18104#include "content/public/common/main_function_params.h"
[email protected]ea500fc62010-03-22 21:34:29105#include "grit/app_locale_settings.h"
[email protected]4e039f242011-12-03 07:29:38106#include "grit/browser_resources.h"
[email protected]34ac8f32009-02-22 23:03:27107#include "grit/chromium_strings.h"
108#include "grit/generated_resources.h"
[email protected]61c6f412011-03-23 20:47:54109#include "grit/platform_locale_settings.h"
[email protected]698cccb12009-03-11 18:47:24110#include "net/base/net_module.h"
[email protected]3b543ab2011-09-17 21:47:00111#include "net/base/sdch_manager.h"
[email protected]aa84a7e2012-03-15 21:29:06112#include "net/cookies/cookie_monster.h"
[email protected]8e6441ca2010-08-19 05:56:38113#include "net/http/http_stream_factory.h"
[email protected]f90a3a5e2010-10-25 05:17:24114#include "net/spdy/spdy_session.h"
[email protected]4a5e1782010-03-05 20:49:02115#include "net/spdy/spdy_session_pool.h"
[email protected]abb26092010-11-11 22:19:00116#include "net/url_request/url_request.h"
[email protected]c9a5a9422011-05-26 13:17:59117#include "net/websockets/websocket_job.h"
[email protected]e39027a2011-01-24 21:41:54118#include "ui/base/l10n/l10n_util.h"
[email protected]6bae7352012-06-15 15:46:46119#include "ui/base/layout.h"
[email protected]42ce29d2011-01-20 23:19:46120#include "ui/base/resource/resource_bundle.h"
[email protected]63942f22012-05-01 06:11:52121#include "ui/base/resource/resource_handle.h"
[email protected]1bcdb532009-01-16 17:47:57122
[email protected]3e087992011-04-14 22:28:12123#if defined(OS_LINUX) && !defined(OS_CHROMEOS)
124#include "chrome/browser/first_run/upgrade_util_linux.h"
125#endif
126
[email protected]d4fe1fbc2010-05-19 17:36:06127#if defined(OS_CHROMEOS)
[email protected]3e087992011-04-14 22:28:12128#include "chrome/browser/chromeos/cros/cros_library.h"
[email protected]edaf5962012-08-01 11:31:31129#include "chrome/browser/chromeos/settings/cros_settings.h"
130#include "chrome/browser/chromeos/settings/cros_settings_names.h"
[email protected]d4fe1fbc2010-05-19 17:36:06131#endif
132
[email protected]022614ef92008-12-30 20:50:01133// TODO(port): several win-only methods have been pulled out of this, but
[email protected]5c9587c2008-12-09 21:20:16134// BrowserMain() as a whole needs to be broken apart so that it's usable by
[email protected]022614ef92008-12-30 20:50:01135// other platforms. For now, it's just a stub. This is a serious work in
[email protected]5c9587c2008-12-09 21:20:16136// progress and should not be taken as an indication of a real refactoring.
137
138#if defined(OS_WIN)
[email protected]28f576f2011-08-26 20:46:55139#include "base/environment.h" // For PreRead experiment.
[email protected]0fd23af2011-02-20 06:33:04140#include "base/win/windows_version.h"
[email protected]a3abd5572011-04-15 02:09:33141#include "chrome/browser/browser_util_win.h"
[email protected]b48c9182011-10-26 18:03:30142#include "chrome/browser/chrome_browser_main_win.h"
[email protected]118b4a92011-04-14 01:15:13143#include "chrome/browser/first_run/try_chrome_dialog_view.h"
[email protected]3e087992011-04-14 22:28:12144#include "chrome/browser/first_run/upgrade_util_win.h"
[email protected]f870a322009-01-16 21:47:27145#include "chrome/browser/net/url_fixer_upper.h"
[email protected]297fb7c22012-06-26 12:37:49146#include "chrome/browser/ui/network_profile_bubble.h"
[email protected]68c921f2008-10-15 21:37:46147#include "chrome/installer/util/helper.h"
148#include "chrome/installer/util/install_util.h"
[email protected]67da235a2008-10-24 16:18:27149#include "chrome/installer/util/shell_util.h"
initial.commit09911bf2008-07-26 23:55:29150#include "net/base/net_util.h"
[email protected]8ff1d422009-07-07 21:31:39151#include "printing/printed_document.h"
[email protected]c051a1b2011-01-21 23:30:17152#include "ui/base/l10n/l10n_util_win.h"
[email protected]6bae7352012-06-15 15:46:46153#include "ui/base/win/dpi.h"
[email protected]7f0ebc92009-02-05 18:34:21154#endif // defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:29155
[email protected]0a357e632010-04-02 21:26:58156#if defined(OS_MACOSX)
[email protected]a1e1d4902010-04-02 22:02:30157#include <Security/Security.h>
[email protected]10189862011-04-15 21:42:03158
[email protected]badf5cf2011-10-29 03:44:44159#include "base/mac/scoped_nsautorelease_pool.h"
[email protected]51a7a9d2011-09-27 17:21:41160#include "chrome/browser/mac/keystone_glue.h"
[email protected]3dd6f5e2010-06-01 20:28:03161#endif
162
[email protected]81d9b72d2012-03-26 22:29:17163#if defined(ENABLE_RLZ)
164#include "chrome/browser/rlz/rlz.h"
165#endif
166
[email protected]fc9c69bf2009-08-27 01:10:44167#if defined(TOOLKIT_VIEWS)
[email protected]477ae052011-11-18 23:53:57168#include "ui/views/focus/accelerator_handler.h"
[email protected]dd092152009-05-18 21:20:33169#endif
170
[email protected]b48c9182011-10-26 18:03:30171#if defined(USE_X11)
172#include "chrome/browser/chrome_browser_main_x11.h"
173#endif
174
[email protected]631bb742011-11-02 11:29:39175using content::BrowserThread;
176
[email protected]3aada4602011-08-11 22:25:55177namespace {
initial.commit09911bf2008-07-26 23:55:29178
179// This function provides some ways to test crash and assertion handling
180// behavior of the program.
[email protected]9bade092009-11-13 17:04:42181void HandleTestParameters(const CommandLine& command_line) {
initial.commit09911bf2008-07-26 23:55:29182 // This parameter causes an assertion.
183 if (command_line.HasSwitch(switches::kBrowserAssertTest)) {
184 DCHECK(false);
185 }
186
187 // This parameter causes a null pointer crash (crash reporter trigger).
188 if (command_line.HasSwitch(switches::kBrowserCrashTest)) {
189 int* bad_pointer = NULL;
190 *bad_pointer = 0;
191 }
192}
193
[email protected]fe7c4872012-05-10 20:06:03194void AddFirstRunNewTabs(StartupBrowserCreator* browser_creator,
[email protected]f9886382010-03-10 04:45:54195 const std::vector<GURL>& new_tabs) {
196 for (std::vector<GURL>::const_iterator it = new_tabs.begin();
[email protected]201028d2010-02-12 21:28:01197 it != new_tabs.end(); ++it) {
[email protected]f9886382010-03-10 04:45:54198 if (it->is_valid())
[email protected]fe7c4872012-05-10 20:06:03199 browser_creator->AddFirstRunTab(*it);
[email protected]1188a6c2009-05-09 00:27:42200 }
201}
[email protected]1188a6c2009-05-09 00:27:42202
[email protected]7e59cff2010-03-23 16:12:40203void InitializeNetworkOptions(const CommandLine& parsed_command_line) {
204 if (parsed_command_line.HasSwitch(switches::kEnableFileCookies)) {
205 // Enable cookie storage for file:// URLs. Must do this before the first
206 // Profile (and therefore the first CookieMonster) is created.
207 net::CookieMonster::EnableFileScheme();
208 }
209
[email protected]7e59cff2010-03-23 16:12:40210 if (parsed_command_line.HasSwitch(switches::kIgnoreCertificateErrors))
[email protected]8e6441ca2010-08-19 05:56:38211 net::HttpStreamFactory::set_ignore_certificate_errors(true);
[email protected]7e59cff2010-03-23 16:12:40212
[email protected]b6a50182010-05-12 22:47:14213 if (parsed_command_line.HasSwitch(switches::kHostRules))
[email protected]8e6441ca2010-08-19 05:56:38214 net::HttpStreamFactory::SetHostMappingRules(
[email protected]b6a50182010-05-12 22:47:14215 parsed_command_line.GetSwitchValueASCII(switches::kHostRules));
216
[email protected]8b114dd72011-03-25 05:33:02217 if (parsed_command_line.HasSwitch(switches::kEnableIPPooling))
218 net::SpdySessionPool::enable_ip_pooling(true);
219
220 if (parsed_command_line.HasSwitch(switches::kDisableIPPooling))
221 net::SpdySessionPool::enable_ip_pooling(false);
222
[email protected]7e59cff2010-03-23 16:12:40223 if (parsed_command_line.HasSwitch(switches::kMaxSpdySessionsPerDomain)) {
[email protected]e83326f2010-07-31 17:29:25224 int value;
[email protected]f7ede5d2011-12-14 05:49:22225 base::StringToInt(
226 parsed_command_line.GetSwitchValueASCII(
[email protected]e83326f2010-07-31 17:29:25227 switches::kMaxSpdySessionsPerDomain),
228 &value);
[email protected]7e59cff2010-03-23 16:12:40229 net::SpdySessionPool::set_max_sessions_per_domain(value);
230 }
[email protected]a973a712010-11-24 15:00:18231
[email protected]c9a5a9422011-05-26 13:17:59232 if (parsed_command_line.HasSwitch(switches::kEnableWebSocketOverSpdy)) {
233 // Enable WebSocket over SPDY.
234 net::WebSocketJob::set_websocket_over_spdy_enabled(true);
235 }
[email protected]bbadb112011-12-12 16:55:28236
237 if (parsed_command_line.HasSwitch(switches::kEnableHttpPipelining))
238 net::HttpStreamFactory::set_http_pipelining_enabled(true);
[email protected]f7ede5d2011-12-14 05:49:22239
240 if (parsed_command_line.HasSwitch(switches::kTestingFixedHttpPort)) {
241 int value;
242 base::StringToInt(
243 parsed_command_line.GetSwitchValueASCII(
244 switches::kTestingFixedHttpPort),
245 &value);
246 net::HttpStreamFactory::set_testing_fixed_http_port(value);
247 }
248
249 if (parsed_command_line.HasSwitch(switches::kTestingFixedHttpsPort)) {
250 int value;
251 base::StringToInt(
252 parsed_command_line.GetSwitchValueASCII(
253 switches::kTestingFixedHttpsPort),
254 &value);
255 net::HttpStreamFactory::set_testing_fixed_https_port(value);
256 }
[email protected]7e59cff2010-03-23 16:12:40257}
258
259// Returns the new local state object, guaranteed non-NULL.
260PrefService* InitializeLocalState(const CommandLine& parsed_command_line,
261 bool is_first_run) {
262 FilePath local_state_path;
263 PathService::Get(chrome::FILE_LOCAL_STATE, &local_state_path);
264 bool local_state_file_exists = file_util::PathExists(local_state_path);
265
266 // Load local state. This includes the application locale so we know which
267 // locale dll to load.
268 PrefService* local_state = g_browser_process->local_state();
269 DCHECK(local_state);
270
[email protected]cd87be132011-02-04 13:07:38271 // TODO(brettw,*): this comment about ResourceBundle was here since
272 // initial commit. This comment seems unrelated, bit-rotten and
273 // a candidate for removal.
[email protected]7e59cff2010-03-23 16:12:40274 // Initialize ResourceBundle which handles files loaded from external
275 // sources. This has to be done before uninstall code path and before prefs
276 // are registered.
[email protected]cd87be132011-02-04 13:07:38277 local_state->RegisterStringPref(prefs::kApplicationLocale, std::string());
278#if defined(OS_CHROMEOS)
279 local_state->RegisterStringPref(prefs::kOwnerLocale, std::string());
[email protected]d51665cf2011-02-10 09:21:52280 local_state->RegisterStringPref(prefs::kHardwareKeyboardLayout,
281 std::string());
[email protected]cd87be132011-02-04 13:07:38282#endif // defined(OS_CHROMEOS)
[email protected]c7db33a2010-10-06 18:57:25283#if !defined(OS_CHROMEOS)
[email protected]7e59cff2010-03-23 16:12:40284 local_state->RegisterBooleanPref(prefs::kMetricsReportingEnabled,
285 GoogleUpdateSettings::GetCollectStatsConsent());
[email protected]c7db33a2010-10-06 18:57:25286#endif // !defined(OS_CHROMEOS)
[email protected]7e59cff2010-03-23 16:12:40287
288 if (is_first_run) {
289#if defined(OS_WIN)
290 // During first run we read the google_update registry key to find what
291 // language the user selected when downloading the installer. This
292 // becomes our default language in the prefs.
293 // Other platforms obey the system locale.
294 std::wstring install_lang;
[email protected]ddd231e2010-06-29 20:35:19295 if (GoogleUpdateSettings::GetLanguage(&install_lang)) {
296 local_state->SetString(prefs::kApplicationLocale,
297 WideToASCII(install_lang));
298 }
[email protected]7e59cff2010-03-23 16:12:40299#endif // defined(OS_WIN)
300 }
301
302 // If the local state file for the current profile doesn't exist and the
303 // parent profile command line flag is present, then we should inherit some
304 // local state from the parent profile.
305 // Checking that the local state file for the current profile doesn't exist
306 // is the most robust way to determine whether we need to inherit or not
307 // since the parent profile command line flag can be present even when the
308 // current profile is not a new one, and in that case we do not want to
309 // inherit and reset the user's setting.
310 if (!local_state_file_exists &&
311 parsed_command_line.HasSwitch(switches::kParentProfile)) {
312 FilePath parent_profile =
313 parsed_command_line.GetSwitchValuePath(switches::kParentProfile);
[email protected]d8b08c92010-06-07 13:13:28314 scoped_ptr<PrefService> parent_local_state(
[email protected]21d3a882012-05-31 14:41:55315 PrefService::CreatePrefService(parent_profile,
316 g_browser_process->policy_service(),
317 NULL, false));
[email protected]d8b08c92010-06-07 13:13:28318 parent_local_state->RegisterStringPref(prefs::kApplicationLocale,
[email protected]20ce516d2010-06-18 02:20:04319 std::string());
[email protected]7e59cff2010-03-23 16:12:40320 // Right now, we only inherit the locale setting from the parent profile.
321 local_state->SetString(
322 prefs::kApplicationLocale,
[email protected]d8b08c92010-06-07 13:13:28323 parent_local_state->GetString(prefs::kApplicationLocale));
[email protected]7e59cff2010-03-23 16:12:40324 }
325
[email protected]fab807e2011-04-14 12:24:19326#if defined(OS_CHROMEOS)
327 if (parsed_command_line.HasSwitch(switches::kLoginManager)) {
328 std::string owner_locale = local_state->GetString(prefs::kOwnerLocale);
329 // Ensure that we start with owner's locale.
330 if (!owner_locale.empty() &&
331 local_state->GetString(prefs::kApplicationLocale) != owner_locale &&
332 !local_state->IsManagedPreference(prefs::kApplicationLocale)) {
333 local_state->SetString(prefs::kApplicationLocale, owner_locale);
[email protected]fab807e2011-04-14 12:24:19334 }
335 }
336#endif
337
[email protected]7e59cff2010-03-23 16:12:40338 return local_state;
339}
340
[email protected]20271f32010-03-26 03:04:51341// Initializes the profile, possibly doing some user prompting to pick a
342// fallback profile. Returns the newly created profile, or NULL if startup
343// should not continue.
[email protected]4573fbd2011-10-31 20:25:18344Profile* CreateProfile(const content::MainFunctionParams& parameters,
[email protected]506a87c2011-07-21 16:01:54345 const FilePath& user_data_dir,
346 const CommandLine& parsed_command_line) {
347 Profile* profile;
[email protected]7802309d2011-10-20 15:43:09348 if (ProfileManager::IsMultipleProfilesEnabled() &&
349 parsed_command_line.HasSwitch(switches::kProfileDirectory)) {
350 g_browser_process->local_state()->SetString(prefs::kProfileLastUsed,
[email protected]a5e8c6d42012-04-09 20:22:01351 parsed_command_line.GetSwitchValueASCII(switches::kProfileDirectory));
[email protected]1583def2012-01-25 11:26:32352 // Clear kProfilesLastActive since the user only wants to launch a specific
353 // profile.
354 ListPrefUpdate update(g_browser_process->local_state(),
355 prefs::kProfilesLastActive);
356 ListValue* profile_list = update.Get();
357 profile_list->Clear();
[email protected]506a87c2011-07-21 16:01:54358 }
[email protected]484262f2011-10-31 19:42:00359#if defined(OS_CHROMEOS)
360 // TODO(ivankr): http://crbug.com/83792
361 profile = g_browser_process->profile_manager()->GetDefaultProfile(
362 user_data_dir);
363#else
[email protected]7802309d2011-10-20 15:43:09364 profile = g_browser_process->profile_manager()->GetLastUsedProfile(
365 user_data_dir);
[email protected]484262f2011-10-31 19:42:00366#endif
[email protected]20271f32010-03-26 03:04:51367 if (profile)
368 return profile;
369
370#if defined(OS_WIN)
[email protected]94fbaa42011-09-07 20:09:54371#if defined(USE_AURA)
372 // TODO(beng):
373 NOTIMPLEMENTED();
374#else
[email protected]20271f32010-03-26 03:04:51375 // Ideally, we should be able to run w/o access to disk. For now, we
376 // prompt the user to pick a different user-data-dir and restart chrome
377 // with the new dir.
378 // http://code.google.com/p/chromium/issues/detail?id=11510
[email protected]684dace42012-07-01 14:30:41379 FilePath new_user_data_dir = chrome::ShowUserDataDirDialog(user_data_dir);
[email protected]20271f32010-03-26 03:04:51380
381 if (!new_user_data_dir.empty()) {
382 // Because of the way CommandLine parses, it's sufficient to append a new
383 // --user-data-dir switch. The last flag of the same name wins.
384 // TODO(tc): It would be nice to remove the flag we don't want, but that
385 // sounds risky if we parse differently than CommandLineToArgvW.
[email protected]badf5cf2011-10-29 03:44:44386 CommandLine new_command_line = parameters.command_line;
[email protected]4f08c83f2010-07-29 23:02:34387 new_command_line.AppendSwitchPath(switches::kUserDataDir,
388 new_user_data_dir);
[email protected]e5992182011-07-15 16:47:02389 base::LaunchProcess(new_command_line, base::LaunchOptions(), NULL);
[email protected]20271f32010-03-26 03:04:51390 }
[email protected]94fbaa42011-09-07 20:09:54391#endif
[email protected]20271f32010-03-26 03:04:51392#else
393 // TODO(port): fix this. See comments near the definition of
394 // user_data_dir. It is better to CHECK-fail here than it is to
395 // silently exit because of missing code in the above test.
396 CHECK(profile) << "Cannot get default profile.";
397#endif
398
399 return NULL;
400}
401
[email protected]4e039f242011-12-03 07:29:38402// Load GPU Blacklist, collect preliminary gpu info, and compute preliminary
403// gpu feature flags.
404void InitializeGpuDataManager(const CommandLine& parsed_command_line) {
[email protected]79078df2012-02-16 01:22:32405 content::GpuDataManager::GetInstance();
[email protected]d56ecf922012-02-15 16:03:11406 if (parsed_command_line.HasSwitch(switches::kSkipGpuDataLoading) ||
[email protected]a5e8c6d42012-04-09 20:22:01407 parsed_command_line.HasSwitch(switches::kIgnoreGpuBlacklist)) {
[email protected]4e039f242011-12-03 07:29:38408 return;
[email protected]a5e8c6d42012-04-09 20:22:01409 }
[email protected]4e039f242011-12-03 07:29:38410
411 const base::StringPiece gpu_blacklist_json(
412 ResourceBundle::GetSharedInstance().GetRawDataResource(
[email protected]c49201a2012-05-24 11:04:57413 IDR_GPU_BLACKLIST, ui::SCALE_FACTOR_NONE));
[email protected]d56ecf922012-02-15 16:03:11414 GpuBlacklist* gpu_blacklist = GpuBlacklist::GetInstance();
[email protected]4e039f242011-12-03 07:29:38415 bool succeed = gpu_blacklist->LoadGpuBlacklist(
[email protected]f11fbb712012-03-09 19:05:34416 gpu_blacklist_json.as_string(), GpuBlacklist::kCurrentOsOnly);
[email protected]4e039f242011-12-03 07:29:38417 DCHECK(succeed);
[email protected]d56ecf922012-02-15 16:03:11418 gpu_blacklist->UpdateGpuDataManager();
[email protected]4e039f242011-12-03 07:29:38419}
420
[email protected]e457f742010-04-02 17:25:43421#if defined(OS_MACOSX)
422OSStatus KeychainCallback(SecKeychainEvent keychain_event,
[email protected]d3b05ea2012-01-24 22:57:05423 SecKeychainCallbackInfo* info, void* context) {
[email protected]e457f742010-04-02 17:25:43424 return noErr;
425}
426#endif
427
[email protected]28f576f2011-08-26 20:46:55428// This code is specific to the Windows-only PreReadExperiment field-trial.
429void AddPreReadHistogramTime(const char* name, base::TimeDelta time) {
430 const base::TimeDelta kMin(base::TimeDelta::FromMilliseconds(1));
431 const base::TimeDelta kMax(base::TimeDelta::FromHours(1));
432 static const size_t kBuckets(100);
433
434 // FactoryTimeGet will always return a pointer to the same histogram object,
435 // keyed on its name. There's no need for us to store it explicitly anywhere.
436 base::Histogram* counter = base::Histogram::FactoryTimeGet(
437 name, kMin, kMax, kBuckets, base::Histogram::kUmaTargetedHistogramFlag);
438
439 counter->AddTime(time);
440}
441
[email protected]5d364542012-04-05 07:15:39442bool ProcessSingletonNotificationCallback(const CommandLine& command_line,
443 const FilePath& current_directory) {
444 // Drop the request if the browser process is already in shutdown path.
445 if (!g_browser_process || g_browser_process->IsShuttingDown())
446 return false;
447
448 // TODO(erikwright): Consider removing this - AFAIK it is no longer used.
449 // Handle the --uninstall-extension startup action. This needs to done here
450 // in the process that is running with the target profile, otherwise the
451 // uninstall will fail to unload and remove all components.
452 if (command_line.HasSwitch(switches::kUninstallExtension)) {
453 // The uninstall extension switch can't be combined with the profile
454 // directory switch.
455 DCHECK(!command_line.HasSwitch(switches::kProfileDirectory));
456
457 Profile* profile = ProfileManager::GetLastUsedProfile();
458 if (!profile) {
459 // We should never be called before the profile has been created.
460 NOTREACHED();
461 return true;
462 }
463
[email protected]d9ede582012-08-14 19:21:38464 extensions::StartupHelper extension_startup_helper;
465 extension_startup_helper.UninstallExtension(command_line, profile);
[email protected]5d364542012-04-05 07:15:39466 return true;
467 }
468
[email protected]fe7c4872012-05-10 20:06:03469 StartupBrowserCreator::ProcessCommandLineAlreadyRunning(
[email protected]5d364542012-04-05 07:15:39470 command_line, current_directory);
471 return true;
472}
473
[email protected]a5e8c6d42012-04-09 20:22:01474bool HasImportSwitch(const CommandLine& command_line) {
475 return (command_line.HasSwitch(switches::kImport) ||
476 command_line.HasSwitch(switches::kImportFromFile));
477}
478
[email protected]3aada4602011-08-11 22:25:55479} // namespace
480
[email protected]cbce47242011-08-12 21:40:59481namespace chrome_browser {
482// This error message is not localized because we failed to load the
483// localization data files.
484const char kMissingLocaleDataTitle[] = "Missing File Error";
485const char kMissingLocaleDataMessage[] =
486 "Unable to find locale data files. Please reinstall.";
487} // namespace chrome_browser
488
[email protected]4bb76022011-08-12 03:05:53489// BrowserMainParts ------------------------------------------------------------
490
[email protected]1948a4782012-04-30 13:45:44491// static
492bool ChromeBrowserMainParts::disable_enforcing_cookie_policies_for_tests_ =
493 false;
494
[email protected]f967b722011-09-07 00:58:04495ChromeBrowserMainParts::ChromeBrowserMainParts(
[email protected]4573fbd2011-10-31 20:25:18496 const content::MainFunctionParams& parameters)
[email protected]b48c9182011-10-26 18:03:30497 : parameters_(parameters),
[email protected]badf5cf2011-10-29 03:44:44498 parsed_command_line_(parameters.command_line),
[email protected]b48c9182011-10-26 18:03:30499 result_code_(content::RESULT_CODE_NORMAL_EXIT),
[email protected]08f1c5e2011-12-01 01:54:34500 startup_watcher_(new StartupTimeBomb()),
[email protected]c1702d712011-09-08 19:41:43501 shutdown_watcher_(new ShutdownWatcherHelper()),
[email protected]0c1c4752012-08-09 20:30:24502 startup_timer_(new performance_monitor::StartupTimer()),
[email protected]ae9e12a2012-07-31 17:48:12503 browser_field_trials_(parameters.command_line),
[email protected]c1702d712011-09-08 19:41:43504 record_search_engine_(false),
505 translate_manager_(NULL),
506 profile_(NULL),
[email protected]66013922011-09-13 18:53:42507 run_message_loop_(true),
[email protected]2e5b60a22011-11-28 15:56:41508 notify_result_(ProcessSingleton::PROCESS_NONE),
509 is_first_run_(false),
510 first_run_ui_bypass_(false),
[email protected]2e5b60a22011-11-28 15:56:41511 local_state_(NULL),
512 restart_last_session_(false) {
[email protected]f967b722011-09-07 00:58:04513 // If we're running tests (ui_task is non-null).
[email protected]716476c2011-12-29 00:07:03514 if (parameters.ui_task)
[email protected]b228ecf12011-09-08 17:06:59515 browser_defaults::enable_help_app = false;
[email protected]1948a4782012-04-30 13:45:44516
517 // Chrome disallows cookies by default. All code paths that want to use
518 // cookies need to go through one of Chrome's URLRequestContexts which have
519 // a ChromeNetworkDelegate attached that selectively allows cookies again.
520 if (!disable_enforcing_cookie_policies_for_tests_)
521 net::URLRequest::SetDefaultCookiePolicyToBlock();
[email protected]4bb76022011-08-12 03:05:53522}
523
[email protected]f967b722011-09-07 00:58:04524ChromeBrowserMainParts::~ChromeBrowserMainParts() {
[email protected]50462bf02011-11-21 19:13:31525 for (int i = static_cast<int>(chrome_extra_parts_.size())-1; i >= 0; --i)
526 delete chrome_extra_parts_[i];
527 chrome_extra_parts_.clear();
[email protected]4bb76022011-08-12 03:05:53528}
529
[email protected]069cd9952011-10-01 16:34:45530// This will be called after the command-line has been mutated by about:flags
[email protected]5281db1d2012-01-11 22:51:54531void ChromeBrowserMainParts::SetupMetricsAndFieldTrials() {
[email protected]069cd9952011-10-01 16:34:45532 // Must initialize metrics after labs have been converted into switches,
533 // but before field trials are set up (so that client ID is available for
534 // one-time randomized field trials).
[email protected]5281db1d2012-01-11 22:51:54535#if defined(OS_WIN)
536 if (parsed_command_line_.HasSwitch(switches::kChromeFrame))
537 MetricsLog::set_version_extension("-F");
538#elif defined(ARCH_CPU_64_BITS)
539 MetricsLog::set_version_extension("-64");
540#endif // defined(OS_WIN)
[email protected]069cd9952011-10-01 16:34:45541
542 // Initialize FieldTrialList to support FieldTrials that use one-time
[email protected]e61003a2012-05-24 17:03:19543 // randomization.
[email protected]5281db1d2012-01-11 22:51:54544 MetricsService* metrics = browser_process_->metrics_service();
[email protected]e5354322012-08-09 23:07:37545 bool metrics_reporting_enabled = IsMetricsReportingEnabled();
546 if (metrics_reporting_enabled)
[email protected]5cbeeef72012-02-08 02:05:18547 metrics->ForceClientIdCreation(); // Needed below.
[email protected]e61003a2012-05-24 17:03:19548 field_trial_list_.reset(
[email protected]e5354322012-08-09 23:07:37549 new base::FieldTrialList(
[email protected]20f999b52012-08-24 22:32:59550 metrics->CreateEntropyProvider(metrics_reporting_enabled).release()));
[email protected]4bb76022011-08-12 03:05:53551
[email protected]2d4729582012-04-12 07:08:07552 // Ensure any field trials specified on the command line are initialized.
553 // Also stop the metrics service so that we don't pollute UMA.
554#ifndef NDEBUG
555 const CommandLine* command_line = CommandLine::ForCurrentProcess();
556 if (command_line->HasSwitch(switches::kForceFieldTrials)) {
557 std::string persistent = command_line->GetSwitchValueASCII(
558 switches::kForceFieldTrials);
559 bool ret = base::FieldTrialList::CreateTrialsFromString(persistent);
560 CHECK(ret) << "Invalid --" << switches::kForceFieldTrials <<
561 " list specified.";
562 }
563#endif // NDEBUG
564
[email protected]04811242012-07-06 18:04:29565 chrome_variations::VariationsService* variations_service =
[email protected]54e26c122012-05-17 15:36:21566 browser_process_->variations_service();
567 variations_service->CreateTrialsFromSeed(browser_process_->local_state());
568
[email protected]ae9e12a2012-07-31 17:48:12569 browser_field_trials_.SetupFieldTrials(
570 local_state_->IsManagedPreference(prefs::kMaxConnectionsPerProxy));
[email protected]4bb76022011-08-12 03:05:53571
[email protected]b98def82012-06-28 23:42:45572 SetupPlatformFieldTrials();
573
[email protected]4bb76022011-08-12 03:05:53574 // Initialize FieldTrialSynchronizer system. This is a singleton and is used
[email protected]716476c2011-12-29 00:07:03575 // for posting tasks via base::Bind. Its deleted when it goes out of scope.
576 // Even though base::Bind does AddRef and Release, the object will not be
577 // deleted after the Task is executed.
[email protected]4bb76022011-08-12 03:05:53578 field_trial_synchronizer_ = new FieldTrialSynchronizer();
[email protected]4bb76022011-08-12 03:05:53579}
580
[email protected]069cd9952011-10-01 16:34:45581// ChromeBrowserMainParts: |SetupMetricsAndFieldTrials()| related --------------
582
[email protected]5281db1d2012-01-11 22:51:54583void ChromeBrowserMainParts::StartMetricsRecording() {
584 MetricsService* metrics = g_browser_process->metrics_service();
585 if (parsed_command_line_.HasSwitch(switches::kMetricsRecordingOnly) ||
586 parsed_command_line_.HasSwitch(switches::kEnableBenchmarking)) {
587 // If we're testing then we don't care what the user preference is, we turn
588 // on recording, but not reporting, otherwise tests fail.
589 metrics->StartRecordingOnly();
590 return;
591 }
592
[email protected]5cbeeef72012-02-08 02:05:18593 if (IsMetricsReportingEnabled())
594 metrics->Start();
595}
596
597bool ChromeBrowserMainParts::IsMetricsReportingEnabled() {
[email protected]5281db1d2012-01-11 22:51:54598 // If the user permits metrics reporting with the checkbox in the
599 // prefs, we turn on recording. We disable metrics completely for
[email protected]e5354322012-08-09 23:07:37600 // non-official builds. This can be forced with a flag.
601 const CommandLine* command_line = CommandLine::ForCurrentProcess();
602 if (command_line->HasSwitch(switches::kEnableMetricsReportingForTesting))
603 return true;
604
[email protected]5cbeeef72012-02-08 02:05:18605 bool enabled = false;
[email protected]2d4729582012-04-12 07:08:07606#ifndef NDEBUG
[email protected]9f4dd172012-05-11 02:29:56607 // The debug build doesn't send UMA logs when FieldTrials are forced.
[email protected]2d4729582012-04-12 07:08:07608 if (command_line->HasSwitch(switches::kForceFieldTrials))
609 return false;
610#endif // #ifndef NDEBUG
611
[email protected]5281db1d2012-01-11 22:51:54612#if defined(GOOGLE_CHROME_BUILD)
613#if defined(OS_CHROMEOS)
[email protected]5281db1d2012-01-11 22:51:54614 chromeos::CrosSettings::Get()->GetBoolean(chromeos::kStatsReportingPref,
615 &enabled);
616#else
[email protected]5cbeeef72012-02-08 02:05:18617 enabled = local_state_->GetBoolean(prefs::kMetricsReportingEnabled);
[email protected]5281db1d2012-01-11 22:51:54618#endif // #if defined(OS_CHROMEOS)
[email protected]5281db1d2012-01-11 22:51:54619#endif // defined(GOOGLE_CHROME_BUILD)
[email protected]5cbeeef72012-02-08 02:05:18620 return enabled;
[email protected]5281db1d2012-01-11 22:51:54621}
622
[email protected]4bb76022011-08-12 03:05:53623// -----------------------------------------------------------------------------
624// TODO(viettrungluu): move more/rest of BrowserMain() into BrowserMainParts.
625
[email protected]3aada4602011-08-11 22:25:55626#if defined(OS_WIN)
627#define DLLEXPORT __declspec(dllexport)
628
629// We use extern C for the prototype DLLEXPORT to avoid C++ name mangling.
630extern "C" {
631DLLEXPORT void __cdecl RelaunchChromeBrowserWithNewCommandLineIfNeeded();
632}
633
634DLLEXPORT void __cdecl RelaunchChromeBrowserWithNewCommandLineIfNeeded() {
635 // Need an instance of AtExitManager to handle singleton creations and
636 // deletions. We need this new instance because, the old instance created
637 // in ChromeMain() got destructed when the function returned.
638 base::AtExitManager exit_manager;
639 upgrade_util::RelaunchChromeBrowserWithNewCommandLineIfNeeded();
640}
641#endif
642
[email protected]50462bf02011-11-21 19:13:31643// content::BrowserMainParts implementation ------------------------------------
644
[email protected]c7480942011-11-08 19:18:27645void ChromeBrowserMainParts::PreEarlyInitialization() {
[email protected]687ae4f2012-04-19 00:55:01646 // Single-process is an unsupported and not fully tested mode, so
647 // don't enable it for official Chrome builds (by not setting the client, the
648#if defined(GOOGLE_CHROME_BUILD)
649 if (content::RenderProcessHost::run_renderer_in_process())
650 content::RenderProcessHost::set_run_renderer_in_process(false);
651#endif // GOOGLE_CHROME_BUILD
652
[email protected]50462bf02011-11-21 19:13:31653 for (size_t i = 0; i < chrome_extra_parts_.size(); ++i)
654 chrome_extra_parts_[i]->PreEarlyInitialization();
[email protected]c7480942011-11-08 19:18:27655}
656
657void ChromeBrowserMainParts::PostEarlyInitialization() {
[email protected]50462bf02011-11-21 19:13:31658 for (size_t i = 0; i < chrome_extra_parts_.size(); ++i)
659 chrome_extra_parts_[i]->PostEarlyInitialization();
[email protected]c7480942011-11-08 19:18:27660}
661
662void ChromeBrowserMainParts::ToolkitInitialized() {
[email protected]50462bf02011-11-21 19:13:31663 for (size_t i = 0; i < chrome_extra_parts_.size(); ++i)
664 chrome_extra_parts_[i]->ToolkitInitialized();
[email protected]c7480942011-11-08 19:18:27665}
666
667void ChromeBrowserMainParts::PreMainMessageLoopStart() {
[email protected]50462bf02011-11-21 19:13:31668 for (size_t i = 0; i < chrome_extra_parts_.size(); ++i)
669 chrome_extra_parts_[i]->PreMainMessageLoopStart();
[email protected]c7480942011-11-08 19:18:27670}
671
672void ChromeBrowserMainParts::PostMainMessageLoopStart() {
[email protected]50462bf02011-11-21 19:13:31673 for (size_t i = 0; i < chrome_extra_parts_.size(); ++i)
674 chrome_extra_parts_[i]->PostMainMessageLoopStart();
[email protected]c7480942011-11-08 19:18:27675}
676
[email protected]69479b922012-02-02 09:56:20677int ChromeBrowserMainParts::PreCreateThreads() {
[email protected]2e5b60a22011-11-28 15:56:41678 result_code_ = PreCreateThreadsImpl();
[email protected]7defb4f02012-02-07 18:25:46679 // These members must be initialized before returning from this function.
680 DCHECK(master_prefs_.get());
[email protected]81054f812012-08-30 00:47:09681#if !defined(OS_ANDROID)
[email protected]fe7c4872012-05-10 20:06:03682 DCHECK(browser_creator_.get());
[email protected]81054f812012-08-30 00:47:09683#endif
[email protected]69479b922012-02-02 09:56:20684 return result_code_;
[email protected]2e5b60a22011-11-28 15:56:41685}
686
687int ChromeBrowserMainParts::PreCreateThreadsImpl() {
[email protected]c1702d712011-09-08 19:41:43688 run_message_loop_ = false;
[email protected]0c298a52009-09-25 21:33:52689#if defined(OS_WIN)
[email protected]2e5b60a22011-11-28 15:56:41690 PathService::Get(chrome::DIR_USER_DATA, &user_data_dir_);
[email protected]0c298a52009-09-25 21:33:52691#else
692 // Getting the user data dir can fail if the directory isn't
693 // creatable, for example; on Windows in code below we bring up a
694 // dialog prompting the user to pick a different directory.
695 // However, ProcessSingleton needs a real user_data_dir on Mac/Linux,
696 // so it's better to fail here than fail mysteriously elsewhere.
[email protected]2e5b60a22011-11-28 15:56:41697 CHECK(PathService::Get(chrome::DIR_USER_DATA, &user_data_dir_))
[email protected]0c298a52009-09-25 21:33:52698 << "Must be able to get user data directory!";
699#endif
700
[email protected]81054f812012-08-30 00:47:09701 // Android's first run is done in Java instead of native.
702#if !defined(OS_ANDROID)
[email protected]2e5b60a22011-11-28 15:56:41703 process_singleton_.reset(new ProcessSingleton(user_data_dir_));
[email protected]edf04b512012-02-23 09:47:43704 // Ensure ProcessSingleton won't process messages too early. It will be
705 // unlocked in PostBrowserStart().
706 process_singleton_->Lock(NULL);
initial.commit09911bf2008-07-26 23:55:29707
[email protected]911cbcd2012-03-16 20:55:39708 is_first_run_ =
709 (first_run::IsChromeFirstRun() ||
710 parsed_command_line().HasSwitch(switches::kFirstRun)) &&
[email protected]a5e8c6d42012-04-09 20:22:01711 !HasImportSwitch(parsed_command_line());
[email protected]81054f812012-08-30 00:47:09712#endif
[email protected]911cbcd2012-03-16 20:55:39713 browser_process_.reset(new BrowserProcessImpl(parsed_command_line()));
initial.commit09911bf2008-07-26 23:55:29714
[email protected]9b53dab2011-11-14 05:26:05715 if (parsed_command_line().HasSwitch(switches::kEnableProfiling)) {
[email protected]b2a9bbd2011-10-31 22:36:21716 // User wants to override default tracking status.
717 std::string flag =
[email protected]9b53dab2011-11-14 05:26:05718 parsed_command_line().GetSwitchValueASCII(switches::kEnableProfiling);
[email protected]702a12d2012-02-10 19:43:42719 // Default to basic profiling (no parent child support).
720 tracked_objects::ThreadData::Status status =
721 tracked_objects::ThreadData::PROFILING_ACTIVE;
722 if (flag.compare("0") != 0)
723 status = tracked_objects::ThreadData::DEACTIVATED;
724 else if (flag.compare("child") != 0)
725 status = tracked_objects::ThreadData::PROFILING_CHILDREN_ACTIVE;
726 tracked_objects::ThreadData::InitializeAndSetTrackingStatus(status);
[email protected]b2a9bbd2011-10-31 22:36:21727 }
728
[email protected]61b53f972012-02-10 20:39:23729 if (parsed_command_line().HasSwitch(switches::kProfilingOutputFile)) {
730 tracking_objects_.set_output_file_path(
731 parsed_command_line().GetSwitchValuePath(
732 switches::kProfilingOutputFile));
733 }
734
[email protected]a5e8c6d42012-04-09 20:22:01735 local_state_ = InitializeLocalState(parsed_command_line(), is_first_run_);
[email protected]1b084b5e2010-08-03 07:58:19736
[email protected]7defb4f02012-02-07 18:25:46737 // These members must be initialized before returning from this function.
738 master_prefs_.reset(new first_run::MasterPrefs);
[email protected]81054f812012-08-30 00:47:09739
740#if !defined(OS_ANDROID)
741 // Android doesn't use StartupBrowserCreator.
[email protected]fe7c4872012-05-10 20:06:03742 browser_creator_.reset(new StartupBrowserCreator);
[email protected]81054f812012-08-30 00:47:09743 // TODO(yfriedman): Refactor Android to re-use UMABrowsingActivityObserver
[email protected]3770c242012-07-12 22:58:17744 chrome::UMABrowsingActivityObserver::Init();
[email protected]81054f812012-08-30 00:47:09745#endif
[email protected]7defb4f02012-02-07 18:25:46746
[email protected]6346d152012-05-04 04:02:33747 // Convert active labs into switches. This needs to be done before
748 // ResourceBundle::InitSharedInstanceWithLocale as some loaded resources are
749 // affected by experiment flags (--touch-optimized-ui in particular). Not
750 // needed on Android as there aren't experimental flags.
751 about_flags::ConvertFlagsToSwitches(local_state_,
752 CommandLine::ForCurrentProcess());
[email protected]6346d152012-05-04 04:02:33753 local_state_->UpdateCommandLinePrefStore(CommandLine::ForCurrentProcess());
754
755 // Reset the command line in the crash report details, since we may have
756 // just changed it to include experiments.
757 child_process_logging::SetCommandLine(CommandLine::ForCurrentProcess());
758
[email protected]d4515eb2009-01-30 00:40:43759 // If we're running tests (ui_task is non-null), then the ResourceBundle
760 // has already been initialized.
[email protected]716476c2011-12-29 00:07:03761 if (parameters().ui_task) {
[email protected]17e6cde52012-01-05 00:16:57762 browser_process_->SetApplicationLocale("en-US");
[email protected]ce4b6a92009-12-10 00:04:48763 } else {
[email protected]1fec64352010-07-27 13:55:21764 // Mac starts it earlier in |PreMainMessageLoopStart()| (because it is
765 // needed when loading the MainMenu.nib and the language doesn't depend on
766 // anything since it comes from Cocoa.
[email protected]ce4b6a92009-12-10 00:04:48767#if defined(OS_MACOSX)
[email protected]17e6cde52012-01-05 00:16:57768 browser_process_->SetApplicationLocale(l10n_util::GetLocaleOverride());
[email protected]ce4b6a92009-12-10 00:04:48769#else
[email protected]455ee192010-12-22 16:05:57770 const std::string locale =
[email protected]2e5b60a22011-11-28 15:56:41771 local_state_->GetString(prefs::kApplicationLocale);
[email protected]cd87be132011-02-04 13:07:38772 // On a POSIX OS other than ChromeOS, the parameter that is passed to the
773 // method InitSharedInstance is ignored.
[email protected]455ee192010-12-22 16:05:57774 const std::string loaded_locale =
[email protected]fb317a52012-05-09 15:57:47775 ResourceBundle::InitSharedInstanceWithLocale(locale, NULL);
[email protected]cbce47242011-08-12 21:40:59776 if (loaded_locale.empty() &&
[email protected]f967b722011-09-07 00:58:04777 !parsed_command_line().HasSwitch(switches::kNoErrorDialogs)) {
[email protected]cbce47242011-08-12 21:40:59778 ShowMissingLocaleMessageBox();
779 return chrome::RESULT_CODE_MISSING_DATA;
780 }
[email protected]455ee192010-12-22 16:05:57781 CHECK(!loaded_locale.empty()) << "Locale could not be found for " << locale;
[email protected]17e6cde52012-01-05 00:16:57782 browser_process_->SetApplicationLocale(loaded_locale);
[email protected]e1cb0e92010-06-15 07:23:59783
784 FilePath resources_pack_path;
785 PathService::Get(chrome::FILE_RESOURCES_PACK, &resources_pack_path);
[email protected]6c26d9c2012-07-10 19:18:04786 ResourceBundle::GetSharedInstance().AddDataPackFromPath(
[email protected]c49201a2012-05-24 11:04:57787 resources_pack_path, ui::SCALE_FACTOR_100P);
[email protected]cd87be132011-02-04 13:07:38788#endif // defined(OS_MACOSX)
[email protected]ce4b6a92009-12-10 00:04:48789 }
initial.commit09911bf2008-07-26 23:55:29790
[email protected]d9edd312011-02-16 20:52:28791#if defined(TOOLKIT_GTK)
792 g_set_application_name(l10n_util::GetStringUTF8(IDS_PRODUCT_NAME).c_str());
[email protected]d0646f32009-08-27 00:58:46793#endif
794
[email protected]5d39e3142009-12-17 05:54:10795 std::string try_chrome =
[email protected]f967b722011-09-07 00:58:04796 parsed_command_line().GetSwitchValueASCII(switches::kTryChromeAgain);
[email protected]c4b3858c2009-09-09 19:35:31797 if (!try_chrome.empty()) {
[email protected]94fbaa42011-09-07 20:09:54798#if defined(OS_WIN) && !defined(USE_AURA)
[email protected]54430e22011-03-08 00:39:29799 // Setup.exe has determined that we need to run a retention experiment
800 // and has lauched chrome to show the experiment UI.
[email protected]c1702d712011-09-08 19:41:43801 if (process_singleton_->FoundOtherProcessWindow()) {
[email protected]54430e22011-03-08 00:39:29802 // It seems that we don't need to run the experiment since chrome
803 // in the same profile is already running.
804 VLOG(1) << "Retention experiment not required";
[email protected]118b4a92011-04-14 01:15:13805 return TryChromeDialogView::NOT_NOW;
[email protected]54430e22011-03-08 00:39:29806 }
[email protected]b1a71cc2010-08-03 15:21:27807 int try_chrome_int;
808 base::StringToInt(try_chrome, &try_chrome_int);
[email protected]118b4a92011-04-14 01:15:13809 TryChromeDialogView::Result answer =
[email protected]c1702d712011-09-08 19:41:43810 TryChromeDialogView::Show(try_chrome_int, process_singleton_.get());
[email protected]118b4a92011-04-14 01:15:13811 if (answer == TryChromeDialogView::NOT_NOW)
[email protected]1fcfb202011-07-19 19:53:14812 return chrome::RESULT_CODE_NORMAL_EXIT_CANCEL;
[email protected]118b4a92011-04-14 01:15:13813 if (answer == TryChromeDialogView::UNINSTALL_CHROME)
[email protected]1fcfb202011-07-19 19:53:14814 return chrome::RESULT_CODE_NORMAL_EXIT_EXP2;
[email protected]8b07d4f2012-08-03 22:16:48815 // At this point the user is willing to try chrome again.
816 if (answer == TryChromeDialogView::TRY_CHROME_AS_DEFAULT) {
817 // Only set in the unattended case, the interactive case is Windows 8.
818 if (ShellIntegration::CanSetAsDefaultBrowser() ==
819 ShellIntegration::SET_DEFAULT_UNATTENDED)
820 ShellIntegration::SetAsDefaultBrowser();
821 }
[email protected]28de7d942010-02-23 01:43:04822#else
823 // We don't support retention experiments on Mac or Linux.
[email protected]1fcfb202011-07-19 19:53:14824 return content::RESULT_CODE_NORMAL_EXIT;
[email protected]28de7d942010-02-23 01:43:04825#endif // defined(OS_WIN)
[email protected]37a24e02009-06-12 21:34:14826 }
[email protected]37a24e02009-06-12 21:34:14827
[email protected]81054f812012-08-30 00:47:09828 // Android does first run in Java instead of native.
829#if !defined(OS_ANDROID)
[email protected]74be069e82010-06-25 00:12:49830 // On first run, we need to process the predictor preferences before the
[email protected]34f73fb2010-03-24 20:50:34831 // browser's profile_manager object is created, but after ResourceBundle
832 // is initialized.
[email protected]2e5b60a22011-11-28 15:56:41833 first_run_ui_bypass_ = false; // True to skip first run UI.
834 if (is_first_run_) {
[email protected]a2e6b8e2012-02-15 03:59:49835 first_run_ui_bypass_ = !first_run::ProcessMasterPreferences(
[email protected]2e5b60a22011-11-28 15:56:41836 user_data_dir_, master_prefs_.get());
[email protected]fe7c4872012-05-10 20:06:03837 AddFirstRunNewTabs(browser_creator_.get(), master_prefs_->new_tabs);
[email protected]fe247ac2009-03-19 00:10:48838
839 // If we are running in App mode, we do not want to show the importer
840 // (first run) UI.
[email protected]2e5b60a22011-11-28 15:56:41841 if (!first_run_ui_bypass_ &&
[email protected]f967b722011-09-07 00:58:04842 (parsed_command_line().HasSwitch(switches::kApp) ||
843 parsed_command_line().HasSwitch(switches::kAppId) ||
844 parsed_command_line().HasSwitch(switches::kNoFirstRun)))
[email protected]2e5b60a22011-11-28 15:56:41845 first_run_ui_bypass_ = true;
[email protected]dfa08b042011-12-28 23:07:21846
847 // Create Sentinel if no-first-run argument is passed in.
848 if (parsed_command_line().HasSwitch(switches::kNoFirstRun))
849 first_run::CreateSentinel();
[email protected]fe247ac2009-03-19 00:10:48850 }
[email protected]81054f812012-08-30 00:47:09851#endif
[email protected]fe247ac2009-03-19 00:10:48852
[email protected]f8abf722010-07-07 19:46:24853 // TODO(viettrungluu): why don't we run this earlier?
[email protected]f967b722011-09-07 00:58:04854 if (!parsed_command_line().HasSwitch(switches::kNoErrorDialogs))
[email protected]34f73fb2010-03-24 20:50:34855 WarnAboutMinimumSystemRequirements();
initial.commit09911bf2008-07-26 23:55:29856
[email protected]2ec5a482012-02-22 00:01:18857#if defined(OS_LINUX) || defined(OS_OPENBSD) || defined(OS_MACOSX)
[email protected]e643e57c2012-01-28 02:26:09858 // Set the product channel for crash reports.
859 child_process_logging::SetChannel(
860 chrome::VersionInfo::GetVersionStringModifier());
861#endif
862
[email protected]332749032011-10-22 00:32:46863 InitializeNetworkOptions(parsed_command_line());
[email protected]332749032011-10-22 00:32:46864
[email protected]83ab4a282012-07-12 18:19:45865 // Initialize tracking synchronizer system.
[email protected]10fe40c2011-11-05 03:27:46866 tracking_synchronizer_ = new chrome_browser_metrics::TrackingSynchronizer();
[email protected]332749032011-10-22 00:32:46867
[email protected]6b5f21d2009-04-13 17:01:35868 // Now that all preferences have been registered, set the install date
869 // for the uninstall metrics if this is our first run. This only actually
870 // gets used if the user has metrics reporting enabled at uninstall time.
871 int64 install_date =
[email protected]2e5b60a22011-11-28 15:56:41872 local_state_->GetInt64(prefs::kUninstallMetricsInstallDate);
[email protected]6b5f21d2009-04-13 17:01:35873 if (install_date == 0) {
[email protected]2e5b60a22011-11-28 15:56:41874 local_state_->SetInt64(prefs::kUninstallMetricsInstallDate,
[email protected]6b5f21d2009-04-13 17:01:35875 base::Time::Now().ToTimeT());
876 }
877
[email protected]e457f742010-04-02 17:25:43878#if defined(OS_MACOSX)
879 // Get the Keychain API to register for distributed notifications on the main
880 // thread, which has a proper CFRunloop, instead of later on the I/O thread,
881 // which doesn't. This ensures those notifications will get delivered
882 // properly. See issue 37766.
883 // (Note that the callback mask here is empty. I don't want to register for
884 // any callbacks, I just want to initialize the mechanism.)
885 SecKeychainAddCallback(&KeychainCallback, 0, NULL);
886#endif
887
[email protected]69479b922012-02-02 09:56:20888 // Now the command line has been mutated based on about:flags, we can setup
889 // metrics and initialize field trials. The field trials are needed by
890 // IOThread's initialization which happens in BrowserProcess:PreCreateThreads.
891 SetupMetricsAndFieldTrials();
892
893 // ChromeOS needs ResourceBundle::InitSharedInstance to be called before this.
894 browser_process_->PreCreateThreads();
895
[email protected]2e5b60a22011-11-28 15:56:41896 return content::RESULT_CODE_NORMAL_EXIT;
897}
898
[email protected]2167cd62011-11-30 21:20:21899void ChromeBrowserMainParts::PreMainMessageLoopRun() {
900 result_code_ = PreMainMessageLoopRunImpl();
901
902 for (size_t i = 0; i < chrome_extra_parts_.size(); ++i)
903 chrome_extra_parts_[i]->PreMainMessageLoopRun();
904}
905
[email protected]97ef83f2011-12-19 19:26:36906// PreMainMessageLoopRun calls these extra stages in the following order:
907// PreMainMessageLoopRunImpl()
908// ... initial setup, including browser_process_ setup.
909// PreProfileInit()
910// ... additional setup, including CreateProfile()
911// PostProfileInit()
912// ... additional setup
913// PreBrowserStart()
[email protected]fe7c4872012-05-10 20:06:03914// ... browser_creator_->Start (OR parameters().ui_task->Run())
[email protected]97ef83f2011-12-19 19:26:36915// PostBrowserStart()
916
917void ChromeBrowserMainParts::PreProfileInit() {
918 for (size_t i = 0; i < chrome_extra_parts_.size(); ++i)
919 chrome_extra_parts_[i]->PreProfileInit();
920}
921
922void ChromeBrowserMainParts::PostProfileInit() {
923 for (size_t i = 0; i < chrome_extra_parts_.size(); ++i)
924 chrome_extra_parts_[i]->PostProfileInit();
925}
926
927void ChromeBrowserMainParts::PreBrowserStart() {
928 for (size_t i = 0; i < chrome_extra_parts_.size(); ++i)
929 chrome_extra_parts_[i]->PreBrowserStart();
930}
931
932void ChromeBrowserMainParts::PostBrowserStart() {
[email protected]81054f812012-08-30 00:47:09933#if !defined(OS_ANDROID)
[email protected]278a34d2012-06-07 00:34:58934 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kVisitURLs))
935 RunPageCycler();
[email protected]81054f812012-08-30 00:47:09936#endif
[email protected]278a34d2012-06-07 00:34:58937
[email protected]97ef83f2011-12-19 19:26:36938 for (size_t i = 0; i < chrome_extra_parts_.size(); ++i)
939 chrome_extra_parts_[i]->PostBrowserStart();
[email protected]81054f812012-08-30 00:47:09940#if !defined(OS_ANDROID)
[email protected]edf04b512012-02-23 09:47:43941 // Allow ProcessSingleton to process messages.
942 process_singleton_->Unlock();
[email protected]81054f812012-08-30 00:47:09943#endif
[email protected]97ef83f2011-12-19 19:26:36944}
945
[email protected]81054f812012-08-30 00:47:09946#if !defined(OS_ANDROID)
[email protected]278a34d2012-06-07 00:34:58947void ChromeBrowserMainParts::RunPageCycler() {
948 CommandLine* command_line = CommandLine::ForCurrentProcess();
[email protected]323fd7122012-08-24 14:45:11949 // We assume a native desktop for tests, but we will need to find a way to
950 // get the proper host desktop type once we start running these tests in ASH.
951 Browser* browser = browser::FindBrowserWithProfile(
952 profile_, chrome::HOST_DESKTOP_TYPE_NATIVE);
[email protected]278a34d2012-06-07 00:34:58953 DCHECK(browser);
954 PageCycler* page_cycler = NULL;
955 FilePath input_file =
956 command_line->GetSwitchValuePath(switches::kVisitURLs);
957 page_cycler = new PageCycler(browser, input_file);
958 page_cycler->set_errors_file(
959 input_file.AddExtension(FILE_PATH_LITERAL(".errors")));
960 if (command_line->HasSwitch(switches::kRecordStats)) {
961 page_cycler->set_stats_file(
962 command_line->GetSwitchValuePath(switches::kRecordStats));
963 }
[email protected]165b0562012-06-20 00:41:22964 page_cycler->Run();
[email protected]278a34d2012-06-07 00:34:58965}
[email protected]81054f812012-08-30 00:47:09966#endif // !defined(OS_ANDROID)
[email protected]278a34d2012-06-07 00:34:58967
[email protected]b98def82012-06-28 23:42:45968void ChromeBrowserMainParts::SetupPlatformFieldTrials() {
969 // Base class implementation of this does nothing.
970}
971
[email protected]2e5b60a22011-11-28 15:56:41972int ChromeBrowserMainParts::PreMainMessageLoopRunImpl() {
[email protected]81054f812012-08-30 00:47:09973 // Android updates the metrics service dynamically depending on whether the
974 // application is in the foreground or not. Do not start here.
975#if !defined(OS_ANDROID)
[email protected]5281db1d2012-01-11 22:51:54976 // Now that the file thread has been started, start recording.
977 StartMetricsRecording();
[email protected]81054f812012-08-30 00:47:09978#endif
[email protected]99907362012-01-11 05:41:40979
[email protected]2e5b60a22011-11-28 15:56:41980 // Create watchdog thread after creating all other threads because it will
981 // watch the other threads and they must be running.
982 browser_process_->watchdog_thread();
[email protected]61e95722009-11-25 06:54:11983
[email protected]d2caaa22011-12-12 03:04:15984 // Do any initializating in the browser process that requires all threads
985 // running.
986 browser_process_->PreMainMessageLoopRun();
987
initial.commit09911bf2008-07-26 23:55:29988 // Record last shutdown time into a histogram.
989 browser_shutdown::ReadLastShutdownInfo();
[email protected]04999e62009-09-25 01:27:31990
[email protected]01109dcf2010-03-27 00:56:43991#if defined(OS_WIN)
[email protected]04999e62009-09-25 01:27:31992 // On Windows, we use our startup as an opportunity to do upgrade/uninstall
993 // tasks. Those care whether the browser is already running. On Linux/Mac,
994 // upgrade/uninstall happen separately.
[email protected]a3abd5572011-04-15 02:09:33995 bool already_running = browser_util::IsBrowserAlreadyRunning();
initial.commit09911bf2008-07-26 23:55:29996
997 // If the command line specifies 'uninstall' then we need to work here
998 // unless we detect another chrome browser running.
[email protected]c1702d712011-09-08 19:41:43999 if (parsed_command_line().HasSwitch(switches::kUninstall)) {
[email protected]53c38d232009-02-13 20:52:181000 return DoUninstallTasks(already_running);
[email protected]c1702d712011-09-08 19:41:431001 }
initial.commit09911bf2008-07-26 23:55:291002
[email protected]f967b722011-09-07 00:58:041003 if (parsed_command_line().HasSwitch(switches::kHideIcons) ||
[email protected]c1702d712011-09-08 19:41:431004 parsed_command_line().HasSwitch(switches::kShowIcons)) {
[email protected]b48c9182011-10-26 18:03:301005 return ChromeBrowserMainPartsWin::HandleIconsCommands(
1006 parsed_command_line_);
[email protected]c1702d712011-09-08 19:41:431007 }
[email protected]b48c9182011-10-26 18:03:301008#endif
1009
[email protected]f967b722011-09-07 00:58:041010 if (parsed_command_line().HasSwitch(switches::kMakeDefaultBrowser)) {
[email protected]201028d2010-02-12 21:28:011011 return ShellIntegration::SetAsDefaultBrowser() ?
[email protected]1fcfb202011-07-19 19:53:141012 static_cast<int>(content::RESULT_CODE_NORMAL_EXIT) :
1013 static_cast<int>(chrome::RESULT_CODE_SHELL_INTEGRATION_FAILED);
initial.commit09911bf2008-07-26 23:55:291014 }
1015
[email protected]81054f812012-08-30 00:47:091016 // Android doesn't support extensions and doesn't implement ProcessSingleton.
1017#if !defined(OS_ANDROID)
[email protected]6aeac8342010-10-01 20:21:181018 // If the command line specifies --pack-extension, attempt the pack extension
1019 // startup action and exit.
[email protected]f967b722011-09-07 00:58:041020 if (parsed_command_line().HasSwitch(switches::kPackExtension)) {
[email protected]d9ede582012-08-14 19:21:381021 extensions::StartupHelper extension_startup_helper;
1022 if (extension_startup_helper.PackExtension(parsed_command_line()))
[email protected]1fcfb202011-07-19 19:53:141023 return content::RESULT_CODE_NORMAL_EXIT;
[email protected]c1702d712011-09-08 19:41:431024 return chrome::RESULT_CODE_PACK_EXTENSION_ERROR;
[email protected]6aeac8342010-10-01 20:21:181025 }
1026
[email protected]ed8ce0b2012-07-28 02:18:101027 bool pass_command_line = true;
1028
[email protected]5425d062010-04-11 23:16:021029#if !defined(OS_MACOSX)
1030 // In environments other than Mac OS X we support import of settings
1031 // from other browsers. In case this process is a short-lived "import"
1032 // process that another browser runs just to import the settings, we
1033 // don't want to be checking for another browser process, by design.
[email protected]ed8ce0b2012-07-28 02:18:101034 pass_command_line = !HasImportSwitch(parsed_command_line());
[email protected]bd48c2b02010-04-09 20:32:421035#endif
[email protected]ed8ce0b2012-07-28 02:18:101036
1037 // If we're being launched just to check the connector policy, we are
1038 // short-lived and don't want to be passing that switch off.
1039 pass_command_line = pass_command_line && !parsed_command_line().HasSwitch(
1040 switches::kCheckCloudPrintConnectorPolicy);
1041
1042 if (pass_command_line) {
[email protected]5425d062010-04-11 23:16:021043 // When another process is running, use that process instead of starting a
1044 // new one. NotifyOtherProcess will currently give the other process up to
1045 // 20 seconds to respond. Note that this needs to be done before we attempt
1046 // to read the profile.
[email protected]5d364542012-04-05 07:15:391047 notify_result_ = process_singleton_->NotifyOtherProcessOrCreate(
1048 base::Bind(&ProcessSingletonNotificationCallback));
[email protected]66013922011-09-13 18:53:421049 switch (notify_result_) {
[email protected]5425d062010-04-11 23:16:021050 case ProcessSingleton::PROCESS_NONE:
1051 // No process already running, fall through to starting a new one.
1052 break;
[email protected]bd48c2b02010-04-09 20:32:421053
[email protected]5425d062010-04-11 23:16:021054 case ProcessSingleton::PROCESS_NOTIFIED:
1055#if defined(OS_POSIX) && !defined(OS_MACOSX)
[email protected]d5c45b72011-01-04 18:38:511056 printf("%s\n", base::SysWideToNativeMB(UTF16ToWide(
1057 l10n_util::GetStringUTF16(IDS_USED_EXISTING_BROWSER))).c_str());
[email protected]5425d062010-04-11 23:16:021058#endif
[email protected]ed8ce0b2012-07-28 02:18:101059 return chrome::RESULT_CODE_NORMAL_EXIT_PROCESS_NOTIFIED;
[email protected]bd48c2b02010-04-09 20:32:421060
[email protected]5425d062010-04-11 23:16:021061 case ProcessSingleton::PROFILE_IN_USE:
[email protected]1fcfb202011-07-19 19:53:141062 return chrome::RESULT_CODE_PROFILE_IN_USE;
[email protected]5425d062010-04-11 23:16:021063
[email protected]4a44bc32010-05-28 22:22:441064 case ProcessSingleton::LOCK_ERROR:
1065 LOG(ERROR) << "Failed to create a ProcessSingleton for your profile "
1066 "directory. This means that running multiple instances "
1067 "would start multiple browser processes rather than "
1068 "opening a new window in the existing process. Aborting "
1069 "now to avoid profile corruption.";
[email protected]1fcfb202011-07-19 19:53:141070 return chrome::RESULT_CODE_PROFILE_IN_USE;
[email protected]4a44bc32010-05-28 22:22:441071
[email protected]5425d062010-04-11 23:16:021072 default:
1073 NOTREACHED();
1074 }
[email protected]bd48c2b02010-04-09 20:32:421075 }
[email protected]81054f812012-08-30 00:47:091076#endif // !defined(OS_ANDROID)
[email protected]bd48c2b02010-04-09 20:32:421077
[email protected]2989d7342010-10-27 02:00:091078#if defined(USE_X11)
1079 SetBrowserX11ErrorHandlers();
1080#endif
1081
[email protected]50462bf02011-11-21 19:13:311082 // Desktop construction occurs here, (required before profile creation).
[email protected]97ef83f2011-12-19 19:26:361083 PreProfileInit();
[email protected]50462bf02011-11-21 19:13:311084
[email protected]20271f32010-03-26 03:04:511085 // Profile creation ----------------------------------------------------------
[email protected]08e6f6d2009-12-14 17:42:121086
[email protected]2e5b60a22011-11-28 15:56:411087 if (is_first_run_) {
[email protected]d91268022011-08-26 13:17:371088 // Warn the ProfileManager that an import process will run, possibly
1089 // locking the WebDataService directory of the next Profile created.
[email protected]17e6cde52012-01-05 00:16:571090 browser_process_->profile_manager()->SetWillImport();
[email protected]d91268022011-08-26 13:17:371091 }
1092
[email protected]2e5b60a22011-11-28 15:56:411093 profile_ = CreateProfile(parameters(), user_data_dir_, parsed_command_line());
[email protected]c1702d712011-09-08 19:41:431094 if (!profile_)
[email protected]1fcfb202011-07-19 19:53:141095 return content::RESULT_CODE_NORMAL_EXIT;
[email protected]20271f32010-03-26 03:04:511096
[email protected]81054f812012-08-30 00:47:091097#if defined(ENABLE_BACKGROUND)
[email protected]279170832011-10-12 23:38:031098 // Autoload any profiles which are running background apps.
1099 // TODO(rlp): Do this on a separate thread. See http://crbug.com/99075.
[email protected]d6bbd2932012-03-19 17:10:071100 browser_process_->profile_manager()->AutoloadProfiles();
[email protected]81054f812012-08-30 00:47:091101#endif
[email protected]20271f32010-03-26 03:04:511102 // Post-profile init ---------------------------------------------------------
[email protected]9acc48602009-11-30 21:18:511103
[email protected]81054f812012-08-30 00:47:091104#if !defined(OS_MACOSX) && !defined(OS_ANDROID)
initial.commit09911bf2008-07-26 23:55:291105 // Importing other browser settings is done in a browser-like process
1106 // that exits when this task has finished.
[email protected]3ef4755e2010-09-27 20:25:041107 // TODO(port): Port the Mac's IPC-based implementation to other platforms to
1108 // replace this implementation. http://crbug.com/22142
[email protected]a5e8c6d42012-04-09 20:22:011109 if (HasImportSwitch(parsed_command_line())) {
[email protected]4b516152012-01-07 03:09:151110 return first_run::ImportNow(profile_, parsed_command_line());
[email protected]272709ca2010-04-01 00:24:471111 }
[email protected]7da7f3432009-10-28 20:12:081112#endif
initial.commit09911bf2008-07-26 23:55:291113
[email protected]04999e62009-09-25 01:27:311114#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:291115 // Do the tasks if chrome has been upgraded while it was last running.
[email protected]f967b722011-09-07 00:58:041116 if (!already_running && upgrade_util::DoUpgradeTasks(parsed_command_line()))
[email protected]1fcfb202011-07-19 19:53:141117 return content::RESULT_CODE_NORMAL_EXIT;
initial.commit09911bf2008-07-26 23:55:291118
[email protected]68c921f2008-10-15 21:37:461119 // Check if there is any machine level Chrome installed on the current
1120 // machine. If yes and the current Chrome process is user level, we do not
1121 // allow the user level Chrome to run. So we notify the user and uninstall
1122 // user level Chrome.
1123 // Note this check should only happen here, after all the checks above
1124 // (uninstall, resource bundle initialization, other chrome browser
1125 // processes etc).
[email protected]6a7748e2011-05-27 16:44:311126 // Do not allow this to occur for Chrome Frame user-to-system handoffs.
[email protected]f967b722011-09-07 00:58:041127 if (!parsed_command_line().HasSwitch(switches::kChromeFrame) &&
[email protected]b48c9182011-10-26 18:03:301128 ChromeBrowserMainPartsWin::CheckMachineLevelInstall()) {
[email protected]1fcfb202011-07-19 19:53:141129 return chrome::RESULT_CODE_MACHINE_LEVEL_INSTALL_EXISTS;
[email protected]c1702d712011-09-08 19:41:431130 }
[email protected]b48c9182011-10-26 18:03:301131#endif
[email protected]68c921f2008-10-15 21:37:461132
[email protected]81054f812012-08-30 00:47:091133#if !defined(OS_ANDROID)
[email protected]7e0831b2010-02-01 08:45:151134 // Create the TranslateManager singleton.
[email protected]c1702d712011-09-08 19:41:431135 translate_manager_ = TranslateManager::GetInstance();
1136 DCHECK(translate_manager_ != NULL);
[email protected]7e0831b2010-02-01 08:45:151137
[email protected]a6c7a8252012-06-25 18:28:241138 // Initialize Managed Mode.
1139 ManagedMode::Init(profile_);
[email protected]81054f812012-08-30 00:47:091140#endif
[email protected]a6c7a8252012-06-25 18:28:241141
1142 // TODO(stevenjb): Move WIN and MACOSX specific code to appropriate Parts.
[email protected]97ef83f2011-12-19 19:26:361143 // (requires supporting early exit).
1144 PostProfileInit();
[email protected]50462bf02011-11-21 19:13:311145
[email protected]81054f812012-08-30 00:47:091146#if !defined(OS_ANDROID)
initial.commit09911bf2008-07-26 23:55:291147 // Show the First Run UI if this is the first time Chrome has been run on
1148 // this computer, or we're being compelled to do so by a command line flag.
1149 // Note that this be done _after_ the PrefService is initialized and all
1150 // preferences are registered, since some of the code that the importer
1151 // touches reads preferences.
[email protected]2e5b60a22011-11-28 15:56:411152 if (is_first_run_) {
1153 if (!first_run_ui_bypass_) {
[email protected]3553f52f2012-01-05 01:01:281154 first_run::AutoImport(profile_,
1155 master_prefs_->homepage_defined,
1156 master_prefs_->do_import_items,
1157 master_prefs_->dont_import_items,
[email protected]3553f52f2012-01-05 01:01:281158 master_prefs_->make_chrome_default,
1159 process_singleton_.get());
[email protected]48e9dfe2012-03-19 16:52:081160#if defined(OS_POSIX) && !defined(OS_CHROMEOS)
[email protected]79117172009-08-28 16:11:441161 // On Windows, the download is tagged with enable/disable stats so there
1162 // is no need for this code.
[email protected]5ee921c2009-07-28 19:21:251163
[email protected]79117172009-08-28 16:11:441164 // If stats reporting was turned on by the first run dialog then toggle
1165 // the pref.
1166 if (GoogleUpdateSettings::GetCollectStatsConsent())
[email protected]2e5b60a22011-11-28 15:56:411167 local_state_->SetBoolean(prefs::kMetricsReportingEnabled, true);
[email protected]48e9dfe2012-03-19 16:52:081168#endif // OS_POSIX && !OS_CHROMEOS
[email protected]2e5b60a22011-11-28 15:56:411169 } // if (!first_run_ui_bypass_)
[email protected]96fe2e22012-03-07 22:54:191170
[email protected]2cd4fde2012-06-26 03:10:261171 chrome::SetNewHomePagePrefs(profile_->GetPrefs());
[email protected]17e6cde52012-01-05 00:16:571172 browser_process_->profile_manager()->OnImportFinished(profile_);
[email protected]eed73252012-08-03 18:47:441173
[email protected]820a1402012-08-09 23:13:511174 if (!master_prefs_->suppress_first_run_default_browser_prompt) {
1175 browser_creator_->set_show_main_browser_window(
1176 !chrome::ShowFirstRunDefaultBrowserPrompt(profile_));
1177 } else {
[email protected]eed73252012-08-03 18:47:441178 browser_creator_->set_is_default_browser_dialog_suppressed(true);
[email protected]820a1402012-08-09 23:13:511179 }
[email protected]2e5b60a22011-11-28 15:56:411180 } // if (is_first_run_)
[email protected]81054f812012-08-30 00:47:091181#endif // !defined(OS_ANDROID)
initial.commit09911bf2008-07-26 23:55:291182
[email protected]b48c9182011-10-26 18:03:301183#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:291184 // Sets things up so that if we crash from this point on, a dialog will
1185 // popup asking the user to restart chrome. It is done this late to avoid
1186 // testing against a bunch of special cases that are taken care early on.
[email protected]b48c9182011-10-26 18:03:301187 ChromeBrowserMainPartsWin::PrepareRestartOnCrashEnviroment(
1188 parsed_command_line());
initial.commit09911bf2008-07-26 23:55:291189
[email protected]0fd23af2011-02-20 06:33:041190 // Registers Chrome with the Windows Restart Manager, which will restore the
1191 // Chrome session when the computer is restarted after a system update.
1192 // This could be run as late as WM_QUERYENDSESSION for system update reboots,
1193 // but should run on startup if extended to handle crashes/hangs/patches.
1194 // Also, better to run once here than once for each HWND's WM_QUERYENDSESSION.
[email protected]b48c9182011-10-26 18:03:301195 if (base::win::GetVersion() >= base::win::VERSION_VISTA) {
1196 ChromeBrowserMainPartsWin::RegisterApplicationRestart(
1197 parsed_command_line());
1198 }
[email protected]a4422802012-05-03 09:19:151199
1200 // Verify that the profile is not on a network share and if so prepare to show
1201 // notification to the user.
[email protected]297fb7c22012-06-26 12:37:491202 if (NetworkProfileBubble::ShouldCheckNetworkProfile(profile_)) {
[email protected]4d0f1732012-06-25 17:55:201203 content::BrowserThread::PostTask(content::BrowserThread::FILE, FROM_HERE,
[email protected]297fb7c22012-06-26 12:37:491204 base::Bind(&NetworkProfileBubble::CheckNetworkProfile, profile_));
[email protected]a4422802012-05-03 09:19:151205 }
[email protected]81d9b72d2012-03-26 22:29:171206#endif // OS_WIN
[email protected]0fd23af2011-02-20 06:33:041207
[email protected]81d9b72d2012-03-26 22:29:171208#if defined(ENABLE_RLZ)
initial.commit09911bf2008-07-26 23:55:291209 // Init the RLZ library. This just binds the dll and schedules a task on the
1210 // file thread to be run sometime later. If this is the first run we record
1211 // the installation event.
[email protected]9c289672011-06-10 13:42:071212 bool google_search_default = false;
[email protected]d74cc18a2011-06-10 17:53:521213 TemplateURLService* template_url_service =
[email protected]c1702d712011-09-08 19:41:431214 TemplateURLServiceFactory::GetForProfile(profile_);
[email protected]d74cc18a2011-06-10 17:53:521215 if (template_url_service) {
1216 const TemplateURL* url_template =
1217 template_url_service->GetDefaultSearchProvider();
[email protected]360ba052012-04-04 17:26:131218 google_search_default =
1219 url_template && url_template->url_ref().HasGoogleBaseURLs();
[email protected]9c289672011-06-10 13:42:071220 }
[email protected]6c6b02d2011-09-02 03:36:471221
[email protected]c1702d712011-09-08 19:41:431222 PrefService* pref_service = profile_->GetPrefs();
[email protected]360ba052012-04-04 17:26:131223 bool google_search_homepage = pref_service &&
1224 google_util::IsGoogleHomePageUrl(
1225 pref_service->GetString(prefs::kHomePage));
[email protected]6c6b02d2011-09-02 03:36:471226
[email protected]2e5b60a22011-11-28 15:56:411227 RLZTracker::InitRlzDelayed(is_first_run_, master_prefs_->ping_delay,
[email protected]6c6b02d2011-09-02 03:36:471228 google_search_default, google_search_homepage);
1229
1230 // Prime the RLZ cache for the home page access point so that its avaiable
1231 // for the startup page if needed (i.e., when the startup page is set to
1232 // the home page).
1233 RLZTracker::GetAccessPointRlz(rlz_lib::CHROME_HOME_PAGE, NULL);
[email protected]81d9b72d2012-03-26 22:29:171234#endif // defined(ENABLE_RLZ)
[email protected]b48c9182011-10-26 18:03:301235
[email protected]a12f7fb2011-01-20 18:21:081236 // Configure modules that need access to resources.
[email protected]64d50ed2009-09-22 21:05:471237 net::NetModule::SetResourceProvider(chrome_common_net::NetResourceProvider);
initial.commit09911bf2008-07-26 23:55:291238
[email protected]3dd1f6d52008-09-15 18:28:091239 // In unittest mode, this will do nothing. In normal mode, this will create
[email protected]665fa012012-05-09 00:24:381240 // the global IntranetRedirectDetector instance, which will promptly go to
1241 // sleep for seven seconds (to avoid slowing startup), and wake up afterwards
1242 // to see if it should do anything else.
[email protected]3dd1f6d52008-09-15 18:28:091243 //
1244 // A simpler way of doing all this would be to have some function which could
[email protected]665fa012012-05-09 00:24:381245 // give the time elapsed since startup, and simply have this object check that
1246 // when asked to initialize itself, but this doesn't seem to exist.
[email protected]3dd1f6d52008-09-15 18:28:091247 //
[email protected]665fa012012-05-09 00:24:381248 // This can't be created in the BrowserProcessImpl constructor because it
1249 // needs to read prefs that get set after that runs.
[email protected]c1702d712011-09-08 19:41:431250 browser_process_->intranet_redirect_detector();
[email protected]ed666562012-03-21 19:49:521251 GoogleSearchCounter::RegisterForNotifications();
initial.commit09911bf2008-07-26 23:55:291252
[email protected]3b543ab2011-09-17 21:47:001253 // Disable SDCH filtering if switches::kEnableSdch is 0.
1254 int sdch_enabled = 1;
1255 if (parsed_command_line().HasSwitch(switches::kEnableSdch)) {
1256 base::StringToInt(parsed_command_line().GetSwitchValueASCII(
1257 switches::kEnableSdch), &sdch_enabled);
1258 if (!sdch_enabled)
1259 net::SdchManager::EnableSdchSupport(false);
1260 }
1261 if (sdch_enabled) {
1262 // Perform A/B test to measure global impact of SDCH support.
1263 // Set up a field trial to see what disabling SDCH does to latency of page
1264 // layout globally.
1265 base::FieldTrial::Probability kSDCH_DIVISOR = 1000;
1266 base::FieldTrial::Probability kSDCH_DISABLE_PROBABILITY = 1; // 0.1% prob.
1267 // After March 31, 2012 builds, it will always be in default group.
[email protected]2d4729582012-04-12 07:08:071268 int sdch_enabled_group = -1;
[email protected]3b543ab2011-09-17 21:47:001269 scoped_refptr<base::FieldTrial> sdch_trial(
[email protected]2d4729582012-04-12 07:08:071270 base::FieldTrialList::FactoryGetFieldTrial(
1271 "GlobalSdch", kSDCH_DIVISOR, "global_enable_sdch", 2012, 3, 31,
1272 &sdch_enabled_group));
[email protected]e695fbd62009-06-30 16:31:541273
[email protected]58d2d2d2010-08-05 22:46:331274 sdch_trial->AppendGroup("global_disable_sdch",
[email protected]e12d50592010-01-06 17:35:191275 kSDCH_DISABLE_PROBABILITY);
[email protected]3b543ab2011-09-17 21:47:001276 if (sdch_enabled_group != sdch_trial->group())
1277 net::SdchManager::EnableSdchSupport(false);
[email protected]60889422008-09-23 01:18:161278 }
[email protected]e695fbd62009-06-30 16:31:541279
[email protected]b7672d12011-11-06 03:19:001280 if (parsed_command_line().HasSwitch(switches::kEnableWatchdog))
1281 InstallJankometer(parsed_command_line());
initial.commit09911bf2008-07-26 23:55:291282
[email protected]e1504d82009-07-03 15:27:151283#if defined(OS_WIN) && !defined(GOOGLE_CHROME_BUILD)
[email protected]f967b722011-09-07 00:58:041284 if (parsed_command_line().HasSwitch(switches::kDebugPrint)) {
[email protected]884fbb72010-08-04 01:47:551285 FilePath path =
[email protected]f967b722011-09-07 00:58:041286 parsed_command_line().GetSwitchValuePath(switches::kDebugPrint);
[email protected]d9d42992010-09-13 19:39:191287 printing::PrintedDocument::set_debug_dump_path(path);
initial.commit09911bf2008-07-26 23:55:291288 }
[email protected]1bcdb532009-01-16 17:47:571289#endif
initial.commit09911bf2008-07-26 23:55:291290
[email protected]f967b722011-09-07 00:58:041291 HandleTestParameters(parsed_command_line());
[email protected]5281db1d2012-01-11 22:51:541292 RecordBreakpadStatusUMA(browser_process_->metrics_service());
[email protected]2e5b60a22011-11-28 15:56:411293 about_flags::RecordUMAStatistics(local_state_);
[email protected]04261032011-07-21 08:42:081294 LanguageUsageMetrics::RecordAcceptLanguages(
[email protected]c1702d712011-09-08 19:41:431295 profile_->GetPrefs()->GetString(prefs::kAcceptLanguages));
[email protected]04261032011-07-21 08:42:081296 LanguageUsageMetrics::RecordApplicationLanguage(
[email protected]17e6cde52012-01-05 00:16:571297 browser_process_->GetApplicationLocale());
[email protected]3cf4f0992009-02-03 23:00:301298
[email protected]6aeac8342010-10-01 20:21:181299 // The extension service may be available at this point. If the command line
1300 // specifies --uninstall-extension, attempt the uninstall extension startup
1301 // action.
[email protected]f967b722011-09-07 00:58:041302 if (parsed_command_line().HasSwitch(switches::kUninstallExtension)) {
[email protected]d9ede582012-08-14 19:21:381303 extensions::StartupHelper extension_startup_helper;
1304 if (extension_startup_helper.UninstallExtension(
1305 parsed_command_line(), profile_))
[email protected]1fcfb202011-07-19 19:53:141306 return content::RESULT_CODE_NORMAL_EXIT;
[email protected]c1702d712011-09-08 19:41:431307 return chrome::RESULT_CODE_UNINSTALL_EXTENSION_ERROR;
[email protected]6aeac8342010-10-01 20:21:181308 }
1309
[email protected]08f1c5e2011-12-01 01:54:341310 // Start watching for hangs during startup. We disarm this hang detector when
1311 // ThreadWatcher takes over or when browser is shutdown or when
1312 // startup_watcher_ is deleted.
1313 startup_watcher_->Arm(base::TimeDelta::FromSeconds(300));
1314
[email protected]c0c55e92011-09-10 18:47:301315 // Start watching for a hang.
1316 MetricsService::LogNeedForCleanShutdown();
1317
[email protected]15406b6a2010-04-21 00:35:011318#if defined(OS_WIN)
[email protected]8a9c63e2010-04-19 21:27:551319 // We check this here because if the profile is OTR (chromeos possibility)
1320 // it won't still be accessible after browser is destroyed.
[email protected]2e5b60a22011-11-28 15:56:411321 record_search_engine_ = is_first_run_ && !profile_->IsOffTheRecord();
[email protected]15406b6a2010-04-21 00:35:011322#endif
[email protected]8a9c63e2010-04-19 21:27:551323
[email protected]17543d62010-10-07 22:37:381324 // Create the instance of the cloud print proxy service so that it can launch
1325 // the service process if needed. This is needed because the service process
1326 // might have shutdown because an update was available.
[email protected]17cefaaf2011-08-02 14:42:581327 // TODO(torne): this should maybe be done with
1328 // ProfileKeyedServiceFactory::ServiceIsCreatedWithProfile() instead?
[email protected]058e5732012-03-01 22:48:031329#if !defined(OS_ANDROID)
[email protected]c1702d712011-09-08 19:41:431330 CloudPrintProxyServiceFactory::GetForProfile(profile_);
[email protected]058e5732012-03-01 22:48:031331#endif
[email protected]17543d62010-10-07 22:37:381332
[email protected]4e039f242011-12-03 07:29:381333 // Load GPU Blacklist.
1334 InitializeGpuDataManager(parsed_command_line());
[email protected]bb4bf9d72011-02-26 02:37:381335
[email protected]dedfabae2011-03-04 04:00:401336 // Start watching all browser threads for responsiveness.
[email protected]f967b722011-09-07 00:58:041337 ThreadWatcherList::StartWatchingAll(parsed_command_line());
[email protected]dedfabae2011-03-04 04:00:401338
[email protected]773ebb92011-11-15 19:06:521339#if !defined(DISABLE_NACL)
[email protected]b1511ab02012-07-02 23:45:511340 if (parsed_command_line().HasSwitch(switches::kPnaclDir)) {
1341 PathService::Override(chrome::DIR_PNACL_BASE,
1342 parsed_command_line().GetSwitchValuePath(
1343 switches::kPnaclDir));
1344 }
[email protected]773ebb92011-11-15 19:06:521345 NaClProcessHost::EarlyStartup();
1346#endif
1347
[email protected]97ef83f2011-12-19 19:26:361348 PreBrowserStart();
1349
[email protected]087453292011-11-22 00:43:121350 // Instantiate the notification UI manager, as this triggers a perf timer
1351 // used to measure startup time. TODO(stevenjb): Figure out what is actually
1352 // triggering the timer and call that explicitly in the approprate place.
1353 // http://crbug.com/105065.
[email protected]17e6cde52012-01-05 00:16:571354 browser_process_->notification_ui_manager();
[email protected]087453292011-11-22 00:43:121355
[email protected]81054f812012-08-30 00:47:091356#if !defined(OS_ANDROID)
[email protected]5b027af2012-06-21 18:32:531357 // Most general initialization is behind us, but opening a
1358 // tab and/or session restore and such is still to be done.
1359 base::TimeTicks browser_open_start = base::TimeTicks::Now();
[email protected]45d72762011-04-15 18:58:201360
[email protected]5b027af2012-06-21 18:32:531361 // We are in regular browser boot sequence. Open initial tabs and enter the
1362 // main message loop.
1363 int result_code;
[email protected]a64ad242012-01-18 15:17:361364#if defined(OS_CHROMEOS)
[email protected]5b027af2012-06-21 18:32:531365 // On ChromeOS multiple profiles doesn't apply, and will break if we load
1366 // them this early as the cryptohome hasn't yet been mounted (which happens
1367 // only once we log in.
1368 std::vector<Profile*> last_opened_profiles;
[email protected]a64ad242012-01-18 15:17:361369#else
[email protected]5b027af2012-06-21 18:32:531370 std::vector<Profile*> last_opened_profiles =
1371 g_browser_process->profile_manager()->GetLastOpenedProfiles();
[email protected]a64ad242012-01-18 15:17:361372#endif
[email protected]5b027af2012-06-21 18:32:531373 if (browser_creator_->Start(parsed_command_line(), FilePath(),
1374 profile_, last_opened_profiles, &result_code)) {
[email protected]c7480942011-11-08 19:18:271375#if defined(OS_WIN) || (defined(OS_LINUX) && !defined(OS_CHROMEOS))
[email protected]5b027af2012-06-21 18:32:531376 // Initialize autoupdate timer. Timer callback costs basically nothing
1377 // when browser is not in persistent mode, so it's OK to let it ride on
1378 // the main thread. This needs to be done here because we don't want
1379 // to start the timer when Chrome is run inside a test harness.
1380 browser_process_->StartAutoupdateTimer();
[email protected]bd48c2b02010-04-09 20:32:421381#endif
1382
[email protected]3cdacd42010-04-30 18:55:531383#if defined(OS_LINUX) && !defined(OS_CHROMEOS)
[email protected]5b027af2012-06-21 18:32:531384 // On Linux, the running exe will be updated if an upgrade becomes
1385 // available while the browser is running. We need to save the last
1386 // modified time of the exe, so we can compare to determine if there is
1387 // an upgrade while the browser is kept alive by a persistent extension.
1388 upgrade_util::SaveLastModifiedTimeOfExe();
[email protected]3cdacd42010-04-30 18:55:531389#endif
1390
[email protected]5b027af2012-06-21 18:32:531391 // Record now as the last successful chrome start.
1392 GoogleUpdateSettings::SetLastRunTime();
[email protected]badf5cf2011-10-29 03:44:441393
1394#if defined(OS_MACOSX)
[email protected]5b027af2012-06-21 18:32:531395 // Call Recycle() here as late as possible, before going into the loop
1396 // because Start() will add things to it while creating the main window.
1397 if (parameters().autorelease_pool)
1398 parameters().autorelease_pool->Recycle();
[email protected]badf5cf2011-10-29 03:44:441399#endif
[email protected]45d72762011-04-15 18:58:201400
[email protected]5b027af2012-06-21 18:32:531401 RecordPreReadExperimentTime("Startup.BrowserOpenTabs",
1402 base::TimeTicks::Now() - browser_open_start);
[email protected]45d72762011-04-15 18:58:201403
[email protected]5b027af2012-06-21 18:32:531404 // TODO(mad): Move this call in a proper place on CrOS.
1405 // http://crosbug.com/17687
[email protected]bafa00e42011-07-18 14:47:261406#if !defined(OS_CHROMEOS)
[email protected]5b027af2012-06-21 18:32:531407 // If we're running tests (ui_task is non-null), then we don't want to
1408 // call FetchLanguageListFromTranslateServer or
[email protected]88bac3f2012-07-22 21:22:571409 // StartRepeatedVariationsSeedFetch.
[email protected]5b027af2012-06-21 18:32:531410 if (parameters().ui_task == NULL) {
1411 // Request new variations seed information from server.
[email protected]88bac3f2012-07-22 21:22:571412 browser_process_->variations_service()->
1413 StartRepeatedVariationsSeedFetch();
[email protected]54e26c122012-05-17 15:36:211414
[email protected]5b027af2012-06-21 18:32:531415 if (translate_manager_ != NULL) {
1416 translate_manager_->FetchLanguageListFromTranslateServer(
1417 profile_->GetPrefs());
[email protected]63e26822011-07-16 19:07:351418 }
[email protected]5b027af2012-06-21 18:32:531419 }
[email protected]bafa00e42011-07-18 14:47:261420#endif
[email protected]fa436972011-06-23 05:58:011421
[email protected]5b027af2012-06-21 18:32:531422 run_message_loop_ = true;
1423 } else {
1424 run_message_loop_ = false;
1425 }
[email protected]fe7c4872012-05-10 20:06:031426 browser_creator_.reset();
[email protected]81054f812012-08-30 00:47:091427#endif // !defined(OS_ANDROID)
[email protected]97ef83f2011-12-19 19:26:361428
1429 PostBrowserStart();
1430
[email protected]886f1142012-05-03 20:00:161431 if (parameters().ui_task) {
[email protected]0c1c4752012-08-09 20:30:241432 // We end the startup timer here if we have parameters to run, because we
1433 // never start to run the main loop (where we normally stop the timer).
1434 startup_timer_->SignalStartupComplete(
1435 performance_monitor::StartupTimer::STARTUP_TEST);
[email protected]886f1142012-05-03 20:00:161436 parameters().ui_task->Run();
1437 delete parameters().ui_task;
1438 run_message_loop_ = false;
1439 }
1440
[email protected]50462bf02011-11-21 19:13:311441 return result_code_;
[email protected]c1702d712011-09-08 19:41:431442}
1443
[email protected]b48c9182011-10-26 18:03:301444bool ChromeBrowserMainParts::MainMessageLoopRun(int* result_code) {
[email protected]82d2c332012-08-07 22:46:311445#if defined(OS_ANDROID)
1446 // Chrome on Android does not use default MessageLoop. It has its own
1447 // Android specific MessageLoop
1448 NOTREACHED();
1449 return true;
1450#else
[email protected]c7480942011-11-08 19:18:271451 // Set the result code set in PreMainMessageLoopRun or set above.
[email protected]b48c9182011-10-26 18:03:301452 *result_code = result_code_;
[email protected]c1702d712011-09-08 19:41:431453 if (!run_message_loop_)
[email protected]b48c9182011-10-26 18:03:301454 return true; // Don't run the default message loop.
[email protected]c1702d712011-09-08 19:41:431455
[email protected]0c1c4752012-08-09 20:30:241456 // These should be invoked as close to the start of the browser's
[email protected]c1702d712011-09-08 19:41:431457 // UI thread message loop as possible to get a stable measurement
1458 // across versions.
1459 RecordBrowserStartupTime();
[email protected]0c1c4752012-08-09 20:30:241460 startup_timer_->SignalStartupComplete(
1461 performance_monitor::StartupTimer::STARTUP_NORMAL);
1462
[email protected]8e937c1e2012-06-28 22:57:301463 DCHECK_EQ(MessageLoop::TYPE_UI, MessageLoop::current()->type());
1464#if !defined(USE_AURA) && defined(TOOLKIT_VIEWS)
[email protected]c1702d712011-09-08 19:41:431465 views::AcceleratorHandler accelerator_handler;
[email protected]8e937c1e2012-06-28 22:57:301466 base::RunLoop run_loop(&accelerator_handler);
1467#else
1468 base::RunLoop run_loop;
[email protected]c1702d712011-09-08 19:41:431469#endif
[email protected]d378f192012-08-14 18:42:031470
1471 if (CommandLine::ForCurrentProcess()->HasSwitch(
1472 switches::kRunPerformanceMonitor)) {
1473 performance_monitor::PerformanceMonitor::GetInstance()->Start();
1474 }
1475
[email protected]8e937c1e2012-06-28 22:57:301476 run_loop.Run();
[email protected]b48c9182011-10-26 18:03:301477
1478 return true;
[email protected]82d2c332012-08-07 22:46:311479#endif
[email protected]c1702d712011-09-08 19:41:431480}
1481
1482void ChromeBrowserMainParts::PostMainMessageLoopRun() {
[email protected]82d2c332012-08-07 22:46:311483#if defined(OS_ANDROID)
1484 // Chrome on Android does not use default MessageLoop. It has its own
1485 // Android specific MessageLoop
1486 NOTREACHED();
1487#else
[email protected]6d823b42011-09-05 02:54:021488 // Start watching for jank during shutdown. It gets disarmed when
[email protected]f967b722011-09-07 00:58:041489 // |shutdown_watcher_| object is destructed.
[email protected]56bdb712012-01-27 00:35:591490 shutdown_watcher_->Arm(base::TimeDelta::FromSeconds(300));
[email protected]6d823b42011-09-05 02:54:021491
[email protected]b69941522011-10-08 03:17:371492 // Disarm the startup hang detector time bomb if it is still Arm'ed.
[email protected]08f1c5e2011-12-01 01:54:341493 startup_watcher_->Disarm();
[email protected]b69941522011-10-08 03:17:371494
[email protected]ef589af2011-12-03 01:07:151495 for (size_t i = 0; i < chrome_extra_parts_.size(); ++i)
1496 chrome_extra_parts_[i]->PostMainMessageLoopRun();
1497
[email protected]15406b6a2010-04-21 00:35:011498#if defined(OS_WIN)
[email protected]2381ed22012-01-26 04:05:291499 // Log the search engine chosen on first run. Do this at shutdown, after any
1500 // changes are made from the first run bubble link, etc.
[email protected]c1702d712011-09-08 19:41:431501 if (record_search_engine_) {
[email protected]8e5c89a2011-06-07 18:13:331502 TemplateURLService* url_service =
[email protected]c1702d712011-09-08 19:41:431503 TemplateURLServiceFactory::GetForProfile(profile_);
[email protected]96b3c5c2010-04-16 20:30:141504 const TemplateURL* default_search_engine =
[email protected]8e5c89a2011-06-07 18:13:331505 url_service->GetDefaultSearchProvider();
[email protected]32cba2b2010-10-19 06:19:481506 // The default engine can be NULL if the administrator has disabled
1507 // default search.
[email protected]23872902010-10-26 03:43:591508 SearchEngineType search_engine_type =
[email protected]360ba052012-04-04 17:26:131509 TemplateURLPrepopulateData::GetEngineType(default_search_engine ?
1510 default_search_engine->url() : std::string());
[email protected]bf7eb2f92010-06-28 22:55:551511 // Record the search engine chosen.
[email protected]b37bdfe2012-03-16 20:53:271512 UMA_HISTOGRAM_ENUMERATION("Chrome.SearchSelectExempt", search_engine_type,
[email protected]2381ed22012-01-26 04:05:291513 SEARCH_ENGINE_MAX);
[email protected]96b3c5c2010-04-16 20:30:141514 }
[email protected]15406b6a2010-04-21 00:35:011515#endif
[email protected]96b3c5c2010-04-16 20:30:141516
[email protected]a6e9dce22011-06-25 00:19:221517 // Some tests don't set parameters.ui_task, so they started translate
1518 // language fetch that was never completed so we need to cleanup here
1519 // otherwise it will be done by the destructor in a wrong thread.
[email protected]716476c2011-12-29 00:07:031520 if (parameters().ui_task == NULL && translate_manager_ != NULL)
[email protected]c1702d712011-09-08 19:41:431521 translate_manager_->CleanupPendingUlrFetcher();
[email protected]a6e9dce22011-06-25 00:19:221522
[email protected]66013922011-09-13 18:53:421523 if (notify_result_ == ProcessSingleton::PROCESS_NONE)
1524 process_singleton_->Cleanup();
[email protected]9f20a6d02009-08-21 01:18:371525
[email protected]0b565182011-03-02 18:11:151526 // Stop all tasks that might run on WatchDogThread.
1527 ThreadWatcherList::StopWatchingAll();
1528
[email protected]17e6cde52012-01-05 00:16:571529 browser_process_->metrics_service()->Stop();
initial.commit09911bf2008-07-26 23:55:291530
[email protected]2e5b60a22011-11-28 15:56:411531 restart_last_session_ = browser_shutdown::ShutdownPreThreadsStop();
1532 browser_process_->StartTearDown();
[email protected]82d2c332012-08-07 22:46:311533#endif
[email protected]2e5b60a22011-11-28 15:56:411534}
1535
[email protected]2e5b60a22011-11-28 15:56:411536void ChromeBrowserMainParts::PostDestroyThreads() {
[email protected]81054f812012-08-30 00:47:091537#if defined(OS_ANDROID)
1538 // On Android, there is no quit/exit. So the browser's main message loop will
1539 // not finish.
1540 NOTREACHED();
1541#else
[email protected]99907362012-01-11 05:41:401542 browser_process_->PostDestroyThreads();
initial.commit09911bf2008-07-26 23:55:291543 // browser_shutdown takes care of deleting browser_process, so we need to
1544 // release it.
[email protected]c1702d712011-09-08 19:41:431545 ignore_result(browser_process_.release());
[email protected]2e5b60a22011-11-28 15:56:411546 browser_shutdown::ShutdownPostThreadsStop(restart_last_session_);
[email protected]c1702d712011-09-08 19:41:431547 master_prefs_.reset();
1548 process_singleton_.reset();
[email protected]c0c55e92011-09-10 18:47:301549
1550 // We need to do this check as late as possible, but due to modularity, this
1551 // may be the last point in Chrome. This would be more effective if done at
1552 // a higher level on the stack, so that it is impossible for an early return
1553 // to bypass this code. Perhaps we need a *final* hook that is called on all
1554 // paths from content/browser/browser_main.
1555 CHECK(MetricsService::UmaMetricsProperlyShutdown());
[email protected]81054f812012-08-30 00:47:091556#endif
[email protected]c1702d712011-09-08 19:41:431557}
initial.commit09911bf2008-07-26 23:55:291558
[email protected]50462bf02011-11-21 19:13:311559// Public members:
1560
1561void ChromeBrowserMainParts::AddParts(ChromeBrowserMainExtraParts* parts) {
1562 chrome_extra_parts_.push_back(parts);
1563}
1564
1565// Misc ------------------------------------------------------------------------
1566
[email protected]843bc462012-06-19 17:43:311567void RecordBrowserStartupTime() {
1568// CurrentProcessInfo::CreationTime() is currently only implemented on Mac and
1569// Windows.
1570#if defined(OS_MACOSX) || defined(OS_WIN)
1571 const base::Time *process_creation_time =
1572 base::CurrentProcessInfo::CreationTime();
1573
1574 if (process_creation_time)
1575 RecordPreReadExperimentTime("Startup.BrowserMessageLoopStartTime",
1576 base::Time::Now() - *process_creation_time);
1577#endif // OS_MACOSX || OS_WIN
1578}
1579
[email protected]28f576f2011-08-26 20:46:551580// This code is specific to the Windows-only PreReadExperiment field-trial.
1581void RecordPreReadExperimentTime(const char* name, base::TimeDelta time) {
1582 DCHECK(name != NULL);
1583
1584 // This gets called with different histogram names, so we don't want to use
1585 // the UMA_HISTOGRAM_CUSTOM_TIMES macro--it uses a static variable, and the
1586 // first call wins.
1587 AddPreReadHistogramTime(name, time);
1588
1589#if defined(OS_WIN)
[email protected]5bca9ee2011-09-03 00:18:421590#if defined(GOOGLE_CHROME_BUILD)
1591 // The pre-read experiment is Windows and Google Chrome specific.
[email protected]28f576f2011-08-26 20:46:551592 scoped_ptr<base::Environment> env(base::Environment::Create());
1593
1594 // Only record the sub-histogram result if the experiment is running
1595 // (environment variable is set, and valid).
[email protected]48078e52012-02-09 00:02:261596 std::string pre_read_percentage;
1597 if (env->GetVar(chrome::kPreReadEnvironmentVariable, &pre_read_percentage)) {
[email protected]28f576f2011-08-26 20:46:551598 std::string uma_name(name);
[email protected]ff5ce2372011-12-21 02:00:591599
1600 // We want XP to record a separate histogram, as the loader on XP
1601 // is very different from the Vista and Win7 loaders.
1602 if (base::win::GetVersion() <= base::win::VERSION_XP)
1603 uma_name += "_XP";
1604
[email protected]48078e52012-02-09 00:02:261605 uma_name += "_PreRead_";
1606 uma_name += pre_read_percentage;
[email protected]28f576f2011-08-26 20:46:551607 AddPreReadHistogramTime(uma_name.c_str(), time);
1608 }
1609#endif
[email protected]5bca9ee2011-09-03 00:18:421610#endif
[email protected]28f576f2011-08-26 20:46:551611}