remote_object_freer.cc 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. // Copyright (c) 2016 GitHub, Inc.
  2. // Use of this source code is governed by the MIT license that can be
  3. // found in the LICENSE file.
  4. #include "atom/common/api/remote_object_freer.h"
  5. #include "atom/common/api/api_messages.h"
  6. #include "base/strings/utf_string_conversions.h"
  7. #include "base/values.h"
  8. #include "content/public/renderer/render_frame.h"
  9. #include "third_party/WebKit/public/web/WebLocalFrame.h"
  10. using blink::WebLocalFrame;
  11. namespace atom {
  12. namespace {
  13. content::RenderFrame* GetCurrentRenderFrame() {
  14. WebLocalFrame* frame = WebLocalFrame::FrameForCurrentContext();
  15. if (!frame)
  16. return nullptr;
  17. return content::RenderFrame::FromWebFrame(frame);
  18. }
  19. } // namespace
  20. // static
  21. void RemoteObjectFreer::BindTo(v8::Isolate* isolate,
  22. v8::Local<v8::Object> target,
  23. const std::string& context_id,
  24. int object_id) {
  25. new RemoteObjectFreer(isolate, target, context_id, object_id);
  26. }
  27. RemoteObjectFreer::RemoteObjectFreer(v8::Isolate* isolate,
  28. v8::Local<v8::Object> target,
  29. const std::string& context_id,
  30. int object_id)
  31. : ObjectLifeMonitor(isolate, target),
  32. context_id_(context_id),
  33. object_id_(object_id),
  34. routing_id_(MSG_ROUTING_NONE) {
  35. content::RenderFrame* render_frame = GetCurrentRenderFrame();
  36. if (render_frame) {
  37. routing_id_ = render_frame->GetRoutingID();
  38. }
  39. }
  40. RemoteObjectFreer::~RemoteObjectFreer() {}
  41. void RemoteObjectFreer::RunDestructor() {
  42. content::RenderFrame* render_frame =
  43. content::RenderFrame::FromRoutingID(routing_id_);
  44. if (!render_frame)
  45. return;
  46. base::string16 channel = base::ASCIIToUTF16("ipc-message");
  47. base::ListValue args;
  48. args.AppendString("ELECTRON_BROWSER_DEREFERENCE");
  49. args.AppendString(context_id_);
  50. args.AppendInteger(object_id_);
  51. render_frame->Send(new AtomFrameHostMsg_Message(render_frame->GetRoutingID(),
  52. channel, args));
  53. }
  54. } // namespace atom