atom_renderer_client.cc 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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/atom_renderer_client.h"
  5. #include <string>
  6. #include <vector>
  7. #include "atom/common/api/atom_bindings.h"
  8. #include "atom/common/api/event_emitter_caller.h"
  9. #include "atom/common/asar/asar_util.h"
  10. #include "atom/common/node_bindings.h"
  11. #include "atom/common/options_switches.h"
  12. #include "atom/renderer/atom_render_frame_observer.h"
  13. #include "atom/renderer/web_worker_observer.h"
  14. #include "base/command_line.h"
  15. #include "content/public/renderer/render_frame.h"
  16. #include "native_mate/dictionary.h"
  17. #include "third_party/blink/public/web/web_document.h"
  18. #include "third_party/blink/public/web/web_local_frame.h"
  19. #include "atom/common/node_includes.h"
  20. #include "third_party/electron_node/src/node_native_module.h"
  21. namespace atom {
  22. namespace {
  23. bool IsDevToolsExtension(content::RenderFrame* render_frame) {
  24. return static_cast<GURL>(render_frame->GetWebFrame()->GetDocument().Url())
  25. .SchemeIs("chrome-extension");
  26. }
  27. } // namespace
  28. AtomRendererClient::AtomRendererClient()
  29. : node_bindings_(NodeBindings::Create(NodeBindings::RENDERER)),
  30. atom_bindings_(new AtomBindings(uv_default_loop())) {}
  31. AtomRendererClient::~AtomRendererClient() {
  32. asar::ClearArchives();
  33. }
  34. void AtomRendererClient::RenderThreadStarted() {
  35. RendererClientBase::RenderThreadStarted();
  36. }
  37. void AtomRendererClient::RenderFrameCreated(
  38. content::RenderFrame* render_frame) {
  39. new AtomRenderFrameObserver(render_frame, this);
  40. RendererClientBase::RenderFrameCreated(render_frame);
  41. }
  42. void AtomRendererClient::RenderViewCreated(content::RenderView* render_view) {
  43. RendererClientBase::RenderViewCreated(render_view);
  44. }
  45. void AtomRendererClient::RunScriptsAtDocumentStart(
  46. content::RenderFrame* render_frame) {
  47. // Inform the document start pharse.
  48. v8::HandleScope handle_scope(v8::Isolate::GetCurrent());
  49. node::Environment* env = GetEnvironment(render_frame);
  50. if (env)
  51. mate::EmitEvent(env->isolate(), env->process_object(), "document-start");
  52. }
  53. void AtomRendererClient::RunScriptsAtDocumentEnd(
  54. content::RenderFrame* render_frame) {
  55. // Inform the document end pharse.
  56. v8::HandleScope handle_scope(v8::Isolate::GetCurrent());
  57. node::Environment* env = GetEnvironment(render_frame);
  58. if (env)
  59. mate::EmitEvent(env->isolate(), env->process_object(), "document-end");
  60. }
  61. void AtomRendererClient::DidCreateScriptContext(
  62. v8::Handle<v8::Context> context,
  63. content::RenderFrame* render_frame) {
  64. RendererClientBase::DidCreateScriptContext(context, render_frame);
  65. // TODO(zcbenz): Do not create Node environment if node integration is not
  66. // enabled.
  67. // Do not load node if we're aren't a main frame or a devtools extension
  68. // unless node support has been explicitly enabled for sub frames
  69. bool is_main_frame =
  70. render_frame->IsMainFrame() && !render_frame->GetWebFrame()->Opener();
  71. bool is_devtools = IsDevToolsExtension(render_frame);
  72. bool allow_node_in_subframes =
  73. base::CommandLine::ForCurrentProcess()->HasSwitch(
  74. switches::kNodeIntegrationInSubFrames);
  75. bool should_load_node =
  76. (is_main_frame || is_devtools || allow_node_in_subframes) &&
  77. !IsWebViewFrame(context, render_frame);
  78. if (!should_load_node) {
  79. return;
  80. }
  81. injected_frames_.insert(render_frame);
  82. // If this is the first environment we are creating, prepare the node
  83. // bindings.
  84. if (!node_integration_initialized_) {
  85. node_integration_initialized_ = true;
  86. node_bindings_->Initialize();
  87. node_bindings_->PrepareMessageLoop();
  88. }
  89. // Setup node tracing controller.
  90. if (!node::tracing::TraceEventHelper::GetAgent())
  91. node::tracing::TraceEventHelper::SetAgent(node::CreateAgent());
  92. // Setup node environment for each window.
  93. node::Environment* env = node_bindings_->CreateEnvironment(context);
  94. environments_.insert(env);
  95. // Add Electron extended APIs.
  96. atom_bindings_->BindTo(env->isolate(), env->process_object());
  97. AddRenderBindings(env->isolate(), env->process_object());
  98. mate::Dictionary process_dict(env->isolate(), env->process_object());
  99. process_dict.SetReadOnly("isMainFrame", render_frame->IsMainFrame());
  100. // Load everything.
  101. node_bindings_->LoadEnvironment(env);
  102. if (node_bindings_->uv_env() == nullptr) {
  103. // Make uv loop being wrapped by window context.
  104. node_bindings_->set_uv_env(env);
  105. // Give the node loop a run to make sure everything is ready.
  106. node_bindings_->RunMessageLoop();
  107. }
  108. }
  109. void AtomRendererClient::WillReleaseScriptContext(
  110. v8::Handle<v8::Context> context,
  111. content::RenderFrame* render_frame) {
  112. if (injected_frames_.find(render_frame) == injected_frames_.end())
  113. return;
  114. injected_frames_.erase(render_frame);
  115. node::Environment* env = node::Environment::GetCurrent(context);
  116. if (environments_.find(env) == environments_.end())
  117. return;
  118. environments_.erase(env);
  119. mate::EmitEvent(env->isolate(), env->process_object(), "exit");
  120. // The main frame may be replaced.
  121. if (env == node_bindings_->uv_env())
  122. node_bindings_->set_uv_env(nullptr);
  123. // Destroy the node environment. We only do this if node support has been
  124. // enabled for sub-frames to avoid a change-of-behavior / introduce crashes
  125. // for existing users.
  126. // TODO(MarshallOfSOund): Free the environment regardless of this switch
  127. if (base::CommandLine::ForCurrentProcess()->HasSwitch(
  128. switches::kNodeIntegrationInSubFrames))
  129. node::FreeEnvironment(env);
  130. // AtomBindings is tracking node environments.
  131. atom_bindings_->EnvironmentDestroyed(env);
  132. }
  133. bool AtomRendererClient::ShouldFork(blink::WebLocalFrame* frame,
  134. const GURL& url,
  135. const std::string& http_method,
  136. bool is_initial_navigation,
  137. bool is_server_redirect) {
  138. // Handle all the navigations and reloads in browser.
  139. // FIXME We only support GET here because http method will be ignored when
  140. // the OpenURLFromTab is triggered, which means form posting would not work,
  141. // we should solve this by patching Chromium in future.
  142. return http_method == "GET";
  143. }
  144. void AtomRendererClient::DidInitializeWorkerContextOnWorkerThread(
  145. v8::Local<v8::Context> context) {
  146. if (base::CommandLine::ForCurrentProcess()->HasSwitch(
  147. switches::kNodeIntegrationInWorker)) {
  148. WebWorkerObserver::GetCurrent()->ContextCreated(context);
  149. }
  150. }
  151. void AtomRendererClient::WillDestroyWorkerContextOnWorkerThread(
  152. v8::Local<v8::Context> context) {
  153. if (base::CommandLine::ForCurrentProcess()->HasSwitch(
  154. switches::kNodeIntegrationInWorker)) {
  155. WebWorkerObserver::GetCurrent()->ContextWillDestroy(context);
  156. }
  157. }
  158. void AtomRendererClient::SetupMainWorldOverrides(
  159. v8::Handle<v8::Context> context,
  160. content::RenderFrame* render_frame) {
  161. // Setup window overrides in the main world context
  162. // Wrap the bundle into a function that receives the isolatedWorld as
  163. // an argument.
  164. auto* isolate = context->GetIsolate();
  165. std::vector<v8::Local<v8::String>> isolated_bundle_params = {
  166. node::FIXED_ONE_BYTE_STRING(isolate, "nodeProcess"),
  167. node::FIXED_ONE_BYTE_STRING(isolate, "isolatedWorld")};
  168. std::vector<v8::Local<v8::Value>> isolated_bundle_args = {
  169. GetEnvironment(render_frame)->process_object(),
  170. GetContext(render_frame->GetWebFrame(), isolate)->Global()};
  171. node::per_process::native_module_loader.CompileAndCall(
  172. context, "electron/js2c/isolated_bundle", &isolated_bundle_params,
  173. &isolated_bundle_args, nullptr);
  174. }
  175. node::Environment* AtomRendererClient::GetEnvironment(
  176. content::RenderFrame* render_frame) const {
  177. if (injected_frames_.find(render_frame) == injected_frames_.end())
  178. return nullptr;
  179. v8::HandleScope handle_scope(v8::Isolate::GetCurrent());
  180. auto context =
  181. GetContext(render_frame->GetWebFrame(), v8::Isolate::GetCurrent());
  182. node::Environment* env = node::Environment::GetCurrent(context);
  183. if (environments_.find(env) == environments_.end())
  184. return nullptr;
  185. return env;
  186. }
  187. } // namespace atom