8a2de0a18c9ae8bdcda4c4d89b154babee820f97
[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   // Return true if Crosswalk is running in service mode, i.e. taking
77   // requests from native IPC mechanism to launch applications.
78   bool is_running_as_service() const { return is_running_as_service_; }
79
80   // Stages of main parts. See content/browser_main_parts.h for description.
81   virtual void PreMainMessageLoopRun();
82   virtual void PostMainMessageLoopRun();
83
84   // Get the latest application locale from system.
85   // locale is a langtag defined in [BCP47]
86   virtual std::string GetLocale() const;
87
88  protected:
89   XWalkRunner();
90
91   // These two hooks should be used to add new port specific
92   // components. Subclasses *must* call the base class implementation.
93   virtual void CreateComponents();
94   virtual void DestroyComponents();
95
96   // Should be used by CreateComponents() implementations.
97   void AddComponent(scoped_ptr<XWalkComponent> component);
98
99   // These specific factory functions are used to allow ports to customize
100   // components.
101   virtual scoped_ptr<ApplicationComponent> CreateAppComponent();
102   virtual scoped_ptr<SysAppsComponent> CreateSysAppsComponent();
103   virtual scoped_ptr<StorageComponent> CreateStorageComponent();
104
105  private:
106   friend class XWalkMainDelegate;
107   friend class ::XWalkTestSuiteInitializer;
108
109   // To track OnRenderProcessHostGone.
110   friend class application::Application;
111
112   // This class acts as an "arm" of XWalkRunner to fulfill Content API needs,
113   // it may call us back in some situations where the a more wider view of the
114   // objects is necessary, e.g. during render process lifecycle callbacks.
115   friend class XWalkContentBrowserClient;
116
117   // We track the render process lifecycle to register Crosswalk
118   // extensions. Some subsystems are mostly implemented using extensions.
119   void OnRenderProcessWillLaunch(content::RenderProcessHost* host);
120   void OnRenderProcessHostGone(content::RenderProcessHost* host);
121
122   // Create the XWalkRunner object. We use a factory function so that we can
123   // switch the concrete class on compile time based on the platform, separating
124   // the per-platform behavior and data in the subclasses.
125   static scoped_ptr<XWalkRunner> Create();
126
127   // Note: this is not public as we want to discourage the rest of Crosswalk to
128   // rely directly on this object.
129   content::ContentBrowserClient* GetContentBrowserClient();
130
131   scoped_ptr<XWalkContentBrowserClient> content_browser_client_;
132   scoped_ptr<RuntimeContext> runtime_context_;
133   scoped_ptr<extensions::XWalkExtensionService> extension_service_;
134   scoped_ptr<XWalkAppExtensionBridge> app_extension_bridge_;
135
136   // XWalkRunner uses the XWalkComponent interface to be able to handle
137   // different subsystems and call them in specific situations, e.g. when
138   // extensions need to be created.
139   ScopedVector<XWalkComponent> components_;
140
141   ApplicationComponent* app_component_;
142
143   bool is_running_as_service_;
144
145   // These variables are used to export some values from the browser process
146   // side to the extension side, such as application IDs and whatnot.
147   void InitializeRuntimeVariablesForExtensions(
148       const content::RenderProcessHost* host,
149       base::ValueMap& runtime_variables);
150
151   DISALLOW_COPY_AND_ASSIGN(XWalkRunner);
152 };
153
154 }  // namespace xwalk
155
156 #endif  // XWALK_RUNTIME_BROWSER_XWALK_RUNNER_H_