OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/after_startup_task_utils.h" | 5 #include "chrome/browser/after_startup_task_utils.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/lazy_instance.h" | 10 #include "base/lazy_instance.h" |
11 #include "base/macros.h" | 11 #include "base/macros.h" |
12 #include "base/memory/ptr_util.h" | 12 #include "base/memory/ptr_util.h" |
13 #include "base/metrics/histogram_macros.h" | 13 #include "base/metrics/histogram_macros.h" |
14 #include "base/process/process_info.h" | 14 #include "base/process/process_info.h" |
15 #include "base/rand_util.h" | 15 #include "base/rand_util.h" |
16 #include "base/synchronization/cancellation_flag.h" | 16 #include "base/synchronization/atomic_flag.h" |
17 #include "base/task_runner.h" | 17 #include "base/task_runner.h" |
18 #include "base/tracked_objects.h" | 18 #include "base/tracked_objects.h" |
19 #include "build/build_config.h" | 19 #include "build/build_config.h" |
20 #include "chrome/browser/ui/browser.h" | 20 #include "chrome/browser/ui/browser.h" |
21 #include "chrome/browser/ui/browser_list.h" | 21 #include "chrome/browser/ui/browser_list.h" |
22 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 22 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
23 #include "content/public/browser/browser_thread.h" | 23 #include "content/public/browser/browser_thread.h" |
24 #include "content/public/browser/render_frame_host.h" | 24 #include "content/public/browser/render_frame_host.h" |
25 #include "content/public/browser/web_contents.h" | 25 #include "content/public/browser/web_contents.h" |
26 #include "content/public/browser/web_contents_observer.h" | 26 #include "content/public/browser/web_contents_observer.h" |
27 | 27 |
28 using content::BrowserThread; | 28 using content::BrowserThread; |
29 using content::WebContents; | 29 using content::WebContents; |
30 using content::WebContentsObserver; | 30 using content::WebContentsObserver; |
31 using StartupCompleteFlag = base::CancellationFlag; | |
32 | 31 |
33 namespace { | 32 namespace { |
34 | 33 |
35 struct AfterStartupTask { | 34 struct AfterStartupTask { |
36 AfterStartupTask(const tracked_objects::Location& from_here, | 35 AfterStartupTask(const tracked_objects::Location& from_here, |
37 const scoped_refptr<base::TaskRunner>& task_runner, | 36 const scoped_refptr<base::TaskRunner>& task_runner, |
38 const base::Closure& task) | 37 const base::Closure& task) |
39 : from_here(from_here), task_runner(task_runner), task(task) {} | 38 : from_here(from_here), task_runner(task_runner), task(task) {} |
40 ~AfterStartupTask() {} | 39 ~AfterStartupTask() {} |
41 | 40 |
42 const tracked_objects::Location from_here; | 41 const tracked_objects::Location from_here; |
43 const scoped_refptr<base::TaskRunner> task_runner; | 42 const scoped_refptr<base::TaskRunner> task_runner; |
44 const base::Closure task; | 43 const base::Closure task; |
45 }; | 44 }; |
46 | 45 |
47 // The flag may be read on any thread, but must only be set on the UI thread. | 46 // The flag may be read on any thread, but must only be set on the UI thread. |
48 base::LazyInstance<StartupCompleteFlag>::Leaky g_startup_complete_flag; | 47 base::LazyInstance<base::AtomicFlag>::Leaky g_startup_complete_flag; |
49 | 48 |
50 // The queue may only be accessed on the UI thread. | 49 // The queue may only be accessed on the UI thread. |
51 base::LazyInstance<std::deque<AfterStartupTask*>>::Leaky g_after_startup_tasks; | 50 base::LazyInstance<std::deque<AfterStartupTask*>>::Leaky g_after_startup_tasks; |
52 | 51 |
53 bool IsBrowserStartupComplete() { | 52 bool IsBrowserStartupComplete() { |
54 // Be sure to initialize the LazyInstance on the main thread since the flag | 53 // Be sure to initialize the LazyInstance on the main thread since the flag |
55 // may only be set on it's initializing thread. | 54 // may only be set on it's initializing thread. |
56 if (g_startup_complete_flag == nullptr) | 55 if (g_startup_complete_flag == nullptr) |
57 return false; | 56 return false; |
58 return g_startup_complete_flag.Get().IsSet(); | 57 return g_startup_complete_flag.Get().IsSet(); |
(...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
224 return ::IsBrowserStartupComplete(); | 223 return ::IsBrowserStartupComplete(); |
225 } | 224 } |
226 | 225 |
227 void AfterStartupTaskUtils::UnsafeResetForTesting() { | 226 void AfterStartupTaskUtils::UnsafeResetForTesting() { |
228 DCHECK(g_after_startup_tasks.Get().empty()); | 227 DCHECK(g_after_startup_tasks.Get().empty()); |
229 if (!IsBrowserStartupComplete()) | 228 if (!IsBrowserStartupComplete()) |
230 return; | 229 return; |
231 g_startup_complete_flag.Get().UnsafeResetForTesting(); | 230 g_startup_complete_flag.Get().UnsafeResetForTesting(); |
232 DCHECK(!IsBrowserStartupComplete()); | 231 DCHECK(!IsBrowserStartupComplete()); |
233 } | 232 } |
OLD | NEW |