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

Side by Side Diff: content/public/test/test_utils.cc

Issue 380993002: Upstream RunBlockingPoolTask(). (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Addressed comments. Created 6 years, 5 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 | Annotate | Revision Log
« no previous file with comments | « content/public/test/test_utils.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 "content/public/test/test_utils.h" 5 #include "content/public/test/test_utils.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/message_loop/message_loop.h" 8 #include "base/message_loop/message_loop.h"
9 #include "base/run_loop.h" 9 #include "base/run_loop.h"
10 #include "base/strings/utf_string_conversions.h" 10 #include "base/strings/utf_string_conversions.h"
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
42 base::Bind(&DeferredQuitRunLoop, quit_task, num_quit_deferrals - 1)); 42 base::Bind(&DeferredQuitRunLoop, quit_task, num_quit_deferrals - 1));
43 } 43 }
44 } 44 }
45 45
46 void RunAllPendingMessageAndSendQuit(BrowserThread::ID thread_id, 46 void RunAllPendingMessageAndSendQuit(BrowserThread::ID thread_id,
47 const base::Closure& quit_task) { 47 const base::Closure& quit_task) {
48 RunAllPendingInMessageLoop(); 48 RunAllPendingInMessageLoop();
49 BrowserThread::PostTask(thread_id, FROM_HERE, quit_task); 49 BrowserThread::PostTask(thread_id, FROM_HERE, quit_task);
50 } 50 }
51 51
52 // Class used handle result callbacks for ExecuteScriptAndGetValue. 52 // Class used to handle result callbacks for ExecuteScriptAndGetValue.
53 class ScriptCallback { 53 class ScriptCallback {
54 public: 54 public:
55 ScriptCallback() { } 55 ScriptCallback() { }
56 virtual ~ScriptCallback() { } 56 virtual ~ScriptCallback() { }
57 void ResultCallback(const base::Value* result); 57 void ResultCallback(const base::Value* result);
58 58
59 scoped_ptr<base::Value> result() { return result_.Pass(); } 59 scoped_ptr<base::Value> result() { return result_.Pass(); }
60 60
61 private: 61 private:
62 scoped_ptr<base::Value> result_; 62 scoped_ptr<base::Value> result_;
63 63
64 DISALLOW_COPY_AND_ASSIGN(ScriptCallback); 64 DISALLOW_COPY_AND_ASSIGN(ScriptCallback);
65 }; 65 };
66 66
67 void ScriptCallback::ResultCallback(const base::Value* result) { 67 void ScriptCallback::ResultCallback(const base::Value* result) {
68 if (result) 68 if (result)
69 result_.reset(result->DeepCopy()); 69 result_.reset(result->DeepCopy());
70 base::MessageLoop::current()->Quit(); 70 base::MessageLoop::current()->Quit();
71 } 71 }
72 72
73 // Monitors if any task is processed by the message loop.
74 class TaskObserver : public base::MessageLoop::TaskObserver {
75 public:
76 TaskObserver() : processed_(false) {}
77 virtual ~TaskObserver() {}
78
79 // MessageLoop::TaskObserver overrides.
80 virtual void WillProcessTask(const base::PendingTask& pending_task) OVERRIDE {
81 }
82 virtual void DidProcessTask(const base::PendingTask& pending_task) OVERRIDE {
83 processed_ = true;
84 }
85
86 // Returns true if any task was processed.
87 bool processed() const { return processed_; }
88
89 private:
90 bool processed_;
91 DISALLOW_COPY_AND_ASSIGN(TaskObserver);
92 };
93
73 // Adapter that makes a WindowedNotificationObserver::ConditionTestCallback from 94 // Adapter that makes a WindowedNotificationObserver::ConditionTestCallback from
74 // a WindowedNotificationObserver::ConditionTestCallbackWithoutSourceAndDetails 95 // a WindowedNotificationObserver::ConditionTestCallbackWithoutSourceAndDetails
75 // by ignoring the notification source and details. 96 // by ignoring the notification source and details.
76 bool IgnoreSourceAndDetails( 97 bool IgnoreSourceAndDetails(
77 const WindowedNotificationObserver:: 98 const WindowedNotificationObserver::
78 ConditionTestCallbackWithoutSourceAndDetails& callback, 99 ConditionTestCallbackWithoutSourceAndDetails& callback,
79 const NotificationSource& source, 100 const NotificationSource& source,
80 const NotificationDetails& details) { 101 const NotificationDetails& details) {
81 return callback.Run(); 102 return callback.Run();
82 } 103 }
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
122 return; 143 return;
123 } 144 }
124 145
125 base::RunLoop run_loop; 146 base::RunLoop run_loop;
126 BrowserThread::PostTask(thread_id, FROM_HERE, 147 BrowserThread::PostTask(thread_id, FROM_HERE,
127 base::Bind(&RunAllPendingMessageAndSendQuit, current_thread_id, 148 base::Bind(&RunAllPendingMessageAndSendQuit, current_thread_id,
128 run_loop.QuitClosure())); 149 run_loop.QuitClosure()));
129 RunThisRunLoop(&run_loop); 150 RunThisRunLoop(&run_loop);
130 } 151 }
131 152
153 void RunAllBlockingPoolTasksUntilIdle() {
154 while (true) {
155 TaskObserver task_observer;
hashimoto 2014/07/15 05:49:40 nit: task_observer should be declared and initiali
mtomasz 2014/07/15 05:59:16 Done.
156 content::BrowserThread::GetBlockingPool()->FlushForTesting();
157
158 base::MessageLoop::current()->AddTaskObserver(&task_observer);
159 base::RunLoop().RunUntilIdle();
160 base::MessageLoop::current()->RemoveTaskObserver(&task_observer);
161
162 if (!task_observer.processed())
163 break;
164 }
165 }
166
132 base::Closure GetQuitTaskForRunLoop(base::RunLoop* run_loop) { 167 base::Closure GetQuitTaskForRunLoop(base::RunLoop* run_loop) {
133 return base::Bind(&DeferredQuitRunLoop, run_loop->QuitClosure(), 168 return base::Bind(&DeferredQuitRunLoop, run_loop->QuitClosure(),
134 kNumQuitDeferrals); 169 kNumQuitDeferrals);
135 } 170 }
136 171
137 scoped_ptr<base::Value> ExecuteScriptAndGetValue( 172 scoped_ptr<base::Value> ExecuteScriptAndGetValue(
138 RenderFrameHost* render_frame_host, const std::string& script) { 173 RenderFrameHost* render_frame_host, const std::string& script) {
139 ScriptCallback observer; 174 ScriptCallback observer;
140 175
141 render_frame_host->ExecuteJavaScript( 176 render_frame_host->ExecuteJavaScript(
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after
268 void InProcessUtilityThreadHelper::BrowserChildProcessHostDisconnected( 303 void InProcessUtilityThreadHelper::BrowserChildProcessHostDisconnected(
269 const ChildProcessData& data) { 304 const ChildProcessData& data) {
270 if (--child_thread_count_) 305 if (--child_thread_count_)
271 return; 306 return;
272 307
273 if (runner_.get()) 308 if (runner_.get())
274 runner_->Quit(); 309 runner_->Quit();
275 } 310 }
276 311
277 } // namespace content 312 } // namespace content
OLDNEW
« no previous file with comments | « content/public/test/test_utils.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698