Browse Source

chore: tune-up electron::api::NetLog (#29534)

David Sanders 3 years ago
parent
commit
8844034fa8

+ 9 - 4
shell/browser/api/electron_api_net_log.cc

@@ -7,13 +7,17 @@
 #include <string>
 #include <utility>
 
+#include "base/bind.h"
 #include "base/command_line.h"
+#include "base/files/file_path.h"
 #include "base/task/thread_pool.h"
+#include "base/task_runner_util.h"
 #include "chrome/browser/browser_process.h"
 #include "components/net_log/chrome_net_log.h"
 #include "content/public/browser/storage_partition.h"
 #include "electron/electron_version.h"
 #include "gin/object_template_builder.h"
+#include "net/log/net_log_capture_mode.h"
 #include "shell/browser/electron_browser_context.h"
 #include "shell/browser/net/system_network_context_manager.h"
 #include "shell/common/gin_converters/file_path_converter.h"
@@ -135,9 +139,10 @@ v8::Local<v8::Promise> NetLog::StartLogging(base::FilePath log_path,
   auto* network_context =
       browser_context_->GetDefaultStoragePartition()->GetNetworkContext();
 
-  network_context->CreateNetLogExporter(mojo::MakeRequest(&net_log_exporter_));
-  net_log_exporter_.set_connection_error_handler(base::BindOnce(
-      &NetLog::OnConnectionError, weak_ptr_factory_.GetWeakPtr()));
+  network_context->CreateNetLogExporter(
+      net_log_exporter_.BindNewPipeAndPassReceiver());
+  net_log_exporter_.set_disconnect_handler(
+      base::BindOnce(&NetLog::OnConnectionError, base::Unretained(this)));
 
   base::PostTaskAndReplyWithResult(
       file_task_runner_.get(), FROM_HERE,
@@ -201,7 +206,7 @@ v8::Local<v8::Promise> NetLog::StopLogging(gin::Arguments* args) {
     net_log_exporter_->Stop(
         base::Value(base::Value::Type::DICTIONARY),
         base::BindOnce(
-            [](network::mojom::NetLogExporterPtr,
+            [](mojo::Remote<network::mojom::NetLogExporter>,
                gin_helper::Promise<void> promise, int32_t error) {
               ResolvePromiseWithNetError(std::move(promise), error);
             },

+ 7 - 1
shell/browser/api/electron_api_net_log.h

@@ -6,9 +6,14 @@
 #define SHELL_BROWSER_API_ELECTRON_API_NET_LOG_H_
 
 #include "base/callback.h"
+#include "base/files/file_path.h"
+#include "base/macros.h"
+#include "base/memory/weak_ptr.h"
 #include "base/values.h"
 #include "gin/handle.h"
 #include "gin/wrappable.h"
+#include "mojo/public/cpp/bindings/remote.h"
+#include "net/log/net_log_capture_mode.h"
 #include "services/network/public/mojom/net_log.mojom.h"
 #include "shell/common/gin_helper/promise.h"
 #include "third_party/abseil-cpp/absl/types/optional.h"
@@ -23,6 +28,7 @@ class ElectronBrowserContext;
 
 namespace api {
 
+// The code is referenced from the net_log::NetExportFileWriter class.
 class NetLog : public gin::Wrappable<NetLog> {
  public:
   static gin::Handle<NetLog> Create(v8::Isolate* isolate,
@@ -55,7 +61,7 @@ class NetLog : public gin::Wrappable<NetLog> {
  private:
   ElectronBrowserContext* browser_context_;
 
-  network::mojom::NetLogExporterPtr net_log_exporter_;
+  mojo::Remote<network::mojom::NetLogExporter> net_log_exporter_;
 
   absl::optional<gin_helper::Promise<void>> pending_start_promise_;