refactor: use WeakRef on renderer side of remote (#24037)

This commit is contained in:
Jeremy Rose 2020-06-12 15:50:03 -07:00 committed by GitHub
parent 178e46cd23
commit 379bb174e9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 26 additions and 117 deletions

View file

@ -18,7 +18,6 @@
#if BUILDFLAG(ENABLE_REMOTE_MODULE)
#include "shell/common/api/remote/remote_callback_freer.h"
#include "shell/common/api/remote/remote_object_freer.h"
#endif
namespace std {
@ -148,7 +147,6 @@ void Initialize(v8::Local<v8::Object> exports,
#if BUILDFLAG(ENABLE_REMOTE_MODULE)
dict.SetMethod("setRemoteCallbackFreer",
&electron::RemoteCallbackFreer::BindTo);
dict.SetMethod("setRemoteObjectFreer", &electron::RemoteObjectFreer::BindTo);
dict.SetMethod(
"createDoubleIDWeakMap",
&electron::api::KeyWeakMap<std::pair<std::string, int32_t>>::Create);

View file

@ -1,66 +0,0 @@
// Copyright (c) 2016 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#include "shell/common/api/remote/remote_object_freer.h"
#include "base/strings/utf_string_conversions.h"
#include "base/values.h"
#include "content/public/renderer/render_frame.h"
#include "services/service_manager/public/cpp/interface_provider.h"
#include "shell/common/api/api.mojom.h"
#include "third_party/blink/public/web/web_local_frame.h"
using blink::WebLocalFrame;
namespace electron {
namespace {
content::RenderFrame* GetCurrentRenderFrame() {
WebLocalFrame* frame = WebLocalFrame::FrameForCurrentContext();
if (!frame)
return nullptr;
return content::RenderFrame::FromWebFrame(frame);
}
} // namespace
// static
void RemoteObjectFreer::BindTo(v8::Isolate* isolate,
v8::Local<v8::Object> target,
const std::string& context_id,
int object_id) {
new RemoteObjectFreer(isolate, target, context_id, object_id);
}
RemoteObjectFreer::RemoteObjectFreer(v8::Isolate* isolate,
v8::Local<v8::Object> target,
const std::string& context_id,
int object_id)
: ObjectLifeMonitor(isolate, target),
context_id_(context_id),
object_id_(object_id),
routing_id_(MSG_ROUTING_NONE) {
content::RenderFrame* render_frame = GetCurrentRenderFrame();
if (render_frame) {
routing_id_ = render_frame->GetRoutingID();
}
}
RemoteObjectFreer::~RemoteObjectFreer() = default;
void RemoteObjectFreer::RunDestructor() {
content::RenderFrame* render_frame =
content::RenderFrame::FromRoutingID(routing_id_);
if (!render_frame)
return;
mojom::ElectronBrowserPtr electron_ptr;
render_frame->GetRemoteInterfaces()->GetInterface(
mojo::MakeRequest(&electron_ptr));
electron_ptr->DereferenceRemoteJSObject(context_id_, object_id_);
}
} // namespace electron

View file

@ -1,41 +0,0 @@
// Copyright (c) 2016 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#ifndef SHELL_COMMON_API_REMOTE_REMOTE_OBJECT_FREER_H_
#define SHELL_COMMON_API_REMOTE_REMOTE_OBJECT_FREER_H_
#include <map>
#include <string>
#include "shell/common/api/object_life_monitor.h"
namespace electron {
class RemoteObjectFreer : public ObjectLifeMonitor {
public:
static void BindTo(v8::Isolate* isolate,
v8::Local<v8::Object> target,
const std::string& context_id,
int object_id);
protected:
RemoteObjectFreer(v8::Isolate* isolate,
v8::Local<v8::Object> target,
const std::string& context_id,
int object_id);
~RemoteObjectFreer() override;
void RunDestructor() override;
private:
std::string context_id_;
int object_id_;
int routing_id_;
DISALLOW_COPY_AND_ASSIGN(RemoteObjectFreer);
};
} // namespace electron
#endif // SHELL_COMMON_API_REMOTE_REMOTE_OBJECT_FREER_H_