atom_api_renderer_ipc.cc 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // Copyright (c) 2013 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/renderer/api/atom_api_renderer_ipc.h"
  5. #include "atom/common/api/api_messages.h"
  6. #include "atom/common/native_mate_converters/string16_converter.h"
  7. #include "atom/common/native_mate_converters/value_converter.h"
  8. #include "atom/common/node_includes.h"
  9. #include "content/public/renderer/render_frame.h"
  10. #include "native_mate/dictionary.h"
  11. #include "third_party/WebKit/public/web/WebLocalFrame.h"
  12. using blink::WebLocalFrame;
  13. using content::RenderFrame;
  14. namespace atom {
  15. namespace api {
  16. RenderFrame* GetCurrentRenderFrame() {
  17. WebLocalFrame* frame = WebLocalFrame::FrameForCurrentContext();
  18. if (!frame)
  19. return nullptr;
  20. return RenderFrame::FromWebFrame(frame);
  21. }
  22. void Send(mate::Arguments* args,
  23. const base::string16& channel,
  24. const base::ListValue& arguments) {
  25. RenderFrame* render_frame = GetCurrentRenderFrame();
  26. if (render_frame == nullptr)
  27. return;
  28. bool success = render_frame->Send(new AtomFrameHostMsg_Message(
  29. render_frame->GetRoutingID(), channel, arguments));
  30. if (!success)
  31. args->ThrowError("Unable to send AtomFrameHostMsg_Message");
  32. }
  33. base::string16 SendSync(mate::Arguments* args,
  34. const base::string16& channel,
  35. const base::ListValue& arguments) {
  36. base::string16 json;
  37. RenderFrame* render_frame = GetCurrentRenderFrame();
  38. if (render_frame == nullptr)
  39. return json;
  40. IPC::SyncMessage* message = new AtomFrameHostMsg_Message_Sync(
  41. render_frame->GetRoutingID(), channel, arguments, &json);
  42. bool success = render_frame->Send(message);
  43. if (!success)
  44. args->ThrowError("Unable to send AtomFrameHostMsg_Message_Sync");
  45. return json;
  46. }
  47. void Initialize(v8::Local<v8::Object> exports,
  48. v8::Local<v8::Value> unused,
  49. v8::Local<v8::Context> context,
  50. void* priv) {
  51. mate::Dictionary dict(context->GetIsolate(), exports);
  52. dict.SetMethod("send", &Send);
  53. dict.SetMethod("sendSync", &SendSync);
  54. }
  55. } // namespace api
  56. } // namespace atom
  57. NODE_BUILTIN_MODULE_CONTEXT_AWARE(atom_renderer_ipc, atom::api::Initialize)