fix: add microtask runner and fix promise test (#15071)

* fix: Promise resolution and unit test

* Update to use microtask runner

* Address review
This commit is contained in:
Nitish Sakhawalkar 2018-10-15 08:26:47 -07:00 committed by Samuel Attard
parent 89bf71e580
commit 0c711f690e
13 changed files with 90 additions and 18 deletions

View file

@ -1178,7 +1178,7 @@ v8::Local<v8::Promise> App::GetGPUInfo(v8::Isolate* isolate,
auto* const info_mgr = GPUInfoManager::GetInstance();
if (info_type == "complete") {
#if defined(OS_WIN)
#if defined(OS_WIN) || defined(OS_MACOSX)
info_mgr->FetchCompleteInfo(promise);
#else
info_mgr->FetchBasicInfo(promise);

View file

@ -26,11 +26,12 @@ GPUInfoManager::~GPUInfoManager() {
// Based on
// https://chromium.googlesource.com/chromium/src.git/+/69.0.3497.106/content/browser/gpu/gpu_data_manager_impl_private.cc#838
bool GPUInfoManager::NeedsCompleteGpuInfoCollection() {
#if defined(OS_WIN)
const auto& gpu_info = gpu_data_manager_->GetGPUInfo();
return (gpu_info.dx_diagnostics.values.empty() &&
gpu_info.dx_diagnostics.children.empty());
bool GPUInfoManager::NeedsCompleteGpuInfoCollection() const {
#if defined(OS_MACOSX)
return gpu_data_manager_->GetGPUInfo().gl_vendor.empty();
#elif defined(OS_WIN)
return (gpu_data_manager_->GetGPUInfo().dx_diagnostics.values.empty() &&
gpu_data_manager_->GetGPUInfo().dx_diagnostics.children.empty());
#else
return false;
#endif

View file

@ -24,7 +24,7 @@ class GPUInfoManager : public content::GpuDataManagerObserver {
GPUInfoManager();
~GPUInfoManager() override;
bool NeedsCompleteGpuInfoCollection();
bool NeedsCompleteGpuInfoCollection() const;
void FetchCompleteInfo(scoped_refptr<util::Promise> promise);
void FetchBasicInfo(scoped_refptr<util::Promise> promise);
void OnGpuInfoUpdate() override;

View file

@ -253,6 +253,7 @@ void AtomBrowserMainParts::PreMainMessageLoopRun() {
}
bool AtomBrowserMainParts::MainMessageLoopRun(int* result_code) {
js_env_->OnMessageLoopCreated();
exit_code_ = result_code;
return brightray::BrowserMainParts::MainMessageLoopRun(result_code);
}

View file

@ -6,6 +6,7 @@
#include <string>
#include "atom/browser/microtasks_runner.h"
#include "base/command_line.h"
#include "base/message_loop/message_loop.h"
#include "base/task_scheduler/initialization_util.h"
@ -62,7 +63,15 @@ v8::Isolate* JavascriptEnvironment::Initialize(uv_loop_t* event_loop) {
return isolate;
}
void JavascriptEnvironment::OnMessageLoopCreated() {
DCHECK(!microtasks_runner_);
microtasks_runner_.reset(new MicrotasksRunner(isolate()));
base::MessageLoopCurrent::Get()->AddTaskObserver(microtasks_runner_.get());
}
void JavascriptEnvironment::OnMessageLoopDestroying() {
DCHECK(microtasks_runner_);
base::MessageLoopCurrent::Get()->RemoveTaskObserver(microtasks_runner_.get());
platform_->UnregisterIsolate(isolate_);
}

View file

@ -5,6 +5,8 @@
#ifndef ATOM_BROWSER_JAVASCRIPT_ENVIRONMENT_H_
#define ATOM_BROWSER_JAVASCRIPT_ENVIRONMENT_H_
#include <memory>
#include "base/macros.h"
#include "gin/public/isolate_holder.h"
#include "uv.h" // NOLINT(build/include)
@ -16,12 +18,14 @@ class MultiIsolatePlatform;
namespace atom {
class MicrotasksRunner;
// Manage the V8 isolate and context automatically.
class JavascriptEnvironment {
public:
explicit JavascriptEnvironment(uv_loop_t* event_loop);
~JavascriptEnvironment();
void OnMessageLoopCreated();
void OnMessageLoopDestroying();
node::MultiIsolatePlatform* platform() const { return platform_; }
@ -43,6 +47,8 @@ class JavascriptEnvironment {
v8::Global<v8::Context> context_;
v8::Context::Scope context_scope_;
std::unique_ptr<MicrotasksRunner> microtasks_runner_;
DISALLOW_COPY_AND_ASSIGN(JavascriptEnvironment);
};

View file

@ -0,0 +1,20 @@
// Copyright (c) 2018 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#include "atom/browser/microtasks_runner.h"
#include "v8/include/v8.h"
namespace atom {
MicrotasksRunner::MicrotasksRunner(v8::Isolate* isolate) : isolate_(isolate) {}
void MicrotasksRunner::WillProcessTask(const base::PendingTask& pending_task) {}
void MicrotasksRunner::DidProcessTask(const base::PendingTask& pending_task) {
v8::Isolate::Scope scope(isolate_);
v8::MicrotasksScope::PerformCheckpoint(isolate_);
}
} // namespace atom

View file

@ -0,0 +1,36 @@
// Copyright (c) 2018 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#ifndef ATOM_BROWSER_MICROTASKS_RUNNER_H_
#define ATOM_BROWSER_MICROTASKS_RUNNER_H_
#include "base/message_loop/message_loop.h"
namespace v8 {
class Isolate;
}
namespace atom {
// Microtasks like promise resolution, are run at the end of the current
// task. This class implements a task observer that runs tells v8 to run them.
// Microtasks runner implementation is based on the EndOfTaskRunner in blink.
// Node follows the kExplicit MicrotasksPolicy, and we do the same in browser
// process. Hence, we need to have this task observer to flush the queued
// microtasks.
class MicrotasksRunner : public base::MessageLoop::TaskObserver {
public:
explicit MicrotasksRunner(v8::Isolate* isolate);
// base::MessageLoop::TaskObserver
void WillProcessTask(const base::PendingTask& pending_task) override;
void DidProcessTask(const base::PendingTask& pending_task) override;
private:
v8::Isolate* isolate_;
};
} // namespace atom
#endif // ATOM_BROWSER_MICROTASKS_RUNNER_H_