Don't discard tasks in BridgeTaskRunner

This commit is contained in:
Cheng Zhao 2015-10-22 15:54:27 +08:00
parent bcb78ebc00
commit 05c6300329
3 changed files with 40 additions and 6 deletions

View file

@ -105,6 +105,7 @@ void AtomBrowserMainParts::PreMainMessageLoopRun() {
1000)); 1000));
brightray::BrowserMainParts::PreMainMessageLoopRun(); brightray::BrowserMainParts::PreMainMessageLoopRun();
BridgeTaskRunner::MessageLoopIsReady();
#if defined(USE_X11) #if defined(USE_X11)
libgtk2ui::GtkInitFromCommandLine(*base::CommandLine::ForCurrentProcess()); libgtk2ui::GtkInitFromCommandLine(*base::CommandLine::ForCurrentProcess());

View file

@ -8,13 +8,33 @@
namespace atom { namespace atom {
// static
std::vector<BridgeTaskRunner::TaskPair> BridgeTaskRunner::tasks_;
std::vector<BridgeTaskRunner::TaskPair> BridgeTaskRunner::non_nestable_tasks_;
// static
void BridgeTaskRunner::MessageLoopIsReady() {
auto message_loop = base::MessageLoop::current();
CHECK(message_loop);
for (const TaskPair& task : tasks_) {
message_loop->task_runner()->PostDelayedTask(
base::get<0>(task), base::get<1>(task), base::get<2>(task));
}
for (const TaskPair& task : non_nestable_tasks_) {
message_loop->task_runner()->PostNonNestableDelayedTask(
base::get<0>(task), base::get<1>(task), base::get<2>(task));
}
}
bool BridgeTaskRunner::PostDelayedTask( bool BridgeTaskRunner::PostDelayedTask(
const tracked_objects::Location& from_here, const tracked_objects::Location& from_here,
const base::Closure& task, const base::Closure& task,
base::TimeDelta delay) { base::TimeDelta delay) {
auto message_loop = base::MessageLoop::current(); auto message_loop = base::MessageLoop::current();
if (!message_loop) if (!message_loop) {
return false; tasks_.push_back(base::MakeTuple(from_here, task, delay));
return true;
}
return message_loop->task_runner()->PostDelayedTask(from_here, task, delay); return message_loop->task_runner()->PostDelayedTask(from_here, task, delay);
} }
@ -22,7 +42,7 @@ bool BridgeTaskRunner::PostDelayedTask(
bool BridgeTaskRunner::RunsTasksOnCurrentThread() const { bool BridgeTaskRunner::RunsTasksOnCurrentThread() const {
auto message_loop = base::MessageLoop::current(); auto message_loop = base::MessageLoop::current();
if (!message_loop) if (!message_loop)
return false; return true;
return message_loop->task_runner()->RunsTasksOnCurrentThread(); return message_loop->task_runner()->RunsTasksOnCurrentThread();
} }
@ -32,8 +52,10 @@ bool BridgeTaskRunner::PostNonNestableDelayedTask(
const base::Closure& task, const base::Closure& task,
base::TimeDelta delay) { base::TimeDelta delay) {
auto message_loop = base::MessageLoop::current(); auto message_loop = base::MessageLoop::current();
if (!message_loop) if (!message_loop) {
return false; non_nestable_tasks_.push_back(base::MakeTuple(from_here, task, delay));
return true;
}
return message_loop->task_runner()->PostNonNestableDelayedTask( return message_loop->task_runner()->PostNonNestableDelayedTask(
from_here, task, delay); from_here, task, delay);

View file

@ -5,17 +5,23 @@
#ifndef ATOM_BROWSER_BRIDGE_TASK_RUNNER_H_ #ifndef ATOM_BROWSER_BRIDGE_TASK_RUNNER_H_
#define ATOM_BROWSER_BRIDGE_TASK_RUNNER_H_ #define ATOM_BROWSER_BRIDGE_TASK_RUNNER_H_
#include <vector>
#include "base/single_thread_task_runner.h" #include "base/single_thread_task_runner.h"
#include "base/tuple.h"
namespace atom { namespace atom {
// Post all tasks to the current message loop's task runner if available, // Post all tasks to the current message loop's task runner if available,
// otherwise fail silently. // otherwise delay the work until message loop is ready.
class BridgeTaskRunner : public base::SingleThreadTaskRunner { class BridgeTaskRunner : public base::SingleThreadTaskRunner {
public: public:
BridgeTaskRunner() {} BridgeTaskRunner() {}
~BridgeTaskRunner() override {} ~BridgeTaskRunner() override {}
// Called when message loop is ready.
static void MessageLoopIsReady();
// base::SingleThreadTaskRunner: // base::SingleThreadTaskRunner:
bool PostDelayedTask(const tracked_objects::Location& from_here, bool PostDelayedTask(const tracked_objects::Location& from_here,
const base::Closure& task, const base::Closure& task,
@ -27,6 +33,11 @@ class BridgeTaskRunner : public base::SingleThreadTaskRunner {
base::TimeDelta delay) override; base::TimeDelta delay) override;
private: private:
using TaskPair = base::Tuple<
tracked_objects::Location, base::Closure, base::TimeDelta>;
static std::vector<TaskPair> tasks_;
static std::vector<TaskPair> non_nestable_tasks_;
DISALLOW_COPY_AND_ASSIGN(BridgeTaskRunner); DISALLOW_COPY_AND_ASSIGN(BridgeTaskRunner);
}; };