feat: promisify netLog.stopLogging (#16862)

This commit is contained in:
Milan Burda 2019-02-19 11:48:27 +01:00 committed by Alexey Kuzmin
commit 7e7abc28f5
9 changed files with 78 additions and 35 deletions

View file

@ -20,6 +20,15 @@
#include "atom/common/node_includes.h"
namespace {
void OnGetFilePathToCompletedLog(scoped_refptr<atom::util::Promise> promise,
const base::FilePath& file_path) {
promise->Resolve(file_path);
}
} // namespace
namespace atom {
namespace api {
@ -87,19 +96,17 @@ std::string NetLog::GetCurrentlyLoggingPath() const {
return std::string();
}
void NetLog::StopLogging(mate::Arguments* args) {
net_log::NetExportFileWriter::FilePathCallback callback;
if (!args->GetNext(&callback)) {
args->ThrowError("Invalid callback function");
return;
}
v8::Local<v8::Promise> NetLog::StopLogging(mate::Arguments* args) {
scoped_refptr<util::Promise> promise = new util::Promise(isolate());
if (IsCurrentlyLogging()) {
stop_callback_queue_.emplace_back(callback);
stop_callback_queue_.emplace_back(promise);
net_log_writer_->StopNetLog(nullptr);
} else {
callback.Run(base::FilePath());
promise->Resolve(base::FilePath());
}
return promise->GetHandle();
}
void NetLog::OnNewState(const base::DictionaryValue& state) {
@ -109,9 +116,9 @@ void NetLog::OnNewState(const base::DictionaryValue& state) {
return;
if (GetLoggingState() == "NOT_LOGGING") {
for (auto& callback : stop_callback_queue_) {
if (!callback.is_null())
net_log_writer_->GetFilePathToCompletedLog(callback);
for (auto& promise : stop_callback_queue_) {
net_log_writer_->GetFilePathToCompletedLog(
base::Bind(&OnGetFilePathToCompletedLog, promise));
}
stop_callback_queue_.clear();
}

View file

@ -10,6 +10,7 @@
#include <string>
#include "atom/browser/api/trackable_object.h"
#include "atom/common/promise_util.h"
#include "base/callback.h"
#include "base/values.h"
#include "components/net_log/net_export_file_writer.h"
@ -34,7 +35,7 @@ class NetLog : public mate::TrackableObject<NetLog>,
std::string GetLoggingState() const;
bool IsCurrentlyLogging() const;
std::string GetCurrentlyLoggingPath() const;
void StopLogging(mate::Arguments* args);
v8::Local<v8::Promise> StopLogging(mate::Arguments* args);
protected:
explicit NetLog(v8::Isolate* isolate, AtomBrowserContext* browser_context);
@ -46,8 +47,7 @@ class NetLog : public mate::TrackableObject<NetLog>,
private:
AtomBrowserContext* browser_context_;
net_log::NetExportFileWriter* net_log_writer_;
std::list<net_log::NetExportFileWriter::FilePathCallback>
stop_callback_queue_;
std::list<scoped_refptr<atom::util::Promise>> stop_callback_queue_;
std::unique_ptr<base::DictionaryValue> net_log_state_;
DISALLOW_COPY_AND_ASSIGN(NetLog);

View file

@ -785,6 +785,7 @@ void Session::BuildPrototype(v8::Isolate* isolate,
namespace {
using atom::api::Cookies;
using atom::api::NetLog;
using atom::api::Protocol;
using atom::api::Session;
@ -811,6 +812,9 @@ void Initialize(v8::Local<v8::Object> exports,
dict.Set(
"Cookies",
Cookies::GetConstructor(isolate)->GetFunction(context).ToLocalChecked());
dict.Set(
"NetLog",
NetLog::GetConstructor(isolate)->GetFunction(context).ToLocalChecked());
dict.Set(
"Protocol",
Protocol::GetConstructor(isolate)->GetFunction(context).ToLocalChecked());