atom_render_frame_observer.cc 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. // Copyright (c) 2017 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/atom_render_frame_observer.h"
  5. #include <string>
  6. #include <vector>
  7. #include "atom/common/api/api_messages.h"
  8. #include "atom/common/api/event_emitter_caller.h"
  9. #include "atom/common/native_mate_converters/value_converter.h"
  10. #include "atom/common/node_includes.h"
  11. #include "base/strings/string_number_conversions.h"
  12. #include "base/trace_event/trace_event.h"
  13. #include "content/public/renderer/render_frame.h"
  14. #include "content/public/renderer/render_view.h"
  15. #include "ipc/ipc_message_macros.h"
  16. #include "native_mate/dictionary.h"
  17. #include "net/base/net_module.h"
  18. #include "net/grit/net_resources.h"
  19. #include "third_party/WebKit/public/web/WebDocument.h"
  20. #include "third_party/WebKit/public/web/WebDraggableRegion.h"
  21. #include "third_party/WebKit/public/web/WebElement.h"
  22. #include "third_party/WebKit/public/web/WebKit.h"
  23. #include "third_party/WebKit/public/web/WebLocalFrame.h"
  24. #include "third_party/WebKit/public/web/WebScriptSource.h"
  25. #include "ui/base/resource/resource_bundle.h"
  26. namespace atom {
  27. namespace {
  28. bool GetIPCObject(v8::Isolate* isolate,
  29. v8::Local<v8::Context> context,
  30. v8::Local<v8::Object>* ipc) {
  31. v8::Local<v8::String> key = mate::StringToV8(isolate, "ipc");
  32. v8::Local<v8::Private> privateKey = v8::Private::ForApi(isolate, key);
  33. v8::Local<v8::Object> global_object = context->Global();
  34. v8::Local<v8::Value> value;
  35. if (!global_object->GetPrivate(context, privateKey).ToLocal(&value))
  36. return false;
  37. if (value.IsEmpty() || !value->IsObject())
  38. return false;
  39. *ipc = value->ToObject();
  40. return true;
  41. }
  42. std::vector<v8::Local<v8::Value>> ListValueToVector(
  43. v8::Isolate* isolate,
  44. const base::ListValue& list) {
  45. v8::Local<v8::Value> array = mate::ConvertToV8(isolate, list);
  46. std::vector<v8::Local<v8::Value>> result;
  47. mate::ConvertFromV8(isolate, array, &result);
  48. return result;
  49. }
  50. base::StringPiece NetResourceProvider(int key) {
  51. if (key == IDR_DIR_HEADER_HTML) {
  52. base::StringPiece html_data =
  53. ui::ResourceBundle::GetSharedInstance().GetRawDataResource(
  54. IDR_DIR_HEADER_HTML);
  55. return html_data;
  56. }
  57. return base::StringPiece();
  58. }
  59. } // namespace
  60. AtomRenderFrameObserver::AtomRenderFrameObserver(
  61. content::RenderFrame* frame,
  62. RendererClientBase* renderer_client)
  63. : content::RenderFrameObserver(frame),
  64. render_frame_(frame),
  65. renderer_client_(renderer_client) {
  66. // Initialise resource for directory listing.
  67. net::NetModule::SetResourceProvider(NetResourceProvider);
  68. }
  69. void AtomRenderFrameObserver::DidClearWindowObject() {
  70. renderer_client_->DidClearWindowObject(render_frame_);
  71. }
  72. void AtomRenderFrameObserver::DidCreateDocumentElement() {
  73. document_created_ = true;
  74. }
  75. void AtomRenderFrameObserver::DidCreateScriptContext(
  76. v8::Handle<v8::Context> context,
  77. int world_id) {
  78. if (ShouldNotifyClient(world_id))
  79. renderer_client_->DidCreateScriptContext(context, render_frame_);
  80. if (renderer_client_->isolated_world() && IsMainWorld(world_id) &&
  81. render_frame_->IsMainFrame()) {
  82. CreateIsolatedWorldContext();
  83. renderer_client_->SetupMainWorldOverrides(context);
  84. }
  85. }
  86. void AtomRenderFrameObserver::DraggableRegionsChanged() {
  87. blink::WebVector<blink::WebDraggableRegion> webregions =
  88. render_frame_->GetWebFrame()->GetDocument().DraggableRegions();
  89. std::vector<DraggableRegion> regions;
  90. for (auto& webregion : webregions) {
  91. DraggableRegion region;
  92. render_frame_->GetRenderView()->ConvertViewportToWindowViaWidget(
  93. &webregion.bounds);
  94. region.bounds = webregion.bounds;
  95. region.draggable = webregion.draggable;
  96. regions.push_back(region);
  97. }
  98. Send(new AtomFrameHostMsg_UpdateDraggableRegions(routing_id(), regions));
  99. }
  100. void AtomRenderFrameObserver::WillReleaseScriptContext(
  101. v8::Local<v8::Context> context,
  102. int world_id) {
  103. if (ShouldNotifyClient(world_id))
  104. renderer_client_->WillReleaseScriptContext(context, render_frame_);
  105. }
  106. void AtomRenderFrameObserver::OnDestruct() {
  107. delete this;
  108. }
  109. void AtomRenderFrameObserver::CreateIsolatedWorldContext() {
  110. auto* frame = render_frame_->GetWebFrame();
  111. // This maps to the name shown in the context combo box in the Console tab
  112. // of the dev tools.
  113. frame->SetIsolatedWorldHumanReadableName(
  114. World::ISOLATED_WORLD,
  115. blink::WebString::FromUTF8("Electron Isolated Context"));
  116. // Setup document's origin policy in isolated world
  117. frame->SetIsolatedWorldSecurityOrigin(
  118. World::ISOLATED_WORLD, frame->GetDocument().GetSecurityOrigin());
  119. // Create initial script context in isolated world
  120. blink::WebScriptSource source("void 0");
  121. frame->ExecuteScriptInIsolatedWorld(World::ISOLATED_WORLD, &source, 1);
  122. }
  123. bool AtomRenderFrameObserver::IsMainWorld(int world_id) {
  124. return world_id == World::MAIN_WORLD;
  125. }
  126. bool AtomRenderFrameObserver::IsIsolatedWorld(int world_id) {
  127. return world_id == World::ISOLATED_WORLD;
  128. }
  129. bool AtomRenderFrameObserver::ShouldNotifyClient(int world_id) {
  130. if (renderer_client_->isolated_world() && render_frame_->IsMainFrame())
  131. return IsIsolatedWorld(world_id);
  132. else
  133. return IsMainWorld(world_id);
  134. }
  135. bool AtomRenderFrameObserver::OnMessageReceived(const IPC::Message& message) {
  136. bool handled = true;
  137. IPC_BEGIN_MESSAGE_MAP(AtomRenderFrameObserver, message)
  138. IPC_MESSAGE_HANDLER(AtomFrameMsg_Message, OnBrowserMessage)
  139. IPC_MESSAGE_UNHANDLED(handled = false)
  140. IPC_END_MESSAGE_MAP()
  141. return handled;
  142. }
  143. void AtomRenderFrameObserver::OnBrowserMessage(bool send_to_all,
  144. const base::string16& channel,
  145. const base::ListValue& args) {
  146. // Don't handle browser messages before document element is created.
  147. // When we receive a message from the browser, we try to transfer it
  148. // to a web page, and when we do that Blink creates an empty
  149. // document element if it hasn't been created yet, and it makes our init
  150. // script to run while `window.location` is still "about:blank".
  151. if (!document_created_)
  152. return;
  153. blink::WebLocalFrame* frame = render_frame_->GetWebFrame();
  154. if (!frame || !render_frame_->IsMainFrame())
  155. return;
  156. EmitIPCEvent(frame, channel, args);
  157. // Also send the message to all sub-frames.
  158. if (send_to_all) {
  159. for (blink::WebFrame* child = frame->FirstChild(); child;
  160. child = child->NextSibling())
  161. if (child->IsWebLocalFrame()) {
  162. EmitIPCEvent(child->ToWebLocalFrame(), channel, args);
  163. }
  164. }
  165. }
  166. void AtomRenderFrameObserver::EmitIPCEvent(blink::WebLocalFrame* frame,
  167. const base::string16& channel,
  168. const base::ListValue& args) {
  169. if (!frame)
  170. return;
  171. v8::Isolate* isolate = blink::MainThreadIsolate();
  172. v8::HandleScope handle_scope(isolate);
  173. v8::Local<v8::Context> context = renderer_client_->GetContext(frame, isolate);
  174. v8::Context::Scope context_scope(context);
  175. // Only emit IPC event for context with node integration.
  176. node::Environment* env = node::Environment::GetCurrent(context);
  177. if (!env)
  178. return;
  179. v8::Local<v8::Object> ipc;
  180. if (GetIPCObject(isolate, context, &ipc)) {
  181. TRACE_EVENT0("devtools.timeline", "FunctionCall");
  182. auto args_vector = ListValueToVector(isolate, args);
  183. // Insert the Event object, event.sender is ipc.
  184. mate::Dictionary event = mate::Dictionary::CreateEmpty(isolate);
  185. event.Set("sender", ipc);
  186. args_vector.insert(args_vector.begin(), event.GetHandle());
  187. mate::EmitEvent(isolate, ipc, channel, args_vector);
  188. }
  189. }
  190. } // namespace atom