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