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.
5 #ifndef XWALK_RUNTIME_BROWSER_XWALK_RUNNER_H_
6 #define XWALK_RUNTIME_BROWSER_XWALK_RUNNER_H_
10 #include "base/memory/scoped_ptr.h"
11 #include "base/memory/scoped_vector.h"
12 #include "base/values.h"
15 class ContentBrowserClient;
16 class RenderProcessHost;
19 class XWalkTestSuiteInitializer;
24 class ApplicationComponent;
25 class SysAppsComponent;
27 class XWalkContentBrowserClient;
28 class XWalkAppExtensionBridge;
30 namespace application {
31 class ApplicationSystem;
34 namespace extensions {
35 class XWalkExtensionService;
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.
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();
47 virtual ~XWalkRunner();
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.
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.
58 // Scenarios when it is fine to use the accessors:
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.
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();
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_; }
77 // Stages of main parts. See content/browser_main_parts.h for description.
78 void PreMainMessageLoopRun();
79 void PostMainMessageLoopRun();
81 // Get the latest application locale from system.
82 // locale is a langtag defined in [BCP47]
83 virtual std::string GetLocale() const;
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();
93 // Should be used by CreateComponents() implementations.
94 void AddComponent(scoped_ptr<XWalkComponent> component);
96 // These specific factory functions are used to allow ports to customize
98 virtual scoped_ptr<ApplicationComponent> CreateAppComponent();
99 virtual scoped_ptr<SysAppsComponent> CreateSysAppsComponent();
102 friend class XWalkMainDelegate;
103 friend class ::XWalkTestSuiteInitializer;
105 // To track OnRenderProcessHostGone.
106 friend class Runtime;
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;
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);
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();
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();
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_;
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_;
137 ApplicationComponent* app_component_;
139 bool is_running_as_service_;
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);
147 DISALLOW_COPY_AND_ASSIGN(XWalkRunner);
152 #endif // XWALK_RUNTIME_BROWSER_XWALK_RUNNER_H_