renderer_client_base.h 6.2 KB

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