renderer_client_base.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. #ifndef ELECTRON_SHELL_RENDERER_RENDERER_CLIENT_BASE_H_
  5. #define ELECTRON_SHELL_RENDERER_RENDERER_CLIENT_BASE_H_
  6. #include <memory>
  7. #include <string>
  8. #include "content/public/renderer/content_renderer_client.h"
  9. #include "electron/buildflags/buildflags.h"
  10. #if BUILDFLAG(ENABLE_BUILTIN_SPELLCHECKER)
  11. #include "services/service_manager/public/cpp/binder_registry.h"
  12. #include "services/service_manager/public/cpp/local_interface_provider.h"
  13. class SpellCheck;
  14. #endif
  15. namespace blink {
  16. class WebLocalFrame;
  17. }
  18. namespace gin_helper {
  19. class Dictionary;
  20. }
  21. #if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
  22. namespace extensions {
  23. class ExtensionsClient;
  24. }
  25. #endif
  26. namespace electron {
  27. #if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
  28. class ElectronExtensionsRendererClient;
  29. #endif
  30. class RendererClientBase : public content::ContentRendererClient
  31. #if BUILDFLAG(ENABLE_BUILTIN_SPELLCHECKER)
  32. ,
  33. public service_manager::LocalInterfaceProvider
  34. #endif
  35. {
  36. public:
  37. RendererClientBase();
  38. ~RendererClientBase() override;
  39. static RendererClientBase* Get();
  40. #if BUILDFLAG(ENABLE_BUILTIN_SPELLCHECKER)
  41. // service_manager::LocalInterfaceProvider implementation.
  42. void GetInterface(const std::string& name,
  43. mojo::ScopedMessagePipeHandle interface_pipe) override;
  44. #endif
  45. virtual void DidCreateScriptContext(v8::Local<v8::Context> context,
  46. content::RenderFrame* render_frame) = 0;
  47. virtual void WillReleaseScriptContext(v8::Local<v8::Context> context,
  48. content::RenderFrame* render_frame) = 0;
  49. virtual void DidClearWindowObject(content::RenderFrame* render_frame);
  50. virtual void SetupMainWorldOverrides(v8::Local<v8::Context> context,
  51. content::RenderFrame* render_frame);
  52. std::unique_ptr<blink::WebPrescientNetworking> CreatePrescientNetworking(
  53. content::RenderFrame* render_frame) override;
  54. // Get the context that the Electron API is running in.
  55. v8::Local<v8::Context> GetContext(blink::WebLocalFrame* frame,
  56. v8::Isolate* isolate) const;
  57. static void AllowGuestViewElementDefinition(
  58. v8::Isolate* isolate,
  59. v8::Local<v8::Object> context,
  60. v8::Local<v8::Function> register_cb);
  61. bool IsWebViewFrame(v8::Local<v8::Context> context,
  62. content::RenderFrame* render_frame) const;
  63. #if BUILDFLAG(ENABLE_BUILTIN_SPELLCHECKER)
  64. SpellCheck* GetSpellCheck() { return spellcheck_.get(); }
  65. #endif
  66. protected:
  67. void BindProcess(v8::Isolate* isolate,
  68. gin_helper::Dictionary* process,
  69. content::RenderFrame* render_frame);
  70. bool ShouldLoadPreload(v8::Local<v8::Context> context,
  71. content::RenderFrame* render_frame) const;
  72. // content::ContentRendererClient:
  73. void RenderThreadStarted() override;
  74. void ExposeInterfacesToBrowser(mojo::BinderMap* binders) override;
  75. void RenderFrameCreated(content::RenderFrame*) override;
  76. bool OverrideCreatePlugin(content::RenderFrame* render_frame,
  77. const blink::WebPluginParams& params,
  78. blink::WebPlugin** plugin) override;
  79. void DidSetUserAgent(const std::string& user_agent) override;
  80. bool IsPluginHandledExternally(content::RenderFrame* render_frame,
  81. const blink::WebElement& plugin_element,
  82. const GURL& original_url,
  83. const std::string& mime_type) override;
  84. v8::Local<v8::Object> GetScriptableObject(
  85. const blink::WebElement& plugin_element,
  86. v8::Isolate* isolate) override;
  87. void RunScriptsAtDocumentStart(content::RenderFrame* render_frame) override;
  88. void RunScriptsAtDocumentEnd(content::RenderFrame* render_frame) override;
  89. void RunScriptsAtDocumentIdle(content::RenderFrame* render_frame) override;
  90. bool AllowScriptExtensionForServiceWorker(
  91. const url::Origin& script_origin) override;
  92. void DidInitializeServiceWorkerContextOnWorkerThread(
  93. blink::WebServiceWorkerContextProxy* context_proxy,
  94. const GURL& service_worker_scope,
  95. const GURL& script_url) override;
  96. void WillEvaluateServiceWorkerOnWorkerThread(
  97. blink::WebServiceWorkerContextProxy* context_proxy,
  98. v8::Local<v8::Context> v8_context,
  99. int64_t service_worker_version_id,
  100. const GURL& service_worker_scope,
  101. const GURL& script_url,
  102. const blink::ServiceWorkerToken& service_worker_token) override;
  103. void DidStartServiceWorkerContextOnWorkerThread(
  104. int64_t service_worker_version_id,
  105. const GURL& service_worker_scope,
  106. const GURL& script_url) override;
  107. void WillDestroyServiceWorkerContextOnWorkerThread(
  108. v8::Local<v8::Context> context,
  109. int64_t service_worker_version_id,
  110. const GURL& service_worker_scope,
  111. const GURL& script_url) override;
  112. void WebViewCreated(blink::WebView* web_view,
  113. bool was_created_by_renderer,
  114. const url::Origin* outermost_origin) override;
  115. private:
  116. #if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
  117. std::unique_ptr<extensions::ExtensionsClient> extensions_client_;
  118. std::unique_ptr<ElectronExtensionsRendererClient> extensions_renderer_client_;
  119. #endif
  120. std::string renderer_client_id_;
  121. // An increasing ID used for identifying an V8 context in this process.
  122. int64_t next_context_id_ = 0;
  123. #if BUILDFLAG(ENABLE_BUILTIN_SPELLCHECKER)
  124. std::unique_ptr<SpellCheck> spellcheck_;
  125. #endif
  126. };
  127. } // namespace electron
  128. #endif // ELECTRON_SHELL_RENDERER_RENDERER_CLIENT_BASE_H_