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

Side by Side Diff: chrome/browser/after_startup_task.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, 8 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
(Empty)
1 // Copyright (c) 2015 The Chromium Authors. All rights reserved.
cmumford 2015/03/27 21:59:58 Nit: "(c)"
michaeln 2015/04/03 19:51:37 Done.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "chrome/browser/after_startup_task.h"
6
7 #include "base/lazy_instance.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/metrics/histogram_macros.h"
10 #include "base/process/process_info.h"
11 #include "base/rand_util.h"
12 #include "base/synchronization/cancellation_flag.h"
13 #include "base/task_runner.h"
14 #include "base/tracked_objects.h"
15 #include "chrome/browser/ui/browser.h"
16 #include "chrome/browser/ui/browser_iterator.h"
17 #include "chrome/browser/ui/tabs/tab_strip_model.h"
18 #include "content/public/browser/browser_thread.h"
19 #include "content/public/browser/render_frame_host.h"
20 #include "content/public/browser/web_contents.h"
21 #include "content/public/browser/web_contents_observer.h"
22
23 using content::BrowserThread;
24 using content::WebContents;
25 using content::WebContentsObserver;
26 using StartupCompleteFlag = base::CancellationFlag;
27
28 namespace {
29
30 struct AfterStartupTask {
31 AfterStartupTask(
32 const tracked_objects::Location& from_here,
33 const scoped_refptr<base::TaskRunner>& task_runner,
34 const base::Closure& task)
35 : from_here(from_here), task_runner(task_runner), task(task) {}
36 ~AfterStartupTask() {}
37
38 const tracked_objects::Location from_here;
39 const scoped_refptr<base::TaskRunner> task_runner;
40 const base::Closure task;
41 };
42
43 class StartupObserver;
44 void SetBrowserStartupIsComplete();
45 bool IsBrowserStartupComplete();
46 void QueueTask(scoped_ptr<AfterStartupTask> queued_task);
47 void ScheduleTask(scoped_ptr<AfterStartupTask> queued_task);
48 void RunTask(scoped_ptr<AfterStartupTask> queued_task);
49
50 base::LazyInstance<StartupCompleteFlag>::Leaky g_startup_complete_flag;
51 base::LazyInstance<std::deque<AfterStartupTask*>>::Leaky g_after_startup_tasks;
52
53 void SetBrowserStartupIsComplete() {
54 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
55 #if defined(OS_MACOSX) || defined(OS_WIN) || defined(OS_LINUX)
56 // CurrentProcessInfo::CreationTime() is not available on all platforms.
57 const base::Time process_creation_time =
58 base::CurrentProcessInfo::CreationTime();
59 if (!process_creation_time.is_null()) {
60 UMA_HISTOGRAM_LONG_TIMES(
61 "AfterStartupTasks.RunTime", base::Time::Now() - process_creation_time);
62 }
63 #endif // defined(OS_MACOSX) || defined(OS_WIN) || defined(OS_LINUX)
64 UMA_HISTOGRAM_COUNTS_100(
65 "AfterStartupTasks.Count", g_after_startup_tasks.Get().size());
66 g_startup_complete_flag.Get().Set();
67 for (AfterStartupTask* queued_task : g_after_startup_tasks.Get())
68 ScheduleTask(make_scoped_ptr(queued_task));
69 g_after_startup_tasks.Get().clear();
70 g_after_startup_tasks.Get().shrink_to_fit();
michaeln 2015/03/27 21:35:42 bummer, shrink_to_fit() isn't implemented everywhe
71 }
72
73 bool IsBrowserStartupComplete() {
74 // Be sure to allocate the flag on the main thread.
75 if (g_startup_complete_flag == nullptr)
76 return false;
77 return g_startup_complete_flag.Get().IsSet();
78 }
79
80 void QueueTask(scoped_ptr<AfterStartupTask> queued_task) {
81 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) {
82 BrowserThread::PostTask(
83 BrowserThread::UI,
84 FROM_HERE,
85 base::Bind(QueueTask, base::Passed(queued_task.Pass())));
86 return;
87 }
88 if (IsBrowserStartupComplete()) {
89 ScheduleTask(queued_task.Pass());
90 return;
91 }
92 g_after_startup_tasks.Get().push_back(queued_task.release());
93 }
94
95 void ScheduleTask(scoped_ptr<AfterStartupTask> queued_task) {
96 // Spread their execution over a brief time.
97 const int kMinDelay = 0;
98 const int kMaxDelay = 10;
99 scoped_refptr<base::TaskRunner> target_runner = queued_task->task_runner;
100 tracked_objects::Location from_here = queued_task->from_here;
101 target_runner->PostDelayedTask(
102 from_here,
103 base::Bind(&RunTask, base::Passed(queued_task.Pass())),
104 base::TimeDelta::FromSeconds(base::RandInt(kMinDelay, kMaxDelay)));
105 }
106
107 void RunTask(scoped_ptr<AfterStartupTask> queued_task) {
108 // We're careful to delete the caller's |task| on the target runner's thread.
109 DCHECK(queued_task->task_runner->RunsTasksOnCurrentThread());
110 queued_task->task.Run();
111 }
112
113 // Observes the first page load and set the startup complete flag accordingly.
114 class StartupObserver : public WebContentsObserver {
115 public:
116 StartupObserver() : weak_factory_(this) {}
117 ~StartupObserver() override { DCHECK(IsBrowserStartupComplete()); }
cmumford 2015/03/27 21:59:58 Aren't DCHECKs for programmer errors? If the fails
michaeln 2015/03/27 22:08:00 I guess i'm trying to document that this class isn
118
119 void Start();
120
121 private:
122 void OnStartupComplete() {
123 SetBrowserStartupIsComplete();
124 delete this;
125 }
126
127 void OnFailsafeTimeout() { OnStartupComplete(); }
128
129 // WebContentsObserver overrides
130 void DidFinishLoad(content::RenderFrameHost* render_frame_host,
131 const GURL& validated_url) override {
132 if (!render_frame_host->GetParent())
133 OnStartupComplete();
134 }
135
136 void DidFailLoad(content::RenderFrameHost* render_frame_host,
137 const GURL& validated_url,
138 int error_code,
139 const base::string16& error_description) override {
140 if (!render_frame_host->GetParent())
141 OnStartupComplete();
142 }
143
144 void WebContentsDestroyed() override { OnStartupComplete(); }
145
146 base::WeakPtrFactory<StartupObserver> weak_factory_;
147
148 DISALLOW_COPY_AND_ASSIGN(StartupObserver);
149 };
150
151 void StartupObserver::Start() {
152 // Signal completion quickly when there is no first page to load.
153 const int kShortDelaySecs = 3;
154 base::TimeDelta delay = base::TimeDelta::FromSeconds(kShortDelaySecs);
155
156 #if !defined(OS_ANDROID)
cmumford 2015/03/27 21:59:58 Why not Android?
michaeln 2015/03/27 22:07:59 https://codereview.chromium.org/949293002/#msg35 F
157 WebContents* contents = nullptr;
158 for (chrome::BrowserIterator iter; !iter.done(); iter.Next()) {
159 contents = (*iter)->tab_strip_model()->GetActiveWebContents();
160 if (contents)
161 break;
162 }
163
164 if (contents) {
165 // Give the page time to finish loading.
166 const int kLongerDelayMins = 3;
167 Observe(contents);
168 delay = base::TimeDelta::FromMinutes(kLongerDelayMins);
169 }
170 #endif // !defined(OS_ANDROID)
171
172 BrowserThread::PostDelayedTask(
173 BrowserThread::UI, FROM_HERE,
174 base::Bind(&StartupObserver::OnFailsafeTimeout,
175 weak_factory_.GetWeakPtr()),
176 delay);
177 }
178
179 } // namespace
180
181 void StartMonitoringStartup() {
182 // The observer is self-deleting.
183 (new StartupObserver)->Start();
184 }
185
186 void PostAfterStartupTask(
187 const tracked_objects::Location& from_here,
188 const scoped_refptr<base::TaskRunner>& task_runner,
189 const base::Closure& task) {
190 if (IsBrowserStartupComplete()) {
191 task_runner->PostTask(from_here, task);
192 return;
193 }
194
195 scoped_ptr<AfterStartupTask> queued_task(
196 new AfterStartupTask(from_here, task_runner, task));
197 QueueTask(queued_task.Pass());
198 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698