atom_renderer_client.cc 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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/api/atom_api_renderer_ipc.h"
  13. #include "atom/renderer/atom_render_frame_observer.h"
  14. #include "atom/renderer/web_worker_observer.h"
  15. #include "base/command_line.h"
  16. #include "content/public/renderer/render_frame.h"
  17. #include "native_mate/dictionary.h"
  18. #include "third_party/WebKit/public/web/WebDocument.h"
  19. #include "third_party/WebKit/public/web/WebLocalFrame.h"
  20. #include "atom/common/node_includes.h"
  21. #include "atom_natives.h" // NOLINT: This file is generated with js2c
  22. #include "tracing/trace_event.h"
  23. namespace atom {
  24. namespace {
  25. bool IsDevToolsExtension(content::RenderFrame* render_frame) {
  26. return static_cast<GURL>(render_frame->GetWebFrame()->GetDocument().Url())
  27. .SchemeIs("chrome-extension");
  28. }
  29. } // namespace
  30. AtomRendererClient::AtomRendererClient()
  31. : node_bindings_(NodeBindings::Create(NodeBindings::RENDERER)),
  32. atom_bindings_(new AtomBindings(uv_default_loop())) {}
  33. AtomRendererClient::~AtomRendererClient() {
  34. asar::ClearArchives();
  35. }
  36. void AtomRendererClient::RenderThreadStarted() {
  37. RendererClientBase::RenderThreadStarted();
  38. }
  39. void AtomRendererClient::RenderFrameCreated(
  40. content::RenderFrame* render_frame) {
  41. new AtomRenderFrameObserver(render_frame, this);
  42. RendererClientBase::RenderFrameCreated(render_frame);
  43. }
  44. void AtomRendererClient::RenderViewCreated(content::RenderView* render_view) {
  45. RendererClientBase::RenderViewCreated(render_view);
  46. }
  47. void AtomRendererClient::RunScriptsAtDocumentStart(
  48. content::RenderFrame* render_frame) {
  49. // Inform the document start pharse.
  50. v8::HandleScope handle_scope(v8::Isolate::GetCurrent());
  51. node::Environment* env = GetEnvironment(render_frame);
  52. if (env)
  53. mate::EmitEvent(env->isolate(), env->process_object(), "document-start");
  54. }
  55. void AtomRendererClient::RunScriptsAtDocumentEnd(
  56. content::RenderFrame* render_frame) {
  57. // Inform the document end pharse.
  58. v8::HandleScope handle_scope(v8::Isolate::GetCurrent());
  59. node::Environment* env = GetEnvironment(render_frame);
  60. if (env)
  61. mate::EmitEvent(env->isolate(), env->process_object(), "document-end");
  62. }
  63. void AtomRendererClient::DidCreateScriptContext(
  64. v8::Handle<v8::Context> context,
  65. content::RenderFrame* render_frame) {
  66. // Only allow node integration for the main frame, unless it is a devtools
  67. // extension page.
  68. if (!render_frame->IsMainFrame() && !IsDevToolsExtension(render_frame))
  69. return;
  70. injected_frames_.insert(render_frame);
  71. // Prepare the node bindings.
  72. if (!node_integration_initialized_) {
  73. node_integration_initialized_ = true;
  74. node_bindings_->Initialize();
  75. node_bindings_->PrepareMessageLoop();
  76. }
  77. // Setup node tracing controller.
  78. if (!node::tracing::TraceEventHelper::GetTracingController())
  79. node::tracing::TraceEventHelper::SetTracingController(
  80. new v8::TracingController());
  81. // Setup node environment for each window.
  82. node::Environment* env = node_bindings_->CreateEnvironment(context);
  83. environments_.insert(env);
  84. // Add Electron extended APIs.
  85. atom_bindings_->BindTo(env->isolate(), env->process_object());
  86. AddRenderBindings(env->isolate(), env->process_object());
  87. // Load everything.
  88. node_bindings_->LoadEnvironment(env);
  89. if (node_bindings_->uv_env() == nullptr) {
  90. // Make uv loop being wrapped by window context.
  91. node_bindings_->set_uv_env(env);
  92. // Give the node loop a run to make sure everything is ready.
  93. node_bindings_->RunMessageLoop();
  94. }
  95. }
  96. void AtomRendererClient::WillReleaseScriptContext(
  97. v8::Handle<v8::Context> context,
  98. content::RenderFrame* render_frame) {
  99. if (injected_frames_.find(render_frame) == injected_frames_.end())
  100. return;
  101. injected_frames_.erase(render_frame);
  102. node::Environment* env = node::Environment::GetCurrent(context);
  103. if (environments_.find(env) == environments_.end())
  104. return;
  105. environments_.erase(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. void AtomRendererClient::SetupMainWorldOverrides(
  146. v8::Handle<v8::Context> context) {
  147. // Setup window overrides in the main world context
  148. v8::Isolate* isolate = context->GetIsolate();
  149. // Wrap the bundle into a function that receives the binding object as
  150. // an argument.
  151. std::string left = "(function (binding, require) {\n";
  152. std::string right = "\n})";
  153. auto script = v8::Script::Compile(v8::String::Concat(
  154. mate::ConvertToV8(isolate, left)->ToString(),
  155. v8::String::Concat(node::isolated_bundle_value.ToStringChecked(isolate),
  156. mate::ConvertToV8(isolate, right)->ToString())));
  157. auto func =
  158. v8::Handle<v8::Function>::Cast(script->Run(context).ToLocalChecked());
  159. auto binding = v8::Object::New(isolate);
  160. api::Initialize(binding, v8::Null(isolate), context, nullptr);
  161. // Pass in CLI flags needed to setup window
  162. base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
  163. mate::Dictionary dict(isolate, binding);
  164. if (command_line->HasSwitch(switches::kGuestInstanceID))
  165. dict.Set(options::kGuestInstanceID,
  166. command_line->GetSwitchValueASCII(switches::kGuestInstanceID));
  167. if (command_line->HasSwitch(switches::kOpenerID))
  168. dict.Set(options::kOpenerID,
  169. command_line->GetSwitchValueASCII(switches::kOpenerID));
  170. dict.Set("hiddenPage", command_line->HasSwitch(switches::kHiddenPage));
  171. dict.Set(options::kNativeWindowOpen,
  172. command_line->HasSwitch(switches::kNativeWindowOpen));
  173. v8::Local<v8::Value> args[] = {binding};
  174. ignore_result(func->Call(context, v8::Null(isolate), 1, args));
  175. }
  176. node::Environment* AtomRendererClient::GetEnvironment(
  177. content::RenderFrame* render_frame) const {
  178. if (injected_frames_.find(render_frame) == injected_frames_.end())
  179. return nullptr;
  180. v8::HandleScope handle_scope(v8::Isolate::GetCurrent());
  181. auto context =
  182. GetContext(render_frame->GetWebFrame(), v8::Isolate::GetCurrent());
  183. node::Environment* env = node::Environment::GetCurrent(context);
  184. if (environments_.find(env) == environments_.end())
  185. return nullptr;
  186. return env;
  187. }
  188. } // namespace atom