atom_renderer_client.cc 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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_natives.h" // NOLINT: This file is generated with js2c
  8. #include "atom/common/api/atom_bindings.h"
  9. #include "atom/common/api/event_emitter_caller.h"
  10. #include "atom/common/asar/asar_util.h"
  11. #include "atom/common/atom_constants.h"
  12. #include "atom/common/node_bindings.h"
  13. #include "atom/common/options_switches.h"
  14. #include "atom/renderer/api/atom_api_renderer_ipc.h"
  15. #include "atom/renderer/atom_render_frame_observer.h"
  16. #include "atom/renderer/atom_render_view_observer.h"
  17. #include "atom/renderer/node_array_buffer_bridge.h"
  18. #include "atom/renderer/web_worker_observer.h"
  19. #include "base/command_line.h"
  20. #include "content/public/renderer/render_frame.h"
  21. #include "native_mate/dictionary.h"
  22. #include "third_party/WebKit/public/web/WebDocument.h"
  23. #include "third_party/WebKit/public/web/WebLocalFrame.h"
  24. #include "atom/common/node_includes.h"
  25. namespace atom {
  26. namespace {
  27. bool IsDevToolsExtension(content::RenderFrame* render_frame) {
  28. return static_cast<GURL>(render_frame->GetWebFrame()->document().url())
  29. .SchemeIs("chrome-extension");
  30. }
  31. } // namespace
  32. AtomRendererClient::AtomRendererClient()
  33. : node_integration_initialized_(false),
  34. node_bindings_(NodeBindings::Create(NodeBindings::RENDERER)),
  35. atom_bindings_(new AtomBindings(uv_default_loop())) {
  36. isolated_world_ = base::CommandLine::ForCurrentProcess()->HasSwitch(
  37. switches::kContextIsolation);
  38. }
  39. AtomRendererClient::~AtomRendererClient() {
  40. asar::ClearArchives();
  41. }
  42. void AtomRendererClient::RenderThreadStarted() {
  43. OverrideNodeArrayBuffer();
  44. RendererClientBase::RenderThreadStarted();
  45. }
  46. void AtomRendererClient::RenderFrameCreated(
  47. content::RenderFrame* render_frame) {
  48. RendererClientBase::RenderFrameCreated(render_frame);
  49. }
  50. void AtomRendererClient::RenderViewCreated(content::RenderView* render_view) {
  51. new AtomRenderViewObserver(render_view, this);
  52. RendererClientBase::RenderViewCreated(render_view);
  53. }
  54. void AtomRendererClient::RunScriptsAtDocumentStart(
  55. content::RenderFrame* render_frame) {
  56. // Inform the document start pharse.
  57. node::Environment* env = node_bindings_->uv_env();
  58. if (env) {
  59. v8::HandleScope handle_scope(env->isolate());
  60. mate::EmitEvent(env->isolate(), env->process_object(), "document-start");
  61. }
  62. }
  63. void AtomRendererClient::RunScriptsAtDocumentEnd(
  64. content::RenderFrame* render_frame) {
  65. // Inform the document end pharse.
  66. node::Environment* env = node_bindings_->uv_env();
  67. if (env) {
  68. v8::HandleScope handle_scope(env->isolate());
  69. mate::EmitEvent(env->isolate(), env->process_object(), "document-end");
  70. }
  71. }
  72. void AtomRendererClient::DidCreateScriptContext(
  73. v8::Handle<v8::Context> context, content::RenderFrame* render_frame) {
  74. // Only allow node integration for the main frame, unless it is a devtools
  75. // extension page.
  76. if (!render_frame->IsMainFrame() && !IsDevToolsExtension(render_frame))
  77. return;
  78. // Prepare the node bindings.
  79. if (!node_integration_initialized_) {
  80. node_integration_initialized_ = true;
  81. node_bindings_->Initialize();
  82. node_bindings_->PrepareMessageLoop();
  83. }
  84. // Setup node environment for each window.
  85. node::Environment* env = node_bindings_->CreateEnvironment(context);
  86. // Add Electron extended APIs.
  87. atom_bindings_->BindTo(env->isolate(), env->process_object());
  88. AddRenderBindings(env->isolate(), env->process_object());
  89. // Load everything.
  90. node_bindings_->LoadEnvironment(env);
  91. if (node_bindings_->uv_env() == nullptr) {
  92. // Make uv loop being wrapped by window context.
  93. node_bindings_->set_uv_env(env);
  94. // Give the node loop a run to make sure everything is ready.
  95. node_bindings_->RunMessageLoop();
  96. }
  97. }
  98. void AtomRendererClient::WillReleaseScriptContext(
  99. v8::Handle<v8::Context> context, content::RenderFrame* render_frame) {
  100. // Only allow node integration for the main frame, unless it is a devtools
  101. // extension page.
  102. if (!render_frame->IsMainFrame() && !IsDevToolsExtension(render_frame))
  103. return;
  104. node::Environment* env = node::Environment::GetCurrent(context);
  105. if (env)
  106. mate::EmitEvent(env->isolate(), env->process_object(), "exit");
  107. // The main frame may be replaced.
  108. if (env == node_bindings_->uv_env())
  109. node_bindings_->set_uv_env(nullptr);
  110. // Destroy the node environment.
  111. // This is disabled because pending async tasks may still use the environment
  112. // and would cause crashes later. Node does not seem to clear all async tasks
  113. // when the environment is destroyed.
  114. // node::FreeEnvironment(env);
  115. // AtomBindings is tracking node environments.
  116. atom_bindings_->EnvironmentDestroyed(env);
  117. }
  118. bool AtomRendererClient::ShouldFork(blink::WebLocalFrame* frame,
  119. const GURL& url,
  120. const std::string& http_method,
  121. bool is_initial_navigation,
  122. bool is_server_redirect,
  123. bool* send_referrer) {
  124. // Handle all the navigations and reloads in browser.
  125. // FIXME We only support GET here because http method will be ignored when
  126. // the OpenURLFromTab is triggered, which means form posting would not work,
  127. // we should solve this by patching Chromium in future.
  128. *send_referrer = true;
  129. return http_method == "GET";
  130. }
  131. void AtomRendererClient::DidInitializeWorkerContextOnWorkerThread(
  132. v8::Local<v8::Context> context) {
  133. if (base::CommandLine::ForCurrentProcess()->HasSwitch(
  134. switches::kNodeIntegrationInWorker)) {
  135. WebWorkerObserver::GetCurrent()->ContextCreated(context);
  136. }
  137. }
  138. void AtomRendererClient::WillDestroyWorkerContextOnWorkerThread(
  139. v8::Local<v8::Context> context) {
  140. if (base::CommandLine::ForCurrentProcess()->HasSwitch(
  141. switches::kNodeIntegrationInWorker)) {
  142. WebWorkerObserver::GetCurrent()->ContextWillDestroy(context);
  143. }
  144. }
  145. v8::Local<v8::Context> AtomRendererClient::GetContext(
  146. blink::WebFrame* frame, v8::Isolate* isolate) {
  147. if (isolated_world())
  148. return frame->worldScriptContext(
  149. isolate, World::ISOLATED_WORLD, ExtensionGroup::MAIN_GROUP);
  150. else
  151. return frame->mainWorldScriptContext();
  152. }
  153. void AtomRendererClient::SetupMainWorldOverrides(
  154. v8::Handle<v8::Context> context) {
  155. // Setup window overrides in the main world context
  156. v8::Isolate* isolate = context->GetIsolate();
  157. // Wrap the bundle into a function that receives the binding object as
  158. // an argument.
  159. std::string bundle(node::isolated_bundle_data,
  160. node::isolated_bundle_data + sizeof(node::isolated_bundle_data));
  161. std::string wrapper = "(function (binding, require) {\n" + bundle + "\n})";
  162. auto script = v8::Script::Compile(
  163. mate::ConvertToV8(isolate, wrapper)->ToString());
  164. auto func = v8::Handle<v8::Function>::Cast(
  165. script->Run(context).ToLocalChecked());
  166. auto binding = v8::Object::New(isolate);
  167. api::Initialize(binding, v8::Null(isolate), context, nullptr);
  168. // Pass in CLI flags needed to setup window
  169. base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
  170. mate::Dictionary dict(isolate, binding);
  171. if (command_line->HasSwitch(switches::kGuestInstanceID))
  172. dict.Set(options::kGuestInstanceID,
  173. command_line->GetSwitchValueASCII(switches::kGuestInstanceID));
  174. if (command_line->HasSwitch(switches::kOpenerID))
  175. dict.Set(options::kOpenerID,
  176. command_line->GetSwitchValueASCII(switches::kOpenerID));
  177. dict.Set("hiddenPage", command_line->HasSwitch(switches::kHiddenPage));
  178. v8::Local<v8::Value> args[] = { binding };
  179. ignore_result(func->Call(context, v8::Null(isolate), 1, args));
  180. }
  181. } // namespace atom