renderer_client_base.cc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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/renderer_client_base.h"
  5. #include <memory>
  6. #include <string>
  7. #include <vector>
  8. #include "atom/common/color_util.h"
  9. #include "atom/common/native_mate_converters/value_converter.h"
  10. #include "atom/common/options_switches.h"
  11. #include "atom/renderer/atom_autofill_agent.h"
  12. #include "atom/renderer/atom_render_frame_observer.h"
  13. #include "atom/renderer/atom_render_view_observer.h"
  14. #include "atom/renderer/content_settings_observer.h"
  15. #include "atom/renderer/preferences_manager.h"
  16. #include "base/command_line.h"
  17. #include "base/strings/string_split.h"
  18. #include "base/strings/stringprintf.h"
  19. #include "content/public/common/content_constants.h"
  20. #include "content/public/common/content_switches.h"
  21. #include "content/public/renderer/render_frame.h"
  22. #include "content/public/renderer/render_view.h"
  23. #include "electron/buildflags/buildflags.h"
  24. #include "native_mate/dictionary.h"
  25. #include "printing/buildflags/buildflags.h"
  26. #include "third_party/blink/public/web/blink.h"
  27. #include "third_party/blink/public/web/web_custom_element.h" // NOLINT(build/include_alpha)
  28. #include "third_party/blink/public/web/web_frame_widget.h"
  29. #include "third_party/blink/public/web/web_plugin_params.h"
  30. #include "third_party/blink/public/web/web_script_source.h"
  31. #include "third_party/blink/public/web/web_security_policy.h"
  32. #include "third_party/blink/public/web/web_view.h"
  33. #include "third_party/blink/renderer/platform/weborigin/scheme_registry.h"
  34. #if defined(OS_MACOSX)
  35. #include "base/strings/sys_string_conversions.h"
  36. #endif
  37. #if defined(OS_WIN)
  38. #include <shlobj.h>
  39. #endif
  40. #if BUILDFLAG(ENABLE_PDF_VIEWER)
  41. #include "atom/common/atom_constants.h"
  42. #endif // BUILDFLAG(ENABLE_PDF_VIEWER)
  43. #if BUILDFLAG(ENABLE_PEPPER_FLASH)
  44. #include "chrome/renderer/pepper/pepper_helper.h"
  45. #endif // BUILDFLAG(ENABLE_PEPPER_FLASH)
  46. #if BUILDFLAG(ENABLE_TTS)
  47. #include "chrome/renderer/tts_dispatcher.h"
  48. #endif // BUILDFLAG(ENABLE_TTS)
  49. #if BUILDFLAG(ENABLE_PRINTING)
  50. #include "atom/renderer/printing/print_render_frame_helper_delegate.h"
  51. #include "components/printing/renderer/print_render_frame_helper.h"
  52. #include "printing/print_settings.h"
  53. #endif // BUILDFLAG(ENABLE_PRINTING)
  54. namespace atom {
  55. namespace {
  56. v8::Local<v8::Value> GetRenderProcessPreferences(
  57. const PreferencesManager* preferences_manager,
  58. v8::Isolate* isolate) {
  59. if (preferences_manager->preferences())
  60. return mate::ConvertToV8(isolate, *preferences_manager->preferences());
  61. else
  62. return v8::Null(isolate);
  63. }
  64. std::vector<std::string> ParseSchemesCLISwitch(base::CommandLine* command_line,
  65. const char* switch_name) {
  66. std::string custom_schemes = command_line->GetSwitchValueASCII(switch_name);
  67. return base::SplitString(custom_schemes, ",", base::TRIM_WHITESPACE,
  68. base::SPLIT_WANT_NONEMPTY);
  69. }
  70. void SetHiddenValue(v8::Handle<v8::Context> context,
  71. const base::StringPiece& key,
  72. v8::Local<v8::Value> value) {
  73. v8::Isolate* isolate = context->GetIsolate();
  74. v8::Local<v8::Private> privateKey =
  75. v8::Private::ForApi(isolate, mate::StringToV8(isolate, key));
  76. context->Global()->SetPrivate(context, privateKey, value);
  77. }
  78. } // namespace
  79. RendererClientBase::RendererClientBase() {
  80. auto* command_line = base::CommandLine::ForCurrentProcess();
  81. // Parse --standard-schemes=scheme1,scheme2
  82. std::vector<std::string> standard_schemes_list =
  83. ParseSchemesCLISwitch(command_line, switches::kStandardSchemes);
  84. for (const std::string& scheme : standard_schemes_list)
  85. url::AddStandardScheme(scheme.c_str(), url::SCHEME_WITH_HOST);
  86. isolated_world_ = base::CommandLine::ForCurrentProcess()->HasSwitch(
  87. switches::kContextIsolation);
  88. // We rely on the unique process host id which is notified to the
  89. // renderer process via command line switch from the content layer,
  90. // if this switch is removed from the content layer for some reason,
  91. // we should define our own.
  92. DCHECK(command_line->HasSwitch(::switches::kRendererClientId));
  93. renderer_client_id_ =
  94. command_line->GetSwitchValueASCII(::switches::kRendererClientId);
  95. }
  96. RendererClientBase::~RendererClientBase() {}
  97. void RendererClientBase::DidCreateScriptContext(
  98. v8::Handle<v8::Context> context,
  99. content::RenderFrame* render_frame) {
  100. // global.setHidden("contextId", `${processHostId}-${++next_context_id_}`)
  101. auto context_id = base::StringPrintf(
  102. "%s-%" PRId64, renderer_client_id_.c_str(), ++next_context_id_);
  103. v8::Isolate* isolate = context->GetIsolate();
  104. SetHiddenValue(context, "contextId", mate::ConvertToV8(isolate, context_id));
  105. auto* command_line = base::CommandLine::ForCurrentProcess();
  106. bool enableRemoteModule =
  107. !command_line->HasSwitch(switches::kDisableRemoteModule);
  108. SetHiddenValue(context, "enableRemoteModule",
  109. mate::ConvertToV8(isolate, enableRemoteModule));
  110. }
  111. void RendererClientBase::AddRenderBindings(
  112. v8::Isolate* isolate,
  113. v8::Local<v8::Object> binding_object) {
  114. mate::Dictionary dict(isolate, binding_object);
  115. dict.SetMethod(
  116. "getRenderProcessPreferences",
  117. base::Bind(GetRenderProcessPreferences, preferences_manager_.get()));
  118. }
  119. void RendererClientBase::RenderThreadStarted() {
  120. auto* command_line = base::CommandLine::ForCurrentProcess();
  121. blink::WebCustomElement::AddEmbedderCustomElementName("webview");
  122. blink::WebCustomElement::AddEmbedderCustomElementName("browserplugin");
  123. WTF::String extension_scheme("chrome-extension");
  124. // Extension resources are HTTP-like and safe to expose to the fetch API. The
  125. // rules for the fetch API are consistent with XHR.
  126. blink::SchemeRegistry::RegisterURLSchemeAsSupportingFetchAPI(
  127. extension_scheme);
  128. // Extension resources, when loaded as the top-level document, should bypass
  129. // Blink's strict first-party origin checks.
  130. blink::SchemeRegistry::RegisterURLSchemeAsFirstPartyWhenTopLevel(
  131. extension_scheme);
  132. // In Chrome we should set extension's origins to match the pages they can
  133. // work on, but in Electron currently we just let extensions do anything.
  134. blink::SchemeRegistry::RegisterURLSchemeAsSecure(extension_scheme);
  135. blink::SchemeRegistry::RegisterURLSchemeAsBypassingContentSecurityPolicy(
  136. extension_scheme);
  137. // Parse --secure-schemes=scheme1,scheme2
  138. std::vector<std::string> secure_schemes_list =
  139. ParseSchemesCLISwitch(command_line, switches::kSecureSchemes);
  140. for (const std::string& scheme : secure_schemes_list)
  141. blink::SchemeRegistry::RegisterURLSchemeAsSecure(
  142. WTF::String::FromUTF8(scheme.data(), scheme.length()));
  143. std::vector<std::string> fetch_enabled_schemes =
  144. ParseSchemesCLISwitch(command_line, switches::kFetchSchemes);
  145. for (const std::string& scheme : fetch_enabled_schemes) {
  146. blink::WebSecurityPolicy::RegisterURLSchemeAsSupportingFetchAPI(
  147. blink::WebString::FromASCII(scheme));
  148. }
  149. std::vector<std::string> service_worker_schemes =
  150. ParseSchemesCLISwitch(command_line, switches::kServiceWorkerSchemes);
  151. for (const std::string& scheme : service_worker_schemes)
  152. blink::WebSecurityPolicy::RegisterURLSchemeAsAllowingServiceWorkers(
  153. blink::WebString::FromASCII(scheme));
  154. std::vector<std::string> csp_bypassing_schemes =
  155. ParseSchemesCLISwitch(command_line, switches::kBypassCSPSchemes);
  156. for (const std::string& scheme : csp_bypassing_schemes)
  157. blink::SchemeRegistry::RegisterURLSchemeAsBypassingContentSecurityPolicy(
  158. WTF::String::FromUTF8(scheme.data(), scheme.length()));
  159. // Allow file scheme to handle service worker by default.
  160. // FIXME(zcbenz): Can this be moved elsewhere?
  161. blink::WebSecurityPolicy::RegisterURLSchemeAsAllowingServiceWorkers("file");
  162. blink::SchemeRegistry::RegisterURLSchemeAsSupportingFetchAPI("file");
  163. preferences_manager_.reset(new PreferencesManager);
  164. #if defined(OS_WIN)
  165. // Set ApplicationUserModelID in renderer process.
  166. base::string16 app_id =
  167. command_line->GetSwitchValueNative(switches::kAppUserModelId);
  168. if (!app_id.empty()) {
  169. SetCurrentProcessExplicitAppUserModelID(app_id.c_str());
  170. }
  171. #endif
  172. }
  173. void RendererClientBase::RenderFrameCreated(
  174. content::RenderFrame* render_frame) {
  175. #if defined(TOOLKIT_VIEWS)
  176. new AutofillAgent(render_frame);
  177. #endif
  178. #if BUILDFLAG(ENABLE_PEPPER_FLASH)
  179. new PepperHelper(render_frame);
  180. #endif
  181. new ContentSettingsObserver(render_frame);
  182. #if BUILDFLAG(ENABLE_PRINTING)
  183. new printing::PrintRenderFrameHelper(
  184. render_frame, std::make_unique<atom::PrintRenderFrameHelperDelegate>());
  185. #endif
  186. #if BUILDFLAG(ENABLE_PDF_VIEWER)
  187. // Allow access to file scheme from pdf viewer.
  188. blink::WebSecurityPolicy::AddOriginAccessWhitelistEntry(
  189. GURL(kPdfViewerUIOrigin), "file", "", true);
  190. #endif // BUILDFLAG(ENABLE_PDF_VIEWER)
  191. content::RenderView* render_view = render_frame->GetRenderView();
  192. if (render_frame->IsMainFrame() && render_view) {
  193. blink::WebView* webview = render_view->GetWebView();
  194. if (webview) {
  195. base::CommandLine* cmd = base::CommandLine::ForCurrentProcess();
  196. if (cmd->HasSwitch(switches::kGuestInstanceID)) { // webview.
  197. webview->SetBaseBackgroundColor(SK_ColorTRANSPARENT);
  198. } else { // normal window.
  199. std::string name = cmd->GetSwitchValueASCII(switches::kBackgroundColor);
  200. SkColor color =
  201. name.empty() ? SK_ColorTRANSPARENT : ParseHexColor(name);
  202. webview->SetBaseBackgroundColor(color);
  203. }
  204. }
  205. }
  206. }
  207. void RendererClientBase::RenderViewCreated(content::RenderView* render_view) {
  208. new AtomRenderViewObserver(render_view);
  209. }
  210. void RendererClientBase::DidClearWindowObject(
  211. content::RenderFrame* render_frame) {
  212. // Make sure every page will get a script context created.
  213. render_frame->GetWebFrame()->ExecuteScript(blink::WebScriptSource("void 0"));
  214. }
  215. std::unique_ptr<blink::WebSpeechSynthesizer>
  216. RendererClientBase::OverrideSpeechSynthesizer(
  217. blink::WebSpeechSynthesizerClient* client) {
  218. #if BUILDFLAG(ENABLE_TTS)
  219. return std::make_unique<TtsDispatcher>(client);
  220. #else
  221. return nullptr;
  222. #endif
  223. }
  224. bool RendererClientBase::OverrideCreatePlugin(
  225. content::RenderFrame* render_frame,
  226. const blink::WebPluginParams& params,
  227. blink::WebPlugin** plugin) {
  228. base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
  229. if (params.mime_type.Utf8() == content::kBrowserPluginMimeType ||
  230. #if BUILDFLAG(ENABLE_PDF_VIEWER)
  231. params.mime_type.Utf8() == kPdfPluginMimeType ||
  232. #endif // BUILDFLAG(ENABLE_PDF_VIEWER)
  233. command_line->HasSwitch(switches::kEnablePlugins))
  234. return false;
  235. *plugin = nullptr;
  236. return true;
  237. }
  238. void RendererClientBase::AddSupportedKeySystems(
  239. std::vector<std::unique_ptr<::media::KeySystemProperties>>* key_systems) {
  240. #if defined(WIDEVINE_CDM_AVAILABLE)
  241. key_systems_provider_.AddSupportedKeySystems(key_systems);
  242. #endif
  243. }
  244. bool RendererClientBase::IsKeySystemsUpdateNeeded() {
  245. #if defined(WIDEVINE_CDM_AVAILABLE)
  246. return key_systems_provider_.IsKeySystemsUpdateNeeded();
  247. #else
  248. return false;
  249. #endif
  250. }
  251. void RendererClientBase::DidSetUserAgent(const std::string& user_agent) {
  252. #if BUILDFLAG(ENABLE_PRINTING)
  253. printing::SetAgent(user_agent);
  254. #endif
  255. }
  256. v8::Local<v8::Context> RendererClientBase::GetContext(
  257. blink::WebLocalFrame* frame,
  258. v8::Isolate* isolate) const {
  259. if (isolated_world())
  260. return frame->WorldScriptContext(isolate, World::ISOLATED_WORLD);
  261. else
  262. return frame->MainWorldScriptContext();
  263. }
  264. v8::Local<v8::Value> RendererClientBase::RunScript(
  265. v8::Local<v8::Context> context,
  266. v8::Local<v8::String> source) {
  267. auto maybe_script = v8::Script::Compile(context, source);
  268. v8::Local<v8::Script> script;
  269. if (!maybe_script.ToLocal(&script))
  270. return v8::Local<v8::Value>();
  271. return script->Run(context).ToLocalChecked();
  272. }
  273. bool RendererClientBase::IsWebViewFrame(
  274. v8::Handle<v8::Context> context,
  275. content::RenderFrame* render_frame) const {
  276. auto* isolate = context->GetIsolate();
  277. if (render_frame->IsMainFrame())
  278. return false;
  279. mate::Dictionary window_dict(
  280. isolate, GetContext(render_frame->GetWebFrame(), isolate)->Global());
  281. v8::Local<v8::Object> frame_element;
  282. if (!window_dict.Get("frameElement", &frame_element))
  283. return false;
  284. mate::Dictionary frame_element_dict(isolate, frame_element);
  285. v8::Local<v8::Object> internal;
  286. if (!frame_element_dict.GetHidden("internal", &internal))
  287. return false;
  288. return !internal.IsEmpty();
  289. }
  290. } // namespace atom