Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(301)

Side by Side Diff: chrome/browser/chrome_browser_main.cc

Issue 949293002: Implement a poor man's PostAfterStartupTask() function. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/chrome_browser_main.h" 5 #include "chrome/browser/chrome_browser_main.h"
6 6
7 #include <set> 7 #include <set>
8 #include <string> 8 #include <string>
9 #include <vector> 9 #include <vector>
10 10
(...skipping 13 matching lines...) Expand all
24 #include "base/prefs/pref_value_store.h" 24 #include "base/prefs/pref_value_store.h"
25 #include "base/prefs/scoped_user_pref_update.h" 25 #include "base/prefs/scoped_user_pref_update.h"
26 #include "base/process/process_info.h" 26 #include "base/process/process_info.h"
27 #include "base/profiler/scoped_tracker.h" 27 #include "base/profiler/scoped_tracker.h"
28 #include "base/run_loop.h" 28 #include "base/run_loop.h"
29 #include "base/strings/string_number_conversions.h" 29 #include "base/strings/string_number_conversions.h"
30 #include "base/strings/string_piece.h" 30 #include "base/strings/string_piece.h"
31 #include "base/strings/string_split.h" 31 #include "base/strings/string_split.h"
32 #include "base/strings/sys_string_conversions.h" 32 #include "base/strings/sys_string_conversions.h"
33 #include "base/strings/utf_string_conversions.h" 33 #include "base/strings/utf_string_conversions.h"
34 #include "base/synchronization/cancellation_flag.h"
34 #include "base/sys_info.h" 35 #include "base/sys_info.h"
35 #include "base/threading/platform_thread.h" 36 #include "base/threading/platform_thread.h"
36 #include "base/time/time.h" 37 #include "base/time/time.h"
37 #include "base/trace_event/trace_event.h" 38 #include "base/trace_event/trace_event.h"
38 #include "base/values.h" 39 #include "base/values.h"
39 #include "build/build_config.h" 40 #include "build/build_config.h"
40 #include "cc/base/switches.h" 41 #include "cc/base/switches.h"
41 #include "chrome/browser/about_flags.h" 42 #include "chrome/browser/about_flags.h"
42 #include "chrome/browser/browser_process.h" 43 #include "chrome/browser/browser_process.h"
43 #include "chrome/browser/browser_process_impl.h" 44 #include "chrome/browser/browser_process_impl.h"
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
78 #include "chrome/browser/printing/cloud_print/cloud_print_proxy_service_factory. h" 79 #include "chrome/browser/printing/cloud_print/cloud_print_proxy_service_factory. h"
79 #include "chrome/browser/process_singleton.h" 80 #include "chrome/browser/process_singleton.h"
80 #include "chrome/browser/profiles/profile.h" 81 #include "chrome/browser/profiles/profile.h"
81 #include "chrome/browser/profiles/profile_manager.h" 82 #include "chrome/browser/profiles/profile_manager.h"
82 #include "chrome/browser/profiles/profiles_state.h" 83 #include "chrome/browser/profiles/profiles_state.h"
83 #include "chrome/browser/shell_integration.h" 84 #include "chrome/browser/shell_integration.h"
84 #include "chrome/browser/translate/translate_service.h" 85 #include "chrome/browser/translate/translate_service.h"
85 #include "chrome/browser/ui/app_list/app_list_service.h" 86 #include "chrome/browser/ui/app_list/app_list_service.h"
86 #include "chrome/browser/ui/browser.h" 87 #include "chrome/browser/ui/browser.h"
87 #include "chrome/browser/ui/browser_finder.h" 88 #include "chrome/browser/ui/browser_finder.h"
89 #include "chrome/browser/ui/browser_iterator.h"
88 #include "chrome/browser/ui/host_desktop.h" 90 #include "chrome/browser/ui/host_desktop.h"
89 #include "chrome/browser/ui/startup/bad_flags_prompt.h" 91 #include "chrome/browser/ui/startup/bad_flags_prompt.h"
90 #include "chrome/browser/ui/startup/default_browser_prompt.h" 92 #include "chrome/browser/ui/startup/default_browser_prompt.h"
91 #include "chrome/browser/ui/startup/startup_browser_creator.h" 93 #include "chrome/browser/ui/startup/startup_browser_creator.h"
94 #include "chrome/browser/ui/tabs/tab_strip_model.h"
92 #include "chrome/browser/ui/uma_browsing_activity_observer.h" 95 #include "chrome/browser/ui/uma_browsing_activity_observer.h"
93 #include "chrome/browser/ui/webui/chrome_web_ui_controller_factory.h" 96 #include "chrome/browser/ui/webui/chrome_web_ui_controller_factory.h"
94 #include "chrome/common/chrome_constants.h" 97 #include "chrome/common/chrome_constants.h"
95 #include "chrome/common/chrome_paths.h" 98 #include "chrome/common/chrome_paths.h"
96 #include "chrome/common/chrome_result_codes.h" 99 #include "chrome/common/chrome_result_codes.h"
97 #include "chrome/common/chrome_switches.h" 100 #include "chrome/common/chrome_switches.h"
98 #include "chrome/common/chrome_version_info.h" 101 #include "chrome/common/chrome_version_info.h"
99 #include "chrome/common/crash_keys.h" 102 #include "chrome/common/crash_keys.h"
100 #include "chrome/common/env_vars.h" 103 #include "chrome/common/env_vars.h"
101 #include "chrome/common/logging_chrome.h" 104 #include "chrome/common/logging_chrome.h"
(...skipping 15 matching lines...) Expand all
117 #include "components/translate/content/browser/browser_cld_utils.h" 120 #include "components/translate/content/browser/browser_cld_utils.h"
118 #include "components/translate/content/common/cld_data_source.h" 121 #include "components/translate/content/common/cld_data_source.h"
119 #include "components/translate/core/browser/translate_download_manager.h" 122 #include "components/translate/core/browser/translate_download_manager.h"
120 #include "components/variations/net/variations_http_header_provider.h" 123 #include "components/variations/net/variations_http_header_provider.h"
121 #include "content/public/browser/browser_thread.h" 124 #include "content/public/browser/browser_thread.h"
122 #include "content/public/browser/notification_observer.h" 125 #include "content/public/browser/notification_observer.h"
123 #include "content/public/browser/notification_registrar.h" 126 #include "content/public/browser/notification_registrar.h"
124 #include "content/public/browser/notification_service.h" 127 #include "content/public/browser/notification_service.h"
125 #include "content/public/browser/notification_types.h" 128 #include "content/public/browser/notification_types.h"
126 #include "content/public/browser/power_usage_monitor.h" 129 #include "content/public/browser/power_usage_monitor.h"
130 #include "content/public/browser/render_frame_host.h"
127 #include "content/public/browser/site_instance.h" 131 #include "content/public/browser/site_instance.h"
128 #include "content/public/common/content_client.h" 132 #include "content/public/common/content_client.h"
129 #include "content/public/common/content_switches.h" 133 #include "content/public/common/content_switches.h"
130 #include "content/public/common/main_function_params.h" 134 #include "content/public/common/main_function_params.h"
131 #include "grit/platform_locale_settings.h" 135 #include "grit/platform_locale_settings.h"
132 #include "net/base/net_module.h" 136 #include "net/base/net_module.h"
133 #include "net/cookies/cookie_monster.h" 137 #include "net/cookies/cookie_monster.h"
134 #include "net/http/http_network_layer.h" 138 #include "net/http/http_network_layer.h"
135 #include "net/http/http_stream_factory.h" 139 #include "net/http/http_stream_factory.h"
136 #include "net/url_request/url_request.h" 140 #include "net/url_request/url_request.h"
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
214 218
215 #if defined(ENABLE_WEBRTC) 219 #if defined(ENABLE_WEBRTC)
216 #include "chrome/browser/media/webrtc_log_util.h" 220 #include "chrome/browser/media/webrtc_log_util.h"
217 #endif // defined(ENABLE_WEBRTC) 221 #endif // defined(ENABLE_WEBRTC)
218 222
219 #if defined(USE_AURA) 223 #if defined(USE_AURA)
220 #include "ui/aura/env.h" 224 #include "ui/aura/env.h"
221 #endif // defined(USE_AURA) 225 #endif // defined(USE_AURA)
222 226
223 using content::BrowserThread; 227 using content::BrowserThread;
228 using content::WebContents;
229 using content::WebContentsObserver;
224 230
225 namespace { 231 namespace {
226 232
233 // The basis for when tasks posted via PostAfterStartupTasks begin running.
234 using StartupCompleteFlag = base::CancellationFlag;
235 base::LazyInstance<StartupCompleteFlag>::Leaky g_startup_complete_flag;
236
237 void SetBrowserStartupIsComplete() {
238 g_startup_complete_flag.Get().Set();
jam 2015/03/26 15:38:58 what I had meant earlier (sorry if not clear), is
michaeln 2015/03/26 19:52:33 I've been trying to avoid providing an API to list
jam 2015/03/26 20:22:08 Why doesn't CCBC forward the tasks it gets to a fi
michaeln 2015/03/26 21:39:36 I guess I was trying to avoid the addition of a ne
239 }
240
241 // Observes the first page load and set the startup complete flag accordingly.
242 class StartupObserver : public WebContentsObserver {
243 public:
244 StartupObserver() : weak_factory_(this) {}
245 ~StartupObserver() override { DCHECK(IsBrowserStartupComplete()); }
246
247 void Start();
248
249 private:
250 void OnStartupComplete() {
251 SetBrowserStartupIsComplete();
252 delete this;
253 }
254
255 void OnFailsafeTimeout() { OnStartupComplete(); }
256
257 // WebContentsObserver overrides
258 void DidFinishLoad(content::RenderFrameHost* render_frame_host,
259 const GURL& validated_url) override {
260 if (!render_frame_host->GetParent())
261 OnStartupComplete();
262 }
263
264 void DidFailLoad(content::RenderFrameHost* render_frame_host,
265 const GURL& validated_url,
266 int error_code,
267 const base::string16& error_description) override {
268 if (!render_frame_host->GetParent())
269 OnStartupComplete();
270 }
271
272 void WebContentsDestroyed() override { OnStartupComplete(); }
273
274 base::WeakPtrFactory<StartupObserver> weak_factory_;
275
276 DISALLOW_COPY_AND_ASSIGN(StartupObserver);
277 };
278
279 void StartupObserver::Start() {
280 // Signal completion quickly when there is no first page to load.
281 const int kShortDelaySecs = 3;
282 base::TimeDelta delay = base::TimeDelta::FromSeconds(kShortDelaySecs);
283
284 #if !defined(OS_ANDROID)
285 WebContents* contents = nullptr;
286 for (chrome::BrowserIterator iter; !iter.done(); iter.Next()) {
287 contents = (*iter)->tab_strip_model()->GetActiveWebContents();
288 if (contents)
289 break;
290 }
291
292 if (contents) {
293 // Give the page time to finish loading.
294 const int kLongerDelayMins = 3;
295 Observe(contents);
296 delay = base::TimeDelta::FromMinutes(kLongerDelayMins);
297 }
298 #endif // !defined(OS_ANDROID)
299
300 BrowserThread::PostDelayedTask(
301 BrowserThread::UI, FROM_HERE,
302 base::Bind(&StartupObserver::OnFailsafeTimeout,
303 weak_factory_.GetWeakPtr()),
304 delay);
305 }
306
227 // This function provides some ways to test crash and assertion handling 307 // This function provides some ways to test crash and assertion handling
228 // behavior of the program. 308 // behavior of the program.
229 void HandleTestParameters(const base::CommandLine& command_line) { 309 void HandleTestParameters(const base::CommandLine& command_line) {
230 // This parameter causes a null pointer crash (crash reporter trigger). 310 // This parameter causes a null pointer crash (crash reporter trigger).
231 if (command_line.HasSwitch(switches::kBrowserCrashTest)) { 311 if (command_line.HasSwitch(switches::kBrowserCrashTest)) {
232 int* bad_pointer = NULL; 312 int* bad_pointer = NULL;
233 *bad_pointer = 0; 313 *bad_pointer = 0;
234 } 314 }
235 } 315 }
236 316
(...skipping 346 matching lines...) Expand 10 before | Expand all | Expand 10 after
583 local_state_(NULL), 663 local_state_(NULL),
584 restart_last_session_(false) { 664 restart_last_session_(false) {
585 // If we're running tests (ui_task is non-null). 665 // If we're running tests (ui_task is non-null).
586 if (parameters.ui_task) 666 if (parameters.ui_task)
587 browser_defaults::enable_help_app = false; 667 browser_defaults::enable_help_app = false;
588 668
589 // Chrome disallows cookies by default. All code paths that want to use 669 // Chrome disallows cookies by default. All code paths that want to use
590 // cookies need to go through one of Chrome's URLRequestContexts which have 670 // cookies need to go through one of Chrome's URLRequestContexts which have
591 // a ChromeNetworkDelegate attached that selectively allows cookies again. 671 // a ChromeNetworkDelegate attached that selectively allows cookies again.
592 net::URLRequest::SetDefaultCookiePolicyToBlock(); 672 net::URLRequest::SetDefaultCookiePolicyToBlock();
673
674 // Be sure to allocate the lazy instance on the main thread.
675 g_startup_complete_flag.Get();
593 } 676 }
594 677
595 ChromeBrowserMainParts::~ChromeBrowserMainParts() { 678 ChromeBrowserMainParts::~ChromeBrowserMainParts() {
596 for (int i = static_cast<int>(chrome_extra_parts_.size())-1; i >= 0; --i) 679 for (int i = static_cast<int>(chrome_extra_parts_.size())-1; i >= 0; --i)
597 delete chrome_extra_parts_[i]; 680 delete chrome_extra_parts_[i];
598 chrome_extra_parts_.clear(); 681 chrome_extra_parts_.clear();
599 } 682 }
600 683
601 // This will be called after the command-line has been mutated by about:flags 684 // This will be called after the command-line has been mutated by about:flags
602 void ChromeBrowserMainParts::SetupMetricsAndFieldTrials() { 685 void ChromeBrowserMainParts::SetupMetricsAndFieldTrials() {
(...skipping 501 matching lines...) Expand 10 before | Expand all | Expand 10 after
1104 #endif // !defined(OS_ANDROID) 1187 #endif // !defined(OS_ANDROID)
1105 #if defined(ENABLE_WEBRTC) 1188 #if defined(ENABLE_WEBRTC)
1106 // Set up a task to delete old WebRTC log files for all profiles. Use a delay 1189 // Set up a task to delete old WebRTC log files for all profiles. Use a delay
1107 // to reduce the impact on startup time. 1190 // to reduce the impact on startup time.
1108 BrowserThread::PostDelayedTask( 1191 BrowserThread::PostDelayedTask(
1109 BrowserThread::UI, 1192 BrowserThread::UI,
1110 FROM_HERE, 1193 FROM_HERE,
1111 base::Bind(&WebRtcLogUtil::DeleteOldWebRtcLogFilesForAllProfiles), 1194 base::Bind(&WebRtcLogUtil::DeleteOldWebRtcLogFilesForAllProfiles),
1112 base::TimeDelta::FromMinutes(1)); 1195 base::TimeDelta::FromMinutes(1));
1113 #endif // defined(ENABLE_WEBRTC) 1196 #endif // defined(ENABLE_WEBRTC)
1197
1198 // The observer is self-deleting.
1199 (new StartupObserver)->Start();
1114 } 1200 }
1115 1201
1116 int ChromeBrowserMainParts::PreMainMessageLoopRunImpl() { 1202 int ChromeBrowserMainParts::PreMainMessageLoopRunImpl() {
1117 TRACE_EVENT0("startup", "ChromeBrowserMainParts::PreMainMessageLoopRunImpl"); 1203 TRACE_EVENT0("startup", "ChromeBrowserMainParts::PreMainMessageLoopRunImpl");
1118 SCOPED_UMA_HISTOGRAM_LONG_TIMER("Startup.PreMainMessageLoopRunImplLongTime"); 1204 SCOPED_UMA_HISTOGRAM_LONG_TIMER("Startup.PreMainMessageLoopRunImplLongTime");
1119 const base::TimeTicks start_time_step1 = base::TimeTicks::Now(); 1205 const base::TimeTicks start_time_step1 = base::TimeTicks::Now();
1120 // Android updates the metrics service dynamically depending on whether the 1206 // Android updates the metrics service dynamically depending on whether the
1121 // application is in the foreground or not. Do not start here. 1207 // application is in the foreground or not. Do not start here.
1122 #if !defined(OS_ANDROID) 1208 #if !defined(OS_ANDROID)
1123 // Now that the file thread has been started, start recording. 1209 // Now that the file thread has been started, start recording.
(...skipping 606 matching lines...) Expand 10 before | Expand all | Expand 10 after
1730 chromeos::CrosSettings::Shutdown(); 1816 chromeos::CrosSettings::Shutdown();
1731 #endif // defined(OS_CHROMEOS) 1817 #endif // defined(OS_CHROMEOS)
1732 #endif // defined(OS_ANDROID) 1818 #endif // defined(OS_ANDROID)
1733 } 1819 }
1734 1820
1735 // Public members: 1821 // Public members:
1736 1822
1737 void ChromeBrowserMainParts::AddParts(ChromeBrowserMainExtraParts* parts) { 1823 void ChromeBrowserMainParts::AddParts(ChromeBrowserMainExtraParts* parts) {
1738 chrome_extra_parts_.push_back(parts); 1824 chrome_extra_parts_.push_back(parts);
1739 } 1825 }
1826
1827 bool IsBrowserStartupComplete() {
1828 return g_startup_complete_flag.Get().IsSet();
1829 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698