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