1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
    2. // Use of this source code is governed by a BSD-style license that can be
    3. // found in the LICENSE file.
    4. #ifndef CONTENT_PUBLIC_APP_CONTENT_MAIN_DELEGATE_H_
    5. #define CONTENT_PUBLIC_APP_CONTENT_MAIN_DELEGATE_H_
    6. #include <memory>
    7. #include <string>
    8. #include <vector>
    9. #include "build/build_config.h"
    10. #include "content/common/content_export.h"
    11. #include "content/public/common/main_function_params.h"
    12. #include "third_party/abseil-cpp/absl/types/variant.h"
    13. namespace variations {
    14. class VariationsIdsProvider;
    15. }
    16. namespace content {
    17. class ContentBrowserClient;
    18. class ContentClient;
    19. class ContentGpuClient;
    20. class ContentRendererClient;
    21. class ContentUtilityClient;
    22. class ZygoteForkDelegate;
    23. class CONTENT_EXPORT ContentMainDelegate {
    24. public:
    25. virtual ~ContentMainDelegate() {}
    26. // Tells the embedder that the absolute basic startup has been done, i.e.
    27. // it's now safe to create singletons and check the command line. Return true
    28. // if the process should exit afterwards, and if so, |exit_code| should be
    29. // set. This is the place for embedder to do the things that must happen at
    30. // the start. Most of its startup code should be in the methods below.
    31. virtual bool BasicStartupComplete(int* exit_code);
    32. // This is where the embedder puts all of its startup code that needs to run
    33. // before the sandbox is engaged.
    34. virtual void PreSandboxStartup() {}
    35. // This is where the embedder can add startup code to run after the sandbox
    36. // has been initialized.
    37. virtual void SandboxInitialized(const std::string& process_type) {}
    38. // Asks the embedder to start a process. The embedder may return the
    39. // |main_function_params| back to decline the request and kick-off the
    40. // default behavior or return a non-negative exit code to indicate it handled
    41. // the request.
    42. virtual absl::variant<int, MainFunctionParams> RunProcess(
    43. const std::string& process_type,
    44. MainFunctionParams main_function_params);
    45. // Called right before the process exits.
    46. virtual void ProcessExiting(const std::string& process_type) {}
    47. #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
    48. // Tells the embedder that the zygote process is starting, and allows it to
    49. // specify one or more zygote delegates if it wishes by storing them in
    50. // |*delegates|.
    51. virtual void ZygoteStarting(
    52. std::vector<std::unique_ptr<ZygoteForkDelegate>>* delegates);
    53. // Called every time the zygote process forks.
    54. virtual void ZygoteForked() {}
    55. #endif // BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
    56. // Fatal errors during initialization are reported by this function, so that
    57. // the embedder can implement graceful exit by displaying some message and
    58. // returning initialization error code. Default behavior is CHECK(false).
    59. virtual int TerminateForFatalInitializationError();
    60. // Allows the embedder to prevent locking the scheme registry. The scheme
    61. // registry is the list of URL schemes we recognize, with some additional
    62. // information about each scheme such as whether it expects a host. The
    63. // scheme registry is not thread-safe, so by default it is locked before any
    64. // threads are created to ensure single-threaded access. An embedder can
    65. // override this to prevent the scheme registry from being locked during
    66. // startup, but if they do so then they are responsible for making sure that
    67. // the registry is only accessed in a thread-safe way, and for calling
    68. // url::LockSchemeRegistries() when initialization is complete. If possible,
    69. // prefer registering additional schemes through
    70. // ContentClient::AddAdditionalSchemes over preventing the scheme registry
    71. // from being locked.
    72. virtual bool ShouldLockSchemeRegistry();
    73. // Allows the embedder to perform platform-specific initialization before
    74. // BrowserMain() is invoked (i.e. before BrowserMainRunner, BrowserMainLoop,
    75. // BrowserMainParts, etc. are created).
    76. virtual void PreBrowserMain() {}
    77. // Returns true if content should create field trials and initialize the
    78. // FeatureList instance for this process. Default implementation returns true.
    79. // Embedders that need to control when and/or how FeatureList should be
    80. // created should override and return false.
    81. virtual bool ShouldCreateFeatureList();
    82. // Creates and returns the VariationsIdsProvider. If null is returned,
    83. // a VariationsIdsProvider is created with a mode of `kUseSignedInState`.
    84. // VariationsIdsProvider is a singleton.
    85. virtual variations::VariationsIdsProvider* CreateVariationsIdsProvider();
    86. // Allows the embedder to perform initialization once field trials/FeatureList
    87. // initialization has completed if ShouldCreateFeatureList() returns true.
    88. // Otherwise, the embedder is responsible for calling this method once feature
    89. // list initialization is complete. Called in every process.
    90. virtual void PostFieldTrialInitialization() {}
    91. // Allows the embedder to perform its own initialization after early content
    92. // initialization. At this point, it is possible to post to base::ThreadPool
    93. // or to the main thread loop via base::ThreadTaskRunnerHandle, but the tasks
    94. // won't run immediately.
    95. //
    96. // If ShouldCreateFeatureList() returns true, the field trials and FeatureList
    97. // have been initialized. Otherwise, the implementation must initialize the
    98. // field trials and FeatureList and call PostFieldTrialInitialization().
    99. //
    100. // |is_running_tests| indicates whether it is running in tests.
    101. virtual void PostEarlyInitialization(bool is_running_tests) {}
    102. #if BUILDFLAG(IS_WIN)
    103. // Allows the embedder to indicate that console control events (e.g., Ctrl-C,
    104. // Ctrl-break, or closure of the console) are to be handled. By default, these
    105. // events are not handled, leading to process termination. When an embedder
    106. // returns true to indicate that these events are to be handled, the
    107. // embedder's ContentBrowserClient::SessionEnding function will be called
    108. // when a console control event is received. All non-browser processes will
    109. // swallow the event.
    110. virtual bool ShouldHandleConsoleControlEvents();
    111. #endif
    112. protected:
    113. friend class ContentClientCreator;
    114. friend class ContentClientInitializer;
    115. friend class BrowserTestBase;
    116. // Called once per relevant process type to allow the embedder to customize
    117. // content. If an embedder wants the default (empty) implementation, don't
    118. // override this.
    119. virtual ContentClient* CreateContentClient();
    120. virtual ContentBrowserClient* CreateContentBrowserClient();
    121. virtual ContentGpuClient* CreateContentGpuClient();
    122. virtual ContentRendererClient* CreateContentRendererClient();
    123. virtual ContentUtilityClient* CreateContentUtilityClient();
    124. };
    125. } // namespace content
    126. #endif // CONTENT_PUBLIC_APP_CONTENT_MAIN_DELEGATE_H_