atom_sandboxed_renderer_client.cc 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. // Copyright (c) 2016 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_sandboxed_renderer_client.h"
  5. #include "atom/common/api/api_messages.h"
  6. #include "atom/common/api/atom_bindings.h"
  7. #include "atom/common/native_mate_converters/string16_converter.h"
  8. #include "atom/common/native_mate_converters/v8_value_converter.h"
  9. #include "atom/common/native_mate_converters/value_converter.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 "base/command_line.h"
  15. #include "base/files/file_path.h"
  16. #include "chrome/renderer/printing/print_web_view_helper.h"
  17. #include "content/public/renderer/render_frame.h"
  18. #include "native_mate/dictionary.h"
  19. #include "third_party/WebKit/public/web/WebDocument.h"
  20. #include "third_party/WebKit/public/web/WebKit.h"
  21. #include "atom/common/node_includes.h"
  22. #include "atom_natives.h" // NOLINT: This file is generated with js2c
  23. namespace atom {
  24. namespace {
  25. const std::string kIpcKey = "ipcNative";
  26. const std::string kModuleCacheKey = "native-module-cache";
  27. bool IsDevTools(content::RenderFrame* render_frame) {
  28. return render_frame->GetWebFrame()->GetDocument().Url().ProtocolIs(
  29. "chrome-devtools");
  30. }
  31. v8::Local<v8::Object> GetModuleCache(v8::Isolate* isolate) {
  32. mate::Dictionary global(isolate, isolate->GetCurrentContext()->Global());
  33. v8::Local<v8::Value> cache;
  34. if (!global.GetHidden(kModuleCacheKey, &cache)) {
  35. cache = v8::Object::New(isolate);
  36. global.SetHidden(kModuleCacheKey, cache);
  37. }
  38. return cache->ToObject();
  39. }
  40. // adapted from node.cc
  41. v8::Local<v8::Value> GetBinding(v8::Isolate* isolate,
  42. v8::Local<v8::String> key,
  43. mate::Arguments* margs) {
  44. v8::Local<v8::Object> exports;
  45. std::string module_key = mate::V8ToString(key);
  46. mate::Dictionary cache(isolate, GetModuleCache(isolate));
  47. if (cache.Get(module_key.c_str(), &exports)) {
  48. return exports;
  49. }
  50. auto* mod = node::get_builtin_module(module_key.c_str());
  51. if (!mod) {
  52. char errmsg[1024];
  53. snprintf(errmsg, sizeof(errmsg), "No such module: %s", module_key.c_str());
  54. margs->ThrowError(errmsg);
  55. return exports;
  56. }
  57. exports = v8::Object::New(isolate);
  58. DCHECK_EQ(mod->nm_register_func, nullptr);
  59. DCHECK_NE(mod->nm_context_register_func, nullptr);
  60. mod->nm_context_register_func(exports, v8::Null(isolate),
  61. isolate->GetCurrentContext(), mod->nm_priv);
  62. cache.Set(module_key.c_str(), exports);
  63. return exports;
  64. }
  65. base::CommandLine::StringVector GetArgv() {
  66. return base::CommandLine::ForCurrentProcess()->argv();
  67. }
  68. void InitializeBindings(v8::Local<v8::Object> binding,
  69. v8::Local<v8::Context> context) {
  70. auto* isolate = context->GetIsolate();
  71. mate::Dictionary b(isolate, binding);
  72. b.SetMethod("get", GetBinding);
  73. b.SetMethod("crash", AtomBindings::Crash);
  74. b.SetMethod("hang", AtomBindings::Hang);
  75. b.SetMethod("getArgv", GetArgv);
  76. b.SetMethod("getProcessMemoryInfo", &AtomBindings::GetProcessMemoryInfo);
  77. b.SetMethod("getSystemMemoryInfo", &AtomBindings::GetSystemMemoryInfo);
  78. }
  79. class AtomSandboxedRenderFrameObserver : public AtomRenderFrameObserver {
  80. public:
  81. AtomSandboxedRenderFrameObserver(content::RenderFrame* render_frame,
  82. AtomSandboxedRendererClient* renderer_client)
  83. : AtomRenderFrameObserver(render_frame, renderer_client),
  84. v8_converter_(new atom::V8ValueConverter),
  85. renderer_client_(renderer_client) {
  86. v8_converter_->SetDisableNode(true);
  87. }
  88. protected:
  89. void EmitIPCEvent(blink::WebLocalFrame* frame,
  90. const base::string16& channel,
  91. const base::ListValue& args) override {
  92. if (!frame)
  93. return;
  94. auto* isolate = blink::MainThreadIsolate();
  95. v8::HandleScope handle_scope(isolate);
  96. auto context = frame->MainWorldScriptContext();
  97. v8::Context::Scope context_scope(context);
  98. v8::Local<v8::Value> argv[] = {mate::ConvertToV8(isolate, channel),
  99. v8_converter_->ToV8Value(&args, context)};
  100. renderer_client_->InvokeIpcCallback(
  101. context, "onMessage",
  102. std::vector<v8::Local<v8::Value>>(argv, argv + 2));
  103. }
  104. private:
  105. std::unique_ptr<atom::V8ValueConverter> v8_converter_;
  106. AtomSandboxedRendererClient* renderer_client_;
  107. DISALLOW_COPY_AND_ASSIGN(AtomSandboxedRenderFrameObserver);
  108. };
  109. } // namespace
  110. AtomSandboxedRendererClient::AtomSandboxedRendererClient() {
  111. // Explicitly register electron's builtin modules.
  112. NodeBindings::RegisterBuiltinModules();
  113. }
  114. AtomSandboxedRendererClient::~AtomSandboxedRendererClient() {}
  115. void AtomSandboxedRendererClient::RenderFrameCreated(
  116. content::RenderFrame* render_frame) {
  117. new AtomSandboxedRenderFrameObserver(render_frame, this);
  118. RendererClientBase::RenderFrameCreated(render_frame);
  119. }
  120. void AtomSandboxedRendererClient::RenderViewCreated(
  121. content::RenderView* render_view) {
  122. RendererClientBase::RenderViewCreated(render_view);
  123. }
  124. void AtomSandboxedRendererClient::DidCreateScriptContext(
  125. v8::Handle<v8::Context> context,
  126. content::RenderFrame* render_frame) {
  127. // Only allow preload for the main frame or
  128. // For devtools we still want to run the preload_bundle script
  129. if (!render_frame->IsMainFrame() && !IsDevTools(render_frame))
  130. return;
  131. base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
  132. base::FilePath preload_script_path =
  133. command_line->GetSwitchValuePath(switches::kPreloadScript);
  134. auto* isolate = context->GetIsolate();
  135. v8::HandleScope handle_scope(isolate);
  136. v8::Context::Scope context_scope(context);
  137. // Wrap the bundle into a function that receives the binding object and the
  138. // preload script path as arguments.
  139. std::string left = "(function(binding, preloadPath, require) {\n";
  140. std::string right = "\n})";
  141. // Compile the wrapper and run it to get the function object
  142. auto script = v8::Script::Compile(v8::String::Concat(
  143. mate::ConvertToV8(isolate, left)->ToString(),
  144. v8::String::Concat(node::preload_bundle_value.ToStringChecked(isolate),
  145. mate::ConvertToV8(isolate, right)->ToString())));
  146. auto func =
  147. v8::Handle<v8::Function>::Cast(script->Run(context).ToLocalChecked());
  148. // Create and initialize the binding object
  149. auto binding = v8::Object::New(isolate);
  150. InitializeBindings(binding, context);
  151. AddRenderBindings(isolate, binding);
  152. v8::Local<v8::Value> args[] = {
  153. binding, mate::ConvertToV8(isolate, preload_script_path.value())};
  154. // Execute the function with proper arguments
  155. ignore_result(func->Call(context, v8::Null(isolate), 2, args));
  156. }
  157. void AtomSandboxedRendererClient::WillReleaseScriptContext(
  158. v8::Handle<v8::Context> context,
  159. content::RenderFrame* render_frame) {
  160. // Only allow preload for the main frame
  161. if (!render_frame->IsMainFrame())
  162. return;
  163. auto* isolate = context->GetIsolate();
  164. v8::HandleScope handle_scope(isolate);
  165. v8::Context::Scope context_scope(context);
  166. InvokeIpcCallback(context, "onExit", std::vector<v8::Local<v8::Value>>());
  167. }
  168. void AtomSandboxedRendererClient::InvokeIpcCallback(
  169. v8::Handle<v8::Context> context,
  170. const std::string& callback_name,
  171. std::vector<v8::Handle<v8::Value>> args) {
  172. auto* isolate = context->GetIsolate();
  173. auto binding_key = mate::ConvertToV8(isolate, kIpcKey)->ToString();
  174. auto private_binding_key = v8::Private::ForApi(isolate, binding_key);
  175. auto global_object = context->Global();
  176. v8::Local<v8::Value> value;
  177. if (!global_object->GetPrivate(context, private_binding_key).ToLocal(&value))
  178. return;
  179. if (value.IsEmpty() || !value->IsObject())
  180. return;
  181. auto binding = value->ToObject();
  182. auto callback_key = mate::ConvertToV8(isolate, callback_name)->ToString();
  183. auto callback_value = binding->Get(callback_key);
  184. DCHECK(callback_value->IsFunction()); // set by sandboxed_renderer/init.js
  185. auto callback = v8::Handle<v8::Function>::Cast(callback_value);
  186. ignore_result(callback->Call(context, binding, args.size(), args.data()));
  187. }
  188. } // namespace atom