renderer_client_base.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 SHELL_RENDERER_RENDERER_CLIENT_BASE_H_
  5. #define SHELL_RENDERER_RENDERER_CLIENT_BASE_H_
  6. #include <memory>
  7. #include <string>
  8. #include <vector>
  9. #include "content/public/renderer/content_renderer_client.h"
  10. #include "electron/buildflags/buildflags.h"
  11. #include "printing/buildflags/buildflags.h"
  12. #include "third_party/blink/public/web/web_local_frame.h"
  13. // In SHARED_INTERMEDIATE_DIR.
  14. #include "widevine_cdm_version.h" // NOLINT(build/include_directory)
  15. #if defined(WIDEVINE_CDM_AVAILABLE)
  16. #include "chrome/renderer/media/chrome_key_systems_provider.h" // nogncheck
  17. #endif
  18. #if BUILDFLAG(ENABLE_PDF_VIEWER)
  19. #include "chrome/renderer/pepper/chrome_pdf_print_client.h" // nogncheck
  20. #endif // BUILDFLAG(ENABLE_PDF_VIEWER)
  21. #if BUILDFLAG(ENABLE_BUILTIN_SPELLCHECKER)
  22. #include "services/service_manager/public/cpp/binder_registry.h"
  23. #include "services/service_manager/public/cpp/local_interface_provider.h"
  24. class SpellCheck;
  25. #endif
  26. #if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
  27. namespace extensions {
  28. class ExtensionsClient;
  29. }
  30. namespace content {
  31. struct WebPluginInfo;
  32. }
  33. #endif
  34. namespace guest_view {
  35. class GuestViewContainer;
  36. }
  37. namespace electron {
  38. #if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
  39. class ElectronExtensionsRendererClient;
  40. #endif
  41. class RendererClientBase : public content::ContentRendererClient
  42. #if BUILDFLAG(ENABLE_BUILTIN_SPELLCHECKER)
  43. ,
  44. public service_manager::LocalInterfaceProvider
  45. #endif
  46. {
  47. public:
  48. RendererClientBase();
  49. ~RendererClientBase() override;
  50. #if BUILDFLAG(ENABLE_BUILTIN_SPELLCHECKER)
  51. // service_manager::LocalInterfaceProvider implementation.
  52. void GetInterface(const std::string& name,
  53. mojo::ScopedMessagePipeHandle request_handle) override;
  54. #endif
  55. virtual void DidCreateScriptContext(v8::Handle<v8::Context> context,
  56. content::RenderFrame* render_frame);
  57. virtual void WillReleaseScriptContext(v8::Handle<v8::Context> context,
  58. content::RenderFrame* render_frame) = 0;
  59. virtual void DidClearWindowObject(content::RenderFrame* render_frame);
  60. virtual void SetupMainWorldOverrides(v8::Handle<v8::Context> context,
  61. content::RenderFrame* render_frame) = 0;
  62. virtual void SetupExtensionWorldOverrides(v8::Handle<v8::Context> context,
  63. content::RenderFrame* render_frame,
  64. int world_id) = 0;
  65. std::unique_ptr<blink::WebPrescientNetworking> CreatePrescientNetworking(
  66. content::RenderFrame* render_frame) override;
  67. bool isolated_world() const { return isolated_world_; }
  68. // Get the context that the Electron API is running in.
  69. v8::Local<v8::Context> GetContext(blink::WebLocalFrame* frame,
  70. v8::Isolate* isolate) const;
  71. // Executes a given v8 Script
  72. static v8::Local<v8::Value> RunScript(v8::Local<v8::Context> context,
  73. v8::Local<v8::String> source);
  74. // v8Util.getHiddenValue(window.frameElement, 'internal')
  75. bool IsWebViewFrame(v8::Handle<v8::Context> context,
  76. content::RenderFrame* render_frame) const;
  77. #if BUILDFLAG(ENABLE_BUILTIN_SPELLCHECKER)
  78. SpellCheck* GetSpellCheck() { return spellcheck_.get(); }
  79. #endif
  80. protected:
  81. void AddRenderBindings(v8::Isolate* isolate,
  82. v8::Local<v8::Object> binding_object);
  83. // content::ContentRendererClient:
  84. void RenderThreadStarted() override;
  85. void ExposeInterfacesToBrowser(mojo::BinderMap* binders) override;
  86. void RenderFrameCreated(content::RenderFrame*) override;
  87. bool OverrideCreatePlugin(content::RenderFrame* render_frame,
  88. const blink::WebPluginParams& params,
  89. blink::WebPlugin** plugin) override;
  90. void AddSupportedKeySystems(
  91. std::vector<std::unique_ptr<::media::KeySystemProperties>>* key_systems)
  92. override;
  93. bool IsKeySystemsUpdateNeeded() override;
  94. void DidSetUserAgent(const std::string& user_agent) override;
  95. guest_view::GuestViewContainer* CreateBrowserPluginDelegate(
  96. content::RenderFrame* render_frame,
  97. const content::WebPluginInfo& info,
  98. const std::string& mime_type,
  99. const GURL& original_url);
  100. bool IsPluginHandledExternally(content::RenderFrame* render_frame,
  101. const blink::WebElement& plugin_element,
  102. const GURL& original_url,
  103. const std::string& mime_type) override;
  104. bool IsOriginIsolatedPepperPlugin(const base::FilePath& plugin_path) override;
  105. void RunScriptsAtDocumentStart(content::RenderFrame* render_frame) override;
  106. void RunScriptsAtDocumentEnd(content::RenderFrame* render_frame) override;
  107. void RunScriptsAtDocumentIdle(content::RenderFrame* render_frame) override;
  108. protected:
  109. #if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
  110. // app_shell embedders may need custom extensions client interfaces.
  111. // This class takes ownership of the returned object.
  112. virtual extensions::ExtensionsClient* CreateExtensionsClient();
  113. #endif
  114. private:
  115. #if BUILDFLAG(ENABLE_ELECTRON_EXTENSIONS)
  116. std::unique_ptr<extensions::ExtensionsClient> extensions_client_;
  117. std::unique_ptr<ElectronExtensionsRendererClient> extensions_renderer_client_;
  118. #endif
  119. #if defined(WIDEVINE_CDM_AVAILABLE)
  120. ChromeKeySystemsProvider key_systems_provider_;
  121. #endif
  122. bool isolated_world_;
  123. std::string renderer_client_id_;
  124. // An increasing ID used for indentifying an V8 context in this process.
  125. int64_t next_context_id_ = 0;
  126. #if BUILDFLAG(ENABLE_BUILTIN_SPELLCHECKER)
  127. std::unique_ptr<SpellCheck> spellcheck_;
  128. #endif
  129. #if BUILDFLAG(ENABLE_PDF_VIEWER)
  130. std::unique_ptr<ChromePDFPrintClient> pdf_print_client_;
  131. #endif
  132. };
  133. } // namespace electron
  134. #endif // SHELL_RENDERER_RENDERER_CLIENT_BASE_H_