1 // Copyright 2015 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 
5 #ifndef HEADLESS_LIB_HEADLESS_CONTENT_MAIN_DELEGATE_H_
6 #define HEADLESS_LIB_HEADLESS_CONTENT_MAIN_DELEGATE_H_
7 
8 #include <memory>
9 #include <string>
10 
11 #include "base/compiler_specific.h"
12 #include "base/macros.h"
13 #include "build/build_config.h"
14 #include "content/public/app/content_main_delegate.h"
15 #include "content/public/browser/content_browser_client.h"
16 #include "content/public/renderer/content_renderer_client.h"
17 #include "headless/lib/headless_content_client.h"
18 #include "headless/public/headless_browser.h"
19 #include "headless/public/headless_export.h"
20 
21 #if !defined(CHROME_MULTIPLE_DLL_CHILD)
22 #include "headless/lib/browser/headless_platform_event_source.h"
23 #endif
24 
25 namespace base {
26 namespace debug {
27 struct CrashKeyString;
28 }  // namespace debug
29 class CommandLine;
30 }  // namespace base
31 
32 namespace headless {
33 
34 class HeadlessBrowserImpl;
35 
36 // Exported for tests.
37 class HEADLESS_EXPORT HeadlessContentMainDelegate
38     : public content::ContentMainDelegate {
39  public:
40   explicit HeadlessContentMainDelegate(
41       std::unique_ptr<HeadlessBrowserImpl> browser);
42   explicit HeadlessContentMainDelegate(HeadlessBrowser::Options options);
43   ~HeadlessContentMainDelegate() override;
44 
45   // content::ContentMainDelegate implementation:
46   bool BasicStartupComplete(int* exit_code) override;
47   void PreSandboxStartup() override;
48   int RunProcess(
49       const std::string& process_type,
50       const content::MainFunctionParams& main_function_params) override;
51 #if defined(OS_MACOSX)
52   void PreCreateMainMessageLoop() override;
53 #endif
54   content::ContentClient* CreateContentClient() override;
55   content::ContentBrowserClient* CreateContentBrowserClient() override;
56   content::ContentUtilityClient* CreateContentUtilityClient() override;
57   content::ContentRendererClient* CreateContentRendererClient() override;
58 
59   void PostEarlyInitialization(bool is_running_tests) override;
60 
browser()61   HeadlessBrowserImpl* browser() const { return browser_.get(); }
62 
63 #if defined(OS_LINUX) || defined(OS_BSD)
64   void ZygoteForked() override;
65 #endif
66 
67  private:
68   friend class HeadlessBrowserTest;
69 
70   void Init();
71 
72   HeadlessBrowser::Options* options();
73 
74   static HeadlessContentMainDelegate* GetInstance();
75 
76   void InitLogging(const base::CommandLine& command_line);
77   void InitCrashReporter(const base::CommandLine& command_line);
78 
79   std::unique_ptr<content::ContentRendererClient> renderer_client_;
80   std::unique_ptr<content::ContentBrowserClient> browser_client_;
81   std::unique_ptr<content::ContentUtilityClient> utility_client_;
82   HeadlessContentClient content_client_;
83 #if !defined(CHROME_MULTIPLE_DLL_CHILD)
84   HeadlessPlatformEventSource platform_event_source_;
85 #endif
86 
87   std::unique_ptr<HeadlessBrowserImpl> browser_;
88   std::unique_ptr<HeadlessBrowser::Options> options_;
89 
90   base::debug::CrashKeyString* headless_crash_key_;  // Note: never deallocated.
91 
92   DISALLOW_COPY_AND_ASSIGN(HeadlessContentMainDelegate);
93 };
94 
95 }  // namespace headless
96 
97 #endif  // HEADLESS_LIB_HEADLESS_CONTENT_MAIN_DELEGATE_H_
98