Chromium Code Reviews| 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. | |
| 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 // specifc code (e.g. Gtk or Aura). | |
|
DaveMoore
2011/10/19 21:31:18
Nit: spelling
| |
| 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 void PreEarlyInitialization(); | |
| 91 virtual void PostEarlyInitialization(); | |
| 92 virtual void PreMainMessageLoopStart(); | |
| 93 virtual void PostMainMessageLoopStart(); | |
| 94 virtual void PreMainMessageLoopRun(); | |
| 95 virtual void PostMainMessageLoopRun(); | |
| 96 }; | |
| 97 | |
| 98 // This is a required implementation of BrowserParts. Override this to provide | |
| 99 // application specific behavior. There can only be one implementation of | |
| 100 // BrowserMainParts. | |
| 101 class CONTENT_EXPORT BrowserMainParts : public BrowserParts { | |
| 71 public: | 102 public: |
| 72 explicit BrowserMainParts(const MainFunctionParams& parameters); | 103 explicit BrowserMainParts(const MainFunctionParams& parameters); |
| 73 virtual ~BrowserMainParts(); | 104 virtual ~BrowserMainParts(); |
| 74 | 105 |
| 106 // Adds an additional BrowserParts instance. | |
| 107 virtual void AddParts(BrowserParts* parts); | |
| 108 | |
| 75 // Parts to be called by |BrowserMain()|. | 109 // Parts to be called by |BrowserMain()|. |
| 76 void EarlyInitialization(); | 110 void EarlyInitialization(); |
| 77 void InitializeToolkit(); | 111 void InitializeToolkit(); |
| 78 void MainMessageLoopStart(); | 112 void MainMessageLoopStart(); |
| 79 void RunMainMessageLoopParts(); | 113 void RunMainMessageLoopParts(); |
| 80 | 114 |
| 81 int result_code() const { return result_code_; } | 115 int result_code() const { return result_code_; } |
| 82 | 116 |
| 83 protected: | 117 protected: |
| 84 // Methods to be overridden to provide platform-specific code; these | 118 // Run main message loop. Invokes MessageLoopForUI::current()->Run unless |
| 85 // correspond to the "parts" above. | 119 // 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(); | 120 virtual void MainMessageLoopRun(); |
| 92 virtual void PostMainMessageLoopRun(); | |
| 93 | 121 |
| 94 // Allows an embedder to do any extra toolkit initialization. | 122 // Allows an embedder to do any extra toolkit initialization. |
| 95 virtual void ToolkitInitialized(); | 123 virtual void ToolkitInitialized(); |
| 96 | 124 |
| 97 // Accessors for data members (below) ---------------------------------------- | 125 // Accessors for data members (below) ---------------------------------------- |
| 98 const MainFunctionParams& parameters() const { | 126 const MainFunctionParams& parameters() const { |
| 99 return parameters_; | 127 return parameters_; |
| 100 } | 128 } |
| 101 const CommandLine& parsed_command_line() const { | 129 const CommandLine& parsed_command_line() const { |
| 102 return parsed_command_line_; | 130 return parsed_command_line_; |
| 103 } | 131 } |
| 104 MessageLoop& main_message_loop() const { | 132 MessageLoop& main_message_loop() const { |
| 105 return *main_message_loop_; | 133 return *main_message_loop_; |
| 106 } | 134 } |
| 107 void set_result_code(int result_code) { result_code_ = result_code; } | 135 void set_result_code(int result_code) { result_code_ = result_code; } |
| 108 | 136 |
| 109 private: | 137 private: |
| 110 void InitializeMainThread(); | 138 void InitializeMainThread(); |
| 111 | 139 |
| 112 // Members initialized on construction --------------------------------------- | 140 // Members initialized on construction --------------------------------------- |
| 113 | 141 |
| 114 const MainFunctionParams& parameters_; | 142 const MainFunctionParams& parameters_; |
| 115 const CommandLine& parsed_command_line_; | 143 const CommandLine& parsed_command_line_; |
| 116 int result_code_; | 144 int result_code_; |
| 117 | 145 |
| 146 // Vector of extra parts added by AddParts after construction ---------------- | |
| 147 // The BrowserParts fucntions for each part are called in the order added. | |
| 148 // They are released (destroyed) in the reverse order. | |
| 149 typedef std::vector<BrowserParts*> PartsList; | |
| 150 PartsList added_parts_; | |
| 151 | |
| 118 // Members initialized in |MainMessageLoopStart()| --------------------------- | 152 // Members initialized in |MainMessageLoopStart()| --------------------------- |
| 119 scoped_ptr<MessageLoop> main_message_loop_; | 153 scoped_ptr<MessageLoop> main_message_loop_; |
| 120 scoped_ptr<base::SystemMonitor> system_monitor_; | 154 scoped_ptr<base::SystemMonitor> system_monitor_; |
| 121 scoped_ptr<HighResolutionTimerManager> hi_res_timer_manager_; | 155 scoped_ptr<HighResolutionTimerManager> hi_res_timer_manager_; |
| 122 scoped_ptr<net::NetworkChangeNotifier> network_change_notifier_; | 156 scoped_ptr<net::NetworkChangeNotifier> network_change_notifier_; |
| 123 scoped_ptr<BrowserThread> main_thread_; | 157 scoped_ptr<BrowserThread> main_thread_; |
| 124 | 158 |
| 125 DISALLOW_COPY_AND_ASSIGN(BrowserMainParts); | 159 DISALLOW_COPY_AND_ASSIGN(BrowserMainParts); |
| 126 }; | 160 }; |
| 127 | 161 |
| 128 bool ExitedMainMessageLoop(); | 162 bool ExitedMainMessageLoop(); |
| 129 | 163 |
| 130 } // namespace content | 164 } // namespace content |
| 131 | 165 |
| 132 CONTENT_EXPORT int BrowserMain(const MainFunctionParams& parameters); | 166 CONTENT_EXPORT int BrowserMain(const MainFunctionParams& parameters); |
| 133 | 167 |
| 134 #endif // CONTENT_BROWSER_BROWSER_MAIN_H_ | 168 #endif // CONTENT_BROWSER_BROWSER_MAIN_H_ |
| OLD | NEW |