renderer_client_base.cc 13 KB

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