09d8b6b72a706e7b3782a88680eecc03d5d085a8
[platform/framework/web/crosswalk.git] / src / xwalk / runtime / browser / xwalk_runner.h
1 // Copyright (c) 2013 Intel Corporation. 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 XWALK_RUNTIME_BROWSER_XWALK_RUNNER_H_
6 #define XWALK_RUNTIME_BROWSER_XWALK_RUNNER_H_
7
8 #include <string>
9
10 #include "base/memory/scoped_ptr.h"
11 #include "base/memory/scoped_vector.h"
12 #include "base/values.h"
13
14 #include "xwalk/runtime/browser/storage_component.h"
15
16 namespace content {
17 class ContentBrowserClient;
18 class RenderProcessHost;
19 }
20
21 class XWalkTestSuiteInitializer;
22
23 namespace xwalk {
24
25 class RuntimeContext;
26 class ApplicationComponent;
27 class SysAppsComponent;
28 class XWalkComponent;
29 class XWalkContentBrowserClient;
30 class XWalkAppExtensionBridge;
31
32 namespace application {
33 class Application;
34 class ApplicationSystem;
35 }
36
37 namespace extensions {
38 class XWalkExtensionService;
39 };
40
41 // Main object for the Browser Process execution in Crosswalk. It is created and
42 // owned by XWalkMainDelegate. It's role is to own, setup and teardown all the
43 // subsystems of Crosswalk.
44 class XWalkRunner {
45  public:
46   // Read the comments below before using this. Relying too much on this
47   // accessor makes the code harder to change and harder to reason about.
48   static XWalkRunner* GetInstance();
49
50   virtual ~XWalkRunner();
51
52   // All sub objects should have their dependencies passed during their
53   // initialization, so that these accessors below are not frequently accessed.
54   // Instead of calling these, consider explicitly passing the dependencies
55   // to the objects that need them.
56   //
57   // For example, if "Application System" needs to use "Runtime Context", we
58   // should pass the "Runtime Context" to "Application System" instead of
59   // making "Application System" ask XWalkRunner for its dependency.
60   //
61   // Scenarios when it is fine to use the accessors:
62   //
63   // - Prototyping solutions, in which we want to see the solution working, and
64   //   all dependencies are still not clear. It avoids writing down a lot of
65   //   code just to test something out.
66   //
67   // - In situations where you don't control the creation of a certain
68   //   object. Certain APIs doesn't allow us to pass the dependencies, so we
69   //   need to reach them some way.
70   RuntimeContext* runtime_context() { return runtime_context_.get(); }
71   application::ApplicationSystem* app_system();
72   extensions::XWalkExtensionService* extension_service() {
73     return extension_service_.get();
74   }
75
76   // Stages of main parts. See content/browser_main_parts.h for description.
77   virtual void PreMainMessageLoopRun();
78   virtual void PostMainMessageLoopRun();
79
80  protected:
81   XWalkRunner();
82
83   // These two hooks should be used to add new port specific
84   // components. Subclasses *must* call the base class implementation.
85   virtual void CreateComponents();
86   virtual void DestroyComponents();
87
88   // Should be used by CreateComponents() implementations.
89   void AddComponent(scoped_ptr<XWalkComponent> component);
90
91   // These specific factory functions are used to allow ports to customize
92   // components.
93   virtual scoped_ptr<ApplicationComponent> CreateAppComponent();
94   virtual scoped_ptr<SysAppsComponent> CreateSysAppsComponent();
95   virtual scoped_ptr<StorageComponent> CreateStorageComponent();
96
97  private:
98   friend class XWalkMainDelegate;
99   friend class ::XWalkTestSuiteInitializer;
100
101   // To track OnRenderProcessHostGone.
102   friend class application::Application;
103
104   // This class acts as an "arm" of XWalkRunner to fulfill Content API needs,
105   // it may call us back in some situations where the a more wider view of the
106   // objects is necessary, e.g. during render process lifecycle callbacks.
107   friend class XWalkContentBrowserClient;
108
109   // We track the render process lifecycle to register Crosswalk
110   // extensions. Some subsystems are mostly implemented using extensions.
111   void OnRenderProcessWillLaunch(content::RenderProcessHost* host);
112   void OnRenderProcessHostGone(content::RenderProcessHost* host);
113
114   // Create the XWalkRunner object. We use a factory function so that we can
115   // switch the concrete class on compile time based on the platform, separating
116   // the per-platform behavior and data in the subclasses.
117   static scoped_ptr<XWalkRunner> Create();
118
119   // Note: this is not public as we want to discourage the rest of Crosswalk to
120   // rely directly on this object.
121   content::ContentBrowserClient* GetContentBrowserClient();
122
123   scoped_ptr<XWalkContentBrowserClient> content_browser_client_;
124   scoped_ptr<RuntimeContext> runtime_context_;
125   scoped_ptr<extensions::XWalkExtensionService> extension_service_;
126   scoped_ptr<XWalkAppExtensionBridge> app_extension_bridge_;
127
128   // XWalkRunner uses the XWalkComponent interface to be able to handle
129   // different subsystems and call them in specific situations, e.g. when
130   // extensions need to be created.
131   ScopedVector<XWalkComponent> components_;
132
133   ApplicationComponent* app_component_;
134
135   // These variables are used to export some values from the browser process
136   // side to the extension side, such as application IDs and whatnot.
137   void InitializeRuntimeVariablesForExtensions(
138       const content::RenderProcessHost* host,
139       base::ValueMap* runtime_variables);
140
141   DISALLOW_COPY_AND_ASSIGN(XWalkRunner);
142 };
143
144 }  // namespace xwalk
145
146 #endif  // XWALK_RUNTIME_BROWSER_XWALK_RUNNER_H_