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