]> khamidou — GitHub mirrors - electron.git/commitdiff
Feed gin::PerIsolateData with a task runner
authorCheng Zhao <zcbenz@gmail.com>
Wed, 2 Sep 2015 07:52:17 +0000 (15:52 +0800)
committerCheng Zhao <zcbenz@gmail.com>
Thu, 3 Sep 2015 08:50:23 +0000 (17:50 +0900)
atom/browser/atom_browser_main_parts.cc
atom/browser/atom_browser_main_parts.h
atom/browser/bridge_task_runner.cc [new file with mode: 0644]
atom/browser/bridge_task_runner.h [new file with mode: 0644]
filenames.gypi

index c472b8742f0625c75a67fda57bf53b9a812dec97..b4f61d0f195e39af9c4a59856ca74ba1d65f79b8 100644 (file)
@@ -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();
 
index 97663a72404e67d9bfc8e9f9756c8a1650a76f17..a2909cac53d04658be4dec63d3fec40f84ed2d53 100644 (file)
@@ -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<BrowserProcess> 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<BridgeTaskRunner> bridge_task_runner_;
+
   scoped_ptr<Browser> browser_;
   scoped_ptr<JavascriptEnvironment> js_env_;
   scoped_ptr<NodeBindings> node_bindings_;
diff --git a/atom/browser/bridge_task_runner.cc b/atom/browser/bridge_task_runner.cc
new file mode 100644 (file)
index 0000000..24572f3
--- /dev/null
@@ -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 (file)
index 0000000..01bde48
--- /dev/null
@@ -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_
index af0c56e1ea695e04679fbae16f6733895cddc333..3ea01274cf71e00edb7e09045b376a66b35d301d 100644 (file)
       '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',