OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CONTENT_BROWSER_BROWSER_MAIN_H_ | 5 #ifndef CONTENT_BROWSER_BROWSER_MAIN_H_ |
6 #define CONTENT_BROWSER_BROWSER_MAIN_H_ | 6 #define CONTENT_BROWSER_BROWSER_MAIN_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <vector> | |
10 | |
9 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
10 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
11 #include "content/common/content_export.h" | 13 #include "content/common/content_export.h" |
12 | 14 |
13 class BrowserThread; | 15 class BrowserThread; |
14 class CommandLine; | 16 class CommandLine; |
15 class HighResolutionTimerManager; | 17 class HighResolutionTimerManager; |
16 class MessageLoop; | 18 class MessageLoop; |
17 struct MainFunctionParams; | 19 struct MainFunctionParams; |
18 | 20 |
19 namespace base { | 21 namespace base { |
20 class SystemMonitor; | 22 class SystemMonitor; |
21 } | 23 } |
22 | 24 |
23 namespace net { | 25 namespace net { |
24 class NetworkChangeNotifier; | 26 class NetworkChangeNotifier; |
25 } | 27 } |
26 | 28 |
27 namespace content { | 29 namespace content { |
28 | 30 |
29 // BrowserMainParts: | 31 // These classes contains singleton member variables specific to the browser |
30 // This class contains different "stages" to be executed in |BrowserMain()|, | 32 // process, and virtual methods representing different "stages" to be executed |
31 // mostly initialization. This is made into a class rather than just functions | 33 // from |BrowserMain()|, mostly to do initialization and shutdown. |
32 // so each stage can create and maintain state. Each part is represented by a | 34 // |
33 // single method (e.g., "EarlyInitialization()"), which does the following: | 35 // There are two types of "Parts": |
36 // * BrowserMainParts : There must be exactly one instance derived from this | |
37 // class. It can override any of the BrowserParts methods, in addition to | |
38 // MainMessageLoopRun() and other "main" methods. | |
39 // * BrowserParts: There can be any number of additional "Parts" added to | |
40 // the BrowserMainParts instance with AddParts(). Each stage will be called | |
41 // for these parts in the order they were added. Destruction is in the | |
42 // inverse order. | |
jam
2011/10/20 18:01:19
While I understand why you did it, the concept of
stevenjb
2011/10/21 22:25:49
After some conversation and a closer look at brows
| |
43 // | |
44 // Each stage is represented by a single BrowserMainParts method | |
45 // (e.g., "EarlyInitialization()"), which does the following: | |
34 // - calls a method (e.g., "PreEarlyInitialization()") which individual | 46 // - calls a method (e.g., "PreEarlyInitialization()") which individual |
35 // platforms can override to provide platform-specific code which is to be | 47 // platforms can override to provide platform-specific code which is to be |
36 // executed before the common code; | 48 // executed before the common code; |
49 // - calls the Pre method on the list of added parts to do additional component | |
50 // specific code (e.g. Gtk or Aura). | |
37 // - calls various methods for things common to all platforms (for that given | 51 // - calls various methods for things common to all platforms (for that given |
38 // stage); and | 52 // stage) |
39 // - calls a method (e.g., "PostEarlyInitialization()") for platform-specific | 53 // - calls a method (e.g., "PostEarlyInitialization()") for platform-specific |
40 // code to be called after the common code. | 54 // code to be called after the common code. |
55 // -- calls the Post method for added parts. | |
56 // | |
41 // As indicated above, platforms should override the default "Pre...()" and | 57 // As indicated above, platforms should override the default "Pre...()" and |
42 // "Post...()" methods when necessary; they need not call the superclass's | 58 // "Post...()" methods when necessary; they need not call the superclass's |
43 // implementation (which is empty). | 59 // implementation (which is empty). |
44 // | 60 // |
45 // Parts: | 61 // Parts: |
46 // - EarlyInitialization: things which should be done as soon as possible on | 62 // - EarlyInitialization: things which should be done as soon as possible on |
47 // program start (such as setting up signal handlers) and things to be done | 63 // program start (such as setting up signal handlers) and things to be done |
48 // at some generic time before the start of the main message loop. | 64 // at some generic time before the start of the main message loop. |
49 // - MainMessageLoopStart: things beginning with the start of the main message | 65 // - MainMessageLoopStart: things beginning with the start of the main message |
50 // loop and ending with initialization of the main thread; platform-specific | 66 // loop and ending with initialization of the main thread; platform-specific |
51 // things which should be done immediately before the start of the main | 67 // things which should be done immediately before the start of the main |
52 // message loop should go in |PreMainMessageLoopStart()|. | 68 // message loop should go in |PreMainMessageLoopStart()|. |
53 // - (more to come) | 69 // - RunMainMessageLoopParts: things to be done before and after invoking the |
70 // main message loop run method (e.g. MessageLoopForUI::current()->Run()). | |
54 // | 71 // |
55 // How to add stuff (to existing parts): | 72 // How to add stuff (to existing parts): |
56 // - Figure out when your new code should be executed. What must happen | 73 // - Figure out when your new code should be executed. What must happen |
57 // before/after your code is executed? Are there performance reasons for | 74 // before/after your code is executed? Are there performance reasons for |
58 // running your code at a particular time? Document these things! | 75 // running your code at a particular time? Document these things! |
59 // - Split out any platform-specific bits. Please avoid #ifdefs it at all | 76 // - Split out any platform-specific bits. Please avoid #ifdefs it at all |
60 // possible. You have two choices for platform-specific code: (1) Execute it | 77 // possible. You have two choices for platform-specific code: (1) Execute it |
61 // from one of the platform-specific |Pre/Post...()| methods; do this if the | 78 // from one of the platform-specific |Pre/Post...()| methods; do this if the |
62 // code is unique to a platform type. Or (2) execute it from one of the | 79 // code is unique to a platform type. Or (2) execute it from one of the |
63 // "parts" (e.g., |EarlyInitialization()|) and provide platform-specific | 80 // "parts" (e.g., |EarlyInitialization()|) and provide platform-specific |
64 // implementations of your code (in a virtual method); do this if you need to | 81 // implementations of your code (in a virtual method); do this if you need to |
65 // provide different implementations across most/all platforms. | 82 // provide different implementations across most/all platforms. |
66 // - Unless your new code is just one or two lines, put it into a separate | 83 // - Unless your new code is just one or two lines, put it into a separate |
67 // method with a well-defined purpose. (Likewise, if you're adding to an | 84 // method with a well-defined purpose. (Likewise, if you're adding to an |
68 // existing chunk which makes it longer than one or two lines, please move | 85 // existing chunk which makes it longer than one or two lines, please move |
69 // the code out into a separate method.) | 86 // the code out into a separate method.) |
70 class CONTENT_EXPORT BrowserMainParts { | 87 |
88 class CONTENT_EXPORT BrowserParts { | |
89 public: | |
90 virtual ~BrowserParts(); | |
91 | |
92 virtual void PreEarlyInitialization(); | |
93 virtual void PostEarlyInitialization(); | |
94 virtual void PreMainMessageLoopStart(); | |
95 virtual void PostMainMessageLoopStart(); | |
96 virtual void PreMainMessageLoopRun(); | |
97 virtual void PostMainMessageLoopRun(); | |
98 }; | |
99 | |
100 // This is a required implementation of BrowserParts. Override this to provide | |
101 // application specific behavior. There can only be one implementation of | |
102 // BrowserMainParts. | |
103 class CONTENT_EXPORT BrowserMainParts : public BrowserParts { | |
71 public: | 104 public: |
72 explicit BrowserMainParts(const MainFunctionParams& parameters); | 105 explicit BrowserMainParts(const MainFunctionParams& parameters); |
73 virtual ~BrowserMainParts(); | 106 virtual ~BrowserMainParts(); |
74 | 107 |
108 // Adds an additional BrowserParts instance. | |
109 virtual void AddParts(BrowserParts* parts); | |
110 | |
75 // Parts to be called by |BrowserMain()|. | 111 // Parts to be called by |BrowserMain()|. |
76 void EarlyInitialization(); | 112 void EarlyInitialization(); |
77 void InitializeToolkit(); | 113 void InitializeToolkit(); |
78 void MainMessageLoopStart(); | 114 void MainMessageLoopStart(); |
79 void RunMainMessageLoopParts(); | 115 void RunMainMessageLoopParts(); |
80 | 116 |
81 int result_code() const { return result_code_; } | 117 int result_code() const { return result_code_; } |
82 | 118 |
83 protected: | 119 protected: |
84 // Methods to be overridden to provide platform-specific code; these | 120 // Run main message loop. Invokes MessageLoopForUI::current()->Run unless |
85 // correspond to the "parts" above. | 121 // overridden. |
86 virtual void PreEarlyInitialization(); | |
87 virtual void PostEarlyInitialization(); | |
88 virtual void PreMainMessageLoopStart(); | |
89 virtual void PostMainMessageLoopStart(); | |
90 virtual void PreMainMessageLoopRun(); | |
91 virtual void MainMessageLoopRun(); | 122 virtual void MainMessageLoopRun(); |
92 virtual void PostMainMessageLoopRun(); | |
93 | 123 |
94 // Allows an embedder to do any extra toolkit initialization. | 124 // Allows an embedder to do any extra toolkit initialization. |
95 virtual void ToolkitInitialized(); | 125 virtual void ToolkitInitialized(); |
96 | 126 |
97 // Accessors for data members (below) ---------------------------------------- | 127 // Accessors for data members (below) ---------------------------------------- |
98 const MainFunctionParams& parameters() const { | 128 const MainFunctionParams& parameters() const { |
99 return parameters_; | 129 return parameters_; |
100 } | 130 } |
101 const CommandLine& parsed_command_line() const { | 131 const CommandLine& parsed_command_line() const { |
102 return parsed_command_line_; | 132 return parsed_command_line_; |
103 } | 133 } |
104 MessageLoop& main_message_loop() const { | 134 MessageLoop& main_message_loop() const { |
105 return *main_message_loop_; | 135 return *main_message_loop_; |
106 } | 136 } |
107 void set_result_code(int result_code) { result_code_ = result_code; } | 137 void set_result_code(int result_code) { result_code_ = result_code; } |
108 | 138 |
109 private: | 139 private: |
110 void InitializeMainThread(); | 140 void InitializeMainThread(); |
111 | 141 |
112 // Members initialized on construction --------------------------------------- | 142 // Members initialized on construction --------------------------------------- |
113 | 143 |
114 const MainFunctionParams& parameters_; | 144 const MainFunctionParams& parameters_; |
115 const CommandLine& parsed_command_line_; | 145 const CommandLine& parsed_command_line_; |
116 int result_code_; | 146 int result_code_; |
117 | 147 |
148 // Vector of extra parts added by AddParts after construction ---------------- | |
149 // The BrowserParts fucntions for each part are called in the order added. | |
150 // They are released (destroyed) in the reverse order. | |
151 typedef std::vector<BrowserParts*> PartsList; | |
152 PartsList added_parts_; | |
153 | |
118 // Members initialized in |MainMessageLoopStart()| --------------------------- | 154 // Members initialized in |MainMessageLoopStart()| --------------------------- |
119 scoped_ptr<MessageLoop> main_message_loop_; | 155 scoped_ptr<MessageLoop> main_message_loop_; |
120 scoped_ptr<base::SystemMonitor> system_monitor_; | 156 scoped_ptr<base::SystemMonitor> system_monitor_; |
121 scoped_ptr<HighResolutionTimerManager> hi_res_timer_manager_; | 157 scoped_ptr<HighResolutionTimerManager> hi_res_timer_manager_; |
122 scoped_ptr<net::NetworkChangeNotifier> network_change_notifier_; | 158 scoped_ptr<net::NetworkChangeNotifier> network_change_notifier_; |
123 scoped_ptr<BrowserThread> main_thread_; | 159 scoped_ptr<BrowserThread> main_thread_; |
124 | 160 |
125 DISALLOW_COPY_AND_ASSIGN(BrowserMainParts); | 161 DISALLOW_COPY_AND_ASSIGN(BrowserMainParts); |
126 }; | 162 }; |
127 | 163 |
128 bool ExitedMainMessageLoop(); | 164 bool ExitedMainMessageLoop(); |
129 | 165 |
130 } // namespace content | 166 } // namespace content |
131 | 167 |
132 CONTENT_EXPORT int BrowserMain(const MainFunctionParams& parameters); | 168 CONTENT_EXPORT int BrowserMain(const MainFunctionParams& parameters); |
133 | 169 |
134 #endif // CONTENT_BROWSER_BROWSER_MAIN_H_ | 170 #endif // CONTENT_BROWSER_BROWSER_MAIN_H_ |
171 | |
172 // LocalWords: BrowserMain BrowserMainParts BrowserParts MainMessageLoopRun | |
173 // LocalWords: AddParts EarlyInitialization PreEarlyInitialization Pre Gtk | |
174 // LocalWords: PostEarlyInitialization superclass's MainMessageLoopStart | |
175 // LocalWords: PreMainMessageLoopStart RunMainMessageLoopParts ifdefs | |
OLD | NEW |