2016-04-26 07:10:27 +00:00
|
|
|
// Copyright (c) 2016 GitHub, Inc.
|
|
|
|
// Use of this source code is governed by the MIT license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2019-09-18 16:52:06 +00:00
|
|
|
#include "shell/common/api/remote/remote_object_freer.h"
|
2016-04-26 07:10:27 +00:00
|
|
|
|
|
|
|
#include "base/strings/utf_string_conversions.h"
|
|
|
|
#include "base/values.h"
|
2018-03-09 09:31:09 +00:00
|
|
|
#include "content/public/renderer/render_frame.h"
|
2019-06-19 20:46:59 +00:00
|
|
|
#include "electron/shell/common/api/api.mojom.h"
|
2019-04-02 22:38:16 +00:00
|
|
|
#include "third_party/blink/public/common/associated_interfaces/associated_interface_provider.h"
|
2018-07-20 16:08:18 +00:00
|
|
|
#include "third_party/blink/public/web/web_local_frame.h"
|
2016-04-26 07:10:27 +00:00
|
|
|
|
|
|
|
using blink::WebLocalFrame;
|
|
|
|
|
2019-06-19 21:23:04 +00:00
|
|
|
namespace electron {
|
2016-04-26 07:10:27 +00:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2018-03-09 09:31:09 +00:00
|
|
|
content::RenderFrame* GetCurrentRenderFrame() {
|
2017-06-16 20:42:33 +00:00
|
|
|
WebLocalFrame* frame = WebLocalFrame::FrameForCurrentContext();
|
2016-04-26 07:10:27 +00:00
|
|
|
if (!frame)
|
|
|
|
return nullptr;
|
|
|
|
|
2018-03-09 09:31:09 +00:00
|
|
|
return content::RenderFrame::FromWebFrame(frame);
|
2016-04-26 07:10:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
// static
|
2018-04-18 01:55:30 +00:00
|
|
|
void RemoteObjectFreer::BindTo(v8::Isolate* isolate,
|
|
|
|
v8::Local<v8::Object> target,
|
2018-07-10 08:15:40 +00:00
|
|
|
const std::string& context_id,
|
2018-04-18 01:55:30 +00:00
|
|
|
int object_id) {
|
2018-07-10 08:15:40 +00:00
|
|
|
new RemoteObjectFreer(isolate, target, context_id, object_id);
|
2016-04-26 07:10:27 +00:00
|
|
|
}
|
|
|
|
|
2019-04-16 20:08:11 +00:00
|
|
|
// static
|
|
|
|
void RemoteObjectFreer::AddRef(const std::string& context_id, int object_id) {
|
|
|
|
ref_mapper_[context_id][object_id]++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
std::map<std::string, std::map<int, int>> RemoteObjectFreer::ref_mapper_;
|
|
|
|
|
2018-04-18 01:55:30 +00:00
|
|
|
RemoteObjectFreer::RemoteObjectFreer(v8::Isolate* isolate,
|
|
|
|
v8::Local<v8::Object> target,
|
2018-07-10 08:15:40 +00:00
|
|
|
const std::string& context_id,
|
2018-04-18 01:55:30 +00:00
|
|
|
int object_id)
|
2016-04-26 07:10:27 +00:00
|
|
|
: ObjectLifeMonitor(isolate, target),
|
2018-07-10 08:15:40 +00:00
|
|
|
context_id_(context_id),
|
2016-08-12 14:26:13 +00:00
|
|
|
object_id_(object_id),
|
2016-08-12 20:03:31 +00:00
|
|
|
routing_id_(MSG_ROUTING_NONE) {
|
2018-03-09 09:31:09 +00:00
|
|
|
content::RenderFrame* render_frame = GetCurrentRenderFrame();
|
|
|
|
if (render_frame) {
|
|
|
|
routing_id_ = render_frame->GetRoutingID();
|
2016-08-12 14:26:13 +00:00
|
|
|
}
|
2016-04-26 07:10:27 +00:00
|
|
|
}
|
|
|
|
|
2019-09-16 22:12:00 +00:00
|
|
|
RemoteObjectFreer::~RemoteObjectFreer() = default;
|
2016-04-26 07:10:27 +00:00
|
|
|
|
|
|
|
void RemoteObjectFreer::RunDestructor() {
|
2018-03-09 09:31:09 +00:00
|
|
|
content::RenderFrame* render_frame =
|
|
|
|
content::RenderFrame::FromRoutingID(routing_id_);
|
|
|
|
if (!render_frame)
|
2016-04-26 07:10:27 +00:00
|
|
|
return;
|
|
|
|
|
2019-08-28 14:39:21 +00:00
|
|
|
// Reset our local ref count in case we are in a GC race condition
|
|
|
|
// and will get more references in an inbound IPC message
|
|
|
|
int ref_count = 0;
|
|
|
|
const auto objects_it = ref_mapper_.find(context_id_);
|
|
|
|
if (objects_it != std::end(ref_mapper_)) {
|
|
|
|
auto& objects = objects_it->second;
|
|
|
|
const auto ref_it = objects.find(object_id_);
|
|
|
|
if (ref_it != std::end(objects)) {
|
|
|
|
ref_count = ref_it->second;
|
|
|
|
objects.erase(ref_it);
|
|
|
|
}
|
|
|
|
if (objects.empty())
|
|
|
|
ref_mapper_.erase(objects_it);
|
|
|
|
}
|
|
|
|
|
2019-01-23 16:24:57 +00:00
|
|
|
auto* channel = "ELECTRON_BROWSER_DEREFERENCE";
|
2019-08-28 14:39:21 +00:00
|
|
|
|
2016-04-26 07:10:27 +00:00
|
|
|
base::ListValue args;
|
2018-07-10 08:15:40 +00:00
|
|
|
args.AppendString(context_id_);
|
2016-04-26 07:10:27 +00:00
|
|
|
args.AppendInteger(object_id_);
|
2019-08-28 14:39:21 +00:00
|
|
|
args.AppendInteger(ref_count);
|
2019-04-02 22:38:16 +00:00
|
|
|
|
|
|
|
mojom::ElectronBrowserAssociatedPtr electron_ptr;
|
|
|
|
render_frame->GetRemoteAssociatedInterfaces()->GetInterface(
|
|
|
|
mojo::MakeRequest(&electron_ptr));
|
|
|
|
electron_ptr->Message(true, channel, args.Clone());
|
2016-04-26 07:10:27 +00:00
|
|
|
}
|
|
|
|
|
2019-06-19 21:23:04 +00:00
|
|
|
} // namespace electron
|