From 262b66b93af57f5395ec0e532ccea28f4922b961 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Wed, 2 Sep 2015 15:52:17 +0800 Subject: [PATCH] Feed gin::PerIsolateData with a task runner --- atom/browser/atom_browser_main_parts.cc | 16 ++++++++-- atom/browser/atom_browser_main_parts.h | 5 +++ atom/browser/bridge_task_runner.cc | 42 +++++++++++++++++++++++++ atom/browser/bridge_task_runner.h | 35 +++++++++++++++++++++ filenames.gypi | 2 ++ 5 files changed, 97 insertions(+), 3 deletions(-) create mode 100644 atom/browser/bridge_task_runner.cc create mode 100644 atom/browser/bridge_task_runner.h diff --git a/atom/browser/atom_browser_main_parts.cc b/atom/browser/atom_browser_main_parts.cc index c472b8742f06..b4f61d0f195e 100644 --- a/atom/browser/atom_browser_main_parts.cc +++ b/atom/browser/atom_browser_main_parts.cc @@ -7,12 +7,14 @@ #include "atom/browser/api/trackable_object.h" #include "atom/browser/atom_browser_client.h" #include "atom/browser/atom_browser_context.h" +#include "atom/browser/bridge_task_runner.h" #include "atom/browser/browser.h" #include "atom/browser/javascript_environment.h" #include "atom/browser/node_debugger.h" #include "atom/common/api/atom_bindings.h" #include "atom/common/node_bindings.h" #include "base/command_line.h" +#include "base/thread_task_runner_handle.h" #include "chrome/browser/browser_process.h" #include "v8/include/v8-debug.h" @@ -64,9 +66,17 @@ void AtomBrowserMainParts::PostEarlyInitialization() { SetDPIFromGSettings(); #endif - // The ProxyResolverV8 has setup a complete V8 environment, in order to avoid - // conflicts we only initialize our V8 environment after that. - js_env_.reset(new JavascriptEnvironment); + { + // Temporary set the bridge_task_runner_ as current thread's task runner, + // so we can fool gin::PerIsolateData to use it as its task runner, instead + // of getting current message loop's task runner, which is null for now. + bridge_task_runner_ = new BridgeTaskRunner; + base::ThreadTaskRunnerHandle handle(bridge_task_runner_); + + // The ProxyResolverV8 has setup a complete V8 environment, in order to + // avoid conflicts we only initialize our V8 environment after that. + js_env_.reset(new JavascriptEnvironment); + } node_bindings_->Initialize(); diff --git a/atom/browser/atom_browser_main_parts.h b/atom/browser/atom_browser_main_parts.h index 97663a72404e..a2909cac53d0 100644 --- a/atom/browser/atom_browser_main_parts.h +++ b/atom/browser/atom_browser_main_parts.h @@ -20,6 +20,7 @@ class Browser; class JavascriptEnvironment; class NodeBindings; class NodeDebugger; +class BridgeTaskRunner; class AtomBrowserMainParts : public brightray::BrowserMainParts { public: @@ -54,6 +55,10 @@ class AtomBrowserMainParts : public brightray::BrowserMainParts { // A fake BrowserProcess object that used to feed the source code from chrome. scoped_ptr fake_browser_process_; + // The gin::PerIsolateData requires a task runner to create, so we feed it + // with a task runner that will post all work to main loop. + scoped_refptr bridge_task_runner_; + scoped_ptr browser_; scoped_ptr js_env_; scoped_ptr node_bindings_; diff --git a/atom/browser/bridge_task_runner.cc b/atom/browser/bridge_task_runner.cc new file mode 100644 index 000000000000..24572f3990d8 --- /dev/null +++ b/atom/browser/bridge_task_runner.cc @@ -0,0 +1,42 @@ +// Copyright (c) 2015 GitHub, Inc. +// Use of this source code is governed by the MIT license that can be +// found in the LICENSE file. + +#include "atom/browser/bridge_task_runner.h" + +#include "base/message_loop/message_loop.h" + +namespace atom { + +bool BridgeTaskRunner::PostDelayedTask( + const tracked_objects::Location& from_here, + const base::Closure& task, + base::TimeDelta delay) { + auto message_loop = base::MessageLoop::current(); + if (!message_loop) + return false; + + return message_loop->task_runner()->PostDelayedTask(from_here, task, delay); +} + +bool BridgeTaskRunner::RunsTasksOnCurrentThread() const { + auto message_loop = base::MessageLoop::current(); + if (!message_loop) + return false; + + return message_loop->task_runner()->RunsTasksOnCurrentThread(); +} + +bool BridgeTaskRunner::PostNonNestableDelayedTask( + const tracked_objects::Location& from_here, + const base::Closure& task, + base::TimeDelta delay) { + auto message_loop = base::MessageLoop::current(); + if (!message_loop) + return false; + + return message_loop->task_runner()->PostNonNestableDelayedTask( + from_here, task, delay); +} + +} // namespace atom diff --git a/atom/browser/bridge_task_runner.h b/atom/browser/bridge_task_runner.h new file mode 100644 index 000000000000..01bde48eb805 --- /dev/null +++ b/atom/browser/bridge_task_runner.h @@ -0,0 +1,35 @@ +// Copyright (c) 2015 GitHub, Inc. +// Use of this source code is governed by the MIT license that can be +// found in the LICENSE file. + +#ifndef ATOM_BROWSER_BRIDGE_TASK_RUNNER_H_ +#define ATOM_BROWSER_BRIDGE_TASK_RUNNER_H_ + +#include "base/single_thread_task_runner.h" + +namespace atom { + +// Post all tasks to the current message loop's task runner if available, +// otherwise fail silently. +class BridgeTaskRunner : public base::SingleThreadTaskRunner { + public: + BridgeTaskRunner() {} + ~BridgeTaskRunner() {} + + // base::SingleThreadTaskRunner: + bool PostDelayedTask(const tracked_objects::Location& from_here, + const base::Closure& task, + base::TimeDelta delay) override; + bool RunsTasksOnCurrentThread() const override; + bool PostNonNestableDelayedTask( + const tracked_objects::Location& from_here, + const base::Closure& task, + base::TimeDelta delay) override; + + private: + DISALLOW_COPY_AND_ASSIGN(BridgeTaskRunner); +}; + +} // namespace atom + +#endif // ATOM_BROWSER_BRIDGE_TASK_RUNNER_H_ diff --git a/filenames.gypi b/filenames.gypi index af0c56e1ea69..3ea01274cf71 100644 --- a/filenames.gypi +++ b/filenames.gypi @@ -125,6 +125,8 @@ 'atom/browser/atom_quota_permission_context.h', 'atom/browser/atom_speech_recognition_manager_delegate.cc', 'atom/browser/atom_speech_recognition_manager_delegate.h', + 'atom/browser/bridge_task_runner.cc', + 'atom/browser/bridge_task_runner.h', 'atom/browser/browser.cc', 'atom/browser/browser.h', 'atom/browser/browser_linux.cc',