| 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/sequence_checker.h" |
| 16 #include "base/synchronization/atomic_flag.h" | 17 #include "base/synchronization/atomic_flag.h" |
| 17 #include "base/task_runner.h" | 18 #include "base/task_runner.h" |
| 18 #include "base/tracked_objects.h" | 19 #include "base/tracked_objects.h" |
| 19 #include "build/build_config.h" | 20 #include "build/build_config.h" |
| 20 #include "chrome/browser/ui/browser.h" | 21 #include "chrome/browser/ui/browser.h" |
| 21 #include "chrome/browser/ui/browser_list.h" | 22 #include "chrome/browser/ui/browser_list.h" |
| 22 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 23 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 23 #include "content/public/browser/browser_thread.h" | 24 #include "content/public/browser/browser_thread.h" |
| 24 #include "content/public/browser/render_frame_host.h" | 25 #include "content/public/browser/render_frame_host.h" |
| 25 #include "content/public/browser/web_contents.h" | 26 #include "content/public/browser/web_contents.h" |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 115 ScheduleTask(base::WrapUnique(queued_task)); | 116 ScheduleTask(base::WrapUnique(queued_task)); |
| 116 g_after_startup_tasks.Get().clear(); | 117 g_after_startup_tasks.Get().clear(); |
| 117 | 118 |
| 118 // The shrink_to_fit() method is not available for all of our build targets. | 119 // The shrink_to_fit() method is not available for all of our build targets. |
| 119 std::deque<AfterStartupTask*>(g_after_startup_tasks.Get()) | 120 std::deque<AfterStartupTask*>(g_after_startup_tasks.Get()) |
| 120 .swap(g_after_startup_tasks.Get()); | 121 .swap(g_after_startup_tasks.Get()); |
| 121 } | 122 } |
| 122 | 123 |
| 123 // Observes the first visible page load and sets the startup complete | 124 // Observes the first visible page load and sets the startup complete |
| 124 // flag accordingly. | 125 // flag accordingly. |
| 125 class StartupObserver : public WebContentsObserver, public base::NonThreadSafe { | 126 class StartupObserver : public WebContentsObserver { |
| 126 public: | 127 public: |
| 127 StartupObserver() : weak_factory_(this) {} | 128 StartupObserver() : weak_factory_(this) {} |
| 128 ~StartupObserver() override { DCHECK(IsBrowserStartupComplete()); } | 129 ~StartupObserver() override { |
| 130 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 131 DCHECK(IsBrowserStartupComplete()); |
| 132 } |
| 129 | 133 |
| 130 void Start(); | 134 void Start(); |
| 131 | 135 |
| 132 private: | 136 private: |
| 133 void OnStartupComplete() { | 137 void OnStartupComplete() { |
| 134 DCHECK(CalledOnValidThread()); | 138 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 135 SetBrowserStartupIsComplete(); | 139 SetBrowserStartupIsComplete(); |
| 136 delete this; | 140 delete this; |
| 137 } | 141 } |
| 138 | 142 |
| 139 void OnFailsafeTimeout() { OnStartupComplete(); } | 143 void OnFailsafeTimeout() { OnStartupComplete(); } |
| 140 | 144 |
| 141 // WebContentsObserver overrides | 145 // WebContentsObserver overrides |
| 142 void DidFinishLoad(content::RenderFrameHost* render_frame_host, | 146 void DidFinishLoad(content::RenderFrameHost* render_frame_host, |
| 143 const GURL& validated_url) override { | 147 const GURL& validated_url) override { |
| 144 if (!render_frame_host->GetParent()) | 148 if (!render_frame_host->GetParent()) |
| 145 OnStartupComplete(); | 149 OnStartupComplete(); |
| 146 } | 150 } |
| 147 | 151 |
| 148 void DidFailLoad(content::RenderFrameHost* render_frame_host, | 152 void DidFailLoad(content::RenderFrameHost* render_frame_host, |
| 149 const GURL& validated_url, | 153 const GURL& validated_url, |
| 150 int error_code, | 154 int error_code, |
| 151 const base::string16& error_description, | 155 const base::string16& error_description, |
| 152 bool was_ignored_by_handler) override { | 156 bool was_ignored_by_handler) override { |
| 153 if (!render_frame_host->GetParent()) | 157 if (!render_frame_host->GetParent()) |
| 154 OnStartupComplete(); | 158 OnStartupComplete(); |
| 155 } | 159 } |
| 156 | 160 |
| 157 void WebContentsDestroyed() override { OnStartupComplete(); } | 161 void WebContentsDestroyed() override { OnStartupComplete(); } |
| 158 | 162 |
| 163 SEQUENCE_CHECKER(sequence_checker_); |
| 164 |
| 159 base::WeakPtrFactory<StartupObserver> weak_factory_; | 165 base::WeakPtrFactory<StartupObserver> weak_factory_; |
| 160 | 166 |
| 161 DISALLOW_COPY_AND_ASSIGN(StartupObserver); | 167 DISALLOW_COPY_AND_ASSIGN(StartupObserver); |
| 162 }; | 168 }; |
| 163 | 169 |
| 164 void StartupObserver::Start() { | 170 void StartupObserver::Start() { |
| 165 // Signal completion quickly when there is no first page to load. | 171 // Signal completion quickly when there is no first page to load. |
| 166 const int kShortDelaySecs = 3; | 172 const int kShortDelaySecs = 3; |
| 167 base::TimeDelta delay = base::TimeDelta::FromSeconds(kShortDelaySecs); | 173 base::TimeDelta delay = base::TimeDelta::FromSeconds(kShortDelaySecs); |
| 168 | 174 |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 252 return ::IsBrowserStartupComplete(); | 258 return ::IsBrowserStartupComplete(); |
| 253 } | 259 } |
| 254 | 260 |
| 255 void AfterStartupTaskUtils::UnsafeResetForTesting() { | 261 void AfterStartupTaskUtils::UnsafeResetForTesting() { |
| 256 DCHECK(g_after_startup_tasks.Get().empty()); | 262 DCHECK(g_after_startup_tasks.Get().empty()); |
| 257 if (!IsBrowserStartupComplete()) | 263 if (!IsBrowserStartupComplete()) |
| 258 return; | 264 return; |
| 259 g_startup_complete_flag.Get().UnsafeResetForTesting(); | 265 g_startup_complete_flag.Get().UnsafeResetForTesting(); |
| 260 DCHECK(!IsBrowserStartupComplete()); | 266 DCHECK(!IsBrowserStartupComplete()); |
| 261 } | 267 } |
| OLD | NEW |