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 
5 #ifndef CONTENT_PUBLIC_BROWSER_BROWSER_MAIN_PARTS_H_
6 #define CONTENT_PUBLIC_BROWSER_BROWSER_MAIN_PARTS_H_
7 
8 #include "base/callback.h"
9 #include "content/common/content_export.h"
10 
11 namespace content {
12 
13 // This class contains different "stages" to be executed by |BrowserMain()|,
14 // Each stage is represented by a single BrowserMainParts method, called from
15 // the corresponding method in |BrowserMainLoop| (e.g., EarlyInitialization())
16 // which does the following:
17 //  - calls a method (e.g., "PreEarlyInitialization()") which implements
18 //    platform / tookit specific code for that stage.
19 //  - calls various methods for things common to all platforms (for that stage).
20 //  - calls a method (e.g., "PostEarlyInitialization()") for platform-specific
21 //    code to be called after the common code.
22 //
23 // Stages:
24 //  - EarlyInitialization: things which should be done as soon as possible on
25 //    program start (such as setting up signal handlers) and things to be done
26 //    at some generic time before the start of the main message loop.
27 //  - MainMessageLoopStart: things beginning with the start of the main message
28 //    loop and ending with initialization of the main thread; platform-specific
29 //    things which should be done immediately before the start of the main
30 //    message loop should go in |PreMainMessageLoopStart()|.
31 //  - RunMainMessageLoopParts:  things to be done before and after invoking the
32 //    main message loop run method (e.g. MessageLoopCurrentForUI::Get()->Run()).
33 //
34 // How to add stuff (to existing parts):
35 //  - Figure out when your new code should be executed. What must happen
36 //    before/after your code is executed? Are there performance reasons for
37 //    running your code at a particular time? Document these things!
38 //  - Split out any platform-specific bits. Please avoid #ifdefs it at all
39 //    possible. You have two choices for platform-specific code: (1) Execute it
40 //    from one of the |Pre/Post...()| methods in a embedder's platform-specific
41 //    override (e.g., ChromeBrowserMainPartsWin::PreMainMessageLoopStart()); do
42 //    this if the code is unique to an embedder and platform type. Or (2)
43 //    execute it from one of the "stages" (e.g.,
44 //    |BrowserMainLoop::EarlyInitialization()|) and provide platform-specific
45 //    implementations of your code (in a virtual method); do this if you need to
46 //    provide different implementations across most/all platforms.
47 //  - Unless your new code is just one or two lines, put it into a separate
48 //    method with a well-defined purpose. (Likewise, if you're adding to an
49 //    existing chunk which makes it longer than one or two lines, please move
50 //    the code out into a separate method.)
51 //
52 class CONTENT_EXPORT BrowserMainParts {
53  public:
BrowserMainParts()54   BrowserMainParts() {}
~BrowserMainParts()55   virtual ~BrowserMainParts() {}
56 
57   // A return value other than RESULT_CODE_NORMAL_EXIT indicates error and is
58   // used as the exit status.
59   virtual int PreEarlyInitialization();
60 
PostEarlyInitialization()61   virtual void PostEarlyInitialization() {}
62 
PreMainMessageLoopStart()63   virtual void PreMainMessageLoopStart() {}
64 
PostMainMessageLoopStart()65   virtual void PostMainMessageLoopStart() {}
66 
67   // Allows an embedder to do any extra toolkit initialization.
ToolkitInitialized()68   virtual void ToolkitInitialized() {}
69 
70   // Called just before any child threads owned by the content
71   // framework are created.
72   //
73   // The main message loop has been started at this point (but has not
74   // been run), and the toolkit has been initialized. Returns the error code
75   // (or 0 if no error).
76   virtual int PreCreateThreads();
77 
78   // This is called right after all child threads owned by the content framework
79   // are created.
PostCreateThreads()80   virtual void PostCreateThreads() {}
81 
82   // This is called just before the main message loop is run.  The
83   // various browser threads have all been created at this point
PreMainMessageLoopRun()84   virtual void PreMainMessageLoopRun() {}
85 
86   // Returns true if the message loop was run, false otherwise.
87   // If this returns false, the default implementation will be run.
88   // May set |result_code|, which will be returned by |BrowserMain()|.
89   virtual bool MainMessageLoopRun(int* result_code);
90 
91   // Provides an embedder with a Closure which will quit the default main
92   // message loop. This is call only if MainMessageLoopRun returns false.
PreDefaultMainMessageLoopRun(base::OnceClosure quit_closure)93   virtual void PreDefaultMainMessageLoopRun(base::OnceClosure quit_closure) {}
94 
95   // This happens after the main message loop has stopped, but before
96   // threads are stopped.
PostMainMessageLoopRun()97   virtual void PostMainMessageLoopRun() {}
98 
99   // Called as the very last part of shutdown, after threads have been
100   // stopped and destroyed.
PostDestroyThreads()101   virtual void PostDestroyThreads() {}
102 };
103 
104 }  // namespace content
105 
106 #endif  // CONTENT_PUBLIC_BROWSER_BROWSER_MAIN_PARTS_H_
107