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 {
32 class ApplicationSystem;
35 namespace extensions {
36 class XWalkExtensionService;
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.
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();
48 virtual ~XWalkRunner();
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.
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.
59 // Scenarios when it is fine to use the accessors:
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.
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();
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_; }
78 // Stages of main parts. See content/browser_main_parts.h for description.
79 void PreMainMessageLoopRun();
80 void PostMainMessageLoopRun();
82 // Get the latest application locale from system.
83 // locale is a langtag defined in [BCP47]
84 virtual std::string GetLocale() const;
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();
94 // Should be used by CreateComponents() implementations.
95 void AddComponent(scoped_ptr<XWalkComponent> component);
97 // These specific factory functions are used to allow ports to customize
99 virtual scoped_ptr<ApplicationComponent> CreateAppComponent();
100 virtual scoped_ptr<SysAppsComponent> CreateSysAppsComponent();
103 friend class XWalkMainDelegate;
104 friend class ::XWalkTestSuiteInitializer;
106 // To track OnRenderProcessHostGone.
107 friend class application::Application;
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;
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);
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();
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();
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_;
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_;
138 ApplicationComponent* app_component_;
140 bool is_running_as_service_;
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);
148 DISALLOW_COPY_AND_ASSIGN(XWalkRunner);
153 #endif // XWALK_RUNTIME_BROWSER_XWALK_RUNNER_H_